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 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
18 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
20 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
21 31120ada 2018-04-30 stsp #include <errno.h>
22 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
23 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
25 9f7d7167 2018-04-29 stsp #include <curses.h>
26 9f7d7167 2018-04-29 stsp #include <panel.h>
27 9f7d7167 2018-04-29 stsp #include <locale.h>
28 61266923 2020-01-14 stsp #include <signal.h>
29 9f7d7167 2018-04-29 stsp #include <stdlib.h>
30 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
31 26ed57b2 2018-05-19 stsp #include <stdio.h>
32 9f7d7167 2018-04-29 stsp #include <getopt.h>
33 9f7d7167 2018-04-29 stsp #include <string.h>
34 9f7d7167 2018-04-29 stsp #include <err.h>
35 80ddbec8 2018-04-29 stsp #include <unistd.h>
36 26ed57b2 2018-05-19 stsp #include <limits.h>
37 61e69b96 2018-05-20 stsp #include <wchar.h>
38 788c352e 2018-06-16 stsp #include <time.h>
39 84451b3e 2018-07-10 stsp #include <pthread.h>
40 5036bf37 2018-09-24 stsp #include <libgen.h>
41 60493ae3 2019-06-20 stsp #include <regex.h>
42 3da8ef85 2021-09-21 stsp #include <sched.h>
44 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
46 53ccebc2 2019-07-30 stsp #include "got_version.h"
47 9f7d7167 2018-04-29 stsp #include "got_error.h"
48 80ddbec8 2018-04-29 stsp #include "got_object.h"
49 80ddbec8 2018-04-29 stsp #include "got_reference.h"
50 80ddbec8 2018-04-29 stsp #include "got_repository.h"
51 80ddbec8 2018-04-29 stsp #include "got_diff.h"
52 511a516b 2018-05-19 stsp #include "got_opentemp.h"
53 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
54 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
55 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
56 a70480e0 2018-06-23 stsp #include "got_blame.h"
57 c2db6724 2019-01-04 stsp #include "got_privsep.h"
58 1dd54920 2019-05-11 stsp #include "got_path.h"
59 b7165be3 2019-02-05 stsp #include "got_worktree.h"
62 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
66 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
69 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
70 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
73 9f7d7167 2018-04-29 stsp #ifndef nitems
74 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
77 9f7d7167 2018-04-29 stsp struct tog_cmd {
78 c2301be8 2018-04-30 stsp const char *name;
79 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
80 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
83 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
84 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
87 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
88 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
90 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
91 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
92 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
93 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
94 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
96 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
97 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
98 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
99 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
100 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
101 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
104 d6b05b5b 2018-08-04 stsp enum tog_view_type {
105 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
106 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
107 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
108 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
109 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
112 a5d43cac 2022-07-01 thomas enum tog_view_mode {
113 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
114 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
115 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
118 a5d43cac 2022-07-01 thomas #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
120 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
122 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
123 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
124 ba4f502b 2018-08-04 stsp struct got_object_id *id;
125 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
128 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
129 ba4f502b 2018-08-04 stsp struct commit_queue {
130 ba4f502b 2018-08-04 stsp int ncommits;
131 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
134 f26dddb7 2019-11-08 stsp struct tog_color {
135 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
136 6d17833f 2019-11-08 stsp regex_t regex;
137 6d17833f 2019-11-08 stsp short colorpair;
139 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
141 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
142 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
143 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
145 2183bbf6 2022-01-23 thomas static const struct got_error *
146 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
147 2183bbf6 2022-01-23 thomas struct got_reference* re2)
149 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
150 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
151 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
153 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
154 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
155 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
156 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
157 2183bbf6 2022-01-23 thomas *cmp = -1;
158 2183bbf6 2022-01-23 thomas return NULL;
159 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
160 2183bbf6 2022-01-23 thomas *cmp = 1;
161 2183bbf6 2022-01-23 thomas return NULL;
164 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
165 2183bbf6 2022-01-23 thomas return NULL;
168 11b20872 2019-11-08 stsp static const struct got_error *
169 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
171 51a10b52 2020-12-26 stsp const struct got_error *err;
173 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
174 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
177 51a10b52 2020-12-26 stsp return err;
179 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
183 51a10b52 2020-12-26 stsp static void
184 51a10b52 2020-12-26 stsp tog_free_refs(void)
186 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
187 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
188 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
190 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
193 51a10b52 2020-12-26 stsp static const struct got_error *
194 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
195 11b20872 2019-11-08 stsp int idx, short color)
197 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
198 11b20872 2019-11-08 stsp struct tog_color *tc;
199 11b20872 2019-11-08 stsp int regerr = 0;
201 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
202 11b20872 2019-11-08 stsp return NULL;
204 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
206 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
207 11b20872 2019-11-08 stsp if (tc == NULL)
208 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
209 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
210 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
211 11b20872 2019-11-08 stsp if (regerr) {
212 11b20872 2019-11-08 stsp static char regerr_msg[512];
213 11b20872 2019-11-08 stsp static char err_msg[512];
214 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
215 11b20872 2019-11-08 stsp sizeof(regerr_msg));
216 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
217 11b20872 2019-11-08 stsp regerr_msg);
218 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
220 11b20872 2019-11-08 stsp return err;
222 11b20872 2019-11-08 stsp tc->colorpair = idx;
223 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
224 11b20872 2019-11-08 stsp return NULL;
227 11b20872 2019-11-08 stsp static void
228 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
230 11b20872 2019-11-08 stsp struct tog_color *tc;
232 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
233 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
234 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
235 11b20872 2019-11-08 stsp regfree(&tc->regex);
240 ef20f542 2022-06-26 thomas static struct tog_color *
241 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
243 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
245 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
246 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
250 11b20872 2019-11-08 stsp return NULL;
254 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
256 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
257 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
258 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
259 11b20872 2019-11-08 stsp return COLOR_CYAN;
260 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
261 11b20872 2019-11-08 stsp return COLOR_YELLOW;
262 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
263 11b20872 2019-11-08 stsp return COLOR_GREEN;
264 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
265 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
266 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
267 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
268 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
269 91b8c405 2020-01-25 stsp return COLOR_CYAN;
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
271 11b20872 2019-11-08 stsp return COLOR_GREEN;
272 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
273 11b20872 2019-11-08 stsp return COLOR_GREEN;
274 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
275 11b20872 2019-11-08 stsp return COLOR_CYAN;
276 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
277 11b20872 2019-11-08 stsp return COLOR_YELLOW;
278 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
279 6458efa5 2020-11-24 stsp return COLOR_GREEN;
280 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
281 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
282 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
283 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
284 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
285 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
291 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
293 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
295 11b20872 2019-11-08 stsp if (val == NULL)
296 11b20872 2019-11-08 stsp return default_color_value(envvar);
298 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
299 11b20872 2019-11-08 stsp return COLOR_BLACK;
300 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
301 11b20872 2019-11-08 stsp return COLOR_RED;
302 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
303 11b20872 2019-11-08 stsp return COLOR_GREEN;
304 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
305 11b20872 2019-11-08 stsp return COLOR_YELLOW;
306 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
307 11b20872 2019-11-08 stsp return COLOR_BLUE;
308 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
309 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
310 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
311 11b20872 2019-11-08 stsp return COLOR_CYAN;
312 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
313 11b20872 2019-11-08 stsp return COLOR_WHITE;
314 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
317 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 15a087fe 2019-02-21 stsp int first_displayed_line;
327 15a087fe 2019-02-21 stsp int last_displayed_line;
329 15a087fe 2019-02-21 stsp int diff_context;
330 3dbaef42 2020-11-24 stsp int ignore_whitespace;
331 64453f7e 2020-11-21 stsp int force_text_diff;
332 15a087fe 2019-02-21 stsp struct got_repository *repo;
333 bddb1296 2019-11-08 stsp struct tog_colors colors;
334 fe621944 2020-11-10 stsp size_t nlines;
335 f44b1f58 2020-02-02 tracey off_t *line_offsets;
336 f44b1f58 2020-02-02 tracey int matched_line;
337 f44b1f58 2020-02-02 tracey int selected_line;
339 4fc71f3b 2022-07-12 thomas /* passed from log or blame view; may be NULL */
340 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view;
343 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
344 f2d749db 2022-07-12 thomas static volatile sig_atomic_t tog_thread_error;
346 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
347 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
348 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
349 1a76625f 2018-10-22 stsp int commits_needed;
350 fb280deb 2021-08-30 stsp int load_all;
351 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
352 1a76625f 2018-10-22 stsp struct commit_queue *commits;
353 1a76625f 2018-10-22 stsp const char *in_repo_path;
354 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
355 1a76625f 2018-10-22 stsp struct got_repository *repo;
356 1af5eddf 2022-06-23 thomas int *pack_fds;
357 1a76625f 2018-10-22 stsp int log_complete;
358 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
359 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
360 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
361 13add988 2019-10-15 stsp int *searching;
362 13add988 2019-10-15 stsp int *search_next_done;
363 13add988 2019-10-15 stsp regex_t *regex;
366 1a76625f 2018-10-22 stsp struct tog_log_view_state {
367 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
368 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
369 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
370 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
371 b01e7d3b 2018-08-04 stsp int selected;
372 b01e7d3b 2018-08-04 stsp char *in_repo_path;
373 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
374 b672a97a 2020-01-27 stsp int log_branches;
375 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
376 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
377 1a76625f 2018-10-22 stsp sig_atomic_t quit;
378 1a76625f 2018-10-22 stsp pthread_t thread;
379 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
380 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
381 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
382 11b20872 2019-11-08 stsp struct tog_colors colors;
385 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
386 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
387 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
388 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
389 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
390 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
391 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
392 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
393 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
394 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
395 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
396 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
397 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
398 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
399 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
401 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
402 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
403 e9424729 2018-08-04 stsp int nlines;
405 e9424729 2018-08-04 stsp struct tog_view *view;
406 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
410 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
411 e9424729 2018-08-04 stsp const char *path;
412 e9424729 2018-08-04 stsp struct got_repository *repo;
413 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
414 e9424729 2018-08-04 stsp int *complete;
415 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
416 fc06ba56 2019-08-22 stsp void *cancel_arg;
419 e9424729 2018-08-04 stsp struct tog_blame {
421 be659d10 2020-11-18 stsp off_t filesize;
422 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
423 6fcac457 2018-11-19 stsp int nlines;
424 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
425 e9424729 2018-08-04 stsp pthread_t thread;
426 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
427 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
428 e9424729 2018-08-04 stsp const char *path;
429 7cd52833 2022-06-23 thomas int *pack_fds;
432 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
433 7cbe629d 2018-08-04 stsp int first_displayed_line;
434 7cbe629d 2018-08-04 stsp int last_displayed_line;
435 7cbe629d 2018-08-04 stsp int selected_line;
436 4fc71f3b 2022-07-12 thomas int last_diffed_line;
437 7cbe629d 2018-08-04 stsp int blame_complete;
440 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
441 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
442 e5a0f69f 2018-08-18 stsp char *path;
443 7cbe629d 2018-08-04 stsp struct got_repository *repo;
444 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
445 e9424729 2018-08-04 stsp struct tog_blame blame;
446 6c4c42e0 2019-06-24 stsp int matched_line;
447 11b20872 2019-11-08 stsp struct tog_colors colors;
450 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
451 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
452 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
453 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
454 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
455 ad80ab7b 2018-08-04 stsp int selected;
458 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
460 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
461 ad80ab7b 2018-08-04 stsp char *tree_label;
462 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
463 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
464 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
465 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
466 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
467 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
468 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
469 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
470 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
471 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
472 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
473 6458efa5 2020-11-24 stsp struct tog_colors colors;
476 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
477 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
478 6458efa5 2020-11-24 stsp struct got_reference *ref;
482 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
484 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
485 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
486 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
487 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
488 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
489 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
490 6458efa5 2020-11-24 stsp struct got_repository *repo;
491 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
492 bddb1296 2019-11-08 stsp struct tog_colors colors;
496 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
498 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
499 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
500 669b5ffa 2018-10-07 stsp * there is enough screen estate.
502 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
503 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
505 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
506 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
507 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
509 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
510 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
512 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
514 cc3c9aac 2018-08-01 stsp struct tog_view {
515 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
516 26ed57b2 2018-05-19 stsp WINDOW *window;
517 26ed57b2 2018-05-19 stsp PANEL *panel;
518 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
519 53d2bdd3 2022-07-10 thomas int resized_y, resized_x; /* begin_y/x based on user resizing */
520 05171be4 2022-06-23 thomas int maxx, x; /* max column and current start column */
521 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
522 a5d43cac 2022-07-01 thomas int nscrolled, offset; /* lines scrolled and hsplit line offset */
523 07b0611c 2022-06-23 thomas int ch, count; /* current keymap and count prefix */
524 53d2bdd3 2022-07-10 thomas int resize; /* set when in a resize event */
525 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
527 669b5ffa 2018-10-07 stsp struct tog_view *parent;
528 669b5ffa 2018-10-07 stsp struct tog_view *child;
531 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
532 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
533 e78dc838 2020-12-04 stsp * between parent and child.
534 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
535 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
536 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
537 e78dc838 2020-12-04 stsp * situations.
539 e78dc838 2020-12-04 stsp int focus_child;
541 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
542 5dc9f4bc 2018-08-04 stsp /* type-specific state */
543 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
545 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
546 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
547 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
548 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
549 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
552 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
553 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
554 e78dc838 2020-12-04 stsp struct tog_view *, int);
555 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
556 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
558 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
559 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
560 c0c4acc8 2021-01-24 stsp int search_started;
561 60493ae3 2019-06-20 stsp int searching;
562 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
563 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
564 60493ae3 2019-06-20 stsp int search_next_done;
565 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
566 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
567 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
568 1803e47f 2019-06-22 stsp regex_t regex;
569 41605754 2020-11-12 stsp regmatch_t regmatch;
572 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
573 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
574 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
575 78756c87 2020-11-24 stsp struct got_repository *);
576 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
577 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
578 e78dc838 2020-12-04 stsp struct tog_view *, int);
579 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
580 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
581 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
582 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
584 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
585 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
586 78756c87 2020-11-24 stsp const char *, const char *, int);
587 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
588 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
589 e78dc838 2020-12-04 stsp struct tog_view *, int);
590 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
591 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
592 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
594 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
595 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
596 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
597 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
598 e78dc838 2020-12-04 stsp struct tog_view *, int);
599 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
600 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
601 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
602 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
604 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
605 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
606 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
607 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
608 e78dc838 2020-12-04 stsp struct tog_view *, int);
609 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
610 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
611 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
613 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
614 6458efa5 2020-11-24 stsp struct got_repository *);
615 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
616 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
617 e78dc838 2020-12-04 stsp struct tog_view *, int);
618 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
619 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
620 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
622 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
623 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
624 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
625 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
626 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
628 25791caa 2018-10-24 stsp static void
629 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
631 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
634 83baff54 2019-08-12 stsp static void
635 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
637 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
640 61266923 2020-01-14 stsp static void
641 61266923 2020-01-14 stsp tog_sigcont(int signo)
643 61266923 2020-01-14 stsp tog_sigcont_received = 1;
646 296152d1 2022-05-31 thomas static void
647 296152d1 2022-05-31 thomas tog_sigint(int signo)
649 296152d1 2022-05-31 thomas tog_sigint_received = 1;
652 296152d1 2022-05-31 thomas static void
653 296152d1 2022-05-31 thomas tog_sigterm(int signo)
655 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
658 296152d1 2022-05-31 thomas static int
659 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
661 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
662 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
665 e5a0f69f 2018-08-18 stsp static const struct got_error *
666 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
668 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *child_err = NULL;
670 669b5ffa 2018-10-07 stsp if (view->child) {
671 f2d749db 2022-07-12 thomas child_err = view_close(view->child);
672 669b5ffa 2018-10-07 stsp view->child = NULL;
674 e5a0f69f 2018-08-18 stsp if (view->close)
675 e5a0f69f 2018-08-18 stsp err = view->close(view);
676 ea5e7bb5 2018-08-01 stsp if (view->panel)
677 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
678 ea5e7bb5 2018-08-01 stsp if (view->window)
679 ea5e7bb5 2018-08-01 stsp delwin(view->window);
680 ea5e7bb5 2018-08-01 stsp free(view);
681 f2d749db 2022-07-12 thomas return err ? err : child_err;
684 ea5e7bb5 2018-08-01 stsp static struct tog_view *
685 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
686 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
688 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
690 ea5e7bb5 2018-08-01 stsp if (view == NULL)
691 ea5e7bb5 2018-08-01 stsp return NULL;
693 d6b05b5b 2018-08-04 stsp view->type = type;
694 f7d12f7e 2018-08-01 stsp view->lines = LINES;
695 f7d12f7e 2018-08-01 stsp view->cols = COLS;
696 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
697 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
698 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
699 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
700 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
701 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
702 96a765a8 2018-08-04 stsp view_close(view);
703 ea5e7bb5 2018-08-01 stsp return NULL;
705 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
706 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
707 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
708 96a765a8 2018-08-04 stsp view_close(view);
709 ea5e7bb5 2018-08-01 stsp return NULL;
712 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
713 ea5e7bb5 2018-08-01 stsp return view;
717 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
719 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
721 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
724 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
725 a5d43cac 2022-07-01 thomas static int
726 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
728 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
731 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
733 5c60c32a 2018-10-18 stsp static const struct got_error *
734 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
736 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
738 53d2bdd3 2022-07-10 thomas if (!view->resize && view->mode == TOG_VIEW_SPLIT_HRZN) {
739 53d2bdd3 2022-07-10 thomas if (view->resized_y && view->resized_y < view->lines)
740 53d2bdd3 2022-07-10 thomas view->begin_y = view->resized_y;
742 53d2bdd3 2022-07-10 thomas view->begin_y = view_split_begin_y(view->nlines);
743 a5d43cac 2022-07-01 thomas view->begin_x = 0;
744 53d2bdd3 2022-07-10 thomas } else if (!view->resize) {
745 53d2bdd3 2022-07-10 thomas if (view->resized_x && view->resized_x < view->cols - 1 &&
746 53d2bdd3 2022-07-10 thomas view->cols > 119)
747 53d2bdd3 2022-07-10 thomas view->begin_x = view->resized_x;
749 53d2bdd3 2022-07-10 thomas view->begin_x = view_split_begin_x(0);
750 a5d43cac 2022-07-01 thomas view->begin_y = 0;
752 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
753 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
754 5c60c32a 2018-10-18 stsp view->lines = LINES;
755 5c60c32a 2018-10-18 stsp view->cols = COLS;
756 5c60c32a 2018-10-18 stsp err = view_resize(view);
758 5c60c32a 2018-10-18 stsp return err;
760 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
761 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
763 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
764 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
766 5c60c32a 2018-10-18 stsp return NULL;
769 5c60c32a 2018-10-18 stsp static const struct got_error *
770 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
772 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
774 5c60c32a 2018-10-18 stsp view->begin_x = 0;
775 53d2bdd3 2022-07-10 thomas view->begin_y = view->resize ? view->begin_y : 0;
776 53d2bdd3 2022-07-10 thomas view->nlines = view->resize ? view->nlines : LINES;
777 5c60c32a 2018-10-18 stsp view->ncols = COLS;
778 5c60c32a 2018-10-18 stsp view->lines = LINES;
779 5c60c32a 2018-10-18 stsp view->cols = COLS;
780 5c60c32a 2018-10-18 stsp err = view_resize(view);
782 5c60c32a 2018-10-18 stsp return err;
784 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
785 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
787 5c60c32a 2018-10-18 stsp return NULL;
791 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
793 5c60c32a 2018-10-18 stsp return view->parent == NULL;
796 b65b3ea0 2022-06-23 thomas static int
797 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
799 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
802 e44940c3 2022-07-01 thomas static int
803 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
805 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
808 444d5325 2022-07-03 thomas static int
809 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
811 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
812 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
815 a5d43cac 2022-07-01 thomas static void
816 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
818 a5d43cac 2022-07-01 thomas PANEL *panel;
819 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
821 a5d43cac 2022-07-01 thomas if (view->parent)
822 a5d43cac 2022-07-01 thomas return view_border(view->parent);
824 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
825 a5d43cac 2022-07-01 thomas if (panel == NULL)
828 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
829 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
830 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
831 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
832 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
834 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
835 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
838 53d2bdd3 2022-07-10 thomas static const struct got_error *view_init_hsplit(struct tog_view *, int);
839 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
840 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
841 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
842 53d2bdd3 2022-07-10 thomas static void view_get_split(struct tog_view *, int *, int *);
844 4d8c2215 2018-08-19 stsp static const struct got_error *
845 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
847 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
848 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
850 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
852 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
853 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
855 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
856 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
857 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
859 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
861 4918811f 2022-07-01 thomas if (view->child) {
862 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
864 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
865 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
866 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
867 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
868 40236d76 2022-06-23 thomas ncols = COLS;
870 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
871 5c60c32a 2018-10-18 stsp if (view->child->focussed)
872 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
874 5c60c32a 2018-10-18 stsp show_panel(view->panel);
876 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
878 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
879 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
882 a5d43cac 2022-07-01 thomas * Request commits if terminal height was increased in a log
883 a5d43cac 2022-07-01 thomas * view so we have enough commits loaded to populate the view.
885 a5d43cac 2022-07-01 thomas if (view->type == TOG_VIEW_LOG && dif > 0) {
886 a5d43cac 2022-07-01 thomas struct tog_log_view_state *ts = &view->state.log;
888 a5d43cac 2022-07-01 thomas if (ts->commits.ncommits < ts->selected_entry->idx +
889 a5d43cac 2022-07-01 thomas view->lines - ts->selected) {
890 a5d43cac 2022-07-01 thomas view->nscrolled = ts->selected_entry->idx +
891 a5d43cac 2022-07-01 thomas view->lines - ts->selected -
892 a5d43cac 2022-07-01 thomas ts->commits.ncommits + dif;
893 a5d43cac 2022-07-01 thomas err = request_log_commits(view);
895 a5d43cac 2022-07-01 thomas return err;
900 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
901 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
902 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
904 a5d43cac 2022-07-01 thomas if (hs) {
905 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
907 a5d43cac 2022-07-01 thomas return err;
908 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
909 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
911 a5d43cac 2022-07-01 thomas return err;
913 a5d43cac 2022-07-01 thomas view_border(view);
914 a5d43cac 2022-07-01 thomas update_panels();
915 a5d43cac 2022-07-01 thomas doupdate();
916 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
917 a5d43cac 2022-07-01 thomas nlines = view->nlines;
919 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
920 40236d76 2022-06-23 thomas ncols = COLS;
922 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
923 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
924 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
925 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
926 40236d76 2022-06-23 thomas wclear(view->window);
928 40236d76 2022-06-23 thomas view->nlines = nlines;
929 40236d76 2022-06-23 thomas view->ncols = ncols;
930 40236d76 2022-06-23 thomas view->lines = LINES;
931 40236d76 2022-06-23 thomas view->cols = COLS;
933 5c60c32a 2018-10-18 stsp return NULL;
936 97cb21cd 2022-07-12 thomas static void
937 97cb21cd 2022-07-12 thomas view_adjust_offset(struct tog_view *view, int n)
939 97cb21cd 2022-07-12 thomas if (n == 0)
942 97cb21cd 2022-07-12 thomas if (view->parent && view->parent->offset) {
943 97cb21cd 2022-07-12 thomas if (view->parent->offset + n >= 0)
944 97cb21cd 2022-07-12 thomas view->parent->offset += n;
946 97cb21cd 2022-07-12 thomas view->parent->offset = 0;
947 97cb21cd 2022-07-12 thomas } else if (view->offset) {
948 97cb21cd 2022-07-12 thomas if (view->offset - n >= 0)
949 97cb21cd 2022-07-12 thomas view->offset -= n;
951 97cb21cd 2022-07-12 thomas view->offset = 0;
955 53d2bdd3 2022-07-10 thomas static const struct got_error *
956 53d2bdd3 2022-07-10 thomas view_resize_split(struct tog_view *view, int resize)
958 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
959 53d2bdd3 2022-07-10 thomas struct tog_view *v = NULL;
961 53d2bdd3 2022-07-10 thomas if (view->parent)
962 53d2bdd3 2022-07-10 thomas v = view->parent;
964 53d2bdd3 2022-07-10 thomas v = view;
966 53d2bdd3 2022-07-10 thomas if (!v->child || !view_is_splitscreen(v->child))
967 53d2bdd3 2022-07-10 thomas return NULL;
969 53d2bdd3 2022-07-10 thomas v->resize = v->child->resize = resize; /* lock for resize event */
971 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
972 ae98518f 2022-07-12 thomas int y = v->child->begin_y;
974 53d2bdd3 2022-07-10 thomas if (v->child->resized_y)
975 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
976 53d2bdd3 2022-07-10 thomas if (view->parent)
977 53d2bdd3 2022-07-10 thomas v->child->begin_y -= resize;
979 53d2bdd3 2022-07-10 thomas v->child->begin_y += resize;
980 53d2bdd3 2022-07-10 thomas if (v->child->begin_y < 3) {
981 53d2bdd3 2022-07-10 thomas view->count = 0;
982 53d2bdd3 2022-07-10 thomas v->child->begin_y = 3;
983 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_y > LINES - 1) {
984 53d2bdd3 2022-07-10 thomas view->count = 0;
985 53d2bdd3 2022-07-10 thomas v->child->begin_y = LINES - 1;
987 53d2bdd3 2022-07-10 thomas v->ncols = COLS;
988 53d2bdd3 2022-07-10 thomas v->child->ncols = COLS;
989 97cb21cd 2022-07-12 thomas view_adjust_offset(view, resize);
990 53d2bdd3 2022-07-10 thomas err = view_init_hsplit(v, v->child->begin_y);
992 53d2bdd3 2022-07-10 thomas return err;
993 53d2bdd3 2022-07-10 thomas v->child->resized_y = v->child->begin_y;
994 ae98518f 2022-07-12 thomas if (y > v->child->begin_y) /* split increased */
995 ae98518f 2022-07-12 thomas v->child->nscrolled = y - v->child->begin_y;
997 53d2bdd3 2022-07-10 thomas if (v->child->resized_x)
998 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
999 53d2bdd3 2022-07-10 thomas if (view->parent)
1000 53d2bdd3 2022-07-10 thomas v->child->begin_x -= resize;
1002 53d2bdd3 2022-07-10 thomas v->child->begin_x += resize;
1003 53d2bdd3 2022-07-10 thomas if (v->child->begin_x < 11) {
1004 53d2bdd3 2022-07-10 thomas view->count = 0;
1005 53d2bdd3 2022-07-10 thomas v->child->begin_x = 11;
1006 53d2bdd3 2022-07-10 thomas } else if (v->child->begin_x > COLS - 1) {
1007 53d2bdd3 2022-07-10 thomas view->count = 0;
1008 53d2bdd3 2022-07-10 thomas v->child->begin_x = COLS - 1;
1010 53d2bdd3 2022-07-10 thomas v->child->resized_x = v->child->begin_x;
1013 53d2bdd3 2022-07-10 thomas v->child->mode = v->mode;
1014 53d2bdd3 2022-07-10 thomas v->child->nlines = v->lines - v->child->begin_y;
1015 53d2bdd3 2022-07-10 thomas v->child->ncols = v->cols - v->child->begin_x;
1016 53d2bdd3 2022-07-10 thomas v->focus_child = 1;
1018 53d2bdd3 2022-07-10 thomas err = view_fullscreen(v);
1019 53d2bdd3 2022-07-10 thomas if (err)
1020 53d2bdd3 2022-07-10 thomas return err;
1021 53d2bdd3 2022-07-10 thomas err = view_splitscreen(v->child);
1022 53d2bdd3 2022-07-10 thomas if (err)
1023 53d2bdd3 2022-07-10 thomas return err;
1025 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1026 53d2bdd3 2022-07-10 thomas err = offset_selection_down(v->child);
1027 53d2bdd3 2022-07-10 thomas if (err)
1028 53d2bdd3 2022-07-10 thomas return err;
1031 ae98518f 2022-07-12 thomas if (v->type == TOG_VIEW_LOG && v->nscrolled)
1032 53d2bdd3 2022-07-10 thomas err = request_log_commits(v);
1033 ae98518f 2022-07-12 thomas else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1034 53d2bdd3 2022-07-10 thomas err = request_log_commits(v->child);
1036 53d2bdd3 2022-07-10 thomas v->resize = v->child->resize = 0;
1038 53d2bdd3 2022-07-10 thomas return err;
1041 53d2bdd3 2022-07-10 thomas static void
1042 53d2bdd3 2022-07-10 thomas view_transfer_size(struct tog_view *dst, struct tog_view *src)
1044 53d2bdd3 2022-07-10 thomas struct tog_view *v = src->child ? src->child : src;
1046 53d2bdd3 2022-07-10 thomas dst->resized_x = v->resized_x;
1047 53d2bdd3 2022-07-10 thomas dst->resized_y = v->resized_y;
1050 669b5ffa 2018-10-07 stsp static const struct got_error *
1051 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
1053 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1055 669b5ffa 2018-10-07 stsp if (view->child == NULL)
1056 669b5ffa 2018-10-07 stsp return NULL;
1058 669b5ffa 2018-10-07 stsp err = view_close(view->child);
1059 669b5ffa 2018-10-07 stsp view->child = NULL;
1060 669b5ffa 2018-10-07 stsp return err;
1063 40236d76 2022-06-23 thomas static const struct got_error *
1064 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
1066 53d2bdd3 2022-07-10 thomas const struct got_error *err = NULL;
1068 669b5ffa 2018-10-07 stsp view->child = child;
1069 669b5ffa 2018-10-07 stsp child->parent = view;
1071 53d2bdd3 2022-07-10 thomas err = view_resize(view);
1072 53d2bdd3 2022-07-10 thomas if (err)
1073 53d2bdd3 2022-07-10 thomas return err;
1075 53d2bdd3 2022-07-10 thomas if (view->child->resized_x || view->child->resized_y)
1076 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1078 53d2bdd3 2022-07-10 thomas return err;
1081 34bc9ec9 2019-02-22 stsp static void
1082 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
1084 25791caa 2018-10-24 stsp int cols, lines;
1085 25791caa 2018-10-24 stsp struct winsize size;
1087 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
1088 25791caa 2018-10-24 stsp cols = 80; /* Default */
1089 25791caa 2018-10-24 stsp lines = 24;
1091 25791caa 2018-10-24 stsp cols = size.ws_col;
1092 25791caa 2018-10-24 stsp lines = size.ws_row;
1094 25791caa 2018-10-24 stsp resize_term(lines, cols);
1097 2b49a8ae 2019-06-22 stsp static const struct got_error *
1098 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
1100 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
1101 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1102 2b49a8ae 2019-06-22 stsp char pattern[1024];
1105 c0c4acc8 2021-01-24 stsp if (view->search_started) {
1106 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
1107 c0c4acc8 2021-01-24 stsp view->searching = 0;
1108 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
1110 c0c4acc8 2021-01-24 stsp view->search_started = 0;
1112 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
1113 2b49a8ae 2019-06-22 stsp return NULL;
1115 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1116 a5d43cac 2022-07-01 thomas v = view->child;
1118 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
1119 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1121 634cb454 2022-07-03 thomas nodelay(view->window, FALSE); /* block for search term input */
1122 2b49a8ae 2019-06-22 stsp nocbreak();
1124 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
1125 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1128 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1129 2b49a8ae 2019-06-22 stsp if (ret == ERR)
1130 2b49a8ae 2019-06-22 stsp return NULL;
1132 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1133 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1134 7c32bd05 2019-06-22 stsp if (err) {
1135 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1136 7c32bd05 2019-06-22 stsp return err;
1138 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1139 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1140 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1141 2b49a8ae 2019-06-22 stsp view->search_next(view);
1144 2b49a8ae 2019-06-22 stsp return NULL;
1147 ddbc4d37 2022-07-12 thomas /* Switch split mode. If view is a parent or child, draw the new splitscreen. */
1148 64486692 2022-07-07 thomas static const struct got_error *
1149 64486692 2022-07-07 thomas switch_split(struct tog_view *view)
1151 64486692 2022-07-07 thomas const struct got_error *err = NULL;
1152 64486692 2022-07-07 thomas struct tog_view *v = NULL;
1154 64486692 2022-07-07 thomas if (view->parent)
1155 64486692 2022-07-07 thomas v = view->parent;
1157 64486692 2022-07-07 thomas v = view;
1159 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN)
1160 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1162 64486692 2022-07-07 thomas v->mode = TOG_VIEW_SPLIT_HRZN;
1164 ddbc4d37 2022-07-12 thomas if (!v->child)
1165 ddbc4d37 2022-07-12 thomas return NULL;
1166 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->cols < 120)
1167 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_NONE;
1169 64486692 2022-07-07 thomas view_get_split(v, &v->child->begin_y, &v->child->begin_x);
1170 53d2bdd3 2022-07-10 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN && v->child->resized_y)
1171 53d2bdd3 2022-07-10 thomas v->child->begin_y = v->child->resized_y;
1172 ddbc4d37 2022-07-12 thomas else if (v->mode == TOG_VIEW_SPLIT_VERT && v->child->resized_x)
1173 53d2bdd3 2022-07-10 thomas v->child->begin_x = v->child->resized_x;
1176 64486692 2022-07-07 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1177 64486692 2022-07-07 thomas v->ncols = COLS;
1178 64486692 2022-07-07 thomas v->child->ncols = COLS;
1179 ddbc4d37 2022-07-12 thomas v->child->nscrolled = LINES - v->child->nlines;
1181 64486692 2022-07-07 thomas err = view_init_hsplit(v, v->child->begin_y);
1182 64486692 2022-07-07 thomas if (err)
1183 64486692 2022-07-07 thomas return err;
1185 64486692 2022-07-07 thomas v->child->mode = v->mode;
1186 64486692 2022-07-07 thomas v->child->nlines = v->lines - v->child->begin_y;
1187 64486692 2022-07-07 thomas v->focus_child = 1;
1189 64486692 2022-07-07 thomas err = view_fullscreen(v);
1190 64486692 2022-07-07 thomas if (err)
1191 64486692 2022-07-07 thomas return err;
1192 64486692 2022-07-07 thomas err = view_splitscreen(v->child);
1193 64486692 2022-07-07 thomas if (err)
1194 64486692 2022-07-07 thomas return err;
1196 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_NONE)
1197 ddbc4d37 2022-07-12 thomas v->mode = TOG_VIEW_SPLIT_VERT;
1198 ddbc4d37 2022-07-12 thomas if (v->mode == TOG_VIEW_SPLIT_HRZN) {
1199 ddbc4d37 2022-07-12 thomas err = offset_selection_down(v);
1200 64486692 2022-07-07 thomas err = offset_selection_down(v->child);
1201 ddbc4d37 2022-07-12 thomas } else {
1202 ddbc4d37 2022-07-12 thomas offset_selection_up(v);
1203 ddbc4d37 2022-07-12 thomas offset_selection_up(v->child);
1205 ae98518f 2022-07-12 thomas if (v->type == TOG_VIEW_LOG && v->nscrolled)
1206 ddbc4d37 2022-07-12 thomas err = request_log_commits(v);
1207 ae98518f 2022-07-12 thomas else if (v->child->type == TOG_VIEW_LOG && v->child->nscrolled)
1208 ddbc4d37 2022-07-12 thomas err = request_log_commits(v->child);
1210 64486692 2022-07-07 thomas return err;
1214 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1215 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1217 07b0611c 2022-06-23 thomas static int
1218 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1220 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1221 a5d43cac 2022-07-01 thomas int x, n = 0;
1223 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1224 a5d43cac 2022-07-01 thomas v = view->child;
1225 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1226 a5d43cac 2022-07-01 thomas v = view->parent;
1228 07b0611c 2022-06-23 thomas view->count = 0;
1229 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1230 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1231 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1232 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1235 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1236 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1237 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1238 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1242 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1243 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1245 07b0611c 2022-06-23 thomas if (n >= 9999999)
1246 07b0611c 2022-06-23 thomas n = 9999999;
1248 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1249 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1251 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1252 07b0611c 2022-06-23 thomas view->count = n;
1254 07b0611c 2022-06-23 thomas return c;
1257 0cf4efb1 2018-09-29 stsp static const struct got_error *
1258 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1259 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1261 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1262 669b5ffa 2018-10-07 stsp struct tog_view *v;
1263 1a76625f 2018-10-22 stsp int ch, errcode;
1265 e5a0f69f 2018-08-18 stsp *new = NULL;
1267 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1268 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1269 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1270 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1271 07b0611c 2022-06-23 thomas view->count = 0;
1274 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1275 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1276 82954512 2020-02-03 stsp if (errcode)
1277 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1278 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1279 3da8ef85 2021-09-21 stsp sched_yield();
1280 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1281 82954512 2020-02-03 stsp if (errcode)
1282 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1283 82954512 2020-02-03 stsp "pthread_mutex_lock");
1284 60493ae3 2019-06-20 stsp view->search_next(view);
1285 60493ae3 2019-06-20 stsp return NULL;
1288 634cb454 2022-07-03 thomas nodelay(view->window, FALSE);
1289 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1290 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1291 1a76625f 2018-10-22 stsp if (errcode)
1292 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1293 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1294 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1295 634cb454 2022-07-03 thomas cbreak();
1296 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1297 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1298 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1299 634cb454 2022-07-03 thomas view->count = 0;
1301 634cb454 2022-07-03 thomas ch = view->ch;
1302 634cb454 2022-07-03 thomas } else {
1303 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1304 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1305 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1307 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1308 1a76625f 2018-10-22 stsp if (errcode)
1309 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1310 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1312 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1313 25791caa 2018-10-24 stsp tog_resizeterm();
1314 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1315 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1316 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1317 25791caa 2018-10-24 stsp err = view_resize(v);
1319 25791caa 2018-10-24 stsp return err;
1320 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1322 25791caa 2018-10-24 stsp return err;
1323 cdfcfb03 2020-12-06 stsp if (v->child) {
1324 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1326 cdfcfb03 2020-12-06 stsp return err;
1327 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1328 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1330 cdfcfb03 2020-12-06 stsp return err;
1331 53d2bdd3 2022-07-10 thomas if (v->child->resized_x || v->child->resized_y) {
1332 53d2bdd3 2022-07-10 thomas err = view_resize_split(v, 0);
1333 53d2bdd3 2022-07-10 thomas if (err)
1334 53d2bdd3 2022-07-10 thomas return err;
1340 e5a0f69f 2018-08-18 stsp switch (ch) {
1342 07b0611c 2022-06-23 thomas view->count = 0;
1343 1e37a5c2 2019-05-12 jcs if (view->child) {
1344 e78dc838 2020-12-04 stsp view->focussed = 0;
1345 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1346 e78dc838 2020-12-04 stsp view->focus_child = 1;
1347 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1348 e78dc838 2020-12-04 stsp view->focussed = 0;
1349 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1350 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1351 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1352 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1353 a5d43cac 2022-07-01 thomas view->parent->type == TOG_VIEW_LOG) {
1354 a5d43cac 2022-07-01 thomas err = request_log_commits(view->parent);
1355 a5d43cac 2022-07-01 thomas if (err)
1356 a5d43cac 2022-07-01 thomas return err;
1358 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1359 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1360 a5d43cac 2022-07-01 thomas if (err)
1361 a5d43cac 2022-07-01 thomas return err;
1366 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1367 a5d43cac 2022-07-01 thomas if (view->parent->type == TOG_VIEW_LOG) {
1368 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1369 a5d43cac 2022-07-01 thomas err = request_log_commits(view->parent);
1370 a5d43cac 2022-07-01 thomas if (err)
1373 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1375 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1376 9970f7fc 2020-12-03 stsp view->dying = 1;
1381 1c5e5faa 2022-06-23 thomas case 'F':
1382 07b0611c 2022-06-23 thomas view->count = 0;
1383 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1384 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1386 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1387 e78dc838 2020-12-04 stsp view->focussed = 0;
1388 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1389 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1390 53d2bdd3 2022-07-10 thomas } else {
1391 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1392 53d2bdd3 2022-07-10 thomas if (!err)
1393 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1397 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1398 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1400 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1401 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1402 e78dc838 2020-12-04 stsp view->focussed = 1;
1403 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1405 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1406 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1407 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1408 53d2bdd3 2022-07-10 thomas if (!err)
1409 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 0);
1413 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1415 a5d43cac 2022-07-01 thomas if (err)
1417 a5d43cac 2022-07-01 thomas if (view->type == TOG_VIEW_LOG) {
1418 a5d43cac 2022-07-01 thomas err = request_log_commits(view);
1419 a5d43cac 2022-07-01 thomas if (err)
1422 a5d43cac 2022-07-01 thomas if (view->parent)
1423 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1424 a5d43cac 2022-07-01 thomas if (!err)
1425 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1427 64486692 2022-07-07 thomas case 'S':
1428 53d2bdd3 2022-07-10 thomas view->count = 0;
1429 64486692 2022-07-07 thomas err = switch_split(view);
1431 53d2bdd3 2022-07-10 thomas case '-':
1432 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, -1);
1434 53d2bdd3 2022-07-10 thomas case '+':
1435 53d2bdd3 2022-07-10 thomas err = view_resize_split(view, 1);
1437 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1440 07b0611c 2022-06-23 thomas view->count = 0;
1441 60493ae3 2019-06-20 stsp if (view->search_start)
1442 2b49a8ae 2019-06-22 stsp view_search_start(view);
1444 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1448 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1449 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1450 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1451 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1452 60493ae3 2019-06-20 stsp view->search_next(view);
1454 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1456 adf4c9e0 2022-07-03 thomas case 'A':
1457 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1458 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1460 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1461 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1462 adf4c9e0 2022-07-03 thomas if (v->reset) {
1463 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1464 adf4c9e0 2022-07-03 thomas if (err)
1465 adf4c9e0 2022-07-03 thomas return err;
1467 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1468 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1469 adf4c9e0 2022-07-03 thomas if (err)
1470 adf4c9e0 2022-07-03 thomas return err;
1475 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1479 e5a0f69f 2018-08-18 stsp return err;
1482 ef20f542 2022-06-26 thomas static int
1483 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1485 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1486 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1488 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1490 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1493 669b5ffa 2018-10-07 stsp return view->focussed;
1496 bcbd79e2 2018-08-19 stsp static const struct got_error *
1497 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1499 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1500 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1501 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1502 64486692 2022-07-07 thomas char *mode;
1503 fd823528 2018-10-22 stsp int fast_refresh = 10;
1504 1a76625f 2018-10-22 stsp int done = 0, errcode;
1506 64486692 2022-07-07 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
1507 64486692 2022-07-07 thomas if (!mode || !(*mode == 'h' || *mode == 'H'))
1508 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_VERT;
1510 64486692 2022-07-07 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
1512 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1513 1a76625f 2018-10-22 stsp if (errcode)
1514 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1516 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1517 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1519 1004088d 2018-09-29 stsp view->focussed = 1;
1520 878940b7 2018-09-29 stsp err = view->show(view);
1522 0cf4efb1 2018-09-29 stsp return err;
1523 0cf4efb1 2018-09-29 stsp update_panels();
1524 0cf4efb1 2018-09-29 stsp doupdate();
1525 f2d749db 2022-07-12 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_thread_error &&
1526 f2d749db 2022-07-12 thomas !tog_fatal_signal_received()) {
1527 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1528 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1529 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1531 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1534 9970f7fc 2020-12-03 stsp if (view->dying) {
1535 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1537 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1538 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1540 e78dc838 2020-12-04 stsp else if (view->parent)
1541 669b5ffa 2018-10-07 stsp prev = view->parent;
1543 e78dc838 2020-12-04 stsp if (view->parent) {
1544 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1545 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1546 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1547 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1548 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1549 40236d76 2022-06-23 thomas if (err)
1551 53d2bdd3 2022-07-10 thomas /* Make resized splits persist. */
1552 53d2bdd3 2022-07-10 thomas view_transfer_size(view->parent, view);
1554 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1556 9970f7fc 2020-12-03 stsp err = view_close(view);
1558 e5a0f69f 2018-08-18 stsp goto done;
1560 e78dc838 2020-12-04 stsp view = NULL;
1561 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1562 e78dc838 2020-12-04 stsp if (v->focussed)
1565 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1566 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1568 e78dc838 2020-12-04 stsp view = prev;
1569 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1570 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1571 e78dc838 2020-12-04 stsp tog_view_list_head);
1573 e78dc838 2020-12-04 stsp if (view) {
1574 e78dc838 2020-12-04 stsp if (view->focus_child) {
1575 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1576 e78dc838 2020-12-04 stsp view = view->child;
1578 e78dc838 2020-12-04 stsp view->focussed = 1;
1582 bcbd79e2 2018-08-19 stsp if (new_view) {
1583 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1584 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1585 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1586 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1588 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1589 86c66b02 2018-10-18 stsp err = view_close(v);
1591 86c66b02 2018-10-18 stsp goto done;
1594 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1595 fed7eaa8 2018-10-24 stsp view = new_view;
1597 669b5ffa 2018-10-07 stsp if (view) {
1598 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1599 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1600 e78dc838 2020-12-04 stsp view = view->child;
1602 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1603 e78dc838 2020-12-04 stsp view = view->parent;
1605 e78dc838 2020-12-04 stsp show_panel(view->panel);
1606 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1607 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1608 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1609 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1611 1a76625f 2018-10-22 stsp goto done;
1613 669b5ffa 2018-10-07 stsp err = view->show(view);
1615 1a76625f 2018-10-22 stsp goto done;
1616 669b5ffa 2018-10-07 stsp if (view->child) {
1617 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1619 1a76625f 2018-10-22 stsp goto done;
1621 1a76625f 2018-10-22 stsp update_panels();
1622 1a76625f 2018-10-22 stsp doupdate();
1626 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1627 f2d749db 2022-07-12 thomas const struct got_error *close_err;
1628 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1629 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1630 f2d749db 2022-07-12 thomas close_err = view_close(view);
1631 f2d749db 2022-07-12 thomas if (close_err && err == NULL)
1632 f2d749db 2022-07-12 thomas err = close_err;
1635 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1636 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1637 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1639 e5a0f69f 2018-08-18 stsp return err;
1642 4ed7e80c 2018-05-20 stsp __dead static void
1643 9f7d7167 2018-04-29 stsp usage_log(void)
1646 c70c5802 2018-08-01 stsp fprintf(stderr,
1647 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1648 9f7d7167 2018-04-29 stsp getprogname());
1652 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1653 80ddbec8 2018-04-29 stsp static const struct got_error *
1654 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1656 00dfcb92 2018-06-11 stsp char *vis = NULL;
1657 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1659 963b370f 2018-05-20 stsp *ws = NULL;
1660 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1661 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1662 00dfcb92 2018-06-11 stsp int vislen;
1663 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1664 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1666 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1667 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1669 00dfcb92 2018-06-11 stsp return err;
1670 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1671 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1672 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1673 a7f50699 2018-06-11 stsp goto done;
1677 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1678 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1679 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1680 a7f50699 2018-06-11 stsp goto done;
1683 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1684 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1686 00dfcb92 2018-06-11 stsp free(vis);
1687 963b370f 2018-05-20 stsp if (err) {
1688 963b370f 2018-05-20 stsp free(*ws);
1689 963b370f 2018-05-20 stsp *ws = NULL;
1690 963b370f 2018-05-20 stsp *wlen = 0;
1692 963b370f 2018-05-20 stsp return err;
1695 05171be4 2022-06-23 thomas static const struct got_error *
1696 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1698 05171be4 2022-06-23 thomas char *dst;
1699 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1701 05171be4 2022-06-23 thomas *ptr = NULL;
1702 05171be4 2022-06-23 thomas n = len = strlen(src);
1703 83316834 2022-06-23 thomas dst = malloc(n + 1);
1704 05171be4 2022-06-23 thomas if (dst == NULL)
1705 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1707 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1708 05171be4 2022-06-23 thomas const char c = src[idx];
1710 05171be4 2022-06-23 thomas if (c == '\t') {
1711 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1712 b235ad5d 2022-06-23 thomas char *p;
1714 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1715 83316834 2022-06-23 thomas if (p == NULL) {
1716 83316834 2022-06-23 thomas free(dst);
1717 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1720 83316834 2022-06-23 thomas dst = p;
1721 05171be4 2022-06-23 thomas n += nb;
1722 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1723 05171be4 2022-06-23 thomas sz += nb;
1725 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1729 05171be4 2022-06-23 thomas dst[sz] = '\0';
1730 05171be4 2022-06-23 thomas *ptr = dst;
1731 05171be4 2022-06-23 thomas return NULL;
1735 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1736 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1737 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1738 8d208d34 2022-06-23 thomas * *rcol.
1740 8d208d34 2022-06-23 thomas static int
1741 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1743 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1745 8d208d34 2022-06-23 thomas if (n == 0) {
1746 8d208d34 2022-06-23 thomas *rcol = cols;
1747 8d208d34 2022-06-23 thomas return off;
1750 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1751 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1752 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1754 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1756 8d208d34 2022-06-23 thomas if (width == -1) {
1757 8d208d34 2022-06-23 thomas width = 1;
1758 8d208d34 2022-06-23 thomas wline[i] = L'.';
1761 8d208d34 2022-06-23 thomas if (cols + width > n)
1763 8d208d34 2022-06-23 thomas cols += width;
1766 8d208d34 2022-06-23 thomas *rcol = cols;
1767 8d208d34 2022-06-23 thomas return i;
1771 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1772 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1773 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1775 f91a2b48 2022-06-23 thomas static const struct got_error *
1776 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1777 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1779 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1780 8d208d34 2022-06-23 thomas int cols;
1781 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1782 05171be4 2022-06-23 thomas char *exstr = NULL;
1783 963b370f 2018-05-20 stsp size_t wlen;
1784 8d208d34 2022-06-23 thomas int i, scrollx;
1786 963b370f 2018-05-20 stsp *wlinep = NULL;
1787 b700b5d6 2018-07-10 stsp *widthp = 0;
1789 05171be4 2022-06-23 thomas if (expand) {
1790 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1791 05171be4 2022-06-23 thomas if (err)
1792 05171be4 2022-06-23 thomas return err;
1795 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1796 05171be4 2022-06-23 thomas free(exstr);
1798 963b370f 2018-05-20 stsp return err;
1800 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1802 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1803 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1806 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1807 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1811 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1812 8d208d34 2022-06-23 thomas wline[i] = L'\0';
1814 b700b5d6 2018-07-10 stsp if (widthp)
1815 b700b5d6 2018-07-10 stsp *widthp = cols;
1816 f91a2b48 2022-06-23 thomas if (scrollxp)
1817 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1819 963b370f 2018-05-20 stsp free(wline);
1821 963b370f 2018-05-20 stsp *wlinep = wline;
1822 963b370f 2018-05-20 stsp return err;
1825 8b473291 2019-02-21 stsp static const struct got_error*
1826 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1827 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1829 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1830 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1832 8b473291 2019-02-21 stsp const char *name;
1834 8b473291 2019-02-21 stsp *refs_str = NULL;
1836 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1837 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1838 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1841 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1842 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1844 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1845 8b473291 2019-02-21 stsp name += 5;
1846 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1847 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1849 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1850 8b473291 2019-02-21 stsp name += 6;
1851 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1852 8b473291 2019-02-21 stsp name += 8;
1853 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1854 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1857 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1860 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1861 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1862 5d844a1e 2019-08-13 stsp if (err) {
1863 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1864 48cae60d 2020-09-22 stsp free(ref_id);
1867 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1868 5d844a1e 2019-08-13 stsp err = NULL;
1869 5d844a1e 2019-08-13 stsp tag = NULL;
1872 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1873 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1874 48cae60d 2020-09-22 stsp free(ref_id);
1876 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1877 52b5abe1 2019-08-13 stsp if (cmp != 0)
1879 8b473291 2019-02-21 stsp s = *refs_str;
1880 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1881 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1882 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1884 8b473291 2019-02-21 stsp *refs_str = NULL;
1890 8b473291 2019-02-21 stsp return err;
1893 963b370f 2018-05-20 stsp static const struct got_error *
1894 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1895 27a741e5 2019-09-11 stsp int col_tab_align)
1897 e6b8b890 2020-12-29 naddy char *smallerthan;
1899 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1900 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1901 5813d178 2019-03-09 stsp author = smallerthan + 1;
1902 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1903 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1904 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1907 5813d178 2019-03-09 stsp static const struct got_error *
1908 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1909 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1910 8fdc79fe 2020-12-01 naddy int author_display_cols)
1912 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1913 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1914 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1915 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1916 5813d178 2019-03-09 stsp char *author = NULL;
1917 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1918 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1919 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1920 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1921 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1922 ccb26ccd 2018-11-05 stsp struct tm tm;
1923 45d799e2 2018-12-23 stsp time_t committer_time;
1924 11b20872 2019-11-08 stsp struct tog_color *tc;
1926 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1927 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1928 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1929 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1930 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1932 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1933 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1935 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1936 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1938 11b20872 2019-11-08 stsp wattr_on(view->window,
1939 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1940 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1942 11b20872 2019-11-08 stsp wattr_off(view->window,
1943 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1944 27a741e5 2019-09-11 stsp col = limit;
1945 b39d25c7 2018-07-10 stsp if (col > avail)
1946 b39d25c7 2018-07-10 stsp goto done;
1948 6570a66d 2019-11-08 stsp if (avail >= 120) {
1949 6570a66d 2019-11-08 stsp char *id_str;
1950 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1952 6570a66d 2019-11-08 stsp goto done;
1953 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1955 11b20872 2019-11-08 stsp wattr_on(view->window,
1956 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1957 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1959 11b20872 2019-11-08 stsp wattr_off(view->window,
1960 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1961 6570a66d 2019-11-08 stsp free(id_str);
1963 6570a66d 2019-11-08 stsp if (col > avail)
1964 6570a66d 2019-11-08 stsp goto done;
1967 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1968 5813d178 2019-03-09 stsp if (author == NULL) {
1969 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1970 80ddbec8 2018-04-29 stsp goto done;
1972 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1974 bb737323 2018-05-20 stsp goto done;
1975 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1977 11b20872 2019-11-08 stsp wattr_on(view->window,
1978 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1979 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1981 11b20872 2019-11-08 stsp wattr_off(view->window,
1982 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1983 bb737323 2018-05-20 stsp col += author_width;
1984 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1985 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1987 bb737323 2018-05-20 stsp author_width++;
1989 9c2eaf34 2018-05-20 stsp if (col > avail)
1990 9c2eaf34 2018-05-20 stsp goto done;
1992 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1994 6d9fbc00 2018-04-29 stsp goto done;
1995 bb737323 2018-05-20 stsp logmsg = logmsg0;
1996 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1998 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1999 bb737323 2018-05-20 stsp if (newline)
2000 bb737323 2018-05-20 stsp *newline = '\0';
2001 f91a2b48 2022-06-23 thomas limit = avail - col;
2002 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
2003 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
2004 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
2005 f91a2b48 2022-06-23 thomas limit, col, 1);
2006 331b1a16 2022-06-23 thomas if (err)
2007 331b1a16 2022-06-23 thomas goto done;
2008 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
2009 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
2010 27a741e5 2019-09-11 stsp while (col < avail) {
2011 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
2015 80ddbec8 2018-04-29 stsp free(logmsg0);
2016 bb737323 2018-05-20 stsp free(wlogmsg);
2017 5813d178 2019-03-09 stsp free(author);
2018 bb737323 2018-05-20 stsp free(wauthor);
2019 80ddbec8 2018-04-29 stsp free(line);
2020 80ddbec8 2018-04-29 stsp return err;
2023 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
2024 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
2025 899d86c2 2018-05-10 stsp struct got_object_id *id)
2027 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
2029 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
2030 80ddbec8 2018-04-29 stsp if (entry == NULL)
2031 899d86c2 2018-05-10 stsp return NULL;
2033 899d86c2 2018-05-10 stsp entry->id = id;
2034 99db9666 2018-05-07 stsp entry->commit = commit;
2035 899d86c2 2018-05-10 stsp return entry;
2038 99db9666 2018-05-07 stsp static void
2039 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
2041 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
2043 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
2044 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
2045 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
2046 ecb28ae0 2018-07-16 stsp commits->ncommits--;
2047 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
2048 99db9666 2018-05-07 stsp free(entry);
2051 99db9666 2018-05-07 stsp static void
2052 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
2054 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
2055 99db9666 2018-05-07 stsp pop_commit(commits);
2058 c4972b91 2018-05-07 stsp static const struct got_error *
2059 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
2060 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
2062 13add988 2019-10-15 stsp const struct got_error *err = NULL;
2063 13add988 2019-10-15 stsp regmatch_t regmatch;
2064 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
2066 13add988 2019-10-15 stsp *have_match = 0;
2068 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
2070 13add988 2019-10-15 stsp return err;
2072 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2074 13add988 2019-10-15 stsp goto done;
2076 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
2077 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2078 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
2079 13add988 2019-10-15 stsp ®match, 0) == 0 ||
2080 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
2081 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
2082 13add988 2019-10-15 stsp *have_match = 1;
2084 13add988 2019-10-15 stsp free(id_str);
2085 13add988 2019-10-15 stsp free(logmsg);
2086 13add988 2019-10-15 stsp return err;
2089 13add988 2019-10-15 stsp static const struct got_error *
2090 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
2092 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
2095 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
2096 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
2097 1a76625f 2018-10-22 stsp * while updating the display.
2100 93e45b7c 2018-09-24 stsp struct got_object_id *id;
2101 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
2102 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
2103 1a76625f 2018-10-22 stsp int errcode;
2105 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
2106 4e0d2870 2020-12-07 naddy NULL, NULL);
2107 ee780d5c 2020-01-04 stsp if (err || id == NULL)
2110 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
2113 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
2114 9ba79e04 2018-06-11 stsp if (entry == NULL) {
2115 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
2119 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2120 1a76625f 2018-10-22 stsp if (errcode) {
2121 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
2122 13add988 2019-10-15 stsp "pthread_mutex_lock");
2126 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
2127 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
2128 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
2130 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
2131 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
2132 7c1452c1 2020-03-26 stsp int have_match;
2133 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
2136 7c1452c1 2020-03-26 stsp if (have_match)
2137 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
2140 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2141 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2142 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2143 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2146 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
2148 9ba79e04 2018-06-11 stsp return err;
2151 2b779855 2020-12-05 naddy static void
2152 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
2154 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
2155 2b779855 2020-12-05 naddy int ncommits = 0;
2157 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
2158 2b779855 2020-12-05 naddy while (entry) {
2159 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
2160 2b779855 2020-12-05 naddy s->selected_entry = entry;
2163 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
2164 2b779855 2020-12-05 naddy ncommits++;
2168 0553a4e3 2018-04-30 stsp static const struct got_error *
2169 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
2171 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
2172 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
2173 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
2174 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
2175 60493ae3 2019-06-20 stsp int width;
2176 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
2177 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
2178 8b473291 2019-02-21 stsp char *refs_str = NULL;
2179 ecb28ae0 2018-07-16 stsp wchar_t *wline;
2180 11b20872 2019-11-08 stsp struct tog_color *tc;
2181 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
2183 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
2184 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
2185 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
2186 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
2188 ecb28ae0 2018-07-16 stsp return err;
2189 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
2190 d2075bf3 2020-12-25 stsp s->selected_entry->id);
2191 d2075bf3 2020-12-25 stsp if (refs) {
2192 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
2193 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
2195 d2075bf3 2020-12-25 stsp goto done;
2199 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
2200 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
2202 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
2203 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2204 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2205 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
2206 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
2207 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2208 8f4ed634 2020-03-26 stsp goto done;
2211 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
2213 f9686aa5 2020-03-27 stsp if (view->searching) {
2214 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
2215 f9686aa5 2020-03-27 stsp search_str = "no more matches";
2216 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
2217 f9686aa5 2020-03-27 stsp search_str = "no matches found";
2218 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
2219 f9686aa5 2020-03-27 stsp search_str = "searching...";
2222 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
2223 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
2224 f9686aa5 2020-03-27 stsp search_str ? search_str :
2225 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
2226 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
2227 8f4ed634 2020-03-26 stsp goto done;
2231 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2232 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2233 91198554 2022-06-23 thomas "........................................",
2234 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2235 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2236 1a76625f 2018-10-22 stsp header = NULL;
2237 1a76625f 2018-10-22 stsp goto done;
2239 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2240 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2241 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2242 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2243 1a76625f 2018-10-22 stsp header = NULL;
2244 1a76625f 2018-10-22 stsp goto done;
2246 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2248 1a76625f 2018-10-22 stsp goto done;
2250 2814baeb 2018-08-01 stsp werase(view->window);
2252 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2253 a3404814 2018-09-02 stsp wstandout(view->window);
2254 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2256 11b20872 2019-11-08 stsp wattr_on(view->window,
2257 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2258 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2260 11b20872 2019-11-08 stsp wattr_off(view->window,
2261 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2262 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2263 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2266 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2267 a3404814 2018-09-02 stsp wstandend(view->window);
2268 ecb28ae0 2018-07-16 stsp free(wline);
2269 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2270 1a76625f 2018-10-22 stsp goto done;
2272 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2273 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2274 5813d178 2019-03-09 stsp ncommits = 0;
2275 05171be4 2022-06-23 thomas view->maxx = 0;
2276 5813d178 2019-03-09 stsp while (entry) {
2277 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2278 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2279 5813d178 2019-03-09 stsp int width;
2280 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2282 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
2283 5813d178 2019-03-09 stsp if (author == NULL) {
2284 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2285 5813d178 2019-03-09 stsp goto done;
2287 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2288 27a741e5 2019-09-11 stsp date_display_cols);
2289 5813d178 2019-03-09 stsp if (author_cols < width)
2290 5813d178 2019-03-09 stsp author_cols = width;
2291 5813d178 2019-03-09 stsp free(wauthor);
2292 5813d178 2019-03-09 stsp free(author);
2293 05171be4 2022-06-23 thomas err = got_object_commit_get_logmsg(&msg0, entry->commit);
2294 05171be4 2022-06-23 thomas if (err)
2295 05171be4 2022-06-23 thomas goto done;
2296 05171be4 2022-06-23 thomas msg = msg0;
2297 05171be4 2022-06-23 thomas while (*msg == '\n')
2299 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2300 331b1a16 2022-06-23 thomas *eol = '\0';
2301 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2302 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2303 331b1a16 2022-06-23 thomas if (err)
2304 331b1a16 2022-06-23 thomas goto done;
2305 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2306 05171be4 2022-06-23 thomas free(msg0);
2307 331b1a16 2022-06-23 thomas free(wmsg);
2308 7ca04879 2019-10-19 stsp ncommits++;
2309 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2312 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2313 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2314 867c6645 2018-07-10 stsp ncommits = 0;
2315 899d86c2 2018-05-10 stsp while (entry) {
2316 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2318 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2319 2814baeb 2018-08-01 stsp wstandout(view->window);
2320 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2321 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2322 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2323 2814baeb 2018-08-01 stsp wstandend(view->window);
2325 60493ae3 2019-06-20 stsp goto done;
2326 0553a4e3 2018-04-30 stsp ncommits++;
2327 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2328 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2331 a5d43cac 2022-07-01 thomas view_border(view);
2333 1a76625f 2018-10-22 stsp free(id_str);
2334 8b473291 2019-02-21 stsp free(refs_str);
2335 1a76625f 2018-10-22 stsp free(ncommits_str);
2336 1a76625f 2018-10-22 stsp free(header);
2337 80ddbec8 2018-04-29 stsp return err;
2340 07b55e75 2018-05-10 stsp static void
2341 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2343 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2344 07b55e75 2018-05-10 stsp int nscrolled = 0;
2346 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2347 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2350 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2351 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2352 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2353 07b55e75 2018-05-10 stsp if (entry) {
2354 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2355 07b55e75 2018-05-10 stsp nscrolled++;
2360 aa075928 2018-05-10 stsp static const struct got_error *
2361 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2363 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2364 5e224a3e 2019-02-22 stsp int errcode;
2366 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2368 f2d749db 2022-07-12 thomas while (!ta->log_complete && !tog_thread_error &&
2369 f2d749db 2022-07-12 thomas (ta->commits_needed > 0 || ta->load_all)) {
2370 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2371 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2372 7aafa0d1 2019-02-22 stsp if (errcode)
2373 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2374 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2377 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2378 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2380 7c1452c1 2020-03-26 stsp if (!wait)
2383 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2384 ffe38506 2020-12-01 naddy show_log_view(view);
2385 7c1452c1 2020-03-26 stsp update_panels();
2386 7c1452c1 2020-03-26 stsp doupdate();
2388 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2389 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2390 82954512 2020-02-03 stsp if (errcode)
2391 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2392 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2394 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2395 ffe38506 2020-12-01 naddy show_log_view(view);
2396 7c1452c1 2020-03-26 stsp update_panels();
2397 7c1452c1 2020-03-26 stsp doupdate();
2400 5e224a3e 2019-02-22 stsp return NULL;
2403 a5d43cac 2022-07-01 thomas static const struct got_error *
2404 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2406 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2407 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2409 ae98518f 2022-07-12 thomas state->thread_args.commits_needed += view->nscrolled;
2410 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2411 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2413 a5d43cac 2022-07-01 thomas return err;
2416 5e224a3e 2019-02-22 stsp static const struct got_error *
2417 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2419 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2420 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2421 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2422 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2424 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2425 5e224a3e 2019-02-22 stsp return NULL;
2427 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2428 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2429 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2431 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2433 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2434 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2436 5e224a3e 2019-02-22 stsp return err;
2440 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2441 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2444 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2445 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2447 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2448 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2450 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2451 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2453 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
2454 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2456 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2458 dd0a52c1 2018-05-20 stsp return err;
2461 cd0acaa7 2018-05-20 stsp static const struct got_error *
2462 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2463 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2464 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2466 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2467 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2468 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2470 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2471 15a94983 2018-12-23 stsp if (diff_view == NULL)
2472 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2474 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2475 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2476 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2477 e5a0f69f 2018-08-18 stsp if (err == NULL)
2478 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2479 cd0acaa7 2018-05-20 stsp return err;
2482 80ddbec8 2018-04-29 stsp static const struct got_error *
2483 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2484 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2486 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2488 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2489 941e9f74 2019-05-21 stsp if (parent == NULL)
2490 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2492 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2493 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2494 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2495 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2496 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2497 941e9f74 2019-05-21 stsp s->tree = subtree;
2498 941e9f74 2019-05-21 stsp s->selected = 0;
2499 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2500 941e9f74 2019-05-21 stsp return NULL;
2503 941e9f74 2019-05-21 stsp static const struct got_error *
2504 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2505 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2507 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2508 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2509 941e9f74 2019-05-21 stsp const char *p;
2510 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2512 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2514 941e9f74 2019-05-21 stsp while (*p) {
2515 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2516 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2517 56e0773d 2019-11-28 stsp char *te_name;
2519 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2522 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2523 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2524 941e9f74 2019-05-21 stsp if (slash == NULL)
2525 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2527 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2528 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2529 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2532 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2533 56e0773d 2019-11-28 stsp if (te == NULL) {
2534 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2535 56e0773d 2019-11-28 stsp free(te_name);
2538 56e0773d 2019-11-28 stsp free(te_name);
2539 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2541 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2542 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2544 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2545 941e9f74 2019-05-21 stsp if (slash)
2546 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2548 941e9f74 2019-05-21 stsp subpath = strdup(path);
2549 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2550 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2554 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2559 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2560 941e9f74 2019-05-21 stsp free(tree_id);
2564 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2565 941e9f74 2019-05-21 stsp if (err) {
2566 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2569 941e9f74 2019-05-21 stsp if (slash == NULL)
2571 941e9f74 2019-05-21 stsp free(subpath);
2572 941e9f74 2019-05-21 stsp subpath = NULL;
2573 941e9f74 2019-05-21 stsp p = slash;
2576 941e9f74 2019-05-21 stsp free(subpath);
2577 1a76625f 2018-10-22 stsp return err;
2580 61266923 2020-01-14 stsp static const struct got_error *
2581 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2582 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2583 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2585 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2586 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2587 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2589 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2590 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2591 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2593 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2595 55cccc34 2020-02-20 stsp return err;
2596 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2598 55cccc34 2020-02-20 stsp *new_view = tree_view;
2600 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2601 55cccc34 2020-02-20 stsp return NULL;
2603 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2606 55cccc34 2020-02-20 stsp static const struct got_error *
2607 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2609 61266923 2020-01-14 stsp sigset_t sigset;
2610 61266923 2020-01-14 stsp int errcode;
2612 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2613 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2615 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2616 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2617 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2618 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2619 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2620 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2621 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2622 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2623 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2625 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2626 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2627 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2629 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2630 61266923 2020-01-14 stsp if (errcode)
2631 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2633 61266923 2020-01-14 stsp return NULL;
2636 1a76625f 2018-10-22 stsp static void *
2637 1a76625f 2018-10-22 stsp log_thread(void *arg)
2639 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2640 1a76625f 2018-10-22 stsp int errcode = 0;
2641 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2642 1a76625f 2018-10-22 stsp int done = 0;
2645 f2d749db 2022-07-12 thomas * Sync startup with main thread such that we begin our
2646 f2d749db 2022-07-12 thomas * work once view_input() has released the mutex.
2648 f2d749db 2022-07-12 thomas errcode = pthread_mutex_lock(&tog_mutex);
2649 f2d749db 2022-07-12 thomas if (errcode) {
2650 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
2651 61266923 2020-01-14 stsp return (void *)err;
2654 f2d749db 2022-07-12 thomas err = block_signals_used_by_main_thread();
2655 f2d749db 2022-07-12 thomas if (err) {
2656 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2657 f2d749db 2022-07-12 thomas goto done;
2660 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2661 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2662 f2d749db 2022-07-12 thomas if (errcode) {
2663 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode,
2664 f2d749db 2022-07-12 thomas "pthread_mutex_unlock");
2665 f2d749db 2022-07-12 thomas goto done;
2667 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2668 1a76625f 2018-10-22 stsp if (err) {
2669 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2670 f2d749db 2022-07-12 thomas goto done;
2671 1a76625f 2018-10-22 stsp err = NULL;
2673 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2674 1a76625f 2018-10-22 stsp a->commits_needed--;
2676 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2677 3abe8080 2019-04-10 stsp if (errcode) {
2678 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2679 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2680 f2d749db 2022-07-12 thomas goto done;
2681 3abe8080 2019-04-10 stsp } else if (*a->quit)
2683 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2684 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2685 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2686 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2689 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2690 7c1452c1 2020-03-26 stsp if (errcode) {
2691 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2692 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2693 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2694 f2d749db 2022-07-12 thomas goto done;
2698 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2700 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2701 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2702 7c1452c1 2020-03-26 stsp &tog_mutex);
2703 f2d749db 2022-07-12 thomas if (errcode) {
2704 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2705 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2706 f2d749db 2022-07-12 thomas pthread_mutex_unlock(&tog_mutex);
2707 f2d749db 2022-07-12 thomas goto done;
2709 21355643 2020-12-06 stsp if (*a->quit)
2714 3abe8080 2019-04-10 stsp a->log_complete = 1;
2715 f2d749db 2022-07-12 thomas errcode = pthread_mutex_unlock(&tog_mutex);
2716 f2d749db 2022-07-12 thomas if (errcode)
2717 f2d749db 2022-07-12 thomas err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2719 f2d749db 2022-07-12 thomas if (err) {
2720 f2d749db 2022-07-12 thomas tog_thread_error = 1;
2721 f2d749db 2022-07-12 thomas pthread_cond_signal(&a->commit_loaded);
2723 1a76625f 2018-10-22 stsp return (void *)err;
2726 1a76625f 2018-10-22 stsp static const struct got_error *
2727 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2729 f2d749db 2022-07-12 thomas const struct got_error *err = NULL, *thread_err = NULL;
2730 1a76625f 2018-10-22 stsp int errcode;
2732 1a76625f 2018-10-22 stsp if (s->thread) {
2733 1a76625f 2018-10-22 stsp s->quit = 1;
2734 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2735 1a76625f 2018-10-22 stsp if (errcode)
2736 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2737 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2738 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2739 1a76625f 2018-10-22 stsp if (errcode)
2740 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2741 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2742 f2d749db 2022-07-12 thomas errcode = pthread_join(s->thread, (void **)&thread_err);
2743 1a76625f 2018-10-22 stsp if (errcode)
2744 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2745 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2746 1a76625f 2018-10-22 stsp if (errcode)
2747 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2748 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2749 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2752 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2753 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2754 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2757 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2758 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2759 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2760 1af5eddf 2022-06-23 thomas if (err == NULL)
2761 1af5eddf 2022-06-23 thomas err = pack_err;
2762 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2765 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2766 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2767 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2770 f2d749db 2022-07-12 thomas return err ? err : thread_err;
2773 9343a5fb 2018-06-23 stsp static const struct got_error *
2774 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2776 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2777 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2778 276b94a1 2020-11-13 naddy int errcode;
2780 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2782 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2783 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2784 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2786 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2787 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2788 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2790 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2791 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2792 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2793 1a76625f 2018-10-22 stsp free(s->start_id);
2794 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2795 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2796 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2797 1a76625f 2018-10-22 stsp return err;
2800 1a76625f 2018-10-22 stsp static const struct got_error *
2801 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2803 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2805 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2806 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2807 60493ae3 2019-06-20 stsp return NULL;
2810 60493ae3 2019-06-20 stsp static const struct got_error *
2811 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2813 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2814 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2815 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2817 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2818 f9686aa5 2020-03-27 stsp show_log_view(view);
2819 f9686aa5 2020-03-27 stsp update_panels();
2820 f9686aa5 2020-03-27 stsp doupdate();
2822 96e2b566 2019-07-08 stsp if (s->search_entry) {
2823 13add988 2019-10-15 stsp int errcode, ch;
2824 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2825 13add988 2019-10-15 stsp if (errcode)
2826 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2827 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2828 13add988 2019-10-15 stsp ch = wgetch(view->window);
2829 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2830 13add988 2019-10-15 stsp if (errcode)
2831 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2832 13add988 2019-10-15 stsp "pthread_mutex_lock");
2833 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2834 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2835 678cbce5 2019-07-28 stsp return NULL;
2837 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2838 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2840 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2841 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2842 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2843 df5e841d 2022-06-23 thomas int matched_idx = s->matched_entry->idx;
2844 df5e841d 2022-06-23 thomas int selected_idx = s->selected_entry->idx;
2847 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
2848 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
2849 1f4d5162 2022-06-23 thomas * might have changed.
2851 b74feda6 2022-06-23 thomas if (view->searching == TOG_SEARCH_FORWARD) {
2852 df5e841d 2022-06-23 thomas if (matched_idx > selected_idx)
2853 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
2855 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->matched_entry, entry);
2856 b74feda6 2022-06-23 thomas } else {
2857 df5e841d 2022-06-23 thomas if (matched_idx < selected_idx)
2858 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->selected_entry,
2859 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2861 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->matched_entry,
2862 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2865 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2868 60493ae3 2019-06-20 stsp while (1) {
2869 13add988 2019-10-15 stsp int have_match = 0;
2871 60493ae3 2019-06-20 stsp if (entry == NULL) {
2872 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2873 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2874 f9967bca 2020-03-27 stsp view->search_next_done =
2875 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2876 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2877 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2878 f801134a 2019-06-25 stsp return NULL;
2881 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2882 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2883 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2885 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2886 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2889 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2890 13add988 2019-10-15 stsp &view->regex);
2893 13add988 2019-10-15 stsp if (have_match) {
2894 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2895 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2899 96e2b566 2019-07-08 stsp s->search_entry = entry;
2900 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2901 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2903 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2906 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2907 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2908 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2909 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2911 60493ae3 2019-06-20 stsp return err;
2914 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2915 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2917 60493ae3 2019-06-20 stsp return err;
2922 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2924 60493ae3 2019-06-20 stsp return NULL;
2927 60493ae3 2019-06-20 stsp static const struct got_error *
2928 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2929 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2930 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2932 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2933 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2934 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2935 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2936 1a76625f 2018-10-22 stsp int errcode;
2938 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2939 f135c941 2020-02-20 stsp free(s->in_repo_path);
2940 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2941 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2942 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2945 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2946 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2947 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2949 fb2756b9 2018-08-04 stsp s->repo = repo;
2950 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2951 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2952 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2953 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2954 9cd7cbd1 2020-12-07 stsp goto done;
2957 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2958 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2959 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2960 5036bf37 2018-09-24 stsp goto done;
2962 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2964 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2965 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2966 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2967 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2969 11b20872 2019-11-08 stsp goto done;
2970 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2971 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2972 11b20872 2019-11-08 stsp if (err) {
2973 11b20872 2019-11-08 stsp free_colors(&s->colors);
2974 11b20872 2019-11-08 stsp goto done;
2976 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2977 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2978 11b20872 2019-11-08 stsp if (err) {
2979 11b20872 2019-11-08 stsp free_colors(&s->colors);
2980 11b20872 2019-11-08 stsp goto done;
2984 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2985 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2986 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2987 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2988 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2990 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
2991 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2992 1af5eddf 2022-06-23 thomas if (err)
2993 1af5eddf 2022-06-23 thomas goto done;
2995 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2996 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
2997 7cd52833 2022-06-23 thomas if (err)
2998 7cd52833 2022-06-23 thomas goto done;
2999 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
3000 b672a97a 2020-01-27 stsp !s->log_branches);
3002 1a76625f 2018-10-22 stsp goto done;
3003 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
3004 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
3006 c5b78334 2020-01-12 stsp goto done;
3008 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
3009 1a76625f 2018-10-22 stsp if (errcode) {
3010 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
3011 1a76625f 2018-10-22 stsp goto done;
3013 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
3014 7c1452c1 2020-03-26 stsp if (errcode) {
3015 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
3016 7c1452c1 2020-03-26 stsp goto done;
3019 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
3020 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
3021 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
3022 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
3023 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
3024 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
3025 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
3026 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
3027 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
3028 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
3029 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
3030 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
3031 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
3034 1a76625f 2018-10-22 stsp close_log_view(view);
3035 ba4f502b 2018-08-04 stsp return err;
3038 e5a0f69f 2018-08-18 stsp static const struct got_error *
3039 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
3041 f2f6d207 2020-11-24 stsp const struct got_error *err;
3042 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
3044 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
3045 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
3046 2b380cc8 2018-10-24 stsp &s->thread_args);
3047 2b380cc8 2018-10-24 stsp if (errcode)
3048 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3049 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
3050 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
3052 f2f6d207 2020-11-24 stsp return err;
3056 8fdc79fe 2020-12-01 naddy return draw_commits(view);
3059 b31f89ff 2022-07-01 thomas static void
3060 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
3062 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3064 b31f89ff 2022-07-01 thomas if (s->selected_entry->idx == 0)
3065 b31f89ff 2022-07-01 thomas view->count = 0;
3066 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
3069 b31f89ff 2022-07-01 thomas if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
3070 b31f89ff 2022-07-01 thomas || home)
3071 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
3073 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
3074 b31f89ff 2022-07-01 thomas --s->selected;
3076 b31f89ff 2022-07-01 thomas log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
3078 b31f89ff 2022-07-01 thomas select_commit(s);
3082 b31f89ff 2022-07-01 thomas static const struct got_error *
3083 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
3085 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
3086 b31f89ff 2022-07-01 thomas struct commit_queue_entry *first;
3087 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
3089 b31f89ff 2022-07-01 thomas first = s->first_displayed_entry;
3090 b31f89ff 2022-07-01 thomas if (first == NULL) {
3091 b31f89ff 2022-07-01 thomas view->count = 0;
3092 b31f89ff 2022-07-01 thomas return NULL;
3095 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3096 b31f89ff 2022-07-01 thomas s->selected_entry->idx >= s->commits.ncommits - 1)
3097 b31f89ff 2022-07-01 thomas return NULL;
3099 b31f89ff 2022-07-01 thomas if (!page) {
3100 b31f89ff 2022-07-01 thomas int eos = view->nlines - 2;
3102 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3103 a5d43cac 2022-07-01 thomas --eos; /* border consumes the last line */
3104 b31f89ff 2022-07-01 thomas if (s->selected < MIN(eos, s->commits.ncommits - 1))
3105 b31f89ff 2022-07-01 thomas ++s->selected;
3107 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
3108 068ab281 2022-07-01 thomas } else if (s->thread_args.load_all) {
3109 b31f89ff 2022-07-01 thomas if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
3110 b31f89ff 2022-07-01 thomas s->selected += MIN(s->last_displayed_entry->idx -
3111 b31f89ff 2022-07-01 thomas s->selected_entry->idx, page + 1);
3113 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, MIN(page,
3114 b31f89ff 2022-07-01 thomas s->commits.ncommits - s->selected_entry->idx - 1));
3115 b31f89ff 2022-07-01 thomas s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
3116 b31f89ff 2022-07-01 thomas } else {
3117 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, page);
3118 b31f89ff 2022-07-01 thomas if (err)
3119 b31f89ff 2022-07-01 thomas return err;
3120 b31f89ff 2022-07-01 thomas if (first == s->first_displayed_entry && s->selected <
3121 b31f89ff 2022-07-01 thomas MIN(view->nlines - 2, s->commits.ncommits - 1)) {
3122 b31f89ff 2022-07-01 thomas s->selected = MIN(s->commits.ncommits - 1, page);
3125 b31f89ff 2022-07-01 thomas if (err)
3126 b31f89ff 2022-07-01 thomas return err;
3129 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
3130 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
3132 a5d43cac 2022-07-01 thomas s->selected = MIN(s->selected,
3133 a5d43cac 2022-07-01 thomas s->last_displayed_entry->idx - s->first_displayed_entry->idx);
3135 b31f89ff 2022-07-01 thomas select_commit(s);
3137 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
3138 b31f89ff 2022-07-01 thomas s->selected_entry->idx == s->commits.ncommits - 1)
3139 b31f89ff 2022-07-01 thomas view->count = 0;
3141 b31f89ff 2022-07-01 thomas return NULL;
3144 a5d43cac 2022-07-01 thomas static void
3145 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
3150 53d2bdd3 2022-07-10 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
3151 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_y)
3152 53d2bdd3 2022-07-10 thomas *y = view->child->resized_y;
3153 ddbc4d37 2022-07-12 thomas else if (view->resized_y)
3154 ddbc4d37 2022-07-12 thomas *y = view->resized_y;
3156 53d2bdd3 2022-07-10 thomas *y = view_split_begin_y(view->lines);
3157 ddbc4d37 2022-07-12 thomas } else if (view->mode == TOG_VIEW_SPLIT_VERT) {
3158 53d2bdd3 2022-07-10 thomas if (view->child && view->child->resized_x)
3159 53d2bdd3 2022-07-10 thomas *x = view->child->resized_x;
3160 ddbc4d37 2022-07-12 thomas else if (view->resized_x)
3161 ddbc4d37 2022-07-12 thomas *x = view->resized_x;
3163 53d2bdd3 2022-07-10 thomas *x = view_split_begin_x(view->begin_x);
3167 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
3168 e5a0f69f 2018-08-18 stsp static const struct got_error *
3169 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
3171 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
3173 a5d43cac 2022-07-01 thomas view->nlines = y;
3174 64486692 2022-07-07 thomas view->ncols = COLS;
3175 a5d43cac 2022-07-01 thomas err = view_resize(view);
3176 a5d43cac 2022-07-01 thomas if (err)
3177 a5d43cac 2022-07-01 thomas return err;
3179 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
3181 a5d43cac 2022-07-01 thomas return err;
3184 a5d43cac 2022-07-01 thomas static const struct got_error *
3185 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
3187 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3188 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
3189 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
3190 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
3191 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
3192 068ab281 2022-07-01 thomas int begin_x = 0, begin_y = 0, eos, n, nscroll;
3194 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
3195 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
3196 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
3197 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
3198 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, s->commits.ncommits);
3199 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
3201 b31f89ff 2022-07-01 thomas return err;
3204 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
3205 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
3206 068ab281 2022-07-01 thomas --eos; /* border */
3208 528dedf3 2021-08-30 stsp switch (ch) {
3210 1e37a5c2 2019-05-12 jcs s->quit = 1;
3212 05171be4 2022-06-23 thomas case '0':
3213 05171be4 2022-06-23 thomas view->x = 0;
3215 05171be4 2022-06-23 thomas case '$':
3216 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
3217 07b0611c 2022-06-23 thomas view->count = 0;
3219 05171be4 2022-06-23 thomas case KEY_RIGHT:
3220 05171be4 2022-06-23 thomas case 'l':
3221 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
3222 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
3224 07b0611c 2022-06-23 thomas view->count = 0;
3226 05171be4 2022-06-23 thomas case KEY_LEFT:
3227 05171be4 2022-06-23 thomas case 'h':
3228 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
3229 07b0611c 2022-06-23 thomas if (view->x <= 0)
3230 07b0611c 2022-06-23 thomas view->count = 0;
3233 1e37a5c2 2019-05-12 jcs case KEY_UP:
3236 f7140bf5 2021-10-17 thomas case CTRL('p'):
3237 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
3240 912a3f79 2021-08-30 j case KEY_HOME:
3241 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
3242 07b0611c 2022-06-23 thomas view->count = 0;
3244 70f17a53 2022-06-13 thomas case CTRL('u'):
3245 23427b14 2022-06-23 thomas case 'u':
3246 70f17a53 2022-06-13 thomas nscroll /= 2;
3247 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3248 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3249 a4292ac5 2019-05-12 jcs case CTRL('b'):
3250 1c5e5faa 2022-06-23 thomas case 'b':
3251 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3254 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3257 f7140bf5 2021-10-17 thomas case CTRL('n'):
3258 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3261 912a3f79 2021-08-30 j case KEY_END: {
3262 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3263 912a3f79 2021-08-30 j * traverse them all. */
3264 07b0611c 2022-06-23 thomas view->count = 0;
3265 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3266 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3267 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3270 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3271 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3272 068ab281 2022-07-01 thomas for (n = 0; n < eos; n++) {
3273 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3275 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3276 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3278 f3bc9f1d 2021-09-05 naddy if (n > 0)
3279 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3280 2b779855 2020-12-05 naddy select_commit(s);
3283 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3284 23427b14 2022-06-23 thomas case 'd':
3285 70f17a53 2022-06-13 thomas nscroll /= 2;
3286 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3287 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3288 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3289 4c2d69cb 2022-06-23 thomas case 'f':
3290 b31f89ff 2022-07-01 thomas case ' ':
3291 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3293 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3294 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3295 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3296 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3297 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3298 2b779855 2020-12-05 naddy select_commit(s);
3299 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3300 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3301 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3302 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3303 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3306 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3307 444d5325 2022-07-03 thomas case '\r':
3308 07b0611c 2022-06-23 thomas view->count = 0;
3309 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3312 a5d43cac 2022-07-01 thomas /* get dimensions--don't split till initialisation succeeds */
3313 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3314 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
3316 a5d43cac 2022-07-01 thomas err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3317 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3318 78756c87 2020-11-24 stsp view, s->repo);
3322 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3323 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3324 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
3325 a5d43cac 2022-07-01 thomas if (err)
3329 e78dc838 2020-12-04 stsp view->focussed = 0;
3330 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3331 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
3332 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
3334 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3335 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
3336 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3338 1e37a5c2 2019-05-12 jcs return err;
3339 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
3340 40236d76 2022-06-23 thomas if (err)
3341 40236d76 2022-06-23 thomas return err;
3342 e78dc838 2020-12-04 stsp view->focus_child = 1;
3344 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3347 07b0611c 2022-06-23 thomas view->count = 0;
3348 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3350 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3351 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3352 444d5325 2022-07-03 thomas err = browse_commit_tree(&tree_view, begin_y, begin_x,
3353 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3357 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3358 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3359 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3360 444d5325 2022-07-03 thomas if (err)
3363 e78dc838 2020-12-04 stsp view->focussed = 0;
3364 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3365 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
3366 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
3367 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3368 53d2bdd3 2022-07-10 thomas view_transfer_size(tree_view, view);
3369 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3371 1e37a5c2 2019-05-12 jcs return err;
3372 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
3373 40236d76 2022-06-23 thomas if (err)
3374 40236d76 2022-06-23 thomas return err;
3375 e78dc838 2020-12-04 stsp view->focus_child = 1;
3377 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3379 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3380 21355643 2020-12-06 stsp case CTRL('l'):
3382 07b0611c 2022-06-23 thomas view->count = 0;
3383 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3384 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3386 21355643 2020-12-06 stsp err = stop_log_thread(s);
3388 74cfe85e 2020-10-20 stsp return err;
3389 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3390 21355643 2020-12-06 stsp char *parent_path;
3391 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3393 21355643 2020-12-06 stsp return err;
3394 21355643 2020-12-06 stsp free(s->in_repo_path);
3395 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3396 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3397 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3398 21355643 2020-12-06 stsp struct got_object_id *start_id;
3399 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3400 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3401 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3403 21355643 2020-12-06 stsp return err;
3404 21355643 2020-12-06 stsp free(s->start_id);
3405 21355643 2020-12-06 stsp s->start_id = start_id;
3406 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3407 21355643 2020-12-06 stsp } else /* 'B' */
3408 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3410 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3411 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3412 bf7e79b3 2022-06-23 thomas if (err)
3413 bf7e79b3 2022-06-23 thomas return err;
3415 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3416 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3417 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3419 21355643 2020-12-06 stsp return err;
3420 51a10b52 2020-12-26 stsp tog_free_refs();
3421 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3423 ca51c541 2020-12-07 stsp return err;
3424 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3425 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3427 d01904d4 2019-06-25 stsp return err;
3428 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3429 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3431 b672a97a 2020-01-27 stsp return err;
3432 21355643 2020-12-06 stsp free_commits(&s->commits);
3433 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3434 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3435 21355643 2020-12-06 stsp s->selected_entry = NULL;
3436 21355643 2020-12-06 stsp s->selected = 0;
3437 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3438 21355643 2020-12-06 stsp s->quit = 0;
3439 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3440 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3441 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3444 07b0611c 2022-06-23 thomas view->count = 0;
3445 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3446 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3447 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3448 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3449 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3450 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3451 6458efa5 2020-11-24 stsp if (err) {
3452 6458efa5 2020-11-24 stsp view_close(ref_view);
3453 6458efa5 2020-11-24 stsp return err;
3455 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3456 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3457 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3458 444d5325 2022-07-03 thomas if (err)
3461 e78dc838 2020-12-04 stsp view->focussed = 0;
3462 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3463 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
3464 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
3465 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3466 53d2bdd3 2022-07-10 thomas view_transfer_size(ref_view, view);
3467 6458efa5 2020-11-24 stsp err = view_close_child(view);
3469 6458efa5 2020-11-24 stsp return err;
3470 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
3471 40236d76 2022-06-23 thomas if (err)
3472 40236d76 2022-06-23 thomas return err;
3473 e78dc838 2020-12-04 stsp view->focus_child = 1;
3475 6458efa5 2020-11-24 stsp *new_view = ref_view;
3478 07b0611c 2022-06-23 thomas view->count = 0;
3482 80ddbec8 2018-04-29 stsp return err;
3485 4ed7e80c 2018-05-20 stsp static const struct got_error *
3486 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3488 c2db6724 2019-01-04 stsp const struct got_error *error;
3490 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3491 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3492 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3494 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3495 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3497 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3498 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3500 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3501 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3503 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3504 c2db6724 2019-01-04 stsp if (error != NULL)
3505 c2db6724 2019-01-04 stsp return error;
3507 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3508 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3510 c2db6724 2019-01-04 stsp return NULL;
3513 a915003a 2019-02-05 stsp static void
3514 a915003a 2019-02-05 stsp init_curses(void)
3517 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3518 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3519 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3521 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3522 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3523 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3524 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3525 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3527 a915003a 2019-02-05 stsp initscr();
3529 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3532 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3533 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3534 a915003a 2019-02-05 stsp curs_set(0);
3535 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3536 6d17833f 2019-11-08 stsp start_color();
3537 6d17833f 2019-11-08 stsp use_default_colors();
3541 c2db6724 2019-01-04 stsp static const struct got_error *
3542 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3543 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3545 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3547 f135c941 2020-02-20 stsp if (argc == 0) {
3548 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3549 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3550 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3551 f135c941 2020-02-20 stsp return NULL;
3554 f135c941 2020-02-20 stsp if (worktree) {
3555 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3558 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3560 f135c941 2020-02-20 stsp return err;
3561 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3562 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3563 bfd61697 2020-11-14 stsp p) == -1) {
3564 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3565 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3569 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3571 f135c941 2020-02-20 stsp return err;
3574 f135c941 2020-02-20 stsp static const struct got_error *
3575 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3577 80ddbec8 2018-04-29 stsp const struct got_error *error;
3578 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3579 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3580 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3581 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3582 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3583 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3584 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3585 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3586 04cc582a 2018-08-01 stsp struct tog_view *view;
3587 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3589 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3590 80ddbec8 2018-04-29 stsp switch (ch) {
3592 b672a97a 2020-01-27 stsp log_branches = 1;
3595 80ddbec8 2018-04-29 stsp start_commit = optarg;
3598 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3599 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3600 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3604 17020d27 2019-03-07 stsp usage_log();
3605 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3609 80ddbec8 2018-04-29 stsp argc -= optind;
3610 80ddbec8 2018-04-29 stsp argv += optind;
3612 f135c941 2020-02-20 stsp if (argc > 1)
3613 f135c941 2020-02-20 stsp usage_log();
3615 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3616 7cd52833 2022-06-23 thomas if (error != NULL)
3617 7cd52833 2022-06-23 thomas goto done;
3619 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3620 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3621 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3622 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3623 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3624 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3625 c156c7a4 2020-12-18 stsp goto done;
3626 a1fbf39a 2019-08-11 stsp if (worktree)
3627 6962eb72 2020-02-20 stsp repo_path =
3628 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3630 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3631 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3632 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3633 c156c7a4 2020-12-18 stsp goto done;
3637 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3638 80ddbec8 2018-04-29 stsp if (error != NULL)
3639 ecb28ae0 2018-07-16 stsp goto done;
3641 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3642 f135c941 2020-02-20 stsp repo, worktree);
3643 f135c941 2020-02-20 stsp if (error)
3644 f135c941 2020-02-20 stsp goto done;
3646 f135c941 2020-02-20 stsp init_curses();
3648 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3649 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3650 c02c541e 2019-03-29 stsp if (error)
3651 c02c541e 2019-03-29 stsp goto done;
3653 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3654 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3655 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3656 87670572 2020-12-26 naddy if (error)
3657 87670572 2020-12-26 naddy goto done;
3660 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3661 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3662 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3663 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3664 d8f38dc4 2020-12-05 stsp if (error)
3665 d8f38dc4 2020-12-05 stsp goto done;
3666 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3668 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3669 d8f38dc4 2020-12-05 stsp if (error == NULL)
3670 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3671 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3672 d8f38dc4 2020-12-05 stsp goto done;
3673 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3674 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3675 d8f38dc4 2020-12-05 stsp if (error)
3676 d8f38dc4 2020-12-05 stsp goto done;
3679 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3680 04cc582a 2018-08-01 stsp if (view == NULL) {
3681 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3682 04cc582a 2018-08-01 stsp goto done;
3684 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3685 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3686 ba4f502b 2018-08-04 stsp if (error)
3687 ba4f502b 2018-08-04 stsp goto done;
3688 2fc00ff4 2019-08-31 stsp if (worktree) {
3689 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3690 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3691 2fc00ff4 2019-08-31 stsp worktree = NULL;
3693 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3695 f135c941 2020-02-20 stsp free(in_repo_path);
3696 ecb28ae0 2018-07-16 stsp free(repo_path);
3697 ecb28ae0 2018-07-16 stsp free(cwd);
3698 899d86c2 2018-05-10 stsp free(start_id);
3699 d8f38dc4 2020-12-05 stsp free(label);
3701 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3702 1d0f4054 2021-06-17 stsp if (repo) {
3703 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3704 1d0f4054 2021-06-17 stsp if (error == NULL)
3705 1d0f4054 2021-06-17 stsp error = close_err;
3707 ec142235 2019-03-07 stsp if (worktree)
3708 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3709 7cd52833 2022-06-23 thomas if (pack_fds) {
3710 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3711 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3712 7cd52833 2022-06-23 thomas if (error == NULL)
3713 7cd52833 2022-06-23 thomas error = pack_err;
3715 51a10b52 2020-12-26 stsp tog_free_refs();
3716 80ddbec8 2018-04-29 stsp return error;
3719 4ed7e80c 2018-05-20 stsp __dead static void
3720 9f7d7167 2018-04-29 stsp usage_diff(void)
3723 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3724 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3728 6d17833f 2019-11-08 stsp static int
3729 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3730 41605754 2020-11-12 stsp regmatch_t *regmatch)
3732 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3735 ef20f542 2022-06-26 thomas static struct tog_color *
3736 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3738 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3740 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3741 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3742 6d17833f 2019-11-08 stsp return tc;
3745 6d17833f 2019-11-08 stsp return NULL;
3748 4ed7e80c 2018-05-20 stsp static const struct got_error *
3749 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3750 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3752 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3753 666f7b10 2022-06-23 thomas char *exstr = NULL;
3754 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3755 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3756 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
3757 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3759 41605754 2020-11-12 stsp *wtotal = 0;
3761 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3762 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3764 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
3765 666f7b10 2022-06-23 thomas if (err)
3766 666f7b10 2022-06-23 thomas return err;
3768 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
3769 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
3770 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
3771 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
3772 666f7b10 2022-06-23 thomas goto done;
3774 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
3775 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
3776 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3777 cbea3800 2022-06-23 thomas goto done;
3779 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
3780 1065461d 2022-06-23 thomas if (seg2 == NULL) {
3781 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3782 cbea3800 2022-06-23 thomas goto done;
3785 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3786 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3787 cbea3800 2022-06-23 thomas col_tab_align, 1);
3788 cbea3800 2022-06-23 thomas if (err)
3789 cbea3800 2022-06-23 thomas goto done;
3790 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
3791 05171be4 2022-06-23 thomas if (n) {
3792 cbea3800 2022-06-23 thomas free(wline);
3793 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3794 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3795 cbea3800 2022-06-23 thomas if (err)
3796 cbea3800 2022-06-23 thomas goto done;
3797 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3798 cbea3800 2022-06-23 thomas wlimit -= width;
3799 cbea3800 2022-06-23 thomas *wtotal += width;
3802 41605754 2020-11-12 stsp if (wlimit > 0) {
3803 cbea3800 2022-06-23 thomas int i = 0, w = 0;
3804 cbea3800 2022-06-23 thomas size_t wlen;
3806 cbea3800 2022-06-23 thomas free(wline);
3807 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3808 cbea3800 2022-06-23 thomas col_tab_align, 1);
3809 cbea3800 2022-06-23 thomas if (err)
3810 cbea3800 2022-06-23 thomas goto done;
3811 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
3812 cbea3800 2022-06-23 thomas while (i < wlen) {
3813 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
3814 cbea3800 2022-06-23 thomas if (width == -1) {
3815 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
3816 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
3817 cbea3800 2022-06-23 thomas goto done;
3819 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
3821 1c5e5faa 2022-06-23 thomas w += width;
3824 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3825 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
3826 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3827 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
3828 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3829 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
3830 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
3834 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
3835 cbea3800 2022-06-23 thomas free(wline);
3836 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
3837 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
3838 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
3839 cbea3800 2022-06-23 thomas col_tab_align, 1);
3840 cbea3800 2022-06-23 thomas if (err)
3841 cbea3800 2022-06-23 thomas goto done;
3842 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3843 cbea3800 2022-06-23 thomas } else {
3844 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
3845 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3846 cbea3800 2022-06-23 thomas if (err)
3847 cbea3800 2022-06-23 thomas goto done;
3848 cbea3800 2022-06-23 thomas waddwstr(window, wline);
3850 cbea3800 2022-06-23 thomas *wtotal += width2;
3853 05171be4 2022-06-23 thomas free(wline);
3854 666f7b10 2022-06-23 thomas free(exstr);
3855 cbea3800 2022-06-23 thomas free(seg0);
3856 cbea3800 2022-06-23 thomas free(seg1);
3857 cbea3800 2022-06-23 thomas free(seg2);
3858 cbea3800 2022-06-23 thomas return err;
3861 41605754 2020-11-12 stsp static const struct got_error *
3862 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3864 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3865 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3866 61e69b96 2018-05-20 stsp const struct got_error *err;
3867 fe621944 2020-11-10 stsp int nprinted = 0;
3868 b304db33 2018-05-20 stsp char *line;
3869 826082fe 2020-12-10 stsp size_t linesize = 0;
3870 826082fe 2020-12-10 stsp ssize_t linelen;
3871 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3872 61e69b96 2018-05-20 stsp wchar_t *wline;
3873 e0b650dd 2018-05-20 stsp int width;
3874 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3875 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3876 fe621944 2020-11-10 stsp off_t line_offset;
3878 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3879 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3880 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3882 f7d12f7e 2018-08-01 stsp werase(view->window);
3884 a3404814 2018-09-02 stsp if (header) {
3885 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3886 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3887 135a2da0 2020-11-11 stsp header) == -1)
3888 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3889 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3891 135a2da0 2020-11-11 stsp free(line);
3893 a3404814 2018-09-02 stsp return err;
3895 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3896 a3404814 2018-09-02 stsp wstandout(view->window);
3897 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3898 e54cc94a 2020-11-11 stsp free(wline);
3899 e54cc94a 2020-11-11 stsp wline = NULL;
3900 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3901 a3404814 2018-09-02 stsp wstandend(view->window);
3902 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3903 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3905 a3404814 2018-09-02 stsp if (max_lines <= 1)
3906 a3404814 2018-09-02 stsp return NULL;
3907 a3404814 2018-09-02 stsp max_lines--;
3910 89f1a395 2020-12-01 naddy s->eof = 0;
3911 05171be4 2022-06-23 thomas view->maxx = 0;
3912 826082fe 2020-12-10 stsp line = NULL;
3913 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3914 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3915 826082fe 2020-12-10 stsp if (linelen == -1) {
3916 826082fe 2020-12-10 stsp if (feof(s->f)) {
3917 826082fe 2020-12-10 stsp s->eof = 1;
3920 826082fe 2020-12-10 stsp free(line);
3921 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3924 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
3925 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3926 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
3927 cbea3800 2022-06-23 thomas if (err) {
3928 cbea3800 2022-06-23 thomas free(line);
3929 cbea3800 2022-06-23 thomas return err;
3931 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
3932 cbea3800 2022-06-23 thomas free(wline);
3933 cbea3800 2022-06-23 thomas wline = NULL;
3935 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3937 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3938 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3939 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3940 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3941 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3942 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
3943 41605754 2020-11-12 stsp if (err) {
3944 41605754 2020-11-12 stsp free(line);
3945 41605754 2020-11-12 stsp return err;
3948 f1c0ec19 2022-06-23 thomas int skip;
3949 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
3950 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
3951 f1c0ec19 2022-06-23 thomas if (err) {
3952 f1c0ec19 2022-06-23 thomas free(line);
3953 f1c0ec19 2022-06-23 thomas return err;
3955 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
3956 f1c0ec19 2022-06-23 thomas free(wline);
3957 41605754 2020-11-12 stsp wline = NULL;
3960 6d17833f 2019-11-08 stsp wattr_off(view->window,
3961 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3962 f1c0ec19 2022-06-23 thomas if (width <= view->ncols - 1)
3963 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3964 fe621944 2020-11-10 stsp nprinted++;
3966 826082fe 2020-12-10 stsp free(line);
3967 fe621944 2020-11-10 stsp if (nprinted >= 1)
3968 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3969 89f1a395 2020-12-01 naddy (nprinted - 1);
3971 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3973 a5d43cac 2022-07-01 thomas view_border(view);
3975 89f1a395 2020-12-01 naddy if (s->eof) {
3976 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3977 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3978 c3e9aa98 2019-05-13 jcs nprinted++;
3981 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3982 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
3984 c3e9aa98 2019-05-13 jcs return err;
3987 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3988 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3989 e54cc94a 2020-11-11 stsp free(wline);
3990 e54cc94a 2020-11-11 stsp wline = NULL;
3991 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3994 26ed57b2 2018-05-19 stsp return NULL;
3997 abd2672a 2018-12-23 stsp static char *
3998 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
4000 09867e48 2019-08-13 stsp struct tm mytm, *tm;
4001 09867e48 2019-08-13 stsp char *p, *s;
4003 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
4004 09867e48 2019-08-13 stsp if (tm == NULL)
4005 09867e48 2019-08-13 stsp return NULL;
4006 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
4007 09867e48 2019-08-13 stsp if (s == NULL)
4008 09867e48 2019-08-13 stsp return NULL;
4009 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
4011 abd2672a 2018-12-23 stsp *p = '\0';
4015 4ed7e80c 2018-05-20 stsp static const struct got_error *
4016 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
4017 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
4019 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
4020 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
4021 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4022 0208f208 2020-05-05 stsp struct got_object_qid *qid;
4024 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
4025 0208f208 2020-05-05 stsp if (qid != NULL) {
4026 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
4027 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
4028 ec242592 2022-04-22 thomas &qid->id);
4030 0208f208 2020-05-05 stsp return err;
4032 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
4033 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
4034 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
4035 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
4036 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
4038 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
4042 0208f208 2020-05-05 stsp if (tree_id1) {
4043 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
4045 0208f208 2020-05-05 stsp goto done;
4048 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
4049 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
4051 0208f208 2020-05-05 stsp goto done;
4053 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
4054 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
4056 0208f208 2020-05-05 stsp if (tree1)
4057 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
4058 0208f208 2020-05-05 stsp if (tree2)
4059 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
4060 aa8b5dd0 2021-08-01 stsp free(tree_id1);
4061 0208f208 2020-05-05 stsp return err;
4064 0208f208 2020-05-05 stsp static const struct got_error *
4065 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
4069 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
4070 fe621944 2020-11-10 stsp if (p == NULL)
4071 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
4072 fe621944 2020-11-10 stsp *line_offsets = p;
4073 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
4074 fe621944 2020-11-10 stsp (*nlines)++;
4075 fe621944 2020-11-10 stsp return NULL;
4078 fe621944 2020-11-10 stsp static const struct got_error *
4079 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
4080 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4081 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
4083 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
4084 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
4085 15a94983 2018-12-23 stsp struct got_commit_object *commit;
4086 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
4087 45d799e2 2018-12-23 stsp time_t committer_time;
4088 45d799e2 2018-12-23 stsp const char *author, *committer;
4089 8b473291 2019-02-21 stsp char *refs_str = NULL;
4090 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
4091 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
4092 fe621944 2020-11-10 stsp off_t outoff = 0;
4095 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
4097 8b473291 2019-02-21 stsp if (refs) {
4098 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
4100 8b473291 2019-02-21 stsp return err;
4103 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4105 abd2672a 2018-12-23 stsp return err;
4107 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
4108 15a94983 2018-12-23 stsp if (err) {
4109 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
4110 15a94983 2018-12-23 stsp goto done;
4113 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
4115 fe621944 2020-11-10 stsp goto done;
4117 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4118 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
4119 fe621944 2020-11-10 stsp if (n < 0) {
4120 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4121 abd2672a 2018-12-23 stsp goto done;
4123 fe621944 2020-11-10 stsp outoff += n;
4124 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4126 fe621944 2020-11-10 stsp goto done;
4128 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
4129 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
4130 fe621944 2020-11-10 stsp if (n < 0) {
4131 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
4132 abd2672a 2018-12-23 stsp goto done;
4134 fe621944 2020-11-10 stsp outoff += n;
4135 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4137 fe621944 2020-11-10 stsp goto done;
4139 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
4140 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
4141 fe621944 2020-11-10 stsp if (datestr) {
4142 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
4143 fe621944 2020-11-10 stsp if (n < 0) {
4144 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4145 fe621944 2020-11-10 stsp goto done;
4147 fe621944 2020-11-10 stsp outoff += n;
4148 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4150 fe621944 2020-11-10 stsp goto done;
4152 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
4153 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
4154 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
4155 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
4156 fe621944 2020-11-10 stsp if (n < 0) {
4157 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4158 fe621944 2020-11-10 stsp goto done;
4160 fe621944 2020-11-10 stsp outoff += n;
4161 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4163 fe621944 2020-11-10 stsp goto done;
4165 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
4166 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
4167 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
4168 ac4dc263 2021-09-24 thomas int pn = 1;
4169 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
4170 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
4171 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
4172 ac4dc263 2021-09-24 thomas if (err)
4173 ac4dc263 2021-09-24 thomas goto done;
4174 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
4175 ac4dc263 2021-09-24 thomas if (n < 0) {
4176 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
4177 ac4dc263 2021-09-24 thomas goto done;
4179 ac4dc263 2021-09-24 thomas outoff += n;
4180 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
4181 ac4dc263 2021-09-24 thomas if (err)
4182 ac4dc263 2021-09-24 thomas goto done;
4183 ac4dc263 2021-09-24 thomas free(id_str);
4184 ac4dc263 2021-09-24 thomas id_str = NULL;
4188 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
4190 5943eee2 2019-08-13 stsp goto done;
4191 fe621944 2020-11-10 stsp s = logmsg;
4192 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
4193 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
4194 fe621944 2020-11-10 stsp if (n < 0) {
4195 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4196 fe621944 2020-11-10 stsp goto done;
4198 fe621944 2020-11-10 stsp outoff += n;
4199 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4201 fe621944 2020-11-10 stsp goto done;
4204 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
4206 0208f208 2020-05-05 stsp goto done;
4207 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4208 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
4209 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
4210 fe621944 2020-11-10 stsp if (n < 0) {
4211 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
4212 fe621944 2020-11-10 stsp goto done;
4214 fe621944 2020-11-10 stsp outoff += n;
4215 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4217 fe621944 2020-11-10 stsp goto done;
4218 0208f208 2020-05-05 stsp free((char *)pe->path);
4219 0208f208 2020-05-05 stsp free(pe->data);
4222 0208f208 2020-05-05 stsp fputc('\n', outfile);
4224 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
4226 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4227 abd2672a 2018-12-23 stsp free(id_str);
4228 5943eee2 2019-08-13 stsp free(logmsg);
4229 8b473291 2019-02-21 stsp free(refs_str);
4230 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4231 7510f233 2020-08-09 stsp if (err) {
4232 ae6a6978 2020-08-09 stsp free(*line_offsets);
4233 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
4234 ae6a6978 2020-08-09 stsp *nlines = 0;
4236 fe621944 2020-11-10 stsp return err;
4239 abd2672a 2018-12-23 stsp static const struct got_error *
4240 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
4242 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
4243 48ae06ee 2018-10-18 stsp FILE *f = NULL;
4244 15a94983 2018-12-23 stsp int obj_type;
4246 fe621944 2020-11-10 stsp free(s->line_offsets);
4247 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
4248 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
4249 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
4250 fe621944 2020-11-10 stsp s->nlines = 0;
4252 511a516b 2018-05-19 stsp f = got_opentemp();
4253 48ae06ee 2018-10-18 stsp if (f == NULL) {
4254 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4255 48ae06ee 2018-10-18 stsp goto done;
4257 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4258 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4259 fb43ecf1 2019-02-11 stsp goto done;
4263 15a94983 2018-12-23 stsp if (s->id1)
4264 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4266 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4268 15a94983 2018-12-23 stsp goto done;
4270 15a94983 2018-12-23 stsp switch (obj_type) {
4271 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4272 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4273 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4274 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4275 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4277 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4278 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4279 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4280 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4281 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4283 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4284 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4285 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4286 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4287 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4289 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4291 3ffacbe1 2020-02-02 tracey goto done;
4292 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4293 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4294 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4295 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4296 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4297 f44b1f58 2020-02-02 tracey if (err)
4298 f44b1f58 2020-02-02 tracey goto done;
4299 f44b1f58 2020-02-02 tracey } else {
4300 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4301 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4302 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4303 fe621944 2020-11-10 stsp err = write_commit_info(
4304 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4305 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4306 f44b1f58 2020-02-02 tracey if (err)
4307 f44b1f58 2020-02-02 tracey goto done;
4312 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4314 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4315 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4316 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4317 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4321 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4324 f44b1f58 2020-02-02 tracey if (err)
4325 f44b1f58 2020-02-02 tracey goto done;
4327 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4328 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4329 48ae06ee 2018-10-18 stsp return err;
4332 f5215bb9 2019-02-22 stsp static void
4333 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4335 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4336 f5215bb9 2019-02-22 stsp update_panels();
4337 f5215bb9 2019-02-22 stsp doupdate();
4340 f44b1f58 2020-02-02 tracey static const struct got_error *
4341 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4343 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4345 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4346 f44b1f58 2020-02-02 tracey return NULL;
4349 f44b1f58 2020-02-02 tracey static const struct got_error *
4350 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4352 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4353 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4354 f44b1f58 2020-02-02 tracey int lineno;
4355 78eecffc 2022-06-23 thomas char *line = NULL;
4356 826082fe 2020-12-10 stsp size_t linesize = 0;
4357 826082fe 2020-12-10 stsp ssize_t linelen;
4359 f44b1f58 2020-02-02 tracey if (!view->searching) {
4360 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4361 f44b1f58 2020-02-02 tracey return NULL;
4364 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4365 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4366 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4368 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4370 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4372 f44b1f58 2020-02-02 tracey while (1) {
4373 f44b1f58 2020-02-02 tracey off_t offset;
4375 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4376 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4377 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4381 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4382 f44b1f58 2020-02-02 tracey lineno = 1;
4384 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4387 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4388 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4389 f44b1f58 2020-02-02 tracey free(line);
4390 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4392 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4393 78eecffc 2022-06-23 thomas if (linelen != -1) {
4394 78eecffc 2022-06-23 thomas char *exstr;
4395 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4396 78eecffc 2022-06-23 thomas if (err)
4398 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4399 78eecffc 2022-06-23 thomas &view->regmatch)) {
4400 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4401 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4402 78eecffc 2022-06-23 thomas free(exstr);
4405 78eecffc 2022-06-23 thomas free(exstr);
4407 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4408 f44b1f58 2020-02-02 tracey lineno++;
4410 f44b1f58 2020-02-02 tracey lineno--;
4412 826082fe 2020-12-10 stsp free(line);
4414 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4415 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4416 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4419 05171be4 2022-06-23 thomas return err;
4422 48ae06ee 2018-10-18 stsp static const struct got_error *
4423 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4425 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4426 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4428 a0f32f33 2022-06-13 thomas free(s->id1);
4429 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4430 a0f32f33 2022-06-13 thomas free(s->id2);
4431 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4432 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4433 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4434 a0f32f33 2022-06-13 thomas s->f = NULL;
4435 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4436 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4437 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4438 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4439 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4440 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4441 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4442 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4443 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4444 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4445 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4446 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4447 a0f32f33 2022-06-13 thomas free_colors(&s->colors);
4448 a0f32f33 2022-06-13 thomas free(s->line_offsets);
4449 a0f32f33 2022-06-13 thomas s->line_offsets = NULL;
4450 a0f32f33 2022-06-13 thomas s->nlines = 0;
4451 a0f32f33 2022-06-13 thomas return err;
4454 a0f32f33 2022-06-13 thomas static const struct got_error *
4455 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4456 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4457 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4458 4fc71f3b 2022-07-12 thomas struct tog_view *parent_view, struct got_repository *repo)
4460 48ae06ee 2018-10-18 stsp const struct got_error *err;
4461 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4463 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4464 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4465 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4467 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4468 15a94983 2018-12-23 stsp int type1, type2;
4469 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4471 15a94983 2018-12-23 stsp return err;
4472 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4474 15a94983 2018-12-23 stsp return err;
4476 15a94983 2018-12-23 stsp if (type1 != type2)
4477 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4479 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4480 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4481 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4482 f44b1f58 2020-02-02 tracey s->repo = repo;
4483 f44b1f58 2020-02-02 tracey s->id1 = id1;
4484 f44b1f58 2020-02-02 tracey s->id2 = id2;
4485 3dbaef42 2020-11-24 stsp s->label1 = label1;
4486 3dbaef42 2020-11-24 stsp s->label2 = label2;
4488 15a94983 2018-12-23 stsp if (id1) {
4489 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4490 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4491 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4493 5465d566 2020-02-01 tracey s->id1 = NULL;
4495 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4496 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4497 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4498 a0f32f33 2022-06-13 thomas goto done;
4501 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4502 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4503 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4504 9a1d7435 2022-06-23 thomas goto done;
4507 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4508 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4509 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4510 a0f32f33 2022-06-13 thomas goto done;
4513 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4514 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4515 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4516 19a6a6b5 2022-07-01 thomas goto done;
4519 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4520 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4521 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4522 19a6a6b5 2022-07-01 thomas goto done;
4525 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4526 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4527 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4528 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4529 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4530 4fc71f3b 2022-07-12 thomas s->parent_view = parent_view;
4531 5465d566 2020-02-01 tracey s->repo = repo;
4533 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4534 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4535 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4536 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4537 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4539 a0f32f33 2022-06-13 thomas goto done;
4540 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4541 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4542 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4543 a0f32f33 2022-06-13 thomas if (err)
4544 a0f32f33 2022-06-13 thomas goto done;
4545 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4546 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4547 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4548 a0f32f33 2022-06-13 thomas if (err)
4549 a0f32f33 2022-06-13 thomas goto done;
4551 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4552 9b4458b4 2022-06-26 thomas "^(commit [0-9a-f]|parent [0-9]|"
4553 9b4458b4 2022-06-26 thomas "(blob|file|tree|commit) [-+] |"
4554 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4555 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4556 a0f32f33 2022-06-13 thomas if (err)
4557 a0f32f33 2022-06-13 thomas goto done;
4559 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4560 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4561 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4562 a0f32f33 2022-06-13 thomas if (err)
4563 a0f32f33 2022-06-13 thomas goto done;
4565 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4566 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4567 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4568 a0f32f33 2022-06-13 thomas if (err)
4569 a0f32f33 2022-06-13 thomas goto done;
4572 4fc71f3b 2022-07-12 thomas if (parent_view && parent_view->type == TOG_VIEW_LOG &&
4573 4fc71f3b 2022-07-12 thomas view_is_splitscreen(view))
4574 4fc71f3b 2022-07-12 thomas show_log_view(parent_view); /* draw border */
4575 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4577 5465d566 2020-02-01 tracey err = create_diff(s);
4579 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4580 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4581 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4582 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4583 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4584 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4586 a0f32f33 2022-06-13 thomas if (err)
4587 a0f32f33 2022-06-13 thomas close_diff_view(view);
4588 e5a0f69f 2018-08-18 stsp return err;
4591 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4592 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4594 a3404814 2018-09-02 stsp const struct got_error *err;
4595 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4596 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4597 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4599 a3404814 2018-09-02 stsp if (s->id1) {
4600 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4602 a3404814 2018-09-02 stsp return err;
4603 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4605 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4607 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4609 a3404814 2018-09-02 stsp return err;
4610 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4612 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4613 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4614 a3404814 2018-09-02 stsp free(id_str1);
4615 a3404814 2018-09-02 stsp free(id_str2);
4616 a3404814 2018-09-02 stsp return err;
4618 a3404814 2018-09-02 stsp free(id_str1);
4619 a3404814 2018-09-02 stsp free(id_str2);
4621 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4622 267bb3b8 2021-08-01 stsp free(header);
4623 267bb3b8 2021-08-01 stsp return err;
4626 15a087fe 2019-02-21 stsp static const struct got_error *
4627 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4628 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4630 d7a04538 2019-02-21 stsp const struct got_error *err;
4631 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4632 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4633 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4635 15a087fe 2019-02-21 stsp free(s->id2);
4636 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4637 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4638 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4640 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4642 d7a04538 2019-02-21 stsp return err;
4643 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4644 15a087fe 2019-02-21 stsp free(s->id1);
4645 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4646 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4647 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4648 15a087fe 2019-02-21 stsp return NULL;
4651 0cf4efb1 2018-09-29 stsp static const struct got_error *
4652 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4654 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4656 adf4c9e0 2022-07-03 thomas view->count = 0;
4657 adf4c9e0 2022-07-03 thomas wclear(view->window);
4658 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4659 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4660 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4661 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4662 adf4c9e0 2022-07-03 thomas return create_diff(s);
4665 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_selected_commit_id(struct tog_blame_line *,
4666 4fc71f3b 2022-07-12 thomas int, int, int);
4667 4fc71f3b 2022-07-12 thomas static struct got_object_id *get_annotation_for_line(struct tog_blame_line *,
4668 4fc71f3b 2022-07-12 thomas int, int);
4670 adf4c9e0 2022-07-03 thomas static const struct got_error *
4671 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4673 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4674 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4675 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4676 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4677 826082fe 2020-12-10 stsp char *line = NULL;
4678 826082fe 2020-12-10 stsp size_t linesize = 0;
4679 826082fe 2020-12-10 stsp ssize_t linelen;
4680 4fc71f3b 2022-07-12 thomas int i, nscroll = view->nlines - 1, up = 0;
4682 e5a0f69f 2018-08-18 stsp switch (ch) {
4683 05171be4 2022-06-23 thomas case '0':
4684 05171be4 2022-06-23 thomas view->x = 0;
4686 05171be4 2022-06-23 thomas case '$':
4687 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4688 07b0611c 2022-06-23 thomas view->count = 0;
4690 05171be4 2022-06-23 thomas case KEY_RIGHT:
4691 05171be4 2022-06-23 thomas case 'l':
4692 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4693 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4695 07b0611c 2022-06-23 thomas view->count = 0;
4697 05171be4 2022-06-23 thomas case KEY_LEFT:
4698 05171be4 2022-06-23 thomas case 'h':
4699 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4700 07b0611c 2022-06-23 thomas if (view->x <= 0)
4701 07b0611c 2022-06-23 thomas view->count = 0;
4705 3dbaef42 2020-11-24 stsp if (ch == 'a')
4706 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4707 3dbaef42 2020-11-24 stsp if (ch == 'w')
4708 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4709 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
4712 912a3f79 2021-08-30 j case KEY_HOME:
4713 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4714 07b0611c 2022-06-23 thomas view->count = 0;
4717 912a3f79 2021-08-30 j case KEY_END:
4718 07b0611c 2022-06-23 thomas view->count = 0;
4722 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4726 1e37a5c2 2019-05-12 jcs case KEY_UP:
4727 f7140bf5 2021-10-17 thomas case CTRL('p'):
4728 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4729 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4731 07b0611c 2022-06-23 thomas view->count = 0;
4733 70f17a53 2022-06-13 thomas case CTRL('u'):
4734 23427b14 2022-06-23 thomas case 'u':
4735 70f17a53 2022-06-13 thomas nscroll /= 2;
4736 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4737 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4738 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4739 1c5e5faa 2022-06-23 thomas case 'b':
4740 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
4741 07b0611c 2022-06-23 thomas view->count = 0;
4745 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
4746 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4749 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4750 f7140bf5 2021-10-17 thomas case CTRL('n'):
4751 1e37a5c2 2019-05-12 jcs if (!s->eof)
4752 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4754 07b0611c 2022-06-23 thomas view->count = 0;
4756 70f17a53 2022-06-13 thomas case CTRL('d'):
4757 23427b14 2022-06-23 thomas case 'd':
4758 70f17a53 2022-06-13 thomas nscroll /= 2;
4759 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4760 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4761 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4762 1c5e5faa 2022-06-23 thomas case 'f':
4764 07b0611c 2022-06-23 thomas if (s->eof) {
4765 07b0611c 2022-06-23 thomas view->count = 0;
4769 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4770 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4771 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4772 826082fe 2020-12-10 stsp if (linelen == -1) {
4773 826082fe 2020-12-10 stsp if (feof(s->f)) {
4774 826082fe 2020-12-10 stsp s->eof = 1;
4776 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4780 826082fe 2020-12-10 stsp free(line);
4783 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4784 1e37a5c2 2019-05-12 jcs s->diff_context--;
4785 aa61903a 2021-12-31 thomas s->matched_line = 0;
4786 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4787 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4788 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4789 27829c9e 2020-11-21 stsp s->nlines) {
4790 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4791 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4794 07b0611c 2022-06-23 thomas view->count = 0;
4797 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4798 1e37a5c2 2019-05-12 jcs s->diff_context++;
4799 aa61903a 2021-12-31 thomas s->matched_line = 0;
4800 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4801 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4803 07b0611c 2022-06-23 thomas view->count = 0;
4808 4fc71f3b 2022-07-12 thomas /* FALL THROUGH */
4809 4fc71f3b 2022-07-12 thomas case '>':
4810 4fc71f3b 2022-07-12 thomas case '.':
4811 4fc71f3b 2022-07-12 thomas if (s->parent_view == NULL) {
4812 07b0611c 2022-06-23 thomas view->count = 0;
4815 4fc71f3b 2022-07-12 thomas s->parent_view->count = view->count;
4817 4fc71f3b 2022-07-12 thomas if (s->parent_view->type == TOG_VIEW_LOG) {
4818 4fc71f3b 2022-07-12 thomas ls = &s->parent_view->state.log;
4819 4fc71f3b 2022-07-12 thomas old_selected_entry = ls->selected_entry;
4821 4fc71f3b 2022-07-12 thomas err = input_log_view(NULL, s->parent_view,
4822 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4823 4fc71f3b 2022-07-12 thomas if (err)
4825 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4827 4fc71f3b 2022-07-12 thomas if (old_selected_entry == ls->selected_entry)
4830 4fc71f3b 2022-07-12 thomas err = set_selected_commit(s, ls->selected_entry);
4831 4fc71f3b 2022-07-12 thomas if (err)
4833 4fc71f3b 2022-07-12 thomas } else if (s->parent_view->type == TOG_VIEW_BLAME) {
4834 4fc71f3b 2022-07-12 thomas struct tog_blame_view_state *bs;
4835 4fc71f3b 2022-07-12 thomas struct got_object_id *id, *prev_id;
4837 4fc71f3b 2022-07-12 thomas bs = &s->parent_view->state.blame;
4838 4fc71f3b 2022-07-12 thomas prev_id = get_annotation_for_line(bs->blame.lines,
4839 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->last_diffed_line);
4841 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view,
4842 4fc71f3b 2022-07-12 thomas up ? KEY_UP : KEY_DOWN);
4843 4fc71f3b 2022-07-12 thomas if (err)
4845 4fc71f3b 2022-07-12 thomas view->count = s->parent_view->count;
4847 4fc71f3b 2022-07-12 thomas if (prev_id == NULL)
4849 4fc71f3b 2022-07-12 thomas id = get_selected_commit_id(bs->blame.lines,
4850 4fc71f3b 2022-07-12 thomas bs->blame.nlines, bs->first_displayed_line,
4851 4fc71f3b 2022-07-12 thomas bs->selected_line);
4852 4fc71f3b 2022-07-12 thomas if (id == NULL)
4855 4fc71f3b 2022-07-12 thomas if (!got_object_id_cmp(prev_id, id))
4858 4fc71f3b 2022-07-12 thomas err = input_blame_view(&view, s->parent_view, KEY_ENTER);
4859 4fc71f3b 2022-07-12 thomas if (err)
4862 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4863 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4864 aa61903a 2021-12-31 thomas s->matched_line = 0;
4865 05171be4 2022-06-23 thomas view->x = 0;
4867 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4868 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4871 07b0611c 2022-06-23 thomas view->count = 0;
4875 bcbd79e2 2018-08-19 stsp return err;
4878 4ed7e80c 2018-05-20 stsp static const struct got_error *
4879 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4881 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4882 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4883 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4884 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4885 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4886 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4887 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4888 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4889 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4890 3dbaef42 2020-11-24 stsp const char *errstr;
4891 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4892 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4894 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4895 26ed57b2 2018-05-19 stsp switch (ch) {
4897 64453f7e 2020-11-21 stsp force_text_diff = 1;
4900 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4902 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4903 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
4904 8f666e67 2022-02-12 thomas errstr, errstr);
4906 09b5bff8 2020-02-23 naddy case 'r':
4907 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4908 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4909 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4911 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4914 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4917 17020d27 2019-03-07 stsp usage_diff();
4918 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4922 26ed57b2 2018-05-19 stsp argc -= optind;
4923 26ed57b2 2018-05-19 stsp argv += optind;
4925 26ed57b2 2018-05-19 stsp if (argc == 0) {
4926 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4927 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4928 15a94983 2018-12-23 stsp id_str1 = argv[0];
4929 15a94983 2018-12-23 stsp id_str2 = argv[1];
4931 26ed57b2 2018-05-19 stsp usage_diff();
4933 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4934 7cd52833 2022-06-23 thomas if (error)
4935 7cd52833 2022-06-23 thomas goto done;
4937 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4938 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4939 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4940 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4941 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4942 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4943 c156c7a4 2020-12-18 stsp goto done;
4944 a273ac94 2020-02-23 naddy if (worktree)
4945 a273ac94 2020-02-23 naddy repo_path =
4946 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4948 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4949 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4950 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4951 c156c7a4 2020-12-18 stsp goto done;
4955 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4956 eb6600df 2019-01-04 stsp if (error)
4957 eb6600df 2019-01-04 stsp goto done;
4959 a273ac94 2020-02-23 naddy init_curses();
4961 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4962 51a10b52 2020-12-26 stsp if (error)
4963 51a10b52 2020-12-26 stsp goto done;
4965 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4966 26ed57b2 2018-05-19 stsp if (error)
4967 26ed57b2 2018-05-19 stsp goto done;
4969 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4970 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4971 26ed57b2 2018-05-19 stsp if (error)
4972 26ed57b2 2018-05-19 stsp goto done;
4974 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4975 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4976 26ed57b2 2018-05-19 stsp if (error)
4977 26ed57b2 2018-05-19 stsp goto done;
4979 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4980 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4981 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4982 ea5e7bb5 2018-08-01 stsp goto done;
4984 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4985 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4986 5dc9f4bc 2018-08-04 stsp if (error)
4987 5dc9f4bc 2018-08-04 stsp goto done;
4988 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4990 3dbaef42 2020-11-24 stsp free(label1);
4991 3dbaef42 2020-11-24 stsp free(label2);
4992 c02c541e 2019-03-29 stsp free(repo_path);
4993 a273ac94 2020-02-23 naddy free(cwd);
4994 1d0f4054 2021-06-17 stsp if (repo) {
4995 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4996 1d0f4054 2021-06-17 stsp if (error == NULL)
4997 1d0f4054 2021-06-17 stsp error = close_err;
4999 a273ac94 2020-02-23 naddy if (worktree)
5000 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
5001 7cd52833 2022-06-23 thomas if (pack_fds) {
5002 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5003 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5004 7cd52833 2022-06-23 thomas if (error == NULL)
5005 7cd52833 2022-06-23 thomas error = pack_err;
5007 51a10b52 2020-12-26 stsp tog_free_refs();
5008 26ed57b2 2018-05-19 stsp return error;
5011 4ed7e80c 2018-05-20 stsp __dead static void
5012 9f7d7167 2018-04-29 stsp usage_blame(void)
5015 91198554 2022-06-23 thomas fprintf(stderr,
5016 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
5017 9f7d7167 2018-04-29 stsp getprogname());
5021 84451b3e 2018-07-10 stsp struct tog_blame_line {
5022 84451b3e 2018-07-10 stsp int annotated;
5023 84451b3e 2018-07-10 stsp struct got_object_id *id;
5026 4ed7e80c 2018-05-20 stsp static const struct got_error *
5027 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
5029 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5030 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5031 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
5032 84451b3e 2018-07-10 stsp const struct got_error *err;
5033 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
5034 826082fe 2020-12-10 stsp char *line = NULL;
5035 826082fe 2020-12-10 stsp size_t linesize = 0;
5036 826082fe 2020-12-10 stsp ssize_t linelen;
5037 84451b3e 2018-07-10 stsp wchar_t *wline;
5038 27a741e5 2019-09-11 stsp int width;
5039 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
5040 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
5041 ab089a2a 2018-07-12 stsp char *id_str;
5042 11b20872 2019-11-08 stsp struct tog_color *tc;
5044 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
5046 ab089a2a 2018-07-12 stsp return err;
5048 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
5049 f7d12f7e 2018-08-01 stsp werase(view->window);
5051 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
5052 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5053 ab089a2a 2018-07-12 stsp free(id_str);
5054 ab089a2a 2018-07-12 stsp return err;
5057 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5058 ab089a2a 2018-07-12 stsp free(line);
5059 2550e4c3 2018-07-13 stsp line = NULL;
5061 1cae65b4 2019-09-22 stsp return err;
5062 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5063 a3404814 2018-09-02 stsp wstandout(view->window);
5064 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5066 11b20872 2019-11-08 stsp wattr_on(view->window,
5067 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5068 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5070 11b20872 2019-11-08 stsp wattr_off(view->window,
5071 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5072 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5073 a3404814 2018-09-02 stsp wstandend(view->window);
5074 2550e4c3 2018-07-13 stsp free(wline);
5075 2550e4c3 2018-07-13 stsp wline = NULL;
5076 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5077 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5079 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
5080 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
5081 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
5082 ab089a2a 2018-07-12 stsp free(id_str);
5083 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5085 ab089a2a 2018-07-12 stsp free(id_str);
5086 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
5087 3f60a8ef 2018-07-10 stsp free(line);
5088 2550e4c3 2018-07-13 stsp line = NULL;
5090 3f60a8ef 2018-07-10 stsp return err;
5091 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5092 2550e4c3 2018-07-13 stsp free(wline);
5093 2550e4c3 2018-07-13 stsp wline = NULL;
5094 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5095 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5097 4f7c3e5e 2020-12-01 naddy s->eof = 0;
5098 05171be4 2022-06-23 thomas view->maxx = 0;
5099 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
5100 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
5101 826082fe 2020-12-10 stsp if (linelen == -1) {
5102 826082fe 2020-12-10 stsp if (feof(blame->f)) {
5103 826082fe 2020-12-10 stsp s->eof = 1;
5106 84451b3e 2018-07-10 stsp free(line);
5107 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
5109 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
5112 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
5113 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
5114 cbea3800 2022-06-23 thomas if (err) {
5115 cbea3800 2022-06-23 thomas free(line);
5116 cbea3800 2022-06-23 thomas return err;
5118 cbea3800 2022-06-23 thomas free(wline);
5119 cbea3800 2022-06-23 thomas wline = NULL;
5120 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
5122 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5123 f7d12f7e 2018-08-01 stsp wstandout(view->window);
5125 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
5126 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
5127 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
5128 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
5129 4fc71f3b 2022-07-12 thomas !(nprinted == s->selected_line - 1)) {
5130 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5131 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
5132 8d0fe45a 2019-08-12 stsp char *id_str;
5133 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
5134 91198554 2022-06-23 thomas blame_line->id);
5135 8d0fe45a 2019-08-12 stsp if (err) {
5136 8d0fe45a 2019-08-12 stsp free(line);
5137 8d0fe45a 2019-08-12 stsp return err;
5139 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5141 11b20872 2019-11-08 stsp wattr_on(view->window,
5142 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5143 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
5145 11b20872 2019-11-08 stsp wattr_off(view->window,
5146 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5147 8d0fe45a 2019-08-12 stsp free(id_str);
5148 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
5150 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5151 8d0fe45a 2019-08-12 stsp prev_id = NULL;
5154 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
5155 ee41ec32 2018-07-10 stsp prev_id = NULL;
5158 4fc71f3b 2022-07-12 thomas if (nprinted == s->selected_line - 1)
5159 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5160 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
5162 41605754 2020-11-12 stsp if (view->ncols <= 9) {
5163 41605754 2020-11-12 stsp width = 9;
5164 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
5165 4f7c3e5e 2020-12-01 naddy s->matched_line &&
5166 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
5167 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
5168 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
5169 41605754 2020-11-12 stsp if (err) {
5170 41605754 2020-11-12 stsp free(line);
5171 41605754 2020-11-12 stsp return err;
5173 41605754 2020-11-12 stsp width += 9;
5175 923086fd 2022-06-23 thomas int skip;
5176 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
5177 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
5178 923086fd 2022-06-23 thomas if (err) {
5179 923086fd 2022-06-23 thomas free(line);
5180 923086fd 2022-06-23 thomas return err;
5182 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
5183 02bce7e2 2022-06-23 thomas width += 9;
5184 41605754 2020-11-12 stsp free(wline);
5185 41605754 2020-11-12 stsp wline = NULL;
5188 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
5189 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
5190 84451b3e 2018-07-10 stsp if (++nprinted == 1)
5191 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
5193 826082fe 2020-12-10 stsp free(line);
5194 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
5196 a5d43cac 2022-07-01 thomas view_border(view);
5198 84451b3e 2018-07-10 stsp return NULL;
5201 84451b3e 2018-07-10 stsp static const struct got_error *
5202 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
5203 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
5205 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
5206 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
5207 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
5208 1a76625f 2018-10-22 stsp int errcode;
5210 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
5211 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
5212 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
5214 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5215 1a76625f 2018-10-22 stsp if (errcode)
5216 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
5218 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
5219 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
5220 d68a0a7d 2018-07-10 stsp goto done;
5223 d68a0a7d 2018-07-10 stsp if (lineno == -1)
5224 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
5226 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
5227 d68a0a7d 2018-07-10 stsp if (line->annotated)
5228 d68a0a7d 2018-07-10 stsp goto done;
5230 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
5231 84451b3e 2018-07-10 stsp if (line->id == NULL) {
5232 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5233 84451b3e 2018-07-10 stsp goto done;
5235 84451b3e 2018-07-10 stsp line->annotated = 1;
5237 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5238 1a76625f 2018-10-22 stsp if (errcode)
5239 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5240 84451b3e 2018-07-10 stsp return err;
5243 84451b3e 2018-07-10 stsp static void *
5244 84451b3e 2018-07-10 stsp blame_thread(void *arg)
5246 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
5247 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
5248 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
5249 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
5250 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
5252 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
5253 9117a7b7 2022-07-01 thomas if (fd1 == -1)
5254 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
5256 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
5257 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
5258 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5259 9117a7b7 2022-07-01 thomas goto done;
5262 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
5263 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
5264 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5265 9117a7b7 2022-07-01 thomas goto done;
5267 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5268 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5269 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5270 9117a7b7 2022-07-01 thomas goto done;
5273 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5275 9117a7b7 2022-07-01 thomas goto done;
5277 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5278 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5279 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5280 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5281 fc06ba56 2019-08-22 stsp err = NULL;
5283 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5284 9117a7b7 2022-07-01 thomas if (errcode) {
5285 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5286 9117a7b7 2022-07-01 thomas goto done;
5289 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5290 1d0f4054 2021-06-17 stsp if (err == NULL)
5291 1d0f4054 2021-06-17 stsp err = close_err;
5292 c9beca56 2018-07-22 stsp ta->repo = NULL;
5293 c9beca56 2018-07-22 stsp *ta->complete = 1;
5295 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5296 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5297 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5300 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5301 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5302 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5303 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5304 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5305 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5306 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5307 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5309 18430de3 2018-07-10 stsp return (void *)err;
5312 245d91c1 2018-07-12 stsp static struct got_object_id *
5313 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5314 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5316 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5318 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5319 8d0fe45a 2019-08-12 stsp return NULL;
5321 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5322 4fc71f3b 2022-07-12 thomas if (!line->annotated)
5323 4fc71f3b 2022-07-12 thomas return NULL;
5325 4fc71f3b 2022-07-12 thomas return line->id;
5328 4fc71f3b 2022-07-12 thomas static struct got_object_id *
5329 4fc71f3b 2022-07-12 thomas get_annotation_for_line(struct tog_blame_line *lines, int nlines,
5330 4fc71f3b 2022-07-12 thomas int lineno)
5332 4fc71f3b 2022-07-12 thomas struct tog_blame_line *line;
5334 4fc71f3b 2022-07-12 thomas if (nlines <= 0 || lineno >= nlines)
5335 4fc71f3b 2022-07-12 thomas return NULL;
5337 4fc71f3b 2022-07-12 thomas line = &lines[lineno - 1];
5338 245d91c1 2018-07-12 stsp if (!line->annotated)
5339 245d91c1 2018-07-12 stsp return NULL;
5341 245d91c1 2018-07-12 stsp return line->id;
5344 b880a918 2018-07-10 stsp static const struct got_error *
5345 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5347 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5350 245d91c1 2018-07-12 stsp if (blame->thread) {
5351 1a76625f 2018-10-22 stsp int errcode;
5352 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5353 1a76625f 2018-10-22 stsp if (errcode)
5354 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5355 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5356 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5357 1a76625f 2018-10-22 stsp if (errcode)
5358 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5359 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5360 1a76625f 2018-10-22 stsp if (errcode)
5361 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5362 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5363 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5364 245d91c1 2018-07-12 stsp err = NULL;
5365 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5367 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5368 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5369 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5370 1d0f4054 2021-06-17 stsp if (err == NULL)
5371 1d0f4054 2021-06-17 stsp err = close_err;
5372 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5374 245d91c1 2018-07-12 stsp if (blame->f) {
5375 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5376 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5377 245d91c1 2018-07-12 stsp blame->f = NULL;
5379 57670559 2018-12-23 stsp if (blame->lines) {
5380 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5381 57670559 2018-12-23 stsp free(blame->lines[i].id);
5382 57670559 2018-12-23 stsp free(blame->lines);
5383 57670559 2018-12-23 stsp blame->lines = NULL;
5385 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5386 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5387 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5388 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5389 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5390 7cd52833 2022-06-23 thomas if (err == NULL)
5391 7cd52833 2022-06-23 thomas err = pack_err;
5392 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5394 245d91c1 2018-07-12 stsp return err;
5397 245d91c1 2018-07-12 stsp static const struct got_error *
5398 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5400 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5401 fc06ba56 2019-08-22 stsp int *done = arg;
5402 fc06ba56 2019-08-22 stsp int errcode;
5404 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5405 fc06ba56 2019-08-22 stsp if (errcode)
5406 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5407 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5409 fc06ba56 2019-08-22 stsp if (*done)
5410 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5412 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5413 fc06ba56 2019-08-22 stsp if (errcode)
5414 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5415 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5417 fc06ba56 2019-08-22 stsp return err;
5420 fc06ba56 2019-08-22 stsp static const struct got_error *
5421 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5423 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5424 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5425 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5426 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5427 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5428 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5429 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5430 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5431 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5433 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5434 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5436 15a94983 2018-12-23 stsp return err;
5438 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5439 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5440 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5441 f4ae6ddb 2022-07-01 thomas goto done;
5444 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5445 945f9229 2022-04-16 thomas if (err)
5446 945f9229 2022-04-16 thomas goto done;
5448 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5450 84451b3e 2018-07-10 stsp goto done;
5452 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5453 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5454 84451b3e 2018-07-10 stsp goto done;
5457 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5459 a70480e0 2018-06-23 stsp goto done;
5460 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5461 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5462 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5463 84451b3e 2018-07-10 stsp goto done;
5465 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5466 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5468 1fddf795 2021-01-20 stsp goto done;
5469 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5470 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5471 84451b3e 2018-07-10 stsp goto done;
5474 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5475 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5476 b02560ec 2019-08-19 stsp blame->nlines--;
5478 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5479 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5480 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5481 84451b3e 2018-07-10 stsp goto done;
5484 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5486 bd24772e 2018-07-11 stsp goto done;
5487 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5488 7cd52833 2022-06-23 thomas pack_fds);
5489 7cd52833 2022-06-23 thomas if (err)
5490 7cd52833 2022-06-23 thomas goto done;
5492 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5493 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5494 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5495 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5496 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5497 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5498 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5499 245d91c1 2018-07-12 stsp goto done;
5501 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5503 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5504 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5505 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5506 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5507 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5508 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5509 a5388363 2020-12-01 naddy s->blame_complete = 0;
5511 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5512 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5513 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5514 f5a09613 2020-12-13 naddy s->selected_line = 1;
5516 aa61903a 2021-12-31 thomas s->matched_line = 0;
5519 945f9229 2022-04-16 thomas if (commit)
5520 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5521 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5522 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5524 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5525 27d434c2 2018-09-15 stsp free(obj_id);
5527 245d91c1 2018-07-12 stsp stop_blame(blame);
5528 245d91c1 2018-07-12 stsp return err;
5531 245d91c1 2018-07-12 stsp static const struct got_error *
5532 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5533 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5535 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5536 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5538 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5540 c4843652 2019-08-12 stsp s->path = strdup(path);
5541 c4843652 2019-08-12 stsp if (s->path == NULL)
5542 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5544 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5545 c4843652 2019-08-12 stsp if (err) {
5546 c4843652 2019-08-12 stsp free(s->path);
5547 7cbe629d 2018-08-04 stsp return err;
5550 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5551 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5552 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5553 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5554 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5555 fb2756b9 2018-08-04 stsp s->repo = repo;
5556 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5557 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5559 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5560 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5561 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5562 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5564 11b20872 2019-11-08 stsp return err;
5567 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5568 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5569 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5570 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5571 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5572 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5574 a5388363 2020-12-01 naddy return run_blame(view);
5577 e5a0f69f 2018-08-18 stsp static const struct got_error *
5578 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5580 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5581 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5583 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5584 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5586 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5587 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5588 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5589 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5590 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5593 e5a0f69f 2018-08-18 stsp free(s->path);
5594 11b20872 2019-11-08 stsp free_colors(&s->colors);
5595 e5a0f69f 2018-08-18 stsp return err;
5598 7cbe629d 2018-08-04 stsp static const struct got_error *
5599 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5601 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5603 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5604 6c4c42e0 2019-06-24 stsp return NULL;
5607 6c4c42e0 2019-06-24 stsp static const struct got_error *
5608 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5610 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5611 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5612 6c4c42e0 2019-06-24 stsp int lineno;
5613 78eecffc 2022-06-23 thomas char *line = NULL;
5614 826082fe 2020-12-10 stsp size_t linesize = 0;
5615 826082fe 2020-12-10 stsp ssize_t linelen;
5617 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5618 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5619 6c4c42e0 2019-06-24 stsp return NULL;
5622 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5623 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5624 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5626 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5628 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5630 6c4c42e0 2019-06-24 stsp while (1) {
5631 6c4c42e0 2019-06-24 stsp off_t offset;
5633 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5634 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5635 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5639 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5640 6c4c42e0 2019-06-24 stsp lineno = 1;
5642 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5645 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5646 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5647 6c4c42e0 2019-06-24 stsp free(line);
5648 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5650 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5651 78eecffc 2022-06-23 thomas if (linelen != -1) {
5652 78eecffc 2022-06-23 thomas char *exstr;
5653 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5654 78eecffc 2022-06-23 thomas if (err)
5656 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5657 78eecffc 2022-06-23 thomas &view->regmatch)) {
5658 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5659 78eecffc 2022-06-23 thomas s->matched_line = lineno;
5660 78eecffc 2022-06-23 thomas free(exstr);
5663 78eecffc 2022-06-23 thomas free(exstr);
5665 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5670 826082fe 2020-12-10 stsp free(line);
5672 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5673 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5674 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5677 05171be4 2022-06-23 thomas return err;
5680 6c4c42e0 2019-06-24 stsp static const struct got_error *
5681 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5683 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5684 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5685 2b380cc8 2018-10-24 stsp int errcode;
5687 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
5688 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5689 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5690 2b380cc8 2018-10-24 stsp if (errcode)
5691 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5693 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5696 51fe7530 2019-08-19 stsp if (s->blame_complete)
5697 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5699 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5701 a5d43cac 2022-07-01 thomas view_border(view);
5702 e5a0f69f 2018-08-18 stsp return err;
5705 e5a0f69f 2018-08-18 stsp static const struct got_error *
5706 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5708 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5709 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
5710 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5711 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
5713 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
5714 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
5715 a5d43cac 2022-07-01 thomas --eos; /* border */
5717 e5a0f69f 2018-08-18 stsp switch (ch) {
5718 05171be4 2022-06-23 thomas case '0':
5719 05171be4 2022-06-23 thomas view->x = 0;
5721 05171be4 2022-06-23 thomas case '$':
5722 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5723 07b0611c 2022-06-23 thomas view->count = 0;
5725 05171be4 2022-06-23 thomas case KEY_RIGHT:
5726 05171be4 2022-06-23 thomas case 'l':
5727 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5728 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5730 07b0611c 2022-06-23 thomas view->count = 0;
5732 05171be4 2022-06-23 thomas case KEY_LEFT:
5733 05171be4 2022-06-23 thomas case 'h':
5734 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5735 07b0611c 2022-06-23 thomas if (view->x <= 0)
5736 07b0611c 2022-06-23 thomas view->count = 0;
5739 1e37a5c2 2019-05-12 jcs s->done = 1;
5741 4deef56f 2021-09-02 naddy case 'g':
5742 4deef56f 2021-09-02 naddy case KEY_HOME:
5743 4deef56f 2021-09-02 naddy s->selected_line = 1;
5744 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5745 07b0611c 2022-06-23 thomas view->count = 0;
5747 4deef56f 2021-09-02 naddy case 'G':
5748 4deef56f 2021-09-02 naddy case KEY_END:
5749 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
5750 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5751 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5753 a5d43cac 2022-07-01 thomas s->selected_line = eos;
5754 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
5756 07b0611c 2022-06-23 thomas view->count = 0;
5759 1e37a5c2 2019-05-12 jcs case KEY_UP:
5760 f7140bf5 2021-10-17 thomas case CTRL('p'):
5761 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5762 1e37a5c2 2019-05-12 jcs s->selected_line--;
5763 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5764 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5765 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5767 07b0611c 2022-06-23 thomas view->count = 0;
5769 70f17a53 2022-06-13 thomas case CTRL('u'):
5770 23427b14 2022-06-23 thomas case 'u':
5771 70f17a53 2022-06-13 thomas nscroll /= 2;
5772 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5773 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5774 ea025d1d 2020-02-22 naddy case CTRL('b'):
5775 1c5e5faa 2022-06-23 thomas case 'b':
5776 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5777 07b0611c 2022-06-23 thomas if (view->count > 1)
5778 07b0611c 2022-06-23 thomas nscroll += nscroll;
5779 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
5780 07b0611c 2022-06-23 thomas view->count = 0;
5783 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
5784 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
5786 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5789 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5790 f7140bf5 2021-10-17 thomas case CTRL('n'):
5791 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
5792 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5793 1e37a5c2 2019-05-12 jcs s->selected_line++;
5794 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5795 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5797 07b0611c 2022-06-23 thomas view->count = 0;
5799 1c5e5faa 2022-06-23 thomas case 'c':
5800 1e37a5c2 2019-05-12 jcs case 'p': {
5801 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5803 07b0611c 2022-06-23 thomas view->count = 0;
5804 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5805 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5806 1e37a5c2 2019-05-12 jcs if (id == NULL)
5808 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5809 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
5810 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5811 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5812 1e37a5c2 2019-05-12 jcs int obj_type;
5813 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5814 1e37a5c2 2019-05-12 jcs s->repo, id);
5817 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5818 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5819 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5820 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5823 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5824 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
5825 ec242592 2022-04-22 thomas s->repo, &pid->id);
5826 945f9229 2022-04-16 thomas if (err)
5828 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5829 945f9229 2022-04-16 thomas pcommit, s->path);
5830 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
5831 e5a0f69f 2018-08-18 stsp if (err) {
5832 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5833 1e37a5c2 2019-05-12 jcs err = NULL;
5834 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5837 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5839 1e37a5c2 2019-05-12 jcs free(blob_id);
5840 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5841 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5842 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5845 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5846 ec242592 2022-04-22 thomas &pid->id);
5847 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5849 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5850 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
5852 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5857 1e37a5c2 2019-05-12 jcs s->done = 1;
5858 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5859 1e37a5c2 2019-05-12 jcs s->done = 0;
5860 1e37a5c2 2019-05-12 jcs if (thread_err)
5862 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5863 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5864 a5388363 2020-12-01 naddy err = run_blame(view);
5869 1c5e5faa 2022-06-23 thomas case 'C': {
5870 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5872 07b0611c 2022-06-23 thomas view->count = 0;
5873 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5874 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
5876 1e37a5c2 2019-05-12 jcs s->done = 1;
5877 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5878 1e37a5c2 2019-05-12 jcs s->done = 0;
5879 1e37a5c2 2019-05-12 jcs if (thread_err)
5881 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5882 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5883 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5884 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5885 a5388363 2020-12-01 naddy err = run_blame(view);
5890 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5891 1e37a5c2 2019-05-12 jcs case '\r': {
5892 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5893 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5894 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5896 07b0611c 2022-06-23 thomas view->count = 0;
5897 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5898 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5899 1e37a5c2 2019-05-12 jcs if (id == NULL)
5901 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5904 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5905 4fc71f3b 2022-07-12 thomas if (*new_view) {
5906 4fc71f3b 2022-07-12 thomas /* traversed from diff view, release diff resources */
5907 4fc71f3b 2022-07-12 thomas err = close_diff_view(*new_view);
5908 4fc71f3b 2022-07-12 thomas if (err)
5910 4fc71f3b 2022-07-12 thomas diff_view = *new_view;
5911 4fc71f3b 2022-07-12 thomas } else {
5912 4fc71f3b 2022-07-12 thomas if (view_is_parent_view(view))
5913 4fc71f3b 2022-07-12 thomas view_get_split(view, &begin_y, &begin_x);
5915 4fc71f3b 2022-07-12 thomas diff_view = view_open(0, 0, begin_y, begin_x,
5916 4fc71f3b 2022-07-12 thomas TOG_VIEW_DIFF);
5917 4fc71f3b 2022-07-12 thomas if (diff_view == NULL) {
5918 4fc71f3b 2022-07-12 thomas got_object_commit_close(commit);
5919 4fc71f3b 2022-07-12 thomas err = got_error_from_errno("view_open");
5923 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5924 4fc71f3b 2022-07-12 thomas id, NULL, NULL, 3, 0, 0, view, s->repo);
5925 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5927 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5930 4fc71f3b 2022-07-12 thomas s->last_diffed_line = s->first_displayed_line - 1 +
5931 4fc71f3b 2022-07-12 thomas s->selected_line;
5932 4fc71f3b 2022-07-12 thomas if (*new_view)
5933 4fc71f3b 2022-07-12 thomas break; /* still open from active diff view */
5934 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
5935 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
5936 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
5937 a5d43cac 2022-07-01 thomas if (err)
5941 e78dc838 2020-12-04 stsp view->focussed = 0;
5942 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5943 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
5944 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
5945 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5946 53d2bdd3 2022-07-10 thomas view_transfer_size(diff_view, view);
5947 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5950 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
5951 40236d76 2022-06-23 thomas if (err)
5953 e78dc838 2020-12-04 stsp view->focus_child = 1;
5955 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5960 70f17a53 2022-06-13 thomas case CTRL('d'):
5961 23427b14 2022-06-23 thomas case 'd':
5962 70f17a53 2022-06-13 thomas nscroll /= 2;
5963 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5964 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5965 ea025d1d 2020-02-22 naddy case CTRL('f'):
5966 1c5e5faa 2022-06-23 thomas case 'f':
5968 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5969 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5970 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5971 07b0611c 2022-06-23 thomas view->count = 0;
5974 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5975 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5976 70f17a53 2022-06-13 thomas s->selected_line +=
5977 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
5978 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
5980 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
5981 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
5983 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5984 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
5986 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5987 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5988 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5989 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5993 07b0611c 2022-06-23 thomas view->count = 0;
5996 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
5999 adf4c9e0 2022-07-03 thomas static const struct got_error *
6000 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
6002 adf4c9e0 2022-07-03 thomas const struct got_error *err;
6003 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
6005 adf4c9e0 2022-07-03 thomas view->count = 0;
6006 adf4c9e0 2022-07-03 thomas s->done = 1;
6007 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
6008 adf4c9e0 2022-07-03 thomas s->done = 0;
6009 adf4c9e0 2022-07-03 thomas if (err)
6010 adf4c9e0 2022-07-03 thomas return err;
6011 adf4c9e0 2022-07-03 thomas return run_blame(view);
6014 a70480e0 2018-06-23 stsp static const struct got_error *
6015 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
6017 a70480e0 2018-06-23 stsp const struct got_error *error;
6018 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
6019 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
6020 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6021 0587e10c 2020-07-23 stsp char *link_target = NULL;
6022 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6023 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6024 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
6026 e1cd8fed 2018-08-01 stsp struct tog_view *view;
6027 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6029 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6030 a70480e0 2018-06-23 stsp switch (ch) {
6032 a70480e0 2018-06-23 stsp commit_id_str = optarg;
6035 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
6036 69069811 2018-08-02 stsp if (repo_path == NULL)
6037 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6041 17020d27 2019-03-07 stsp usage_blame();
6042 a70480e0 2018-06-23 stsp /* NOTREACHED */
6046 a70480e0 2018-06-23 stsp argc -= optind;
6047 a70480e0 2018-06-23 stsp argv += optind;
6049 f135c941 2020-02-20 stsp if (argc != 1)
6050 a70480e0 2018-06-23 stsp usage_blame();
6052 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6053 7cd52833 2022-06-23 thomas if (error != NULL)
6054 7cd52833 2022-06-23 thomas goto done;
6056 69069811 2018-08-02 stsp if (repo_path == NULL) {
6057 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6058 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6059 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6060 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6061 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6062 c156c7a4 2020-12-18 stsp goto done;
6063 f135c941 2020-02-20 stsp if (worktree)
6064 eb41ed75 2019-02-05 stsp repo_path =
6065 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6067 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
6068 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6069 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6070 c156c7a4 2020-12-18 stsp goto done;
6074 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6075 c02c541e 2019-03-29 stsp if (error != NULL)
6076 8e94dd5b 2019-01-04 stsp goto done;
6078 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
6079 f135c941 2020-02-20 stsp worktree);
6080 c02c541e 2019-03-29 stsp if (error)
6081 92205607 2019-01-04 stsp goto done;
6083 f135c941 2020-02-20 stsp init_curses();
6085 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6086 51a10b52 2020-12-26 stsp if (error)
6087 51a10b52 2020-12-26 stsp goto done;
6089 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6090 eb41ed75 2019-02-05 stsp if (error)
6091 69069811 2018-08-02 stsp goto done;
6093 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
6094 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
6095 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
6096 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
6097 a70480e0 2018-06-23 stsp if (error != NULL)
6098 66b4983c 2018-06-23 stsp goto done;
6099 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6100 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
6102 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6103 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6105 a19e88aa 2018-06-23 stsp if (error != NULL)
6106 8b473291 2019-02-21 stsp goto done;
6108 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
6109 e1cd8fed 2018-08-01 stsp if (view == NULL) {
6110 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6111 e1cd8fed 2018-08-01 stsp goto done;
6114 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6115 945f9229 2022-04-16 thomas if (error)
6116 945f9229 2022-04-16 thomas goto done;
6118 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
6119 945f9229 2022-04-16 thomas commit, repo);
6120 7cbe629d 2018-08-04 stsp if (error)
6121 7cbe629d 2018-08-04 stsp goto done;
6123 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
6124 78756c87 2020-11-24 stsp commit_id, repo);
6125 0587e10c 2020-07-23 stsp if (error)
6126 0587e10c 2020-07-23 stsp goto done;
6127 12314ad4 2019-08-31 stsp if (worktree) {
6128 12314ad4 2019-08-31 stsp /* Release work tree lock. */
6129 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
6130 12314ad4 2019-08-31 stsp worktree = NULL;
6132 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6134 69069811 2018-08-02 stsp free(repo_path);
6135 f135c941 2020-02-20 stsp free(in_repo_path);
6136 0587e10c 2020-07-23 stsp free(link_target);
6137 69069811 2018-08-02 stsp free(cwd);
6138 a70480e0 2018-06-23 stsp free(commit_id);
6139 945f9229 2022-04-16 thomas if (commit)
6140 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6141 eb41ed75 2019-02-05 stsp if (worktree)
6142 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
6143 1d0f4054 2021-06-17 stsp if (repo) {
6144 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6145 1d0f4054 2021-06-17 stsp if (error == NULL)
6146 1d0f4054 2021-06-17 stsp error = close_err;
6148 7cd52833 2022-06-23 thomas if (pack_fds) {
6149 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6150 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6151 7cd52833 2022-06-23 thomas if (error == NULL)
6152 7cd52833 2022-06-23 thomas error = pack_err;
6154 51a10b52 2020-12-26 stsp tog_free_refs();
6155 a70480e0 2018-06-23 stsp return error;
6158 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6159 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
6161 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
6162 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6163 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
6164 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
6165 f26dddb7 2019-11-08 stsp struct tog_color *tc;
6166 56e0773d 2019-11-28 stsp int width, n, i, nentries;
6167 d86d3b18 2020-12-01 naddy int limit = view->nlines;
6169 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
6170 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
6171 a5d43cac 2022-07-01 thomas --limit; /* border */
6173 f7d12f7e 2018-08-01 stsp werase(view->window);
6175 ffd1d5e5 2018-06-23 stsp if (limit == 0)
6176 ffd1d5e5 2018-06-23 stsp return NULL;
6178 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
6181 ffd1d5e5 2018-06-23 stsp return err;
6182 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6183 a3404814 2018-09-02 stsp wstandout(view->window);
6184 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
6186 11b20872 2019-11-08 stsp wattr_on(view->window,
6187 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6188 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6190 11b20872 2019-11-08 stsp wattr_off(view->window,
6191 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6192 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
6193 a3404814 2018-09-02 stsp wstandend(view->window);
6194 2550e4c3 2018-07-13 stsp free(wline);
6195 2550e4c3 2018-07-13 stsp wline = NULL;
6196 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6197 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6198 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6199 ffd1d5e5 2018-06-23 stsp return NULL;
6200 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
6203 ce52c690 2018-06-23 stsp return err;
6204 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6205 2550e4c3 2018-07-13 stsp free(wline);
6206 2550e4c3 2018-07-13 stsp wline = NULL;
6207 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6208 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6209 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6210 ffd1d5e5 2018-06-23 stsp return NULL;
6211 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6212 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
6213 a1eca9bb 2018-06-23 stsp return NULL;
6215 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
6216 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
6217 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
6218 0cf4efb1 2018-09-29 stsp if (view->focussed)
6219 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6220 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
6222 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
6223 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
6224 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6225 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6226 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6227 ffd1d5e5 2018-06-23 stsp return NULL;
6231 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
6234 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
6235 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
6236 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
6237 848d6979 2019-08-12 stsp const char *modestr = "";
6238 56e0773d 2019-11-28 stsp mode_t mode;
6240 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
6241 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
6243 d86d3b18 2020-12-01 naddy if (s->show_ids) {
6244 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
6245 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
6247 638f9024 2019-05-13 stsp return got_error_from_errno(
6248 230a42bd 2019-05-11 jcs "got_object_id_str");
6250 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
6251 63c5ca5d 2019-08-24 stsp modestr = "$";
6252 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
6255 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
6256 d86d3b18 2020-12-01 naddy te, s->repo);
6257 0d6c6ee3 2020-05-20 stsp if (err) {
6258 0d6c6ee3 2020-05-20 stsp free(id_str);
6259 0d6c6ee3 2020-05-20 stsp return err;
6261 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
6262 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
6263 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
6265 848d6979 2019-08-12 stsp modestr = "@";
6267 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
6268 848d6979 2019-08-12 stsp modestr = "/";
6269 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
6270 848d6979 2019-08-12 stsp modestr = "*";
6271 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
6272 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
6273 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
6274 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
6275 1d13200f 2018-07-12 stsp free(id_str);
6276 0d6c6ee3 2020-05-20 stsp free(link_target);
6277 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6279 1d13200f 2018-07-12 stsp free(id_str);
6280 0d6c6ee3 2020-05-20 stsp free(link_target);
6281 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6283 ffd1d5e5 2018-06-23 stsp if (err) {
6284 ffd1d5e5 2018-06-23 stsp free(line);
6287 d86d3b18 2020-12-01 naddy if (n == s->selected) {
6288 0cf4efb1 2018-09-29 stsp if (view->focussed)
6289 0cf4efb1 2018-09-29 stsp wstandout(view->window);
6290 d86d3b18 2020-12-01 naddy s->selected_entry = te;
6292 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
6294 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
6295 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6296 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6298 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6299 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6300 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6301 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6302 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6303 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6304 ffd1d5e5 2018-06-23 stsp free(line);
6305 2550e4c3 2018-07-13 stsp free(wline);
6306 2550e4c3 2018-07-13 stsp wline = NULL;
6308 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6309 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6310 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6314 ffd1d5e5 2018-06-23 stsp return err;
6317 ffd1d5e5 2018-06-23 stsp static void
6318 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6320 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6321 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6322 fa86c4bf 2020-11-29 stsp int i = 0;
6324 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6327 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6328 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6329 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6330 fa86c4bf 2020-11-29 stsp if (!isroot)
6331 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6334 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6335 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6339 a5d43cac 2022-07-01 thomas static const struct got_error *
6340 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6342 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6343 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6344 ffd1d5e5 2018-06-23 stsp int n = 0;
6346 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6347 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6348 694d3271 2020-12-01 naddy s->first_displayed_entry);
6350 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6352 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6353 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6354 a5d43cac 2022-07-01 thomas if (last)
6355 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6356 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6357 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6358 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6362 a5d43cac 2022-07-01 thomas return NULL;
6365 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6366 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6367 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6369 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6370 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6371 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6373 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6374 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6375 56e0773d 2019-11-28 stsp + 1 /* slash */;
6377 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6379 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6380 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6381 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6383 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6384 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6385 d9765a41 2018-06-23 stsp while (pt) {
6386 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6387 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6388 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6389 cb2ebc8a 2018-06-23 stsp goto done;
6391 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6392 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6393 cb2ebc8a 2018-06-23 stsp goto done;
6395 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6398 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6399 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6400 ce52c690 2018-06-23 stsp goto done;
6404 ce52c690 2018-06-23 stsp if (err) {
6405 ce52c690 2018-06-23 stsp free(*path);
6406 ce52c690 2018-06-23 stsp *path = NULL;
6408 ce52c690 2018-06-23 stsp return err;
6411 ce52c690 2018-06-23 stsp static const struct got_error *
6412 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6413 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6414 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6416 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6417 ce52c690 2018-06-23 stsp char *path;
6418 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6420 a0de39f3 2019-08-09 stsp *new_view = NULL;
6422 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6424 ce52c690 2018-06-23 stsp return err;
6426 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6427 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6428 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6429 83ce39e3 2019-08-12 stsp goto done;
6432 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6433 e5a0f69f 2018-08-18 stsp if (err) {
6434 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6435 fc06ba56 2019-08-22 stsp err = NULL;
6436 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6438 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6440 83ce39e3 2019-08-12 stsp free(path);
6441 69efd4c4 2018-07-18 stsp return err;
6444 69efd4c4 2018-07-18 stsp static const struct got_error *
6445 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6446 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6448 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6449 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6450 69efd4c4 2018-07-18 stsp char *path;
6452 a0de39f3 2019-08-09 stsp *new_view = NULL;
6454 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6455 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6456 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6458 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6460 69efd4c4 2018-07-18 stsp return err;
6462 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6465 e5a0f69f 2018-08-18 stsp view_close(log_view);
6467 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6468 cb2ebc8a 2018-06-23 stsp free(path);
6469 cb2ebc8a 2018-06-23 stsp return err;
6472 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6473 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6474 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6476 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6477 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6478 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6479 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6481 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6482 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6484 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6485 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6486 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6488 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6490 bc573f3b 2021-07-10 stsp goto done;
6493 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6494 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6495 bc573f3b 2021-07-10 stsp * closed on demand.
6497 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6498 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6500 bc573f3b 2021-07-10 stsp goto done;
6501 bc573f3b 2021-07-10 stsp s->tree = s->root;
6503 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6504 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6505 ffd1d5e5 2018-06-23 stsp goto done;
6507 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6508 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6509 ffd1d5e5 2018-06-23 stsp goto done;
6512 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6513 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6514 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6515 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6516 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6517 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6518 9cd7cbd1 2020-12-07 stsp goto done;
6521 fb2756b9 2018-08-04 stsp s->repo = repo;
6523 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6524 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6525 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6526 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6528 c0b01bdb 2019-11-08 stsp goto done;
6529 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6530 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6532 c0b01bdb 2019-11-08 stsp goto done;
6533 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6534 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6535 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6537 c0b01bdb 2019-11-08 stsp goto done;
6539 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6540 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6541 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6543 11b20872 2019-11-08 stsp goto done;
6545 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6546 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6548 c0b01bdb 2019-11-08 stsp goto done;
6551 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6552 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6553 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6554 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6555 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6557 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6558 bc573f3b 2021-07-10 stsp if (commit)
6559 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6561 bc573f3b 2021-07-10 stsp close_tree_view(view);
6562 ad80ab7b 2018-08-04 stsp return err;
6565 e5a0f69f 2018-08-18 stsp static const struct got_error *
6566 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6568 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6570 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6571 fb2756b9 2018-08-04 stsp free(s->tree_label);
6572 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6573 6484ec90 2018-09-29 stsp free(s->commit_id);
6574 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6575 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6576 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6577 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6578 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6579 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6580 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6581 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6582 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6583 ad80ab7b 2018-08-04 stsp free(parent);
6586 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6587 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6588 bc573f3b 2021-07-10 stsp if (s->root)
6589 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6590 7c32bd05 2019-06-22 stsp return NULL;
6593 7c32bd05 2019-06-22 stsp static const struct got_error *
6594 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6596 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6598 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6599 7c32bd05 2019-06-22 stsp return NULL;
6602 7c32bd05 2019-06-22 stsp static int
6603 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6605 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6607 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6611 7c32bd05 2019-06-22 stsp static const struct got_error *
6612 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6614 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6615 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6617 7c32bd05 2019-06-22 stsp if (!view->searching) {
6618 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6619 7c32bd05 2019-06-22 stsp return NULL;
6622 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6623 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6624 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6625 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6626 56e0773d 2019-11-28 stsp s->selected_entry);
6628 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6630 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6631 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6633 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6634 56e0773d 2019-11-28 stsp s->selected_entry);
6637 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6638 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
6639 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6640 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6642 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6645 7c32bd05 2019-06-22 stsp while (1) {
6646 56e0773d 2019-11-28 stsp if (te == NULL) {
6647 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6648 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6649 ac66afb8 2019-06-24 stsp return NULL;
6651 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6652 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6654 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6657 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6658 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6659 56e0773d 2019-11-28 stsp s->matched_entry = te;
6663 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6664 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6666 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6669 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6670 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6671 7c32bd05 2019-06-22 stsp s->selected = 0;
6674 e5a0f69f 2018-08-18 stsp return NULL;
6677 ad80ab7b 2018-08-04 stsp static const struct got_error *
6678 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6680 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6681 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6682 e5a0f69f 2018-08-18 stsp char *parent_path;
6684 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6686 e5a0f69f 2018-08-18 stsp return err;
6688 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6689 e5a0f69f 2018-08-18 stsp free(parent_path);
6691 a5d43cac 2022-07-01 thomas view_border(view);
6692 e5a0f69f 2018-08-18 stsp return err;
6695 e5a0f69f 2018-08-18 stsp static const struct got_error *
6696 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6698 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6699 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6700 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6701 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6702 444d5325 2022-07-03 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6704 e5a0f69f 2018-08-18 stsp switch (ch) {
6706 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6707 07b0611c 2022-06-23 thomas view->count = 0;
6710 07b0611c 2022-06-23 thomas view->count = 0;
6711 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6713 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6714 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6715 444d5325 2022-07-03 thomas err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6716 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6717 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6718 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6719 444d5325 2022-07-03 thomas if (err)
6722 e78dc838 2020-12-04 stsp view->focussed = 0;
6723 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6724 444d5325 2022-07-03 thomas log_view->mode = view->mode;
6725 444d5325 2022-07-03 thomas log_view->nlines = view->lines - begin_y;
6726 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6727 53d2bdd3 2022-07-10 thomas view_transfer_size(log_view, view);
6728 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6730 1e37a5c2 2019-05-12 jcs return err;
6731 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
6732 40236d76 2022-06-23 thomas if (err)
6733 40236d76 2022-06-23 thomas return err;
6734 e78dc838 2020-12-04 stsp view->focus_child = 1;
6736 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6739 07b0611c 2022-06-23 thomas view->count = 0;
6740 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6741 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6742 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6743 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6744 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6745 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6746 152c1c93 2020-11-29 stsp if (err) {
6747 152c1c93 2020-11-29 stsp view_close(ref_view);
6748 152c1c93 2020-11-29 stsp return err;
6750 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6751 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6752 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6753 444d5325 2022-07-03 thomas if (err)
6756 e78dc838 2020-12-04 stsp view->focussed = 0;
6757 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6758 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
6759 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
6760 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6761 53d2bdd3 2022-07-10 thomas view_transfer_size(ref_view, view);
6762 152c1c93 2020-11-29 stsp err = view_close_child(view);
6764 152c1c93 2020-11-29 stsp return err;
6765 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
6766 40236d76 2022-06-23 thomas if (err)
6767 40236d76 2022-06-23 thomas return err;
6768 e78dc838 2020-12-04 stsp view->focus_child = 1;
6770 152c1c93 2020-11-29 stsp *new_view = ref_view;
6772 e4526bf5 2021-09-03 naddy case 'g':
6773 e4526bf5 2021-09-03 naddy case KEY_HOME:
6774 e4526bf5 2021-09-03 naddy s->selected = 0;
6775 07b0611c 2022-06-23 thomas view->count = 0;
6776 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6777 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6778 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6780 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6782 e4526bf5 2021-09-03 naddy case 'G':
6783 a5d43cac 2022-07-01 thomas case KEY_END: {
6784 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
6786 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
6787 a5d43cac 2022-07-01 thomas --eos; /* border */
6788 e4526bf5 2021-09-03 naddy s->selected = 0;
6789 07b0611c 2022-06-23 thomas view->count = 0;
6790 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6791 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
6792 e4526bf5 2021-09-03 naddy if (te == NULL) {
6793 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
6794 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6799 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6800 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6802 e4526bf5 2021-09-03 naddy if (n > 0)
6803 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6807 1e37a5c2 2019-05-12 jcs case KEY_UP:
6808 f7140bf5 2021-10-17 thomas case CTRL('p'):
6809 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6810 1e37a5c2 2019-05-12 jcs s->selected--;
6813 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6814 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6815 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6816 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6817 07b0611c 2022-06-23 thomas view->count = 0;
6819 70f17a53 2022-06-13 thomas case CTRL('u'):
6820 23427b14 2022-06-23 thomas case 'u':
6821 70f17a53 2022-06-13 thomas nscroll /= 2;
6822 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6823 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6824 ea025d1d 2020-02-22 naddy case CTRL('b'):
6825 1c5e5faa 2022-06-23 thomas case 'b':
6826 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6827 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6828 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6829 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6831 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6832 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6834 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
6835 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6836 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6837 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6838 07b0611c 2022-06-23 thomas view->count = 0;
6841 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6842 f7140bf5 2021-10-17 thomas case CTRL('n'):
6843 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6844 1e37a5c2 2019-05-12 jcs s->selected++;
6847 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6848 07b0611c 2022-06-23 thomas == NULL) {
6849 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6850 07b0611c 2022-06-23 thomas view->count = 0;
6853 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
6855 70f17a53 2022-06-13 thomas case CTRL('d'):
6856 23427b14 2022-06-23 thomas case 'd':
6857 70f17a53 2022-06-13 thomas nscroll /= 2;
6858 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6859 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6860 ea025d1d 2020-02-22 naddy case CTRL('f'):
6861 1c5e5faa 2022-06-23 thomas case 'f':
6862 4c2d69cb 2022-06-23 thomas case ' ':
6863 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6865 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6866 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6867 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
6868 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
6870 07b0611c 2022-06-23 thomas view->count = 0;
6873 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
6875 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6877 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6878 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6879 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6880 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6881 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
6882 07b0611c 2022-06-23 thomas view->count = 0;
6885 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6886 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6888 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6889 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6890 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6891 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6892 1e37a5c2 2019-05-12 jcs s->selected_entry =
6893 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6894 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6895 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
6896 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
6897 a5d43cac 2022-07-01 thomas if (err)
6900 1e37a5c2 2019-05-12 jcs free(parent);
6901 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6902 56e0773d 2019-11-28 stsp s->selected_entry))) {
6903 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6904 07b0611c 2022-06-23 thomas view->count = 0;
6905 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6906 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6909 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6910 941e9f74 2019-05-21 stsp if (err) {
6911 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6914 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
6915 56e0773d 2019-11-28 stsp s->selected_entry))) {
6916 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
6917 a5d43cac 2022-07-01 thomas int begin_x = 0, begin_y = 0;
6919 a5d43cac 2022-07-01 thomas if (view_is_parent_view(view))
6920 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
6922 a5d43cac 2022-07-01 thomas err = blame_tree_entry(&blame_view, begin_y, begin_x,
6923 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
6924 78756c87 2020-11-24 stsp s->commit_id, s->repo);
6928 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6929 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6930 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6931 a5d43cac 2022-07-01 thomas if (err)
6935 07b0611c 2022-06-23 thomas view->count = 0;
6936 e78dc838 2020-12-04 stsp view->focussed = 0;
6937 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
6938 a5d43cac 2022-07-01 thomas blame_view->mode = view->mode;
6939 a5d43cac 2022-07-01 thomas blame_view->nlines = view->lines - begin_y;
6940 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
6941 53d2bdd3 2022-07-10 thomas view_transfer_size(blame_view, view);
6942 669b5ffa 2018-10-07 stsp err = view_close_child(view);
6944 669b5ffa 2018-10-07 stsp return err;
6945 40236d76 2022-06-23 thomas err = view_set_child(view, blame_view);
6946 40236d76 2022-06-23 thomas if (err)
6947 40236d76 2022-06-23 thomas return err;
6948 e78dc838 2020-12-04 stsp view->focus_child = 1;
6950 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
6953 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6954 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6955 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6956 07b0611c 2022-06-23 thomas view->count = 0;
6959 07b0611c 2022-06-23 thomas view->count = 0;
6963 ffd1d5e5 2018-06-23 stsp return err;
6966 ffd1d5e5 2018-06-23 stsp __dead static void
6967 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6970 91198554 2022-06-23 thomas fprintf(stderr,
6971 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6972 ffd1d5e5 2018-06-23 stsp getprogname());
6976 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6977 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6979 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6980 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6981 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6982 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6983 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6984 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6985 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6986 4e97c21c 2020-12-06 stsp char *label = NULL;
6987 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6988 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6990 5221c383 2018-08-01 stsp struct tog_view *view;
6991 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6993 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6994 ffd1d5e5 2018-06-23 stsp switch (ch) {
6996 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6999 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
7000 74283ab8 2020-02-07 stsp if (repo_path == NULL)
7001 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
7005 e99e2d15 2020-11-24 naddy usage_tree();
7006 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
7010 ffd1d5e5 2018-06-23 stsp argc -= optind;
7011 ffd1d5e5 2018-06-23 stsp argv += optind;
7013 55cccc34 2020-02-20 stsp if (argc > 1)
7014 e99e2d15 2020-11-24 naddy usage_tree();
7016 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7017 7cd52833 2022-06-23 thomas if (error != NULL)
7018 7cd52833 2022-06-23 thomas goto done;
7020 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
7021 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7022 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7023 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7024 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7025 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7026 c156c7a4 2020-12-18 stsp goto done;
7027 55cccc34 2020-02-20 stsp if (worktree)
7028 52185f70 2019-02-05 stsp repo_path =
7029 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
7031 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
7032 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7033 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7034 c156c7a4 2020-12-18 stsp goto done;
7038 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7039 c02c541e 2019-03-29 stsp if (error != NULL)
7040 52185f70 2019-02-05 stsp goto done;
7042 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7043 55cccc34 2020-02-20 stsp repo, worktree);
7044 55cccc34 2020-02-20 stsp if (error)
7045 55cccc34 2020-02-20 stsp goto done;
7047 55cccc34 2020-02-20 stsp init_curses();
7049 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7050 c02c541e 2019-03-29 stsp if (error)
7051 52185f70 2019-02-05 stsp goto done;
7053 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7054 51a10b52 2020-12-26 stsp if (error)
7055 51a10b52 2020-12-26 stsp goto done;
7057 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
7058 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
7059 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
7060 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7061 4e97c21c 2020-12-06 stsp if (error)
7062 4e97c21c 2020-12-06 stsp goto done;
7063 4e97c21c 2020-12-06 stsp head_ref_name = label;
7065 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
7066 4e97c21c 2020-12-06 stsp if (error == NULL)
7067 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
7068 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
7069 4e97c21c 2020-12-06 stsp goto done;
7070 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
7071 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7072 4e97c21c 2020-12-06 stsp if (error)
7073 4e97c21c 2020-12-06 stsp goto done;
7076 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7077 945f9229 2022-04-16 thomas if (error)
7078 945f9229 2022-04-16 thomas goto done;
7080 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
7081 5221c383 2018-08-01 stsp if (view == NULL) {
7082 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
7083 5221c383 2018-08-01 stsp goto done;
7085 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
7086 ad80ab7b 2018-08-04 stsp if (error)
7087 ad80ab7b 2018-08-04 stsp goto done;
7088 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
7089 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
7090 d91faf3b 2020-12-01 naddy in_repo_path);
7091 55cccc34 2020-02-20 stsp if (error)
7092 55cccc34 2020-02-20 stsp goto done;
7095 55cccc34 2020-02-20 stsp if (worktree) {
7096 55cccc34 2020-02-20 stsp /* Release work tree lock. */
7097 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
7098 55cccc34 2020-02-20 stsp worktree = NULL;
7100 e5a0f69f 2018-08-18 stsp error = view_loop(view);
7102 52185f70 2019-02-05 stsp free(repo_path);
7103 e4a0e26d 2020-02-20 stsp free(cwd);
7104 ffd1d5e5 2018-06-23 stsp free(commit_id);
7105 4e97c21c 2020-12-06 stsp free(label);
7107 486cd271 2020-12-06 stsp got_ref_close(ref);
7108 1d0f4054 2021-06-17 stsp if (repo) {
7109 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7110 1d0f4054 2021-06-17 stsp if (error == NULL)
7111 1d0f4054 2021-06-17 stsp error = close_err;
7113 7cd52833 2022-06-23 thomas if (pack_fds) {
7114 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7115 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7116 7cd52833 2022-06-23 thomas if (error == NULL)
7117 7cd52833 2022-06-23 thomas error = pack_err;
7119 51a10b52 2020-12-26 stsp tog_free_refs();
7120 ffd1d5e5 2018-06-23 stsp return error;
7123 6458efa5 2020-11-24 stsp static const struct got_error *
7124 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
7126 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
7127 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7129 6458efa5 2020-11-24 stsp s->nrefs = 0;
7130 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
7131 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
7132 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
7133 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
7134 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
7137 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
7138 6458efa5 2020-11-24 stsp if (re == NULL)
7139 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
7141 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
7142 8924d611 2020-12-26 stsp if (re->ref == NULL)
7143 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
7144 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
7145 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
7148 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7149 6458efa5 2020-11-24 stsp return NULL;
7152 ef20f542 2022-06-26 thomas static void
7153 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
7155 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7157 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
7158 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7159 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
7160 8924d611 2020-12-26 stsp got_ref_close(re->ref);
7165 6458efa5 2020-11-24 stsp static const struct got_error *
7166 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
7168 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7169 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7171 6458efa5 2020-11-24 stsp s->selected_entry = 0;
7172 6458efa5 2020-11-24 stsp s->repo = repo;
7174 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
7175 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
7177 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7179 6458efa5 2020-11-24 stsp return err;
7181 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
7182 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
7183 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
7184 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
7186 6458efa5 2020-11-24 stsp goto done;
7188 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
7189 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
7190 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
7192 6458efa5 2020-11-24 stsp goto done;
7194 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
7195 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
7196 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
7197 2183bbf6 2022-01-23 thomas if (err)
7198 2183bbf6 2022-01-23 thomas goto done;
7200 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
7201 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
7202 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
7204 6458efa5 2020-11-24 stsp goto done;
7207 6458efa5 2020-11-24 stsp view->show = show_ref_view;
7208 6458efa5 2020-11-24 stsp view->input = input_ref_view;
7209 6458efa5 2020-11-24 stsp view->close = close_ref_view;
7210 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
7211 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
7214 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7215 6458efa5 2020-11-24 stsp return err;
7218 6458efa5 2020-11-24 stsp static const struct got_error *
7219 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
7221 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7223 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7224 6458efa5 2020-11-24 stsp free_colors(&s->colors);
7226 6458efa5 2020-11-24 stsp return NULL;
7229 6458efa5 2020-11-24 stsp static const struct got_error *
7230 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
7231 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7233 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7234 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
7235 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
7236 6458efa5 2020-11-24 stsp int obj_type;
7238 c42c9805 2020-11-24 stsp *commit_id = NULL;
7240 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
7242 6458efa5 2020-11-24 stsp return err;
7244 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
7246 6458efa5 2020-11-24 stsp goto done;
7248 6458efa5 2020-11-24 stsp switch (obj_type) {
7249 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
7250 c42c9805 2020-11-24 stsp *commit_id = obj_id;
7252 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
7253 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
7255 6458efa5 2020-11-24 stsp goto done;
7256 c42c9805 2020-11-24 stsp free(obj_id);
7257 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
7258 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7260 6458efa5 2020-11-24 stsp goto done;
7261 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
7262 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7263 c42c9805 2020-11-24 stsp goto done;
7265 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
7266 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
7267 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
7268 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
7269 c42c9805 2020-11-24 stsp goto done;
7273 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
7279 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
7280 c42c9805 2020-11-24 stsp if (err) {
7281 c42c9805 2020-11-24 stsp free(*commit_id);
7282 c42c9805 2020-11-24 stsp *commit_id = NULL;
7284 c42c9805 2020-11-24 stsp return err;
7287 c42c9805 2020-11-24 stsp static const struct got_error *
7288 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
7289 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7291 c42c9805 2020-11-24 stsp struct tog_view *log_view;
7292 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7293 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
7295 c42c9805 2020-11-24 stsp *new_view = NULL;
7297 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7298 c42c9805 2020-11-24 stsp if (err) {
7299 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7300 c42c9805 2020-11-24 stsp return err;
7302 c42c9805 2020-11-24 stsp return NULL;
7305 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7306 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7307 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7308 6458efa5 2020-11-24 stsp goto done;
7311 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7312 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7315 6458efa5 2020-11-24 stsp view_close(log_view);
7317 6458efa5 2020-11-24 stsp *new_view = log_view;
7318 c42c9805 2020-11-24 stsp free(commit_id);
7319 6458efa5 2020-11-24 stsp return err;
7322 ce5b7c56 2019-07-09 stsp static void
7323 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7325 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7326 34ba6917 2020-11-30 stsp int i = 0;
7328 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7331 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7332 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7333 34ba6917 2020-11-30 stsp if (re == NULL)
7335 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7336 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7340 a5d43cac 2022-07-01 thomas static const struct got_error *
7341 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7343 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7344 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7345 6458efa5 2020-11-24 stsp int n = 0;
7347 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7348 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7350 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7352 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7353 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7354 a5d43cac 2022-07-01 thomas if (last)
7355 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7356 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7357 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7358 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7362 a5d43cac 2022-07-01 thomas return NULL;
7365 6458efa5 2020-11-24 stsp static const struct got_error *
7366 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7368 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7370 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7371 6458efa5 2020-11-24 stsp return NULL;
7374 6458efa5 2020-11-24 stsp static int
7375 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7377 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7379 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7383 6458efa5 2020-11-24 stsp static const struct got_error *
7384 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7386 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7387 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7389 6458efa5 2020-11-24 stsp if (!view->searching) {
7390 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7391 6458efa5 2020-11-24 stsp return NULL;
7394 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7395 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7396 6458efa5 2020-11-24 stsp if (s->selected_entry)
7397 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7399 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7400 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7402 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7403 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7405 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7406 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7409 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7410 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7411 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7412 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7414 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7417 6458efa5 2020-11-24 stsp while (1) {
7418 6458efa5 2020-11-24 stsp if (re == NULL) {
7419 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7420 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7421 6458efa5 2020-11-24 stsp return NULL;
7423 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7424 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7426 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7429 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7430 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7431 6458efa5 2020-11-24 stsp s->matched_entry = re;
7435 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7436 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7438 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7441 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7442 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7443 6458efa5 2020-11-24 stsp s->selected = 0;
7446 6458efa5 2020-11-24 stsp return NULL;
7449 6458efa5 2020-11-24 stsp static const struct got_error *
7450 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7452 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7453 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7454 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7455 6458efa5 2020-11-24 stsp char *line = NULL;
7456 6458efa5 2020-11-24 stsp wchar_t *wline;
7457 6458efa5 2020-11-24 stsp struct tog_color *tc;
7458 6458efa5 2020-11-24 stsp int width, n;
7459 6458efa5 2020-11-24 stsp int limit = view->nlines;
7461 6458efa5 2020-11-24 stsp werase(view->window);
7463 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7464 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7465 a5d43cac 2022-07-01 thomas --limit; /* border */
7467 6458efa5 2020-11-24 stsp if (limit == 0)
7468 6458efa5 2020-11-24 stsp return NULL;
7470 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7472 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7473 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7474 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7476 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7477 6458efa5 2020-11-24 stsp if (err) {
7478 6458efa5 2020-11-24 stsp free(line);
7479 6458efa5 2020-11-24 stsp return err;
7481 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7482 6458efa5 2020-11-24 stsp wstandout(view->window);
7483 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7484 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7485 6458efa5 2020-11-24 stsp wstandend(view->window);
7486 6458efa5 2020-11-24 stsp free(wline);
7487 6458efa5 2020-11-24 stsp wline = NULL;
7488 6458efa5 2020-11-24 stsp free(line);
7489 6458efa5 2020-11-24 stsp line = NULL;
7490 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7491 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7492 6458efa5 2020-11-24 stsp if (--limit <= 0)
7493 6458efa5 2020-11-24 stsp return NULL;
7496 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7497 6458efa5 2020-11-24 stsp char *line = NULL;
7498 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7500 84227eb1 2022-06-23 thomas if (s->show_date) {
7501 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7502 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7503 84227eb1 2022-06-23 thomas struct got_object_id *id;
7504 84227eb1 2022-06-23 thomas struct tm tm;
7505 84227eb1 2022-06-23 thomas time_t t;
7507 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7508 84227eb1 2022-06-23 thomas if (err)
7509 84227eb1 2022-06-23 thomas return err;
7510 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7511 84227eb1 2022-06-23 thomas if (err) {
7512 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7513 84227eb1 2022-06-23 thomas free(id);
7514 84227eb1 2022-06-23 thomas return err;
7516 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7518 84227eb1 2022-06-23 thomas if (err) {
7519 84227eb1 2022-06-23 thomas free(id);
7520 84227eb1 2022-06-23 thomas return err;
7522 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7523 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7524 84227eb1 2022-06-23 thomas } else {
7525 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7526 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7528 84227eb1 2022-06-23 thomas free(id);
7529 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7530 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7531 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7532 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7534 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7535 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7536 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7537 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7538 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7539 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7540 6458efa5 2020-11-24 stsp struct got_object_id *id;
7541 6458efa5 2020-11-24 stsp char *id_str;
7542 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7544 6458efa5 2020-11-24 stsp return err;
7545 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7546 6458efa5 2020-11-24 stsp if (err) {
7548 6458efa5 2020-11-24 stsp return err;
7550 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7551 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7552 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7554 6458efa5 2020-11-24 stsp free(id_str);
7555 6458efa5 2020-11-24 stsp return err;
7558 6458efa5 2020-11-24 stsp free(id_str);
7559 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7560 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7561 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7563 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7565 6458efa5 2020-11-24 stsp if (err) {
7566 6458efa5 2020-11-24 stsp free(line);
7567 6458efa5 2020-11-24 stsp return err;
7569 6458efa5 2020-11-24 stsp if (n == s->selected) {
7570 6458efa5 2020-11-24 stsp if (view->focussed)
7571 6458efa5 2020-11-24 stsp wstandout(view->window);
7572 6458efa5 2020-11-24 stsp s->selected_entry = re;
7574 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7576 6458efa5 2020-11-24 stsp wattr_on(view->window,
7577 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7578 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7580 6458efa5 2020-11-24 stsp wattr_off(view->window,
7581 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7582 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7583 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7584 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7585 6458efa5 2020-11-24 stsp wstandend(view->window);
7586 6458efa5 2020-11-24 stsp free(line);
7587 6458efa5 2020-11-24 stsp free(wline);
7588 6458efa5 2020-11-24 stsp wline = NULL;
7590 6458efa5 2020-11-24 stsp s->ndisplayed++;
7591 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7594 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7597 a5d43cac 2022-07-01 thomas view_border(view);
7598 6458efa5 2020-11-24 stsp return err;
7601 6458efa5 2020-11-24 stsp static const struct got_error *
7602 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7603 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7605 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7606 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7607 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7609 c42c9805 2020-11-24 stsp *new_view = NULL;
7611 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7612 c42c9805 2020-11-24 stsp if (err) {
7613 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7614 c42c9805 2020-11-24 stsp return err;
7616 c42c9805 2020-11-24 stsp return NULL;
7620 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7621 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7622 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7623 c42c9805 2020-11-24 stsp goto done;
7626 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7627 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7629 c42c9805 2020-11-24 stsp goto done;
7631 c42c9805 2020-11-24 stsp *new_view = tree_view;
7633 c42c9805 2020-11-24 stsp free(commit_id);
7634 c42c9805 2020-11-24 stsp return err;
7636 c42c9805 2020-11-24 stsp static const struct got_error *
7637 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7639 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7640 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7641 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7642 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7643 a5d43cac 2022-07-01 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7645 6458efa5 2020-11-24 stsp switch (ch) {
7647 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7648 07b0611c 2022-06-23 thomas view->count = 0;
7650 84227eb1 2022-06-23 thomas case 'm':
7651 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
7652 07b0611c 2022-06-23 thomas view->count = 0;
7654 98182bd0 2021-11-20 thomas case 'o':
7655 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
7656 07b0611c 2022-06-23 thomas view->count = 0;
7657 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7658 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
7659 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
7660 c591440f 2021-11-20 thomas if (err)
7662 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
7663 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
7664 c591440f 2021-11-20 thomas &tog_refs, s->repo);
7665 3bfadbd4 2021-11-20 thomas if (err)
7667 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
7668 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
7670 6458efa5 2020-11-24 stsp case KEY_ENTER:
7671 6458efa5 2020-11-24 stsp case '\r':
7672 07b0611c 2022-06-23 thomas view->count = 0;
7673 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7675 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7676 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
7678 a5d43cac 2022-07-01 thomas err = log_ref_entry(&log_view, begin_y, begin_x,
7679 a5d43cac 2022-07-01 thomas s->selected_entry, s->repo);
7680 a5d43cac 2022-07-01 thomas if (err)
7683 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7684 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7685 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7686 a5d43cac 2022-07-01 thomas if (err)
7690 e78dc838 2020-12-04 stsp view->focussed = 0;
7691 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7692 a5d43cac 2022-07-01 thomas log_view->mode = view->mode;
7693 a5d43cac 2022-07-01 thomas log_view->nlines = view->lines - begin_y;
7694 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7695 53d2bdd3 2022-07-10 thomas view_transfer_size(log_view, view);
7696 6458efa5 2020-11-24 stsp err = view_close_child(view);
7698 6458efa5 2020-11-24 stsp return err;
7699 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
7700 40236d76 2022-06-23 thomas if (err)
7701 40236d76 2022-06-23 thomas return err;
7702 e78dc838 2020-12-04 stsp view->focus_child = 1;
7704 6458efa5 2020-11-24 stsp *new_view = log_view;
7707 07b0611c 2022-06-23 thomas view->count = 0;
7708 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7710 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7711 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
7712 444d5325 2022-07-03 thomas err = browse_ref_tree(&tree_view, begin_y, begin_x,
7713 444d5325 2022-07-03 thomas s->selected_entry, s->repo);
7714 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7716 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7717 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7718 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
7719 444d5325 2022-07-03 thomas if (err)
7722 e78dc838 2020-12-04 stsp view->focussed = 0;
7723 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7724 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
7725 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
7726 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7727 53d2bdd3 2022-07-10 thomas view_transfer_size(tree_view, view);
7728 c42c9805 2020-11-24 stsp err = view_close_child(view);
7730 c42c9805 2020-11-24 stsp return err;
7731 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
7732 40236d76 2022-06-23 thomas if (err)
7733 40236d76 2022-06-23 thomas return err;
7734 e78dc838 2020-12-04 stsp view->focus_child = 1;
7736 c42c9805 2020-11-24 stsp *new_view = tree_view;
7738 e4526bf5 2021-09-03 naddy case 'g':
7739 e4526bf5 2021-09-03 naddy case KEY_HOME:
7740 e4526bf5 2021-09-03 naddy s->selected = 0;
7741 07b0611c 2022-06-23 thomas view->count = 0;
7742 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7744 e4526bf5 2021-09-03 naddy case 'G':
7745 a5d43cac 2022-07-01 thomas case KEY_END: {
7746 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
7748 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7749 a5d43cac 2022-07-01 thomas --eos; /* border */
7750 e4526bf5 2021-09-03 naddy s->selected = 0;
7751 07b0611c 2022-06-23 thomas view->count = 0;
7752 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7753 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7754 e4526bf5 2021-09-03 naddy if (re == NULL)
7756 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7757 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7759 e4526bf5 2021-09-03 naddy if (n > 0)
7760 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7764 6458efa5 2020-11-24 stsp case KEY_UP:
7765 f7140bf5 2021-10-17 thomas case CTRL('p'):
7766 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7767 6458efa5 2020-11-24 stsp s->selected--;
7770 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7771 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7772 07b0611c 2022-06-23 thomas view->count = 0;
7774 70f17a53 2022-06-13 thomas case CTRL('u'):
7775 23427b14 2022-06-23 thomas case 'u':
7776 70f17a53 2022-06-13 thomas nscroll /= 2;
7777 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7778 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7779 6458efa5 2020-11-24 stsp case CTRL('b'):
7780 1c5e5faa 2022-06-23 thomas case 'b':
7781 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7782 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
7783 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
7784 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7785 07b0611c 2022-06-23 thomas view->count = 0;
7788 6458efa5 2020-11-24 stsp case KEY_DOWN:
7789 f7140bf5 2021-10-17 thomas case CTRL('n'):
7790 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7791 6458efa5 2020-11-24 stsp s->selected++;
7794 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7795 6458efa5 2020-11-24 stsp /* can't scroll any further */
7796 07b0611c 2022-06-23 thomas view->count = 0;
7799 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
7801 70f17a53 2022-06-13 thomas case CTRL('d'):
7802 23427b14 2022-06-23 thomas case 'd':
7803 70f17a53 2022-06-13 thomas nscroll /= 2;
7804 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7805 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7806 6458efa5 2020-11-24 stsp case CTRL('f'):
7807 1c5e5faa 2022-06-23 thomas case 'f':
7808 4c2d69cb 2022-06-23 thomas case ' ':
7809 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7810 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7811 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7812 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7813 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7814 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
7815 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
7816 07b0611c 2022-06-23 thomas view->count = 0;
7819 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
7821 6458efa5 2020-11-24 stsp case CTRL('l'):
7822 07b0611c 2022-06-23 thomas view->count = 0;
7823 8924d611 2020-12-26 stsp tog_free_refs();
7824 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
7827 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7828 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7830 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7831 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7832 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7835 07b0611c 2022-06-23 thomas view->count = 0;
7839 6458efa5 2020-11-24 stsp return err;
7842 6458efa5 2020-11-24 stsp __dead static void
7843 6458efa5 2020-11-24 stsp usage_ref(void)
7846 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7847 6458efa5 2020-11-24 stsp getprogname());
7851 6458efa5 2020-11-24 stsp static const struct got_error *
7852 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7854 6458efa5 2020-11-24 stsp const struct got_error *error;
7855 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7856 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7857 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7859 6458efa5 2020-11-24 stsp struct tog_view *view;
7860 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7862 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7863 6458efa5 2020-11-24 stsp switch (ch) {
7865 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7866 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7867 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7871 e99e2d15 2020-11-24 naddy usage_ref();
7872 6458efa5 2020-11-24 stsp /* NOTREACHED */
7876 6458efa5 2020-11-24 stsp argc -= optind;
7877 6458efa5 2020-11-24 stsp argv += optind;
7879 6458efa5 2020-11-24 stsp if (argc > 1)
7880 e99e2d15 2020-11-24 naddy usage_ref();
7882 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7883 7cd52833 2022-06-23 thomas if (error != NULL)
7884 7cd52833 2022-06-23 thomas goto done;
7886 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7887 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7888 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7889 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7890 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7891 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7892 c156c7a4 2020-12-18 stsp goto done;
7893 6458efa5 2020-11-24 stsp if (worktree)
7894 6458efa5 2020-11-24 stsp repo_path =
7895 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7897 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7898 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7899 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7900 c156c7a4 2020-12-18 stsp goto done;
7904 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7905 6458efa5 2020-11-24 stsp if (error != NULL)
7906 6458efa5 2020-11-24 stsp goto done;
7908 6458efa5 2020-11-24 stsp init_curses();
7910 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7911 51a10b52 2020-12-26 stsp if (error)
7912 51a10b52 2020-12-26 stsp goto done;
7914 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7915 6458efa5 2020-11-24 stsp if (error)
7916 6458efa5 2020-11-24 stsp goto done;
7918 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7919 6458efa5 2020-11-24 stsp if (view == NULL) {
7920 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
7921 6458efa5 2020-11-24 stsp goto done;
7924 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
7925 6458efa5 2020-11-24 stsp if (error)
7926 6458efa5 2020-11-24 stsp goto done;
7928 6458efa5 2020-11-24 stsp if (worktree) {
7929 6458efa5 2020-11-24 stsp /* Release work tree lock. */
7930 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
7931 6458efa5 2020-11-24 stsp worktree = NULL;
7933 6458efa5 2020-11-24 stsp error = view_loop(view);
7935 6458efa5 2020-11-24 stsp free(repo_path);
7936 6458efa5 2020-11-24 stsp free(cwd);
7937 1d0f4054 2021-06-17 stsp if (repo) {
7938 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7939 1d0f4054 2021-06-17 stsp if (close_err)
7940 1d0f4054 2021-06-17 stsp error = close_err;
7942 7cd52833 2022-06-23 thomas if (pack_fds) {
7943 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7944 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7945 7cd52833 2022-06-23 thomas if (error == NULL)
7946 7cd52833 2022-06-23 thomas error = pack_err;
7948 51a10b52 2020-12-26 stsp tog_free_refs();
7949 6458efa5 2020-11-24 stsp return error;
7953 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
7954 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
7956 6458efa5 2020-11-24 stsp static void
7957 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
7959 a5d43cac 2022-07-01 thomas switch (view->type) {
7960 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
7961 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
7962 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
7963 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
7967 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
7968 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
7970 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
7971 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
7974 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
7975 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
7976 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
7978 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
7979 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
7980 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
7982 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
7983 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
7984 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
7986 a5d43cac 2022-07-01 thomas default:
7990 a5d43cac 2022-07-01 thomas view->offset = 0;
7994 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
7995 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
7997 a5d43cac 2022-07-01 thomas static const struct got_error *
7998 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
8000 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
8001 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
8002 a5d43cac 2022-07-01 thomas int *selected = NULL;
8003 a5d43cac 2022-07-01 thomas int header, offset;
8005 a5d43cac 2022-07-01 thomas switch (view->type) {
8006 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
8007 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
8008 a5d43cac 2022-07-01 thomas header = 3;
8009 a5d43cac 2022-07-01 thomas scrolld = NULL;
8010 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
8011 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
8012 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
8013 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
8014 a5d43cac 2022-07-01 thomas view->offset = offset;
8018 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
8019 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
8020 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
8021 64486692 2022-07-07 thomas header = view_is_parent_view(view) ? 3 : 2;
8022 a5d43cac 2022-07-01 thomas selected = &s->selected;
8025 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
8026 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
8027 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
8028 a5d43cac 2022-07-01 thomas header = 3;
8029 a5d43cac 2022-07-01 thomas selected = &s->selected;
8032 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
8033 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
8034 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
8035 a5d43cac 2022-07-01 thomas header = 5;
8036 a5d43cac 2022-07-01 thomas selected = &s->selected;
8039 a5d43cac 2022-07-01 thomas default:
8040 a5d43cac 2022-07-01 thomas selected = NULL;
8041 a5d43cac 2022-07-01 thomas scrolld = NULL;
8042 a5d43cac 2022-07-01 thomas header = 0;
8046 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
8047 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
8048 a5d43cac 2022-07-01 thomas view->offset = offset;
8049 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
8050 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
8051 a5d43cac 2022-07-01 thomas *selected -= offset;
8055 a5d43cac 2022-07-01 thomas return err;
8058 a5d43cac 2022-07-01 thomas static void
8059 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
8063 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
8064 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
8065 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
8066 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
8068 6879ba42 2020-10-01 naddy fputc('\n', fp);
8071 4ed7e80c 2018-05-20 stsp __dead static void
8072 6879ba42 2020-10-01 naddy usage(int hflag, int status)
8074 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
8076 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
8077 6879ba42 2020-10-01 naddy getprogname());
8078 6879ba42 2020-10-01 naddy if (hflag) {
8079 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
8080 6879ba42 2020-10-01 naddy list_commands(fp);
8082 6879ba42 2020-10-01 naddy exit(status);
8085 c2301be8 2018-04-30 stsp static char **
8086 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
8088 ee85c5e8 2020-02-29 stsp va_list ap;
8089 c2301be8 2018-04-30 stsp char **argv;
8092 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
8094 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
8095 c2301be8 2018-04-30 stsp if (argv == NULL)
8096 c2301be8 2018-04-30 stsp err(1, "calloc");
8097 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
8098 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
8099 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
8100 e10c916e 2019-09-15 hiltjo err(1, "strdup");
8103 ee85c5e8 2020-02-29 stsp va_end(ap);
8104 c2301be8 2018-04-30 stsp return argv;
8108 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
8109 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
8110 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
8111 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
8112 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
8114 ee85c5e8 2020-02-29 stsp static const struct got_error *
8115 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
8117 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
8118 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8119 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
8120 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
8121 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
8122 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
8123 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
8124 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
8125 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
8127 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
8128 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
8129 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
8131 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
8132 7cd52833 2022-06-23 thomas if (error != NULL)
8133 7cd52833 2022-06-23 thomas goto done;
8135 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
8136 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8137 ee85c5e8 2020-02-29 stsp goto done;
8139 ee85c5e8 2020-02-29 stsp if (worktree)
8140 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
8142 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
8143 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
8144 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
8145 ee85c5e8 2020-02-29 stsp goto done;
8148 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8149 ee85c5e8 2020-02-29 stsp if (error != NULL)
8150 ee85c5e8 2020-02-29 stsp goto done;
8152 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
8153 ee85c5e8 2020-02-29 stsp repo, worktree);
8154 ee85c5e8 2020-02-29 stsp if (error)
8155 ee85c5e8 2020-02-29 stsp goto done;
8157 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
8158 84de9106 2020-12-26 stsp if (error)
8159 84de9106 2020-12-26 stsp goto done;
8160 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
8161 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
8162 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
8163 ee85c5e8 2020-02-29 stsp if (error)
8164 ee85c5e8 2020-02-29 stsp goto done;
8166 ee85c5e8 2020-02-29 stsp if (worktree) {
8167 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8168 ee85c5e8 2020-02-29 stsp worktree = NULL;
8171 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
8172 945f9229 2022-04-16 thomas if (error)
8173 945f9229 2022-04-16 thomas goto done;
8175 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
8176 ee85c5e8 2020-02-29 stsp if (error) {
8177 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
8178 ee85c5e8 2020-02-29 stsp goto done;
8179 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
8180 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
8181 6879ba42 2020-10-01 naddy usage(1, 1);
8182 ee85c5e8 2020-02-29 stsp /* not reached */
8185 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8186 1d0f4054 2021-06-17 stsp if (error == NULL)
8187 1d0f4054 2021-06-17 stsp error = close_err;
8188 ee85c5e8 2020-02-29 stsp repo = NULL;
8190 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
8191 ee85c5e8 2020-02-29 stsp if (error)
8192 ee85c5e8 2020-02-29 stsp goto done;
8194 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
8196 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
8197 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
8199 1d0f4054 2021-06-17 stsp if (repo) {
8200 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
8201 1d0f4054 2021-06-17 stsp if (error == NULL)
8202 1d0f4054 2021-06-17 stsp error = close_err;
8204 945f9229 2022-04-16 thomas if (commit)
8205 945f9229 2022-04-16 thomas got_object_commit_close(commit);
8206 ee85c5e8 2020-02-29 stsp if (worktree)
8207 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
8208 7cd52833 2022-06-23 thomas if (pack_fds) {
8209 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
8210 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
8211 7cd52833 2022-06-23 thomas if (error == NULL)
8212 7cd52833 2022-06-23 thomas error = pack_err;
8215 ee85c5e8 2020-02-29 stsp free(commit_id_str);
8216 ee85c5e8 2020-02-29 stsp free(commit_id);
8217 ee85c5e8 2020-02-29 stsp free(cwd);
8218 ee85c5e8 2020-02-29 stsp free(repo_path);
8219 ee85c5e8 2020-02-29 stsp free(in_repo_path);
8220 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
8222 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
8223 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
8224 ee85c5e8 2020-02-29 stsp free(cmd_argv);
8226 87670572 2020-12-26 naddy tog_free_refs();
8227 ee85c5e8 2020-02-29 stsp return error;
8231 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
8233 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
8234 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
8235 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
8236 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
8237 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
8238 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
8239 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
8241 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
8243 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
8245 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
8246 9f7d7167 2018-04-29 stsp switch (ch) {
8248 9f7d7167 2018-04-29 stsp hflag = 1;
8251 53ccebc2 2019-07-30 stsp Vflag = 1;
8254 6879ba42 2020-10-01 naddy usage(hflag, 1);
8255 9f7d7167 2018-04-29 stsp /* NOTREACHED */
8259 9f7d7167 2018-04-29 stsp argc -= optind;
8260 9f7d7167 2018-04-29 stsp argv += optind;
8261 9814e6a3 2020-09-27 naddy optind = 1;
8262 c2301be8 2018-04-30 stsp optreset = 1;
8264 53ccebc2 2019-07-30 stsp if (Vflag) {
8265 53ccebc2 2019-07-30 stsp got_version_print_str();
8266 6879ba42 2020-10-01 naddy return 0;
8269 4010e238 2020-12-04 stsp #ifndef PROFILE
8270 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
8271 4010e238 2020-12-04 stsp NULL) == -1)
8272 4010e238 2020-12-04 stsp err(1, "pledge");
8275 c2301be8 2018-04-30 stsp if (argc == 0) {
8276 f29d3e89 2018-06-23 stsp if (hflag)
8277 6879ba42 2020-10-01 naddy usage(hflag, 0);
8278 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
8279 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
8281 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
8285 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
8286 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
8287 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
8288 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
8289 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
8295 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
8296 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
8297 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
8298 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
8299 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
8300 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8303 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8304 ee85c5e8 2020-02-29 stsp if (argc != 1)
8305 6879ba42 2020-10-01 naddy usage(0, 1);
8306 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8307 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8309 ee85c5e8 2020-02-29 stsp if (hflag)
8310 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8312 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8316 b46c1e04 2020-09-20 naddy putchar('\n');
8317 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8319 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8320 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8321 a2f4a359 2020-02-28 stsp free(cmd_argv);
8324 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8325 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);