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 7e8004ba 2022-09-11 thomas struct commit_queue *real_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;
364 7e8004ba 2022-09-11 thomas int *limiting;
365 7e8004ba 2022-09-11 thomas int limit_match;
366 7e8004ba 2022-09-11 thomas regex_t *limit_regex;
367 7e8004ba 2022-09-11 thomas struct commit_queue *limit_commits;
370 1a76625f 2018-10-22 stsp struct tog_log_view_state {
371 7e8004ba 2022-09-11 thomas struct commit_queue *commits;
372 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
373 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
374 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
375 7e8004ba 2022-09-11 thomas struct commit_queue real_commits;
376 b01e7d3b 2018-08-04 stsp int selected;
377 b01e7d3b 2018-08-04 stsp char *in_repo_path;
378 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
379 b672a97a 2020-01-27 stsp int log_branches;
380 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
381 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
382 1a76625f 2018-10-22 stsp sig_atomic_t quit;
383 1a76625f 2018-10-22 stsp pthread_t thread;
384 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
385 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
386 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
387 11b20872 2019-11-08 stsp struct tog_colors colors;
388 f69c5a46 2022-07-19 thomas int use_committer;
389 7e8004ba 2022-09-11 thomas int limit_view;
390 7e8004ba 2022-09-11 thomas regex_t limit_regex;
391 7e8004ba 2022-09-11 thomas struct commit_queue limit_commits;
394 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
395 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
396 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
397 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
398 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
399 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
400 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
401 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
402 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
403 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
404 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
405 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
406 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
407 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
408 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
410 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
411 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
412 e9424729 2018-08-04 stsp int nlines;
414 e9424729 2018-08-04 stsp struct tog_view *view;
415 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
419 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
420 e9424729 2018-08-04 stsp const char *path;
421 e9424729 2018-08-04 stsp struct got_repository *repo;
422 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
423 e9424729 2018-08-04 stsp int *complete;
424 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
425 fc06ba56 2019-08-22 stsp void *cancel_arg;
428 e9424729 2018-08-04 stsp struct tog_blame {
430 be659d10 2020-11-18 stsp off_t filesize;
431 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
432 6fcac457 2018-11-19 stsp int nlines;
433 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
434 e9424729 2018-08-04 stsp pthread_t thread;
435 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
436 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
437 e9424729 2018-08-04 stsp const char *path;
438 7cd52833 2022-06-23 thomas int *pack_fds;
441 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
442 7cbe629d 2018-08-04 stsp int first_displayed_line;
443 7cbe629d 2018-08-04 stsp int last_displayed_line;
444 7cbe629d 2018-08-04 stsp int selected_line;
445 4fc71f3b 2022-07-12 thomas int last_diffed_line;
446 7cbe629d 2018-08-04 stsp int blame_complete;
449 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
450 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
451 e5a0f69f 2018-08-18 stsp char *path;
452 7cbe629d 2018-08-04 stsp struct got_repository *repo;
453 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
454 2a31b33b 2022-07-23 thomas struct got_object_id *id_to_log;
455 e9424729 2018-08-04 stsp struct tog_blame blame;
456 6c4c42e0 2019-06-24 stsp int matched_line;
457 11b20872 2019-11-08 stsp struct tog_colors colors;
460 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
461 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
462 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
463 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
464 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
465 ad80ab7b 2018-08-04 stsp int selected;
468 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
470 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
471 ad80ab7b 2018-08-04 stsp char *tree_label;
472 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
473 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
474 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
475 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
476 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
477 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
478 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
479 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
480 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
481 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
482 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
483 6458efa5 2020-11-24 stsp struct tog_colors colors;
486 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
487 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
488 6458efa5 2020-11-24 stsp struct got_reference *ref;
492 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
494 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
495 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
496 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
497 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
498 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
499 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
500 6458efa5 2020-11-24 stsp struct got_repository *repo;
501 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
502 bddb1296 2019-11-08 stsp struct tog_colors colors;
506 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
508 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
509 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
510 669b5ffa 2018-10-07 stsp * there is enough screen estate.
512 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
513 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
515 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
516 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
517 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
519 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
520 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
522 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
524 cc3c9aac 2018-08-01 stsp struct tog_view {
525 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
526 26ed57b2 2018-05-19 stsp WINDOW *window;
527 26ed57b2 2018-05-19 stsp PANEL *panel;
528 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
529 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
530 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
531 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
532 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
533 07dd3ed3 2022-08-06 thomas int gline, hiline; /* navigate to and highlight this nG line */
534 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
535 ea0bff04 2022-07-19 thomas int resized; /* set when in a resize event */
536 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
538 669b5ffa 2018-10-07 stsp struct tog_view *parent;
539 669b5ffa 2018-10-07 stsp struct tog_view *child;
542 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
543 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
544 e78dc838 2020-12-04 stsp * between parent and child.
545 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
546 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
547 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
548 e78dc838 2020-12-04 stsp * situations.
550 e78dc838 2020-12-04 stsp int focus_child;
552 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
553 5dc9f4bc 2018-08-04 stsp /* type-specific state */
554 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
556 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
557 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
558 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
559 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
560 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
563 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
564 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
565 e78dc838 2020-12-04 stsp struct tog_view *, int);
566 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
567 ea0bff04 2022-07-19 thomas const struct got_error *(*resize)(struct tog_view *, int);
568 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
570 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
571 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
572 c0c4acc8 2021-01-24 stsp int search_started;
573 60493ae3 2019-06-20 stsp int searching;
574 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
575 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
576 60493ae3 2019-06-20 stsp int search_next_done;
577 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
578 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
579 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
580 1803e47f 2019-06-22 stsp regex_t regex;
581 41605754 2020-11-12 stsp regmatch_t regmatch;
584 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
585 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
586 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
587 78756c87 2020-11-24 stsp struct got_repository *);
588 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
589 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
590 e78dc838 2020-12-04 stsp struct tog_view *, int);
591 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
592 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
593 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
594 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
596 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
597 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
598 78756c87 2020-11-24 stsp const char *, const char *, int);
599 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
600 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
601 e78dc838 2020-12-04 stsp struct tog_view *, int);
602 ea0bff04 2022-07-19 thomas static const struct got_error *resize_log_view(struct tog_view *, int);
603 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
604 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
605 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
607 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
608 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
609 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
610 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
611 e78dc838 2020-12-04 stsp struct tog_view *, int);
612 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
613 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
614 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
615 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
617 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
618 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
619 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
620 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
621 e78dc838 2020-12-04 stsp struct tog_view *, int);
622 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
623 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
624 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
626 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
627 6458efa5 2020-11-24 stsp struct got_repository *);
628 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
629 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
630 e78dc838 2020-12-04 stsp struct tog_view *, int);
631 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
632 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
633 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
635 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
636 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
637 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
638 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
639 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
641 25791caa 2018-10-24 stsp static void
642 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
644 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
647 83baff54 2019-08-12 stsp static void
648 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
650 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
653 61266923 2020-01-14 stsp static void
654 61266923 2020-01-14 stsp tog_sigcont(int signo)
656 61266923 2020-01-14 stsp tog_sigcont_received = 1;
659 296152d1 2022-05-31 thomas static void
660 296152d1 2022-05-31 thomas tog_sigint(int signo)
662 296152d1 2022-05-31 thomas tog_sigint_received = 1;
665 296152d1 2022-05-31 thomas static void
666 296152d1 2022-05-31 thomas tog_sigterm(int signo)
668 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
671 296152d1 2022-05-31 thomas static int
672 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
674 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
675 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
678 e5a0f69f 2018-08-18 stsp static const struct got_error *
679 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
681 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
683 669b5ffa 2018-10-07 stsp if (view->child) {
684 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
685 669b5ffa 2018-10-07 stsp view->child = NULL;
687 e5a0f69f 2018-08-18 stsp if (view->close)
688 e5a0f69f 2018-08-18 stsp err = view->close(view);
689 ea5e7bb5 2018-08-01 stsp if (view->panel)
690 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
691 ea5e7bb5 2018-08-01 stsp if (view->window)
692 ea5e7bb5 2018-08-01 stsp delwin(view->window);
693 ea5e7bb5 2018-08-01 stsp free(view);
694 f2d749db 2022-07-12 thomas return err ? err : child_err;
697 ea5e7bb5 2018-08-01 stsp static struct tog_view *
698 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
699 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
701 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
703 ea5e7bb5 2018-08-01 stsp if (view == NULL)
704 ea5e7bb5 2018-08-01 stsp return NULL;
706 d6b05b5b 2018-08-04 stsp view->type = type;
707 f7d12f7e 2018-08-01 stsp view->lines = LINES;
708 f7d12f7e 2018-08-01 stsp view->cols = COLS;
709 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
710 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
711 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
712 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
713 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
714 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
715 96a765a8 2018-08-04 stsp view_close(view);
716 ea5e7bb5 2018-08-01 stsp return NULL;
718 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
719 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
720 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
721 96a765a8 2018-08-04 stsp view_close(view);
722 ea5e7bb5 2018-08-01 stsp return NULL;
725 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
726 ea5e7bb5 2018-08-01 stsp return view;
730 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
732 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
734 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
737 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
738 a5d43cac 2022-07-01 thomas static int
739 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
741 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
744 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
746 5c60c32a 2018-10-18 stsp static const struct got_error *
747 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
749 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
751 ea0bff04 2022-07-19 thomas if (!view->resized && view->mode == TOG_VIEW_SPLIT_HRZN) {
752 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
753 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
755 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
756 a5d43cac 2022-07-01 thomas view->begin_x = 0;
757 ea0bff04 2022-07-19 thomas } else if (!view->resized) {
758 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
759 53d2bdd3 2022-07-10 thomas view->cols > 119)
760 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
762 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
763 a5d43cac 2022-07-01 thomas view->begin_y = 0;
765 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
766 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
767 5c60c32a 2018-10-18 stsp view->lines = LINES;
768 5c60c32a 2018-10-18 stsp view->cols = COLS;
769 5c60c32a 2018-10-18 stsp err = view_resize(view);
771 5c60c32a 2018-10-18 stsp return err;
773 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
774 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
776 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
777 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
779 5c60c32a 2018-10-18 stsp return NULL;
782 5c60c32a 2018-10-18 stsp static const struct got_error *
783 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
785 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
787 5c60c32a 2018-10-18 stsp view->begin_x = 0;
788 ea0bff04 2022-07-19 thomas view->begin_y = view->resized ? view->begin_y : 0;
789 ea0bff04 2022-07-19 thomas view->nlines = view->resized ? view->nlines : LINES;
790 5c60c32a 2018-10-18 stsp view->ncols = COLS;
791 5c60c32a 2018-10-18 stsp view->lines = LINES;
792 5c60c32a 2018-10-18 stsp view->cols = COLS;
793 5c60c32a 2018-10-18 stsp err = view_resize(view);
795 5c60c32a 2018-10-18 stsp return err;
797 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
798 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
800 5c60c32a 2018-10-18 stsp return NULL;
804 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
806 5c60c32a 2018-10-18 stsp return view->parent == NULL;
809 b65b3ea0 2022-06-23 thomas static int
810 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
812 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
815 e44940c3 2022-07-01 thomas static int
816 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
818 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
821 444d5325 2022-07-03 thomas static int
822 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
824 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
825 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
828 a5d43cac 2022-07-01 thomas static void
829 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
831 a5d43cac 2022-07-01 thomas PANEL *panel;
832 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
834 a5d43cac 2022-07-01 thomas if (view->parent)
835 a5d43cac 2022-07-01 thomas return view_border(view->parent);
837 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
838 a5d43cac 2022-07-01 thomas if (panel == NULL)
841 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
842 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
843 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
844 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
845 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
847 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
848 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
851 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
852 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
853 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
854 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
855 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
857 4d8c2215 2018-08-19 stsp static const struct got_error *
858 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
860 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
861 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
863 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
865 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
866 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
868 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
869 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
870 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
872 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
874 4918811f 2022-07-01 thomas if (view->child) {
875 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
877 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
878 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
879 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
880 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
881 40236d76 2022-06-23 thomas ncols = COLS;
883 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
884 5c60c32a 2018-10-18 stsp if (view->child->focussed)
885 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
887 5c60c32a 2018-10-18 stsp show_panel(view->panel);
889 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
891 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
892 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
895 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
896 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
897 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
899 a5d43cac 2022-07-01 thomas if (hs) {
900 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
902 a5d43cac 2022-07-01 thomas return err;
903 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
904 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
906 a5d43cac 2022-07-01 thomas return err;
908 a5d43cac 2022-07-01 thomas view_border(view);
909 a5d43cac 2022-07-01 thomas update_panels();
910 a5d43cac 2022-07-01 thomas doupdate();
911 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
912 a5d43cac 2022-07-01 thomas nlines = view->nlines;
914 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
915 40236d76 2022-06-23 thomas ncols = COLS;
917 ea0bff04 2022-07-19 thomas if (view->resize && dif > 0) {
918 ea0bff04 2022-07-19 thomas err = view->resize(view, dif);
920 ea0bff04 2022-07-19 thomas return err;
923 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
924 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
925 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
926 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
927 40236d76 2022-06-23 thomas wclear(view->window);
929 40236d76 2022-06-23 thomas view->nlines = nlines;
930 40236d76 2022-06-23 thomas view->ncols = ncols;
931 40236d76 2022-06-23 thomas view->lines = LINES;
932 40236d76 2022-06-23 thomas view->cols = COLS;
934 5c60c32a 2018-10-18 stsp return NULL;
937 ea0bff04 2022-07-19 thomas static const struct got_error *
938 ea0bff04 2022-07-19 thomas resize_log_view(struct tog_view *view, int increase)
940 3a0139e8 2022-07-22 thomas struct tog_log_view_state *s = &view->state.log;
941 3a0139e8 2022-07-22 thomas const struct got_error *err = NULL;
942 3a0139e8 2022-07-22 thomas int n = 0;
944 3a0139e8 2022-07-22 thomas if (s->selected_entry)
945 3a0139e8 2022-07-22 thomas n = s->selected_entry->idx + view->lines - s->selected;
948 ea0bff04 2022-07-19 thomas * Request commits to account for the increased
949 ea0bff04 2022-07-19 thomas * height so we have enough to populate the view.
951 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < n) {
952 7e8004ba 2022-09-11 thomas view->nscrolled = n - s->commits->ncommits + increase + 1;
953 ea0bff04 2022-07-19 thomas err = request_log_commits(view);
956 ea0bff04 2022-07-19 thomas return err;
959 97cb21cd 2022-07-12 thomas static void
960 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
962 97cb21cd 2022-07-12 thomas if (n == 0)
965 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
966 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
967 97cb21cd 2022-07-12 thomas view->parent->offset += n;
969 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
970 97cb21cd 2022-07-12 thomas } else if (view->offset) {
971 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
972 97cb21cd 2022-07-12 thomas view->offset -= n;
974 97cb21cd 2022-07-12 thomas view->offset = 0;
978 53d2bdd3 2022-07-10 thomas static const struct got_error *
979 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
981 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
982 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
984 53d2bdd3 2022-07-10 thomas if (view->parent)
985 53d2bdd3 2022-07-10 thomas v = view->parent;
987 53d2bdd3 2022-07-10 thomas v = view;
989 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
990 53d2bdd3 2022-07-10 thomas return NULL;
992 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = resize; /* lock for resize event */
994 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
995 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
996 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
997 53d2bdd3 2022-07-10 thomas if (view->parent)
998 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
1000 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
1001 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
1002 53d2bdd3 2022-07-10 thomas view->count = 0;
1003 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
1004 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
1005 53d2bdd3 2022-07-10 thomas view->count = 0;
1006 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
1008 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
1009 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
1010 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
1011 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
1012 53d2bdd3 2022-07-10 thomas if (err)
1013 53d2bdd3 2022-07-10 thomas return err;
1014 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
1015 53d2bdd3 2022-07-10 thomas } else {
1016 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
1017 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1018 53d2bdd3 2022-07-10 thomas if (view->parent)
1019 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1021 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1022 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1023 53d2bdd3 2022-07-10 thomas view->count = 0;
1024 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1025 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1026 53d2bdd3 2022-07-10 thomas view->count = 0;
1027 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1029 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1032 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1033 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1034 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1035 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1037 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1038 53d2bdd3 2022-07-10 thomas if (err)
1039 53d2bdd3 2022-07-10 thomas return err;
1040 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1041 53d2bdd3 2022-07-10 thomas if (err)
1042 53d2bdd3 2022-07-10 thomas return err;
1044 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1045 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1046 53d2bdd3 2022-07-10 thomas if (err)
1047 53d2bdd3 2022-07-10 thomas return err;
1050 3a0139e8 2022-07-22 thomas if (v->resize)
1051 3a0139e8 2022-07-22 thomas err = v->resize(v, 0);
1052 3a0139e8 2022-07-22 thomas else if (v->child->resize)
1053 3a0139e8 2022-07-22 thomas err = v->child->resize(v->child, 0);
1055 ea0bff04 2022-07-19 thomas v->resized = v->child->resized = 0;
1057 53d2bdd3 2022-07-10 thomas return err;
1060 53d2bdd3 2022-07-10 thomas static void
1061 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1063 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1065 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1066 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1069 669b5ffa 2018-10-07 stsp static const struct got_error *
1070 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1072 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1074 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1075 669b5ffa 2018-10-07 stsp return NULL;
1077 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1078 669b5ffa 2018-10-07 stsp view->child = NULL;
1079 669b5ffa 2018-10-07 stsp return err;
1082 40236d76 2022-06-23 thomas static const struct got_error *
1083 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1085 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1087 669b5ffa 2018-10-07 stsp view->child = child;
1088 669b5ffa 2018-10-07 stsp child->parent = view;
1090 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1091 53d2bdd3 2022-07-10 thomas if (err)
1092 53d2bdd3 2022-07-10 thomas return err;
1094 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1095 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1097 53d2bdd3 2022-07-10 thomas return err;
1100 2a31b33b 2022-07-23 thomas static const struct got_error *view_dispatch_request(struct tog_view **,
1101 2a31b33b 2022-07-23 thomas struct tog_view *, enum tog_view_type, int, int);
1103 2a31b33b 2022-07-23 thomas static const struct got_error *
1104 2a31b33b 2022-07-23 thomas view_request_new(struct tog_view **requested, struct tog_view *view,
1105 2a31b33b 2022-07-23 thomas enum tog_view_type request)
1107 2a31b33b 2022-07-23 thomas struct tog_view *new_view = NULL;
1108 2a31b33b 2022-07-23 thomas const struct got_error *err;
1109 2a31b33b 2022-07-23 thomas int y = 0, x = 0;
1111 2a31b33b 2022-07-23 thomas *requested = NULL;
1113 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view))
1114 2a31b33b 2022-07-23 thomas view_get_split(view, &y, &x);
1116 2a31b33b 2022-07-23 thomas err = view_dispatch_request(&new_view, view, request, y, x);
1117 2a31b33b 2022-07-23 thomas if (err)
1118 2a31b33b 2022-07-23 thomas return err;
1120 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view) && view->mode == TOG_VIEW_SPLIT_HRZN) {
1121 2a31b33b 2022-07-23 thomas err = view_init_hsplit(view, y);
1122 2a31b33b 2022-07-23 thomas if (err)
1123 2a31b33b 2022-07-23 thomas return err;
1126 2a31b33b 2022-07-23 thomas view->focussed = 0;
1127 2a31b33b 2022-07-23 thomas new_view->focussed = 1;
1128 2a31b33b 2022-07-23 thomas new_view->mode = view->mode;
1129 2a31b33b 2022-07-23 thomas new_view->nlines = view->lines - y;
1131 2a31b33b 2022-07-23 thomas if (view_is_parent_view(view)) {
1132 2a31b33b 2022-07-23 thomas view_transfer_size(new_view, view);
1133 2a31b33b 2022-07-23 thomas err = view_close_child(view);
1134 2a31b33b 2022-07-23 thomas if (err)
1135 2a31b33b 2022-07-23 thomas return err;
1136 2a31b33b 2022-07-23 thomas err = view_set_child(view, new_view);
1137 2a31b33b 2022-07-23 thomas if (err)
1138 2a31b33b 2022-07-23 thomas return err;
1139 2a31b33b 2022-07-23 thomas view->focus_child = 1;
1141 2a31b33b 2022-07-23 thomas *requested = new_view;
1143 2a31b33b 2022-07-23 thomas return NULL;
1146 34bc9ec9 2019-02-22 stsp static void
1147 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1149 25791caa 2018-10-24 stsp int cols, lines;
1150 25791caa 2018-10-24 stsp struct winsize size;
1152 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1153 25791caa 2018-10-24 stsp cols = 80; /* Default */
1154 25791caa 2018-10-24 stsp lines = 24;
1156 25791caa 2018-10-24 stsp cols = size.ws_col;
1157 25791caa 2018-10-24 stsp lines = size.ws_row;
1159 25791caa 2018-10-24 stsp resize_term(lines, cols);
1162 2b49a8ae 2019-06-22 stsp static const struct got_error *
1163 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1165 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1166 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1167 2b49a8ae 2019-06-22 stsp char pattern[1024];
1170 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1171 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1172 c0c4acc8 2021-01-24 stsp view->searching = 0;
1173 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1175 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1177 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1178 2b49a8ae 2019-06-22 stsp return NULL;
1180 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1181 a5d43cac 2022-07-01 thomas v = view->child;
1183 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1184 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1186 634cb454 2022-07-03 thomas nodelay(view->window, FALSE); /* block for search term input */
1187 2b49a8ae 2019-06-22 stsp nocbreak();
1189 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1190 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1193 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1194 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1195 2b49a8ae 2019-06-22 stsp return NULL;
1197 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1198 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1199 7c32bd05 2019-06-22 stsp if (err) {
1200 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1201 7c32bd05 2019-06-22 stsp return err;
1203 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1204 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1205 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1206 2b49a8ae 2019-06-22 stsp view->search_next(view);
1209 2b49a8ae 2019-06-22 stsp return NULL;
1212 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1213 64486692 2022-07-07 thomas static const struct got_error *
1214 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1216 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1217 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1219 64486692 2022-07-07 thomas if (view->parent)
1220 64486692 2022-07-07 thomas v = view->parent;
1222 64486692 2022-07-07 thomas v = view;
1224 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1225 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1227 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1229 ddbc4d37 2022-07-12 thomas if (!v->child)
1230 ddbc4d37 2022-07-12 thomas return NULL;
1231 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1232 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1234 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1235 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1236 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1237 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1238 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1241 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1242 64486692 2022-07-07 thomas v->ncols = COLS;
1243 64486692 2022-07-07 thomas v->child->ncols = COLS;
1244 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1246 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1247 64486692 2022-07-07 thomas if (err)
1248 64486692 2022-07-07 thomas return err;
1250 64486692 2022-07-07 thomas v->child->mode = v->mode;
1251 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1252 64486692 2022-07-07 thomas v->focus_child = 1;
1254 64486692 2022-07-07 thomas err = view_fullscreen(v);
1255 64486692 2022-07-07 thomas if (err)
1256 64486692 2022-07-07 thomas return err;
1257 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1258 64486692 2022-07-07 thomas if (err)
1259 64486692 2022-07-07 thomas return err;
1261 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1262 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1263 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1264 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1265 cf1fe301 2022-07-29 thomas if (err)
1266 cf1fe301 2022-07-29 thomas return err;
1267 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1268 cf1fe301 2022-07-29 thomas if (err)
1269 cf1fe301 2022-07-29 thomas return err;
1270 ddbc4d37 2022-07-12 thomas } else {
1271 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1272 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1274 f4e6231a 2022-07-22 thomas if (v->resize)
1275 f4e6231a 2022-07-22 thomas err = v->resize(v, 0);
1276 f4e6231a 2022-07-22 thomas else if (v->child->resize)
1277 f4e6231a 2022-07-22 thomas err = v->child->resize(v->child, 0);
1279 64486692 2022-07-07 thomas return err;
1283 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1284 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1286 07b0611c 2022-06-23 thomas static int
1287 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1289 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1290 a5d43cac 2022-07-01 thomas int x, n = 0;
1292 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1293 a5d43cac 2022-07-01 thomas v = view->child;
1294 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1295 a5d43cac 2022-07-01 thomas v = view->parent;
1297 07b0611c 2022-06-23 thomas view->count = 0;
1298 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1299 07dd3ed3 2022-08-06 thomas nodelay(view->window, FALSE);
1300 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1301 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1302 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1305 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1306 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1307 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1308 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1312 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1313 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1315 07b0611c 2022-06-23 thomas if (n >= 9999999)
1316 07b0611c 2022-06-23 thomas n = 9999999;
1318 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1319 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1321 07dd3ed3 2022-08-06 thomas if (c == 'G' || c == 'g') { /* nG key map */
1322 07dd3ed3 2022-08-06 thomas view->gline = view->hiline = n;
1327 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1328 07b0611c 2022-06-23 thomas view->count = n;
1330 07b0611c 2022-06-23 thomas return c;
1333 0cf4efb1 2018-09-29 stsp static const struct got_error *
1334 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1335 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1337 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1338 669b5ffa 2018-10-07 stsp struct tog_view *v;
1339 1a76625f 2018-10-22 stsp int ch, errcode;
1341 e5a0f69f 2018-08-18 stsp *new = NULL;
1343 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1344 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1345 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1346 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1347 07b0611c 2022-06-23 thomas view->count = 0;
1350 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1351 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1352 82954512 2020-02-03 stsp if (errcode)
1353 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1354 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1355 3da8ef85 2021-09-21 stsp sched_yield();
1356 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1357 82954512 2020-02-03 stsp if (errcode)
1358 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1359 82954512 2020-02-03 stsp "pthread_mutex_lock");
1360 60493ae3 2019-06-20 stsp view->search_next(view);
1361 60493ae3 2019-06-20 stsp return NULL;
1364 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1365 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1366 1a76625f 2018-10-22 stsp if (errcode)
1367 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1368 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1369 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1370 634cb454 2022-07-03 thomas cbreak();
1371 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1372 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1373 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1374 634cb454 2022-07-03 thomas view->count = 0;
1376 634cb454 2022-07-03 thomas ch = view->ch;
1377 634cb454 2022-07-03 thomas } else {
1378 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1379 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1380 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1382 07dd3ed3 2022-08-06 thomas if (view->hiline && ch != ERR && ch != 0)
1383 07dd3ed3 2022-08-06 thomas view->hiline = 0; /* key pressed, clear line highlight */
1384 07dd3ed3 2022-08-06 thomas nodelay(view->window, TRUE);
1385 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1386 1a76625f 2018-10-22 stsp if (errcode)
1387 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1389 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1390 25791caa 2018-10-24 stsp tog_resizeterm();
1391 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1392 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1393 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1394 25791caa 2018-10-24 stsp err = view_resize(v);
1396 25791caa 2018-10-24 stsp return err;
1397 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1399 25791caa 2018-10-24 stsp return err;
1400 cdfcfb03 2020-12-06 stsp if (v->child) {
1401 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1403 cdfcfb03 2020-12-06 stsp return err;
1404 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1405 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1407 cdfcfb03 2020-12-06 stsp return err;
1408 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1409 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1410 53d2bdd3 2022-07-10 thomas if (err)
1411 53d2bdd3 2022-07-10 thomas return err;
1417 e5a0f69f 2018-08-18 stsp switch (ch) {
1419 07b0611c 2022-06-23 thomas view->count = 0;
1420 1e37a5c2 2019-05-12 jcs if (view->child) {
1421 e78dc838 2020-12-04 stsp view->focussed = 0;
1422 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1423 e78dc838 2020-12-04 stsp view->focus_child = 1;
1424 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1425 e78dc838 2020-12-04 stsp view->focussed = 0;
1426 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1427 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1428 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1429 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1430 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent,
1432 a5d43cac 2022-07-01 thomas if (err)
1433 a5d43cac 2022-07-01 thomas return err;
1435 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1436 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1437 a5d43cac 2022-07-01 thomas if (err)
1438 a5d43cac 2022-07-01 thomas return err;
1443 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1444 3a0139e8 2022-07-22 thomas if (view->parent->resize) {
1445 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1446 3a0139e8 2022-07-22 thomas err = view->parent->resize(view->parent, 0);
1447 a5d43cac 2022-07-01 thomas if (err)
1450 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1452 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1453 9970f7fc 2020-12-03 stsp view->dying = 1;
1458 1c5e5faa 2022-06-23 thomas case 'F':
1459 07b0611c 2022-06-23 thomas view->count = 0;
1460 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1461 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1463 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1464 e78dc838 2020-12-04 stsp view->focussed = 0;
1465 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1466 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1467 53d2bdd3 2022-07-10 thomas } else {
1468 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1469 53d2bdd3 2022-07-10 thomas if (!err)
1470 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1474 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1475 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1477 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1478 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1479 e78dc838 2020-12-04 stsp view->focussed = 1;
1480 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1482 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1483 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1484 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1485 53d2bdd3 2022-07-10 thomas if (!err)
1486 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1490 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1492 a5d43cac 2022-07-01 thomas if (err)
1494 3a0139e8 2022-07-22 thomas if (view->resize) {
1495 3a0139e8 2022-07-22 thomas err = view->resize(view, 0);
1496 a5d43cac 2022-07-01 thomas if (err)
1499 a5d43cac 2022-07-01 thomas if (view->parent)
1500 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1501 a5d43cac 2022-07-01 thomas if (!err)
1502 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1504 64486692 2022-07-07 thomas case 'S':
1505 53d2bdd3 2022-07-10 thomas view->count = 0;
1506 64486692 2022-07-07 thomas err = switch_split(view);
1508 53d2bdd3 2022-07-10 thomas case '-':
1509 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1511 53d2bdd3 2022-07-10 thomas case '+':
1512 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1514 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1517 07b0611c 2022-06-23 thomas view->count = 0;
1518 60493ae3 2019-06-20 stsp if (view->search_start)
1519 2b49a8ae 2019-06-22 stsp view_search_start(view);
1521 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1525 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1526 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1527 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1528 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1529 60493ae3 2019-06-20 stsp view->search_next(view);
1531 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1533 adf4c9e0 2022-07-03 thomas case 'A':
1534 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1535 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1537 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1538 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1539 adf4c9e0 2022-07-03 thomas if (v->reset) {
1540 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1541 adf4c9e0 2022-07-03 thomas if (err)
1542 adf4c9e0 2022-07-03 thomas return err;
1544 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1545 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1546 adf4c9e0 2022-07-03 thomas if (err)
1547 adf4c9e0 2022-07-03 thomas return err;
1552 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1556 e5a0f69f 2018-08-18 stsp return err;
1559 ef20f542 2022-06-26 thomas static int
1560 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1562 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1563 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1565 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1567 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1570 669b5ffa 2018-10-07 stsp return view->focussed;
1573 bcbd79e2 2018-08-19 stsp static const struct got_error *
1574 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1576 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1577 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1578 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1579 64486692 2022-07-07 thomas char *mode;
1580 fd823528 2018-10-22 stsp int fast_refresh = 10;
1581 1a76625f 2018-10-22 stsp int done = 0, errcode;
1583 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1584 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1585 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1587 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1589 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1590 1a76625f 2018-10-22 stsp if (errcode)
1591 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1593 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1594 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1596 1004088d 2018-09-29 stsp view->focussed = 1;
1597 878940b7 2018-09-29 stsp err = view->show(view);
1599 0cf4efb1 2018-09-29 stsp return err;
1600 0cf4efb1 2018-09-29 stsp update_panels();
1601 0cf4efb1 2018-09-29 stsp doupdate();
1602 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1603 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1604 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1605 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1606 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1608 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1611 9970f7fc 2020-12-03 stsp if (view->dying) {
1612 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1614 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1615 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1617 e78dc838 2020-12-04 stsp else if (view->parent)
1618 669b5ffa 2018-10-07 stsp prev = view->parent;
1620 e78dc838 2020-12-04 stsp if (view->parent) {
1621 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1622 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1623 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1624 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1625 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1626 40236d76 2022-06-23 thomas if (err)
1628 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1629 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1631 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1633 9970f7fc 2020-12-03 stsp err = view_close(view);
1635 e5a0f69f 2018-08-18 stsp goto done;
1637 e78dc838 2020-12-04 stsp view = NULL;
1638 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1639 e78dc838 2020-12-04 stsp if (v->focussed)
1642 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1643 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1645 e78dc838 2020-12-04 stsp view = prev;
1646 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1647 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1648 e78dc838 2020-12-04 stsp tog_view_list_head);
1650 e78dc838 2020-12-04 stsp if (view) {
1651 e78dc838 2020-12-04 stsp if (view->focus_child) {
1652 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1653 e78dc838 2020-12-04 stsp view = view->child;
1655 e78dc838 2020-12-04 stsp view->focussed = 1;
1659 bcbd79e2 2018-08-19 stsp if (new_view) {
1660 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1661 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1662 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1663 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1665 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1666 86c66b02 2018-10-18 stsp err = view_close(v);
1668 86c66b02 2018-10-18 stsp goto done;
1671 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1672 fed7eaa8 2018-10-24 stsp view = new_view;
1674 669b5ffa 2018-10-07 stsp if (view) {
1675 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1676 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1677 e78dc838 2020-12-04 stsp view = view->child;
1679 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1680 e78dc838 2020-12-04 stsp view = view->parent;
1682 e78dc838 2020-12-04 stsp show_panel(view->panel);
1683 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1684 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1685 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1686 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1688 1a76625f 2018-10-22 stsp goto done;
1690 669b5ffa 2018-10-07 stsp err = view->show(view);
1692 1a76625f 2018-10-22 stsp goto done;
1693 669b5ffa 2018-10-07 stsp if (view->child) {
1694 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1696 1a76625f 2018-10-22 stsp goto done;
1698 1a76625f 2018-10-22 stsp update_panels();
1699 1a76625f 2018-10-22 stsp doupdate();
1703 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1704 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1705 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1706 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1707 f2d749db 2022-07-12 thomas close_err = view_close(view);
1708 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1709 f2d749db 2022-07-12 thomas err = close_err;
1712 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1713 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1714 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1716 e5a0f69f 2018-08-18 stsp return err;
1719 4ed7e80c 2018-05-20 stsp __dead static void
1720 9f7d7167 2018-04-29 stsp usage_log(void)
1723 c70c5802 2018-08-01 stsp fprintf(stderr,
1724 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1725 9f7d7167 2018-04-29 stsp getprogname());
1729 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1730 80ddbec8 2018-04-29 stsp static const struct got_error *
1731 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1733 00dfcb92 2018-06-11 stsp char *vis = NULL;
1734 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1736 963b370f 2018-05-20 stsp *ws = NULL;
1737 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1738 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1739 00dfcb92 2018-06-11 stsp int vislen;
1740 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1741 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1743 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1744 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1746 00dfcb92 2018-06-11 stsp return err;
1747 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1748 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1749 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1750 a7f50699 2018-06-11 stsp goto done;
1754 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1755 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1756 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1757 a7f50699 2018-06-11 stsp goto done;
1760 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1761 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1763 00dfcb92 2018-06-11 stsp free(vis);
1764 963b370f 2018-05-20 stsp if (err) {
1765 963b370f 2018-05-20 stsp free(*ws);
1766 963b370f 2018-05-20 stsp *ws = NULL;
1767 963b370f 2018-05-20 stsp *wlen = 0;
1769 963b370f 2018-05-20 stsp return err;
1772 05171be4 2022-06-23 thomas static const struct got_error *
1773 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1775 05171be4 2022-06-23 thomas char *dst;
1776 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1778 05171be4 2022-06-23 thomas *ptr = NULL;
1779 05171be4 2022-06-23 thomas n = len = strlen(src);
1780 83316834 2022-06-23 thomas dst = malloc(n + 1);
1781 05171be4 2022-06-23 thomas if (dst == NULL)
1782 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1784 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1785 05171be4 2022-06-23 thomas const char c = src[idx];
1787 05171be4 2022-06-23 thomas if (c == '\t') {
1788 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1789 b235ad5d 2022-06-23 thomas char *p;
1791 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1792 83316834 2022-06-23 thomas if (p == NULL) {
1793 83316834 2022-06-23 thomas free(dst);
1794 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1797 83316834 2022-06-23 thomas dst = p;
1798 05171be4 2022-06-23 thomas n += nb;
1799 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1800 05171be4 2022-06-23 thomas sz += nb;
1802 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1806 05171be4 2022-06-23 thomas dst[sz] = '\0';
1807 05171be4 2022-06-23 thomas *ptr = dst;
1808 05171be4 2022-06-23 thomas return NULL;
1812 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1813 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1814 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1815 8d208d34 2022-06-23 thomas * *rcol.
1817 8d208d34 2022-06-23 thomas static int
1818 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1820 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1822 8d208d34 2022-06-23 thomas if (n == 0) {
1823 8d208d34 2022-06-23 thomas *rcol = cols;
1824 8d208d34 2022-06-23 thomas return off;
1827 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1828 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1829 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1831 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1833 8d208d34 2022-06-23 thomas if (width == -1) {
1834 8d208d34 2022-06-23 thomas width = 1;
1835 8d208d34 2022-06-23 thomas wline[i] = L'.';
1838 8d208d34 2022-06-23 thomas if (cols + width > n)
1840 8d208d34 2022-06-23 thomas cols += width;
1843 8d208d34 2022-06-23 thomas *rcol = cols;
1844 8d208d34 2022-06-23 thomas return i;
1848 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1849 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1850 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1852 f91a2b48 2022-06-23 thomas static const struct got_error *
1853 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1854 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1856 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1857 8d208d34 2022-06-23 thomas int cols;
1858 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1859 05171be4 2022-06-23 thomas char *exstr = NULL;
1860 963b370f 2018-05-20 stsp size_t wlen;
1861 8d208d34 2022-06-23 thomas int i, scrollx;
1863 963b370f 2018-05-20 stsp *wlinep = NULL;
1864 b700b5d6 2018-07-10 stsp *widthp = 0;
1866 05171be4 2022-06-23 thomas if (expand) {
1867 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1868 05171be4 2022-06-23 thomas if (err)
1869 05171be4 2022-06-23 thomas return err;
1872 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1873 05171be4 2022-06-23 thomas free(exstr);
1875 963b370f 2018-05-20 stsp return err;
1877 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1879 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1880 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1883 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1884 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1888 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1889 8d208d34 2022-06-23 thomas wline[i] = L'\0';
1891 b700b5d6 2018-07-10 stsp if (widthp)
1892 b700b5d6 2018-07-10 stsp *widthp = cols;
1893 f91a2b48 2022-06-23 thomas if (scrollxp)
1894 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1896 963b370f 2018-05-20 stsp free(wline);
1898 963b370f 2018-05-20 stsp *wlinep = wline;
1899 963b370f 2018-05-20 stsp return err;
1902 8b473291 2019-02-21 stsp static const struct got_error*
1903 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1904 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1906 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1907 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1909 8b473291 2019-02-21 stsp const char *name;
1911 8b473291 2019-02-21 stsp *refs_str = NULL;
1913 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1914 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1915 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1918 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1919 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1921 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1922 8b473291 2019-02-21 stsp name += 5;
1923 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1924 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1926 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1927 8b473291 2019-02-21 stsp name += 6;
1928 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1929 8b473291 2019-02-21 stsp name += 8;
1930 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1931 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1934 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1937 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1938 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1939 5d844a1e 2019-08-13 stsp if (err) {
1940 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1941 48cae60d 2020-09-22 stsp free(ref_id);
1944 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1945 5d844a1e 2019-08-13 stsp err = NULL;
1946 5d844a1e 2019-08-13 stsp tag = NULL;
1949 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1950 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1951 48cae60d 2020-09-22 stsp free(ref_id);
1953 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1954 52b5abe1 2019-08-13 stsp if (cmp != 0)
1956 8b473291 2019-02-21 stsp s = *refs_str;
1957 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1958 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1959 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1961 8b473291 2019-02-21 stsp *refs_str = NULL;
1967 8b473291 2019-02-21 stsp return err;
1970 963b370f 2018-05-20 stsp static const struct got_error *
1971 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1972 27a741e5 2019-09-11 stsp int col_tab_align)
1974 e6b8b890 2020-12-29 naddy char *smallerthan;
1976 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1977 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1978 5813d178 2019-03-09 stsp author = smallerthan + 1;
1979 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1980 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1981 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1984 5813d178 2019-03-09 stsp static const struct got_error *
1985 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1986 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1987 8fdc79fe 2020-12-01 naddy int author_display_cols)
1989 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1990 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1991 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1992 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1993 5813d178 2019-03-09 stsp char *author = NULL;
1994 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1995 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1996 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1997 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1998 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1999 ccb26ccd 2018-11-05 stsp struct tm tm;
2000 45d799e2 2018-12-23 stsp time_t committer_time;
2001 11b20872 2019-11-08 stsp struct tog_color *tc;
2003 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2004 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
2005 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
2006 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
2007 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
2009 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
2010 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
2012 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
2013 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
2015 11b20872 2019-11-08 stsp wattr_on(view->window,
2016 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2017 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
2019 11b20872 2019-11-08 stsp wattr_off(view->window,
2020 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2021 27a741e5 2019-09-11 stsp col = limit;
2022 b39d25c7 2018-07-10 stsp if (col > avail)
2023 b39d25c7 2018-07-10 stsp goto done;
2025 6570a66d 2019-11-08 stsp if (avail >= 120) {
2026 6570a66d 2019-11-08 stsp char *id_str;
2027 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
2029 6570a66d 2019-11-08 stsp goto done;
2030 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2032 11b20872 2019-11-08 stsp wattr_on(view->window,
2033 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2034 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
2036 11b20872 2019-11-08 stsp wattr_off(view->window,
2037 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2038 6570a66d 2019-11-08 stsp free(id_str);
2040 6570a66d 2019-11-08 stsp if (col > avail)
2041 6570a66d 2019-11-08 stsp goto done;
2044 f69c5a46 2022-07-19 thomas if (s->use_committer)
2045 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(commit));
2047 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(commit));
2048 5813d178 2019-03-09 stsp if (author == NULL) {
2049 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2050 80ddbec8 2018-04-29 stsp goto done;
2052 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
2054 bb737323 2018-05-20 stsp goto done;
2055 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
2057 11b20872 2019-11-08 stsp wattr_on(view->window,
2058 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2059 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
2060 bb737323 2018-05-20 stsp col += author_width;
2061 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
2062 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2064 bb737323 2018-05-20 stsp author_width++;
2067 f31d6c3b 2022-09-09 thomas wattr_off(view->window,
2068 f31d6c3b 2022-09-09 thomas COLOR_PAIR(tc->colorpair), NULL);
2069 9c2eaf34 2018-05-20 stsp if (col > avail)
2070 9c2eaf34 2018-05-20 stsp goto done;
2072 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2074 6d9fbc00 2018-04-29 stsp goto done;
2075 bb737323 2018-05-20 stsp logmsg = logmsg0;
2076 bb737323 2018-05-20 stsp while (*logmsg == '\n')
2078 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
2079 bb737323 2018-05-20 stsp if (newline)
2080 bb737323 2018-05-20 stsp *newline = '\0';
2081 f91a2b48 2022-06-23 thomas limit = avail - col;
2082 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2083 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2084 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2085 f91a2b48 2022-06-23 thomas limit, col, 1);
2086 331b1a16 2022-06-23 thomas if (err)
2087 331b1a16 2022-06-23 thomas goto done;
2088 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2089 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2090 27a741e5 2019-09-11 stsp while (col < avail) {
2091 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2095 80ddbec8 2018-04-29 stsp free(logmsg0);
2096 bb737323 2018-05-20 stsp free(wlogmsg);
2097 5813d178 2019-03-09 stsp free(author);
2098 bb737323 2018-05-20 stsp free(wauthor);
2099 80ddbec8 2018-04-29 stsp free(line);
2100 80ddbec8 2018-04-29 stsp return err;
2103 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2104 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2105 899d86c2 2018-05-10 stsp struct got_object_id *id)
2107 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2108 d68b9737 2022-09-05 thomas struct got_object_id *dup;
2110 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2111 80ddbec8 2018-04-29 stsp if (entry == NULL)
2112 899d86c2 2018-05-10 stsp return NULL;
2114 d68b9737 2022-09-05 thomas dup = got_object_id_dup(id);
2115 d68b9737 2022-09-05 thomas if (dup == NULL) {
2116 d68b9737 2022-09-05 thomas free(entry);
2117 d68b9737 2022-09-05 thomas return NULL;
2120 d68b9737 2022-09-05 thomas entry->id = dup;
2121 99db9666 2018-05-07 stsp entry->commit = commit;
2122 899d86c2 2018-05-10 stsp return entry;
2125 99db9666 2018-05-07 stsp static void
2126 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2128 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2130 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2131 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2132 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2133 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2134 d68b9737 2022-09-05 thomas free(entry->id);
2135 99db9666 2018-05-07 stsp free(entry);
2138 99db9666 2018-05-07 stsp static void
2139 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2141 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2142 99db9666 2018-05-07 stsp pop_commit(commits);
2145 c4972b91 2018-05-07 stsp static const struct got_error *
2146 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2147 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2149 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2150 13add988 2019-10-15 stsp regmatch_t regmatch;
2151 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2153 13add988 2019-10-15 stsp *have_match = 0;
2155 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2157 13add988 2019-10-15 stsp return err;
2159 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2161 13add988 2019-10-15 stsp goto done;
2163 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2164 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2165 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2166 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2167 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2168 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2169 13add988 2019-10-15 stsp *have_match = 1;
2171 13add988 2019-10-15 stsp free(id_str);
2172 13add988 2019-10-15 stsp free(logmsg);
2173 13add988 2019-10-15 stsp return err;
2176 13add988 2019-10-15 stsp static const struct got_error *
2177 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2179 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2182 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2183 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2184 1a76625f 2018-10-22 stsp * while updating the display.
2187 7210b715 2022-09-11 thomas struct got_object_id id;
2188 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2189 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2190 7e8004ba 2022-09-11 thomas int limit_match = 0;
2191 1a76625f 2018-10-22 stsp int errcode;
2193 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2194 4e0d2870 2020-12-07 naddy NULL, NULL);
2195 7210b715 2022-09-11 thomas if (err)
2198 7210b715 2022-09-11 thomas err = got_object_open_as_commit(&commit, a->repo, &id);
2201 7210b715 2022-09-11 thomas entry = alloc_commit_queue_entry(commit, &id);
2202 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2203 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2207 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2208 1a76625f 2018-10-22 stsp if (errcode) {
2209 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2210 13add988 2019-10-15 stsp "pthread_mutex_lock");
2214 7e8004ba 2022-09-11 thomas entry->idx = a->real_commits->ncommits;
2215 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->real_commits->head, entry, entry);
2216 7e8004ba 2022-09-11 thomas a->real_commits->ncommits++;
2218 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2219 7e8004ba 2022-09-11 thomas err = match_commit(&limit_match, &id, commit,
2220 7e8004ba 2022-09-11 thomas a->limit_regex);
2221 7e8004ba 2022-09-11 thomas if (err)
2224 7e8004ba 2022-09-11 thomas if (limit_match) {
2225 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
2227 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(
2228 7e8004ba 2022-09-11 thomas entry->commit, entry->id);
2229 7e8004ba 2022-09-11 thomas if (matched == NULL) {
2230 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
2231 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
2235 7e8004ba 2022-09-11 thomas err = got_object_commit_dup(&matched->commit,
2236 7e8004ba 2022-09-11 thomas entry->commit);
2237 7e8004ba 2022-09-11 thomas if (err)
2240 7e8004ba 2022-09-11 thomas matched->idx = a->limit_commits->ncommits;
2241 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&a->limit_commits->head,
2242 7e8004ba 2022-09-11 thomas matched, entry);
2243 7e8004ba 2022-09-11 thomas a->limit_commits->ncommits++;
2247 7e8004ba 2022-09-11 thomas * This is how we signal log_thread() that we
2248 7e8004ba 2022-09-11 thomas * have found a match, and that it should be
2249 7e8004ba 2022-09-11 thomas * counted as a new entry for the view.
2251 7e8004ba 2022-09-11 thomas a->limit_match = limit_match;
2254 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2255 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2256 7c1452c1 2020-03-26 stsp int have_match;
2257 7210b715 2022-09-11 thomas err = match_commit(&have_match, &id, commit, a->regex);
2261 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2262 7e8004ba 2022-09-11 thomas if (limit_match && have_match)
2263 7e8004ba 2022-09-11 thomas *a->search_next_done =
2264 7e8004ba 2022-09-11 thomas TOG_SEARCH_HAVE_MORE;
2265 7e8004ba 2022-09-11 thomas } else if (have_match)
2266 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2269 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2270 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2271 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2272 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2275 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2277 9ba79e04 2018-06-11 stsp return err;
2280 2b779855 2020-12-05 naddy static void
2281 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2283 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2284 2b779855 2020-12-05 naddy int ncommits = 0;
2286 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2287 2b779855 2020-12-05 naddy while (entry) {
2288 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2289 2b779855 2020-12-05 naddy s->selected_entry = entry;
2292 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2293 2b779855 2020-12-05 naddy ncommits++;
2297 0553a4e3 2018-04-30 stsp static const struct got_error *
2298 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2300 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2301 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2302 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2303 bd3f8225 2022-08-12 thomas int limit = view->nlines;
2304 60493ae3 2019-06-20 stsp int width;
2305 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2306 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2307 8b473291 2019-02-21 stsp char *refs_str = NULL;
2308 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2309 11b20872 2019-11-08 stsp struct tog_color *tc;
2310 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2312 bd3f8225 2022-08-12 thomas if (view_is_hsplit_top(view))
2313 bd3f8225 2022-08-12 thomas --limit; /* account for border */
2315 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2316 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2317 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2318 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2320 ecb28ae0 2018-07-16 stsp return err;
2321 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2322 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2323 d2075bf3 2020-12-25 stsp if (refs) {
2324 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2325 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2327 d2075bf3 2020-12-25 stsp goto done;
2331 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2332 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2334 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2335 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2336 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2337 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2338 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2339 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2340 8f4ed634 2020-03-26 stsp goto done;
2343 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2344 7e8004ba 2022-09-11 thomas const char *limit_str = NULL;
2346 f9686aa5 2020-03-27 stsp if (view->searching) {
2347 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2348 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2349 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2350 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2351 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2352 f9686aa5 2020-03-27 stsp search_str = "searching...";
2355 7e8004ba 2022-09-11 thomas if (s->limit_view && s->commits->ncommits == 0)
2356 7e8004ba 2022-09-11 thomas limit_str = "no matches found";
2358 7e8004ba 2022-09-11 thomas if (asprintf(&ncommits_str, " [%d/%d] %s %s",
2359 7e8004ba 2022-09-11 thomas entry ? entry->idx + 1 : 0, s->commits->ncommits,
2360 7e8004ba 2022-09-11 thomas search_str ? search_str : (refs_str ? refs_str : ""),
2361 7e8004ba 2022-09-11 thomas limit_str ? limit_str : "") == -1) {
2362 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2363 8f4ed634 2020-03-26 stsp goto done;
2367 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2368 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2369 91198554 2022-06-23 thomas "........................................",
2370 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2371 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2372 1a76625f 2018-10-22 stsp header = NULL;
2373 1a76625f 2018-10-22 stsp goto done;
2375 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2376 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2377 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2378 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2379 1a76625f 2018-10-22 stsp header = NULL;
2380 1a76625f 2018-10-22 stsp goto done;
2382 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2384 1a76625f 2018-10-22 stsp goto done;
2386 2814baeb 2018-08-01 stsp werase(view->window);
2388 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2389 a3404814 2018-09-02 stsp wstandout(view->window);
2390 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2392 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
2393 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2394 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2395 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2399 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
2400 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2401 a3404814 2018-09-02 stsp wstandend(view->window);
2402 ecb28ae0 2018-07-16 stsp free(wline);
2403 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2404 1a76625f 2018-10-22 stsp goto done;
2406 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2407 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2408 5813d178 2019-03-09 stsp ncommits = 0;
2409 05171be4 2022-06-23 thomas view->maxx = 0;
2410 5813d178 2019-03-09 stsp while (entry) {
2411 f69c5a46 2022-07-19 thomas struct got_commit_object *c = entry->commit;
2412 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2413 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2414 5813d178 2019-03-09 stsp int width;
2415 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2417 f69c5a46 2022-07-19 thomas if (s->use_committer)
2418 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_committer(c));
2420 f69c5a46 2022-07-19 thomas author = strdup(got_object_commit_get_author(c));
2421 5813d178 2019-03-09 stsp if (author == NULL) {
2422 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2423 5813d178 2019-03-09 stsp goto done;
2425 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2426 27a741e5 2019-09-11 stsp date_display_cols);
2427 5813d178 2019-03-09 stsp if (author_cols < width)
2428 5813d178 2019-03-09 stsp author_cols = width;
2429 5813d178 2019-03-09 stsp free(wauthor);
2430 5813d178 2019-03-09 stsp free(author);
2431 ef944b8b 2022-07-21 thomas if (err)
2432 ef944b8b 2022-07-21 thomas goto done;
2433 f69c5a46 2022-07-19 thomas err = got_object_commit_get_logmsg(&msg0, c);
2434 05171be4 2022-06-23 thomas if (err)
2435 05171be4 2022-06-23 thomas goto done;
2436 05171be4 2022-06-23 thomas msg = msg0;
2437 05171be4 2022-06-23 thomas while (*msg == '\n')
2439 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2440 331b1a16 2022-06-23 thomas *eol = '\0';
2441 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2442 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2443 331b1a16 2022-06-23 thomas if (err)
2444 331b1a16 2022-06-23 thomas goto done;
2445 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2446 05171be4 2022-06-23 thomas free(msg0);
2447 331b1a16 2022-06-23 thomas free(wmsg);
2448 7ca04879 2019-10-19 stsp ncommits++;
2449 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2452 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2453 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2454 867c6645 2018-07-10 stsp ncommits = 0;
2455 899d86c2 2018-05-10 stsp while (entry) {
2456 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2458 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2459 2814baeb 2018-08-01 stsp wstandout(view->window);
2460 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2461 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2462 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2463 2814baeb 2018-08-01 stsp wstandend(view->window);
2465 60493ae3 2019-06-20 stsp goto done;
2466 0553a4e3 2018-04-30 stsp ncommits++;
2467 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2468 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2471 a5d43cac 2022-07-01 thomas view_border(view);
2473 1a76625f 2018-10-22 stsp free(id_str);
2474 8b473291 2019-02-21 stsp free(refs_str);
2475 1a76625f 2018-10-22 stsp free(ncommits_str);
2476 1a76625f 2018-10-22 stsp free(header);
2477 80ddbec8 2018-04-29 stsp return err;
2480 07b55e75 2018-05-10 stsp static void
2481 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2483 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2484 07b55e75 2018-05-10 stsp int nscrolled = 0;
2486 7e8004ba 2022-09-11 thomas entry = TAILQ_FIRST(&s->commits->head);
2487 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2490 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2491 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2492 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2493 07b55e75 2018-05-10 stsp if (entry) {
2494 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2495 07b55e75 2018-05-10 stsp nscrolled++;
2500 aa075928 2018-05-10 stsp static const struct got_error *
2501 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2503 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2504 5e224a3e 2019-02-22 stsp int errcode;
2506 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2508 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2509 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2510 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2511 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2512 7aafa0d1 2019-02-22 stsp if (errcode)
2513 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2514 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2517 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2518 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2520 7c1452c1 2020-03-26 stsp if (!wait)
2523 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2524 ffe38506 2020-12-01 naddy show_log_view(view);
2525 7c1452c1 2020-03-26 stsp update_panels();
2526 7c1452c1 2020-03-26 stsp doupdate();
2528 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2529 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2530 82954512 2020-02-03 stsp if (errcode)
2531 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2532 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2534 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2535 ffe38506 2020-12-01 naddy show_log_view(view);
2536 7c1452c1 2020-03-26 stsp update_panels();
2537 7c1452c1 2020-03-26 stsp doupdate();
2540 5e224a3e 2019-02-22 stsp return NULL;
2543 a5d43cac 2022-07-01 thomas static const struct got_error *
2544 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2546 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2547 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2549 fe731b51 2022-07-14 thomas if (state->thread_args.log_complete)
2550 fe731b51 2022-07-14 thomas return NULL;
2552 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2553 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2554 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2556 a5d43cac 2022-07-01 thomas return err;
2559 5e224a3e 2019-02-22 stsp static const struct got_error *
2560 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2562 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2563 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2564 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2565 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2567 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2568 5e224a3e 2019-02-22 stsp return NULL;
2570 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2571 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < ncommits_needed &&
2572 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2574 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2576 07dd3ed3 2022-08-06 thomas s->thread_args.commits_needed +=
2577 7e8004ba 2022-09-11 thomas ncommits_needed - s->commits->ncommits;
2578 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2580 5e224a3e 2019-02-22 stsp return err;
2584 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2585 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2588 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2589 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2591 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2592 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2594 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2595 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2597 fe731b51 2022-07-14 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN && !s->thread_args.log_complete)
2598 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2600 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2602 dd0a52c1 2018-05-20 stsp return err;
2605 cd0acaa7 2018-05-20 stsp static const struct got_error *
2606 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2607 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2608 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2610 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2611 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2612 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2614 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2615 15a94983 2018-12-23 stsp if (diff_view == NULL)
2616 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2618 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2619 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2620 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2621 e5a0f69f 2018-08-18 stsp if (err == NULL)
2622 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2623 cd0acaa7 2018-05-20 stsp return err;
2626 80ddbec8 2018-04-29 stsp static const struct got_error *
2627 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2628 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2630 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2632 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2633 941e9f74 2019-05-21 stsp if (parent == NULL)
2634 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2636 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2637 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2638 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2639 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2640 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2641 941e9f74 2019-05-21 stsp s->tree = subtree;
2642 941e9f74 2019-05-21 stsp s->selected = 0;
2643 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2644 941e9f74 2019-05-21 stsp return NULL;
2647 941e9f74 2019-05-21 stsp static const struct got_error *
2648 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2649 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2651 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2652 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2653 941e9f74 2019-05-21 stsp const char *p;
2654 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2656 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2658 941e9f74 2019-05-21 stsp while (*p) {
2659 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2660 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2661 56e0773d 2019-11-28 stsp char *te_name;
2663 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2666 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2667 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2668 941e9f74 2019-05-21 stsp if (slash == NULL)
2669 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2671 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2672 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2673 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2676 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2677 56e0773d 2019-11-28 stsp if (te == NULL) {
2678 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2679 56e0773d 2019-11-28 stsp free(te_name);
2682 56e0773d 2019-11-28 stsp free(te_name);
2683 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2685 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2686 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2688 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2689 941e9f74 2019-05-21 stsp if (slash)
2690 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2692 941e9f74 2019-05-21 stsp subpath = strdup(path);
2693 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2694 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2698 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2703 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2704 941e9f74 2019-05-21 stsp free(tree_id);
2708 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2709 941e9f74 2019-05-21 stsp if (err) {
2710 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2713 941e9f74 2019-05-21 stsp if (slash == NULL)
2715 941e9f74 2019-05-21 stsp free(subpath);
2716 941e9f74 2019-05-21 stsp subpath = NULL;
2717 941e9f74 2019-05-21 stsp p = slash;
2720 941e9f74 2019-05-21 stsp free(subpath);
2721 1a76625f 2018-10-22 stsp return err;
2724 61266923 2020-01-14 stsp static const struct got_error *
2725 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2726 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2727 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2729 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2730 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2731 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2733 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2734 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2735 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2737 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2739 55cccc34 2020-02-20 stsp return err;
2740 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2742 55cccc34 2020-02-20 stsp *new_view = tree_view;
2744 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2745 55cccc34 2020-02-20 stsp return NULL;
2747 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2750 55cccc34 2020-02-20 stsp static const struct got_error *
2751 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2753 61266923 2020-01-14 stsp sigset_t sigset;
2754 61266923 2020-01-14 stsp int errcode;
2756 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2757 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2759 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2760 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2761 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2762 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2763 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2764 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2765 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2766 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2767 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2769 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2770 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2771 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2773 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2774 61266923 2020-01-14 stsp if (errcode)
2775 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2777 61266923 2020-01-14 stsp return NULL;
2780 1a76625f 2018-10-22 stsp static void *
2781 1a76625f 2018-10-22 stsp log_thread(void *arg)
2783 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2784 1a76625f 2018-10-22 stsp int errcode = 0;
2785 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2786 1a76625f 2018-10-22 stsp int done = 0;
2789 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2790 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2792 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2793 f2d749db 2022-07-12 thomas if (errcode) {
2794 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2795 61266923 2020-01-14 stsp return (void *)err;
2798 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2799 f2d749db 2022-07-12 thomas if (err) {
2800 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2801 f2d749db 2022-07-12 thomas goto done;
2804 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2805 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2806 f2d749db 2022-07-12 thomas if (errcode) {
2807 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2808 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2809 f2d749db 2022-07-12 thomas goto done;
2811 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2812 1a76625f 2018-10-22 stsp if (err) {
2813 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2814 f2d749db 2022-07-12 thomas goto done;
2815 1a76625f 2018-10-22 stsp err = NULL;
2817 7e8004ba 2022-09-11 thomas } else if (a->commits_needed > 0 && !a->load_all) {
2818 7e8004ba 2022-09-11 thomas if (*a->limiting) {
2819 7e8004ba 2022-09-11 thomas if (a->limit_match)
2820 7e8004ba 2022-09-11 thomas a->commits_needed--;
2822 7e8004ba 2022-09-11 thomas a->commits_needed--;
2825 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2826 3abe8080 2019-04-10 stsp if (errcode) {
2827 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2828 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2829 f2d749db 2022-07-12 thomas goto done;
2830 3abe8080 2019-04-10 stsp } else if (*a->quit)
2832 7e8004ba 2022-09-11 thomas else if (*a->limiting && *a->first_displayed_entry == NULL) {
2833 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2834 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->limit_commits->head);
2835 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2836 7e8004ba 2022-09-11 thomas } else if (*a->first_displayed_entry == NULL) {
2837 7e8004ba 2022-09-11 thomas *a->first_displayed_entry =
2838 7e8004ba 2022-09-11 thomas TAILQ_FIRST(&a->real_commits->head);
2839 7e8004ba 2022-09-11 thomas *a->selected_entry = *a->first_displayed_entry;
2842 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2843 7c1452c1 2020-03-26 stsp if (errcode) {
2844 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2845 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2846 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2847 f2d749db 2022-07-12 thomas goto done;
2851 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2853 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2854 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2855 7c1452c1 2020-03-26 stsp &tog_mutex);
2856 f2d749db 2022-07-12 thomas if (errcode) {
2857 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2858 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2859 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2860 f2d749db 2022-07-12 thomas goto done;
2862 21355643 2020-12-06 stsp if (*a->quit)
2867 3abe8080 2019-04-10 stsp a->log_complete = 1;
2868 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2869 f2d749db 2022-07-12 thomas if (errcode)
2870 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2872 f2d749db 2022-07-12 thomas if (err) {
2873 f2d749db 2022-07-12 thomas tog_thread_error = 1;
2874 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
2876 1a76625f 2018-10-22 stsp return (void *)err;
2879 1a76625f 2018-10-22 stsp static const struct got_error *
2880 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2882 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
2883 1a76625f 2018-10-22 stsp int errcode;
2885 1a76625f 2018-10-22 stsp if (s->thread) {
2886 1a76625f 2018-10-22 stsp s->quit = 1;
2887 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2888 1a76625f 2018-10-22 stsp if (errcode)
2889 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2890 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2891 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2892 1a76625f 2018-10-22 stsp if (errcode)
2893 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2894 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2895 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
2896 1a76625f 2018-10-22 stsp if (errcode)
2897 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2898 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2899 1a76625f 2018-10-22 stsp if (errcode)
2900 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2901 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2902 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2905 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2906 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2907 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2910 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2911 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2912 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2913 1af5eddf 2022-06-23 thomas if (err == NULL)
2914 1af5eddf 2022-06-23 thomas err = pack_err;
2915 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2918 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2919 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2920 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2923 f2d749db 2022-07-12 thomas return err ? err : thread_err;
2926 9343a5fb 2018-06-23 stsp static const struct got_error *
2927 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2929 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2930 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2931 276b94a1 2020-11-13 naddy int errcode;
2933 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2935 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2936 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2937 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2939 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2940 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2941 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2943 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
2944 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
2945 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2946 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2947 1a76625f 2018-10-22 stsp free(s->start_id);
2948 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2949 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2950 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2951 1a76625f 2018-10-22 stsp return err;
2955 7e8004ba 2022-09-11 thomas * We use two queues to implement the limit feature: first consists of
2956 7e8004ba 2022-09-11 thomas * commits matching the current limit_regex; second is the real queue
2957 7e8004ba 2022-09-11 thomas * of all known commits (real_commits). When the user starts limiting,
2958 7e8004ba 2022-09-11 thomas * we swap queues such that all movement and displaying functionality
2959 7e8004ba 2022-09-11 thomas * works with very slight change.
2961 1a76625f 2018-10-22 stsp static const struct got_error *
2962 7e8004ba 2022-09-11 thomas limit_log_view(struct tog_view *view)
2964 7e8004ba 2022-09-11 thomas struct tog_log_view_state *s = &view->state.log;
2965 7e8004ba 2022-09-11 thomas struct commit_queue_entry *entry;
2966 7e8004ba 2022-09-11 thomas struct tog_view *v = view;
2967 7e8004ba 2022-09-11 thomas const struct got_error *err = NULL;
2968 7e8004ba 2022-09-11 thomas char pattern[1024];
2969 7e8004ba 2022-09-11 thomas int ret;
2971 7e8004ba 2022-09-11 thomas if (view_is_hsplit_top(view))
2972 7e8004ba 2022-09-11 thomas v = view->child;
2973 7e8004ba 2022-09-11 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
2974 7e8004ba 2022-09-11 thomas v = view->parent;
2976 7e8004ba 2022-09-11 thomas /* Get the pattern */
2977 7e8004ba 2022-09-11 thomas wmove(v->window, v->nlines - 1, 0);
2978 7e8004ba 2022-09-11 thomas wclrtoeol(v->window);
2979 7e8004ba 2022-09-11 thomas mvwaddstr(v->window, v->nlines - 1, 0, "&/");
2980 7e8004ba 2022-09-11 thomas nodelay(v->window, FALSE);
2981 7e8004ba 2022-09-11 thomas nocbreak();
2983 7e8004ba 2022-09-11 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
2984 7e8004ba 2022-09-11 thomas cbreak();
2985 7e8004ba 2022-09-11 thomas noecho();
2986 7e8004ba 2022-09-11 thomas nodelay(v->window, TRUE);
2987 7e8004ba 2022-09-11 thomas if (ret == ERR)
2988 7e8004ba 2022-09-11 thomas return NULL;
2990 7e8004ba 2022-09-11 thomas if (*pattern == '\0') {
2992 7e8004ba 2022-09-11 thomas * Safety measure for the situation where the user
2993 7e8004ba 2022-09-11 thomas * resets limit without previously limiting anything.
2995 7e8004ba 2022-09-11 thomas if (!s->limit_view)
2996 7e8004ba 2022-09-11 thomas return NULL;
2999 7e8004ba 2022-09-11 thomas * User could have pressed Ctrl+L, which refreshed the
3000 7e8004ba 2022-09-11 thomas * commit queues, it means we can't save previously
3001 7e8004ba 2022-09-11 thomas * (before limit took place) displayed entries,
3002 7e8004ba 2022-09-11 thomas * because they would point to already free'ed memory,
3003 7e8004ba 2022-09-11 thomas * so we are forced to always select first entry of
3004 7e8004ba 2022-09-11 thomas * the queue.
3006 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3007 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->real_commits.head);
3008 7e8004ba 2022-09-11 thomas s->selected_entry = s->first_displayed_entry;
3009 7e8004ba 2022-09-11 thomas s->selected = 0;
3010 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3012 7e8004ba 2022-09-11 thomas return NULL;
3015 7e8004ba 2022-09-11 thomas if (regcomp(&s->limit_regex, pattern, REG_EXTENDED | REG_NEWLINE))
3016 7e8004ba 2022-09-11 thomas return NULL;
3018 7e8004ba 2022-09-11 thomas s->limit_view = 1;
3020 7e8004ba 2022-09-11 thomas /* Clear the screen while loading limit view */
3021 7e8004ba 2022-09-11 thomas s->first_displayed_entry = NULL;
3022 7e8004ba 2022-09-11 thomas s->last_displayed_entry = NULL;
3023 7e8004ba 2022-09-11 thomas s->selected_entry = NULL;
3024 7e8004ba 2022-09-11 thomas s->commits = &s->limit_commits;
3026 7e8004ba 2022-09-11 thomas /* Prepare limit queue for new search */
3027 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3028 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3030 7e8004ba 2022-09-11 thomas /* First process commits, which are in queue already */
3031 7e8004ba 2022-09-11 thomas TAILQ_FOREACH(entry, &s->real_commits.head, entry) {
3032 7e8004ba 2022-09-11 thomas int have_match = 0;
3034 7e8004ba 2022-09-11 thomas err = match_commit(&have_match, entry->id,
3035 7e8004ba 2022-09-11 thomas entry->commit, &s->limit_regex);
3036 7e8004ba 2022-09-11 thomas if (err)
3037 7e8004ba 2022-09-11 thomas return err;
3039 7e8004ba 2022-09-11 thomas if (have_match) {
3040 7e8004ba 2022-09-11 thomas struct commit_queue_entry *matched;
3042 7e8004ba 2022-09-11 thomas matched = alloc_commit_queue_entry(entry->commit,
3043 7e8004ba 2022-09-11 thomas entry->id);
3044 7e8004ba 2022-09-11 thomas if (matched == NULL) {
3045 7e8004ba 2022-09-11 thomas err = got_error_from_errno(
3046 7e8004ba 2022-09-11 thomas "alloc_commit_queue_entry");
3050 7e8004ba 2022-09-11 thomas err = got_object_commit_dup(&matched->commit,
3051 7e8004ba 2022-09-11 thomas entry->commit);
3052 7e8004ba 2022-09-11 thomas if (err) {
3053 7e8004ba 2022-09-11 thomas free(matched);
3054 7e8004ba 2022-09-11 thomas return err;
3057 7e8004ba 2022-09-11 thomas matched->idx = s->limit_commits.ncommits;
3058 7e8004ba 2022-09-11 thomas TAILQ_INSERT_TAIL(&s->limit_commits.head,
3059 7e8004ba 2022-09-11 thomas matched, entry);
3060 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits++;
3064 7e8004ba 2022-09-11 thomas /* Second process all the commits, until we fill the screen */
3065 7e8004ba 2022-09-11 thomas if (s->limit_commits.ncommits < view->nlines - 1 &&
3066 7e8004ba 2022-09-11 thomas !s->thread_args.log_complete) {
3067 7e8004ba 2022-09-11 thomas s->thread_args.commits_needed +=
3068 7e8004ba 2022-09-11 thomas view->nlines - s->limit_commits.ncommits - 1;
3069 7e8004ba 2022-09-11 thomas err = trigger_log_thread(view, 1);
3070 7e8004ba 2022-09-11 thomas if (err)
3071 7e8004ba 2022-09-11 thomas return err;
3074 7e8004ba 2022-09-11 thomas s->first_displayed_entry = TAILQ_FIRST(&s->commits->head);
3075 7e8004ba 2022-09-11 thomas s->selected_entry = TAILQ_FIRST(&s->commits->head);
3076 7e8004ba 2022-09-11 thomas s->selected = 0;
3078 7e8004ba 2022-09-11 thomas return NULL;
3081 7e8004ba 2022-09-11 thomas static const struct got_error *
3082 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
3084 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3086 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
3087 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3088 60493ae3 2019-06-20 stsp return NULL;
3091 60493ae3 2019-06-20 stsp static const struct got_error *
3092 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
3094 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
3095 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
3096 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
3098 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
3099 f9686aa5 2020-03-27 stsp show_log_view(view);
3100 f9686aa5 2020-03-27 stsp update_panels();
3101 f9686aa5 2020-03-27 stsp doupdate();
3103 96e2b566 2019-07-08 stsp if (s->search_entry) {
3104 13add988 2019-10-15 stsp int errcode, ch;
3105 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3106 13add988 2019-10-15 stsp if (errcode)
3107 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3108 13add988 2019-10-15 stsp "pthread_mutex_unlock");
3109 13add988 2019-10-15 stsp ch = wgetch(view->window);
3110 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
3111 13add988 2019-10-15 stsp if (errcode)
3112 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
3113 13add988 2019-10-15 stsp "pthread_mutex_lock");
3114 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
3115 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3116 678cbce5 2019-07-28 stsp return NULL;
3118 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
3119 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
3121 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
3122 96e2b566 2019-07-08 stsp commit_queue_head, entry);
3123 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
3124 df5e841d 2022-06-23 thomas int matched_idx = s->matched_entry->idx;
3125 df5e841d 2022-06-23 thomas int selected_idx = s->selected_entry->idx;
3128 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
3129 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
3130 1f4d5162 2022-06-23 thomas * might have changed.
3132 b74feda6 2022-06-23 thomas if (view->searching == TOG_SEARCH_FORWARD) {
3133 df5e841d 2022-06-23 thomas if (matched_idx > selected_idx)
3134 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
3136 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->matched_entry, entry);
3137 b74feda6 2022-06-23 thomas } else {
3138 df5e841d 2022-06-23 thomas if (matched_idx < selected_idx)
3139 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->selected_entry,
3140 df5e841d 2022-06-23 thomas commit_queue_head, entry);
3142 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->matched_entry,
3143 df5e841d 2022-06-23 thomas commit_queue_head, entry);
3146 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
3149 60493ae3 2019-06-20 stsp while (1) {
3150 13add988 2019-10-15 stsp int have_match = 0;
3152 60493ae3 2019-06-20 stsp if (entry == NULL) {
3153 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
3154 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
3155 f9967bca 2020-03-27 stsp view->search_next_done =
3156 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
3157 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
3158 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
3159 f801134a 2019-06-25 stsp return NULL;
3162 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
3163 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
3164 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
3166 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
3167 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
3170 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
3171 13add988 2019-10-15 stsp &view->regex);
3174 13add988 2019-10-15 stsp if (have_match) {
3175 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3176 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
3180 96e2b566 2019-07-08 stsp s->search_entry = entry;
3181 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
3182 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
3184 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
3187 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
3188 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
3189 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
3190 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
3192 60493ae3 2019-06-20 stsp return err;
3195 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
3196 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
3198 60493ae3 2019-06-20 stsp return err;
3203 96e2b566 2019-07-08 stsp s->search_entry = NULL;
3205 60493ae3 2019-06-20 stsp return NULL;
3208 60493ae3 2019-06-20 stsp static const struct got_error *
3209 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
3210 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
3211 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
3213 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
3214 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3215 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
3216 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
3217 1a76625f 2018-10-22 stsp int errcode;
3219 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
3220 f135c941 2020-02-20 stsp free(s->in_repo_path);
3221 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
3222 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
3223 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3226 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
3227 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->real_commits.head);
3228 7e8004ba 2022-09-11 thomas s->real_commits.ncommits = 0;
3229 7e8004ba 2022-09-11 thomas s->commits = &s->real_commits;
3231 7e8004ba 2022-09-11 thomas TAILQ_INIT(&s->limit_commits.head);
3232 7e8004ba 2022-09-11 thomas s->limit_view = 0;
3233 7e8004ba 2022-09-11 thomas s->limit_commits.ncommits = 0;
3235 fb2756b9 2018-08-04 stsp s->repo = repo;
3236 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
3237 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
3238 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
3239 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
3240 9cd7cbd1 2020-12-07 stsp goto done;
3243 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
3244 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
3245 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3246 5036bf37 2018-09-24 stsp goto done;
3248 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
3250 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3251 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3252 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
3253 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3255 11b20872 2019-11-08 stsp goto done;
3256 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
3257 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3258 11b20872 2019-11-08 stsp if (err) {
3259 11b20872 2019-11-08 stsp free_colors(&s->colors);
3260 11b20872 2019-11-08 stsp goto done;
3262 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
3263 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3264 11b20872 2019-11-08 stsp if (err) {
3265 11b20872 2019-11-08 stsp free_colors(&s->colors);
3266 11b20872 2019-11-08 stsp goto done;
3270 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
3271 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
3272 ea0bff04 2022-07-19 thomas view->resize = resize_log_view;
3273 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
3274 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
3275 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
3277 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3278 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3279 1af5eddf 2022-06-23 thomas if (err)
3280 1af5eddf 2022-06-23 thomas goto done;
3282 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
3283 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3284 7cd52833 2022-06-23 thomas if (err)
3285 7cd52833 2022-06-23 thomas goto done;
3286 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3287 b672a97a 2020-01-27 stsp !s->log_branches);
3289 1a76625f 2018-10-22 stsp goto done;
3290 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3291 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3293 c5b78334 2020-01-12 stsp goto done;
3295 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3296 1a76625f 2018-10-22 stsp if (errcode) {
3297 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3298 1a76625f 2018-10-22 stsp goto done;
3300 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3301 7c1452c1 2020-03-26 stsp if (errcode) {
3302 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3303 7c1452c1 2020-03-26 stsp goto done;
3306 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3307 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3308 7e8004ba 2022-09-11 thomas s->thread_args.real_commits = &s->real_commits;
3309 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3310 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3311 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3312 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3313 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3314 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3315 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3316 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3317 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3318 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3319 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3320 7e8004ba 2022-09-11 thomas s->thread_args.limiting = &s->limit_view;
3321 7e8004ba 2022-09-11 thomas s->thread_args.limit_regex = &s->limit_regex;
3322 7e8004ba 2022-09-11 thomas s->thread_args.limit_commits = &s->limit_commits;
3325 1a76625f 2018-10-22 stsp close_log_view(view);
3326 ba4f502b 2018-08-04 stsp return err;
3329 e5a0f69f 2018-08-18 stsp static const struct got_error *
3330 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3332 f2f6d207 2020-11-24 stsp const struct got_error *err;
3333 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3335 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3336 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3337 2b380cc8 2018-10-24 stsp &s->thread_args);
3338 2b380cc8 2018-10-24 stsp if (errcode)
3339 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3340 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3341 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3343 f2f6d207 2020-11-24 stsp return err;
3347 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3350 b31f89ff 2022-07-01 thomas static void
3351 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3353 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3355 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3357 7e8004ba 2022-09-11 thomas if (s->selected_entry->idx == 0)
3358 7e8004ba 2022-09-11 thomas view->count = 0;
3360 7e8004ba 2022-09-11 thomas if ((page && TAILQ_FIRST(&s->commits->head) == s->first_displayed_entry)
3361 b31f89ff 2022-07-01 thomas || home)
3362 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3364 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3365 b31f89ff 2022-07-01 thomas --s->selected;
3367 7e8004ba 2022-09-11 thomas log_scroll_up(s, home ? s->commits->ncommits : MAX(page, 1));
3369 b31f89ff 2022-07-01 thomas select_commit(s);
3373 b31f89ff 2022-07-01 thomas static const struct got_error *
3374 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3376 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3377 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3378 7ed048bd 2022-08-12 thomas int eos = view->nlines - 2;
3380 7e8004ba 2022-09-11 thomas if (s->first_displayed_entry == NULL)
3381 7e8004ba 2022-09-11 thomas return NULL;
3383 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3384 7e8004ba 2022-09-11 thomas s->selected_entry->idx >= s->commits->ncommits - 1)
3385 b31f89ff 2022-07-01 thomas return NULL;
3387 7ed048bd 2022-08-12 thomas if (view_is_hsplit_top(view))
3388 7ed048bd 2022-08-12 thomas --eos; /* border consumes the last line */
3390 7ed048bd 2022-08-12 thomas if (!page) {
3391 7e8004ba 2022-09-11 thomas if (s->selected < MIN(eos, s->commits->ncommits - 1))
3392 b31f89ff 2022-07-01 thomas ++s->selected;
3394 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3395 c0be8933 2022-08-12 thomas } else if (s->thread_args.load_all && s->thread_args.log_complete) {
3396 7ed048bd 2022-08-12 thomas struct commit_queue_entry *entry;
3399 7ed048bd 2022-08-12 thomas s->selected = 0;
3400 7e8004ba 2022-09-11 thomas entry = TAILQ_LAST(&s->commits->head, commit_queue_head);
3401 7ed048bd 2022-08-12 thomas s->last_displayed_entry = entry;
3402 7ed048bd 2022-08-12 thomas for (n = 0; n <= eos; n++) {
3403 7ed048bd 2022-08-12 thomas if (entry == NULL)
3405 7ed048bd 2022-08-12 thomas s->first_displayed_entry = entry;
3406 7ed048bd 2022-08-12 thomas entry = TAILQ_PREV(entry, commit_queue_head, entry);
3408 7ed048bd 2022-08-12 thomas if (n > 0)
3409 7ed048bd 2022-08-12 thomas s->selected = n - 1;
3410 b31f89ff 2022-07-01 thomas } else {
3411 7e8004ba 2022-09-11 thomas if (s->last_displayed_entry->idx == s->commits->ncommits - 1 &&
3412 bd3f8225 2022-08-12 thomas s->thread_args.log_complete)
3413 bd3f8225 2022-08-12 thomas s->selected += MIN(page,
3414 7e8004ba 2022-09-11 thomas s->commits->ncommits - s->selected_entry->idx - 1);
3416 bd3f8225 2022-08-12 thomas err = log_scroll_down(view, page);
3418 b31f89ff 2022-07-01 thomas if (err)
3419 b31f89ff 2022-07-01 thomas return err;
3422 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3423 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3425 25100026 2022-08-13 thomas if (s->first_displayed_entry && s->last_displayed_entry) {
3426 25100026 2022-08-13 thomas s->selected = MIN(s->selected,
3427 25100026 2022-08-13 thomas s->last_displayed_entry->idx -
3428 25100026 2022-08-13 thomas s->first_displayed_entry->idx);
3431 b31f89ff 2022-07-01 thomas select_commit(s);
3433 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3434 7e8004ba 2022-09-11 thomas s->selected_entry->idx == s->commits->ncommits - 1)
3435 b31f89ff 2022-07-01 thomas view->count = 0;
3437 b31f89ff 2022-07-01 thomas return NULL;
3440 a5d43cac 2022-07-01 thomas static void
3441 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3446 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3447 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3448 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3449 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3450 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3452 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3453 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3454 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3455 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3456 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3457 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3459 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3463 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3464 e5a0f69f 2018-08-18 stsp static const struct got_error *
3465 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3467 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3469 a5d43cac 2022-07-01 thomas view->nlines = y;
3470 64486692 2022-07-07 thomas view->ncols = COLS;
3471 a5d43cac 2022-07-01 thomas err = view_resize(view);
3472 a5d43cac 2022-07-01 thomas if (err)
3473 a5d43cac 2022-07-01 thomas return err;
3475 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3477 a5d43cac 2022-07-01 thomas return err;
3480 07dd3ed3 2022-08-06 thomas static const struct got_error *
3481 07dd3ed3 2022-08-06 thomas log_goto_line(struct tog_view *view, int nlines)
3483 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
3484 07dd3ed3 2022-08-06 thomas struct tog_log_view_state *s = &view->state.log;
3485 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
3487 25100026 2022-08-13 thomas if (s->first_displayed_entry == NULL || s->last_displayed_entry == NULL)
3488 25100026 2022-08-13 thomas return NULL;
3490 07dd3ed3 2022-08-06 thomas g = view->gline;
3491 07dd3ed3 2022-08-06 thomas view->gline = 0;
3493 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
3494 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
3495 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
3496 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
3497 07dd3ed3 2022-08-06 thomas select_commit(s);
3498 07dd3ed3 2022-08-06 thomas return NULL;
3501 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
3502 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view, g - idx - 1);
3503 07dd3ed3 2022-08-06 thomas if (!err && g > s->selected_entry->idx + 1)
3504 07dd3ed3 2022-08-06 thomas err = log_move_cursor_down(view,
3505 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1);
3506 07dd3ed3 2022-08-06 thomas if (err)
3507 07dd3ed3 2022-08-06 thomas return err;
3508 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
3509 07dd3ed3 2022-08-06 thomas log_move_cursor_up(view, idx - g + 1, 0);
3511 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
3512 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
3514 07dd3ed3 2022-08-06 thomas select_commit(s);
3515 07dd3ed3 2022-08-06 thomas return NULL;
3519 a5d43cac 2022-07-01 thomas static const struct got_error *
3520 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3522 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3523 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3524 7ed048bd 2022-08-12 thomas int eos, nscroll;
3526 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3527 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3528 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3529 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3530 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3531 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3533 c0be8933 2022-08-12 thomas if (err)
3534 c0be8933 2022-08-12 thomas return err;
3537 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3538 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3539 068ab281 2022-07-01 thomas --eos; /* border */
3541 07dd3ed3 2022-08-06 thomas if (view->gline)
3542 07dd3ed3 2022-08-06 thomas return log_goto_line(view, eos);
3544 528dedf3 2021-08-30 stsp switch (ch) {
3545 7e8004ba 2022-09-11 thomas case '&':
3546 7e8004ba 2022-09-11 thomas err = limit_log_view(view);
3549 1e37a5c2 2019-05-12 jcs s->quit = 1;
3551 05171be4 2022-06-23 thomas case '0':
3552 05171be4 2022-06-23 thomas view->x = 0;
3554 05171be4 2022-06-23 thomas case '$':
3555 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3556 07b0611c 2022-06-23 thomas view->count = 0;
3558 05171be4 2022-06-23 thomas case KEY_RIGHT:
3559 05171be4 2022-06-23 thomas case 'l':
3560 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3561 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3563 07b0611c 2022-06-23 thomas view->count = 0;
3565 05171be4 2022-06-23 thomas case KEY_LEFT:
3566 05171be4 2022-06-23 thomas case 'h':
3567 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3568 07b0611c 2022-06-23 thomas if (view->x <= 0)
3569 07b0611c 2022-06-23 thomas view->count = 0;
3572 1e37a5c2 2019-05-12 jcs case KEY_UP:
3575 f7140bf5 2021-10-17 thomas case CTRL('p'):
3576 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3579 912a3f79 2021-08-30 j case KEY_HOME:
3580 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3581 07b0611c 2022-06-23 thomas view->count = 0;
3583 70f17a53 2022-06-13 thomas case CTRL('u'):
3584 23427b14 2022-06-23 thomas case 'u':
3585 70f17a53 2022-06-13 thomas nscroll /= 2;
3586 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3587 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3588 a4292ac5 2019-05-12 jcs case CTRL('b'):
3589 1c5e5faa 2022-06-23 thomas case 'b':
3590 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3593 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3596 f7140bf5 2021-10-17 thomas case CTRL('n'):
3597 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3599 f69c5a46 2022-07-19 thomas case '@':
3600 f69c5a46 2022-07-19 thomas s->use_committer = !s->use_committer;
3603 912a3f79 2021-08-30 j case KEY_END: {
3604 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3605 912a3f79 2021-08-30 j * traverse them all. */
3606 07b0611c 2022-06-23 thomas view->count = 0;
3607 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 1;
3608 7ed048bd 2022-08-12 thomas if (!s->thread_args.log_complete)
3609 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3610 7e8004ba 2022-09-11 thomas err = log_move_cursor_down(view, s->commits->ncommits);
3611 7ed048bd 2022-08-12 thomas s->thread_args.load_all = 0;
3614 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3615 23427b14 2022-06-23 thomas case 'd':
3616 70f17a53 2022-06-13 thomas nscroll /= 2;
3617 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3618 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3619 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3620 4c2d69cb 2022-06-23 thomas case 'f':
3621 b31f89ff 2022-07-01 thomas case ' ':
3622 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3624 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3625 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3626 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3627 7e8004ba 2022-09-11 thomas if (s->selected > s->commits->ncommits - 1)
3628 7e8004ba 2022-09-11 thomas s->selected = s->commits->ncommits - 1;
3629 2b779855 2020-12-05 naddy select_commit(s);
3630 7e8004ba 2022-09-11 thomas if (s->commits->ncommits < view->nlines - 1 &&
3631 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3632 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3633 7e8004ba 2022-09-11 thomas s->commits->ncommits;
3634 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3637 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3638 444d5325 2022-07-03 thomas case '\r':
3639 07b0611c 2022-06-23 thomas view->count = 0;
3640 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3642 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_DIFF);
3644 1be4947a 2022-07-22 thomas case 'T':
3645 07b0611c 2022-06-23 thomas view->count = 0;
3646 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3648 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
3650 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3651 21355643 2020-12-06 stsp case CTRL('l'):
3653 07b0611c 2022-06-23 thomas view->count = 0;
3654 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3655 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3657 21355643 2020-12-06 stsp err = stop_log_thread(s);
3659 74cfe85e 2020-10-20 stsp return err;
3660 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3661 21355643 2020-12-06 stsp char *parent_path;
3662 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3664 21355643 2020-12-06 stsp return err;
3665 21355643 2020-12-06 stsp free(s->in_repo_path);
3666 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3667 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3668 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3669 21355643 2020-12-06 stsp struct got_object_id *start_id;
3670 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3671 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3672 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3674 21355643 2020-12-06 stsp return err;
3675 21355643 2020-12-06 stsp free(s->start_id);
3676 21355643 2020-12-06 stsp s->start_id = start_id;
3677 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3678 21355643 2020-12-06 stsp } else /* 'B' */
3679 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3681 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3682 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3683 bf7e79b3 2022-06-23 thomas if (err)
3684 bf7e79b3 2022-06-23 thomas return err;
3686 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3687 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3688 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3690 21355643 2020-12-06 stsp return err;
3691 51a10b52 2020-12-26 stsp tog_free_refs();
3692 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3694 ca51c541 2020-12-07 stsp return err;
3695 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3696 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3698 d01904d4 2019-06-25 stsp return err;
3699 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3700 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3702 b672a97a 2020-01-27 stsp return err;
3703 7e8004ba 2022-09-11 thomas free_commits(&s->real_commits);
3704 7e8004ba 2022-09-11 thomas free_commits(&s->limit_commits);
3705 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3706 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3707 21355643 2020-12-06 stsp s->selected_entry = NULL;
3708 21355643 2020-12-06 stsp s->selected = 0;
3709 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3710 21355643 2020-12-06 stsp s->quit = 0;
3711 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3712 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3713 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3714 94ecf40d 2022-07-22 thomas view->offset = 0;
3716 1be4947a 2022-07-22 thomas case 'R':
3717 07b0611c 2022-06-23 thomas view->count = 0;
3718 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
3721 07b0611c 2022-06-23 thomas view->count = 0;
3725 80ddbec8 2018-04-29 stsp return err;
3728 4ed7e80c 2018-05-20 stsp static const struct got_error *
3729 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3731 c2db6724 2019-01-04 stsp const struct got_error *error;
3733 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3734 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3735 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3737 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3738 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3740 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3741 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3743 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3744 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3746 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3747 c2db6724 2019-01-04 stsp if (error != NULL)
3748 c2db6724 2019-01-04 stsp return error;
3750 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3751 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3753 c2db6724 2019-01-04 stsp return NULL;
3756 a915003a 2019-02-05 stsp static void
3757 a915003a 2019-02-05 stsp init_curses(void)
3760 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3761 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3762 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3764 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3765 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3766 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3767 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3768 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3770 a915003a 2019-02-05 stsp initscr();
3772 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3775 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3776 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3777 a915003a 2019-02-05 stsp curs_set(0);
3778 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3779 6d17833f 2019-11-08 stsp start_color();
3780 6d17833f 2019-11-08 stsp use_default_colors();
3784 c2db6724 2019-01-04 stsp static const struct got_error *
3785 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3786 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3788 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3790 f135c941 2020-02-20 stsp if (argc == 0) {
3791 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3792 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3793 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3794 f135c941 2020-02-20 stsp return NULL;
3797 f135c941 2020-02-20 stsp if (worktree) {
3798 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3801 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3803 f135c941 2020-02-20 stsp return err;
3804 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3805 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3806 bfd61697 2020-11-14 stsp p) == -1) {
3807 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3808 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3812 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3814 f135c941 2020-02-20 stsp return err;
3817 f135c941 2020-02-20 stsp static const struct got_error *
3818 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3820 80ddbec8 2018-04-29 stsp const struct got_error *error;
3821 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3822 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3823 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3824 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3825 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3826 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3827 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3828 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3829 04cc582a 2018-08-01 stsp struct tog_view *view;
3830 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3832 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3833 80ddbec8 2018-04-29 stsp switch (ch) {
3835 b672a97a 2020-01-27 stsp log_branches = 1;
3838 80ddbec8 2018-04-29 stsp start_commit = optarg;
3841 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3842 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3843 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3847 17020d27 2019-03-07 stsp usage_log();
3848 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3852 80ddbec8 2018-04-29 stsp argc -= optind;
3853 80ddbec8 2018-04-29 stsp argv += optind;
3855 f135c941 2020-02-20 stsp if (argc > 1)
3856 f135c941 2020-02-20 stsp usage_log();
3858 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3859 7cd52833 2022-06-23 thomas if (error != NULL)
3860 7cd52833 2022-06-23 thomas goto done;
3862 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3863 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3864 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3865 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3866 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3867 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3868 c156c7a4 2020-12-18 stsp goto done;
3869 a1fbf39a 2019-08-11 stsp if (worktree)
3870 6962eb72 2020-02-20 stsp repo_path =
3871 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3873 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3874 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3875 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3876 c156c7a4 2020-12-18 stsp goto done;
3880 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3881 80ddbec8 2018-04-29 stsp if (error != NULL)
3882 ecb28ae0 2018-07-16 stsp goto done;
3884 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3885 f135c941 2020-02-20 stsp repo, worktree);
3886 f135c941 2020-02-20 stsp if (error)
3887 f135c941 2020-02-20 stsp goto done;
3889 f135c941 2020-02-20 stsp init_curses();
3891 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3892 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3893 c02c541e 2019-03-29 stsp if (error)
3894 c02c541e 2019-03-29 stsp goto done;
3896 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3897 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3898 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3899 87670572 2020-12-26 naddy if (error)
3900 87670572 2020-12-26 naddy goto done;
3903 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3904 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3905 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3906 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3907 d8f38dc4 2020-12-05 stsp if (error)
3908 d8f38dc4 2020-12-05 stsp goto done;
3909 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3911 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3912 d8f38dc4 2020-12-05 stsp if (error == NULL)
3913 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3914 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3915 d8f38dc4 2020-12-05 stsp goto done;
3916 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3917 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3918 d8f38dc4 2020-12-05 stsp if (error)
3919 d8f38dc4 2020-12-05 stsp goto done;
3922 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3923 04cc582a 2018-08-01 stsp if (view == NULL) {
3924 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3925 04cc582a 2018-08-01 stsp goto done;
3927 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3928 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3929 ba4f502b 2018-08-04 stsp if (error)
3930 ba4f502b 2018-08-04 stsp goto done;
3931 2fc00ff4 2019-08-31 stsp if (worktree) {
3932 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3933 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3934 2fc00ff4 2019-08-31 stsp worktree = NULL;
3936 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3938 f135c941 2020-02-20 stsp free(in_repo_path);
3939 ecb28ae0 2018-07-16 stsp free(repo_path);
3940 ecb28ae0 2018-07-16 stsp free(cwd);
3941 899d86c2 2018-05-10 stsp free(start_id);
3942 d8f38dc4 2020-12-05 stsp free(label);
3944 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3945 1d0f4054 2021-06-17 stsp if (repo) {
3946 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3947 1d0f4054 2021-06-17 stsp if (error == NULL)
3948 1d0f4054 2021-06-17 stsp error = close_err;
3950 ec142235 2019-03-07 stsp if (worktree)
3951 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3952 7cd52833 2022-06-23 thomas if (pack_fds) {
3953 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3954 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3955 7cd52833 2022-06-23 thomas if (error == NULL)
3956 7cd52833 2022-06-23 thomas error = pack_err;
3958 51a10b52 2020-12-26 stsp tog_free_refs();
3959 80ddbec8 2018-04-29 stsp return error;
3962 4ed7e80c 2018-05-20 stsp __dead static void
3963 9f7d7167 2018-04-29 stsp usage_diff(void)
3966 d6506a3d 2022-08-16 thomas fprintf(stderr, "usage: %s diff [-aw] [-C number] [-r repository-path] "
3967 d6506a3d 2022-08-16 thomas "object1 object2\n", getprogname());
3971 6d17833f 2019-11-08 stsp static int
3972 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3973 41605754 2020-11-12 stsp regmatch_t *regmatch)
3975 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3978 ef20f542 2022-06-26 thomas static struct tog_color *
3979 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3981 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3983 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3984 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3985 6d17833f 2019-11-08 stsp return tc;
3988 6d17833f 2019-11-08 stsp return NULL;
3991 4ed7e80c 2018-05-20 stsp static const struct got_error *
3992 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3993 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3995 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3996 666f7b10 2022-06-23 thomas char *exstr = NULL;
3997 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3998 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3999 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
4000 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
4002 41605754 2020-11-12 stsp *wtotal = 0;
4004 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
4005 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
4007 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
4008 666f7b10 2022-06-23 thomas if (err)
4009 666f7b10 2022-06-23 thomas return err;
4011 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
4012 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
4013 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
4014 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
4015 666f7b10 2022-06-23 thomas goto done;
4017 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
4018 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
4019 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4020 cbea3800 2022-06-23 thomas goto done;
4022 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
4023 1065461d 2022-06-23 thomas if (seg2 == NULL) {
4024 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
4025 cbea3800 2022-06-23 thomas goto done;
4028 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
4029 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
4030 cbea3800 2022-06-23 thomas col_tab_align, 1);
4031 cbea3800 2022-06-23 thomas if (err)
4032 cbea3800 2022-06-23 thomas goto done;
4033 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
4034 05171be4 2022-06-23 thomas if (n) {
4035 cbea3800 2022-06-23 thomas free(wline);
4036 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
4037 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4038 cbea3800 2022-06-23 thomas if (err)
4039 cbea3800 2022-06-23 thomas goto done;
4040 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4041 cbea3800 2022-06-23 thomas wlimit -= width;
4042 cbea3800 2022-06-23 thomas *wtotal += width;
4045 41605754 2020-11-12 stsp if (wlimit > 0) {
4046 cbea3800 2022-06-23 thomas int i = 0, w = 0;
4047 cbea3800 2022-06-23 thomas size_t wlen;
4049 cbea3800 2022-06-23 thomas free(wline);
4050 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
4051 cbea3800 2022-06-23 thomas col_tab_align, 1);
4052 cbea3800 2022-06-23 thomas if (err)
4053 cbea3800 2022-06-23 thomas goto done;
4054 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
4055 cbea3800 2022-06-23 thomas while (i < wlen) {
4056 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
4057 cbea3800 2022-06-23 thomas if (width == -1) {
4058 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
4059 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
4060 cbea3800 2022-06-23 thomas goto done;
4062 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
4064 1c5e5faa 2022-06-23 thomas w += width;
4067 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
4068 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
4069 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
4070 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
4071 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
4072 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
4073 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
4077 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
4078 cbea3800 2022-06-23 thomas free(wline);
4079 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
4080 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
4081 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
4082 cbea3800 2022-06-23 thomas col_tab_align, 1);
4083 cbea3800 2022-06-23 thomas if (err)
4084 cbea3800 2022-06-23 thomas goto done;
4085 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
4086 cbea3800 2022-06-23 thomas } else {
4087 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
4088 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
4089 cbea3800 2022-06-23 thomas if (err)
4090 cbea3800 2022-06-23 thomas goto done;
4091 cbea3800 2022-06-23 thomas waddwstr(window, wline);
4093 cbea3800 2022-06-23 thomas *wtotal += width2;
4096 05171be4 2022-06-23 thomas free(wline);
4097 666f7b10 2022-06-23 thomas free(exstr);
4098 cbea3800 2022-06-23 thomas free(seg0);
4099 cbea3800 2022-06-23 thomas free(seg1);
4100 cbea3800 2022-06-23 thomas free(seg2);
4101 cbea3800 2022-06-23 thomas return err;
4104 07dd3ed3 2022-08-06 thomas static int
4105 07dd3ed3 2022-08-06 thomas gotoline(struct tog_view *view, int *lineno, int *nprinted)
4107 07dd3ed3 2022-08-06 thomas FILE *f = NULL;
4108 07dd3ed3 2022-08-06 thomas int *eof, *first, *selected;
4110 07dd3ed3 2022-08-06 thomas if (view->type == TOG_VIEW_DIFF) {
4111 07dd3ed3 2022-08-06 thomas struct tog_diff_view_state *s = &view->state.diff;
4113 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4114 07dd3ed3 2022-08-06 thomas selected = first;
4115 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4116 07dd3ed3 2022-08-06 thomas f = s->f;
4117 07dd3ed3 2022-08-06 thomas } else if (view->type == TOG_VIEW_BLAME) {
4118 07dd3ed3 2022-08-06 thomas struct tog_blame_view_state *s = &view->state.blame;
4120 07dd3ed3 2022-08-06 thomas first = &s->first_displayed_line;
4121 07dd3ed3 2022-08-06 thomas selected = &s->selected_line;
4122 07dd3ed3 2022-08-06 thomas eof = &s->eof;
4123 07dd3ed3 2022-08-06 thomas f = s->blame.f;
4125 07dd3ed3 2022-08-06 thomas return 0;
4127 07dd3ed3 2022-08-06 thomas /* Center gline in the middle of the page like vi(1). */
4128 07dd3ed3 2022-08-06 thomas if (*lineno < view->gline - (view->nlines - 3) / 2)
4129 07dd3ed3 2022-08-06 thomas return 0;
4130 07dd3ed3 2022-08-06 thomas if (*first != 1 && (*lineno > view->gline - (view->nlines - 3) / 2)) {
4131 07dd3ed3 2022-08-06 thomas rewind(f);
4132 07dd3ed3 2022-08-06 thomas *eof = 0;
4133 07dd3ed3 2022-08-06 thomas *first = 1;
4134 07dd3ed3 2022-08-06 thomas *lineno = 0;
4135 07dd3ed3 2022-08-06 thomas *nprinted = 0;
4136 07dd3ed3 2022-08-06 thomas return 0;
4139 07dd3ed3 2022-08-06 thomas *selected = view->gline <= (view->nlines - 3) / 2 ?
4140 07dd3ed3 2022-08-06 thomas view->gline : (view->nlines - 3) / 2 + 1;
4141 07dd3ed3 2022-08-06 thomas view->gline = 0;
4143 07dd3ed3 2022-08-06 thomas return 1;
4146 41605754 2020-11-12 stsp static const struct got_error *
4147 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
4149 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
4150 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4151 61e69b96 2018-05-20 stsp const struct got_error *err;
4152 82c78e96 2022-08-06 thomas int nprinted = 0;
4153 b304db33 2018-05-20 stsp char *line;
4154 826082fe 2020-12-10 stsp size_t linesize = 0;
4155 826082fe 2020-12-10 stsp ssize_t linelen;
4156 61e69b96 2018-05-20 stsp wchar_t *wline;
4157 e0b650dd 2018-05-20 stsp int width;
4158 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
4159 89f1a395 2020-12-01 naddy int nlines = s->nlines;
4160 fe621944 2020-11-10 stsp off_t line_offset;
4162 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1;
4163 82c78e96 2022-08-06 thomas line_offset = s->lines[s->first_displayed_line - 1].offset;
4164 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
4165 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
4167 f7d12f7e 2018-08-01 stsp werase(view->window);
4169 07dd3ed3 2022-08-06 thomas if (view->gline > s->nlines - 1)
4170 07dd3ed3 2022-08-06 thomas view->gline = s->nlines - 1;
4172 a3404814 2018-09-02 stsp if (header) {
4173 07dd3ed3 2022-08-06 thomas int ln = view->gline ? view->gline <= (view->nlines - 3) / 2 ?
4174 07dd3ed3 2022-08-06 thomas 1 : view->gline - (view->nlines - 3) / 2 :
4175 82c78e96 2022-08-06 thomas s->lineno + s->selected_line;
4177 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s", ln, nlines, header) == -1)
4178 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
4179 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
4181 135a2da0 2020-11-11 stsp free(line);
4183 a3404814 2018-09-02 stsp return err;
4185 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4186 a3404814 2018-09-02 stsp wstandout(view->window);
4187 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
4188 e54cc94a 2020-11-11 stsp free(wline);
4189 e54cc94a 2020-11-11 stsp wline = NULL;
4190 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
4191 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
4192 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4193 a3404814 2018-09-02 stsp wstandend(view->window);
4195 a3404814 2018-09-02 stsp if (max_lines <= 1)
4196 a3404814 2018-09-02 stsp return NULL;
4197 a3404814 2018-09-02 stsp max_lines--;
4200 89f1a395 2020-12-01 naddy s->eof = 0;
4201 05171be4 2022-06-23 thomas view->maxx = 0;
4202 826082fe 2020-12-10 stsp line = NULL;
4203 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
4204 6568f0aa 2022-08-06 thomas enum got_diff_line_type linetype;
4205 6568f0aa 2022-08-06 thomas attr_t attr = 0;
4207 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4208 826082fe 2020-12-10 stsp if (linelen == -1) {
4209 826082fe 2020-12-10 stsp if (feof(s->f)) {
4210 826082fe 2020-12-10 stsp s->eof = 1;
4213 826082fe 2020-12-10 stsp free(line);
4214 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
4217 82c78e96 2022-08-06 thomas if (++s->lineno < s->first_displayed_line)
4218 07dd3ed3 2022-08-06 thomas continue;
4219 82c78e96 2022-08-06 thomas if (view->gline && !gotoline(view, &s->lineno, &nprinted))
4220 07dd3ed3 2022-08-06 thomas continue;
4221 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline)
4222 07dd3ed3 2022-08-06 thomas attr = A_STANDOUT;
4224 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4225 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
4226 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
4227 cbea3800 2022-06-23 thomas if (err) {
4228 cbea3800 2022-06-23 thomas free(line);
4229 cbea3800 2022-06-23 thomas return err;
4231 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4232 cbea3800 2022-06-23 thomas free(wline);
4233 cbea3800 2022-06-23 thomas wline = NULL;
4235 6568f0aa 2022-08-06 thomas linetype = s->lines[s->lineno].type;
4236 6568f0aa 2022-08-06 thomas if (linetype > GOT_DIFF_LINE_LOGMSG &&
4237 6568f0aa 2022-08-06 thomas linetype < GOT_DIFF_LINE_CONTEXT)
4238 6568f0aa 2022-08-06 thomas attr |= COLOR_PAIR(linetype);
4239 07dd3ed3 2022-08-06 thomas if (attr)
4240 07dd3ed3 2022-08-06 thomas wattron(view->window, attr);
4241 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
4242 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4243 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
4244 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4245 41605754 2020-11-12 stsp if (err) {
4246 41605754 2020-11-12 stsp free(line);
4247 41605754 2020-11-12 stsp return err;
4250 f1c0ec19 2022-06-23 thomas int skip;
4251 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4252 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
4253 f1c0ec19 2022-06-23 thomas if (err) {
4254 f1c0ec19 2022-06-23 thomas free(line);
4255 f1c0ec19 2022-06-23 thomas return err;
4257 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4258 f1c0ec19 2022-06-23 thomas free(wline);
4259 41605754 2020-11-12 stsp wline = NULL;
4261 82c78e96 2022-08-06 thomas if (s->lineno == view->hiline) {
4262 07dd3ed3 2022-08-06 thomas /* highlight full gline length */
4263 07dd3ed3 2022-08-06 thomas while (width++ < view->ncols)
4264 07dd3ed3 2022-08-06 thomas waddch(view->window, ' ');
4265 07dd3ed3 2022-08-06 thomas } else {
4266 07dd3ed3 2022-08-06 thomas if (width <= view->ncols - 1)
4267 07dd3ed3 2022-08-06 thomas waddch(view->window, '\n');
4269 07dd3ed3 2022-08-06 thomas if (attr)
4270 07dd3ed3 2022-08-06 thomas wattroff(view->window, attr);
4271 07dd3ed3 2022-08-06 thomas if (++nprinted == 1)
4272 82c78e96 2022-08-06 thomas s->first_displayed_line = s->lineno;
4274 826082fe 2020-12-10 stsp free(line);
4275 fe621944 2020-11-10 stsp if (nprinted >= 1)
4276 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
4277 89f1a395 2020-12-01 naddy (nprinted - 1);
4279 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
4281 a5d43cac 2022-07-01 thomas view_border(view);
4283 89f1a395 2020-12-01 naddy if (s->eof) {
4284 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
4285 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
4286 c3e9aa98 2019-05-13 jcs nprinted++;
4289 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
4290 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
4292 c3e9aa98 2019-05-13 jcs return err;
4295 c3e9aa98 2019-05-13 jcs wstandout(view->window);
4296 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
4297 e54cc94a 2020-11-11 stsp free(wline);
4298 e54cc94a 2020-11-11 stsp wline = NULL;
4299 c3e9aa98 2019-05-13 jcs wstandend(view->window);
4302 26ed57b2 2018-05-19 stsp return NULL;
4305 abd2672a 2018-12-23 stsp static char *
4306 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4308 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4309 09867e48 2019-08-13 stsp char *p, *s;
4311 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4312 09867e48 2019-08-13 stsp if (tm == NULL)
4313 09867e48 2019-08-13 stsp return NULL;
4314 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4315 09867e48 2019-08-13 stsp if (s == NULL)
4316 09867e48 2019-08-13 stsp return NULL;
4317 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4319 abd2672a 2018-12-23 stsp *p = '\0';
4323 4ed7e80c 2018-05-20 stsp static const struct got_error *
4324 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4325 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4327 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4328 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4329 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4330 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4332 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4333 0208f208 2020-05-05 stsp if (qid != NULL) {
4334 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4335 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4336 ec242592 2022-04-22 thomas &qid->id);
4338 0208f208 2020-05-05 stsp return err;
4340 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4341 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4342 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4343 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4344 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4346 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4350 0208f208 2020-05-05 stsp if (tree_id1) {
4351 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4353 0208f208 2020-05-05 stsp goto done;
4356 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4357 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4359 0208f208 2020-05-05 stsp goto done;
4361 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4362 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4364 0208f208 2020-05-05 stsp if (tree1)
4365 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4366 0208f208 2020-05-05 stsp if (tree2)
4367 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4368 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4369 0208f208 2020-05-05 stsp return err;
4372 0208f208 2020-05-05 stsp static const struct got_error *
4373 82c78e96 2022-08-06 thomas add_line_metadata(struct got_diff_line **lines, size_t *nlines,
4374 82c78e96 2022-08-06 thomas off_t off, uint8_t type)
4376 82c78e96 2022-08-06 thomas struct got_diff_line *p;
4378 82c78e96 2022-08-06 thomas p = reallocarray(*lines, *nlines + 1, sizeof(**lines));
4379 fe621944 2020-11-10 stsp if (p == NULL)
4380 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4381 82c78e96 2022-08-06 thomas *lines = p;
4382 82c78e96 2022-08-06 thomas (*lines)[*nlines].offset = off;
4383 82c78e96 2022-08-06 thomas (*lines)[*nlines].type = type;
4384 fe621944 2020-11-10 stsp (*nlines)++;
4386 fe621944 2020-11-10 stsp return NULL;
4389 fe621944 2020-11-10 stsp static const struct got_error *
4390 82c78e96 2022-08-06 thomas write_commit_info(struct got_diff_line **lines, size_t *nlines,
4391 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4392 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4394 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4395 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4396 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4397 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4398 45d799e2 2018-12-23 stsp time_t committer_time;
4399 45d799e2 2018-12-23 stsp const char *author, *committer;
4400 8b473291 2019-02-21 stsp char *refs_str = NULL;
4401 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4402 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4403 fe621944 2020-11-10 stsp off_t outoff = 0;
4406 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4408 8b473291 2019-02-21 stsp if (refs) {
4409 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4411 8b473291 2019-02-21 stsp return err;
4414 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4416 abd2672a 2018-12-23 stsp return err;
4418 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4419 15a94983 2018-12-23 stsp if (err) {
4420 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4421 15a94983 2018-12-23 stsp goto done;
4424 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, 0, GOT_DIFF_LINE_NONE);
4426 fe621944 2020-11-10 stsp goto done;
4428 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4429 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4430 fe621944 2020-11-10 stsp if (n < 0) {
4431 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4432 abd2672a 2018-12-23 stsp goto done;
4434 fe621944 2020-11-10 stsp outoff += n;
4435 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_META);
4437 fe621944 2020-11-10 stsp goto done;
4439 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4440 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4441 fe621944 2020-11-10 stsp if (n < 0) {
4442 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4443 abd2672a 2018-12-23 stsp goto done;
4445 fe621944 2020-11-10 stsp outoff += n;
4446 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_AUTHOR);
4448 fe621944 2020-11-10 stsp goto done;
4450 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4451 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4452 fe621944 2020-11-10 stsp if (datestr) {
4453 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4454 fe621944 2020-11-10 stsp if (n < 0) {
4455 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4456 fe621944 2020-11-10 stsp goto done;
4458 fe621944 2020-11-10 stsp outoff += n;
4459 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4460 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_DATE);
4462 fe621944 2020-11-10 stsp goto done;
4464 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4465 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4466 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4467 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4468 fe621944 2020-11-10 stsp if (n < 0) {
4469 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4470 fe621944 2020-11-10 stsp goto done;
4472 fe621944 2020-11-10 stsp outoff += n;
4473 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4474 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_AUTHOR);
4476 fe621944 2020-11-10 stsp goto done;
4478 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4479 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4480 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4481 ac4dc263 2021-09-24 thomas int pn = 1;
4482 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4483 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4484 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4485 ac4dc263 2021-09-24 thomas if (err)
4486 ac4dc263 2021-09-24 thomas goto done;
4487 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4488 ac4dc263 2021-09-24 thomas if (n < 0) {
4489 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4490 ac4dc263 2021-09-24 thomas goto done;
4492 ac4dc263 2021-09-24 thomas outoff += n;
4493 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4494 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_META);
4495 ac4dc263 2021-09-24 thomas if (err)
4496 ac4dc263 2021-09-24 thomas goto done;
4497 ac4dc263 2021-09-24 thomas free(id_str);
4498 ac4dc263 2021-09-24 thomas id_str = NULL;
4502 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4504 5943eee2 2019-08-13 stsp goto done;
4505 fe621944 2020-11-10 stsp s = logmsg;
4506 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4507 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4508 fe621944 2020-11-10 stsp if (n < 0) {
4509 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4510 fe621944 2020-11-10 stsp goto done;
4512 fe621944 2020-11-10 stsp outoff += n;
4513 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4514 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_LOGMSG);
4516 fe621944 2020-11-10 stsp goto done;
4519 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4521 0208f208 2020-05-05 stsp goto done;
4522 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4523 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4524 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4525 fe621944 2020-11-10 stsp if (n < 0) {
4526 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4527 fe621944 2020-11-10 stsp goto done;
4529 fe621944 2020-11-10 stsp outoff += n;
4530 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff,
4531 82c78e96 2022-08-06 thomas GOT_DIFF_LINE_CHANGES);
4533 fe621944 2020-11-10 stsp goto done;
4534 0208f208 2020-05-05 stsp free((char *)pe->path);
4535 0208f208 2020-05-05 stsp free(pe->data);
4538 0208f208 2020-05-05 stsp fputc('\n', outfile);
4540 82c78e96 2022-08-06 thomas err = add_line_metadata(lines, nlines, outoff, GOT_DIFF_LINE_NONE);
4542 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4543 abd2672a 2018-12-23 stsp free(id_str);
4544 5943eee2 2019-08-13 stsp free(logmsg);
4545 8b473291 2019-02-21 stsp free(refs_str);
4546 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4547 7510f233 2020-08-09 stsp if (err) {
4548 82c78e96 2022-08-06 thomas free(*lines);
4549 82c78e96 2022-08-06 thomas *lines = NULL;
4550 ae6a6978 2020-08-09 stsp *nlines = 0;
4552 fe621944 2020-11-10 stsp return err;
4555 abd2672a 2018-12-23 stsp static const struct got_error *
4556 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4558 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4559 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4560 15a94983 2018-12-23 stsp int obj_type;
4562 82c78e96 2022-08-06 thomas free(s->lines);
4563 82c78e96 2022-08-06 thomas s->lines = malloc(sizeof(*s->lines));
4564 82c78e96 2022-08-06 thomas if (s->lines == NULL)
4565 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4566 fe621944 2020-11-10 stsp s->nlines = 0;
4568 511a516b 2018-05-19 stsp f = got_opentemp();
4569 48ae06ee 2018-10-18 stsp if (f == NULL) {
4570 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4571 48ae06ee 2018-10-18 stsp goto done;
4573 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4574 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4575 fb43ecf1 2019-02-11 stsp goto done;
4579 15a94983 2018-12-23 stsp if (s->id1)
4580 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4582 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4584 15a94983 2018-12-23 stsp goto done;
4586 15a94983 2018-12-23 stsp switch (obj_type) {
4587 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4588 82c78e96 2022-08-06 thomas err = got_diff_objects_as_blobs(&s->lines, &s->nlines,
4589 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4590 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4591 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4593 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4594 82c78e96 2022-08-06 thomas err = got_diff_objects_as_trees(&s->lines, &s->nlines,
4595 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4596 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4597 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4599 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4600 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4601 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4602 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4603 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4605 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4607 3ffacbe1 2020-02-02 tracey goto done;
4608 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4609 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4610 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4611 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines, &s->nlines, s->id2,
4612 82c78e96 2022-08-06 thomas refs, s->repo, s->f);
4613 f44b1f58 2020-02-02 tracey if (err)
4614 f44b1f58 2020-02-02 tracey goto done;
4615 f44b1f58 2020-02-02 tracey } else {
4616 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4617 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4618 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4619 82c78e96 2022-08-06 thomas err = write_commit_info(&s->lines,
4620 82c78e96 2022-08-06 thomas &s->nlines, s->id2, refs, s->repo,
4622 f44b1f58 2020-02-02 tracey if (err)
4623 f44b1f58 2020-02-02 tracey goto done;
4628 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4630 82c78e96 2022-08-06 thomas err = got_diff_objects_as_commits(&s->lines, &s->nlines,
4631 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4632 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4633 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4637 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4641 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4642 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4643 48ae06ee 2018-10-18 stsp return err;
4646 f5215bb9 2019-02-22 stsp static void
4647 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4649 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4650 f5215bb9 2019-02-22 stsp update_panels();
4651 f5215bb9 2019-02-22 stsp doupdate();
4654 f44b1f58 2020-02-02 tracey static const struct got_error *
4655 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4657 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4659 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4660 f44b1f58 2020-02-02 tracey return NULL;
4663 f44b1f58 2020-02-02 tracey static const struct got_error *
4664 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4666 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4667 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4668 f44b1f58 2020-02-02 tracey int lineno;
4669 78eecffc 2022-06-23 thomas char *line = NULL;
4670 826082fe 2020-12-10 stsp size_t linesize = 0;
4671 826082fe 2020-12-10 stsp ssize_t linelen;
4673 f44b1f58 2020-02-02 tracey if (!view->searching) {
4674 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4675 f44b1f58 2020-02-02 tracey return NULL;
4678 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4679 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4680 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4682 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4684 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4686 f44b1f58 2020-02-02 tracey while (1) {
4687 f44b1f58 2020-02-02 tracey off_t offset;
4689 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4690 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4691 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4695 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4696 f44b1f58 2020-02-02 tracey lineno = 1;
4698 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4701 82c78e96 2022-08-06 thomas offset = s->lines[lineno - 1].offset;
4702 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4703 f44b1f58 2020-02-02 tracey free(line);
4704 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4706 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4707 78eecffc 2022-06-23 thomas if (linelen != -1) {
4708 78eecffc 2022-06-23 thomas char *exstr;
4709 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4710 78eecffc 2022-06-23 thomas if (err)
4712 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4713 78eecffc 2022-06-23 thomas &view->regmatch)) {
4714 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4715 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4716 78eecffc 2022-06-23 thomas free(exstr);
4719 78eecffc 2022-06-23 thomas free(exstr);
4721 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4722 f44b1f58 2020-02-02 tracey lineno++;
4724 f44b1f58 2020-02-02 tracey lineno--;
4726 826082fe 2020-12-10 stsp free(line);
4728 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4729 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4730 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4733 05171be4 2022-06-23 thomas return err;
4736 48ae06ee 2018-10-18 stsp static const struct got_error *
4737 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4739 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4740 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4742 a0f32f33 2022-06-13 thomas free(s->id1);
4743 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4744 a0f32f33 2022-06-13 thomas free(s->id2);
4745 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4746 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4747 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4748 a0f32f33 2022-06-13 thomas s->f = NULL;
4749 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4750 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4751 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4752 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4753 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4754 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4755 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4756 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4757 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4758 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4759 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4760 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4761 82c78e96 2022-08-06 thomas free(s->lines);
4762 82c78e96 2022-08-06 thomas s->lines = NULL;
4763 a0f32f33 2022-06-13 thomas s->nlines = 0;
4764 a0f32f33 2022-06-13 thomas return err;
4767 a0f32f33 2022-06-13 thomas static const struct got_error *
4768 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4769 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4770 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4771 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4773 48ae06ee 2018-10-18 stsp const struct got_error *err;
4774 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4776 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4777 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4778 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4780 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4781 15a94983 2018-12-23 stsp int type1, type2;
4782 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4784 15a94983 2018-12-23 stsp return err;
4785 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4787 15a94983 2018-12-23 stsp return err;
4789 15a94983 2018-12-23 stsp if (type1 != type2)
4790 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4792 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4793 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4794 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4795 f44b1f58 2020-02-02 tracey s->repo = repo;
4796 f44b1f58 2020-02-02 tracey s->id1 = id1;
4797 f44b1f58 2020-02-02 tracey s->id2 = id2;
4798 3dbaef42 2020-11-24 stsp s->label1 = label1;
4799 3dbaef42 2020-11-24 stsp s->label2 = label2;
4801 15a94983 2018-12-23 stsp if (id1) {
4802 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4803 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4804 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4806 5465d566 2020-02-01 tracey s->id1 = NULL;
4808 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4809 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4810 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4811 a0f32f33 2022-06-13 thomas goto done;
4814 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4815 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4816 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4817 9a1d7435 2022-06-23 thomas goto done;
4820 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4821 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4822 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4823 a0f32f33 2022-06-13 thomas goto done;
4826 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4827 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4828 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4829 19a6a6b5 2022-07-01 thomas goto done;
4832 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4833 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4834 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4835 19a6a6b5 2022-07-01 thomas goto done;
4838 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4839 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4840 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4841 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4842 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4843 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
4844 5465d566 2020-02-01 tracey s->repo = repo;
4846 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4849 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_MINUS,
4850 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_MINUS"), -1);
4851 6568f0aa 2022-08-06 thomas if (rc != ERR)
4852 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_PLUS,
4853 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_PLUS"), -1);
4854 6568f0aa 2022-08-06 thomas if (rc != ERR)
4855 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_HUNK,
4856 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"), -1);
4857 6568f0aa 2022-08-06 thomas if (rc != ERR)
4858 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_META,
4859 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4860 6568f0aa 2022-08-06 thomas if (rc != ERR)
4861 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_CHANGES,
4862 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4863 6568f0aa 2022-08-06 thomas if (rc != ERR)
4864 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_MIN,
4865 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4866 6568f0aa 2022-08-06 thomas if (rc != ERR)
4867 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_BLOB_PLUS,
4868 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DIFF_META"), -1);
4869 6568f0aa 2022-08-06 thomas if (rc != ERR)
4870 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_AUTHOR,
4871 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_AUTHOR"), -1);
4872 6568f0aa 2022-08-06 thomas if (rc != ERR)
4873 6568f0aa 2022-08-06 thomas rc = init_pair(GOT_DIFF_LINE_DATE,
4874 6568f0aa 2022-08-06 thomas get_color_value("TOG_COLOR_DATE"), -1);
4875 6568f0aa 2022-08-06 thomas if (rc == ERR) {
4876 6568f0aa 2022-08-06 thomas err = got_error(GOT_ERR_RANGE);
4877 a0f32f33 2022-06-13 thomas goto done;
4881 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4882 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
4883 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
4884 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4886 5465d566 2020-02-01 tracey err = create_diff(s);
4888 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4889 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4890 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4891 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4892 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4893 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4895 a0f32f33 2022-06-13 thomas if (err)
4896 a0f32f33 2022-06-13 thomas close_diff_view(view);
4897 e5a0f69f 2018-08-18 stsp return err;
4900 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4901 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4903 a3404814 2018-09-02 stsp const struct got_error *err;
4904 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4905 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4906 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4908 a3404814 2018-09-02 stsp if (s->id1) {
4909 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4911 a3404814 2018-09-02 stsp return err;
4912 d3f8b1f9 2022-08-31 thomas label1 = s->label1 ? s->label1 : id_str1;
4914 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4916 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4918 a3404814 2018-09-02 stsp return err;
4919 d3f8b1f9 2022-08-31 thomas label2 = s->label2 ? s->label2 : id_str2;
4921 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4922 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4923 a3404814 2018-09-02 stsp free(id_str1);
4924 a3404814 2018-09-02 stsp free(id_str2);
4925 a3404814 2018-09-02 stsp return err;
4927 a3404814 2018-09-02 stsp free(id_str1);
4928 a3404814 2018-09-02 stsp free(id_str2);
4930 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4931 267bb3b8 2021-08-01 stsp free(header);
4932 267bb3b8 2021-08-01 stsp return err;
4935 15a087fe 2019-02-21 stsp static const struct got_error *
4936 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4937 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4939 d7a04538 2019-02-21 stsp const struct got_error *err;
4940 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4941 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4942 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4944 15a087fe 2019-02-21 stsp free(s->id2);
4945 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4946 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4947 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4949 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4951 d7a04538 2019-02-21 stsp return err;
4952 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4953 15a087fe 2019-02-21 stsp free(s->id1);
4954 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4955 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4956 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4957 15a087fe 2019-02-21 stsp return NULL;
4960 0cf4efb1 2018-09-29 stsp static const struct got_error *
4961 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4963 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4965 adf4c9e0 2022-07-03 thomas view->count = 0;
4966 adf4c9e0 2022-07-03 thomas wclear(view->window);
4967 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4968 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4969 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4970 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4971 adf4c9e0 2022-07-03 thomas return create_diff(s);
4974 82c78e96 2022-08-06 thomas static void
4975 82c78e96 2022-08-06 thomas diff_prev_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4977 82c78e96 2022-08-06 thomas int start, i;
4979 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line - 1;
4981 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
4982 82c78e96 2022-08-06 thomas if (i == 0)
4983 82c78e96 2022-08-06 thomas i = s->nlines - 1;
4984 82c78e96 2022-08-06 thomas if (--i == start)
4985 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
4988 82c78e96 2022-08-06 thomas s->selected_line = 1;
4989 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
4992 82c78e96 2022-08-06 thomas static void
4993 82c78e96 2022-08-06 thomas diff_next_index(struct tog_diff_view_state *s, enum got_diff_line_type type)
4995 82c78e96 2022-08-06 thomas int start, i;
4997 82c78e96 2022-08-06 thomas i = start = s->first_displayed_line + 1;
4999 82c78e96 2022-08-06 thomas while (s->lines[i].type != type) {
5000 82c78e96 2022-08-06 thomas if (i == s->nlines - 1)
5002 82c78e96 2022-08-06 thomas if (++i == start)
5003 82c78e96 2022-08-06 thomas return; /* do nothing, requested type not in file */
5006 82c78e96 2022-08-06 thomas s->selected_line = 1;
5007 82c78e96 2022-08-06 thomas s->first_displayed_line = i;
5010 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
5011 4fc71f3b 2022-07-12 thomas int, int, int);
5012 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
5013 4fc71f3b 2022-07-12 thomas int, int);
5015 adf4c9e0 2022-07-03 thomas static const struct got_error *
5016 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
5018 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
5019 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
5020 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
5021 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
5022 826082fe 2020-12-10 stsp char *line = NULL;
5023 826082fe 2020-12-10 stsp size_t linesize = 0;
5024 826082fe 2020-12-10 stsp ssize_t linelen;
5025 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
5027 82c78e96 2022-08-06 thomas s->lineno = s->first_displayed_line - 1 + s->selected_line;
5029 e5a0f69f 2018-08-18 stsp switch (ch) {
5030 05171be4 2022-06-23 thomas case '0':
5031 05171be4 2022-06-23 thomas view->x = 0;
5033 05171be4 2022-06-23 thomas case '$':
5034 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5035 07b0611c 2022-06-23 thomas view->count = 0;
5037 05171be4 2022-06-23 thomas case KEY_RIGHT:
5038 05171be4 2022-06-23 thomas case 'l':
5039 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5040 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5042 07b0611c 2022-06-23 thomas view->count = 0;
5044 05171be4 2022-06-23 thomas case KEY_LEFT:
5045 05171be4 2022-06-23 thomas case 'h':
5046 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5047 07b0611c 2022-06-23 thomas if (view->x <= 0)
5048 07b0611c 2022-06-23 thomas view->count = 0;
5052 3dbaef42 2020-11-24 stsp if (ch == 'a')
5053 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
5054 3dbaef42 2020-11-24 stsp if (ch == 'w')
5055 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
5056 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
5059 912a3f79 2021-08-30 j case KEY_HOME:
5060 912a3f79 2021-08-30 j s->first_displayed_line = 1;
5061 07b0611c 2022-06-23 thomas view->count = 0;
5064 912a3f79 2021-08-30 j case KEY_END:
5065 07b0611c 2022-06-23 thomas view->count = 0;
5069 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
5073 1e37a5c2 2019-05-12 jcs case KEY_UP:
5074 f7140bf5 2021-10-17 thomas case CTRL('p'):
5075 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
5076 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5078 07b0611c 2022-06-23 thomas view->count = 0;
5080 70f17a53 2022-06-13 thomas case CTRL('u'):
5081 23427b14 2022-06-23 thomas case 'u':
5082 70f17a53 2022-06-13 thomas nscroll /= 2;
5083 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5084 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5085 a60a9dc4 2019-05-13 jcs case CTRL('b'):
5086 1c5e5faa 2022-06-23 thomas case 'b':
5087 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
5088 07b0611c 2022-06-23 thomas view->count = 0;
5092 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
5093 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5096 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5097 f7140bf5 2021-10-17 thomas case CTRL('n'):
5098 1e37a5c2 2019-05-12 jcs if (!s->eof)
5099 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5101 07b0611c 2022-06-23 thomas view->count = 0;
5103 70f17a53 2022-06-13 thomas case CTRL('d'):
5104 23427b14 2022-06-23 thomas case 'd':
5105 70f17a53 2022-06-13 thomas nscroll /= 2;
5106 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5107 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5108 a60a9dc4 2019-05-13 jcs case CTRL('f'):
5109 1c5e5faa 2022-06-23 thomas case 'f':
5111 07b0611c 2022-06-23 thomas if (s->eof) {
5112 07b0611c 2022-06-23 thomas view->count = 0;
5116 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
5117 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
5118 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5119 826082fe 2020-12-10 stsp if (linelen == -1) {
5120 826082fe 2020-12-10 stsp if (feof(s->f)) {
5121 826082fe 2020-12-10 stsp s->eof = 1;
5123 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
5127 826082fe 2020-12-10 stsp free(line);
5129 82c78e96 2022-08-06 thomas case '(':
5130 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_BLOB_MIN);
5132 82c78e96 2022-08-06 thomas case ')':
5133 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_BLOB_MIN);
5135 82c78e96 2022-08-06 thomas case '{':
5136 82c78e96 2022-08-06 thomas diff_prev_index(s, GOT_DIFF_LINE_HUNK);
5138 82c78e96 2022-08-06 thomas case '}':
5139 82c78e96 2022-08-06 thomas diff_next_index(s, GOT_DIFF_LINE_HUNK);
5142 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
5143 1e37a5c2 2019-05-12 jcs s->diff_context--;
5144 aa61903a 2021-12-31 thomas s->matched_line = 0;
5145 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5146 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5147 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
5148 27829c9e 2020-11-21 stsp s->nlines) {
5149 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
5150 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
5153 07b0611c 2022-06-23 thomas view->count = 0;
5156 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
5157 1e37a5c2 2019-05-12 jcs s->diff_context++;
5158 aa61903a 2021-12-31 thomas s->matched_line = 0;
5159 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5160 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5162 07b0611c 2022-06-23 thomas view->count = 0;
5166 777aae21 2022-07-20 thomas case 'K':
5168 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
5169 4fc71f3b 2022-07-12 thomas case '>':
5170 4fc71f3b 2022-07-12 thomas case '.':
5171 777aae21 2022-07-20 thomas case 'J':
5172 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
5173 07b0611c 2022-06-23 thomas view->count = 0;
5176 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
5178 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
5179 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
5180 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
5182 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
5183 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5184 4fc71f3b 2022-07-12 thomas if (err)
5186 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5188 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
5191 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
5192 4fc71f3b 2022-07-12 thomas if (err)
5194 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
5195 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
5196 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
5198 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
5199 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
5200 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
5202 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
5203 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
5204 4fc71f3b 2022-07-12 thomas if (err)
5206 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
5208 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
5210 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
5211 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
5212 4fc71f3b 2022-07-12 thomas bs->selected_line);
5213 4fc71f3b 2022-07-12 thomas if (id == NULL)
5216 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
5219 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
5220 4fc71f3b 2022-07-12 thomas if (err)
5223 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5224 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
5225 aa61903a 2021-12-31 thomas s->matched_line = 0;
5226 05171be4 2022-06-23 thomas view->x = 0;
5228 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
5229 1e37a5c2 2019-05-12 jcs err = create_diff(s);
5232 07b0611c 2022-06-23 thomas view->count = 0;
5236 bcbd79e2 2018-08-19 stsp return err;
5239 4ed7e80c 2018-05-20 stsp static const struct got_error *
5240 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
5242 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
5243 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
5244 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
5245 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
5246 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
5247 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
5248 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
5249 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
5250 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
5251 3dbaef42 2020-11-24 stsp const char *errstr;
5252 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
5253 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5255 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
5256 26ed57b2 2018-05-19 stsp switch (ch) {
5258 64453f7e 2020-11-21 stsp force_text_diff = 1;
5261 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5263 3dbaef42 2020-11-24 stsp if (errstr != NULL)
5264 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
5265 8f666e67 2022-02-12 thomas errstr, errstr);
5267 09b5bff8 2020-02-23 naddy case 'r':
5268 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
5269 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
5270 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
5272 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
5275 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
5278 17020d27 2019-03-07 stsp usage_diff();
5279 26ed57b2 2018-05-19 stsp /* NOTREACHED */
5283 26ed57b2 2018-05-19 stsp argc -= optind;
5284 26ed57b2 2018-05-19 stsp argv += optind;
5286 26ed57b2 2018-05-19 stsp if (argc == 0) {
5287 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
5288 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
5289 15a94983 2018-12-23 stsp id_str1 = argv[0];
5290 15a94983 2018-12-23 stsp id_str2 = argv[1];
5292 26ed57b2 2018-05-19 stsp usage_diff();
5294 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5295 7cd52833 2022-06-23 thomas if (error)
5296 7cd52833 2022-06-23 thomas goto done;
5298 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
5299 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5300 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5301 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5302 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5303 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5304 c156c7a4 2020-12-18 stsp goto done;
5305 a273ac94 2020-02-23 naddy if (worktree)
5306 a273ac94 2020-02-23 naddy repo_path =
5307 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
5309 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
5310 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5311 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5312 c156c7a4 2020-12-18 stsp goto done;
5316 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5317 eb6600df 2019-01-04 stsp if (error)
5318 eb6600df 2019-01-04 stsp goto done;
5320 a273ac94 2020-02-23 naddy init_curses();
5322 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5323 51a10b52 2020-12-26 stsp if (error)
5324 51a10b52 2020-12-26 stsp goto done;
5326 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5327 26ed57b2 2018-05-19 stsp if (error)
5328 26ed57b2 2018-05-19 stsp goto done;
5330 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
5331 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5332 26ed57b2 2018-05-19 stsp if (error)
5333 26ed57b2 2018-05-19 stsp goto done;
5335 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
5336 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
5337 26ed57b2 2018-05-19 stsp if (error)
5338 26ed57b2 2018-05-19 stsp goto done;
5340 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
5341 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
5342 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5343 ea5e7bb5 2018-08-01 stsp goto done;
5345 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
5346 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
5347 5dc9f4bc 2018-08-04 stsp if (error)
5348 5dc9f4bc 2018-08-04 stsp goto done;
5349 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5351 3dbaef42 2020-11-24 stsp free(label1);
5352 3dbaef42 2020-11-24 stsp free(label2);
5353 c02c541e 2019-03-29 stsp free(repo_path);
5354 a273ac94 2020-02-23 naddy free(cwd);
5355 1d0f4054 2021-06-17 stsp if (repo) {
5356 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5357 1d0f4054 2021-06-17 stsp if (error == NULL)
5358 1d0f4054 2021-06-17 stsp error = close_err;
5360 a273ac94 2020-02-23 naddy if (worktree)
5361 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5362 7cd52833 2022-06-23 thomas if (pack_fds) {
5363 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5364 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5365 7cd52833 2022-06-23 thomas if (error == NULL)
5366 7cd52833 2022-06-23 thomas error = pack_err;
5368 51a10b52 2020-12-26 stsp tog_free_refs();
5369 26ed57b2 2018-05-19 stsp return error;
5372 4ed7e80c 2018-05-20 stsp __dead static void
5373 9f7d7167 2018-04-29 stsp usage_blame(void)
5376 91198554 2022-06-23 thomas fprintf(stderr,
5377 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5378 9f7d7167 2018-04-29 stsp getprogname());
5382 84451b3e 2018-07-10 stsp struct tog_blame_line {
5383 84451b3e 2018-07-10 stsp int annotated;
5384 84451b3e 2018-07-10 stsp struct got_object_id *id;
5387 4ed7e80c 2018-05-20 stsp static const struct got_error *
5388 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5390 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5391 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5392 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5393 84451b3e 2018-07-10 stsp const struct got_error *err;
5394 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5395 826082fe 2020-12-10 stsp char *line = NULL;
5396 826082fe 2020-12-10 stsp size_t linesize = 0;
5397 826082fe 2020-12-10 stsp ssize_t linelen;
5398 84451b3e 2018-07-10 stsp wchar_t *wline;
5399 27a741e5 2019-09-11 stsp int width;
5400 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5401 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5402 ab089a2a 2018-07-12 stsp char *id_str;
5403 11b20872 2019-11-08 stsp struct tog_color *tc;
5405 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5407 ab089a2a 2018-07-12 stsp return err;
5409 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5410 f7d12f7e 2018-08-01 stsp werase(view->window);
5412 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5413 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5414 ab089a2a 2018-07-12 stsp free(id_str);
5415 ab089a2a 2018-07-12 stsp return err;
5418 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5419 ab089a2a 2018-07-12 stsp free(line);
5420 2550e4c3 2018-07-13 stsp line = NULL;
5422 1cae65b4 2019-09-22 stsp return err;
5423 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5424 a3404814 2018-09-02 stsp wstandout(view->window);
5425 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5427 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
5428 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5429 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
5430 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
5432 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
5433 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5434 a3404814 2018-09-02 stsp wstandend(view->window);
5435 2550e4c3 2018-07-13 stsp free(wline);
5436 2550e4c3 2018-07-13 stsp wline = NULL;
5438 07dd3ed3 2022-08-06 thomas if (view->gline > blame->nlines)
5439 07dd3ed3 2022-08-06 thomas view->gline = blame->nlines;
5441 07dd3ed3 2022-08-06 thomas if (asprintf(&line, "[%d/%d] %s%s", view->gline ? view->gline :
5442 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5443 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5444 ab089a2a 2018-07-12 stsp free(id_str);
5445 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5447 ab089a2a 2018-07-12 stsp free(id_str);
5448 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5449 3f60a8ef 2018-07-10 stsp free(line);
5450 2550e4c3 2018-07-13 stsp line = NULL;
5452 3f60a8ef 2018-07-10 stsp return err;
5453 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5454 2550e4c3 2018-07-13 stsp free(wline);
5455 2550e4c3 2018-07-13 stsp wline = NULL;
5456 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5457 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5459 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5460 05171be4 2022-06-23 thomas view->maxx = 0;
5461 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5462 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5463 826082fe 2020-12-10 stsp if (linelen == -1) {
5464 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5465 826082fe 2020-12-10 stsp s->eof = 1;
5468 84451b3e 2018-07-10 stsp free(line);
5469 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5471 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5472 07dd3ed3 2022-08-06 thomas continue;
5473 07dd3ed3 2022-08-06 thomas if (view->gline && !gotoline(view, &lineno, &nprinted))
5476 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5477 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5478 cbea3800 2022-06-23 thomas if (err) {
5479 cbea3800 2022-06-23 thomas free(line);
5480 cbea3800 2022-06-23 thomas return err;
5482 cbea3800 2022-06-23 thomas free(wline);
5483 cbea3800 2022-06-23 thomas wline = NULL;
5484 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5486 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5487 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5489 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5490 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5491 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5492 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5493 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5494 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5495 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5496 8d0fe45a 2019-08-12 stsp char *id_str;
5497 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5498 91198554 2022-06-23 thomas blame_line->id);
5499 8d0fe45a 2019-08-12 stsp if (err) {
5500 8d0fe45a 2019-08-12 stsp free(line);
5501 8d0fe45a 2019-08-12 stsp return err;
5503 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5505 11b20872 2019-11-08 stsp wattr_on(view->window,
5506 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5507 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5509 11b20872 2019-11-08 stsp wattr_off(view->window,
5510 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5511 8d0fe45a 2019-08-12 stsp free(id_str);
5512 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5514 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5515 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5518 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5519 ee41ec32 2018-07-10 stsp prev_id = NULL;
5522 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5523 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5524 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5526 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5527 41605754 2020-11-12 stsp width = 9;
5528 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5529 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5530 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5531 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5532 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5533 41605754 2020-11-12 stsp if (err) {
5534 41605754 2020-11-12 stsp free(line);
5535 41605754 2020-11-12 stsp return err;
5537 41605754 2020-11-12 stsp width += 9;
5539 923086fd 2022-06-23 thomas int skip;
5540 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5541 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5542 923086fd 2022-06-23 thomas if (err) {
5543 923086fd 2022-06-23 thomas free(line);
5544 923086fd 2022-06-23 thomas return err;
5546 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5547 02bce7e2 2022-06-23 thomas width += 9;
5548 41605754 2020-11-12 stsp free(wline);
5549 41605754 2020-11-12 stsp wline = NULL;
5552 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5553 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5554 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5555 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5557 826082fe 2020-12-10 stsp free(line);
5558 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5560 a5d43cac 2022-07-01 thomas view_border(view);
5562 84451b3e 2018-07-10 stsp return NULL;
5565 84451b3e 2018-07-10 stsp static const struct got_error *
5566 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5567 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5569 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5570 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5571 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5572 1a76625f 2018-10-22 stsp int errcode;
5574 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5575 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5576 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5578 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5579 1a76625f 2018-10-22 stsp if (errcode)
5580 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5582 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5583 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5584 d68a0a7d 2018-07-10 stsp goto done;
5587 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5588 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5590 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5591 d68a0a7d 2018-07-10 stsp if (line->annotated)
5592 d68a0a7d 2018-07-10 stsp goto done;
5594 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5595 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5596 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5597 84451b3e 2018-07-10 stsp goto done;
5599 84451b3e 2018-07-10 stsp line->annotated = 1;
5601 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5602 1a76625f 2018-10-22 stsp if (errcode)
5603 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5604 84451b3e 2018-07-10 stsp return err;
5607 84451b3e 2018-07-10 stsp static void *
5608 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5610 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5611 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5612 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5613 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5614 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5616 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5617 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5618 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5620 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5621 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5622 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5623 9117a7b7 2022-07-01 thomas goto done;
5626 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5627 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5628 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5629 9117a7b7 2022-07-01 thomas goto done;
5631 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5632 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5633 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5634 9117a7b7 2022-07-01 thomas goto done;
5637 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5639 9117a7b7 2022-07-01 thomas goto done;
5641 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5642 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5643 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5644 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5645 fc06ba56 2019-08-22 stsp err = NULL;
5647 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5648 9117a7b7 2022-07-01 thomas if (errcode) {
5649 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5650 9117a7b7 2022-07-01 thomas goto done;
5653 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5654 1d0f4054 2021-06-17 stsp if (err == NULL)
5655 1d0f4054 2021-06-17 stsp err = close_err;
5656 c9beca56 2018-07-22 stsp ta->repo = NULL;
5657 c9beca56 2018-07-22 stsp *ta->complete = 1;
5659 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5660 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5661 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5664 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5665 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5666 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5667 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5668 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5669 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5670 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5671 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5673 18430de3 2018-07-10 stsp return (void *)err;
5676 245d91c1 2018-07-12 stsp static struct got_object_id *
5677 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5678 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5680 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5682 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5683 8d0fe45a 2019-08-12 stsp return NULL;
5685 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5686 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5687 4fc71f3b 2022-07-12 thomas return NULL;
5689 4fc71f3b 2022-07-12 thomas return line->id;
5692 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5693 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5694 4fc71f3b 2022-07-12 thomas int lineno)
5696 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5698 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5699 4fc71f3b 2022-07-12 thomas return NULL;
5701 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5702 245d91c1 2018-07-12 stsp if (!line->annotated)
5703 245d91c1 2018-07-12 stsp return NULL;
5705 245d91c1 2018-07-12 stsp return line->id;
5708 b880a918 2018-07-10 stsp static const struct got_error *
5709 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5711 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5714 245d91c1 2018-07-12 stsp if (blame->thread) {
5715 1a76625f 2018-10-22 stsp int errcode;
5716 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5717 1a76625f 2018-10-22 stsp if (errcode)
5718 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5719 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5720 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5721 1a76625f 2018-10-22 stsp if (errcode)
5722 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5723 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5724 1a76625f 2018-10-22 stsp if (errcode)
5725 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5726 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5727 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5728 245d91c1 2018-07-12 stsp err = NULL;
5729 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5731 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5732 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5733 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5734 1d0f4054 2021-06-17 stsp if (err == NULL)
5735 1d0f4054 2021-06-17 stsp err = close_err;
5736 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5738 245d91c1 2018-07-12 stsp if (blame->f) {
5739 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5740 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5741 245d91c1 2018-07-12 stsp blame->f = NULL;
5743 57670559 2018-12-23 stsp if (blame->lines) {
5744 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5745 57670559 2018-12-23 stsp free(blame->lines[i].id);
5746 57670559 2018-12-23 stsp free(blame->lines);
5747 57670559 2018-12-23 stsp blame->lines = NULL;
5749 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5750 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5751 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5752 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5753 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5754 7cd52833 2022-06-23 thomas if (err == NULL)
5755 7cd52833 2022-06-23 thomas err = pack_err;
5756 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5758 245d91c1 2018-07-12 stsp return err;
5761 245d91c1 2018-07-12 stsp static const struct got_error *
5762 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5764 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5765 fc06ba56 2019-08-22 stsp int *done = arg;
5766 fc06ba56 2019-08-22 stsp int errcode;
5768 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5769 fc06ba56 2019-08-22 stsp if (errcode)
5770 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5771 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5773 fc06ba56 2019-08-22 stsp if (*done)
5774 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5776 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5777 fc06ba56 2019-08-22 stsp if (errcode)
5778 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5779 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5781 fc06ba56 2019-08-22 stsp return err;
5784 fc06ba56 2019-08-22 stsp static const struct got_error *
5785 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5787 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5788 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5789 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5790 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5791 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5792 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5793 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5794 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5795 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5797 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5798 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5800 15a94983 2018-12-23 stsp return err;
5802 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5803 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5804 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5805 f4ae6ddb 2022-07-01 thomas goto done;
5808 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5809 945f9229 2022-04-16 thomas if (err)
5810 945f9229 2022-04-16 thomas goto done;
5812 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5814 84451b3e 2018-07-10 stsp goto done;
5816 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5817 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5818 84451b3e 2018-07-10 stsp goto done;
5821 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5823 a70480e0 2018-06-23 stsp goto done;
5824 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5825 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5826 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5827 84451b3e 2018-07-10 stsp goto done;
5829 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5830 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5832 1fddf795 2021-01-20 stsp goto done;
5833 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5834 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5835 84451b3e 2018-07-10 stsp goto done;
5838 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5839 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5840 b02560ec 2019-08-19 stsp blame->nlines--;
5842 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5843 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5844 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5845 84451b3e 2018-07-10 stsp goto done;
5848 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5850 bd24772e 2018-07-11 stsp goto done;
5851 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5852 7cd52833 2022-06-23 thomas pack_fds);
5853 7cd52833 2022-06-23 thomas if (err)
5854 7cd52833 2022-06-23 thomas goto done;
5856 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5857 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5858 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5859 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5860 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5861 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5862 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5863 245d91c1 2018-07-12 stsp goto done;
5865 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5867 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5868 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5869 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5870 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5871 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5872 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5873 a5388363 2020-12-01 naddy s->blame_complete = 0;
5875 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5876 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5877 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5878 f5a09613 2020-12-13 naddy s->selected_line = 1;
5880 aa61903a 2021-12-31 thomas s->matched_line = 0;
5883 945f9229 2022-04-16 thomas if (commit)
5884 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5885 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5886 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5888 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5889 27d434c2 2018-09-15 stsp free(obj_id);
5891 245d91c1 2018-07-12 stsp stop_blame(blame);
5892 245d91c1 2018-07-12 stsp return err;
5895 245d91c1 2018-07-12 stsp static const struct got_error *
5896 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5897 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5899 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5900 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5902 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5904 c4843652 2019-08-12 stsp s->path = strdup(path);
5905 c4843652 2019-08-12 stsp if (s->path == NULL)
5906 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5908 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5909 c4843652 2019-08-12 stsp if (err) {
5910 c4843652 2019-08-12 stsp free(s->path);
5911 7cbe629d 2018-08-04 stsp return err;
5914 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5915 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5916 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5917 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5918 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5919 fb2756b9 2018-08-04 stsp s->repo = repo;
5920 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5921 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5923 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5924 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5925 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5926 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5928 11b20872 2019-11-08 stsp return err;
5931 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5932 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5933 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5934 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5935 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5936 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5938 a5388363 2020-12-01 naddy return run_blame(view);
5941 e5a0f69f 2018-08-18 stsp static const struct got_error *
5942 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5944 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5945 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5947 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5948 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5950 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5951 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5952 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5953 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5954 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5957 e5a0f69f 2018-08-18 stsp free(s->path);
5958 11b20872 2019-11-08 stsp free_colors(&s->colors);
5959 e5a0f69f 2018-08-18 stsp return err;
5962 7cbe629d 2018-08-04 stsp static const struct got_error *
5963 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5965 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5967 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5968 6c4c42e0 2019-06-24 stsp return NULL;
5971 6c4c42e0 2019-06-24 stsp static const struct got_error *
5972 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5974 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5975 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5976 6c4c42e0 2019-06-24 stsp int lineno;
5977 78eecffc 2022-06-23 thomas char *line = NULL;
5978 826082fe 2020-12-10 stsp size_t linesize = 0;
5979 826082fe 2020-12-10 stsp ssize_t linelen;
5981 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5982 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5983 6c4c42e0 2019-06-24 stsp return NULL;
5986 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5987 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5988 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5990 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5992 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5994 6c4c42e0 2019-06-24 stsp while (1) {
5995 6c4c42e0 2019-06-24 stsp off_t offset;
5997 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5998 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5999 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6003 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6004 6c4c42e0 2019-06-24 stsp lineno = 1;
6006 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
6009 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
6010 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
6011 6c4c42e0 2019-06-24 stsp free(line);
6012 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
6014 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
6015 78eecffc 2022-06-23 thomas if (linelen != -1) {
6016 78eecffc 2022-06-23 thomas char *exstr;
6017 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
6018 78eecffc 2022-06-23 thomas if (err)
6020 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
6021 78eecffc 2022-06-23 thomas &view->regmatch)) {
6022 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
6023 78eecffc 2022-06-23 thomas s->matched_line = lineno;
6024 78eecffc 2022-06-23 thomas free(exstr);
6027 78eecffc 2022-06-23 thomas free(exstr);
6029 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6034 826082fe 2020-12-10 stsp free(line);
6036 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
6037 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
6038 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
6041 05171be4 2022-06-23 thomas return err;
6044 6c4c42e0 2019-06-24 stsp static const struct got_error *
6045 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
6047 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6048 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
6049 2b380cc8 2018-10-24 stsp int errcode;
6051 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
6052 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
6053 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
6054 2b380cc8 2018-10-24 stsp if (errcode)
6055 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
6057 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
6060 51fe7530 2019-08-19 stsp if (s->blame_complete)
6061 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
6063 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
6065 a5d43cac 2022-07-01 thomas view_border(view);
6066 e5a0f69f 2018-08-18 stsp return err;
6069 e5a0f69f 2018-08-18 stsp static const struct got_error *
6070 eaeaa612 2022-07-20 thomas log_annotated_line(struct tog_view **new_view, int begin_y, int begin_x,
6071 eaeaa612 2022-07-20 thomas struct got_repository *repo, struct got_object_id *id)
6073 eaeaa612 2022-07-20 thomas struct tog_view *log_view;
6074 eaeaa612 2022-07-20 thomas const struct got_error *err = NULL;
6076 eaeaa612 2022-07-20 thomas *new_view = NULL;
6078 eaeaa612 2022-07-20 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6079 eaeaa612 2022-07-20 thomas if (log_view == NULL)
6080 eaeaa612 2022-07-20 thomas return got_error_from_errno("view_open");
6082 eaeaa612 2022-07-20 thomas err = open_log_view(log_view, id, repo, GOT_REF_HEAD, "", 0);
6083 eaeaa612 2022-07-20 thomas if (err)
6084 eaeaa612 2022-07-20 thomas view_close(log_view);
6086 eaeaa612 2022-07-20 thomas *new_view = log_view;
6088 eaeaa612 2022-07-20 thomas return err;
6091 eaeaa612 2022-07-20 thomas static const struct got_error *
6092 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
6094 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
6095 2a31b33b 2022-07-23 thomas struct tog_view *diff_view;
6096 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
6097 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
6099 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
6100 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6101 a5d43cac 2022-07-01 thomas --eos; /* border */
6103 e5a0f69f 2018-08-18 stsp switch (ch) {
6104 05171be4 2022-06-23 thomas case '0':
6105 05171be4 2022-06-23 thomas view->x = 0;
6107 05171be4 2022-06-23 thomas case '$':
6108 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
6109 07b0611c 2022-06-23 thomas view->count = 0;
6111 05171be4 2022-06-23 thomas case KEY_RIGHT:
6112 05171be4 2022-06-23 thomas case 'l':
6113 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
6114 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
6116 07b0611c 2022-06-23 thomas view->count = 0;
6118 05171be4 2022-06-23 thomas case KEY_LEFT:
6119 05171be4 2022-06-23 thomas case 'h':
6120 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
6121 07b0611c 2022-06-23 thomas if (view->x <= 0)
6122 07b0611c 2022-06-23 thomas view->count = 0;
6125 1e37a5c2 2019-05-12 jcs s->done = 1;
6127 4deef56f 2021-09-02 naddy case 'g':
6128 4deef56f 2021-09-02 naddy case KEY_HOME:
6129 4deef56f 2021-09-02 naddy s->selected_line = 1;
6130 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6131 07b0611c 2022-06-23 thomas view->count = 0;
6133 4deef56f 2021-09-02 naddy case 'G':
6134 4deef56f 2021-09-02 naddy case KEY_END:
6135 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
6136 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
6137 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
6139 a5d43cac 2022-07-01 thomas s->selected_line = eos;
6140 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
6142 07b0611c 2022-06-23 thomas view->count = 0;
6145 1e37a5c2 2019-05-12 jcs case KEY_UP:
6146 f7140bf5 2021-10-17 thomas case CTRL('p'):
6147 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
6148 1e37a5c2 2019-05-12 jcs s->selected_line--;
6149 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
6150 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
6151 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
6153 07b0611c 2022-06-23 thomas view->count = 0;
6155 70f17a53 2022-06-13 thomas case CTRL('u'):
6156 23427b14 2022-06-23 thomas case 'u':
6157 70f17a53 2022-06-13 thomas nscroll /= 2;
6158 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6159 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6160 ea025d1d 2020-02-22 naddy case CTRL('b'):
6161 1c5e5faa 2022-06-23 thomas case 'b':
6162 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
6163 07b0611c 2022-06-23 thomas if (view->count > 1)
6164 07b0611c 2022-06-23 thomas nscroll += nscroll;
6165 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
6166 07b0611c 2022-06-23 thomas view->count = 0;
6169 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
6170 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
6172 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
6175 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6176 f7140bf5 2021-10-17 thomas case CTRL('n'):
6177 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
6178 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
6179 1e37a5c2 2019-05-12 jcs s->selected_line++;
6180 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
6181 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
6183 07b0611c 2022-06-23 thomas view->count = 0;
6185 1c5e5faa 2022-06-23 thomas case 'c':
6186 1e37a5c2 2019-05-12 jcs case 'p': {
6187 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6189 07b0611c 2022-06-23 thomas view->count = 0;
6190 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6191 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6192 1e37a5c2 2019-05-12 jcs if (id == NULL)
6194 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
6195 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
6196 15a94983 2018-12-23 stsp struct got_object_qid *pid;
6197 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
6198 1e37a5c2 2019-05-12 jcs int obj_type;
6199 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
6200 1e37a5c2 2019-05-12 jcs s->repo, id);
6203 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
6204 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
6205 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
6206 15a94983 2018-12-23 stsp got_object_commit_close(commit);
6209 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
6210 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
6211 ec242592 2022-04-22 thomas s->repo, &pid->id);
6212 945f9229 2022-04-16 thomas if (err)
6214 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
6215 945f9229 2022-04-16 thomas pcommit, s->path);
6216 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
6217 e5a0f69f 2018-08-18 stsp if (err) {
6218 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
6219 1e37a5c2 2019-05-12 jcs err = NULL;
6220 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6223 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
6225 1e37a5c2 2019-05-12 jcs free(blob_id);
6226 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
6227 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
6228 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6231 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6232 ec242592 2022-04-22 thomas &pid->id);
6233 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6235 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
6236 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
6238 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
6243 1e37a5c2 2019-05-12 jcs s->done = 1;
6244 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6245 1e37a5c2 2019-05-12 jcs s->done = 0;
6246 1e37a5c2 2019-05-12 jcs if (thread_err)
6248 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
6249 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
6250 a5388363 2020-12-01 naddy err = run_blame(view);
6255 1c5e5faa 2022-06-23 thomas case 'C': {
6256 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
6258 07b0611c 2022-06-23 thomas view->count = 0;
6259 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
6260 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
6262 1e37a5c2 2019-05-12 jcs s->done = 1;
6263 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
6264 1e37a5c2 2019-05-12 jcs s->done = 0;
6265 1e37a5c2 2019-05-12 jcs if (thread_err)
6267 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
6268 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
6269 1e37a5c2 2019-05-12 jcs s->blamed_commit =
6270 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
6271 a5388363 2020-12-01 naddy err = run_blame(view);
6276 2a31b33b 2022-07-23 thomas case 'L':
6277 eaeaa612 2022-07-20 thomas view->count = 0;
6278 2a31b33b 2022-07-23 thomas s->id_to_log = get_selected_commit_id(s->blame.lines,
6279 2a31b33b 2022-07-23 thomas s->blame.nlines, s->first_displayed_line, s->selected_line);
6280 2a31b33b 2022-07-23 thomas if (s->id_to_log)
6281 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
6283 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6284 1e37a5c2 2019-05-12 jcs case '\r': {
6285 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
6286 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
6287 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
6289 07b0611c 2022-06-23 thomas view->count = 0;
6290 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
6291 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
6292 1e37a5c2 2019-05-12 jcs if (id == NULL)
6294 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
6297 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
6298 4fc71f3b 2022-07-12 thomas if (*new_view) {
6299 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
6300 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
6301 4fc71f3b 2022-07-12 thomas if (err)
6303 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
6304 4fc71f3b 2022-07-12 thomas } else {
6305 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
6306 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
6308 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
6309 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
6310 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
6311 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
6312 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
6316 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
6317 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
6318 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
6320 1e37a5c2 2019-05-12 jcs view_close(diff_view);
6323 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
6324 4fc71f3b 2022-07-12 thomas s->selected_line;
6325 4fc71f3b 2022-07-12 thomas if (*new_view)
6326 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
6327 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6328 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6329 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6330 a5d43cac 2022-07-01 thomas if (err)
6334 e78dc838 2020-12-04 stsp view->focussed = 0;
6335 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
6336 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
6337 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
6338 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6339 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
6340 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6343 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
6344 40236d76 2022-06-23 thomas if (err)
6346 e78dc838 2020-12-04 stsp view->focus_child = 1;
6348 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
6353 70f17a53 2022-06-13 thomas case CTRL('d'):
6354 23427b14 2022-06-23 thomas case 'd':
6355 70f17a53 2022-06-13 thomas nscroll /= 2;
6356 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6357 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6358 ea025d1d 2020-02-22 naddy case CTRL('f'):
6359 1c5e5faa 2022-06-23 thomas case 'f':
6361 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6362 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
6363 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
6364 07b0611c 2022-06-23 thomas view->count = 0;
6367 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
6368 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
6369 70f17a53 2022-06-13 thomas s->selected_line +=
6370 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
6371 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
6373 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
6374 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
6376 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
6377 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
6379 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6380 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
6381 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
6382 1e37a5c2 2019-05-12 jcs view->nlines - 2);
6386 07b0611c 2022-06-23 thomas view->count = 0;
6389 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
6392 adf4c9e0 2022-07-03 thomas static const struct got_error *
6393 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6395 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6396 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6398 adf4c9e0 2022-07-03 thomas view->count = 0;
6399 adf4c9e0 2022-07-03 thomas s->done = 1;
6400 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6401 adf4c9e0 2022-07-03 thomas s->done = 0;
6402 adf4c9e0 2022-07-03 thomas if (err)
6403 adf4c9e0 2022-07-03 thomas return err;
6404 adf4c9e0 2022-07-03 thomas return run_blame(view);
6407 a70480e0 2018-06-23 stsp static const struct got_error *
6408 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6410 a70480e0 2018-06-23 stsp const struct got_error *error;
6411 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6412 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6413 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6414 0587e10c 2020-07-23 stsp char *link_target = NULL;
6415 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6416 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6417 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6419 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6420 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6422 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6423 a70480e0 2018-06-23 stsp switch (ch) {
6425 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6428 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6429 69069811 2018-08-02 stsp if (repo_path == NULL)
6430 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6434 17020d27 2019-03-07 stsp usage_blame();
6435 a70480e0 2018-06-23 stsp /* NOTREACHED */
6439 a70480e0 2018-06-23 stsp argc -= optind;
6440 a70480e0 2018-06-23 stsp argv += optind;
6442 f135c941 2020-02-20 stsp if (argc != 1)
6443 a70480e0 2018-06-23 stsp usage_blame();
6445 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6446 7cd52833 2022-06-23 thomas if (error != NULL)
6447 7cd52833 2022-06-23 thomas goto done;
6449 69069811 2018-08-02 stsp if (repo_path == NULL) {
6450 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6451 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6452 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6453 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6454 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6455 c156c7a4 2020-12-18 stsp goto done;
6456 f135c941 2020-02-20 stsp if (worktree)
6457 eb41ed75 2019-02-05 stsp repo_path =
6458 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6460 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6461 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6462 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6463 c156c7a4 2020-12-18 stsp goto done;
6467 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6468 c02c541e 2019-03-29 stsp if (error != NULL)
6469 8e94dd5b 2019-01-04 stsp goto done;
6471 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6472 f135c941 2020-02-20 stsp worktree);
6473 c02c541e 2019-03-29 stsp if (error)
6474 92205607 2019-01-04 stsp goto done;
6476 f135c941 2020-02-20 stsp init_curses();
6478 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6479 51a10b52 2020-12-26 stsp if (error)
6480 51a10b52 2020-12-26 stsp goto done;
6482 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6483 eb41ed75 2019-02-05 stsp if (error)
6484 69069811 2018-08-02 stsp goto done;
6486 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6487 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6488 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6489 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6490 a70480e0 2018-06-23 stsp if (error != NULL)
6491 66b4983c 2018-06-23 stsp goto done;
6492 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6493 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6495 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6496 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6498 a19e88aa 2018-06-23 stsp if (error != NULL)
6499 8b473291 2019-02-21 stsp goto done;
6501 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6502 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6503 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6504 e1cd8fed 2018-08-01 stsp goto done;
6507 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6508 945f9229 2022-04-16 thomas if (error)
6509 945f9229 2022-04-16 thomas goto done;
6511 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6512 945f9229 2022-04-16 thomas commit, repo);
6513 7cbe629d 2018-08-04 stsp if (error)
6514 7cbe629d 2018-08-04 stsp goto done;
6516 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6517 78756c87 2020-11-24 stsp commit_id, repo);
6518 0587e10c 2020-07-23 stsp if (error)
6519 0587e10c 2020-07-23 stsp goto done;
6520 12314ad4 2019-08-31 stsp if (worktree) {
6521 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6522 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6523 12314ad4 2019-08-31 stsp worktree = NULL;
6525 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6527 69069811 2018-08-02 stsp free(repo_path);
6528 f135c941 2020-02-20 stsp free(in_repo_path);
6529 0587e10c 2020-07-23 stsp free(link_target);
6530 69069811 2018-08-02 stsp free(cwd);
6531 a70480e0 2018-06-23 stsp free(commit_id);
6532 945f9229 2022-04-16 thomas if (commit)
6533 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6534 eb41ed75 2019-02-05 stsp if (worktree)
6535 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6536 1d0f4054 2021-06-17 stsp if (repo) {
6537 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6538 1d0f4054 2021-06-17 stsp if (error == NULL)
6539 1d0f4054 2021-06-17 stsp error = close_err;
6541 7cd52833 2022-06-23 thomas if (pack_fds) {
6542 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6543 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6544 7cd52833 2022-06-23 thomas if (error == NULL)
6545 7cd52833 2022-06-23 thomas error = pack_err;
6547 51a10b52 2020-12-26 stsp tog_free_refs();
6548 a70480e0 2018-06-23 stsp return error;
6551 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6552 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6554 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6555 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6556 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6557 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6558 86f4aab9 2022-09-09 thomas char *index = NULL;
6559 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6560 07dd3ed3 2022-08-06 thomas int width, n, nentries, i = 1;
6561 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6563 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6564 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6565 a5d43cac 2022-07-01 thomas --limit; /* border */
6567 f7d12f7e 2018-08-01 stsp werase(view->window);
6569 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6570 ffd1d5e5 2018-06-23 stsp return NULL;
6572 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6575 ffd1d5e5 2018-06-23 stsp return err;
6576 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6577 a3404814 2018-09-02 stsp wstandout(view->window);
6578 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6580 86f4aab9 2022-09-09 thomas wattr_on(view->window, COLOR_PAIR(tc->colorpair), NULL);
6581 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6582 86f4aab9 2022-09-09 thomas free(wline);
6583 86f4aab9 2022-09-09 thomas wline = NULL;
6584 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
6585 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
6587 86f4aab9 2022-09-09 thomas wattr_off(view->window, COLOR_PAIR(tc->colorpair), NULL);
6588 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6589 a3404814 2018-09-02 stsp wstandend(view->window);
6590 86f4aab9 2022-09-09 thomas if (--limit <= 0)
6591 86f4aab9 2022-09-09 thomas return NULL;
6593 6f5f393a 2022-08-12 thomas i += s->selected;
6594 6f5f393a 2022-08-12 thomas if (s->first_displayed_entry) {
6595 6f5f393a 2022-08-12 thomas i += got_tree_entry_get_index(s->first_displayed_entry);
6596 6f5f393a 2022-08-12 thomas if (s->tree != s->root)
6597 6f5f393a 2022-08-12 thomas ++i; /* account for ".." entry */
6599 07dd3ed3 2022-08-06 thomas nentries = got_object_tree_get_nentries(s->tree);
6600 86f4aab9 2022-09-09 thomas if (asprintf(&index, "[%d/%d] %s",
6601 86f4aab9 2022-09-09 thomas i, nentries + (s->tree == s->root ? 0 : 1), parent_path) == -1)
6602 86f4aab9 2022-09-09 thomas return got_error_from_errno("asprintf");
6603 86f4aab9 2022-09-09 thomas err = format_line(&wline, &width, NULL, index, 0, view->ncols, 0, 0);
6604 86f4aab9 2022-09-09 thomas free(index);
6606 ce52c690 2018-06-23 stsp return err;
6607 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6608 2550e4c3 2018-07-13 stsp free(wline);
6609 2550e4c3 2018-07-13 stsp wline = NULL;
6610 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6611 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6612 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6613 ffd1d5e5 2018-06-23 stsp return NULL;
6614 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6615 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6616 a1eca9bb 2018-06-23 stsp return NULL;
6618 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6619 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6620 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6621 0cf4efb1 2018-09-29 stsp if (view->focussed)
6622 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6623 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6625 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6626 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6627 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6628 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6629 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6630 ffd1d5e5 2018-06-23 stsp return NULL;
6634 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6637 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6638 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6639 848d6979 2019-08-12 stsp const char *modestr = "";
6640 56e0773d 2019-11-28 stsp mode_t mode;
6642 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6643 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6645 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6646 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6647 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6649 638f9024 2019-05-13 stsp return got_error_from_errno(
6650 230a42bd 2019-05-11 jcs "got_object_id_str");
6652 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6653 63c5ca5d 2019-08-24 stsp modestr = "$";
6654 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6657 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6658 d86d3b18 2020-12-01 naddy te, s->repo);
6659 0d6c6ee3 2020-05-20 stsp if (err) {
6660 0d6c6ee3 2020-05-20 stsp free(id_str);
6661 0d6c6ee3 2020-05-20 stsp return err;
6663 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6664 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6665 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6667 848d6979 2019-08-12 stsp modestr = "@";
6669 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6670 848d6979 2019-08-12 stsp modestr = "/";
6671 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6672 848d6979 2019-08-12 stsp modestr = "*";
6673 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6674 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6675 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6676 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6677 1d13200f 2018-07-12 stsp free(id_str);
6678 0d6c6ee3 2020-05-20 stsp free(link_target);
6679 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6681 1d13200f 2018-07-12 stsp free(id_str);
6682 0d6c6ee3 2020-05-20 stsp free(link_target);
6683 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6685 ffd1d5e5 2018-06-23 stsp if (err) {
6686 ffd1d5e5 2018-06-23 stsp free(line);
6689 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6690 0cf4efb1 2018-09-29 stsp if (view->focussed)
6691 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6692 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6694 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6696 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6697 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6698 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6700 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6701 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6702 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6703 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6704 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6705 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6706 ffd1d5e5 2018-06-23 stsp free(line);
6707 2550e4c3 2018-07-13 stsp free(wline);
6708 2550e4c3 2018-07-13 stsp wline = NULL;
6710 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6711 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6712 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6716 ffd1d5e5 2018-06-23 stsp return err;
6719 ffd1d5e5 2018-06-23 stsp static void
6720 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6722 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6723 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6724 fa86c4bf 2020-11-29 stsp int i = 0;
6726 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6729 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6730 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6731 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6732 fa86c4bf 2020-11-29 stsp if (!isroot)
6733 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6736 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6737 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6741 a5d43cac 2022-07-01 thomas static const struct got_error *
6742 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6744 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6745 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6746 ffd1d5e5 2018-06-23 stsp int n = 0;
6748 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6749 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6750 694d3271 2020-12-01 naddy s->first_displayed_entry);
6752 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6754 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6755 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6756 07dd3ed3 2022-08-06 thomas if (last) {
6757 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
6758 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6760 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6761 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6762 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6766 a5d43cac 2022-07-01 thomas return NULL;
6769 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6770 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6771 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6773 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6774 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6775 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6777 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6778 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6779 56e0773d 2019-11-28 stsp + 1 /* slash */;
6781 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6783 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6784 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6785 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6787 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6788 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6789 d9765a41 2018-06-23 stsp while (pt) {
6790 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6791 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6792 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6793 cb2ebc8a 2018-06-23 stsp goto done;
6795 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6796 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6797 cb2ebc8a 2018-06-23 stsp goto done;
6799 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6802 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6803 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6804 ce52c690 2018-06-23 stsp goto done;
6808 ce52c690 2018-06-23 stsp if (err) {
6809 ce52c690 2018-06-23 stsp free(*path);
6810 ce52c690 2018-06-23 stsp *path = NULL;
6812 ce52c690 2018-06-23 stsp return err;
6815 ce52c690 2018-06-23 stsp static const struct got_error *
6816 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6817 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6818 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6820 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6821 ce52c690 2018-06-23 stsp char *path;
6822 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6824 a0de39f3 2019-08-09 stsp *new_view = NULL;
6826 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6828 ce52c690 2018-06-23 stsp return err;
6830 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6831 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6832 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6833 83ce39e3 2019-08-12 stsp goto done;
6836 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6837 e5a0f69f 2018-08-18 stsp if (err) {
6838 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6839 fc06ba56 2019-08-22 stsp err = NULL;
6840 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6842 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6844 83ce39e3 2019-08-12 stsp free(path);
6845 69efd4c4 2018-07-18 stsp return err;
6848 69efd4c4 2018-07-18 stsp static const struct got_error *
6849 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6850 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6852 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6853 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6854 69efd4c4 2018-07-18 stsp char *path;
6856 a0de39f3 2019-08-09 stsp *new_view = NULL;
6858 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6859 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6860 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6862 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6864 69efd4c4 2018-07-18 stsp return err;
6866 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6869 e5a0f69f 2018-08-18 stsp view_close(log_view);
6871 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6872 cb2ebc8a 2018-06-23 stsp free(path);
6873 cb2ebc8a 2018-06-23 stsp return err;
6876 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6877 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6878 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6880 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6881 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6882 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6883 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6885 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6886 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6888 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6889 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6890 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6892 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6894 bc573f3b 2021-07-10 stsp goto done;
6897 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6898 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6899 bc573f3b 2021-07-10 stsp * closed on demand.
6901 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6902 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6904 bc573f3b 2021-07-10 stsp goto done;
6905 bc573f3b 2021-07-10 stsp s->tree = s->root;
6907 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6908 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6909 ffd1d5e5 2018-06-23 stsp goto done;
6911 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6912 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6913 ffd1d5e5 2018-06-23 stsp goto done;
6916 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6917 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6918 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6919 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6920 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6921 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6922 9cd7cbd1 2020-12-07 stsp goto done;
6925 fb2756b9 2018-08-04 stsp s->repo = repo;
6927 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6928 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6929 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6930 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6932 c0b01bdb 2019-11-08 stsp goto done;
6933 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6934 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6936 c0b01bdb 2019-11-08 stsp goto done;
6937 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6938 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6939 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6941 c0b01bdb 2019-11-08 stsp goto done;
6943 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6944 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6945 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6947 11b20872 2019-11-08 stsp goto done;
6949 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6950 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6952 c0b01bdb 2019-11-08 stsp goto done;
6955 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6956 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6957 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6958 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6959 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6961 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6962 bc573f3b 2021-07-10 stsp if (commit)
6963 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6965 bc573f3b 2021-07-10 stsp close_tree_view(view);
6966 ad80ab7b 2018-08-04 stsp return err;
6969 e5a0f69f 2018-08-18 stsp static const struct got_error *
6970 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6972 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6974 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6975 fb2756b9 2018-08-04 stsp free(s->tree_label);
6976 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6977 6484ec90 2018-09-29 stsp free(s->commit_id);
6978 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6979 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6980 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6981 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6982 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6983 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6984 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6985 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6986 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6987 ad80ab7b 2018-08-04 stsp free(parent);
6990 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6991 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6992 bc573f3b 2021-07-10 stsp if (s->root)
6993 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6994 7c32bd05 2019-06-22 stsp return NULL;
6997 7c32bd05 2019-06-22 stsp static const struct got_error *
6998 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
7000 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7002 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
7003 7c32bd05 2019-06-22 stsp return NULL;
7006 7c32bd05 2019-06-22 stsp static int
7007 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
7009 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
7011 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
7015 7c32bd05 2019-06-22 stsp static const struct got_error *
7016 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
7018 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
7019 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
7021 7c32bd05 2019-06-22 stsp if (!view->searching) {
7022 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7023 7c32bd05 2019-06-22 stsp return NULL;
7026 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7027 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7028 7c32bd05 2019-06-22 stsp if (s->selected_entry)
7029 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
7030 56e0773d 2019-11-28 stsp s->selected_entry);
7032 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7034 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
7035 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7037 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
7038 56e0773d 2019-11-28 stsp s->selected_entry);
7041 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7042 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
7043 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7044 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7046 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7049 7c32bd05 2019-06-22 stsp while (1) {
7050 56e0773d 2019-11-28 stsp if (te == NULL) {
7051 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
7052 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7053 ac66afb8 2019-06-24 stsp return NULL;
7055 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7056 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
7058 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
7061 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
7062 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7063 56e0773d 2019-11-28 stsp s->matched_entry = te;
7067 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
7068 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
7070 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
7073 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
7074 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
7075 7c32bd05 2019-06-22 stsp s->selected = 0;
7078 e5a0f69f 2018-08-18 stsp return NULL;
7081 ad80ab7b 2018-08-04 stsp static const struct got_error *
7082 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
7084 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
7085 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
7086 e5a0f69f 2018-08-18 stsp char *parent_path;
7088 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
7090 e5a0f69f 2018-08-18 stsp return err;
7092 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
7093 e5a0f69f 2018-08-18 stsp free(parent_path);
7095 a5d43cac 2022-07-01 thomas view_border(view);
7096 e5a0f69f 2018-08-18 stsp return err;
7099 07dd3ed3 2022-08-06 thomas static const struct got_error *
7100 07dd3ed3 2022-08-06 thomas tree_goto_line(struct tog_view *view, int nlines)
7102 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
7103 07dd3ed3 2022-08-06 thomas struct tog_tree_view_state *s = &view->state.tree;
7104 07dd3ed3 2022-08-06 thomas struct got_tree_entry **fte, **lte, **ste;
7105 07dd3ed3 2022-08-06 thomas int g, last, first = 1, i = 1;
7106 07dd3ed3 2022-08-06 thomas int root = s->tree == s->root;
7107 07dd3ed3 2022-08-06 thomas int off = root ? 1 : 2;
7109 07dd3ed3 2022-08-06 thomas g = view->gline;
7110 07dd3ed3 2022-08-06 thomas view->gline = 0;
7112 07dd3ed3 2022-08-06 thomas if (g == 0)
7114 07dd3ed3 2022-08-06 thomas else if (g > got_object_tree_get_nentries(s->tree))
7115 07dd3ed3 2022-08-06 thomas g = got_object_tree_get_nentries(s->tree) + (root ? 0 : 1);
7117 07dd3ed3 2022-08-06 thomas fte = &s->first_displayed_entry;
7118 07dd3ed3 2022-08-06 thomas lte = &s->last_displayed_entry;
7119 07dd3ed3 2022-08-06 thomas ste = &s->selected_entry;
7121 07dd3ed3 2022-08-06 thomas if (*fte != NULL) {
7122 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7123 07dd3ed3 2022-08-06 thomas first += off; /* account for ".." */
7125 07dd3ed3 2022-08-06 thomas last = got_tree_entry_get_index(*lte);
7126 07dd3ed3 2022-08-06 thomas last += off;
7128 07dd3ed3 2022-08-06 thomas if (g >= first && g <= last && g - first < nlines) {
7129 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7130 07dd3ed3 2022-08-06 thomas return NULL; /* gline is on the current page */
7133 07dd3ed3 2022-08-06 thomas if (*ste != NULL) {
7134 07dd3ed3 2022-08-06 thomas i = got_tree_entry_get_index(*ste);
7135 07dd3ed3 2022-08-06 thomas i += off;
7138 07dd3ed3 2022-08-06 thomas if (i < g) {
7139 07dd3ed3 2022-08-06 thomas err = tree_scroll_down(view, g - i);
7140 07dd3ed3 2022-08-06 thomas if (err)
7141 07dd3ed3 2022-08-06 thomas return err;
7142 07dd3ed3 2022-08-06 thomas if (got_tree_entry_get_index(*lte) >=
7143 07dd3ed3 2022-08-06 thomas got_object_tree_get_nentries(s->tree) - 1 &&
7144 07dd3ed3 2022-08-06 thomas first + s->selected < g &&
7145 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1) {
7146 07dd3ed3 2022-08-06 thomas first = got_tree_entry_get_index(*fte);
7147 07dd3ed3 2022-08-06 thomas first += off;
7148 07dd3ed3 2022-08-06 thomas s->selected = g - first;
7150 07dd3ed3 2022-08-06 thomas } else if (i > g)
7151 07dd3ed3 2022-08-06 thomas tree_scroll_up(s, i - g);
7153 07dd3ed3 2022-08-06 thomas if (g < nlines &&
7154 07dd3ed3 2022-08-06 thomas (*fte == NULL || (root && !got_tree_entry_get_index(*fte))))
7155 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
7157 07dd3ed3 2022-08-06 thomas return NULL;
7160 e5a0f69f 2018-08-18 stsp static const struct got_error *
7161 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
7163 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
7164 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
7165 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
7166 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 3;
7168 07dd3ed3 2022-08-06 thomas if (view->gline)
7169 07dd3ed3 2022-08-06 thomas return tree_goto_line(view, nscroll);
7171 e5a0f69f 2018-08-18 stsp switch (ch) {
7173 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
7174 07b0611c 2022-06-23 thomas view->count = 0;
7176 1be4947a 2022-07-22 thomas case 'L':
7177 07b0611c 2022-06-23 thomas view->count = 0;
7178 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
7180 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
7182 1be4947a 2022-07-22 thomas case 'R':
7183 07b0611c 2022-06-23 thomas view->count = 0;
7184 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_REF);
7186 e4526bf5 2021-09-03 naddy case 'g':
7187 e4526bf5 2021-09-03 naddy case KEY_HOME:
7188 e4526bf5 2021-09-03 naddy s->selected = 0;
7189 07b0611c 2022-06-23 thomas view->count = 0;
7190 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
7191 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
7192 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
7194 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7196 e4526bf5 2021-09-03 naddy case 'G':
7197 a5d43cac 2022-07-01 thomas case KEY_END: {
7198 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
7200 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7201 a5d43cac 2022-07-01 thomas --eos; /* border */
7202 e4526bf5 2021-09-03 naddy s->selected = 0;
7203 07b0611c 2022-06-23 thomas view->count = 0;
7204 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
7205 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7206 e4526bf5 2021-09-03 naddy if (te == NULL) {
7207 47f5fcf4 2022-07-16 thomas if (s->tree != s->root) {
7208 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
7213 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
7214 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
7216 e4526bf5 2021-09-03 naddy if (n > 0)
7217 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7221 1e37a5c2 2019-05-12 jcs case KEY_UP:
7222 f7140bf5 2021-10-17 thomas case CTRL('p'):
7223 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
7224 1e37a5c2 2019-05-12 jcs s->selected--;
7227 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
7228 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7229 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7230 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7231 07b0611c 2022-06-23 thomas view->count = 0;
7233 70f17a53 2022-06-13 thomas case CTRL('u'):
7234 23427b14 2022-06-23 thomas case 'u':
7235 70f17a53 2022-06-13 thomas nscroll /= 2;
7236 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7237 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
7238 ea025d1d 2020-02-22 naddy case CTRL('b'):
7239 1c5e5faa 2022-06-23 thomas case 'b':
7240 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
7241 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
7242 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
7243 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7245 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
7246 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
7248 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
7249 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
7250 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
7251 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
7252 07b0611c 2022-06-23 thomas view->count = 0;
7255 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
7256 f7140bf5 2021-10-17 thomas case CTRL('n'):
7257 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
7258 1e37a5c2 2019-05-12 jcs s->selected++;
7261 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7262 07b0611c 2022-06-23 thomas == NULL) {
7263 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
7264 07b0611c 2022-06-23 thomas view->count = 0;
7267 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
7269 70f17a53 2022-06-13 thomas case CTRL('d'):
7270 23427b14 2022-06-23 thomas case 'd':
7271 70f17a53 2022-06-13 thomas nscroll /= 2;
7272 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7273 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
7274 ea025d1d 2020-02-22 naddy case CTRL('f'):
7275 1c5e5faa 2022-06-23 thomas case 'f':
7276 4c2d69cb 2022-06-23 thomas case ' ':
7277 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
7279 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
7280 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
7281 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7282 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7284 07b0611c 2022-06-23 thomas view->count = 0;
7287 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
7289 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
7291 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
7292 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
7293 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
7294 1e37a5c2 2019-05-12 jcs /* user selected '..' */
7295 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
7296 07b0611c 2022-06-23 thomas view->count = 0;
7299 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
7300 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
7302 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
7303 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
7304 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
7305 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
7306 1e37a5c2 2019-05-12 jcs s->selected_entry =
7307 1e37a5c2 2019-05-12 jcs parent->selected_entry;
7308 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
7309 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
7310 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
7311 a5d43cac 2022-07-01 thomas if (err)
7314 1e37a5c2 2019-05-12 jcs free(parent);
7315 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
7316 56e0773d 2019-11-28 stsp s->selected_entry))) {
7317 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
7318 07b0611c 2022-06-23 thomas view->count = 0;
7319 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
7320 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
7323 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
7324 941e9f74 2019-05-21 stsp if (err) {
7325 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
7328 2a31b33b 2022-07-23 thomas } else if (S_ISREG(got_tree_entry_get_mode(s->selected_entry)))
7329 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_BLAME);
7331 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
7332 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
7333 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
7334 07b0611c 2022-06-23 thomas view->count = 0;
7337 07b0611c 2022-06-23 thomas view->count = 0;
7341 ffd1d5e5 2018-06-23 stsp return err;
7344 ffd1d5e5 2018-06-23 stsp __dead static void
7345 ffd1d5e5 2018-06-23 stsp usage_tree(void)
7348 91198554 2022-06-23 thomas fprintf(stderr,
7349 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
7350 ffd1d5e5 2018-06-23 stsp getprogname());
7354 ffd1d5e5 2018-06-23 stsp static const struct got_error *
7355 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
7357 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
7358 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
7359 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
7360 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7361 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
7362 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7363 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
7364 4e97c21c 2020-12-06 stsp char *label = NULL;
7365 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
7366 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
7368 5221c383 2018-08-01 stsp struct tog_view *view;
7369 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7371 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
7372 ffd1d5e5 2018-06-23 stsp switch (ch) {
7374 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
7377 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7378 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7379 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7383 e99e2d15 2020-11-24 naddy usage_tree();
7384 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7388 ffd1d5e5 2018-06-23 stsp argc -= optind;
7389 ffd1d5e5 2018-06-23 stsp argv += optind;
7391 55cccc34 2020-02-20 stsp if (argc > 1)
7392 e99e2d15 2020-11-24 naddy usage_tree();
7394 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7395 7cd52833 2022-06-23 thomas if (error != NULL)
7396 7cd52833 2022-06-23 thomas goto done;
7398 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7399 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7400 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7401 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7402 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7403 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7404 c156c7a4 2020-12-18 stsp goto done;
7405 55cccc34 2020-02-20 stsp if (worktree)
7406 52185f70 2019-02-05 stsp repo_path =
7407 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7409 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7410 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7411 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7412 c156c7a4 2020-12-18 stsp goto done;
7416 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7417 c02c541e 2019-03-29 stsp if (error != NULL)
7418 52185f70 2019-02-05 stsp goto done;
7420 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7421 55cccc34 2020-02-20 stsp repo, worktree);
7422 55cccc34 2020-02-20 stsp if (error)
7423 55cccc34 2020-02-20 stsp goto done;
7425 55cccc34 2020-02-20 stsp init_curses();
7427 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7428 c02c541e 2019-03-29 stsp if (error)
7429 52185f70 2019-02-05 stsp goto done;
7431 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7432 51a10b52 2020-12-26 stsp if (error)
7433 51a10b52 2020-12-26 stsp goto done;
7435 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7436 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7437 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7438 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7439 4e97c21c 2020-12-06 stsp if (error)
7440 4e97c21c 2020-12-06 stsp goto done;
7441 4e97c21c 2020-12-06 stsp head_ref_name = label;
7443 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7444 4e97c21c 2020-12-06 stsp if (error == NULL)
7445 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7446 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7447 4e97c21c 2020-12-06 stsp goto done;
7448 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7449 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7450 4e97c21c 2020-12-06 stsp if (error)
7451 4e97c21c 2020-12-06 stsp goto done;
7454 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7455 945f9229 2022-04-16 thomas if (error)
7456 945f9229 2022-04-16 thomas goto done;
7458 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7459 5221c383 2018-08-01 stsp if (view == NULL) {
7460 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7461 5221c383 2018-08-01 stsp goto done;
7463 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7464 ad80ab7b 2018-08-04 stsp if (error)
7465 ad80ab7b 2018-08-04 stsp goto done;
7466 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7467 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7468 d91faf3b 2020-12-01 naddy in_repo_path);
7469 55cccc34 2020-02-20 stsp if (error)
7470 55cccc34 2020-02-20 stsp goto done;
7473 55cccc34 2020-02-20 stsp if (worktree) {
7474 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7475 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7476 55cccc34 2020-02-20 stsp worktree = NULL;
7478 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7480 52185f70 2019-02-05 stsp free(repo_path);
7481 e4a0e26d 2020-02-20 stsp free(cwd);
7482 ffd1d5e5 2018-06-23 stsp free(commit_id);
7483 4e97c21c 2020-12-06 stsp free(label);
7485 486cd271 2020-12-06 stsp got_ref_close(ref);
7486 1d0f4054 2021-06-17 stsp if (repo) {
7487 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7488 1d0f4054 2021-06-17 stsp if (error == NULL)
7489 1d0f4054 2021-06-17 stsp error = close_err;
7491 7cd52833 2022-06-23 thomas if (pack_fds) {
7492 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7493 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7494 7cd52833 2022-06-23 thomas if (error == NULL)
7495 7cd52833 2022-06-23 thomas error = pack_err;
7497 51a10b52 2020-12-26 stsp tog_free_refs();
7498 ffd1d5e5 2018-06-23 stsp return error;
7501 6458efa5 2020-11-24 stsp static const struct got_error *
7502 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7504 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7505 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7507 6458efa5 2020-11-24 stsp s->nrefs = 0;
7508 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7509 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7510 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7511 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7512 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7515 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7516 6458efa5 2020-11-24 stsp if (re == NULL)
7517 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7519 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7520 8924d611 2020-12-26 stsp if (re->ref == NULL)
7521 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7522 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7523 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7526 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7527 6458efa5 2020-11-24 stsp return NULL;
7530 ef20f542 2022-06-26 thomas static void
7531 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7533 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7535 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7536 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7537 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7538 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7543 6458efa5 2020-11-24 stsp static const struct got_error *
7544 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7546 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7547 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7549 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7550 6458efa5 2020-11-24 stsp s->repo = repo;
7552 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7553 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7555 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7557 6458efa5 2020-11-24 stsp return err;
7559 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7560 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7561 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7562 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7564 6458efa5 2020-11-24 stsp goto done;
7566 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7567 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7568 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7570 6458efa5 2020-11-24 stsp goto done;
7572 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7573 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7574 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7575 2183bbf6 2022-01-23 thomas if (err)
7576 2183bbf6 2022-01-23 thomas goto done;
7578 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7579 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7580 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7582 6458efa5 2020-11-24 stsp goto done;
7585 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7586 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7587 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7588 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7589 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7592 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7593 6458efa5 2020-11-24 stsp return err;
7596 6458efa5 2020-11-24 stsp static const struct got_error *
7597 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7599 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7601 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7602 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7604 6458efa5 2020-11-24 stsp return NULL;
7607 6458efa5 2020-11-24 stsp static const struct got_error *
7608 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7609 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7611 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7612 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7613 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7614 6458efa5 2020-11-24 stsp int obj_type;
7616 c42c9805 2020-11-24 stsp *commit_id = NULL;
7618 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7620 6458efa5 2020-11-24 stsp return err;
7622 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7624 6458efa5 2020-11-24 stsp goto done;
7626 6458efa5 2020-11-24 stsp switch (obj_type) {
7627 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7628 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7630 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7631 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7633 6458efa5 2020-11-24 stsp goto done;
7634 c42c9805 2020-11-24 stsp free(obj_id);
7635 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7636 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7638 6458efa5 2020-11-24 stsp goto done;
7639 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7640 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7641 c42c9805 2020-11-24 stsp goto done;
7643 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7644 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7645 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7646 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7647 c42c9805 2020-11-24 stsp goto done;
7651 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7657 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7658 c42c9805 2020-11-24 stsp if (err) {
7659 c42c9805 2020-11-24 stsp free(*commit_id);
7660 c42c9805 2020-11-24 stsp *commit_id = NULL;
7662 c42c9805 2020-11-24 stsp return err;
7665 c42c9805 2020-11-24 stsp static const struct got_error *
7666 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7667 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7669 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7670 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7671 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7673 c42c9805 2020-11-24 stsp *new_view = NULL;
7675 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7676 c42c9805 2020-11-24 stsp if (err) {
7677 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7678 c42c9805 2020-11-24 stsp return err;
7680 c42c9805 2020-11-24 stsp return NULL;
7683 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7684 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7685 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7686 6458efa5 2020-11-24 stsp goto done;
7689 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7690 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7693 6458efa5 2020-11-24 stsp view_close(log_view);
7695 6458efa5 2020-11-24 stsp *new_view = log_view;
7696 c42c9805 2020-11-24 stsp free(commit_id);
7697 6458efa5 2020-11-24 stsp return err;
7700 ce5b7c56 2019-07-09 stsp static void
7701 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7703 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7704 34ba6917 2020-11-30 stsp int i = 0;
7706 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7709 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7710 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7711 34ba6917 2020-11-30 stsp if (re == NULL)
7713 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7714 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7718 a5d43cac 2022-07-01 thomas static const struct got_error *
7719 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7721 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7722 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7723 6458efa5 2020-11-24 stsp int n = 0;
7725 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7726 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7728 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7730 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7731 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7732 07dd3ed3 2022-08-06 thomas if (last) {
7733 07dd3ed3 2022-08-06 thomas s->last_displayed_entry = last;
7734 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7736 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7737 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7738 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7742 a5d43cac 2022-07-01 thomas return NULL;
7745 6458efa5 2020-11-24 stsp static const struct got_error *
7746 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7748 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7750 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7751 6458efa5 2020-11-24 stsp return NULL;
7754 6458efa5 2020-11-24 stsp static int
7755 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7757 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7759 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7763 6458efa5 2020-11-24 stsp static const struct got_error *
7764 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7766 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7767 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7769 6458efa5 2020-11-24 stsp if (!view->searching) {
7770 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7771 6458efa5 2020-11-24 stsp return NULL;
7774 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7775 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7776 6458efa5 2020-11-24 stsp if (s->selected_entry)
7777 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7779 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7780 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7782 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7783 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7785 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7786 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7789 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7790 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7791 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7792 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7794 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7797 6458efa5 2020-11-24 stsp while (1) {
7798 6458efa5 2020-11-24 stsp if (re == NULL) {
7799 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7800 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7801 6458efa5 2020-11-24 stsp return NULL;
7803 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7804 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7806 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7809 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7810 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7811 6458efa5 2020-11-24 stsp s->matched_entry = re;
7815 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7816 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7818 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7821 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7822 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7823 6458efa5 2020-11-24 stsp s->selected = 0;
7826 6458efa5 2020-11-24 stsp return NULL;
7829 6458efa5 2020-11-24 stsp static const struct got_error *
7830 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7832 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7833 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7834 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7835 6458efa5 2020-11-24 stsp char *line = NULL;
7836 6458efa5 2020-11-24 stsp wchar_t *wline;
7837 6458efa5 2020-11-24 stsp struct tog_color *tc;
7838 6458efa5 2020-11-24 stsp int width, n;
7839 6458efa5 2020-11-24 stsp int limit = view->nlines;
7841 6458efa5 2020-11-24 stsp werase(view->window);
7843 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7844 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7845 a5d43cac 2022-07-01 thomas --limit; /* border */
7847 6458efa5 2020-11-24 stsp if (limit == 0)
7848 6458efa5 2020-11-24 stsp return NULL;
7850 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7852 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7853 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7854 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7856 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7857 6458efa5 2020-11-24 stsp if (err) {
7858 6458efa5 2020-11-24 stsp free(line);
7859 6458efa5 2020-11-24 stsp return err;
7861 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7862 6458efa5 2020-11-24 stsp wstandout(view->window);
7863 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7864 86f4aab9 2022-09-09 thomas while (width++ < view->ncols)
7865 86f4aab9 2022-09-09 thomas waddch(view->window, ' ');
7866 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7867 6458efa5 2020-11-24 stsp wstandend(view->window);
7868 6458efa5 2020-11-24 stsp free(wline);
7869 6458efa5 2020-11-24 stsp wline = NULL;
7870 6458efa5 2020-11-24 stsp free(line);
7871 6458efa5 2020-11-24 stsp line = NULL;
7872 6458efa5 2020-11-24 stsp if (--limit <= 0)
7873 6458efa5 2020-11-24 stsp return NULL;
7876 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7877 6458efa5 2020-11-24 stsp char *line = NULL;
7878 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7880 84227eb1 2022-06-23 thomas if (s->show_date) {
7881 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7882 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7883 84227eb1 2022-06-23 thomas struct got_object_id *id;
7884 84227eb1 2022-06-23 thomas struct tm tm;
7885 84227eb1 2022-06-23 thomas time_t t;
7887 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7888 84227eb1 2022-06-23 thomas if (err)
7889 84227eb1 2022-06-23 thomas return err;
7890 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7891 84227eb1 2022-06-23 thomas if (err) {
7892 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7893 84227eb1 2022-06-23 thomas free(id);
7894 84227eb1 2022-06-23 thomas return err;
7896 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7898 84227eb1 2022-06-23 thomas if (err) {
7899 84227eb1 2022-06-23 thomas free(id);
7900 84227eb1 2022-06-23 thomas return err;
7902 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7903 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7904 84227eb1 2022-06-23 thomas } else {
7905 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7906 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7908 84227eb1 2022-06-23 thomas free(id);
7909 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7910 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7911 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7912 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7914 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7915 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7916 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7917 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7918 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7919 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7920 6458efa5 2020-11-24 stsp struct got_object_id *id;
7921 6458efa5 2020-11-24 stsp char *id_str;
7922 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7924 6458efa5 2020-11-24 stsp return err;
7925 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7926 6458efa5 2020-11-24 stsp if (err) {
7928 6458efa5 2020-11-24 stsp return err;
7930 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7931 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7932 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7934 6458efa5 2020-11-24 stsp free(id_str);
7935 6458efa5 2020-11-24 stsp return err;
7938 6458efa5 2020-11-24 stsp free(id_str);
7939 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7940 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7941 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7943 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7945 6458efa5 2020-11-24 stsp if (err) {
7946 6458efa5 2020-11-24 stsp free(line);
7947 6458efa5 2020-11-24 stsp return err;
7949 6458efa5 2020-11-24 stsp if (n == s->selected) {
7950 6458efa5 2020-11-24 stsp if (view->focussed)
7951 6458efa5 2020-11-24 stsp wstandout(view->window);
7952 6458efa5 2020-11-24 stsp s->selected_entry = re;
7954 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7956 6458efa5 2020-11-24 stsp wattr_on(view->window,
7957 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7958 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7960 6458efa5 2020-11-24 stsp wattr_off(view->window,
7961 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7962 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7963 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7964 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7965 6458efa5 2020-11-24 stsp wstandend(view->window);
7966 6458efa5 2020-11-24 stsp free(line);
7967 6458efa5 2020-11-24 stsp free(wline);
7968 6458efa5 2020-11-24 stsp wline = NULL;
7970 6458efa5 2020-11-24 stsp s->ndisplayed++;
7971 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7974 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7977 a5d43cac 2022-07-01 thomas view_border(view);
7978 6458efa5 2020-11-24 stsp return err;
7981 6458efa5 2020-11-24 stsp static const struct got_error *
7982 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7983 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7985 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7986 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7987 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7989 c42c9805 2020-11-24 stsp *new_view = NULL;
7991 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7992 c42c9805 2020-11-24 stsp if (err) {
7993 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7994 c42c9805 2020-11-24 stsp return err;
7996 c42c9805 2020-11-24 stsp return NULL;
8000 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
8001 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
8002 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
8003 c42c9805 2020-11-24 stsp goto done;
8006 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
8007 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
8009 c42c9805 2020-11-24 stsp goto done;
8011 c42c9805 2020-11-24 stsp *new_view = tree_view;
8013 c42c9805 2020-11-24 stsp free(commit_id);
8014 c42c9805 2020-11-24 stsp return err;
8017 07dd3ed3 2022-08-06 thomas static const struct got_error *
8018 07dd3ed3 2022-08-06 thomas ref_goto_line(struct tog_view *view, int nlines)
8020 07dd3ed3 2022-08-06 thomas const struct got_error *err = NULL;
8021 07dd3ed3 2022-08-06 thomas struct tog_ref_view_state *s = &view->state.ref;
8022 07dd3ed3 2022-08-06 thomas int g, idx = s->selected_entry->idx;
8024 07dd3ed3 2022-08-06 thomas g = view->gline;
8025 07dd3ed3 2022-08-06 thomas view->gline = 0;
8027 07dd3ed3 2022-08-06 thomas if (g == 0)
8029 07dd3ed3 2022-08-06 thomas else if (g > s->nrefs)
8030 07dd3ed3 2022-08-06 thomas g = s->nrefs;
8032 07dd3ed3 2022-08-06 thomas if (g >= s->first_displayed_entry->idx + 1 &&
8033 07dd3ed3 2022-08-06 thomas g <= s->last_displayed_entry->idx + 1 &&
8034 07dd3ed3 2022-08-06 thomas g - s->first_displayed_entry->idx - 1 < nlines) {
8035 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8036 07dd3ed3 2022-08-06 thomas return NULL;
8039 07dd3ed3 2022-08-06 thomas if (idx + 1 < g) {
8040 07dd3ed3 2022-08-06 thomas err = ref_scroll_down(view, g - idx - 1);
8041 07dd3ed3 2022-08-06 thomas if (err)
8042 07dd3ed3 2022-08-06 thomas return err;
8043 07dd3ed3 2022-08-06 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL &&
8044 07dd3ed3 2022-08-06 thomas s->first_displayed_entry->idx + s->selected < g &&
8045 07dd3ed3 2022-08-06 thomas s->selected < s->ndisplayed - 1)
8046 07dd3ed3 2022-08-06 thomas s->selected = g - s->first_displayed_entry->idx - 1;
8047 07dd3ed3 2022-08-06 thomas } else if (idx + 1 > g)
8048 07dd3ed3 2022-08-06 thomas ref_scroll_up(s, idx - g + 1);
8050 07dd3ed3 2022-08-06 thomas if (g < nlines && s->first_displayed_entry->idx == 0)
8051 07dd3ed3 2022-08-06 thomas s->selected = g - 1;
8053 07dd3ed3 2022-08-06 thomas return NULL;
8057 c42c9805 2020-11-24 stsp static const struct got_error *
8058 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
8060 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
8061 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
8062 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
8063 2a31b33b 2022-07-23 thomas int n, nscroll = view->nlines - 1;
8065 07dd3ed3 2022-08-06 thomas if (view->gline)
8066 07dd3ed3 2022-08-06 thomas return ref_goto_line(view, nscroll);
8068 6458efa5 2020-11-24 stsp switch (ch) {
8070 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
8071 07b0611c 2022-06-23 thomas view->count = 0;
8073 84227eb1 2022-06-23 thomas case 'm':
8074 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
8075 07b0611c 2022-06-23 thomas view->count = 0;
8077 98182bd0 2021-11-20 thomas case 'o':
8078 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
8079 07b0611c 2022-06-23 thomas view->count = 0;
8080 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
8081 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
8082 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
8083 c591440f 2021-11-20 thomas if (err)
8085 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
8086 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
8087 c591440f 2021-11-20 thomas &tog_refs, s->repo);
8088 3bfadbd4 2021-11-20 thomas if (err)
8090 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
8091 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
8093 6458efa5 2020-11-24 stsp case KEY_ENTER:
8094 6458efa5 2020-11-24 stsp case '\r':
8095 07b0611c 2022-06-23 thomas view->count = 0;
8096 6458efa5 2020-11-24 stsp if (!s->selected_entry)
8098 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_LOG);
8100 1be4947a 2022-07-22 thomas case 'T':
8101 07b0611c 2022-06-23 thomas view->count = 0;
8102 c42c9805 2020-11-24 stsp if (!s->selected_entry)
8104 2a31b33b 2022-07-23 thomas err = view_request_new(new_view, view, TOG_VIEW_TREE);
8106 e4526bf5 2021-09-03 naddy case 'g':
8107 e4526bf5 2021-09-03 naddy case KEY_HOME:
8108 e4526bf5 2021-09-03 naddy s->selected = 0;
8109 07b0611c 2022-06-23 thomas view->count = 0;
8110 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
8112 e4526bf5 2021-09-03 naddy case 'G':
8113 a5d43cac 2022-07-01 thomas case KEY_END: {
8114 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
8116 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
8117 a5d43cac 2022-07-01 thomas --eos; /* border */
8118 e4526bf5 2021-09-03 naddy s->selected = 0;
8119 07b0611c 2022-06-23 thomas view->count = 0;
8120 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
8121 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
8122 e4526bf5 2021-09-03 naddy if (re == NULL)
8124 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
8125 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
8127 e4526bf5 2021-09-03 naddy if (n > 0)
8128 e4526bf5 2021-09-03 naddy s->selected = n - 1;
8132 6458efa5 2020-11-24 stsp case KEY_UP:
8133 f7140bf5 2021-10-17 thomas case CTRL('p'):
8134 6458efa5 2020-11-24 stsp if (s->selected > 0) {
8135 6458efa5 2020-11-24 stsp s->selected--;
8138 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
8139 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8140 07b0611c 2022-06-23 thomas view->count = 0;
8142 70f17a53 2022-06-13 thomas case CTRL('u'):
8143 23427b14 2022-06-23 thomas case 'u':
8144 70f17a53 2022-06-13 thomas nscroll /= 2;
8145 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8146 6458efa5 2020-11-24 stsp case KEY_PPAGE:
8147 6458efa5 2020-11-24 stsp case CTRL('b'):
8148 1c5e5faa 2022-06-23 thomas case 'b':
8149 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
8150 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
8151 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
8152 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
8153 07b0611c 2022-06-23 thomas view->count = 0;
8156 6458efa5 2020-11-24 stsp case KEY_DOWN:
8157 f7140bf5 2021-10-17 thomas case CTRL('n'):
8158 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
8159 6458efa5 2020-11-24 stsp s->selected++;
8162 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8163 6458efa5 2020-11-24 stsp /* can't scroll any further */
8164 07b0611c 2022-06-23 thomas view->count = 0;
8167 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
8169 70f17a53 2022-06-13 thomas case CTRL('d'):
8170 23427b14 2022-06-23 thomas case 'd':
8171 70f17a53 2022-06-13 thomas nscroll /= 2;
8172 70f17a53 2022-06-13 thomas /* FALL THROUGH */
8173 6458efa5 2020-11-24 stsp case KEY_NPAGE:
8174 6458efa5 2020-11-24 stsp case CTRL('f'):
8175 1c5e5faa 2022-06-23 thomas case 'f':
8176 4c2d69cb 2022-06-23 thomas case ' ':
8177 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
8178 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
8179 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
8180 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
8181 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
8182 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
8183 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
8184 07b0611c 2022-06-23 thomas view->count = 0;
8187 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
8189 6458efa5 2020-11-24 stsp case CTRL('l'):
8190 07b0611c 2022-06-23 thomas view->count = 0;
8191 8924d611 2020-12-26 stsp tog_free_refs();
8192 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
8195 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
8196 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
8198 6458efa5 2020-11-24 stsp case KEY_RESIZE:
8199 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
8200 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
8203 07b0611c 2022-06-23 thomas view->count = 0;
8207 6458efa5 2020-11-24 stsp return err;
8210 6458efa5 2020-11-24 stsp __dead static void
8211 6458efa5 2020-11-24 stsp usage_ref(void)
8214 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
8215 6458efa5 2020-11-24 stsp getprogname());
8219 6458efa5 2020-11-24 stsp static const struct got_error *
8220 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
8222 6458efa5 2020-11-24 stsp const struct got_error *error;
8223 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
8224 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
8225 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
8227 6458efa5 2020-11-24 stsp struct tog_view *view;
8228 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8230 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
8231 6458efa5 2020-11-24 stsp switch (ch) {
8233 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
8234 6458efa5 2020-11-24 stsp if (repo_path == NULL)
8235 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
8239 e99e2d15 2020-11-24 naddy usage_ref();
8240 6458efa5 2020-11-24 stsp /* NOTREACHED */
8244 6458efa5 2020-11-24 stsp argc -= optind;
8245 6458efa5 2020-11-24 stsp argv += optind;
8247 6458efa5 2020-11-24 stsp if (argc > 1)
8248 e99e2d15 2020-11-24 naddy usage_ref();
8250 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8251 7cd52833 2022-06-23 thomas if (error != NULL)
8252 7cd52833 2022-06-23 thomas goto done;
8254 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
8255 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
8256 c156c7a4 2020-12-18 stsp if (cwd == NULL)
8257 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
8258 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
8259 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8260 c156c7a4 2020-12-18 stsp goto done;
8261 6458efa5 2020-11-24 stsp if (worktree)
8262 6458efa5 2020-11-24 stsp repo_path =
8263 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
8265 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
8266 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
8267 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
8268 c156c7a4 2020-12-18 stsp goto done;
8272 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8273 6458efa5 2020-11-24 stsp if (error != NULL)
8274 6458efa5 2020-11-24 stsp goto done;
8276 6458efa5 2020-11-24 stsp init_curses();
8278 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
8279 51a10b52 2020-12-26 stsp if (error)
8280 51a10b52 2020-12-26 stsp goto done;
8282 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8283 6458efa5 2020-11-24 stsp if (error)
8284 6458efa5 2020-11-24 stsp goto done;
8286 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
8287 6458efa5 2020-11-24 stsp if (view == NULL) {
8288 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
8289 6458efa5 2020-11-24 stsp goto done;
8292 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
8293 6458efa5 2020-11-24 stsp if (error)
8294 6458efa5 2020-11-24 stsp goto done;
8296 6458efa5 2020-11-24 stsp if (worktree) {
8297 6458efa5 2020-11-24 stsp /* Release work tree lock. */
8298 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
8299 6458efa5 2020-11-24 stsp worktree = NULL;
8301 6458efa5 2020-11-24 stsp error = view_loop(view);
8303 6458efa5 2020-11-24 stsp free(repo_path);
8304 6458efa5 2020-11-24 stsp free(cwd);
8305 1d0f4054 2021-06-17 stsp if (repo) {
8306 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8307 1d0f4054 2021-06-17 stsp if (close_err)
8308 1d0f4054 2021-06-17 stsp error = close_err;
8310 7cd52833 2022-06-23 thomas if (pack_fds) {
8311 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8312 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8313 7cd52833 2022-06-23 thomas if (error == NULL)
8314 7cd52833 2022-06-23 thomas error = pack_err;
8316 51a10b52 2020-12-26 stsp tog_free_refs();
8317 6458efa5 2020-11-24 stsp return error;
8320 2a31b33b 2022-07-23 thomas static const struct got_error *
8321 2a31b33b 2022-07-23 thomas view_dispatch_request(struct tog_view **new_view, struct tog_view *view,
8322 2a31b33b 2022-07-23 thomas enum tog_view_type request, int y, int x)
8324 2a31b33b 2022-07-23 thomas const struct got_error *err = NULL;
8326 2a31b33b 2022-07-23 thomas *new_view = NULL;
8328 2a31b33b 2022-07-23 thomas switch (request) {
8329 2a31b33b 2022-07-23 thomas case TOG_VIEW_DIFF:
8330 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG) {
8331 2a31b33b 2022-07-23 thomas struct tog_log_view_state *s = &view->state.log;
8333 2a31b33b 2022-07-23 thomas err = open_diff_view_for_commit(new_view, y, x,
8334 2a31b33b 2022-07-23 thomas s->selected_entry->commit, s->selected_entry->id,
8335 2a31b33b 2022-07-23 thomas view, s->repo);
8337 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8338 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8340 2a31b33b 2022-07-23 thomas case TOG_VIEW_BLAME:
8341 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_TREE) {
8342 2a31b33b 2022-07-23 thomas struct tog_tree_view_state *s = &view->state.tree;
8344 2a31b33b 2022-07-23 thomas err = blame_tree_entry(new_view, y, x,
8345 2a31b33b 2022-07-23 thomas s->selected_entry, &s->parents, s->commit_id,
8346 2a31b33b 2022-07-23 thomas s->repo);
8348 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8349 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8351 2a31b33b 2022-07-23 thomas case TOG_VIEW_LOG:
8352 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_BLAME)
8353 2a31b33b 2022-07-23 thomas err = log_annotated_line(new_view, y, x,
8354 2a31b33b 2022-07-23 thomas view->state.blame.repo, view->state.blame.id_to_log);
8355 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
8356 2a31b33b 2022-07-23 thomas err = log_selected_tree_entry(new_view, y, x,
8357 2a31b33b 2022-07-23 thomas &view->state.tree);
8358 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
8359 2a31b33b 2022-07-23 thomas err = log_ref_entry(new_view, y, x,
8360 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
8361 2a31b33b 2022-07-23 thomas view->state.ref.repo);
8363 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8364 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8366 2a31b33b 2022-07-23 thomas case TOG_VIEW_TREE:
8367 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
8368 2a31b33b 2022-07-23 thomas err = browse_commit_tree(new_view, y, x,
8369 2a31b33b 2022-07-23 thomas view->state.log.selected_entry,
8370 2a31b33b 2022-07-23 thomas view->state.log.in_repo_path,
8371 2a31b33b 2022-07-23 thomas view->state.log.head_ref_name,
8372 2a31b33b 2022-07-23 thomas view->state.log.repo);
8373 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_REF)
8374 2a31b33b 2022-07-23 thomas err = browse_ref_tree(new_view, y, x,
8375 2a31b33b 2022-07-23 thomas view->state.ref.selected_entry,
8376 2a31b33b 2022-07-23 thomas view->state.ref.repo);
8378 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL,
8379 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8381 2a31b33b 2022-07-23 thomas case TOG_VIEW_REF:
8382 2a31b33b 2022-07-23 thomas *new_view = view_open(0, 0, y, x, TOG_VIEW_REF);
8383 2a31b33b 2022-07-23 thomas if (*new_view == NULL)
8384 2a31b33b 2022-07-23 thomas return got_error_from_errno("view_open");
8385 2a31b33b 2022-07-23 thomas if (view->type == TOG_VIEW_LOG)
8386 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.log.repo);
8387 2a31b33b 2022-07-23 thomas else if (view->type == TOG_VIEW_TREE)
8388 2a31b33b 2022-07-23 thomas err = open_ref_view(*new_view, view->state.tree.repo);
8390 2a31b33b 2022-07-23 thomas err = got_error_msg(GOT_ERR_NOT_IMPL,
8391 2a31b33b 2022-07-23 thomas "parent/child view pair not supported");
8392 2a31b33b 2022-07-23 thomas if (err)
8393 2a31b33b 2022-07-23 thomas view_close(*new_view);
8395 2a31b33b 2022-07-23 thomas default:
8396 2a31b33b 2022-07-23 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "invalid view");
8399 2a31b33b 2022-07-23 thomas return err;
8403 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
8404 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
8406 6458efa5 2020-11-24 stsp static void
8407 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
8409 a5d43cac 2022-07-01 thomas switch (view->type) {
8410 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8411 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8412 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
8413 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
8417 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
8418 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
8420 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
8421 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
8424 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
8425 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
8426 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
8428 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
8429 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
8430 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
8432 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
8433 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
8434 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
8436 a5d43cac 2022-07-01 thomas default:
8440 a5d43cac 2022-07-01 thomas view->offset = 0;
8444 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
8445 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
8447 a5d43cac 2022-07-01 thomas static const struct got_error *
8448 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
8450 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
8451 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
8452 a5d43cac 2022-07-01 thomas int *selected = NULL;
8453 a5d43cac 2022-07-01 thomas int header, offset;
8455 a5d43cac 2022-07-01 thomas switch (view->type) {
8456 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8457 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8458 a5d43cac 2022-07-01 thomas header = 3;
8459 a5d43cac 2022-07-01 thomas scrolld = NULL;
8460 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
8461 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
8462 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
8463 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
8464 a5d43cac 2022-07-01 thomas view->offset = offset;
8468 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
8469 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
8470 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
8471 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
8472 a5d43cac 2022-07-01 thomas selected = &s->selected;
8475 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
8476 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8477 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
8478 a5d43cac 2022-07-01 thomas header = 3;
8479 a5d43cac 2022-07-01 thomas selected = &s->selected;
8482 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
8483 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
8484 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
8485 a5d43cac 2022-07-01 thomas header = 5;
8486 a5d43cac 2022-07-01 thomas selected = &s->selected;
8489 a5d43cac 2022-07-01 thomas default:
8490 a5d43cac 2022-07-01 thomas selected = NULL;
8491 a5d43cac 2022-07-01 thomas scrolld = NULL;
8492 a5d43cac 2022-07-01 thomas header = 0;
8496 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
8497 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
8498 a5d43cac 2022-07-01 thomas view->offset = offset;
8499 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
8500 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
8501 a5d43cac 2022-07-01 thomas *selected -= offset;
8505 a5d43cac 2022-07-01 thomas return err;
8508 a5d43cac 2022-07-01 thomas static void
8509 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8513 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8514 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8515 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
8516 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8518 6879ba42 2020-10-01 naddy fputc('\n', fp);
8521 4ed7e80c 2018-05-20 stsp __dead static void
8522 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8524 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8526 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8527 6879ba42 2020-10-01 naddy getprogname());
8528 6879ba42 2020-10-01 naddy if (hflag) {
8529 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8530 6879ba42 2020-10-01 naddy list_commands(fp);
8532 6879ba42 2020-10-01 naddy exit(status);
8535 c2301be8 2018-04-30 stsp static char **
8536 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8538 ee85c5e8 2020-02-29 stsp va_list ap;
8539 c2301be8 2018-04-30 stsp char **argv;
8542 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8544 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8545 c2301be8 2018-04-30 stsp if (argv == NULL)
8546 c2301be8 2018-04-30 stsp err(1, "calloc");
8547 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8548 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8549 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8550 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8553 ee85c5e8 2020-02-29 stsp va_end(ap);
8554 c2301be8 2018-04-30 stsp return argv;
8558 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8559 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8560 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8561 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8562 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8564 ee85c5e8 2020-02-29 stsp static const struct got_error *
8565 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8567 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8568 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8569 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8570 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8571 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8572 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8573 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8574 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8575 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8577 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8578 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8579 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8581 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8582 7cd52833 2022-06-23 thomas if (error != NULL)
8583 7cd52833 2022-06-23 thomas goto done;
8585 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8586 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8587 ee85c5e8 2020-02-29 stsp goto done;
8589 ee85c5e8 2020-02-29 stsp if (worktree)
8590 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8592 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8593 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8594 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8595 ee85c5e8 2020-02-29 stsp goto done;
8598 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8599 ee85c5e8 2020-02-29 stsp if (error != NULL)
8600 ee85c5e8 2020-02-29 stsp goto done;
8602 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8603 ee85c5e8 2020-02-29 stsp repo, worktree);
8604 ee85c5e8 2020-02-29 stsp if (error)
8605 ee85c5e8 2020-02-29 stsp goto done;
8607 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8608 84de9106 2020-12-26 stsp if (error)
8609 84de9106 2020-12-26 stsp goto done;
8610 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8611 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8612 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8613 ee85c5e8 2020-02-29 stsp if (error)
8614 ee85c5e8 2020-02-29 stsp goto done;
8616 ee85c5e8 2020-02-29 stsp if (worktree) {
8617 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8618 ee85c5e8 2020-02-29 stsp worktree = NULL;
8621 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8622 945f9229 2022-04-16 thomas if (error)
8623 945f9229 2022-04-16 thomas goto done;
8625 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8626 ee85c5e8 2020-02-29 stsp if (error) {
8627 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8628 ee85c5e8 2020-02-29 stsp goto done;
8629 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8630 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8631 6879ba42 2020-10-01 naddy usage(1, 1);
8632 ee85c5e8 2020-02-29 stsp /* not reached */
8635 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8636 ee85c5e8 2020-02-29 stsp if (error)
8637 ee85c5e8 2020-02-29 stsp goto done;
8639 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8641 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8642 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8644 1d0f4054 2021-06-17 stsp if (repo) {
8645 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8646 1d0f4054 2021-06-17 stsp if (error == NULL)
8647 1d0f4054 2021-06-17 stsp error = close_err;
8649 945f9229 2022-04-16 thomas if (commit)
8650 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8651 ee85c5e8 2020-02-29 stsp if (worktree)
8652 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8653 7cd52833 2022-06-23 thomas if (pack_fds) {
8654 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8655 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8656 7cd52833 2022-06-23 thomas if (error == NULL)
8657 7cd52833 2022-06-23 thomas error = pack_err;
8660 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8661 ee85c5e8 2020-02-29 stsp free(commit_id);
8662 ee85c5e8 2020-02-29 stsp free(cwd);
8663 ee85c5e8 2020-02-29 stsp free(repo_path);
8664 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8665 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8667 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8668 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8669 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8671 87670572 2020-12-26 naddy tog_free_refs();
8672 ee85c5e8 2020-02-29 stsp return error;
8676 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8678 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8679 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8680 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8681 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8682 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
8683 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8684 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8686 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
8688 cacf1690 2022-09-06 thomas if (!isatty(STDIN_FILENO))
8689 cacf1690 2022-09-06 thomas errx(1, "standard input is not a tty");
8691 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8693 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8694 9f7d7167 2018-04-29 stsp switch (ch) {
8696 9f7d7167 2018-04-29 stsp hflag = 1;
8699 53ccebc2 2019-07-30 stsp Vflag = 1;
8702 6879ba42 2020-10-01 naddy usage(hflag, 1);
8703 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8707 9f7d7167 2018-04-29 stsp argc -= optind;
8708 9f7d7167 2018-04-29 stsp argv += optind;
8709 9814e6a3 2020-09-27 naddy optind = 1;
8710 c2301be8 2018-04-30 stsp optreset = 1;
8712 53ccebc2 2019-07-30 stsp if (Vflag) {
8713 53ccebc2 2019-07-30 stsp got_version_print_str();
8714 6879ba42 2020-10-01 naddy return 0;
8717 4010e238 2020-12-04 stsp #ifndef PROFILE
8718 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8719 4010e238 2020-12-04 stsp NULL) == -1)
8720 4010e238 2020-12-04 stsp err(1, "pledge");
8723 c2301be8 2018-04-30 stsp if (argc == 0) {
8724 f29d3e89 2018-06-23 stsp if (hflag)
8725 6879ba42 2020-10-01 naddy usage(hflag, 0);
8726 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8727 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8729 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8733 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8734 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8735 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8736 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8737 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8743 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8744 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
8745 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
8746 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8747 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
8748 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8751 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8752 ee85c5e8 2020-02-29 stsp if (argc != 1)
8753 6879ba42 2020-10-01 naddy usage(0, 1);
8754 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8755 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8757 ee85c5e8 2020-02-29 stsp if (hflag)
8758 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8760 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8764 b46c1e04 2020-09-20 naddy putchar('\n');
8765 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8767 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8768 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8769 a2f4a359 2020-02-28 stsp free(cmd_argv);
8772 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8773 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);