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"
61 dd038bc6 2021-09-21 thomas.ad //#define update_panels() (0)
62 dd038bc6 2021-09-21 thomas.ad //#define doupdate() (0)
65 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
69 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
72 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
73 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
76 9f7d7167 2018-04-29 stsp #ifndef nitems
77 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
80 9f7d7167 2018-04-29 stsp struct tog_cmd {
81 c2301be8 2018-04-30 stsp const char *name;
82 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
83 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
86 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
88 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
89 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
90 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
91 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
93 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
94 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
95 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
96 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
97 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
99 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
100 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
101 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
102 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
103 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
104 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
107 d6b05b5b 2018-08-04 stsp enum tog_view_type {
108 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
109 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
110 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
111 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
112 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
115 a5d43cac 2022-07-01 thomas enum tog_view_mode {
116 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_NONE,
117 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_VERT,
118 a5d43cac 2022-07-01 thomas TOG_VIEW_SPLIT_HRZN
121 a5d43cac 2022-07-01 thomas #define HSPLIT_SCALE 0.3 /* default horizontal split scale */
123 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
125 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
126 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
127 ba4f502b 2018-08-04 stsp struct got_object_id *id;
128 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
131 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
132 ba4f502b 2018-08-04 stsp struct commit_queue {
133 ba4f502b 2018-08-04 stsp int ncommits;
134 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
137 f26dddb7 2019-11-08 stsp struct tog_color {
138 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
139 6d17833f 2019-11-08 stsp regex_t regex;
140 6d17833f 2019-11-08 stsp short colorpair;
142 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
144 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
145 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
146 adf4c9e0 2022-07-03 thomas static enum got_diff_algorithm tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
148 2183bbf6 2022-01-23 thomas static const struct got_error *
149 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
150 2183bbf6 2022-01-23 thomas struct got_reference* re2)
152 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
153 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
154 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
156 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
157 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
158 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
159 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
160 2183bbf6 2022-01-23 thomas *cmp = -1;
161 2183bbf6 2022-01-23 thomas return NULL;
162 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
163 2183bbf6 2022-01-23 thomas *cmp = 1;
164 2183bbf6 2022-01-23 thomas return NULL;
167 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
168 2183bbf6 2022-01-23 thomas return NULL;
171 11b20872 2019-11-08 stsp static const struct got_error *
172 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
174 51a10b52 2020-12-26 stsp const struct got_error *err;
176 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
177 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
180 51a10b52 2020-12-26 stsp return err;
182 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
186 51a10b52 2020-12-26 stsp static void
187 51a10b52 2020-12-26 stsp tog_free_refs(void)
189 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
190 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
191 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
193 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
196 51a10b52 2020-12-26 stsp static const struct got_error *
197 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
198 11b20872 2019-11-08 stsp int idx, short color)
200 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
201 11b20872 2019-11-08 stsp struct tog_color *tc;
202 11b20872 2019-11-08 stsp int regerr = 0;
204 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
205 11b20872 2019-11-08 stsp return NULL;
207 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
209 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
210 11b20872 2019-11-08 stsp if (tc == NULL)
211 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
212 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
213 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
214 11b20872 2019-11-08 stsp if (regerr) {
215 11b20872 2019-11-08 stsp static char regerr_msg[512];
216 11b20872 2019-11-08 stsp static char err_msg[512];
217 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
218 11b20872 2019-11-08 stsp sizeof(regerr_msg));
219 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
220 11b20872 2019-11-08 stsp regerr_msg);
221 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
223 11b20872 2019-11-08 stsp return err;
225 11b20872 2019-11-08 stsp tc->colorpair = idx;
226 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
227 11b20872 2019-11-08 stsp return NULL;
230 11b20872 2019-11-08 stsp static void
231 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
233 11b20872 2019-11-08 stsp struct tog_color *tc;
235 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
236 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
237 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
238 11b20872 2019-11-08 stsp regfree(&tc->regex);
243 ef20f542 2022-06-26 thomas static struct tog_color *
244 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
246 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
248 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
249 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
253 11b20872 2019-11-08 stsp return NULL;
257 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
259 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
260 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
261 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
262 11b20872 2019-11-08 stsp return COLOR_CYAN;
263 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
264 11b20872 2019-11-08 stsp return COLOR_YELLOW;
265 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
266 11b20872 2019-11-08 stsp return COLOR_GREEN;
267 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
268 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
269 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
270 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
271 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
272 91b8c405 2020-01-25 stsp return COLOR_CYAN;
273 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
274 11b20872 2019-11-08 stsp return COLOR_GREEN;
275 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
276 11b20872 2019-11-08 stsp return COLOR_GREEN;
277 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
278 11b20872 2019-11-08 stsp return COLOR_CYAN;
279 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
280 11b20872 2019-11-08 stsp return COLOR_YELLOW;
281 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
282 6458efa5 2020-11-24 stsp return COLOR_GREEN;
283 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
284 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
285 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
286 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
287 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
288 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
294 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
296 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
298 11b20872 2019-11-08 stsp if (val == NULL)
299 11b20872 2019-11-08 stsp return default_color_value(envvar);
301 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
302 11b20872 2019-11-08 stsp return COLOR_BLACK;
303 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
304 11b20872 2019-11-08 stsp return COLOR_RED;
305 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
306 11b20872 2019-11-08 stsp return COLOR_GREEN;
307 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
308 11b20872 2019-11-08 stsp return COLOR_YELLOW;
309 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
310 11b20872 2019-11-08 stsp return COLOR_BLUE;
311 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
312 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
313 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
314 11b20872 2019-11-08 stsp return COLOR_CYAN;
315 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
316 11b20872 2019-11-08 stsp return COLOR_WHITE;
317 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
320 11b20872 2019-11-08 stsp return default_color_value(envvar);
324 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
325 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
326 3dbaef42 2020-11-24 stsp const char *label1, *label2;
327 a0f32f33 2022-06-13 thomas FILE *f, *f1, *f2;
328 19a6a6b5 2022-07-01 thomas int fd1, fd2;
329 15a087fe 2019-02-21 stsp int first_displayed_line;
330 15a087fe 2019-02-21 stsp int last_displayed_line;
332 15a087fe 2019-02-21 stsp int diff_context;
333 3dbaef42 2020-11-24 stsp int ignore_whitespace;
334 64453f7e 2020-11-21 stsp int force_text_diff;
335 15a087fe 2019-02-21 stsp struct got_repository *repo;
336 bddb1296 2019-11-08 stsp struct tog_colors colors;
337 fe621944 2020-11-10 stsp size_t nlines;
338 f44b1f58 2020-02-02 tracey off_t *line_offsets;
339 f44b1f58 2020-02-02 tracey int matched_line;
340 f44b1f58 2020-02-02 tracey int selected_line;
342 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
343 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
346 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
348 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
349 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
350 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
351 1a76625f 2018-10-22 stsp int commits_needed;
352 fb280deb 2021-08-30 stsp int load_all;
353 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
354 1a76625f 2018-10-22 stsp struct commit_queue *commits;
355 1a76625f 2018-10-22 stsp const char *in_repo_path;
356 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
357 1a76625f 2018-10-22 stsp struct got_repository *repo;
358 1af5eddf 2022-06-23 thomas int *pack_fds;
359 1a76625f 2018-10-22 stsp int log_complete;
360 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
361 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
362 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
363 13add988 2019-10-15 stsp int *searching;
364 13add988 2019-10-15 stsp int *search_next_done;
365 13add988 2019-10-15 stsp regex_t *regex;
368 1a76625f 2018-10-22 stsp struct tog_log_view_state {
369 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
370 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
371 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
372 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
373 b01e7d3b 2018-08-04 stsp int selected;
374 b01e7d3b 2018-08-04 stsp char *in_repo_path;
375 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
376 b672a97a 2020-01-27 stsp int log_branches;
377 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
378 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
379 1a76625f 2018-10-22 stsp sig_atomic_t quit;
380 1a76625f 2018-10-22 stsp pthread_t thread;
381 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
382 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
383 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
384 11b20872 2019-11-08 stsp struct tog_colors colors;
387 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
388 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
389 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
390 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
391 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
392 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
393 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
394 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
395 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
396 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
397 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
398 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
399 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
400 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
401 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
403 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
404 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
405 e9424729 2018-08-04 stsp int nlines;
407 e9424729 2018-08-04 stsp struct tog_view *view;
408 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
412 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
413 e9424729 2018-08-04 stsp const char *path;
414 e9424729 2018-08-04 stsp struct got_repository *repo;
415 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
416 e9424729 2018-08-04 stsp int *complete;
417 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
418 fc06ba56 2019-08-22 stsp void *cancel_arg;
421 e9424729 2018-08-04 stsp struct tog_blame {
423 be659d10 2020-11-18 stsp off_t filesize;
424 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
425 6fcac457 2018-11-19 stsp int nlines;
426 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
427 e9424729 2018-08-04 stsp pthread_t thread;
428 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
429 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
430 e9424729 2018-08-04 stsp const char *path;
431 7cd52833 2022-06-23 thomas int *pack_fds;
434 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
435 7cbe629d 2018-08-04 stsp int first_displayed_line;
436 7cbe629d 2018-08-04 stsp int last_displayed_line;
437 7cbe629d 2018-08-04 stsp int selected_line;
438 7cbe629d 2018-08-04 stsp int blame_complete;
441 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
442 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
443 e5a0f69f 2018-08-18 stsp char *path;
444 7cbe629d 2018-08-04 stsp struct got_repository *repo;
445 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
446 e9424729 2018-08-04 stsp struct tog_blame blame;
447 6c4c42e0 2019-06-24 stsp int matched_line;
448 11b20872 2019-11-08 stsp struct tog_colors colors;
451 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
452 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
453 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
454 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
455 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
456 ad80ab7b 2018-08-04 stsp int selected;
459 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
461 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
462 ad80ab7b 2018-08-04 stsp char *tree_label;
463 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
464 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
465 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
466 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
467 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
468 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
469 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
470 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
471 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
472 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
473 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
474 6458efa5 2020-11-24 stsp struct tog_colors colors;
477 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
478 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
479 6458efa5 2020-11-24 stsp struct got_reference *ref;
483 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
485 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
486 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
487 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
488 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
489 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
490 84227eb1 2022-06-23 thomas int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
491 6458efa5 2020-11-24 stsp struct got_repository *repo;
492 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
493 bddb1296 2019-11-08 stsp struct tog_colors colors;
497 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
499 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
500 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
501 669b5ffa 2018-10-07 stsp * there is enough screen estate.
503 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
504 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
506 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
507 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
508 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
510 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
511 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
513 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
515 cc3c9aac 2018-08-01 stsp struct tog_view {
516 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
517 26ed57b2 2018-05-19 stsp WINDOW *window;
518 26ed57b2 2018-05-19 stsp PANEL *panel;
519 a5d43cac 2022-07-01 thomas int nlines, ncols, begin_y, begin_x; /* based on split height/width */
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 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
526 669b5ffa 2018-10-07 stsp struct tog_view *parent;
527 669b5ffa 2018-10-07 stsp struct tog_view *child;
530 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
531 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
532 e78dc838 2020-12-04 stsp * between parent and child.
533 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
534 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
535 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
536 e78dc838 2020-12-04 stsp * situations.
538 e78dc838 2020-12-04 stsp int focus_child;
540 a5d43cac 2022-07-01 thomas enum tog_view_mode mode;
541 5dc9f4bc 2018-08-04 stsp /* type-specific state */
542 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
544 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
545 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
546 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
547 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
548 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
551 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
552 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
553 e78dc838 2020-12-04 stsp struct tog_view *, int);
554 adf4c9e0 2022-07-03 thomas const struct got_error *(*reset)(struct tog_view *);
555 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
557 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
558 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
559 c0c4acc8 2021-01-24 stsp int search_started;
560 60493ae3 2019-06-20 stsp int searching;
561 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
562 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
563 60493ae3 2019-06-20 stsp int search_next_done;
564 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
565 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
566 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
567 1803e47f 2019-06-22 stsp regex_t regex;
568 41605754 2020-11-12 stsp regmatch_t regmatch;
571 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
572 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
573 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
574 78756c87 2020-11-24 stsp struct got_repository *);
575 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
576 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
577 e78dc838 2020-12-04 stsp struct tog_view *, int);
578 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_diff_view(struct tog_view *);
579 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
580 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
581 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
583 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
584 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
585 78756c87 2020-11-24 stsp const char *, const char *, int);
586 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
587 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
588 e78dc838 2020-12-04 stsp struct tog_view *, int);
589 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
590 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
591 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
593 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
594 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
595 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
596 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
597 e78dc838 2020-12-04 stsp struct tog_view *, int);
598 adf4c9e0 2022-07-03 thomas static const struct got_error *reset_blame_view(struct tog_view *);
599 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
600 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
601 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
603 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
604 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
605 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
606 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
607 e78dc838 2020-12-04 stsp struct tog_view *, int);
608 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
609 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
610 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
612 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
613 6458efa5 2020-11-24 stsp struct got_repository *);
614 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
615 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
616 e78dc838 2020-12-04 stsp struct tog_view *, int);
617 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
618 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
619 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
621 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
622 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
623 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
624 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigint_received;
625 296152d1 2022-05-31 thomas static volatile sig_atomic_t tog_sigterm_received;
627 25791caa 2018-10-24 stsp static void
628 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
630 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
633 83baff54 2019-08-12 stsp static void
634 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
636 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
639 61266923 2020-01-14 stsp static void
640 61266923 2020-01-14 stsp tog_sigcont(int signo)
642 61266923 2020-01-14 stsp tog_sigcont_received = 1;
645 296152d1 2022-05-31 thomas static void
646 296152d1 2022-05-31 thomas tog_sigint(int signo)
648 296152d1 2022-05-31 thomas tog_sigint_received = 1;
651 296152d1 2022-05-31 thomas static void
652 296152d1 2022-05-31 thomas tog_sigterm(int signo)
654 296152d1 2022-05-31 thomas tog_sigterm_received = 1;
657 296152d1 2022-05-31 thomas static int
658 c31666ae 2022-06-23 thomas tog_fatal_signal_received(void)
660 296152d1 2022-05-31 thomas return (tog_sigpipe_received ||
661 296152d1 2022-05-31 thomas tog_sigint_received || tog_sigint_received);
664 e5a0f69f 2018-08-18 stsp static const struct got_error *
665 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
667 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
669 669b5ffa 2018-10-07 stsp if (view->child) {
670 669b5ffa 2018-10-07 stsp view_close(view->child);
671 669b5ffa 2018-10-07 stsp view->child = NULL;
673 e5a0f69f 2018-08-18 stsp if (view->close)
674 e5a0f69f 2018-08-18 stsp err = view->close(view);
675 ea5e7bb5 2018-08-01 stsp if (view->panel)
676 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
677 ea5e7bb5 2018-08-01 stsp if (view->window)
678 ea5e7bb5 2018-08-01 stsp delwin(view->window);
679 ea5e7bb5 2018-08-01 stsp free(view);
680 e5a0f69f 2018-08-18 stsp return err;
683 ea5e7bb5 2018-08-01 stsp static struct tog_view *
684 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
685 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
687 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
689 ea5e7bb5 2018-08-01 stsp if (view == NULL)
690 ea5e7bb5 2018-08-01 stsp return NULL;
692 d6b05b5b 2018-08-04 stsp view->type = type;
693 f7d12f7e 2018-08-01 stsp view->lines = LINES;
694 f7d12f7e 2018-08-01 stsp view->cols = COLS;
695 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
696 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
697 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
698 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
699 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
700 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
701 96a765a8 2018-08-04 stsp view_close(view);
702 ea5e7bb5 2018-08-01 stsp return NULL;
704 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
705 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
706 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
707 96a765a8 2018-08-04 stsp view_close(view);
708 ea5e7bb5 2018-08-01 stsp return NULL;
711 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
712 ea5e7bb5 2018-08-01 stsp return view;
716 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
718 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
720 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
723 a5d43cac 2022-07-01 thomas /* XXX Stub till we decide what to do. */
724 a5d43cac 2022-07-01 thomas static int
725 a5d43cac 2022-07-01 thomas view_split_begin_y(int lines)
727 a5d43cac 2022-07-01 thomas return lines * HSPLIT_SCALE;
730 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
732 5c60c32a 2018-10-18 stsp static const struct got_error *
733 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
735 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
737 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN) {
738 a5d43cac 2022-07-01 thomas view->begin_y = view_split_begin_y(view->nlines);
739 a5d43cac 2022-07-01 thomas view->begin_x = 0;
741 a5d43cac 2022-07-01 thomas view->begin_x = view_split_begin_x(0);
742 a5d43cac 2022-07-01 thomas view->begin_y = 0;
744 a5d43cac 2022-07-01 thomas view->nlines = LINES - view->begin_y;
745 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
746 5c60c32a 2018-10-18 stsp view->lines = LINES;
747 5c60c32a 2018-10-18 stsp view->cols = COLS;
748 5c60c32a 2018-10-18 stsp err = view_resize(view);
750 5c60c32a 2018-10-18 stsp return err;
752 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN)
753 a5d43cac 2022-07-01 thomas view->parent->nlines = view->begin_y;
755 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
756 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
758 5c60c32a 2018-10-18 stsp return NULL;
761 5c60c32a 2018-10-18 stsp static const struct got_error *
762 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
764 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
766 5c60c32a 2018-10-18 stsp view->begin_x = 0;
767 5c60c32a 2018-10-18 stsp view->begin_y = 0;
768 5c60c32a 2018-10-18 stsp view->nlines = LINES;
769 5c60c32a 2018-10-18 stsp view->ncols = COLS;
770 5c60c32a 2018-10-18 stsp view->lines = LINES;
771 5c60c32a 2018-10-18 stsp view->cols = COLS;
772 5c60c32a 2018-10-18 stsp err = view_resize(view);
774 5c60c32a 2018-10-18 stsp return err;
776 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
777 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
779 5c60c32a 2018-10-18 stsp return NULL;
783 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
785 5c60c32a 2018-10-18 stsp return view->parent == NULL;
788 b65b3ea0 2022-06-23 thomas static int
789 b65b3ea0 2022-06-23 thomas view_is_splitscreen(struct tog_view *view)
791 a5d43cac 2022-07-01 thomas return view->begin_x > 0 || view->begin_y > 0;
794 e44940c3 2022-07-01 thomas static int
795 e44940c3 2022-07-01 thomas view_is_fullscreen(struct tog_view *view)
797 e44940c3 2022-07-01 thomas return view->nlines == LINES && view->ncols == COLS;
800 444d5325 2022-07-03 thomas static int
801 444d5325 2022-07-03 thomas view_is_hsplit_top(struct tog_view *view)
803 444d5325 2022-07-03 thomas return view->mode == TOG_VIEW_SPLIT_HRZN && view->child &&
804 444d5325 2022-07-03 thomas view_is_splitscreen(view->child);
807 a5d43cac 2022-07-01 thomas static void
808 a5d43cac 2022-07-01 thomas view_border(struct tog_view *view)
810 a5d43cac 2022-07-01 thomas PANEL *panel;
811 a5d43cac 2022-07-01 thomas const struct tog_view *view_above;
813 a5d43cac 2022-07-01 thomas if (view->parent)
814 a5d43cac 2022-07-01 thomas return view_border(view->parent);
816 a5d43cac 2022-07-01 thomas panel = panel_above(view->panel);
817 a5d43cac 2022-07-01 thomas if (panel == NULL)
820 a5d43cac 2022-07-01 thomas view_above = panel_userptr(panel);
821 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
822 a5d43cac 2022-07-01 thomas mvwhline(view->window, view_above->begin_y - 1,
823 a5d43cac 2022-07-01 thomas view->begin_x, got_locale_is_utf8() ?
824 a5d43cac 2022-07-01 thomas ACS_HLINE : '-', view->ncols);
826 a5d43cac 2022-07-01 thomas mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
827 a5d43cac 2022-07-01 thomas got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
830 a5d43cac 2022-07-01 thomas static const struct got_error *request_log_commits(struct tog_view *);
831 a5d43cac 2022-07-01 thomas static const struct got_error *offset_selection_down(struct tog_view *);
832 a5d43cac 2022-07-01 thomas static void offset_selection_up(struct tog_view *);
834 4d8c2215 2018-08-19 stsp static const struct got_error *
835 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
837 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
838 a5d43cac 2022-07-01 thomas int dif, nlines, ncols;
840 a5d43cac 2022-07-01 thomas dif = LINES - view->lines; /* line difference */
842 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
843 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
845 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
846 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
847 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
849 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
851 4918811f 2022-07-01 thomas if (view->child) {
852 a5d43cac 2022-07-01 thomas int hs = view->child->begin_y;
854 e44940c3 2022-07-01 thomas if (!view_is_fullscreen(view))
855 1827fdb7 2022-07-01 thomas view->child->begin_x = view_split_begin_x(view->begin_x);
856 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN ||
857 a5d43cac 2022-07-01 thomas view->child->begin_x == 0) {
858 40236d76 2022-06-23 thomas ncols = COLS;
860 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
861 5c60c32a 2018-10-18 stsp if (view->child->focussed)
862 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
864 5c60c32a 2018-10-18 stsp show_panel(view->panel);
866 40236d76 2022-06-23 thomas ncols = view->child->begin_x;
868 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
869 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
872 a5d43cac 2022-07-01 thomas * Request commits if terminal height was increased in a log
873 a5d43cac 2022-07-01 thomas * view so we have enough commits loaded to populate the view.
875 a5d43cac 2022-07-01 thomas if (view->type == TOG_VIEW_LOG && dif > 0) {
876 a5d43cac 2022-07-01 thomas struct tog_log_view_state *ts = &view->state.log;
878 a5d43cac 2022-07-01 thomas if (ts->commits.ncommits < ts->selected_entry->idx +
879 a5d43cac 2022-07-01 thomas view->lines - ts->selected) {
880 a5d43cac 2022-07-01 thomas view->nscrolled = ts->selected_entry->idx +
881 a5d43cac 2022-07-01 thomas view->lines - ts->selected -
882 a5d43cac 2022-07-01 thomas ts->commits.ncommits + dif;
883 a5d43cac 2022-07-01 thomas err = request_log_commits(view);
885 a5d43cac 2022-07-01 thomas return err;
890 a5d43cac 2022-07-01 thomas * XXX This is ugly and needs to be moved into the above
891 a5d43cac 2022-07-01 thomas * logic but "works" for now and my attempts at moving it
892 a5d43cac 2022-07-01 thomas * break either 'tab' or 'F' key maps in horizontal splits.
894 a5d43cac 2022-07-01 thomas if (hs) {
895 a5d43cac 2022-07-01 thomas err = view_splitscreen(view->child);
897 a5d43cac 2022-07-01 thomas return err;
898 a5d43cac 2022-07-01 thomas if (dif < 0) { /* top split decreased */
899 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
901 a5d43cac 2022-07-01 thomas return err;
903 a5d43cac 2022-07-01 thomas view_border(view);
904 a5d43cac 2022-07-01 thomas update_panels();
905 a5d43cac 2022-07-01 thomas doupdate();
906 a5d43cac 2022-07-01 thomas show_panel(view->child->panel);
907 a5d43cac 2022-07-01 thomas nlines = view->nlines;
909 40236d76 2022-06-23 thomas } else if (view->parent == NULL)
910 40236d76 2022-06-23 thomas ncols = COLS;
912 40236d76 2022-06-23 thomas if (wresize(view->window, nlines, ncols) == ERR)
913 40236d76 2022-06-23 thomas return got_error_from_errno("wresize");
914 40236d76 2022-06-23 thomas if (replace_panel(view->panel, view->window) == ERR)
915 40236d76 2022-06-23 thomas return got_error_from_errno("replace_panel");
916 40236d76 2022-06-23 thomas wclear(view->window);
918 40236d76 2022-06-23 thomas view->nlines = nlines;
919 40236d76 2022-06-23 thomas view->ncols = ncols;
920 40236d76 2022-06-23 thomas view->lines = LINES;
921 40236d76 2022-06-23 thomas view->cols = COLS;
923 5c60c32a 2018-10-18 stsp return NULL;
926 669b5ffa 2018-10-07 stsp static const struct got_error *
927 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
929 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
931 669b5ffa 2018-10-07 stsp if (view->child == NULL)
932 669b5ffa 2018-10-07 stsp return NULL;
934 669b5ffa 2018-10-07 stsp err = view_close(view->child);
935 669b5ffa 2018-10-07 stsp view->child = NULL;
936 669b5ffa 2018-10-07 stsp return err;
939 40236d76 2022-06-23 thomas static const struct got_error *
940 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
942 669b5ffa 2018-10-07 stsp view->child = child;
943 669b5ffa 2018-10-07 stsp child->parent = view;
945 40236d76 2022-06-23 thomas return view_resize(view);
948 34bc9ec9 2019-02-22 stsp static void
949 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
951 25791caa 2018-10-24 stsp int cols, lines;
952 25791caa 2018-10-24 stsp struct winsize size;
954 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
955 25791caa 2018-10-24 stsp cols = 80; /* Default */
956 25791caa 2018-10-24 stsp lines = 24;
958 25791caa 2018-10-24 stsp cols = size.ws_col;
959 25791caa 2018-10-24 stsp lines = size.ws_row;
961 25791caa 2018-10-24 stsp resize_term(lines, cols);
964 2b49a8ae 2019-06-22 stsp static const struct got_error *
965 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
967 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
968 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
969 2b49a8ae 2019-06-22 stsp char pattern[1024];
972 c0c4acc8 2021-01-24 stsp if (view->search_started) {
973 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
974 c0c4acc8 2021-01-24 stsp view->searching = 0;
975 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
977 c0c4acc8 2021-01-24 stsp view->search_started = 0;
979 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
980 2b49a8ae 2019-06-22 stsp return NULL;
982 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
983 a5d43cac 2022-07-01 thomas v = view->child;
985 a5d43cac 2022-07-01 thomas mvwaddstr(v->window, v->nlines - 1, 0, "/");
986 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
988 634cb454 2022-07-03 thomas nodelay(view->window, FALSE); /* block for search term input */
989 2b49a8ae 2019-06-22 stsp nocbreak();
991 a5d43cac 2022-07-01 thomas ret = wgetnstr(v->window, pattern, sizeof(pattern));
992 a5d43cac 2022-07-01 thomas wrefresh(v->window);
995 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
996 2b49a8ae 2019-06-22 stsp if (ret == ERR)
997 2b49a8ae 2019-06-22 stsp return NULL;
999 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
1000 7c32bd05 2019-06-22 stsp err = view->search_start(view);
1001 7c32bd05 2019-06-22 stsp if (err) {
1002 7c32bd05 2019-06-22 stsp regfree(&view->regex);
1003 7c32bd05 2019-06-22 stsp return err;
1005 c0c4acc8 2021-01-24 stsp view->search_started = 1;
1006 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
1007 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
1008 2b49a8ae 2019-06-22 stsp view->search_next(view);
1011 2b49a8ae 2019-06-22 stsp return NULL;
1015 fa502711 2022-07-03 thomas * Compute view->count from numeric input. Assign total to view->count and
1016 fa502711 2022-07-03 thomas * return first non-numeric key entered.
1018 07b0611c 2022-06-23 thomas static int
1019 07b0611c 2022-06-23 thomas get_compound_key(struct tog_view *view, int c)
1021 a5d43cac 2022-07-01 thomas struct tog_view *v = view;
1022 a5d43cac 2022-07-01 thomas int x, n = 0;
1024 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
1025 a5d43cac 2022-07-01 thomas v = view->child;
1026 a5d43cac 2022-07-01 thomas else if (view->mode == TOG_VIEW_SPLIT_VERT && view->parent)
1027 a5d43cac 2022-07-01 thomas v = view->parent;
1029 07b0611c 2022-06-23 thomas view->count = 0;
1030 fa502711 2022-07-03 thomas cbreak(); /* block for input */
1031 a5d43cac 2022-07-01 thomas wmove(v->window, v->nlines - 1, 0);
1032 a5d43cac 2022-07-01 thomas wclrtoeol(v->window);
1033 a5d43cac 2022-07-01 thomas waddch(v->window, ':');
1036 a5d43cac 2022-07-01 thomas x = getcurx(v->window);
1037 a5d43cac 2022-07-01 thomas if (x != ERR && x < view->ncols) {
1038 a5d43cac 2022-07-01 thomas waddch(v->window, c);
1039 a5d43cac 2022-07-01 thomas wrefresh(v->window);
1043 07b0611c 2022-06-23 thomas * Don't overflow. Max valid request should be the greatest
1044 07b0611c 2022-06-23 thomas * between the longest and total lines; cap at 10 million.
1046 07b0611c 2022-06-23 thomas if (n >= 9999999)
1047 07b0611c 2022-06-23 thomas n = 9999999;
1049 07b0611c 2022-06-23 thomas n = n * 10 + (c - '0');
1050 07b0611c 2022-06-23 thomas } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
1052 07b0611c 2022-06-23 thomas /* Massage excessive or inapplicable values at the input handler. */
1053 07b0611c 2022-06-23 thomas view->count = n;
1055 07b0611c 2022-06-23 thomas return c;
1058 0cf4efb1 2018-09-29 stsp static const struct got_error *
1059 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
1060 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
1062 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1063 669b5ffa 2018-10-07 stsp struct tog_view *v;
1064 1a76625f 2018-10-22 stsp int ch, errcode;
1066 e5a0f69f 2018-08-18 stsp *new = NULL;
1068 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
1069 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
1070 07b0611c 2022-06-23 thomas view->search_next_done == TOG_SEARCH_HAVE_NONE) {
1071 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
1072 07b0611c 2022-06-23 thomas view->count = 0;
1075 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
1076 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1077 82954512 2020-02-03 stsp if (errcode)
1078 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1079 82954512 2020-02-03 stsp "pthread_mutex_unlock");
1080 3da8ef85 2021-09-21 stsp sched_yield();
1081 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
1082 82954512 2020-02-03 stsp if (errcode)
1083 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1084 82954512 2020-02-03 stsp "pthread_mutex_lock");
1085 60493ae3 2019-06-20 stsp view->search_next(view);
1086 60493ae3 2019-06-20 stsp return NULL;
1089 634cb454 2022-07-03 thomas nodelay(view->window, FALSE);
1090 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
1091 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1092 1a76625f 2018-10-22 stsp if (errcode)
1093 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
1094 634cb454 2022-07-03 thomas /* If we have an unfinished count, let C-g or backspace abort. */
1095 634cb454 2022-07-03 thomas if (view->count && --view->count) {
1096 634cb454 2022-07-03 thomas cbreak();
1097 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1098 07b0611c 2022-06-23 thomas ch = wgetch(view->window);
1099 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
1100 634cb454 2022-07-03 thomas view->count = 0;
1102 634cb454 2022-07-03 thomas ch = view->ch;
1103 634cb454 2022-07-03 thomas } else {
1104 634cb454 2022-07-03 thomas ch = wgetch(view->window);
1105 07b0611c 2022-06-23 thomas if (ch >= '1' && ch <= '9')
1106 07b0611c 2022-06-23 thomas view->ch = ch = get_compound_key(view, ch);
1108 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1109 1a76625f 2018-10-22 stsp if (errcode)
1110 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1111 634cb454 2022-07-03 thomas nodelay(view->window, TRUE);
1113 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
1114 25791caa 2018-10-24 stsp tog_resizeterm();
1115 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
1116 61266923 2020-01-14 stsp tog_sigcont_received = 0;
1117 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
1118 25791caa 2018-10-24 stsp err = view_resize(v);
1120 25791caa 2018-10-24 stsp return err;
1121 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
1123 25791caa 2018-10-24 stsp return err;
1124 cdfcfb03 2020-12-06 stsp if (v->child) {
1125 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
1127 cdfcfb03 2020-12-06 stsp return err;
1128 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
1129 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
1131 cdfcfb03 2020-12-06 stsp return err;
1136 e5a0f69f 2018-08-18 stsp switch (ch) {
1138 07b0611c 2022-06-23 thomas view->count = 0;
1139 1e37a5c2 2019-05-12 jcs if (view->child) {
1140 e78dc838 2020-12-04 stsp view->focussed = 0;
1141 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1142 e78dc838 2020-12-04 stsp view->focus_child = 1;
1143 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
1144 e78dc838 2020-12-04 stsp view->focussed = 0;
1145 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
1146 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1147 a5d43cac 2022-07-01 thomas if (!view_is_splitscreen(view)) {
1148 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN &&
1149 a5d43cac 2022-07-01 thomas view->parent->type == TOG_VIEW_LOG) {
1150 a5d43cac 2022-07-01 thomas err = request_log_commits(view->parent);
1151 a5d43cac 2022-07-01 thomas if (err)
1152 a5d43cac 2022-07-01 thomas return err;
1154 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1155 b65b3ea0 2022-06-23 thomas err = view_fullscreen(view->parent);
1156 a5d43cac 2022-07-01 thomas if (err)
1157 a5d43cac 2022-07-01 thomas return err;
1162 a5d43cac 2022-07-01 thomas if (view->parent && view->mode == TOG_VIEW_SPLIT_HRZN) {
1163 a5d43cac 2022-07-01 thomas if (view->parent->type == TOG_VIEW_LOG) {
1164 a5d43cac 2022-07-01 thomas /* might need more commits to fill fullscreen */
1165 a5d43cac 2022-07-01 thomas err = request_log_commits(view->parent);
1166 a5d43cac 2022-07-01 thomas if (err)
1169 a5d43cac 2022-07-01 thomas offset_selection_up(view->parent);
1170 a5d43cac 2022-07-01 thomas view->parent->mode = TOG_VIEW_SPLIT_NONE;
1172 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1173 9970f7fc 2020-12-03 stsp view->dying = 1;
1178 1c5e5faa 2022-06-23 thomas case 'F':
1179 07b0611c 2022-06-23 thomas view->count = 0;
1180 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1181 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
1183 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
1184 e78dc838 2020-12-04 stsp view->focussed = 0;
1185 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1186 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
1188 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
1191 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
1192 9970f7fc 2020-12-03 stsp KEY_RESIZE);
1194 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
1195 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
1196 e78dc838 2020-12-04 stsp view->focussed = 1;
1197 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
1199 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
1200 a5d43cac 2022-07-01 thomas if (!err && view->mode != TOG_VIEW_SPLIT_HRZN)
1201 b65b3ea0 2022-06-23 thomas err = view_resize(view->parent);
1205 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
1207 a5d43cac 2022-07-01 thomas if (err)
1209 a5d43cac 2022-07-01 thomas if (view->type == TOG_VIEW_LOG) {
1210 a5d43cac 2022-07-01 thomas err = request_log_commits(view);
1211 a5d43cac 2022-07-01 thomas if (err)
1214 a5d43cac 2022-07-01 thomas if (view->parent)
1215 a5d43cac 2022-07-01 thomas err = offset_selection_down(view->parent);
1216 a5d43cac 2022-07-01 thomas if (!err)
1217 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
1219 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1222 07b0611c 2022-06-23 thomas view->count = 0;
1223 60493ae3 2019-06-20 stsp if (view->search_start)
1224 2b49a8ae 2019-06-22 stsp view_search_start(view);
1226 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1230 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
1231 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
1232 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1233 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1234 60493ae3 2019-06-20 stsp view->search_next(view);
1236 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1238 adf4c9e0 2022-07-03 thomas case 'A':
1239 adf4c9e0 2022-07-03 thomas if (tog_diff_algo == GOT_DIFF_ALGORITHM_MYERS)
1240 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
1242 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
1243 adf4c9e0 2022-07-03 thomas TAILQ_FOREACH(v, views, entry) {
1244 adf4c9e0 2022-07-03 thomas if (v->reset) {
1245 adf4c9e0 2022-07-03 thomas err = v->reset(v);
1246 adf4c9e0 2022-07-03 thomas if (err)
1247 adf4c9e0 2022-07-03 thomas return err;
1249 adf4c9e0 2022-07-03 thomas if (v->child && v->child->reset) {
1250 adf4c9e0 2022-07-03 thomas err = v->child->reset(v->child);
1251 adf4c9e0 2022-07-03 thomas if (err)
1252 adf4c9e0 2022-07-03 thomas return err;
1257 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
1261 e5a0f69f 2018-08-18 stsp return err;
1264 ef20f542 2022-06-26 thomas static int
1265 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1267 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1268 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1270 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1272 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1275 669b5ffa 2018-10-07 stsp return view->focussed;
1278 bcbd79e2 2018-08-19 stsp static const struct got_error *
1279 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1281 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1282 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1283 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1284 fd823528 2018-10-22 stsp int fast_refresh = 10;
1285 1a76625f 2018-10-22 stsp int done = 0, errcode;
1287 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1288 1a76625f 2018-10-22 stsp if (errcode)
1289 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1291 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1292 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1294 1004088d 2018-09-29 stsp view->focussed = 1;
1295 878940b7 2018-09-29 stsp err = view->show(view);
1297 0cf4efb1 2018-09-29 stsp return err;
1298 0cf4efb1 2018-09-29 stsp update_panels();
1299 0cf4efb1 2018-09-29 stsp doupdate();
1300 296152d1 2022-05-31 thomas while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1301 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1302 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1303 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1305 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1308 9970f7fc 2020-12-03 stsp if (view->dying) {
1309 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1311 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1312 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1314 e78dc838 2020-12-04 stsp else if (view->parent)
1315 669b5ffa 2018-10-07 stsp prev = view->parent;
1317 e78dc838 2020-12-04 stsp if (view->parent) {
1318 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1319 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1320 a5d43cac 2022-07-01 thomas /* Restore fullscreen line height. */
1321 a5d43cac 2022-07-01 thomas view->parent->nlines = view->parent->lines;
1322 40236d76 2022-06-23 thomas err = view_resize(view->parent);
1323 40236d76 2022-06-23 thomas if (err)
1326 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1328 9970f7fc 2020-12-03 stsp err = view_close(view);
1330 e5a0f69f 2018-08-18 stsp goto done;
1332 e78dc838 2020-12-04 stsp view = NULL;
1333 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1334 e78dc838 2020-12-04 stsp if (v->focussed)
1337 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1338 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1340 e78dc838 2020-12-04 stsp view = prev;
1341 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1342 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1343 e78dc838 2020-12-04 stsp tog_view_list_head);
1345 e78dc838 2020-12-04 stsp if (view) {
1346 e78dc838 2020-12-04 stsp if (view->focus_child) {
1347 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1348 e78dc838 2020-12-04 stsp view = view->child;
1350 e78dc838 2020-12-04 stsp view->focussed = 1;
1354 bcbd79e2 2018-08-19 stsp if (new_view) {
1355 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1356 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1357 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1358 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1360 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1361 86c66b02 2018-10-18 stsp err = view_close(v);
1363 86c66b02 2018-10-18 stsp goto done;
1366 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1367 fed7eaa8 2018-10-24 stsp view = new_view;
1369 669b5ffa 2018-10-07 stsp if (view) {
1370 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1371 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1372 e78dc838 2020-12-04 stsp view = view->child;
1374 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1375 e78dc838 2020-12-04 stsp view = view->parent;
1377 e78dc838 2020-12-04 stsp show_panel(view->panel);
1378 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1379 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1380 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1381 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1383 1a76625f 2018-10-22 stsp goto done;
1385 669b5ffa 2018-10-07 stsp err = view->show(view);
1387 1a76625f 2018-10-22 stsp goto done;
1388 669b5ffa 2018-10-07 stsp if (view->child) {
1389 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1391 1a76625f 2018-10-22 stsp goto done;
1393 1a76625f 2018-10-22 stsp update_panels();
1394 1a76625f 2018-10-22 stsp doupdate();
1398 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1399 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1400 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1401 e5a0f69f 2018-08-18 stsp view_close(view);
1404 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1405 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1406 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1408 e5a0f69f 2018-08-18 stsp return err;
1411 4ed7e80c 2018-05-20 stsp __dead static void
1412 9f7d7167 2018-04-29 stsp usage_log(void)
1415 c70c5802 2018-08-01 stsp fprintf(stderr,
1416 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1417 9f7d7167 2018-04-29 stsp getprogname());
1421 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1422 80ddbec8 2018-04-29 stsp static const struct got_error *
1423 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1425 00dfcb92 2018-06-11 stsp char *vis = NULL;
1426 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1428 963b370f 2018-05-20 stsp *ws = NULL;
1429 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1430 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1431 00dfcb92 2018-06-11 stsp int vislen;
1432 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1433 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1435 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1436 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1438 00dfcb92 2018-06-11 stsp return err;
1439 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1440 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1441 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1442 a7f50699 2018-06-11 stsp goto done;
1446 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1447 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1448 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1449 a7f50699 2018-06-11 stsp goto done;
1452 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1453 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1455 00dfcb92 2018-06-11 stsp free(vis);
1456 963b370f 2018-05-20 stsp if (err) {
1457 963b370f 2018-05-20 stsp free(*ws);
1458 963b370f 2018-05-20 stsp *ws = NULL;
1459 963b370f 2018-05-20 stsp *wlen = 0;
1461 963b370f 2018-05-20 stsp return err;
1464 05171be4 2022-06-23 thomas static const struct got_error *
1465 05171be4 2022-06-23 thomas expand_tab(char **ptr, const char *src)
1467 05171be4 2022-06-23 thomas char *dst;
1468 05171be4 2022-06-23 thomas size_t len, n, idx = 0, sz = 0;
1470 05171be4 2022-06-23 thomas *ptr = NULL;
1471 05171be4 2022-06-23 thomas n = len = strlen(src);
1472 83316834 2022-06-23 thomas dst = malloc(n + 1);
1473 05171be4 2022-06-23 thomas if (dst == NULL)
1474 05171be4 2022-06-23 thomas return got_error_from_errno("malloc");
1476 05171be4 2022-06-23 thomas while (idx < len && src[idx]) {
1477 05171be4 2022-06-23 thomas const char c = src[idx];
1479 05171be4 2022-06-23 thomas if (c == '\t') {
1480 05171be4 2022-06-23 thomas size_t nb = TABSIZE - sz % TABSIZE;
1481 b235ad5d 2022-06-23 thomas char *p;
1483 b235ad5d 2022-06-23 thomas p = realloc(dst, n + nb);
1484 83316834 2022-06-23 thomas if (p == NULL) {
1485 83316834 2022-06-23 thomas free(dst);
1486 83316834 2022-06-23 thomas return got_error_from_errno("realloc");
1489 83316834 2022-06-23 thomas dst = p;
1490 05171be4 2022-06-23 thomas n += nb;
1491 83316834 2022-06-23 thomas memset(dst + sz, ' ', nb);
1492 05171be4 2022-06-23 thomas sz += nb;
1494 05171be4 2022-06-23 thomas dst[sz++] = src[idx];
1498 05171be4 2022-06-23 thomas dst[sz] = '\0';
1499 05171be4 2022-06-23 thomas *ptr = dst;
1500 05171be4 2022-06-23 thomas return NULL;
1504 8d208d34 2022-06-23 thomas * Advance at most n columns from wline starting at offset off.
1505 8d208d34 2022-06-23 thomas * Return the index to the first character after the span operation.
1506 8d208d34 2022-06-23 thomas * Return the combined column width of all spanned wide character in
1507 8d208d34 2022-06-23 thomas * *rcol.
1509 8d208d34 2022-06-23 thomas static int
1510 8d208d34 2022-06-23 thomas span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1512 8d208d34 2022-06-23 thomas int width, i, cols = 0;
1514 8d208d34 2022-06-23 thomas if (n == 0) {
1515 8d208d34 2022-06-23 thomas *rcol = cols;
1516 8d208d34 2022-06-23 thomas return off;
1519 8d208d34 2022-06-23 thomas for (i = off; wline[i] != L'\0'; ++i) {
1520 8d208d34 2022-06-23 thomas if (wline[i] == L'\t')
1521 8d208d34 2022-06-23 thomas width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1523 8d208d34 2022-06-23 thomas width = wcwidth(wline[i]);
1525 8d208d34 2022-06-23 thomas if (width == -1) {
1526 8d208d34 2022-06-23 thomas width = 1;
1527 8d208d34 2022-06-23 thomas wline[i] = L'.';
1530 8d208d34 2022-06-23 thomas if (cols + width > n)
1532 8d208d34 2022-06-23 thomas cols += width;
1535 8d208d34 2022-06-23 thomas *rcol = cols;
1536 8d208d34 2022-06-23 thomas return i;
1540 f91a2b48 2022-06-23 thomas * Format a line for display, ensuring that it won't overflow a width limit.
1541 f91a2b48 2022-06-23 thomas * With scrolling, the width returned refers to the scrolled version of the
1542 f91a2b48 2022-06-23 thomas * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1544 f91a2b48 2022-06-23 thomas static const struct got_error *
1545 f91a2b48 2022-06-23 thomas format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1546 f91a2b48 2022-06-23 thomas const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1548 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1549 8d208d34 2022-06-23 thomas int cols;
1550 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1551 05171be4 2022-06-23 thomas char *exstr = NULL;
1552 963b370f 2018-05-20 stsp size_t wlen;
1553 8d208d34 2022-06-23 thomas int i, scrollx;
1555 963b370f 2018-05-20 stsp *wlinep = NULL;
1556 b700b5d6 2018-07-10 stsp *widthp = 0;
1558 05171be4 2022-06-23 thomas if (expand) {
1559 05171be4 2022-06-23 thomas err = expand_tab(&exstr, line);
1560 05171be4 2022-06-23 thomas if (err)
1561 05171be4 2022-06-23 thomas return err;
1564 05171be4 2022-06-23 thomas err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1565 05171be4 2022-06-23 thomas free(exstr);
1567 963b370f 2018-05-20 stsp return err;
1569 8d208d34 2022-06-23 thomas scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1571 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1572 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1575 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1576 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1580 8d208d34 2022-06-23 thomas i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1581 8d208d34 2022-06-23 thomas wline[i] = L'\0';
1583 b700b5d6 2018-07-10 stsp if (widthp)
1584 b700b5d6 2018-07-10 stsp *widthp = cols;
1585 f91a2b48 2022-06-23 thomas if (scrollxp)
1586 f91a2b48 2022-06-23 thomas *scrollxp = scrollx;
1588 963b370f 2018-05-20 stsp free(wline);
1590 963b370f 2018-05-20 stsp *wlinep = wline;
1591 963b370f 2018-05-20 stsp return err;
1594 8b473291 2019-02-21 stsp static const struct got_error*
1595 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1596 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1598 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1599 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1601 8b473291 2019-02-21 stsp const char *name;
1603 8b473291 2019-02-21 stsp *refs_str = NULL;
1605 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1606 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1607 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1610 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1611 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1613 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1614 8b473291 2019-02-21 stsp name += 5;
1615 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1616 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1618 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1619 8b473291 2019-02-21 stsp name += 6;
1620 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1621 8b473291 2019-02-21 stsp name += 8;
1622 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1623 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1626 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1629 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1630 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1631 5d844a1e 2019-08-13 stsp if (err) {
1632 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1633 48cae60d 2020-09-22 stsp free(ref_id);
1636 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1637 5d844a1e 2019-08-13 stsp err = NULL;
1638 5d844a1e 2019-08-13 stsp tag = NULL;
1641 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1642 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1643 48cae60d 2020-09-22 stsp free(ref_id);
1645 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1646 52b5abe1 2019-08-13 stsp if (cmp != 0)
1648 8b473291 2019-02-21 stsp s = *refs_str;
1649 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1650 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1651 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1653 8b473291 2019-02-21 stsp *refs_str = NULL;
1659 8b473291 2019-02-21 stsp return err;
1662 963b370f 2018-05-20 stsp static const struct got_error *
1663 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1664 27a741e5 2019-09-11 stsp int col_tab_align)
1666 e6b8b890 2020-12-29 naddy char *smallerthan;
1668 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1669 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1670 5813d178 2019-03-09 stsp author = smallerthan + 1;
1671 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1672 f91a2b48 2022-06-23 thomas return format_line(wauthor, author_width, NULL, author, 0, limit,
1673 f91a2b48 2022-06-23 thomas col_tab_align, 0);
1676 5813d178 2019-03-09 stsp static const struct got_error *
1677 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1678 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1679 8fdc79fe 2020-12-01 naddy int author_display_cols)
1681 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1682 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1683 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1684 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1685 5813d178 2019-03-09 stsp char *author = NULL;
1686 f91a2b48 2022-06-23 thomas wchar_t *wlogmsg = NULL, *wauthor = NULL;
1687 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1688 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1689 f91a2b48 2022-06-23 thomas int col, limit, scrollx;
1690 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1691 ccb26ccd 2018-11-05 stsp struct tm tm;
1692 45d799e2 2018-12-23 stsp time_t committer_time;
1693 11b20872 2019-11-08 stsp struct tog_color *tc;
1695 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1696 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1697 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1698 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1699 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1701 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1702 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1704 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1705 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1707 11b20872 2019-11-08 stsp wattr_on(view->window,
1708 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1709 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1711 11b20872 2019-11-08 stsp wattr_off(view->window,
1712 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1713 27a741e5 2019-09-11 stsp col = limit;
1714 b39d25c7 2018-07-10 stsp if (col > avail)
1715 b39d25c7 2018-07-10 stsp goto done;
1717 6570a66d 2019-11-08 stsp if (avail >= 120) {
1718 6570a66d 2019-11-08 stsp char *id_str;
1719 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1721 6570a66d 2019-11-08 stsp goto done;
1722 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1724 11b20872 2019-11-08 stsp wattr_on(view->window,
1725 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1726 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1728 11b20872 2019-11-08 stsp wattr_off(view->window,
1729 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1730 6570a66d 2019-11-08 stsp free(id_str);
1732 6570a66d 2019-11-08 stsp if (col > avail)
1733 6570a66d 2019-11-08 stsp goto done;
1736 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1737 5813d178 2019-03-09 stsp if (author == NULL) {
1738 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1739 80ddbec8 2018-04-29 stsp goto done;
1741 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1743 bb737323 2018-05-20 stsp goto done;
1744 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1746 11b20872 2019-11-08 stsp wattr_on(view->window,
1747 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1748 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1750 11b20872 2019-11-08 stsp wattr_off(view->window,
1751 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1752 bb737323 2018-05-20 stsp col += author_width;
1753 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1754 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1756 bb737323 2018-05-20 stsp author_width++;
1758 9c2eaf34 2018-05-20 stsp if (col > avail)
1759 9c2eaf34 2018-05-20 stsp goto done;
1761 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1763 6d9fbc00 2018-04-29 stsp goto done;
1764 bb737323 2018-05-20 stsp logmsg = logmsg0;
1765 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1767 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1768 bb737323 2018-05-20 stsp if (newline)
1769 bb737323 2018-05-20 stsp *newline = '\0';
1770 f91a2b48 2022-06-23 thomas limit = avail - col;
1771 444d5325 2022-07-03 thomas if (view->child && !view_is_hsplit_top(view) && limit > 0)
1772 5c6cacf5 2022-06-23 thomas limit--; /* for the border */
1773 f91a2b48 2022-06-23 thomas err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1774 f91a2b48 2022-06-23 thomas limit, col, 1);
1775 331b1a16 2022-06-23 thomas if (err)
1776 331b1a16 2022-06-23 thomas goto done;
1777 f91a2b48 2022-06-23 thomas waddwstr(view->window, &wlogmsg[scrollx]);
1778 331b1a16 2022-06-23 thomas col += MAX(logmsg_width, 0);
1779 27a741e5 2019-09-11 stsp while (col < avail) {
1780 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1784 80ddbec8 2018-04-29 stsp free(logmsg0);
1785 bb737323 2018-05-20 stsp free(wlogmsg);
1786 5813d178 2019-03-09 stsp free(author);
1787 bb737323 2018-05-20 stsp free(wauthor);
1788 80ddbec8 2018-04-29 stsp free(line);
1789 80ddbec8 2018-04-29 stsp return err;
1792 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1793 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1794 899d86c2 2018-05-10 stsp struct got_object_id *id)
1796 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1798 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1799 80ddbec8 2018-04-29 stsp if (entry == NULL)
1800 899d86c2 2018-05-10 stsp return NULL;
1802 899d86c2 2018-05-10 stsp entry->id = id;
1803 99db9666 2018-05-07 stsp entry->commit = commit;
1804 899d86c2 2018-05-10 stsp return entry;
1807 99db9666 2018-05-07 stsp static void
1808 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1810 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1812 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1813 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1814 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1815 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1816 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1817 99db9666 2018-05-07 stsp free(entry);
1820 99db9666 2018-05-07 stsp static void
1821 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1823 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1824 99db9666 2018-05-07 stsp pop_commit(commits);
1827 c4972b91 2018-05-07 stsp static const struct got_error *
1828 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1829 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1831 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1832 13add988 2019-10-15 stsp regmatch_t regmatch;
1833 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1835 13add988 2019-10-15 stsp *have_match = 0;
1837 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1839 13add988 2019-10-15 stsp return err;
1841 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1843 13add988 2019-10-15 stsp goto done;
1845 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1846 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1847 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1848 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1849 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1850 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1851 13add988 2019-10-15 stsp *have_match = 1;
1853 13add988 2019-10-15 stsp free(id_str);
1854 13add988 2019-10-15 stsp free(logmsg);
1855 13add988 2019-10-15 stsp return err;
1858 13add988 2019-10-15 stsp static const struct got_error *
1859 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1861 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1864 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1865 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1866 1a76625f 2018-10-22 stsp * while updating the display.
1869 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1870 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1871 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1872 1a76625f 2018-10-22 stsp int errcode;
1874 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1875 4e0d2870 2020-12-07 naddy NULL, NULL);
1876 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1879 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1882 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1883 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1884 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1888 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1889 1a76625f 2018-10-22 stsp if (errcode) {
1890 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1891 13add988 2019-10-15 stsp "pthread_mutex_lock");
1895 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1896 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1897 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1899 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1900 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1901 7c1452c1 2020-03-26 stsp int have_match;
1902 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1905 7c1452c1 2020-03-26 stsp if (have_match)
1906 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1909 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1910 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1911 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1912 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1915 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1917 9ba79e04 2018-06-11 stsp return err;
1920 2b779855 2020-12-05 naddy static void
1921 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1923 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1924 2b779855 2020-12-05 naddy int ncommits = 0;
1926 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1927 2b779855 2020-12-05 naddy while (entry) {
1928 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1929 2b779855 2020-12-05 naddy s->selected_entry = entry;
1932 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1933 2b779855 2020-12-05 naddy ncommits++;
1937 0553a4e3 2018-04-30 stsp static const struct got_error *
1938 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1940 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1941 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1942 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1943 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1944 60493ae3 2019-06-20 stsp int width;
1945 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1946 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1947 8b473291 2019-02-21 stsp char *refs_str = NULL;
1948 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1949 11b20872 2019-11-08 stsp struct tog_color *tc;
1950 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1952 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1953 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1954 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1955 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1957 ecb28ae0 2018-07-16 stsp return err;
1958 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1959 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1960 d2075bf3 2020-12-25 stsp if (refs) {
1961 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1962 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1964 d2075bf3 2020-12-25 stsp goto done;
1968 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1969 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1971 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1972 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1973 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1974 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1975 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1976 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1977 8f4ed634 2020-03-26 stsp goto done;
1980 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1982 f9686aa5 2020-03-27 stsp if (view->searching) {
1983 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1984 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1985 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1986 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1987 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1988 f9686aa5 2020-03-27 stsp search_str = "searching...";
1991 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1992 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1993 f9686aa5 2020-03-27 stsp search_str ? search_str :
1994 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1995 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1996 8f4ed634 2020-03-26 stsp goto done;
2000 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
2001 91198554 2022-06-23 thomas if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
2002 91198554 2022-06-23 thomas "........................................",
2003 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
2004 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2005 1a76625f 2018-10-22 stsp header = NULL;
2006 1a76625f 2018-10-22 stsp goto done;
2008 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
2009 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
2010 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
2011 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2012 1a76625f 2018-10-22 stsp header = NULL;
2013 1a76625f 2018-10-22 stsp goto done;
2015 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
2017 1a76625f 2018-10-22 stsp goto done;
2019 2814baeb 2018-08-01 stsp werase(view->window);
2021 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2022 a3404814 2018-09-02 stsp wstandout(view->window);
2023 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
2025 11b20872 2019-11-08 stsp wattr_on(view->window,
2026 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2027 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
2029 11b20872 2019-11-08 stsp wattr_off(view->window,
2030 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2031 1a76625f 2018-10-22 stsp while (width < view->ncols) {
2032 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
2035 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2036 a3404814 2018-09-02 stsp wstandend(view->window);
2037 ecb28ae0 2018-07-16 stsp free(wline);
2038 ecb28ae0 2018-07-16 stsp if (limit <= 1)
2039 1a76625f 2018-10-22 stsp goto done;
2041 331b1a16 2022-06-23 thomas /* Grow author column size if necessary, and set view->maxx. */
2042 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2043 5813d178 2019-03-09 stsp ncommits = 0;
2044 05171be4 2022-06-23 thomas view->maxx = 0;
2045 5813d178 2019-03-09 stsp while (entry) {
2046 05171be4 2022-06-23 thomas char *author, *eol, *msg, *msg0;
2047 331b1a16 2022-06-23 thomas wchar_t *wauthor, *wmsg;
2048 5813d178 2019-03-09 stsp int width;
2049 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
2051 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
2052 5813d178 2019-03-09 stsp if (author == NULL) {
2053 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
2054 5813d178 2019-03-09 stsp goto done;
2056 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
2057 27a741e5 2019-09-11 stsp date_display_cols);
2058 5813d178 2019-03-09 stsp if (author_cols < width)
2059 5813d178 2019-03-09 stsp author_cols = width;
2060 5813d178 2019-03-09 stsp free(wauthor);
2061 5813d178 2019-03-09 stsp free(author);
2062 05171be4 2022-06-23 thomas err = got_object_commit_get_logmsg(&msg0, entry->commit);
2063 05171be4 2022-06-23 thomas if (err)
2064 05171be4 2022-06-23 thomas goto done;
2065 05171be4 2022-06-23 thomas msg = msg0;
2066 05171be4 2022-06-23 thomas while (*msg == '\n')
2068 05171be4 2022-06-23 thomas if ((eol = strchr(msg, '\n')))
2069 331b1a16 2022-06-23 thomas *eol = '\0';
2070 f91a2b48 2022-06-23 thomas err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
2071 331b1a16 2022-06-23 thomas date_display_cols + author_cols, 0);
2072 331b1a16 2022-06-23 thomas if (err)
2073 331b1a16 2022-06-23 thomas goto done;
2074 331b1a16 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
2075 05171be4 2022-06-23 thomas free(msg0);
2076 331b1a16 2022-06-23 thomas free(wmsg);
2077 7ca04879 2019-10-19 stsp ncommits++;
2078 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
2081 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
2082 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
2083 867c6645 2018-07-10 stsp ncommits = 0;
2084 899d86c2 2018-05-10 stsp while (entry) {
2085 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
2087 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2088 2814baeb 2018-08-01 stsp wstandout(view->window);
2089 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
2090 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
2091 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
2092 2814baeb 2018-08-01 stsp wstandend(view->window);
2094 60493ae3 2019-06-20 stsp goto done;
2095 0553a4e3 2018-04-30 stsp ncommits++;
2096 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
2097 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
2100 a5d43cac 2022-07-01 thomas view_border(view);
2102 1a76625f 2018-10-22 stsp free(id_str);
2103 8b473291 2019-02-21 stsp free(refs_str);
2104 1a76625f 2018-10-22 stsp free(ncommits_str);
2105 1a76625f 2018-10-22 stsp free(header);
2106 80ddbec8 2018-04-29 stsp return err;
2109 07b55e75 2018-05-10 stsp static void
2110 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
2112 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
2113 07b55e75 2018-05-10 stsp int nscrolled = 0;
2115 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
2116 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
2119 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
2120 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
2121 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2122 07b55e75 2018-05-10 stsp if (entry) {
2123 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
2124 07b55e75 2018-05-10 stsp nscrolled++;
2129 aa075928 2018-05-10 stsp static const struct got_error *
2130 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
2132 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
2133 5e224a3e 2019-02-22 stsp int errcode;
2135 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
2137 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
2138 ffe38506 2020-12-01 naddy if (ta->log_complete)
2141 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
2142 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
2143 7aafa0d1 2019-02-22 stsp if (errcode)
2144 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2145 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2148 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
2149 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
2151 7c1452c1 2020-03-26 stsp if (!wait)
2154 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2155 ffe38506 2020-12-01 naddy show_log_view(view);
2156 7c1452c1 2020-03-26 stsp update_panels();
2157 7c1452c1 2020-03-26 stsp doupdate();
2159 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
2160 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2161 82954512 2020-02-03 stsp if (errcode)
2162 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
2163 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2165 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
2166 ffe38506 2020-12-01 naddy show_log_view(view);
2167 7c1452c1 2020-03-26 stsp update_panels();
2168 7c1452c1 2020-03-26 stsp doupdate();
2171 5e224a3e 2019-02-22 stsp return NULL;
2174 a5d43cac 2022-07-01 thomas static const struct got_error *
2175 a5d43cac 2022-07-01 thomas request_log_commits(struct tog_view *view)
2177 a5d43cac 2022-07-01 thomas struct tog_log_view_state *state = &view->state.log;
2178 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2180 a5d43cac 2022-07-01 thomas state->thread_args.commits_needed = view->nscrolled;
2181 a5d43cac 2022-07-01 thomas err = trigger_log_thread(view, 1);
2182 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2184 a5d43cac 2022-07-01 thomas return err;
2187 5e224a3e 2019-02-22 stsp static const struct got_error *
2188 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
2190 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
2191 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
2192 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
2193 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
2195 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
2196 5e224a3e 2019-02-22 stsp return NULL;
2198 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2199 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
2200 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
2202 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
2204 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
2205 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2207 5e224a3e 2019-02-22 stsp return err;
2211 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2212 a5d43cac 2022-07-01 thomas if (pentry == NULL && view->mode != TOG_VIEW_SPLIT_HRZN)
2215 a5d43cac 2022-07-01 thomas s->last_displayed_entry = pentry ?
2216 a5d43cac 2022-07-01 thomas pentry : s->last_displayed_entry;;
2218 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2219 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
2221 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
2222 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
2224 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
2225 a5d43cac 2022-07-01 thomas view->nscrolled += nscrolled;
2227 a5d43cac 2022-07-01 thomas view->nscrolled = 0;
2229 dd0a52c1 2018-05-20 stsp return err;
2232 cd0acaa7 2018-05-20 stsp static const struct got_error *
2233 a5d43cac 2022-07-01 thomas open_diff_view_for_commit(struct tog_view **new_view, int begin_y, int begin_x,
2234 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
2235 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
2237 cd0acaa7 2018-05-20 stsp const struct got_error *err;
2238 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
2239 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
2241 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
2242 15a94983 2018-12-23 stsp if (diff_view == NULL)
2243 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
2245 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2246 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2247 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2248 e5a0f69f 2018-08-18 stsp if (err == NULL)
2249 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
2250 cd0acaa7 2018-05-20 stsp return err;
2253 80ddbec8 2018-04-29 stsp static const struct got_error *
2254 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
2255 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
2257 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
2259 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
2260 941e9f74 2019-05-21 stsp if (parent == NULL)
2261 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
2263 941e9f74 2019-05-21 stsp parent->tree = s->tree;
2264 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
2265 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
2266 941e9f74 2019-05-21 stsp parent->selected = s->selected;
2267 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2268 941e9f74 2019-05-21 stsp s->tree = subtree;
2269 941e9f74 2019-05-21 stsp s->selected = 0;
2270 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
2271 941e9f74 2019-05-21 stsp return NULL;
2274 941e9f74 2019-05-21 stsp static const struct got_error *
2275 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
2276 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
2278 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
2279 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
2280 941e9f74 2019-05-21 stsp const char *p;
2281 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
2283 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
2285 941e9f74 2019-05-21 stsp while (*p) {
2286 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2287 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
2288 56e0773d 2019-11-28 stsp char *te_name;
2290 33cbf02b 2020-01-12 stsp while (p[0] == '/')
2293 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
2294 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2295 941e9f74 2019-05-21 stsp if (slash == NULL)
2296 33cbf02b 2020-01-12 stsp te_name = strdup(p);
2298 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
2299 56e0773d 2019-11-28 stsp if (te_name == NULL) {
2300 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
2303 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
2304 56e0773d 2019-11-28 stsp if (te == NULL) {
2305 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2306 56e0773d 2019-11-28 stsp free(te_name);
2309 56e0773d 2019-11-28 stsp free(te_name);
2310 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
2312 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2313 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
2315 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
2316 941e9f74 2019-05-21 stsp if (slash)
2317 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
2319 941e9f74 2019-05-21 stsp subpath = strdup(path);
2320 941e9f74 2019-05-21 stsp if (subpath == NULL) {
2321 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
2325 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
2330 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
2331 941e9f74 2019-05-21 stsp free(tree_id);
2335 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
2336 941e9f74 2019-05-21 stsp if (err) {
2337 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
2340 941e9f74 2019-05-21 stsp if (slash == NULL)
2342 941e9f74 2019-05-21 stsp free(subpath);
2343 941e9f74 2019-05-21 stsp subpath = NULL;
2344 941e9f74 2019-05-21 stsp p = slash;
2347 941e9f74 2019-05-21 stsp free(subpath);
2348 1a76625f 2018-10-22 stsp return err;
2351 61266923 2020-01-14 stsp static const struct got_error *
2352 444d5325 2022-07-03 thomas browse_commit_tree(struct tog_view **new_view, int begin_y, int begin_x,
2353 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2354 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2356 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2357 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2358 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2360 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
2361 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2362 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2364 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2366 55cccc34 2020-02-20 stsp return err;
2367 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2369 55cccc34 2020-02-20 stsp *new_view = tree_view;
2371 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2372 55cccc34 2020-02-20 stsp return NULL;
2374 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2377 55cccc34 2020-02-20 stsp static const struct got_error *
2378 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2380 61266923 2020-01-14 stsp sigset_t sigset;
2381 61266923 2020-01-14 stsp int errcode;
2383 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2384 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2386 296152d1 2022-05-31 thomas /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2387 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2388 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2389 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2390 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2391 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGINT) == -1)
2392 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2393 296152d1 2022-05-31 thomas if (sigaddset(&sigset, SIGTERM) == -1)
2394 296152d1 2022-05-31 thomas return got_error_from_errno("sigaddset");
2396 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2397 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2398 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2400 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2401 61266923 2020-01-14 stsp if (errcode)
2402 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2404 61266923 2020-01-14 stsp return NULL;
2407 1a76625f 2018-10-22 stsp static void *
2408 1a76625f 2018-10-22 stsp log_thread(void *arg)
2410 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2411 1a76625f 2018-10-22 stsp int errcode = 0;
2412 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2413 1a76625f 2018-10-22 stsp int done = 0;
2415 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2417 61266923 2020-01-14 stsp return (void *)err;
2419 296152d1 2022-05-31 thomas while (!done && !err && !tog_fatal_signal_received()) {
2420 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2421 1a76625f 2018-10-22 stsp if (err) {
2422 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2423 1a76625f 2018-10-22 stsp return (void *)err;
2424 1a76625f 2018-10-22 stsp err = NULL;
2426 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2427 1a76625f 2018-10-22 stsp a->commits_needed--;
2429 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2430 3abe8080 2019-04-10 stsp if (errcode) {
2431 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2432 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2434 3abe8080 2019-04-10 stsp } else if (*a->quit)
2436 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2437 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2438 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2439 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2442 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2443 7c1452c1 2020-03-26 stsp if (errcode) {
2444 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2445 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2446 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2451 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2453 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2454 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2455 7c1452c1 2020-03-26 stsp &tog_mutex);
2456 7c1452c1 2020-03-26 stsp if (errcode)
2457 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2458 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2459 21355643 2020-12-06 stsp if (*a->quit)
2464 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2465 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2466 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2467 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2469 3abe8080 2019-04-10 stsp a->log_complete = 1;
2470 1a76625f 2018-10-22 stsp return (void *)err;
2473 1a76625f 2018-10-22 stsp static const struct got_error *
2474 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2476 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2477 1a76625f 2018-10-22 stsp int errcode;
2479 1a76625f 2018-10-22 stsp if (s->thread) {
2480 1a76625f 2018-10-22 stsp s->quit = 1;
2481 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2482 1a76625f 2018-10-22 stsp if (errcode)
2483 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2484 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2485 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2486 1a76625f 2018-10-22 stsp if (errcode)
2487 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2488 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2489 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2490 1a76625f 2018-10-22 stsp if (errcode)
2491 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2492 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2493 1a76625f 2018-10-22 stsp if (errcode)
2494 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2495 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2496 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2499 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2500 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2501 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2504 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds) {
2505 1af5eddf 2022-06-23 thomas const struct got_error *pack_err =
2506 1af5eddf 2022-06-23 thomas got_repo_pack_fds_close(s->thread_args.pack_fds);
2507 1af5eddf 2022-06-23 thomas if (err == NULL)
2508 1af5eddf 2022-06-23 thomas err = pack_err;
2509 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds = NULL;
2512 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2513 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2514 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2517 9343a5fb 2018-06-23 stsp return err;
2520 9343a5fb 2018-06-23 stsp static const struct got_error *
2521 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2523 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2524 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2525 276b94a1 2020-11-13 naddy int errcode;
2527 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2529 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2530 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2531 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2533 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2534 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2535 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2537 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2538 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2539 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2540 1a76625f 2018-10-22 stsp free(s->start_id);
2541 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2542 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2543 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2544 1a76625f 2018-10-22 stsp return err;
2547 1a76625f 2018-10-22 stsp static const struct got_error *
2548 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2550 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2552 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2553 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2554 60493ae3 2019-06-20 stsp return NULL;
2557 60493ae3 2019-06-20 stsp static const struct got_error *
2558 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2560 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2561 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2562 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2564 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2565 f9686aa5 2020-03-27 stsp show_log_view(view);
2566 f9686aa5 2020-03-27 stsp update_panels();
2567 f9686aa5 2020-03-27 stsp doupdate();
2569 96e2b566 2019-07-08 stsp if (s->search_entry) {
2570 13add988 2019-10-15 stsp int errcode, ch;
2571 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2572 13add988 2019-10-15 stsp if (errcode)
2573 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2574 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2575 13add988 2019-10-15 stsp ch = wgetch(view->window);
2576 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2577 13add988 2019-10-15 stsp if (errcode)
2578 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2579 13add988 2019-10-15 stsp "pthread_mutex_lock");
2580 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE) {
2581 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2582 678cbce5 2019-07-28 stsp return NULL;
2584 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2585 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2587 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2588 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2589 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2590 df5e841d 2022-06-23 thomas int matched_idx = s->matched_entry->idx;
2591 df5e841d 2022-06-23 thomas int selected_idx = s->selected_entry->idx;
2594 1f4d5162 2022-06-23 thomas * If the user has moved the cursor after we hit a match,
2595 1f4d5162 2022-06-23 thomas * the position from where we should continue searching
2596 1f4d5162 2022-06-23 thomas * might have changed.
2598 b74feda6 2022-06-23 thomas if (view->searching == TOG_SEARCH_FORWARD) {
2599 df5e841d 2022-06-23 thomas if (matched_idx > selected_idx)
2600 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->selected_entry, entry);
2602 df5e841d 2022-06-23 thomas entry = TAILQ_NEXT(s->matched_entry, entry);
2603 b74feda6 2022-06-23 thomas } else {
2604 df5e841d 2022-06-23 thomas if (matched_idx < selected_idx)
2605 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->selected_entry,
2606 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2608 df5e841d 2022-06-23 thomas entry = TAILQ_PREV(s->matched_entry,
2609 df5e841d 2022-06-23 thomas commit_queue_head, entry);
2612 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2615 60493ae3 2019-06-20 stsp while (1) {
2616 13add988 2019-10-15 stsp int have_match = 0;
2618 60493ae3 2019-06-20 stsp if (entry == NULL) {
2619 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2620 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2621 f9967bca 2020-03-27 stsp view->search_next_done =
2622 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2623 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2624 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2625 f801134a 2019-06-25 stsp return NULL;
2628 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2629 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2630 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2632 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2633 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2636 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2637 13add988 2019-10-15 stsp &view->regex);
2640 13add988 2019-10-15 stsp if (have_match) {
2641 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2642 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2646 96e2b566 2019-07-08 stsp s->search_entry = entry;
2647 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2648 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2650 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2653 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2654 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2655 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2656 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2658 60493ae3 2019-06-20 stsp return err;
2661 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2662 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2664 60493ae3 2019-06-20 stsp return err;
2669 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2671 60493ae3 2019-06-20 stsp return NULL;
2674 60493ae3 2019-06-20 stsp static const struct got_error *
2675 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2676 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2677 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2679 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2680 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2681 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2682 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2683 1a76625f 2018-10-22 stsp int errcode;
2685 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2686 f135c941 2020-02-20 stsp free(s->in_repo_path);
2687 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2688 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2689 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2692 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2693 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2694 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2696 fb2756b9 2018-08-04 stsp s->repo = repo;
2697 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2698 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2699 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2700 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2701 9cd7cbd1 2020-12-07 stsp goto done;
2704 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2705 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2706 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2707 5036bf37 2018-09-24 stsp goto done;
2709 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2711 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2712 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2713 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2714 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2716 11b20872 2019-11-08 stsp goto done;
2717 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2718 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2719 11b20872 2019-11-08 stsp if (err) {
2720 11b20872 2019-11-08 stsp free_colors(&s->colors);
2721 11b20872 2019-11-08 stsp goto done;
2723 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2724 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2725 11b20872 2019-11-08 stsp if (err) {
2726 11b20872 2019-11-08 stsp free_colors(&s->colors);
2727 11b20872 2019-11-08 stsp goto done;
2731 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2732 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2733 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2734 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2735 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2737 1af5eddf 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
2738 1af5eddf 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2739 1af5eddf 2022-06-23 thomas if (err)
2740 1af5eddf 2022-06-23 thomas goto done;
2742 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2743 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
2744 7cd52833 2022-06-23 thomas if (err)
2745 7cd52833 2022-06-23 thomas goto done;
2746 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2747 b672a97a 2020-01-27 stsp !s->log_branches);
2749 1a76625f 2018-10-22 stsp goto done;
2750 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2751 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2753 c5b78334 2020-01-12 stsp goto done;
2755 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2756 1a76625f 2018-10-22 stsp if (errcode) {
2757 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2758 1a76625f 2018-10-22 stsp goto done;
2760 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2761 7c1452c1 2020-03-26 stsp if (errcode) {
2762 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2763 7c1452c1 2020-03-26 stsp goto done;
2766 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2767 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2768 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2769 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2770 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2771 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2772 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2773 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2774 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2775 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2776 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2777 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2778 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2781 1a76625f 2018-10-22 stsp close_log_view(view);
2782 ba4f502b 2018-08-04 stsp return err;
2785 e5a0f69f 2018-08-18 stsp static const struct got_error *
2786 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2788 f2f6d207 2020-11-24 stsp const struct got_error *err;
2789 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2791 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
2792 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2793 2b380cc8 2018-10-24 stsp &s->thread_args);
2794 2b380cc8 2018-10-24 stsp if (errcode)
2795 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2796 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2797 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2799 f2f6d207 2020-11-24 stsp return err;
2803 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2806 b31f89ff 2022-07-01 thomas static void
2807 b31f89ff 2022-07-01 thomas log_move_cursor_up(struct tog_view *view, int page, int home)
2809 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
2811 b31f89ff 2022-07-01 thomas if (s->selected_entry->idx == 0)
2812 b31f89ff 2022-07-01 thomas view->count = 0;
2813 b31f89ff 2022-07-01 thomas if (s->first_displayed_entry == NULL)
2816 b31f89ff 2022-07-01 thomas if ((page && TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2817 b31f89ff 2022-07-01 thomas || home)
2818 b31f89ff 2022-07-01 thomas s->selected = home ? 0 : MAX(0, s->selected - page - 1);
2820 b31f89ff 2022-07-01 thomas if (!page && !home && s->selected > 0)
2821 b31f89ff 2022-07-01 thomas --s->selected;
2823 b31f89ff 2022-07-01 thomas log_scroll_up(s, home ? s->commits.ncommits : MAX(page, 1));
2825 b31f89ff 2022-07-01 thomas select_commit(s);
2829 b31f89ff 2022-07-01 thomas static const struct got_error *
2830 b31f89ff 2022-07-01 thomas log_move_cursor_down(struct tog_view *view, int page)
2832 b31f89ff 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
2833 b31f89ff 2022-07-01 thomas struct commit_queue_entry *first;
2834 b31f89ff 2022-07-01 thomas const struct got_error *err = NULL;
2836 b31f89ff 2022-07-01 thomas first = s->first_displayed_entry;
2837 b31f89ff 2022-07-01 thomas if (first == NULL) {
2838 b31f89ff 2022-07-01 thomas view->count = 0;
2839 b31f89ff 2022-07-01 thomas return NULL;
2842 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
2843 b31f89ff 2022-07-01 thomas s->selected_entry->idx >= s->commits.ncommits - 1)
2844 b31f89ff 2022-07-01 thomas return NULL;
2846 b31f89ff 2022-07-01 thomas if (!page) {
2847 b31f89ff 2022-07-01 thomas int eos = view->nlines - 2;
2849 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
2850 a5d43cac 2022-07-01 thomas --eos; /* border consumes the last line */
2851 b31f89ff 2022-07-01 thomas if (s->selected < MIN(eos, s->commits.ncommits - 1))
2852 b31f89ff 2022-07-01 thomas ++s->selected;
2854 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, 1);
2855 068ab281 2022-07-01 thomas } else if (s->thread_args.load_all) {
2856 b31f89ff 2022-07-01 thomas if (s->last_displayed_entry->idx == s->commits.ncommits - 1)
2857 b31f89ff 2022-07-01 thomas s->selected += MIN(s->last_displayed_entry->idx -
2858 b31f89ff 2022-07-01 thomas s->selected_entry->idx, page + 1);
2860 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, MIN(page,
2861 b31f89ff 2022-07-01 thomas s->commits.ncommits - s->selected_entry->idx - 1));
2862 b31f89ff 2022-07-01 thomas s->selected = MIN(view->nlines - 2, s->commits.ncommits - 1);
2863 b31f89ff 2022-07-01 thomas } else {
2864 b31f89ff 2022-07-01 thomas err = log_scroll_down(view, page);
2865 b31f89ff 2022-07-01 thomas if (err)
2866 b31f89ff 2022-07-01 thomas return err;
2867 b31f89ff 2022-07-01 thomas if (first == s->first_displayed_entry && s->selected <
2868 b31f89ff 2022-07-01 thomas MIN(view->nlines - 2, s->commits.ncommits - 1)) {
2869 b31f89ff 2022-07-01 thomas s->selected = MIN(s->commits.ncommits - 1, page);
2872 b31f89ff 2022-07-01 thomas if (err)
2873 b31f89ff 2022-07-01 thomas return err;
2876 a5d43cac 2022-07-01 thomas * We might necessarily overshoot in horizontal
2877 a5d43cac 2022-07-01 thomas * splits; if so, select the last displayed commit.
2879 a5d43cac 2022-07-01 thomas s->selected = MIN(s->selected,
2880 a5d43cac 2022-07-01 thomas s->last_displayed_entry->idx - s->first_displayed_entry->idx);
2882 b31f89ff 2022-07-01 thomas select_commit(s);
2884 b31f89ff 2022-07-01 thomas if (s->thread_args.log_complete &&
2885 b31f89ff 2022-07-01 thomas s->selected_entry->idx == s->commits.ncommits - 1)
2886 b31f89ff 2022-07-01 thomas view->count = 0;
2888 b31f89ff 2022-07-01 thomas return NULL;
2892 a5d43cac 2022-07-01 thomas * Get splitscreen dimensions based on TOG_VIEW_SPLIT_MODE:
2893 a5d43cac 2022-07-01 thomas * TOG_VIEW_SPLIT_VERT vertical split if COLS > 119 (default)
2894 a5d43cac 2022-07-01 thomas * TOG_VIEW_SPLIT_HRZN horizontal split
2895 a5d43cac 2022-07-01 thomas * Assign start column and line of the new split to *x and *y, respectively,
2896 a5d43cac 2022-07-01 thomas * and assign view mode to view->mode.
2898 a5d43cac 2022-07-01 thomas static void
2899 a5d43cac 2022-07-01 thomas view_get_split(struct tog_view *view, int *y, int *x)
2901 a5d43cac 2022-07-01 thomas char *mode;
2906 a5d43cac 2022-07-01 thomas mode = getenv("TOG_VIEW_SPLIT_MODE");
2908 a5d43cac 2022-07-01 thomas if (!mode || mode[0] != 'h') {
2909 a5d43cac 2022-07-01 thomas view->mode = TOG_VIEW_SPLIT_VERT;
2910 a5d43cac 2022-07-01 thomas *x = view_split_begin_x(view->begin_x);
2911 a5d43cac 2022-07-01 thomas } else if (mode && mode[0] == 'h') {
2912 a5d43cac 2022-07-01 thomas view->mode = TOG_VIEW_SPLIT_HRZN;
2913 a5d43cac 2022-07-01 thomas *y = view_split_begin_y(view->lines);
2917 a5d43cac 2022-07-01 thomas /* Split view horizontally at y and offset view->state->selected line. */
2918 e5a0f69f 2018-08-18 stsp static const struct got_error *
2919 a5d43cac 2022-07-01 thomas view_init_hsplit(struct tog_view *view, int y)
2921 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
2923 a5d43cac 2022-07-01 thomas view->nlines = y;
2924 a5d43cac 2022-07-01 thomas err = view_resize(view);
2925 a5d43cac 2022-07-01 thomas if (err)
2926 a5d43cac 2022-07-01 thomas return err;
2928 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
2930 a5d43cac 2022-07-01 thomas return err;
2933 a5d43cac 2022-07-01 thomas static const struct got_error *
2934 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2936 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2937 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2938 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2939 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2940 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2941 068ab281 2022-07-01 thomas int begin_x = 0, begin_y = 0, eos, n, nscroll;
2943 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2944 634cb454 2022-07-03 thomas if (ch == CTRL('g') || ch == KEY_BACKSPACE)
2945 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2946 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2947 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, s->commits.ncommits);
2948 068ab281 2022-07-01 thomas s->thread_args.load_all = 0;
2950 b31f89ff 2022-07-01 thomas return err;
2953 068ab281 2022-07-01 thomas eos = nscroll = view->nlines - 1;
2954 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
2955 068ab281 2022-07-01 thomas --eos; /* border */
2957 528dedf3 2021-08-30 stsp switch (ch) {
2959 1e37a5c2 2019-05-12 jcs s->quit = 1;
2961 05171be4 2022-06-23 thomas case '0':
2962 05171be4 2022-06-23 thomas view->x = 0;
2964 05171be4 2022-06-23 thomas case '$':
2965 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 2, 0);
2966 07b0611c 2022-06-23 thomas view->count = 0;
2968 05171be4 2022-06-23 thomas case KEY_RIGHT:
2969 05171be4 2022-06-23 thomas case 'l':
2970 05171be4 2022-06-23 thomas if (view->x + view->ncols / 2 < view->maxx)
2971 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
2973 07b0611c 2022-06-23 thomas view->count = 0;
2975 05171be4 2022-06-23 thomas case KEY_LEFT:
2976 05171be4 2022-06-23 thomas case 'h':
2977 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
2978 07b0611c 2022-06-23 thomas if (view->x <= 0)
2979 07b0611c 2022-06-23 thomas view->count = 0;
2982 1e37a5c2 2019-05-12 jcs case KEY_UP:
2985 f7140bf5 2021-10-17 thomas case CTRL('p'):
2986 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 0);
2989 912a3f79 2021-08-30 j case KEY_HOME:
2990 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, 0, 1);
2991 07b0611c 2022-06-23 thomas view->count = 0;
2993 70f17a53 2022-06-13 thomas case CTRL('u'):
2994 23427b14 2022-06-23 thomas case 'u':
2995 70f17a53 2022-06-13 thomas nscroll /= 2;
2996 70f17a53 2022-06-13 thomas /* FALL THROUGH */
2997 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2998 a4292ac5 2019-05-12 jcs case CTRL('b'):
2999 1c5e5faa 2022-06-23 thomas case 'b':
3000 b31f89ff 2022-07-01 thomas log_move_cursor_up(view, nscroll, 0);
3003 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3006 f7140bf5 2021-10-17 thomas case CTRL('n'):
3007 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, 0);
3010 912a3f79 2021-08-30 j case KEY_END: {
3011 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
3012 912a3f79 2021-08-30 j * traverse them all. */
3013 07b0611c 2022-06-23 thomas view->count = 0;
3014 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
3015 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
3016 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
3019 f3bc9f1d 2021-09-05 naddy s->selected = 0;
3020 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
3021 068ab281 2022-07-01 thomas for (n = 0; n < eos; n++) {
3022 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
3024 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
3025 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
3027 f3bc9f1d 2021-09-05 naddy if (n > 0)
3028 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
3029 2b779855 2020-12-05 naddy select_commit(s);
3032 bccd1d5d 2022-06-13 thomas case CTRL('d'):
3033 23427b14 2022-06-23 thomas case 'd':
3034 70f17a53 2022-06-13 thomas nscroll /= 2;
3035 70f17a53 2022-06-13 thomas /* FALL THROUGH */
3036 70f17a53 2022-06-13 thomas case KEY_NPAGE:
3037 1c5e5faa 2022-06-23 thomas case CTRL('f'):
3038 4c2d69cb 2022-06-23 thomas case 'f':
3039 b31f89ff 2022-07-01 thomas case ' ':
3040 b31f89ff 2022-07-01 thomas err = log_move_cursor_down(view, nscroll);
3042 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3043 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
3044 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
3045 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
3046 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
3047 2b779855 2020-12-05 naddy select_commit(s);
3048 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
3049 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
3050 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
3051 0bf7f153 2020-12-02 naddy s->commits.ncommits;
3052 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
3055 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3056 444d5325 2022-07-03 thomas case '\r':
3057 07b0611c 2022-06-23 thomas view->count = 0;
3058 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3061 a5d43cac 2022-07-01 thomas /* get dimensions--don't split till initialisation succeeds */
3062 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3063 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
3065 a5d43cac 2022-07-01 thomas err = open_diff_view_for_commit(&diff_view, begin_y, begin_x,
3066 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
3067 78756c87 2020-11-24 stsp view, s->repo);
3071 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3072 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) { /* safe to split */
3073 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
3074 a5d43cac 2022-07-01 thomas if (err)
3078 e78dc838 2020-12-04 stsp view->focussed = 0;
3079 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
3080 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
3081 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
3083 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3084 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3086 1e37a5c2 2019-05-12 jcs return err;
3087 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
3088 40236d76 2022-06-23 thomas if (err)
3089 40236d76 2022-06-23 thomas return err;
3090 e78dc838 2020-12-04 stsp view->focus_child = 1;
3092 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3095 07b0611c 2022-06-23 thomas view->count = 0;
3096 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
3098 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3099 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3100 444d5325 2022-07-03 thomas err = browse_commit_tree(&tree_view, begin_y, begin_x,
3101 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
3105 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3106 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3107 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3108 444d5325 2022-07-03 thomas if (err)
3111 e78dc838 2020-12-04 stsp view->focussed = 0;
3112 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
3113 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
3114 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
3115 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3116 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3118 1e37a5c2 2019-05-12 jcs return err;
3119 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
3120 40236d76 2022-06-23 thomas if (err)
3121 40236d76 2022-06-23 thomas return err;
3122 e78dc838 2020-12-04 stsp view->focus_child = 1;
3124 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
3126 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3127 21355643 2020-12-06 stsp case CTRL('l'):
3129 07b0611c 2022-06-23 thomas view->count = 0;
3130 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
3131 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
3133 21355643 2020-12-06 stsp err = stop_log_thread(s);
3135 74cfe85e 2020-10-20 stsp return err;
3136 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
3137 21355643 2020-12-06 stsp char *parent_path;
3138 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
3140 21355643 2020-12-06 stsp return err;
3141 21355643 2020-12-06 stsp free(s->in_repo_path);
3142 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
3143 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
3144 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
3145 21355643 2020-12-06 stsp struct got_object_id *start_id;
3146 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
3147 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
3148 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
3150 21355643 2020-12-06 stsp return err;
3151 21355643 2020-12-06 stsp free(s->start_id);
3152 21355643 2020-12-06 stsp s->start_id = start_id;
3153 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
3154 21355643 2020-12-06 stsp } else /* 'B' */
3155 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
3157 bf7e79b3 2022-06-23 thomas if (s->thread_args.pack_fds == NULL) {
3158 bf7e79b3 2022-06-23 thomas err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
3159 bf7e79b3 2022-06-23 thomas if (err)
3160 bf7e79b3 2022-06-23 thomas return err;
3162 1af5eddf 2022-06-23 thomas err = got_repo_open(&s->thread_args.repo,
3163 1af5eddf 2022-06-23 thomas got_repo_get_path(s->repo), NULL,
3164 1af5eddf 2022-06-23 thomas s->thread_args.pack_fds);
3166 21355643 2020-12-06 stsp return err;
3167 51a10b52 2020-12-26 stsp tog_free_refs();
3168 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
3170 ca51c541 2020-12-07 stsp return err;
3171 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
3172 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
3174 d01904d4 2019-06-25 stsp return err;
3175 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
3176 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
3178 b672a97a 2020-01-27 stsp return err;
3179 21355643 2020-12-06 stsp free_commits(&s->commits);
3180 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
3181 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
3182 21355643 2020-12-06 stsp s->selected_entry = NULL;
3183 21355643 2020-12-06 stsp s->selected = 0;
3184 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
3185 21355643 2020-12-06 stsp s->quit = 0;
3186 a5d43cac 2022-07-01 thomas s->thread_args.commits_needed = view->lines;
3187 e24d0f15 2022-06-23 thomas s->matched_entry = NULL;
3188 e24d0f15 2022-06-23 thomas s->search_entry = NULL;
3191 07b0611c 2022-06-23 thomas view->count = 0;
3192 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
3193 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
3194 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
3195 6458efa5 2020-11-24 stsp if (ref_view == NULL)
3196 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
3197 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
3198 6458efa5 2020-11-24 stsp if (err) {
3199 6458efa5 2020-11-24 stsp view_close(ref_view);
3200 6458efa5 2020-11-24 stsp return err;
3202 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
3203 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
3204 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
3205 444d5325 2022-07-03 thomas if (err)
3208 e78dc838 2020-12-04 stsp view->focussed = 0;
3209 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
3210 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
3211 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
3212 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
3213 6458efa5 2020-11-24 stsp err = view_close_child(view);
3215 6458efa5 2020-11-24 stsp return err;
3216 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
3217 40236d76 2022-06-23 thomas if (err)
3218 40236d76 2022-06-23 thomas return err;
3219 e78dc838 2020-12-04 stsp view->focus_child = 1;
3221 6458efa5 2020-11-24 stsp *new_view = ref_view;
3224 07b0611c 2022-06-23 thomas view->count = 0;
3228 80ddbec8 2018-04-29 stsp return err;
3231 4ed7e80c 2018-05-20 stsp static const struct got_error *
3232 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
3234 c2db6724 2019-01-04 stsp const struct got_error *error;
3236 37c06ea4 2019-07-15 stsp #ifdef PROFILE
3237 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
3238 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
3240 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
3241 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
3243 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
3244 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
3246 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
3247 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
3249 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
3250 c2db6724 2019-01-04 stsp if (error != NULL)
3251 c2db6724 2019-01-04 stsp return error;
3253 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
3254 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
3256 c2db6724 2019-01-04 stsp return NULL;
3259 a915003a 2019-02-05 stsp static void
3260 a915003a 2019-02-05 stsp init_curses(void)
3263 296152d1 2022-05-31 thomas * Override default signal handlers before starting ncurses.
3264 296152d1 2022-05-31 thomas * This should prevent ncurses from installing its own
3265 296152d1 2022-05-31 thomas * broken cleanup() signal handler.
3267 296152d1 2022-05-31 thomas signal(SIGWINCH, tog_sigwinch);
3268 296152d1 2022-05-31 thomas signal(SIGPIPE, tog_sigpipe);
3269 296152d1 2022-05-31 thomas signal(SIGCONT, tog_sigcont);
3270 296152d1 2022-05-31 thomas signal(SIGINT, tog_sigint);
3271 296152d1 2022-05-31 thomas signal(SIGTERM, tog_sigterm);
3273 a915003a 2019-02-05 stsp initscr();
3275 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3278 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
3279 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
3280 a915003a 2019-02-05 stsp curs_set(0);
3281 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
3282 6d17833f 2019-11-08 stsp start_color();
3283 6d17833f 2019-11-08 stsp use_default_colors();
3287 c2db6724 2019-01-04 stsp static const struct got_error *
3288 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3289 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
3291 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
3293 f135c941 2020-02-20 stsp if (argc == 0) {
3294 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
3295 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
3296 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
3297 f135c941 2020-02-20 stsp return NULL;
3300 f135c941 2020-02-20 stsp if (worktree) {
3301 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3304 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
3306 f135c941 2020-02-20 stsp return err;
3307 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
3308 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3309 bfd61697 2020-11-14 stsp p) == -1) {
3310 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
3311 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
3315 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
3317 f135c941 2020-02-20 stsp return err;
3320 f135c941 2020-02-20 stsp static const struct got_error *
3321 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
3323 80ddbec8 2018-04-29 stsp const struct got_error *error;
3324 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
3325 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
3326 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
3327 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3328 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
3329 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
3330 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
3331 f135c941 2020-02-20 stsp int ch, log_branches = 0;
3332 04cc582a 2018-08-01 stsp struct tog_view *view;
3333 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
3335 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3336 80ddbec8 2018-04-29 stsp switch (ch) {
3338 b672a97a 2020-01-27 stsp log_branches = 1;
3341 80ddbec8 2018-04-29 stsp start_commit = optarg;
3344 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3345 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
3346 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3350 17020d27 2019-03-07 stsp usage_log();
3351 80ddbec8 2018-04-29 stsp /* NOTREACHED */
3355 80ddbec8 2018-04-29 stsp argc -= optind;
3356 80ddbec8 2018-04-29 stsp argv += optind;
3358 f135c941 2020-02-20 stsp if (argc > 1)
3359 f135c941 2020-02-20 stsp usage_log();
3361 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
3362 7cd52833 2022-06-23 thomas if (error != NULL)
3363 7cd52833 2022-06-23 thomas goto done;
3365 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
3366 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3367 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3368 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3369 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3370 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3371 c156c7a4 2020-12-18 stsp goto done;
3372 a1fbf39a 2019-08-11 stsp if (worktree)
3373 6962eb72 2020-02-20 stsp repo_path =
3374 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
3376 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
3377 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3378 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3379 c156c7a4 2020-12-18 stsp goto done;
3383 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3384 80ddbec8 2018-04-29 stsp if (error != NULL)
3385 ecb28ae0 2018-07-16 stsp goto done;
3387 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3388 f135c941 2020-02-20 stsp repo, worktree);
3389 f135c941 2020-02-20 stsp if (error)
3390 f135c941 2020-02-20 stsp goto done;
3392 f135c941 2020-02-20 stsp init_curses();
3394 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
3395 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3396 c02c541e 2019-03-29 stsp if (error)
3397 c02c541e 2019-03-29 stsp goto done;
3399 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
3400 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
3401 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3402 87670572 2020-12-26 naddy if (error)
3403 87670572 2020-12-26 naddy goto done;
3406 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
3407 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
3408 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
3409 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3410 d8f38dc4 2020-12-05 stsp if (error)
3411 d8f38dc4 2020-12-05 stsp goto done;
3412 d8f38dc4 2020-12-05 stsp head_ref_name = label;
3414 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3415 d8f38dc4 2020-12-05 stsp if (error == NULL)
3416 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
3417 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
3418 d8f38dc4 2020-12-05 stsp goto done;
3419 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
3420 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3421 d8f38dc4 2020-12-05 stsp if (error)
3422 d8f38dc4 2020-12-05 stsp goto done;
3425 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3426 04cc582a 2018-08-01 stsp if (view == NULL) {
3427 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3428 04cc582a 2018-08-01 stsp goto done;
3430 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
3431 f135c941 2020-02-20 stsp in_repo_path, log_branches);
3432 ba4f502b 2018-08-04 stsp if (error)
3433 ba4f502b 2018-08-04 stsp goto done;
3434 2fc00ff4 2019-08-31 stsp if (worktree) {
3435 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
3436 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
3437 2fc00ff4 2019-08-31 stsp worktree = NULL;
3439 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3441 f135c941 2020-02-20 stsp free(in_repo_path);
3442 ecb28ae0 2018-07-16 stsp free(repo_path);
3443 ecb28ae0 2018-07-16 stsp free(cwd);
3444 899d86c2 2018-05-10 stsp free(start_id);
3445 d8f38dc4 2020-12-05 stsp free(label);
3447 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
3448 1d0f4054 2021-06-17 stsp if (repo) {
3449 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3450 1d0f4054 2021-06-17 stsp if (error == NULL)
3451 1d0f4054 2021-06-17 stsp error = close_err;
3453 ec142235 2019-03-07 stsp if (worktree)
3454 ec142235 2019-03-07 stsp got_worktree_close(worktree);
3455 7cd52833 2022-06-23 thomas if (pack_fds) {
3456 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
3457 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
3458 7cd52833 2022-06-23 thomas if (error == NULL)
3459 7cd52833 2022-06-23 thomas error = pack_err;
3461 51a10b52 2020-12-26 stsp tog_free_refs();
3462 80ddbec8 2018-04-29 stsp return error;
3465 4ed7e80c 2018-05-20 stsp __dead static void
3466 9f7d7167 2018-04-29 stsp usage_diff(void)
3469 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3470 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
3474 6d17833f 2019-11-08 stsp static int
3475 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
3476 41605754 2020-11-12 stsp regmatch_t *regmatch)
3478 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
3481 ef20f542 2022-06-26 thomas static struct tog_color *
3482 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
3484 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
3486 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
3487 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
3488 6d17833f 2019-11-08 stsp return tc;
3491 6d17833f 2019-11-08 stsp return NULL;
3494 4ed7e80c 2018-05-20 stsp static const struct got_error *
3495 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3496 cbea3800 2022-06-23 thomas WINDOW *window, int skipcol, regmatch_t *regmatch)
3498 41605754 2020-11-12 stsp const struct got_error *err = NULL;
3499 666f7b10 2022-06-23 thomas char *exstr = NULL;
3500 cbea3800 2022-06-23 thomas wchar_t *wline = NULL;
3501 cbea3800 2022-06-23 thomas int rme, rms, n, width, scrollx;
3502 cbea3800 2022-06-23 thomas int width0 = 0, width1 = 0, width2 = 0;
3503 cbea3800 2022-06-23 thomas char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3505 41605754 2020-11-12 stsp *wtotal = 0;
3507 05171be4 2022-06-23 thomas rms = regmatch->rm_so;
3508 05171be4 2022-06-23 thomas rme = regmatch->rm_eo;
3510 666f7b10 2022-06-23 thomas err = expand_tab(&exstr, line);
3511 666f7b10 2022-06-23 thomas if (err)
3512 666f7b10 2022-06-23 thomas return err;
3514 cbea3800 2022-06-23 thomas /* Split the line into 3 segments, according to match offsets. */
3515 666f7b10 2022-06-23 thomas seg0 = strndup(exstr, rms);
3516 666f7b10 2022-06-23 thomas if (seg0 == NULL) {
3517 666f7b10 2022-06-23 thomas err = got_error_from_errno("strndup");
3518 666f7b10 2022-06-23 thomas goto done;
3520 666f7b10 2022-06-23 thomas seg1 = strndup(exstr + rms, rme - rms);
3521 cbea3800 2022-06-23 thomas if (seg1 == NULL) {
3522 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3523 cbea3800 2022-06-23 thomas goto done;
3525 666f7b10 2022-06-23 thomas seg2 = strdup(exstr + rme);
3526 1065461d 2022-06-23 thomas if (seg2 == NULL) {
3527 cbea3800 2022-06-23 thomas err = got_error_from_errno("strndup");
3528 cbea3800 2022-06-23 thomas goto done;
3531 05171be4 2022-06-23 thomas /* draw up to matched token if we haven't scrolled past it */
3532 cbea3800 2022-06-23 thomas err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3533 cbea3800 2022-06-23 thomas col_tab_align, 1);
3534 cbea3800 2022-06-23 thomas if (err)
3535 cbea3800 2022-06-23 thomas goto done;
3536 cbea3800 2022-06-23 thomas n = MAX(width0 - skipcol, 0);
3537 05171be4 2022-06-23 thomas if (n) {
3538 cbea3800 2022-06-23 thomas free(wline);
3539 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3540 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3541 cbea3800 2022-06-23 thomas if (err)
3542 cbea3800 2022-06-23 thomas goto done;
3543 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3544 cbea3800 2022-06-23 thomas wlimit -= width;
3545 cbea3800 2022-06-23 thomas *wtotal += width;
3548 41605754 2020-11-12 stsp if (wlimit > 0) {
3549 cbea3800 2022-06-23 thomas int i = 0, w = 0;
3550 cbea3800 2022-06-23 thomas size_t wlen;
3552 cbea3800 2022-06-23 thomas free(wline);
3553 cbea3800 2022-06-23 thomas err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3554 cbea3800 2022-06-23 thomas col_tab_align, 1);
3555 cbea3800 2022-06-23 thomas if (err)
3556 cbea3800 2022-06-23 thomas goto done;
3557 cbea3800 2022-06-23 thomas wlen = wcslen(wline);
3558 cbea3800 2022-06-23 thomas while (i < wlen) {
3559 cbea3800 2022-06-23 thomas width = wcwidth(wline[i]);
3560 cbea3800 2022-06-23 thomas if (width == -1) {
3561 cbea3800 2022-06-23 thomas /* should not happen, tabs are expanded */
3562 cbea3800 2022-06-23 thomas err = got_error(GOT_ERR_RANGE);
3563 cbea3800 2022-06-23 thomas goto done;
3565 cbea3800 2022-06-23 thomas if (width0 + w + width > skipcol)
3567 1c5e5faa 2022-06-23 thomas w += width;
3570 05171be4 2022-06-23 thomas /* draw (visible part of) matched token (if scrolled into it) */
3571 cbea3800 2022-06-23 thomas if (width1 - w > 0) {
3572 05171be4 2022-06-23 thomas wattron(window, A_STANDOUT);
3573 cbea3800 2022-06-23 thomas waddwstr(window, &wline[i]);
3574 05171be4 2022-06-23 thomas wattroff(window, A_STANDOUT);
3575 cbea3800 2022-06-23 thomas wlimit -= (width1 - w);
3576 cbea3800 2022-06-23 thomas *wtotal += (width1 - w);
3580 cbea3800 2022-06-23 thomas if (wlimit > 0) { /* draw rest of line */
3581 cbea3800 2022-06-23 thomas free(wline);
3582 cbea3800 2022-06-23 thomas if (skipcol > width0 + width1) {
3583 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, &scrollx, seg2,
3584 cbea3800 2022-06-23 thomas skipcol - (width0 + width1), wlimit,
3585 cbea3800 2022-06-23 thomas col_tab_align, 1);
3586 cbea3800 2022-06-23 thomas if (err)
3587 cbea3800 2022-06-23 thomas goto done;
3588 cbea3800 2022-06-23 thomas waddwstr(window, &wline[scrollx]);
3589 cbea3800 2022-06-23 thomas } else {
3590 cbea3800 2022-06-23 thomas err = format_line(&wline, &width2, NULL, seg2, 0,
3591 cbea3800 2022-06-23 thomas wlimit, col_tab_align, 1);
3592 cbea3800 2022-06-23 thomas if (err)
3593 cbea3800 2022-06-23 thomas goto done;
3594 cbea3800 2022-06-23 thomas waddwstr(window, wline);
3596 cbea3800 2022-06-23 thomas *wtotal += width2;
3599 05171be4 2022-06-23 thomas free(wline);
3600 666f7b10 2022-06-23 thomas free(exstr);
3601 cbea3800 2022-06-23 thomas free(seg0);
3602 cbea3800 2022-06-23 thomas free(seg1);
3603 cbea3800 2022-06-23 thomas free(seg2);
3604 cbea3800 2022-06-23 thomas return err;
3607 41605754 2020-11-12 stsp static const struct got_error *
3608 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
3610 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
3611 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3612 61e69b96 2018-05-20 stsp const struct got_error *err;
3613 fe621944 2020-11-10 stsp int nprinted = 0;
3614 b304db33 2018-05-20 stsp char *line;
3615 826082fe 2020-12-10 stsp size_t linesize = 0;
3616 826082fe 2020-12-10 stsp ssize_t linelen;
3617 f26dddb7 2019-11-08 stsp struct tog_color *tc;
3618 61e69b96 2018-05-20 stsp wchar_t *wline;
3619 e0b650dd 2018-05-20 stsp int width;
3620 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
3621 89f1a395 2020-12-01 naddy int nlines = s->nlines;
3622 fe621944 2020-11-10 stsp off_t line_offset;
3624 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
3625 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3626 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3628 f7d12f7e 2018-08-01 stsp werase(view->window);
3630 a3404814 2018-09-02 stsp if (header) {
3631 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3632 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3633 135a2da0 2020-11-11 stsp header) == -1)
3634 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3635 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3637 135a2da0 2020-11-11 stsp free(line);
3639 a3404814 2018-09-02 stsp return err;
3641 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3642 a3404814 2018-09-02 stsp wstandout(view->window);
3643 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3644 e54cc94a 2020-11-11 stsp free(wline);
3645 e54cc94a 2020-11-11 stsp wline = NULL;
3646 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3647 a3404814 2018-09-02 stsp wstandend(view->window);
3648 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3649 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3651 a3404814 2018-09-02 stsp if (max_lines <= 1)
3652 a3404814 2018-09-02 stsp return NULL;
3653 a3404814 2018-09-02 stsp max_lines--;
3656 89f1a395 2020-12-01 naddy s->eof = 0;
3657 05171be4 2022-06-23 thomas view->maxx = 0;
3658 826082fe 2020-12-10 stsp line = NULL;
3659 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3660 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3661 826082fe 2020-12-10 stsp if (linelen == -1) {
3662 826082fe 2020-12-10 stsp if (feof(s->f)) {
3663 826082fe 2020-12-10 stsp s->eof = 1;
3666 826082fe 2020-12-10 stsp free(line);
3667 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3670 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
3671 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3672 cbea3800 2022-06-23 thomas view->x ? 1 : 0);
3673 cbea3800 2022-06-23 thomas if (err) {
3674 cbea3800 2022-06-23 thomas free(line);
3675 cbea3800 2022-06-23 thomas return err;
3677 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
3678 cbea3800 2022-06-23 thomas free(wline);
3679 cbea3800 2022-06-23 thomas wline = NULL;
3681 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3683 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3684 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3685 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3686 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3687 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3688 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
3689 41605754 2020-11-12 stsp if (err) {
3690 41605754 2020-11-12 stsp free(line);
3691 41605754 2020-11-12 stsp return err;
3694 f1c0ec19 2022-06-23 thomas int skip;
3695 f1c0ec19 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
3696 f1c0ec19 2022-06-23 thomas view->x, view->ncols, 0, view->x ? 1 : 0);
3697 f1c0ec19 2022-06-23 thomas if (err) {
3698 f1c0ec19 2022-06-23 thomas free(line);
3699 f1c0ec19 2022-06-23 thomas return err;
3701 f1c0ec19 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
3702 f1c0ec19 2022-06-23 thomas free(wline);
3703 41605754 2020-11-12 stsp wline = NULL;
3706 6d17833f 2019-11-08 stsp wattr_off(view->window,
3707 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3708 f1c0ec19 2022-06-23 thomas if (width <= view->ncols - 1)
3709 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3710 fe621944 2020-11-10 stsp nprinted++;
3712 826082fe 2020-12-10 stsp free(line);
3713 fe621944 2020-11-10 stsp if (nprinted >= 1)
3714 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3715 89f1a395 2020-12-01 naddy (nprinted - 1);
3717 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3719 a5d43cac 2022-07-01 thomas view_border(view);
3721 89f1a395 2020-12-01 naddy if (s->eof) {
3722 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3723 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3724 c3e9aa98 2019-05-13 jcs nprinted++;
3727 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3728 f91a2b48 2022-06-23 thomas view->ncols, 0, 0);
3730 c3e9aa98 2019-05-13 jcs return err;
3733 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3734 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3735 e54cc94a 2020-11-11 stsp free(wline);
3736 e54cc94a 2020-11-11 stsp wline = NULL;
3737 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3740 26ed57b2 2018-05-19 stsp return NULL;
3743 abd2672a 2018-12-23 stsp static char *
3744 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3746 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3747 09867e48 2019-08-13 stsp char *p, *s;
3749 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3750 09867e48 2019-08-13 stsp if (tm == NULL)
3751 09867e48 2019-08-13 stsp return NULL;
3752 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3753 09867e48 2019-08-13 stsp if (s == NULL)
3754 09867e48 2019-08-13 stsp return NULL;
3755 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3757 abd2672a 2018-12-23 stsp *p = '\0';
3761 4ed7e80c 2018-05-20 stsp static const struct got_error *
3762 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3763 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3765 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3766 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3767 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3768 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3770 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3771 0208f208 2020-05-05 stsp if (qid != NULL) {
3772 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3773 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3774 ec242592 2022-04-22 thomas &qid->id);
3776 0208f208 2020-05-05 stsp return err;
3778 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3779 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3780 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3781 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3782 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3784 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3788 0208f208 2020-05-05 stsp if (tree_id1) {
3789 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3791 0208f208 2020-05-05 stsp goto done;
3794 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3795 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3797 0208f208 2020-05-05 stsp goto done;
3799 19a6a6b5 2022-07-01 thomas err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
3800 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3802 0208f208 2020-05-05 stsp if (tree1)
3803 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3804 0208f208 2020-05-05 stsp if (tree2)
3805 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3806 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3807 0208f208 2020-05-05 stsp return err;
3810 0208f208 2020-05-05 stsp static const struct got_error *
3811 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3815 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3816 fe621944 2020-11-10 stsp if (p == NULL)
3817 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3818 fe621944 2020-11-10 stsp *line_offsets = p;
3819 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3820 fe621944 2020-11-10 stsp (*nlines)++;
3821 fe621944 2020-11-10 stsp return NULL;
3824 fe621944 2020-11-10 stsp static const struct got_error *
3825 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3826 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3827 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3829 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3830 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3831 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3832 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3833 45d799e2 2018-12-23 stsp time_t committer_time;
3834 45d799e2 2018-12-23 stsp const char *author, *committer;
3835 8b473291 2019-02-21 stsp char *refs_str = NULL;
3836 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3837 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3838 fe621944 2020-11-10 stsp off_t outoff = 0;
3841 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3843 8b473291 2019-02-21 stsp if (refs) {
3844 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3846 8b473291 2019-02-21 stsp return err;
3849 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3851 abd2672a 2018-12-23 stsp return err;
3853 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3854 15a94983 2018-12-23 stsp if (err) {
3855 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3856 15a94983 2018-12-23 stsp goto done;
3859 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3861 fe621944 2020-11-10 stsp goto done;
3863 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3864 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3865 fe621944 2020-11-10 stsp if (n < 0) {
3866 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3867 abd2672a 2018-12-23 stsp goto done;
3869 fe621944 2020-11-10 stsp outoff += n;
3870 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3872 fe621944 2020-11-10 stsp goto done;
3874 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3875 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3876 fe621944 2020-11-10 stsp if (n < 0) {
3877 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3878 abd2672a 2018-12-23 stsp goto done;
3880 fe621944 2020-11-10 stsp outoff += n;
3881 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3883 fe621944 2020-11-10 stsp goto done;
3885 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3886 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3887 fe621944 2020-11-10 stsp if (datestr) {
3888 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3889 fe621944 2020-11-10 stsp if (n < 0) {
3890 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3891 fe621944 2020-11-10 stsp goto done;
3893 fe621944 2020-11-10 stsp outoff += n;
3894 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3896 fe621944 2020-11-10 stsp goto done;
3898 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3899 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3900 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3901 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3902 fe621944 2020-11-10 stsp if (n < 0) {
3903 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3904 fe621944 2020-11-10 stsp goto done;
3906 fe621944 2020-11-10 stsp outoff += n;
3907 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3909 fe621944 2020-11-10 stsp goto done;
3911 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
3912 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
3913 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
3914 ac4dc263 2021-09-24 thomas int pn = 1;
3915 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
3916 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
3917 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
3918 ac4dc263 2021-09-24 thomas if (err)
3919 ac4dc263 2021-09-24 thomas goto done;
3920 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3921 ac4dc263 2021-09-24 thomas if (n < 0) {
3922 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
3923 ac4dc263 2021-09-24 thomas goto done;
3925 ac4dc263 2021-09-24 thomas outoff += n;
3926 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
3927 ac4dc263 2021-09-24 thomas if (err)
3928 ac4dc263 2021-09-24 thomas goto done;
3929 ac4dc263 2021-09-24 thomas free(id_str);
3930 ac4dc263 2021-09-24 thomas id_str = NULL;
3934 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3936 5943eee2 2019-08-13 stsp goto done;
3937 fe621944 2020-11-10 stsp s = logmsg;
3938 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3939 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3940 fe621944 2020-11-10 stsp if (n < 0) {
3941 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3942 fe621944 2020-11-10 stsp goto done;
3944 fe621944 2020-11-10 stsp outoff += n;
3945 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3947 fe621944 2020-11-10 stsp goto done;
3950 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3952 0208f208 2020-05-05 stsp goto done;
3953 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3954 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3955 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3956 fe621944 2020-11-10 stsp if (n < 0) {
3957 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3958 fe621944 2020-11-10 stsp goto done;
3960 fe621944 2020-11-10 stsp outoff += n;
3961 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3963 fe621944 2020-11-10 stsp goto done;
3964 0208f208 2020-05-05 stsp free((char *)pe->path);
3965 0208f208 2020-05-05 stsp free(pe->data);
3968 0208f208 2020-05-05 stsp fputc('\n', outfile);
3970 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3972 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3973 abd2672a 2018-12-23 stsp free(id_str);
3974 5943eee2 2019-08-13 stsp free(logmsg);
3975 8b473291 2019-02-21 stsp free(refs_str);
3976 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3977 7510f233 2020-08-09 stsp if (err) {
3978 ae6a6978 2020-08-09 stsp free(*line_offsets);
3979 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3980 ae6a6978 2020-08-09 stsp *nlines = 0;
3982 fe621944 2020-11-10 stsp return err;
3985 abd2672a 2018-12-23 stsp static const struct got_error *
3986 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3988 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3989 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3990 15a94983 2018-12-23 stsp int obj_type;
3992 fe621944 2020-11-10 stsp free(s->line_offsets);
3993 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3994 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3995 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3996 fe621944 2020-11-10 stsp s->nlines = 0;
3998 511a516b 2018-05-19 stsp f = got_opentemp();
3999 48ae06ee 2018-10-18 stsp if (f == NULL) {
4000 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4001 48ae06ee 2018-10-18 stsp goto done;
4003 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
4004 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4005 fb43ecf1 2019-02-11 stsp goto done;
4009 15a94983 2018-12-23 stsp if (s->id1)
4010 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
4012 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
4014 15a94983 2018-12-23 stsp goto done;
4016 15a94983 2018-12-23 stsp switch (obj_type) {
4017 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
4018 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
4019 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
4020 adf4c9e0 2022-07-03 thomas s->label1, s->label2, tog_diff_algo, s->diff_context,
4021 19a6a6b5 2022-07-01 thomas s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
4023 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
4024 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
4025 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
4026 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4027 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4029 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
4030 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
4031 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
4032 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
4033 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
4035 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
4037 3ffacbe1 2020-02-02 tracey goto done;
4038 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
4039 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
4040 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
4041 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
4042 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4043 f44b1f58 2020-02-02 tracey if (err)
4044 f44b1f58 2020-02-02 tracey goto done;
4045 f44b1f58 2020-02-02 tracey } else {
4046 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
4047 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
4048 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
4049 fe621944 2020-11-10 stsp err = write_commit_info(
4050 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
4051 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
4052 f44b1f58 2020-02-02 tracey if (err)
4053 f44b1f58 2020-02-02 tracey goto done;
4058 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
4060 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
4061 19a6a6b5 2022-07-01 thomas s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
4062 adf4c9e0 2022-07-03 thomas tog_diff_algo, s->diff_context, s->ignore_whitespace,
4063 25ec7006 2022-07-01 thomas s->force_text_diff, s->repo, s->f);
4067 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4070 f44b1f58 2020-02-02 tracey if (err)
4071 f44b1f58 2020-02-02 tracey goto done;
4073 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
4074 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
4075 48ae06ee 2018-10-18 stsp return err;
4078 f5215bb9 2019-02-22 stsp static void
4079 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
4081 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
4082 f5215bb9 2019-02-22 stsp update_panels();
4083 f5215bb9 2019-02-22 stsp doupdate();
4086 f44b1f58 2020-02-02 tracey static const struct got_error *
4087 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
4089 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4091 f44b1f58 2020-02-02 tracey s->matched_line = 0;
4092 f44b1f58 2020-02-02 tracey return NULL;
4095 f44b1f58 2020-02-02 tracey static const struct got_error *
4096 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
4098 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
4099 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
4100 f44b1f58 2020-02-02 tracey int lineno;
4101 78eecffc 2022-06-23 thomas char *line = NULL;
4102 826082fe 2020-12-10 stsp size_t linesize = 0;
4103 826082fe 2020-12-10 stsp ssize_t linelen;
4105 f44b1f58 2020-02-02 tracey if (!view->searching) {
4106 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4107 f44b1f58 2020-02-02 tracey return NULL;
4110 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4111 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4112 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
4114 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
4116 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
4118 f44b1f58 2020-02-02 tracey while (1) {
4119 f44b1f58 2020-02-02 tracey off_t offset;
4121 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
4122 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
4123 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4127 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4128 f44b1f58 2020-02-02 tracey lineno = 1;
4130 f44b1f58 2020-02-02 tracey lineno = s->nlines;
4133 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
4134 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
4135 f44b1f58 2020-02-02 tracey free(line);
4136 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
4138 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4139 78eecffc 2022-06-23 thomas if (linelen != -1) {
4140 78eecffc 2022-06-23 thomas char *exstr;
4141 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
4142 78eecffc 2022-06-23 thomas if (err)
4144 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
4145 78eecffc 2022-06-23 thomas &view->regmatch)) {
4146 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
4147 78eecffc 2022-06-23 thomas s->matched_line = lineno;
4148 78eecffc 2022-06-23 thomas free(exstr);
4151 78eecffc 2022-06-23 thomas free(exstr);
4153 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
4154 f44b1f58 2020-02-02 tracey lineno++;
4156 f44b1f58 2020-02-02 tracey lineno--;
4158 826082fe 2020-12-10 stsp free(line);
4160 f44b1f58 2020-02-02 tracey if (s->matched_line) {
4161 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
4162 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4165 05171be4 2022-06-23 thomas return err;
4168 48ae06ee 2018-10-18 stsp static const struct got_error *
4169 a0f32f33 2022-06-13 thomas close_diff_view(struct tog_view *view)
4171 a0f32f33 2022-06-13 thomas const struct got_error *err = NULL;
4172 a0f32f33 2022-06-13 thomas struct tog_diff_view_state *s = &view->state.diff;
4174 a0f32f33 2022-06-13 thomas free(s->id1);
4175 a0f32f33 2022-06-13 thomas s->id1 = NULL;
4176 a0f32f33 2022-06-13 thomas free(s->id2);
4177 a0f32f33 2022-06-13 thomas s->id2 = NULL;
4178 a0f32f33 2022-06-13 thomas if (s->f && fclose(s->f) == EOF)
4179 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4180 a0f32f33 2022-06-13 thomas s->f = NULL;
4181 19a6a6b5 2022-07-01 thomas if (s->f1 && fclose(s->f1) == EOF && err == NULL)
4182 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4183 a0f32f33 2022-06-13 thomas s->f1 = NULL;
4184 19a6a6b5 2022-07-01 thomas if (s->f2 && fclose(s->f2) == EOF && err == NULL)
4185 a0f32f33 2022-06-13 thomas err = got_error_from_errno("fclose");
4186 a0f32f33 2022-06-13 thomas s->f2 = NULL;
4187 19a6a6b5 2022-07-01 thomas if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
4188 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4189 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4190 19a6a6b5 2022-07-01 thomas if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
4191 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("close");
4192 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4193 a0f32f33 2022-06-13 thomas free_colors(&s->colors);
4194 a0f32f33 2022-06-13 thomas free(s->line_offsets);
4195 a0f32f33 2022-06-13 thomas s->line_offsets = NULL;
4196 a0f32f33 2022-06-13 thomas s->nlines = 0;
4197 a0f32f33 2022-06-13 thomas return err;
4200 a0f32f33 2022-06-13 thomas static const struct got_error *
4201 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
4202 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
4203 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
4204 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
4206 48ae06ee 2018-10-18 stsp const struct got_error *err;
4207 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
4209 a0f32f33 2022-06-13 thomas memset(s, 0, sizeof(*s));
4210 19a6a6b5 2022-07-01 thomas s->fd1 = -1;
4211 19a6a6b5 2022-07-01 thomas s->fd2 = -1;
4213 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
4214 15a94983 2018-12-23 stsp int type1, type2;
4215 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
4217 15a94983 2018-12-23 stsp return err;
4218 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
4220 15a94983 2018-12-23 stsp return err;
4222 15a94983 2018-12-23 stsp if (type1 != type2)
4223 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
4225 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
4226 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
4227 f44b1f58 2020-02-02 tracey s->selected_line = 1;
4228 f44b1f58 2020-02-02 tracey s->repo = repo;
4229 f44b1f58 2020-02-02 tracey s->id1 = id1;
4230 f44b1f58 2020-02-02 tracey s->id2 = id2;
4231 3dbaef42 2020-11-24 stsp s->label1 = label1;
4232 3dbaef42 2020-11-24 stsp s->label2 = label2;
4234 15a94983 2018-12-23 stsp if (id1) {
4235 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
4236 5465d566 2020-02-01 tracey if (s->id1 == NULL)
4237 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4239 5465d566 2020-02-01 tracey s->id1 = NULL;
4241 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
4242 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
4243 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_object_id_dup");
4244 a0f32f33 2022-06-13 thomas goto done;
4247 9a1d7435 2022-06-23 thomas s->f1 = got_opentemp();
4248 9a1d7435 2022-06-23 thomas if (s->f1 == NULL) {
4249 9a1d7435 2022-06-23 thomas err = got_error_from_errno("got_opentemp");
4250 9a1d7435 2022-06-23 thomas goto done;
4253 a0f32f33 2022-06-13 thomas s->f2 = got_opentemp();
4254 a0f32f33 2022-06-13 thomas if (s->f2 == NULL) {
4255 a0f32f33 2022-06-13 thomas err = got_error_from_errno("got_opentemp");
4256 a0f32f33 2022-06-13 thomas goto done;
4259 19a6a6b5 2022-07-01 thomas s->fd1 = got_opentempfd();
4260 19a6a6b5 2022-07-01 thomas if (s->fd1 == -1) {
4261 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4262 19a6a6b5 2022-07-01 thomas goto done;
4265 19a6a6b5 2022-07-01 thomas s->fd2 = got_opentempfd();
4266 19a6a6b5 2022-07-01 thomas if (s->fd2 == -1) {
4267 19a6a6b5 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4268 19a6a6b5 2022-07-01 thomas goto done;
4271 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
4272 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
4273 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
4274 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
4275 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
4276 5465d566 2020-02-01 tracey s->log_view = log_view;
4277 5465d566 2020-02-01 tracey s->repo = repo;
4279 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4280 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4281 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4282 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
4283 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
4285 a0f32f33 2022-06-13 thomas goto done;
4286 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
4287 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
4288 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
4289 a0f32f33 2022-06-13 thomas if (err)
4290 a0f32f33 2022-06-13 thomas goto done;
4291 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4292 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
4293 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
4294 a0f32f33 2022-06-13 thomas if (err)
4295 a0f32f33 2022-06-13 thomas goto done;
4297 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
4298 9b4458b4 2022-06-26 thomas "^(commit [0-9a-f]|parent [0-9]|"
4299 9b4458b4 2022-06-26 thomas "(blob|file|tree|commit) [-+] |"
4300 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
4301 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
4302 a0f32f33 2022-06-13 thomas if (err)
4303 a0f32f33 2022-06-13 thomas goto done;
4305 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4306 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
4307 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
4308 a0f32f33 2022-06-13 thomas if (err)
4309 a0f32f33 2022-06-13 thomas goto done;
4311 5465d566 2020-02-01 tracey err = add_color(&s->colors,
4312 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
4313 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
4314 a0f32f33 2022-06-13 thomas if (err)
4315 a0f32f33 2022-06-13 thomas goto done;
4318 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
4319 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
4320 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
4322 5465d566 2020-02-01 tracey err = create_diff(s);
4324 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
4325 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
4326 adf4c9e0 2022-07-03 thomas view->reset = reset_diff_view;
4327 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
4328 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
4329 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
4331 a0f32f33 2022-06-13 thomas if (err)
4332 a0f32f33 2022-06-13 thomas close_diff_view(view);
4333 e5a0f69f 2018-08-18 stsp return err;
4336 5dc9f4bc 2018-08-04 stsp static const struct got_error *
4337 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
4339 a3404814 2018-09-02 stsp const struct got_error *err;
4340 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
4341 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
4342 3dbaef42 2020-11-24 stsp const char *label1, *label2;
4344 a3404814 2018-09-02 stsp if (s->id1) {
4345 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
4347 a3404814 2018-09-02 stsp return err;
4348 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
4350 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
4352 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
4354 a3404814 2018-09-02 stsp return err;
4355 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
4357 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4358 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4359 a3404814 2018-09-02 stsp free(id_str1);
4360 a3404814 2018-09-02 stsp free(id_str2);
4361 a3404814 2018-09-02 stsp return err;
4363 a3404814 2018-09-02 stsp free(id_str1);
4364 a3404814 2018-09-02 stsp free(id_str2);
4366 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
4367 267bb3b8 2021-08-01 stsp free(header);
4368 267bb3b8 2021-08-01 stsp return err;
4371 15a087fe 2019-02-21 stsp static const struct got_error *
4372 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
4373 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
4375 d7a04538 2019-02-21 stsp const struct got_error *err;
4376 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
4377 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
4378 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
4380 15a087fe 2019-02-21 stsp free(s->id2);
4381 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
4382 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
4383 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
4385 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4387 d7a04538 2019-02-21 stsp return err;
4388 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
4389 15a087fe 2019-02-21 stsp free(s->id1);
4390 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
4391 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4392 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
4393 15a087fe 2019-02-21 stsp return NULL;
4396 0cf4efb1 2018-09-29 stsp static const struct got_error *
4397 adf4c9e0 2022-07-03 thomas reset_diff_view(struct tog_view *view)
4399 adf4c9e0 2022-07-03 thomas struct tog_diff_view_state *s = &view->state.diff;
4401 adf4c9e0 2022-07-03 thomas view->count = 0;
4402 adf4c9e0 2022-07-03 thomas wclear(view->window);
4403 adf4c9e0 2022-07-03 thomas s->first_displayed_line = 1;
4404 adf4c9e0 2022-07-03 thomas s->last_displayed_line = view->nlines;
4405 adf4c9e0 2022-07-03 thomas s->matched_line = 0;
4406 adf4c9e0 2022-07-03 thomas diff_view_indicate_progress(view);
4407 adf4c9e0 2022-07-03 thomas return create_diff(s);
4410 adf4c9e0 2022-07-03 thomas static const struct got_error *
4411 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4413 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
4414 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
4415 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
4416 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
4417 826082fe 2020-12-10 stsp char *line = NULL;
4418 826082fe 2020-12-10 stsp size_t linesize = 0;
4419 826082fe 2020-12-10 stsp ssize_t linelen;
4420 70f17a53 2022-06-13 thomas int i, nscroll = view->nlines - 1;
4422 e5a0f69f 2018-08-18 stsp switch (ch) {
4423 05171be4 2022-06-23 thomas case '0':
4424 05171be4 2022-06-23 thomas view->x = 0;
4426 05171be4 2022-06-23 thomas case '$':
4427 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
4428 07b0611c 2022-06-23 thomas view->count = 0;
4430 05171be4 2022-06-23 thomas case KEY_RIGHT:
4431 05171be4 2022-06-23 thomas case 'l':
4432 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
4433 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
4435 07b0611c 2022-06-23 thomas view->count = 0;
4437 05171be4 2022-06-23 thomas case KEY_LEFT:
4438 05171be4 2022-06-23 thomas case 'h':
4439 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
4440 07b0611c 2022-06-23 thomas if (view->x <= 0)
4441 07b0611c 2022-06-23 thomas view->count = 0;
4445 3dbaef42 2020-11-24 stsp if (ch == 'a')
4446 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
4447 3dbaef42 2020-11-24 stsp if (ch == 'w')
4448 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
4449 adf4c9e0 2022-07-03 thomas err = reset_diff_view(view);
4452 912a3f79 2021-08-30 j case KEY_HOME:
4453 912a3f79 2021-08-30 j s->first_displayed_line = 1;
4454 07b0611c 2022-06-23 thomas view->count = 0;
4457 912a3f79 2021-08-30 j case KEY_END:
4458 07b0611c 2022-06-23 thomas view->count = 0;
4462 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
4466 1e37a5c2 2019-05-12 jcs case KEY_UP:
4467 f7140bf5 2021-10-17 thomas case CTRL('p'):
4468 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
4469 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4471 07b0611c 2022-06-23 thomas view->count = 0;
4473 70f17a53 2022-06-13 thomas case CTRL('u'):
4474 23427b14 2022-06-23 thomas case 'u':
4475 70f17a53 2022-06-13 thomas nscroll /= 2;
4476 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4477 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4478 a60a9dc4 2019-05-13 jcs case CTRL('b'):
4479 1c5e5faa 2022-06-23 thomas case 'b':
4480 07b0611c 2022-06-23 thomas if (s->first_displayed_line == 1) {
4481 07b0611c 2022-06-23 thomas view->count = 0;
4485 70f17a53 2022-06-13 thomas while (i++ < nscroll && s->first_displayed_line > 1)
4486 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4489 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4490 f7140bf5 2021-10-17 thomas case CTRL('n'):
4491 1e37a5c2 2019-05-12 jcs if (!s->eof)
4492 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4494 07b0611c 2022-06-23 thomas view->count = 0;
4496 70f17a53 2022-06-13 thomas case CTRL('d'):
4497 23427b14 2022-06-23 thomas case 'd':
4498 70f17a53 2022-06-13 thomas nscroll /= 2;
4499 70f17a53 2022-06-13 thomas /* FALL THROUGH */
4500 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4501 a60a9dc4 2019-05-13 jcs case CTRL('f'):
4502 1c5e5faa 2022-06-23 thomas case 'f':
4504 07b0611c 2022-06-23 thomas if (s->eof) {
4505 07b0611c 2022-06-23 thomas view->count = 0;
4509 70f17a53 2022-06-13 thomas while (!s->eof && i++ < nscroll) {
4510 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
4511 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4512 826082fe 2020-12-10 stsp if (linelen == -1) {
4513 826082fe 2020-12-10 stsp if (feof(s->f)) {
4514 826082fe 2020-12-10 stsp s->eof = 1;
4516 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
4520 826082fe 2020-12-10 stsp free(line);
4523 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
4524 1e37a5c2 2019-05-12 jcs s->diff_context--;
4525 aa61903a 2021-12-31 thomas s->matched_line = 0;
4526 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4527 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4528 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
4529 27829c9e 2020-11-21 stsp s->nlines) {
4530 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
4531 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
4534 07b0611c 2022-06-23 thomas view->count = 0;
4537 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4538 1e37a5c2 2019-05-12 jcs s->diff_context++;
4539 aa61903a 2021-12-31 thomas s->matched_line = 0;
4540 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4541 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4543 07b0611c 2022-06-23 thomas view->count = 0;
4547 07b0611c 2022-06-23 thomas if (s->log_view == NULL) {
4548 07b0611c 2022-06-23 thomas view->count = 0;
4551 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4552 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4554 07b0611c 2022-06-23 thomas /* view->count handled in input_log_view() */
4555 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
4559 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4562 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4566 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4567 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4568 aa61903a 2021-12-31 thomas s->matched_line = 0;
4569 05171be4 2022-06-23 thomas view->x = 0;
4571 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4572 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4576 07b0611c 2022-06-23 thomas if (s->log_view == NULL) {
4577 07b0611c 2022-06-23 thomas view->count = 0;
4580 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
4581 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
4583 07b0611c 2022-06-23 thomas /* view->count handled in input_log_view() */
4584 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
4588 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
4591 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
4595 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4596 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
4597 aa61903a 2021-12-31 thomas s->matched_line = 0;
4598 05171be4 2022-06-23 thomas view->x = 0;
4600 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
4601 1e37a5c2 2019-05-12 jcs err = create_diff(s);
4604 07b0611c 2022-06-23 thomas view->count = 0;
4608 bcbd79e2 2018-08-19 stsp return err;
4611 4ed7e80c 2018-05-20 stsp static const struct got_error *
4612 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
4614 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
4615 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
4616 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
4617 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4618 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
4619 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
4620 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
4621 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
4622 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
4623 3dbaef42 2020-11-24 stsp const char *errstr;
4624 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
4625 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
4627 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4628 26ed57b2 2018-05-19 stsp switch (ch) {
4630 64453f7e 2020-11-21 stsp force_text_diff = 1;
4633 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4635 3dbaef42 2020-11-24 stsp if (errstr != NULL)
4636 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
4637 8f666e67 2022-02-12 thomas errstr, errstr);
4639 09b5bff8 2020-02-23 naddy case 'r':
4640 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
4641 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
4642 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
4644 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
4647 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
4650 17020d27 2019-03-07 stsp usage_diff();
4651 26ed57b2 2018-05-19 stsp /* NOTREACHED */
4655 26ed57b2 2018-05-19 stsp argc -= optind;
4656 26ed57b2 2018-05-19 stsp argv += optind;
4658 26ed57b2 2018-05-19 stsp if (argc == 0) {
4659 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
4660 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
4661 15a94983 2018-12-23 stsp id_str1 = argv[0];
4662 15a94983 2018-12-23 stsp id_str2 = argv[1];
4664 26ed57b2 2018-05-19 stsp usage_diff();
4666 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
4667 7cd52833 2022-06-23 thomas if (error)
4668 7cd52833 2022-06-23 thomas goto done;
4670 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
4671 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4672 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4673 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4674 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4675 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4676 c156c7a4 2020-12-18 stsp goto done;
4677 a273ac94 2020-02-23 naddy if (worktree)
4678 a273ac94 2020-02-23 naddy repo_path =
4679 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
4681 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
4682 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4683 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4684 c156c7a4 2020-12-18 stsp goto done;
4688 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4689 eb6600df 2019-01-04 stsp if (error)
4690 eb6600df 2019-01-04 stsp goto done;
4692 a273ac94 2020-02-23 naddy init_curses();
4694 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4695 51a10b52 2020-12-26 stsp if (error)
4696 51a10b52 2020-12-26 stsp goto done;
4698 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4699 26ed57b2 2018-05-19 stsp if (error)
4700 26ed57b2 2018-05-19 stsp goto done;
4702 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4703 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4704 26ed57b2 2018-05-19 stsp if (error)
4705 26ed57b2 2018-05-19 stsp goto done;
4707 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4708 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4709 26ed57b2 2018-05-19 stsp if (error)
4710 26ed57b2 2018-05-19 stsp goto done;
4712 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4713 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
4714 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4715 ea5e7bb5 2018-08-01 stsp goto done;
4717 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4718 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
4719 5dc9f4bc 2018-08-04 stsp if (error)
4720 5dc9f4bc 2018-08-04 stsp goto done;
4721 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4723 3dbaef42 2020-11-24 stsp free(label1);
4724 3dbaef42 2020-11-24 stsp free(label2);
4725 c02c541e 2019-03-29 stsp free(repo_path);
4726 a273ac94 2020-02-23 naddy free(cwd);
4727 1d0f4054 2021-06-17 stsp if (repo) {
4728 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4729 1d0f4054 2021-06-17 stsp if (error == NULL)
4730 1d0f4054 2021-06-17 stsp error = close_err;
4732 a273ac94 2020-02-23 naddy if (worktree)
4733 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
4734 7cd52833 2022-06-23 thomas if (pack_fds) {
4735 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
4736 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
4737 7cd52833 2022-06-23 thomas if (error == NULL)
4738 7cd52833 2022-06-23 thomas error = pack_err;
4740 51a10b52 2020-12-26 stsp tog_free_refs();
4741 26ed57b2 2018-05-19 stsp return error;
4744 4ed7e80c 2018-05-20 stsp __dead static void
4745 9f7d7167 2018-04-29 stsp usage_blame(void)
4748 91198554 2022-06-23 thomas fprintf(stderr,
4749 91198554 2022-06-23 thomas "usage: %s blame [-c commit] [-r repository-path] path\n",
4750 9f7d7167 2018-04-29 stsp getprogname());
4754 84451b3e 2018-07-10 stsp struct tog_blame_line {
4755 84451b3e 2018-07-10 stsp int annotated;
4756 84451b3e 2018-07-10 stsp struct got_object_id *id;
4759 4ed7e80c 2018-05-20 stsp static const struct got_error *
4760 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4762 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4763 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4764 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4765 84451b3e 2018-07-10 stsp const struct got_error *err;
4766 923086fd 2022-06-23 thomas int lineno = 0, nprinted = 0;
4767 826082fe 2020-12-10 stsp char *line = NULL;
4768 826082fe 2020-12-10 stsp size_t linesize = 0;
4769 826082fe 2020-12-10 stsp ssize_t linelen;
4770 84451b3e 2018-07-10 stsp wchar_t *wline;
4771 27a741e5 2019-09-11 stsp int width;
4772 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4773 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4774 ab089a2a 2018-07-12 stsp char *id_str;
4775 11b20872 2019-11-08 stsp struct tog_color *tc;
4777 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
4779 ab089a2a 2018-07-12 stsp return err;
4781 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4782 f7d12f7e 2018-08-01 stsp werase(view->window);
4784 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4785 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4786 ab089a2a 2018-07-12 stsp free(id_str);
4787 ab089a2a 2018-07-12 stsp return err;
4790 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4791 ab089a2a 2018-07-12 stsp free(line);
4792 2550e4c3 2018-07-13 stsp line = NULL;
4794 1cae65b4 2019-09-22 stsp return err;
4795 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4796 a3404814 2018-09-02 stsp wstandout(view->window);
4797 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4799 11b20872 2019-11-08 stsp wattr_on(view->window,
4800 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4801 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4803 11b20872 2019-11-08 stsp wattr_off(view->window,
4804 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4805 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4806 a3404814 2018-09-02 stsp wstandend(view->window);
4807 2550e4c3 2018-07-13 stsp free(wline);
4808 2550e4c3 2018-07-13 stsp wline = NULL;
4809 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4810 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4812 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4813 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4814 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4815 ab089a2a 2018-07-12 stsp free(id_str);
4816 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4818 ab089a2a 2018-07-12 stsp free(id_str);
4819 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4820 3f60a8ef 2018-07-10 stsp free(line);
4821 2550e4c3 2018-07-13 stsp line = NULL;
4823 3f60a8ef 2018-07-10 stsp return err;
4824 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4825 2550e4c3 2018-07-13 stsp free(wline);
4826 2550e4c3 2018-07-13 stsp wline = NULL;
4827 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4828 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4830 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4831 05171be4 2022-06-23 thomas view->maxx = 0;
4832 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4833 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4834 826082fe 2020-12-10 stsp if (linelen == -1) {
4835 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4836 826082fe 2020-12-10 stsp s->eof = 1;
4839 84451b3e 2018-07-10 stsp free(line);
4840 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4842 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4845 cbea3800 2022-06-23 thomas /* Set view->maxx based on full line length. */
4846 cbea3800 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4847 cbea3800 2022-06-23 thomas if (err) {
4848 cbea3800 2022-06-23 thomas free(line);
4849 cbea3800 2022-06-23 thomas return err;
4851 cbea3800 2022-06-23 thomas free(wline);
4852 cbea3800 2022-06-23 thomas wline = NULL;
4853 cbea3800 2022-06-23 thomas view->maxx = MAX(view->maxx, width);
4855 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4856 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4858 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4859 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4860 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4861 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4862 27a741e5 2019-09-11 stsp !(view->focussed &&
4863 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4864 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4865 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4866 8d0fe45a 2019-08-12 stsp char *id_str;
4867 91198554 2022-06-23 thomas err = got_object_id_str(&id_str,
4868 91198554 2022-06-23 thomas blame_line->id);
4869 8d0fe45a 2019-08-12 stsp if (err) {
4870 8d0fe45a 2019-08-12 stsp free(line);
4871 8d0fe45a 2019-08-12 stsp return err;
4873 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4875 11b20872 2019-11-08 stsp wattr_on(view->window,
4876 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4877 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4879 11b20872 2019-11-08 stsp wattr_off(view->window,
4880 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4881 8d0fe45a 2019-08-12 stsp free(id_str);
4882 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4884 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4885 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4888 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4889 ee41ec32 2018-07-10 stsp prev_id = NULL;
4892 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4893 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4894 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4896 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4897 41605754 2020-11-12 stsp width = 9;
4898 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4899 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4900 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4901 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4902 05171be4 2022-06-23 thomas view->window, view->x, regmatch);
4903 41605754 2020-11-12 stsp if (err) {
4904 41605754 2020-11-12 stsp free(line);
4905 41605754 2020-11-12 stsp return err;
4907 41605754 2020-11-12 stsp width += 9;
4909 923086fd 2022-06-23 thomas int skip;
4910 923086fd 2022-06-23 thomas err = format_line(&wline, &width, &skip, line,
4911 923086fd 2022-06-23 thomas view->x, view->ncols - 9, 9, 1);
4912 923086fd 2022-06-23 thomas if (err) {
4913 923086fd 2022-06-23 thomas free(line);
4914 923086fd 2022-06-23 thomas return err;
4916 923086fd 2022-06-23 thomas waddwstr(view->window, &wline[skip]);
4917 02bce7e2 2022-06-23 thomas width += 9;
4918 41605754 2020-11-12 stsp free(wline);
4919 41605754 2020-11-12 stsp wline = NULL;
4922 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4923 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4924 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4925 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4927 826082fe 2020-12-10 stsp free(line);
4928 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4930 a5d43cac 2022-07-01 thomas view_border(view);
4932 84451b3e 2018-07-10 stsp return NULL;
4935 84451b3e 2018-07-10 stsp static const struct got_error *
4936 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
4937 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
4939 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4940 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4941 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4942 1a76625f 2018-10-22 stsp int errcode;
4944 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4945 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4946 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4948 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4949 1a76625f 2018-10-22 stsp if (errcode)
4950 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4952 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4953 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4954 d68a0a7d 2018-07-10 stsp goto done;
4957 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4958 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4960 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4961 d68a0a7d 2018-07-10 stsp if (line->annotated)
4962 d68a0a7d 2018-07-10 stsp goto done;
4964 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4965 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4966 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4967 84451b3e 2018-07-10 stsp goto done;
4969 84451b3e 2018-07-10 stsp line->annotated = 1;
4971 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4972 1a76625f 2018-10-22 stsp if (errcode)
4973 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4974 84451b3e 2018-07-10 stsp return err;
4977 84451b3e 2018-07-10 stsp static void *
4978 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4980 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4981 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4982 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4983 9117a7b7 2022-07-01 thomas int errcode, fd1 = -1, fd2 = -1;
4984 9117a7b7 2022-07-01 thomas FILE *f1 = NULL, *f2 = NULL;
4986 9117a7b7 2022-07-01 thomas fd1 = got_opentempfd();
4987 9117a7b7 2022-07-01 thomas if (fd1 == -1)
4988 00a8878e 2022-07-01 thomas return (void *)got_error_from_errno("got_opentempfd");
4990 9117a7b7 2022-07-01 thomas fd2 = got_opentempfd();
4991 9117a7b7 2022-07-01 thomas if (fd2 == -1) {
4992 9117a7b7 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
4993 9117a7b7 2022-07-01 thomas goto done;
4996 9117a7b7 2022-07-01 thomas f1 = got_opentemp();
4997 9117a7b7 2022-07-01 thomas if (f1 == NULL) {
4998 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
4999 9117a7b7 2022-07-01 thomas goto done;
5001 9117a7b7 2022-07-01 thomas f2 = got_opentemp();
5002 9117a7b7 2022-07-01 thomas if (f2 == NULL) {
5003 9117a7b7 2022-07-01 thomas err = (void *)got_error_from_errno("got_opentemp");
5004 9117a7b7 2022-07-01 thomas goto done;
5007 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
5009 9117a7b7 2022-07-01 thomas goto done;
5011 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
5012 adf4c9e0 2022-07-03 thomas tog_diff_algo, blame_cb, ta->cb_args,
5013 25ec7006 2022-07-01 thomas ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1, f2);
5014 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
5015 fc06ba56 2019-08-22 stsp err = NULL;
5017 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5018 9117a7b7 2022-07-01 thomas if (errcode) {
5019 9117a7b7 2022-07-01 thomas err = got_error_set_errno(errcode, "pthread_mutex_lock");
5020 9117a7b7 2022-07-01 thomas goto done;
5023 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
5024 1d0f4054 2021-06-17 stsp if (err == NULL)
5025 1d0f4054 2021-06-17 stsp err = close_err;
5026 c9beca56 2018-07-22 stsp ta->repo = NULL;
5027 c9beca56 2018-07-22 stsp *ta->complete = 1;
5029 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5030 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
5031 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
5034 9117a7b7 2022-07-01 thomas if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5035 00a8878e 2022-07-01 thomas err = got_error_from_errno("close");
5036 9117a7b7 2022-07-01 thomas if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5037 9117a7b7 2022-07-01 thomas err = got_error_from_errno("close");
5038 9117a7b7 2022-07-01 thomas if (f1 && fclose(f1) == EOF && err == NULL)
5039 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5040 9117a7b7 2022-07-01 thomas if (f2 && fclose(f2) == EOF && err == NULL)
5041 9117a7b7 2022-07-01 thomas err = got_error_from_errno("fclose");
5043 18430de3 2018-07-10 stsp return (void *)err;
5046 245d91c1 2018-07-12 stsp static struct got_object_id *
5047 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
5048 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
5050 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
5052 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
5053 8d0fe45a 2019-08-12 stsp return NULL;
5055 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
5056 245d91c1 2018-07-12 stsp if (!line->annotated)
5057 245d91c1 2018-07-12 stsp return NULL;
5059 245d91c1 2018-07-12 stsp return line->id;
5062 b880a918 2018-07-10 stsp static const struct got_error *
5063 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
5065 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
5068 245d91c1 2018-07-12 stsp if (blame->thread) {
5069 1a76625f 2018-10-22 stsp int errcode;
5070 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5071 1a76625f 2018-10-22 stsp if (errcode)
5072 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5073 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
5074 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
5075 1a76625f 2018-10-22 stsp if (errcode)
5076 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
5077 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5078 1a76625f 2018-10-22 stsp if (errcode)
5079 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
5080 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
5081 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
5082 245d91c1 2018-07-12 stsp err = NULL;
5083 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
5085 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
5086 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
5087 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
5088 1d0f4054 2021-06-17 stsp if (err == NULL)
5089 1d0f4054 2021-06-17 stsp err = close_err;
5090 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
5092 245d91c1 2018-07-12 stsp if (blame->f) {
5093 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
5094 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
5095 245d91c1 2018-07-12 stsp blame->f = NULL;
5097 57670559 2018-12-23 stsp if (blame->lines) {
5098 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
5099 57670559 2018-12-23 stsp free(blame->lines[i].id);
5100 57670559 2018-12-23 stsp free(blame->lines);
5101 57670559 2018-12-23 stsp blame->lines = NULL;
5103 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
5104 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
5105 7cd52833 2022-06-23 thomas if (blame->pack_fds) {
5106 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5107 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(blame->pack_fds);
5108 7cd52833 2022-06-23 thomas if (err == NULL)
5109 7cd52833 2022-06-23 thomas err = pack_err;
5110 ece63358 2022-06-23 thomas blame->pack_fds = NULL;
5112 245d91c1 2018-07-12 stsp return err;
5115 245d91c1 2018-07-12 stsp static const struct got_error *
5116 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
5118 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
5119 fc06ba56 2019-08-22 stsp int *done = arg;
5120 fc06ba56 2019-08-22 stsp int errcode;
5122 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
5123 fc06ba56 2019-08-22 stsp if (errcode)
5124 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5125 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
5127 fc06ba56 2019-08-22 stsp if (*done)
5128 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
5130 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
5131 fc06ba56 2019-08-22 stsp if (errcode)
5132 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
5133 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
5135 fc06ba56 2019-08-22 stsp return err;
5138 fc06ba56 2019-08-22 stsp static const struct got_error *
5139 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
5141 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
5142 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
5143 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
5144 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5145 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
5146 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
5147 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
5148 f4ae6ddb 2022-07-01 thomas int obj_type, fd = -1;
5149 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5151 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
5152 ec242592 2022-04-22 thomas &s->blamed_commit->id);
5154 15a94983 2018-12-23 stsp return err;
5156 f4ae6ddb 2022-07-01 thomas fd = got_opentempfd();
5157 f4ae6ddb 2022-07-01 thomas if (fd == -1) {
5158 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("got_opentempfd");
5159 f4ae6ddb 2022-07-01 thomas goto done;
5162 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
5163 945f9229 2022-04-16 thomas if (err)
5164 945f9229 2022-04-16 thomas goto done;
5166 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
5168 84451b3e 2018-07-10 stsp goto done;
5170 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5171 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5172 84451b3e 2018-07-10 stsp goto done;
5175 f4ae6ddb 2022-07-01 thomas err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
5177 a70480e0 2018-06-23 stsp goto done;
5178 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
5179 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
5180 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
5181 84451b3e 2018-07-10 stsp goto done;
5183 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
5184 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
5186 1fddf795 2021-01-20 stsp goto done;
5187 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
5188 1fddf795 2021-01-20 stsp s->blame_complete = 1;
5189 84451b3e 2018-07-10 stsp goto done;
5192 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5193 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
5194 b02560ec 2019-08-19 stsp blame->nlines--;
5196 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
5197 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
5198 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
5199 84451b3e 2018-07-10 stsp goto done;
5202 7cd52833 2022-06-23 thomas err = got_repo_pack_fds_open(&pack_fds);
5204 bd24772e 2018-07-11 stsp goto done;
5205 7cd52833 2022-06-23 thomas err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
5206 7cd52833 2022-06-23 thomas pack_fds);
5207 7cd52833 2022-06-23 thomas if (err)
5208 7cd52833 2022-06-23 thomas goto done;
5210 7cd52833 2022-06-23 thomas blame->pack_fds = pack_fds;
5211 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
5212 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
5213 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
5214 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
5215 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
5216 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5217 245d91c1 2018-07-12 stsp goto done;
5219 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
5221 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
5222 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
5223 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
5224 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
5225 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
5226 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
5227 a5388363 2020-12-01 naddy s->blame_complete = 0;
5229 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
5230 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
5231 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
5232 f5a09613 2020-12-13 naddy s->selected_line = 1;
5234 aa61903a 2021-12-31 thomas s->matched_line = 0;
5237 945f9229 2022-04-16 thomas if (commit)
5238 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5239 f4ae6ddb 2022-07-01 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
5240 f4ae6ddb 2022-07-01 thomas err = got_error_from_errno("close");
5242 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
5243 27d434c2 2018-09-15 stsp free(obj_id);
5245 245d91c1 2018-07-12 stsp stop_blame(blame);
5246 245d91c1 2018-07-12 stsp return err;
5249 245d91c1 2018-07-12 stsp static const struct got_error *
5250 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
5251 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5253 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
5254 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5256 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
5258 c4843652 2019-08-12 stsp s->path = strdup(path);
5259 c4843652 2019-08-12 stsp if (s->path == NULL)
5260 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
5262 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
5263 c4843652 2019-08-12 stsp if (err) {
5264 c4843652 2019-08-12 stsp free(s->path);
5265 7cbe629d 2018-08-04 stsp return err;
5268 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
5269 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
5270 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
5271 fb2756b9 2018-08-04 stsp s->selected_line = 1;
5272 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
5273 fb2756b9 2018-08-04 stsp s->repo = repo;
5274 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
5275 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
5277 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5278 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5279 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
5280 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5282 11b20872 2019-11-08 stsp return err;
5285 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
5286 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
5287 adf4c9e0 2022-07-03 thomas view->reset = reset_blame_view;
5288 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
5289 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
5290 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
5292 a5388363 2020-12-01 naddy return run_blame(view);
5295 e5a0f69f 2018-08-18 stsp static const struct got_error *
5296 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
5298 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5299 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5301 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
5302 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
5304 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
5305 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
5306 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
5307 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5308 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
5311 e5a0f69f 2018-08-18 stsp free(s->path);
5312 11b20872 2019-11-08 stsp free_colors(&s->colors);
5313 e5a0f69f 2018-08-18 stsp return err;
5316 7cbe629d 2018-08-04 stsp static const struct got_error *
5317 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
5319 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5321 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
5322 6c4c42e0 2019-06-24 stsp return NULL;
5325 6c4c42e0 2019-06-24 stsp static const struct got_error *
5326 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
5328 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
5329 05171be4 2022-06-23 thomas const struct got_error *err = NULL;
5330 6c4c42e0 2019-06-24 stsp int lineno;
5331 78eecffc 2022-06-23 thomas char *line = NULL;
5332 826082fe 2020-12-10 stsp size_t linesize = 0;
5333 826082fe 2020-12-10 stsp ssize_t linelen;
5335 6c4c42e0 2019-06-24 stsp if (!view->searching) {
5336 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5337 6c4c42e0 2019-06-24 stsp return NULL;
5340 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5341 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5342 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
5344 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
5346 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
5348 6c4c42e0 2019-06-24 stsp while (1) {
5349 6c4c42e0 2019-06-24 stsp off_t offset;
5351 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
5352 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
5353 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5357 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5358 6c4c42e0 2019-06-24 stsp lineno = 1;
5360 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
5363 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
5364 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5365 6c4c42e0 2019-06-24 stsp free(line);
5366 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
5368 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
5369 78eecffc 2022-06-23 thomas if (linelen != -1) {
5370 78eecffc 2022-06-23 thomas char *exstr;
5371 78eecffc 2022-06-23 thomas err = expand_tab(&exstr, line);
5372 78eecffc 2022-06-23 thomas if (err)
5374 78eecffc 2022-06-23 thomas if (match_line(exstr, &view->regex, 1,
5375 78eecffc 2022-06-23 thomas &view->regmatch)) {
5376 78eecffc 2022-06-23 thomas view->search_next_done = TOG_SEARCH_HAVE_MORE;
5377 78eecffc 2022-06-23 thomas s->matched_line = lineno;
5378 78eecffc 2022-06-23 thomas free(exstr);
5381 78eecffc 2022-06-23 thomas free(exstr);
5383 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5388 826082fe 2020-12-10 stsp free(line);
5390 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
5391 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
5392 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
5395 05171be4 2022-06-23 thomas return err;
5398 6c4c42e0 2019-06-24 stsp static const struct got_error *
5399 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
5401 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5402 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
5403 2b380cc8 2018-10-24 stsp int errcode;
5405 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
5406 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5407 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
5408 2b380cc8 2018-10-24 stsp if (errcode)
5409 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
5411 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
5414 51fe7530 2019-08-19 stsp if (s->blame_complete)
5415 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
5417 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
5419 a5d43cac 2022-07-01 thomas view_border(view);
5420 e5a0f69f 2018-08-18 stsp return err;
5423 e5a0f69f 2018-08-18 stsp static const struct got_error *
5424 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5426 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
5427 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
5428 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
5429 a5d43cac 2022-07-01 thomas int eos, nscroll, begin_y = 0, begin_x = 0;
5431 a5d43cac 2022-07-01 thomas eos = nscroll = view->nlines - 2;
5432 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
5433 a5d43cac 2022-07-01 thomas --eos; /* border */
5435 e5a0f69f 2018-08-18 stsp switch (ch) {
5436 05171be4 2022-06-23 thomas case '0':
5437 05171be4 2022-06-23 thomas view->x = 0;
5439 05171be4 2022-06-23 thomas case '$':
5440 05171be4 2022-06-23 thomas view->x = MAX(view->maxx - view->ncols / 3, 0);
5441 07b0611c 2022-06-23 thomas view->count = 0;
5443 05171be4 2022-06-23 thomas case KEY_RIGHT:
5444 05171be4 2022-06-23 thomas case 'l':
5445 05171be4 2022-06-23 thomas if (view->x + view->ncols / 3 < view->maxx)
5446 05171be4 2022-06-23 thomas view->x += 2; /* move two columns right */
5448 07b0611c 2022-06-23 thomas view->count = 0;
5450 05171be4 2022-06-23 thomas case KEY_LEFT:
5451 05171be4 2022-06-23 thomas case 'h':
5452 05171be4 2022-06-23 thomas view->x -= MIN(view->x, 2); /* move two columns back */
5453 07b0611c 2022-06-23 thomas if (view->x <= 0)
5454 07b0611c 2022-06-23 thomas view->count = 0;
5457 1e37a5c2 2019-05-12 jcs s->done = 1;
5459 4deef56f 2021-09-02 naddy case 'g':
5460 4deef56f 2021-09-02 naddy case KEY_HOME:
5461 4deef56f 2021-09-02 naddy s->selected_line = 1;
5462 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5463 07b0611c 2022-06-23 thomas view->count = 0;
5465 4deef56f 2021-09-02 naddy case 'G':
5466 4deef56f 2021-09-02 naddy case KEY_END:
5467 a5d43cac 2022-07-01 thomas if (s->blame.nlines < eos) {
5468 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
5469 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
5471 a5d43cac 2022-07-01 thomas s->selected_line = eos;
5472 a5d43cac 2022-07-01 thomas s->first_displayed_line = s->blame.nlines - (eos - 1);
5474 07b0611c 2022-06-23 thomas view->count = 0;
5477 1e37a5c2 2019-05-12 jcs case KEY_UP:
5478 f7140bf5 2021-10-17 thomas case CTRL('p'):
5479 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
5480 1e37a5c2 2019-05-12 jcs s->selected_line--;
5481 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
5482 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
5483 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
5485 07b0611c 2022-06-23 thomas view->count = 0;
5487 70f17a53 2022-06-13 thomas case CTRL('u'):
5488 23427b14 2022-06-23 thomas case 'u':
5489 70f17a53 2022-06-13 thomas nscroll /= 2;
5490 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5491 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5492 ea025d1d 2020-02-22 naddy case CTRL('b'):
5493 1c5e5faa 2022-06-23 thomas case 'b':
5494 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
5495 07b0611c 2022-06-23 thomas if (view->count > 1)
5496 07b0611c 2022-06-23 thomas nscroll += nscroll;
5497 70f17a53 2022-06-13 thomas s->selected_line = MAX(1, s->selected_line - nscroll);
5498 07b0611c 2022-06-23 thomas view->count = 0;
5501 70f17a53 2022-06-13 thomas if (s->first_displayed_line > nscroll)
5502 70f17a53 2022-06-13 thomas s->first_displayed_line -= nscroll;
5504 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
5507 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5508 f7140bf5 2021-10-17 thomas case CTRL('n'):
5509 a5d43cac 2022-07-01 thomas if (s->selected_line < eos && s->first_displayed_line +
5510 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
5511 1e37a5c2 2019-05-12 jcs s->selected_line++;
5512 a5d43cac 2022-07-01 thomas else if (s->first_displayed_line < s->blame.nlines - (eos - 1))
5513 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
5515 07b0611c 2022-06-23 thomas view->count = 0;
5517 1c5e5faa 2022-06-23 thomas case 'c':
5518 1e37a5c2 2019-05-12 jcs case 'p': {
5519 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5521 07b0611c 2022-06-23 thomas view->count = 0;
5522 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5523 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5524 1e37a5c2 2019-05-12 jcs if (id == NULL)
5526 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
5527 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
5528 15a94983 2018-12-23 stsp struct got_object_qid *pid;
5529 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
5530 1e37a5c2 2019-05-12 jcs int obj_type;
5531 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
5532 1e37a5c2 2019-05-12 jcs s->repo, id);
5535 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
5536 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
5537 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
5538 15a94983 2018-12-23 stsp got_object_commit_close(commit);
5541 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
5542 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
5543 ec242592 2022-04-22 thomas s->repo, &pid->id);
5544 945f9229 2022-04-16 thomas if (err)
5546 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
5547 945f9229 2022-04-16 thomas pcommit, s->path);
5548 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
5549 e5a0f69f 2018-08-18 stsp if (err) {
5550 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
5551 1e37a5c2 2019-05-12 jcs err = NULL;
5552 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5555 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
5557 1e37a5c2 2019-05-12 jcs free(blob_id);
5558 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
5559 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
5560 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5563 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5564 ec242592 2022-04-22 thomas &pid->id);
5565 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5567 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
5568 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
5570 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
5575 1e37a5c2 2019-05-12 jcs s->done = 1;
5576 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5577 1e37a5c2 2019-05-12 jcs s->done = 0;
5578 1e37a5c2 2019-05-12 jcs if (thread_err)
5580 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
5581 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
5582 a5388363 2020-12-01 naddy err = run_blame(view);
5587 1c5e5faa 2022-06-23 thomas case 'C': {
5588 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
5590 07b0611c 2022-06-23 thomas view->count = 0;
5591 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
5592 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
5594 1e37a5c2 2019-05-12 jcs s->done = 1;
5595 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
5596 1e37a5c2 2019-05-12 jcs s->done = 0;
5597 1e37a5c2 2019-05-12 jcs if (thread_err)
5599 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5600 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
5601 1e37a5c2 2019-05-12 jcs s->blamed_commit =
5602 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
5603 a5388363 2020-12-01 naddy err = run_blame(view);
5608 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5609 1e37a5c2 2019-05-12 jcs case '\r': {
5610 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
5611 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
5612 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
5614 07b0611c 2022-06-23 thomas view->count = 0;
5615 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5616 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
5617 1e37a5c2 2019-05-12 jcs if (id == NULL)
5619 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
5622 a5d43cac 2022-07-01 thomas pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
5623 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5624 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
5626 a5d43cac 2022-07-01 thomas diff_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_DIFF);
5627 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
5628 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5629 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
5632 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5633 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5634 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
5636 1e37a5c2 2019-05-12 jcs view_close(diff_view);
5639 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
5640 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
5641 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
5642 a5d43cac 2022-07-01 thomas if (err)
5646 e78dc838 2020-12-04 stsp view->focussed = 0;
5647 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
5648 a5d43cac 2022-07-01 thomas diff_view->mode = view->mode;
5649 a5d43cac 2022-07-01 thomas diff_view->nlines = view->lines - begin_y;
5650 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5651 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5654 40236d76 2022-06-23 thomas err = view_set_child(view, diff_view);
5655 40236d76 2022-06-23 thomas if (err)
5657 e78dc838 2020-12-04 stsp view->focus_child = 1;
5659 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
5664 70f17a53 2022-06-13 thomas case CTRL('d'):
5665 23427b14 2022-06-23 thomas case 'd':
5666 70f17a53 2022-06-13 thomas nscroll /= 2;
5667 70f17a53 2022-06-13 thomas /* FALL THROUGH */
5668 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5669 ea025d1d 2020-02-22 naddy case CTRL('f'):
5670 1c5e5faa 2022-06-23 thomas case 'f':
5672 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5673 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
5674 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
5675 07b0611c 2022-06-23 thomas view->count = 0;
5678 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
5679 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
5680 70f17a53 2022-06-13 thomas s->selected_line +=
5681 70f17a53 2022-06-13 thomas MIN(nscroll, s->last_displayed_line -
5682 70f17a53 2022-06-13 thomas s->first_displayed_line - s->selected_line + 1);
5684 70f17a53 2022-06-13 thomas if (s->last_displayed_line + nscroll <= s->blame.nlines)
5685 70f17a53 2022-06-13 thomas s->first_displayed_line += nscroll;
5687 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
5688 70f17a53 2022-06-13 thomas s->blame.nlines - (view->nlines - 3);
5690 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5691 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
5692 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
5693 1e37a5c2 2019-05-12 jcs view->nlines - 2);
5697 07b0611c 2022-06-23 thomas view->count = 0;
5700 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
5703 adf4c9e0 2022-07-03 thomas static const struct got_error *
5704 adf4c9e0 2022-07-03 thomas reset_blame_view(struct tog_view *view)
5706 adf4c9e0 2022-07-03 thomas const struct got_error *err;
5707 adf4c9e0 2022-07-03 thomas struct tog_blame_view_state *s = &view->state.blame;
5709 adf4c9e0 2022-07-03 thomas view->count = 0;
5710 adf4c9e0 2022-07-03 thomas s->done = 1;
5711 adf4c9e0 2022-07-03 thomas err = stop_blame(&s->blame);
5712 adf4c9e0 2022-07-03 thomas s->done = 0;
5713 adf4c9e0 2022-07-03 thomas if (err)
5714 adf4c9e0 2022-07-03 thomas return err;
5715 adf4c9e0 2022-07-03 thomas return run_blame(view);
5718 a70480e0 2018-06-23 stsp static const struct got_error *
5719 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
5721 a70480e0 2018-06-23 stsp const struct got_error *error;
5722 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
5723 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
5724 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5725 0587e10c 2020-07-23 stsp char *link_target = NULL;
5726 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5727 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5728 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
5730 e1cd8fed 2018-08-01 stsp struct tog_view *view;
5731 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
5733 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5734 a70480e0 2018-06-23 stsp switch (ch) {
5736 a70480e0 2018-06-23 stsp commit_id_str = optarg;
5739 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
5740 69069811 2018-08-02 stsp if (repo_path == NULL)
5741 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5745 17020d27 2019-03-07 stsp usage_blame();
5746 a70480e0 2018-06-23 stsp /* NOTREACHED */
5750 a70480e0 2018-06-23 stsp argc -= optind;
5751 a70480e0 2018-06-23 stsp argv += optind;
5753 f135c941 2020-02-20 stsp if (argc != 1)
5754 a70480e0 2018-06-23 stsp usage_blame();
5756 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
5757 7cd52833 2022-06-23 thomas if (error != NULL)
5758 7cd52833 2022-06-23 thomas goto done;
5760 69069811 2018-08-02 stsp if (repo_path == NULL) {
5761 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5762 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5763 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5764 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5765 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5766 c156c7a4 2020-12-18 stsp goto done;
5767 f135c941 2020-02-20 stsp if (worktree)
5768 eb41ed75 2019-02-05 stsp repo_path =
5769 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5771 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
5772 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5773 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5774 c156c7a4 2020-12-18 stsp goto done;
5778 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5779 c02c541e 2019-03-29 stsp if (error != NULL)
5780 8e94dd5b 2019-01-04 stsp goto done;
5782 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5783 f135c941 2020-02-20 stsp worktree);
5784 c02c541e 2019-03-29 stsp if (error)
5785 92205607 2019-01-04 stsp goto done;
5787 f135c941 2020-02-20 stsp init_curses();
5789 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5790 51a10b52 2020-12-26 stsp if (error)
5791 51a10b52 2020-12-26 stsp goto done;
5793 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5794 eb41ed75 2019-02-05 stsp if (error)
5795 69069811 2018-08-02 stsp goto done;
5797 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
5798 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
5799 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5800 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5801 a70480e0 2018-06-23 stsp if (error != NULL)
5802 66b4983c 2018-06-23 stsp goto done;
5803 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5804 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
5806 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5807 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5809 a19e88aa 2018-06-23 stsp if (error != NULL)
5810 8b473291 2019-02-21 stsp goto done;
5812 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5813 e1cd8fed 2018-08-01 stsp if (view == NULL) {
5814 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5815 e1cd8fed 2018-08-01 stsp goto done;
5818 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
5819 945f9229 2022-04-16 thomas if (error)
5820 945f9229 2022-04-16 thomas goto done;
5822 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5823 945f9229 2022-04-16 thomas commit, repo);
5824 7cbe629d 2018-08-04 stsp if (error)
5825 7cbe629d 2018-08-04 stsp goto done;
5827 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
5828 78756c87 2020-11-24 stsp commit_id, repo);
5829 0587e10c 2020-07-23 stsp if (error)
5830 0587e10c 2020-07-23 stsp goto done;
5831 12314ad4 2019-08-31 stsp if (worktree) {
5832 12314ad4 2019-08-31 stsp /* Release work tree lock. */
5833 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
5834 12314ad4 2019-08-31 stsp worktree = NULL;
5836 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5838 69069811 2018-08-02 stsp free(repo_path);
5839 f135c941 2020-02-20 stsp free(in_repo_path);
5840 0587e10c 2020-07-23 stsp free(link_target);
5841 69069811 2018-08-02 stsp free(cwd);
5842 a70480e0 2018-06-23 stsp free(commit_id);
5843 945f9229 2022-04-16 thomas if (commit)
5844 945f9229 2022-04-16 thomas got_object_commit_close(commit);
5845 eb41ed75 2019-02-05 stsp if (worktree)
5846 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
5847 1d0f4054 2021-06-17 stsp if (repo) {
5848 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5849 1d0f4054 2021-06-17 stsp if (error == NULL)
5850 1d0f4054 2021-06-17 stsp error = close_err;
5852 7cd52833 2022-06-23 thomas if (pack_fds) {
5853 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
5854 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
5855 7cd52833 2022-06-23 thomas if (error == NULL)
5856 7cd52833 2022-06-23 thomas error = pack_err;
5858 51a10b52 2020-12-26 stsp tog_free_refs();
5859 a70480e0 2018-06-23 stsp return error;
5862 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5863 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
5865 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
5866 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5867 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
5868 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
5869 f26dddb7 2019-11-08 stsp struct tog_color *tc;
5870 56e0773d 2019-11-28 stsp int width, n, i, nentries;
5871 d86d3b18 2020-12-01 naddy int limit = view->nlines;
5873 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
5874 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
5875 a5d43cac 2022-07-01 thomas --limit; /* border */
5877 f7d12f7e 2018-08-01 stsp werase(view->window);
5879 ffd1d5e5 2018-06-23 stsp if (limit == 0)
5880 ffd1d5e5 2018-06-23 stsp return NULL;
5882 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5885 ffd1d5e5 2018-06-23 stsp return err;
5886 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5887 a3404814 2018-09-02 stsp wstandout(view->window);
5888 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5890 11b20872 2019-11-08 stsp wattr_on(view->window,
5891 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5892 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5894 11b20872 2019-11-08 stsp wattr_off(view->window,
5895 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5896 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
5897 a3404814 2018-09-02 stsp wstandend(view->window);
5898 2550e4c3 2018-07-13 stsp free(wline);
5899 2550e4c3 2018-07-13 stsp wline = NULL;
5900 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5901 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5902 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5903 ffd1d5e5 2018-06-23 stsp return NULL;
5904 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5907 ce52c690 2018-06-23 stsp return err;
5908 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5909 2550e4c3 2018-07-13 stsp free(wline);
5910 2550e4c3 2018-07-13 stsp wline = NULL;
5911 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5912 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5913 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5914 ffd1d5e5 2018-06-23 stsp return NULL;
5915 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5916 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5917 a1eca9bb 2018-06-23 stsp return NULL;
5919 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5920 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5921 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5922 0cf4efb1 2018-09-29 stsp if (view->focussed)
5923 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5924 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5926 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5927 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5928 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5929 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5930 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5931 ffd1d5e5 2018-06-23 stsp return NULL;
5935 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5938 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5939 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5940 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5941 848d6979 2019-08-12 stsp const char *modestr = "";
5942 56e0773d 2019-11-28 stsp mode_t mode;
5944 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5945 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5947 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5948 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5949 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5951 638f9024 2019-05-13 stsp return got_error_from_errno(
5952 230a42bd 2019-05-11 jcs "got_object_id_str");
5954 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5955 63c5ca5d 2019-08-24 stsp modestr = "$";
5956 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5959 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5960 d86d3b18 2020-12-01 naddy te, s->repo);
5961 0d6c6ee3 2020-05-20 stsp if (err) {
5962 0d6c6ee3 2020-05-20 stsp free(id_str);
5963 0d6c6ee3 2020-05-20 stsp return err;
5965 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5966 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5967 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5969 848d6979 2019-08-12 stsp modestr = "@";
5971 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5972 848d6979 2019-08-12 stsp modestr = "/";
5973 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5974 848d6979 2019-08-12 stsp modestr = "*";
5975 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5976 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5977 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5978 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5979 1d13200f 2018-07-12 stsp free(id_str);
5980 0d6c6ee3 2020-05-20 stsp free(link_target);
5981 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5983 1d13200f 2018-07-12 stsp free(id_str);
5984 0d6c6ee3 2020-05-20 stsp free(link_target);
5985 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5987 ffd1d5e5 2018-06-23 stsp if (err) {
5988 ffd1d5e5 2018-06-23 stsp free(line);
5991 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5992 0cf4efb1 2018-09-29 stsp if (view->focussed)
5993 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5994 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5996 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5998 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5999 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6000 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
6002 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
6003 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
6004 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
6005 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
6006 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
6007 f7d12f7e 2018-08-01 stsp wstandend(view->window);
6008 ffd1d5e5 2018-06-23 stsp free(line);
6009 2550e4c3 2018-07-13 stsp free(wline);
6010 2550e4c3 2018-07-13 stsp wline = NULL;
6012 d86d3b18 2020-12-01 naddy s->ndisplayed++;
6013 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
6014 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
6018 ffd1d5e5 2018-06-23 stsp return err;
6021 ffd1d5e5 2018-06-23 stsp static void
6022 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
6024 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
6025 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
6026 fa86c4bf 2020-11-29 stsp int i = 0;
6028 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
6031 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
6032 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
6033 fa86c4bf 2020-11-29 stsp if (te == NULL) {
6034 fa86c4bf 2020-11-29 stsp if (!isroot)
6035 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
6038 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
6039 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
6043 a5d43cac 2022-07-01 thomas static const struct got_error *
6044 a5d43cac 2022-07-01 thomas tree_scroll_down(struct tog_view *view, int maxscroll)
6046 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
6047 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
6048 ffd1d5e5 2018-06-23 stsp int n = 0;
6050 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6051 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
6052 694d3271 2020-12-01 naddy s->first_displayed_entry);
6054 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
6056 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6057 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
6058 a5d43cac 2022-07-01 thomas if (last)
6059 a5d43cac 2022-07-01 thomas last = got_tree_entry_get_next(s->tree, last);
6060 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN && next)) {
6061 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6062 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
6066 a5d43cac 2022-07-01 thomas return NULL;
6069 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6070 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
6071 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
6073 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
6074 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
6075 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
6077 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
6078 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
6079 56e0773d 2019-11-28 stsp + 1 /* slash */;
6081 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
6083 ce52c690 2018-06-23 stsp *path = calloc(1, len);
6084 ffd1d5e5 2018-06-23 stsp if (path == NULL)
6085 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
6087 ce52c690 2018-06-23 stsp (*path)[0] = '/';
6088 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
6089 d9765a41 2018-06-23 stsp while (pt) {
6090 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
6091 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
6092 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6093 cb2ebc8a 2018-06-23 stsp goto done;
6095 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
6096 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6097 cb2ebc8a 2018-06-23 stsp goto done;
6099 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
6102 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
6103 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
6104 ce52c690 2018-06-23 stsp goto done;
6108 ce52c690 2018-06-23 stsp if (err) {
6109 ce52c690 2018-06-23 stsp free(*path);
6110 ce52c690 2018-06-23 stsp *path = NULL;
6112 ce52c690 2018-06-23 stsp return err;
6115 ce52c690 2018-06-23 stsp static const struct got_error *
6116 a5d43cac 2022-07-01 thomas blame_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6117 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
6118 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6120 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
6121 ce52c690 2018-06-23 stsp char *path;
6122 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
6124 a0de39f3 2019-08-09 stsp *new_view = NULL;
6126 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
6128 ce52c690 2018-06-23 stsp return err;
6130 a5d43cac 2022-07-01 thomas blame_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_BLAME);
6131 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
6132 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
6133 83ce39e3 2019-08-12 stsp goto done;
6136 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
6137 e5a0f69f 2018-08-18 stsp if (err) {
6138 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
6139 fc06ba56 2019-08-22 stsp err = NULL;
6140 e5a0f69f 2018-08-18 stsp view_close(blame_view);
6142 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
6144 83ce39e3 2019-08-12 stsp free(path);
6145 69efd4c4 2018-07-18 stsp return err;
6148 69efd4c4 2018-07-18 stsp static const struct got_error *
6149 444d5325 2022-07-03 thomas log_selected_tree_entry(struct tog_view **new_view, int begin_y, int begin_x,
6150 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
6152 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
6153 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
6154 69efd4c4 2018-07-18 stsp char *path;
6156 a0de39f3 2019-08-09 stsp *new_view = NULL;
6158 444d5325 2022-07-03 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
6159 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
6160 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
6162 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
6164 69efd4c4 2018-07-18 stsp return err;
6166 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
6169 e5a0f69f 2018-08-18 stsp view_close(log_view);
6171 e5a0f69f 2018-08-18 stsp *new_view = log_view;
6172 cb2ebc8a 2018-06-23 stsp free(path);
6173 cb2ebc8a 2018-06-23 stsp return err;
6176 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6177 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
6178 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
6180 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
6181 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
6182 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6183 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
6185 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
6186 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
6188 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
6189 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
6190 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
6192 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6194 bc573f3b 2021-07-10 stsp goto done;
6197 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
6198 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
6199 bc573f3b 2021-07-10 stsp * closed on demand.
6201 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
6202 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
6204 bc573f3b 2021-07-10 stsp goto done;
6205 bc573f3b 2021-07-10 stsp s->tree = s->root;
6207 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
6208 ffd1d5e5 2018-06-23 stsp if (err != NULL)
6209 ffd1d5e5 2018-06-23 stsp goto done;
6211 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
6212 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
6213 ffd1d5e5 2018-06-23 stsp goto done;
6216 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
6217 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
6218 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
6219 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
6220 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
6221 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
6222 9cd7cbd1 2020-12-07 stsp goto done;
6225 fb2756b9 2018-08-04 stsp s->repo = repo;
6227 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6228 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
6229 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
6230 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
6232 c0b01bdb 2019-11-08 stsp goto done;
6233 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
6234 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
6236 c0b01bdb 2019-11-08 stsp goto done;
6237 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
6238 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
6239 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
6241 c0b01bdb 2019-11-08 stsp goto done;
6243 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
6244 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
6245 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
6247 11b20872 2019-11-08 stsp goto done;
6249 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
6250 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
6252 c0b01bdb 2019-11-08 stsp goto done;
6255 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
6256 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
6257 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
6258 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
6259 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
6261 ad80ab7b 2018-08-04 stsp free(commit_id_str);
6262 bc573f3b 2021-07-10 stsp if (commit)
6263 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
6265 bc573f3b 2021-07-10 stsp close_tree_view(view);
6266 ad80ab7b 2018-08-04 stsp return err;
6269 e5a0f69f 2018-08-18 stsp static const struct got_error *
6270 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
6272 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6274 bddb1296 2019-11-08 stsp free_colors(&s->colors);
6275 fb2756b9 2018-08-04 stsp free(s->tree_label);
6276 6484ec90 2018-09-29 stsp s->tree_label = NULL;
6277 6484ec90 2018-09-29 stsp free(s->commit_id);
6278 6484ec90 2018-09-29 stsp s->commit_id = NULL;
6279 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
6280 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
6281 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
6282 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
6283 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
6284 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
6285 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
6286 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
6287 ad80ab7b 2018-08-04 stsp free(parent);
6290 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
6291 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
6292 bc573f3b 2021-07-10 stsp if (s->root)
6293 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
6294 7c32bd05 2019-06-22 stsp return NULL;
6297 7c32bd05 2019-06-22 stsp static const struct got_error *
6298 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
6300 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6302 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
6303 7c32bd05 2019-06-22 stsp return NULL;
6306 7c32bd05 2019-06-22 stsp static int
6307 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
6309 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
6311 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
6315 7c32bd05 2019-06-22 stsp static const struct got_error *
6316 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
6318 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
6319 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
6321 7c32bd05 2019-06-22 stsp if (!view->searching) {
6322 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6323 7c32bd05 2019-06-22 stsp return NULL;
6326 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6327 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6328 7c32bd05 2019-06-22 stsp if (s->selected_entry)
6329 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
6330 56e0773d 2019-11-28 stsp s->selected_entry);
6332 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6334 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
6335 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6337 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
6338 56e0773d 2019-11-28 stsp s->selected_entry);
6341 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6342 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
6343 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6344 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6346 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6349 7c32bd05 2019-06-22 stsp while (1) {
6350 56e0773d 2019-11-28 stsp if (te == NULL) {
6351 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
6352 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6353 ac66afb8 2019-06-24 stsp return NULL;
6355 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6356 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
6358 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
6361 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
6362 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6363 56e0773d 2019-11-28 stsp s->matched_entry = te;
6367 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
6368 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
6370 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
6373 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
6374 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
6375 7c32bd05 2019-06-22 stsp s->selected = 0;
6378 e5a0f69f 2018-08-18 stsp return NULL;
6381 ad80ab7b 2018-08-04 stsp static const struct got_error *
6382 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
6384 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
6385 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
6386 e5a0f69f 2018-08-18 stsp char *parent_path;
6388 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
6390 e5a0f69f 2018-08-18 stsp return err;
6392 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
6393 e5a0f69f 2018-08-18 stsp free(parent_path);
6395 a5d43cac 2022-07-01 thomas view_border(view);
6396 e5a0f69f 2018-08-18 stsp return err;
6399 e5a0f69f 2018-08-18 stsp static const struct got_error *
6400 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6402 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
6403 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
6404 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
6405 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
6406 444d5325 2022-07-03 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 3;
6408 e5a0f69f 2018-08-18 stsp switch (ch) {
6410 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
6411 07b0611c 2022-06-23 thomas view->count = 0;
6414 07b0611c 2022-06-23 thomas view->count = 0;
6415 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
6417 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
6418 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6419 444d5325 2022-07-03 thomas err = log_selected_tree_entry(&log_view, begin_y, begin_x, s);
6420 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6421 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6422 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6423 444d5325 2022-07-03 thomas if (err)
6426 e78dc838 2020-12-04 stsp view->focussed = 0;
6427 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6428 444d5325 2022-07-03 thomas log_view->mode = view->mode;
6429 444d5325 2022-07-03 thomas log_view->nlines = view->lines - begin_y;
6430 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
6431 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
6433 1e37a5c2 2019-05-12 jcs return err;
6434 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
6435 40236d76 2022-06-23 thomas if (err)
6436 40236d76 2022-06-23 thomas return err;
6437 e78dc838 2020-12-04 stsp view->focus_child = 1;
6439 1e37a5c2 2019-05-12 jcs *new_view = log_view;
6442 07b0611c 2022-06-23 thomas view->count = 0;
6443 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
6444 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
6445 444d5325 2022-07-03 thomas ref_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_REF);
6446 152c1c93 2020-11-29 stsp if (ref_view == NULL)
6447 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
6448 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
6449 152c1c93 2020-11-29 stsp if (err) {
6450 152c1c93 2020-11-29 stsp view_close(ref_view);
6451 152c1c93 2020-11-29 stsp return err;
6453 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6454 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6455 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
6456 444d5325 2022-07-03 thomas if (err)
6459 e78dc838 2020-12-04 stsp view->focussed = 0;
6460 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
6461 444d5325 2022-07-03 thomas ref_view->mode = view->mode;
6462 444d5325 2022-07-03 thomas ref_view->nlines = view->lines - begin_y;
6463 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
6464 152c1c93 2020-11-29 stsp err = view_close_child(view);
6466 152c1c93 2020-11-29 stsp return err;
6467 40236d76 2022-06-23 thomas err = view_set_child(view, ref_view);
6468 40236d76 2022-06-23 thomas if (err)
6469 40236d76 2022-06-23 thomas return err;
6470 e78dc838 2020-12-04 stsp view->focus_child = 1;
6472 152c1c93 2020-11-29 stsp *new_view = ref_view;
6474 e4526bf5 2021-09-03 naddy case 'g':
6475 e4526bf5 2021-09-03 naddy case KEY_HOME:
6476 e4526bf5 2021-09-03 naddy s->selected = 0;
6477 07b0611c 2022-06-23 thomas view->count = 0;
6478 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
6479 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
6480 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
6482 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6484 e4526bf5 2021-09-03 naddy case 'G':
6485 a5d43cac 2022-07-01 thomas case KEY_END: {
6486 a5d43cac 2022-07-01 thomas int eos = view->nlines - 3;
6488 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
6489 a5d43cac 2022-07-01 thomas --eos; /* border */
6490 e4526bf5 2021-09-03 naddy s->selected = 0;
6491 07b0611c 2022-06-23 thomas view->count = 0;
6492 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
6493 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
6494 e4526bf5 2021-09-03 naddy if (te == NULL) {
6495 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
6496 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
6501 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
6502 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
6504 e4526bf5 2021-09-03 naddy if (n > 0)
6505 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6509 1e37a5c2 2019-05-12 jcs case KEY_UP:
6510 f7140bf5 2021-10-17 thomas case CTRL('p'):
6511 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
6512 1e37a5c2 2019-05-12 jcs s->selected--;
6515 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
6516 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6517 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6518 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6519 07b0611c 2022-06-23 thomas view->count = 0;
6521 70f17a53 2022-06-13 thomas case CTRL('u'):
6522 23427b14 2022-06-23 thomas case 'u':
6523 70f17a53 2022-06-13 thomas nscroll /= 2;
6524 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6525 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
6526 ea025d1d 2020-02-22 naddy case CTRL('b'):
6527 1c5e5faa 2022-06-23 thomas case 'b':
6528 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
6529 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
6530 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
6531 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6533 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
6534 70f17a53 2022-06-13 thomas s->selected -= MIN(s->selected, nscroll);
6536 70f17a53 2022-06-13 thomas tree_scroll_up(s, MAX(0, nscroll));
6537 07b0611c 2022-06-23 thomas if (s->selected_entry == NULL ||
6538 07b0611c 2022-06-23 thomas (s->tree == s->root && s->selected_entry ==
6539 07b0611c 2022-06-23 thomas got_object_tree_get_first_entry(s->tree)))
6540 07b0611c 2022-06-23 thomas view->count = 0;
6543 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
6544 f7140bf5 2021-10-17 thomas case CTRL('n'):
6545 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
6546 1e37a5c2 2019-05-12 jcs s->selected++;
6549 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6550 07b0611c 2022-06-23 thomas == NULL) {
6551 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
6552 07b0611c 2022-06-23 thomas view->count = 0;
6555 a5d43cac 2022-07-01 thomas tree_scroll_down(view, 1);
6557 70f17a53 2022-06-13 thomas case CTRL('d'):
6558 23427b14 2022-06-23 thomas case 'd':
6559 70f17a53 2022-06-13 thomas nscroll /= 2;
6560 70f17a53 2022-06-13 thomas /* FALL THROUGH */
6561 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
6562 ea025d1d 2020-02-22 naddy case CTRL('f'):
6563 1c5e5faa 2022-06-23 thomas case 'f':
6564 4c2d69cb 2022-06-23 thomas case ' ':
6565 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6567 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
6568 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
6569 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
6570 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
6572 07b0611c 2022-06-23 thomas view->count = 0;
6575 a5d43cac 2022-07-01 thomas tree_scroll_down(view, nscroll);
6577 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
6579 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
6580 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6581 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
6582 1e37a5c2 2019-05-12 jcs /* user selected '..' */
6583 07b0611c 2022-06-23 thomas if (s->tree == s->root) {
6584 07b0611c 2022-06-23 thomas view->count = 0;
6587 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
6588 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
6590 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
6591 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
6592 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
6593 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
6594 1e37a5c2 2019-05-12 jcs s->selected_entry =
6595 1e37a5c2 2019-05-12 jcs parent->selected_entry;
6596 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
6597 a5d43cac 2022-07-01 thomas if (s->selected > view->nlines - 3) {
6598 a5d43cac 2022-07-01 thomas err = offset_selection_down(view);
6599 a5d43cac 2022-07-01 thomas if (err)
6602 1e37a5c2 2019-05-12 jcs free(parent);
6603 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
6604 56e0773d 2019-11-28 stsp s->selected_entry))) {
6605 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
6606 07b0611c 2022-06-23 thomas view->count = 0;
6607 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
6608 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
6611 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
6612 941e9f74 2019-05-21 stsp if (err) {
6613 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
6616 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
6617 56e0773d 2019-11-28 stsp s->selected_entry))) {
6618 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
6619 a5d43cac 2022-07-01 thomas int begin_x = 0, begin_y = 0;
6621 a5d43cac 2022-07-01 thomas if (view_is_parent_view(view))
6622 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
6624 a5d43cac 2022-07-01 thomas err = blame_tree_entry(&blame_view, begin_y, begin_x,
6625 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
6626 78756c87 2020-11-24 stsp s->commit_id, s->repo);
6630 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
6631 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
6632 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
6633 a5d43cac 2022-07-01 thomas if (err)
6637 07b0611c 2022-06-23 thomas view->count = 0;
6638 e78dc838 2020-12-04 stsp view->focussed = 0;
6639 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
6640 a5d43cac 2022-07-01 thomas blame_view->mode = view->mode;
6641 a5d43cac 2022-07-01 thomas blame_view->nlines = view->lines - begin_y;
6642 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
6643 669b5ffa 2018-10-07 stsp err = view_close_child(view);
6645 669b5ffa 2018-10-07 stsp return err;
6646 40236d76 2022-06-23 thomas err = view_set_child(view, blame_view);
6647 40236d76 2022-06-23 thomas if (err)
6648 40236d76 2022-06-23 thomas return err;
6649 e78dc838 2020-12-04 stsp view->focus_child = 1;
6651 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
6654 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
6655 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6656 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
6657 07b0611c 2022-06-23 thomas view->count = 0;
6660 07b0611c 2022-06-23 thomas view->count = 0;
6664 ffd1d5e5 2018-06-23 stsp return err;
6667 ffd1d5e5 2018-06-23 stsp __dead static void
6668 ffd1d5e5 2018-06-23 stsp usage_tree(void)
6671 91198554 2022-06-23 thomas fprintf(stderr,
6672 91198554 2022-06-23 thomas "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6673 ffd1d5e5 2018-06-23 stsp getprogname());
6677 ffd1d5e5 2018-06-23 stsp static const struct got_error *
6678 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
6680 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
6681 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
6682 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
6683 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6684 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
6685 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6686 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
6687 4e97c21c 2020-12-06 stsp char *label = NULL;
6688 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
6689 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
6691 5221c383 2018-08-01 stsp struct tog_view *view;
6692 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
6694 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6695 ffd1d5e5 2018-06-23 stsp switch (ch) {
6697 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
6700 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
6701 74283ab8 2020-02-07 stsp if (repo_path == NULL)
6702 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
6706 e99e2d15 2020-11-24 naddy usage_tree();
6707 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
6711 ffd1d5e5 2018-06-23 stsp argc -= optind;
6712 ffd1d5e5 2018-06-23 stsp argv += optind;
6714 55cccc34 2020-02-20 stsp if (argc > 1)
6715 e99e2d15 2020-11-24 naddy usage_tree();
6717 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
6718 7cd52833 2022-06-23 thomas if (error != NULL)
6719 7cd52833 2022-06-23 thomas goto done;
6721 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
6722 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6723 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6724 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6725 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6726 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6727 c156c7a4 2020-12-18 stsp goto done;
6728 55cccc34 2020-02-20 stsp if (worktree)
6729 52185f70 2019-02-05 stsp repo_path =
6730 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
6732 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
6733 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6734 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6735 c156c7a4 2020-12-18 stsp goto done;
6739 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6740 c02c541e 2019-03-29 stsp if (error != NULL)
6741 52185f70 2019-02-05 stsp goto done;
6743 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6744 55cccc34 2020-02-20 stsp repo, worktree);
6745 55cccc34 2020-02-20 stsp if (error)
6746 55cccc34 2020-02-20 stsp goto done;
6748 55cccc34 2020-02-20 stsp init_curses();
6750 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6751 c02c541e 2019-03-29 stsp if (error)
6752 52185f70 2019-02-05 stsp goto done;
6754 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6755 51a10b52 2020-12-26 stsp if (error)
6756 51a10b52 2020-12-26 stsp goto done;
6758 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
6759 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
6760 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
6761 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6762 4e97c21c 2020-12-06 stsp if (error)
6763 4e97c21c 2020-12-06 stsp goto done;
6764 4e97c21c 2020-12-06 stsp head_ref_name = label;
6766 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
6767 4e97c21c 2020-12-06 stsp if (error == NULL)
6768 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
6769 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
6770 4e97c21c 2020-12-06 stsp goto done;
6771 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
6772 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6773 4e97c21c 2020-12-06 stsp if (error)
6774 4e97c21c 2020-12-06 stsp goto done;
6777 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6778 945f9229 2022-04-16 thomas if (error)
6779 945f9229 2022-04-16 thomas goto done;
6781 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6782 5221c383 2018-08-01 stsp if (view == NULL) {
6783 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
6784 5221c383 2018-08-01 stsp goto done;
6786 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
6787 ad80ab7b 2018-08-04 stsp if (error)
6788 ad80ab7b 2018-08-04 stsp goto done;
6789 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
6790 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
6791 d91faf3b 2020-12-01 naddy in_repo_path);
6792 55cccc34 2020-02-20 stsp if (error)
6793 55cccc34 2020-02-20 stsp goto done;
6796 55cccc34 2020-02-20 stsp if (worktree) {
6797 55cccc34 2020-02-20 stsp /* Release work tree lock. */
6798 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
6799 55cccc34 2020-02-20 stsp worktree = NULL;
6801 e5a0f69f 2018-08-18 stsp error = view_loop(view);
6803 52185f70 2019-02-05 stsp free(repo_path);
6804 e4a0e26d 2020-02-20 stsp free(cwd);
6805 ffd1d5e5 2018-06-23 stsp free(commit_id);
6806 4e97c21c 2020-12-06 stsp free(label);
6808 486cd271 2020-12-06 stsp got_ref_close(ref);
6809 1d0f4054 2021-06-17 stsp if (repo) {
6810 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6811 1d0f4054 2021-06-17 stsp if (error == NULL)
6812 1d0f4054 2021-06-17 stsp error = close_err;
6814 7cd52833 2022-06-23 thomas if (pack_fds) {
6815 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
6816 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
6817 7cd52833 2022-06-23 thomas if (error == NULL)
6818 7cd52833 2022-06-23 thomas error = pack_err;
6820 51a10b52 2020-12-26 stsp tog_free_refs();
6821 ffd1d5e5 2018-06-23 stsp return error;
6824 6458efa5 2020-11-24 stsp static const struct got_error *
6825 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
6827 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
6828 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6830 6458efa5 2020-11-24 stsp s->nrefs = 0;
6831 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
6832 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
6833 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
6834 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
6835 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
6838 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
6839 6458efa5 2020-11-24 stsp if (re == NULL)
6840 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
6842 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
6843 8924d611 2020-12-26 stsp if (re->ref == NULL)
6844 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
6845 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
6846 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
6849 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6850 6458efa5 2020-11-24 stsp return NULL;
6853 ef20f542 2022-06-26 thomas static void
6854 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
6856 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6858 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
6859 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6860 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
6861 8924d611 2020-12-26 stsp got_ref_close(re->ref);
6866 6458efa5 2020-11-24 stsp static const struct got_error *
6867 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
6869 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6870 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6872 6458efa5 2020-11-24 stsp s->selected_entry = 0;
6873 6458efa5 2020-11-24 stsp s->repo = repo;
6875 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
6876 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
6878 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6880 6458efa5 2020-11-24 stsp return err;
6882 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
6883 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
6884 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
6885 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
6887 6458efa5 2020-11-24 stsp goto done;
6889 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
6890 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
6891 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
6893 6458efa5 2020-11-24 stsp goto done;
6895 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
6896 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
6897 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
6898 2183bbf6 2022-01-23 thomas if (err)
6899 2183bbf6 2022-01-23 thomas goto done;
6901 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
6902 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
6903 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
6905 6458efa5 2020-11-24 stsp goto done;
6908 6458efa5 2020-11-24 stsp view->show = show_ref_view;
6909 6458efa5 2020-11-24 stsp view->input = input_ref_view;
6910 6458efa5 2020-11-24 stsp view->close = close_ref_view;
6911 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
6912 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
6915 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6916 6458efa5 2020-11-24 stsp return err;
6919 6458efa5 2020-11-24 stsp static const struct got_error *
6920 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
6922 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6924 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6925 6458efa5 2020-11-24 stsp free_colors(&s->colors);
6927 6458efa5 2020-11-24 stsp return NULL;
6930 6458efa5 2020-11-24 stsp static const struct got_error *
6931 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
6932 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6934 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6935 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
6936 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
6937 6458efa5 2020-11-24 stsp int obj_type;
6939 c42c9805 2020-11-24 stsp *commit_id = NULL;
6941 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
6943 6458efa5 2020-11-24 stsp return err;
6945 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
6947 6458efa5 2020-11-24 stsp goto done;
6949 6458efa5 2020-11-24 stsp switch (obj_type) {
6950 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
6951 c42c9805 2020-11-24 stsp *commit_id = obj_id;
6953 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
6954 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
6956 6458efa5 2020-11-24 stsp goto done;
6957 c42c9805 2020-11-24 stsp free(obj_id);
6958 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
6959 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6961 6458efa5 2020-11-24 stsp goto done;
6962 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6963 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6964 c42c9805 2020-11-24 stsp goto done;
6966 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
6967 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
6968 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
6969 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
6970 c42c9805 2020-11-24 stsp goto done;
6974 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
6980 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
6981 c42c9805 2020-11-24 stsp if (err) {
6982 c42c9805 2020-11-24 stsp free(*commit_id);
6983 c42c9805 2020-11-24 stsp *commit_id = NULL;
6985 c42c9805 2020-11-24 stsp return err;
6988 c42c9805 2020-11-24 stsp static const struct got_error *
6989 a5d43cac 2022-07-01 thomas log_ref_entry(struct tog_view **new_view, int begin_y, int begin_x,
6990 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6992 c42c9805 2020-11-24 stsp struct tog_view *log_view;
6993 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6994 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
6996 c42c9805 2020-11-24 stsp *new_view = NULL;
6998 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6999 c42c9805 2020-11-24 stsp if (err) {
7000 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7001 c42c9805 2020-11-24 stsp return err;
7003 c42c9805 2020-11-24 stsp return NULL;
7006 a5d43cac 2022-07-01 thomas log_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_LOG);
7007 6458efa5 2020-11-24 stsp if (log_view == NULL) {
7008 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
7009 6458efa5 2020-11-24 stsp goto done;
7012 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
7013 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
7016 6458efa5 2020-11-24 stsp view_close(log_view);
7018 6458efa5 2020-11-24 stsp *new_view = log_view;
7019 c42c9805 2020-11-24 stsp free(commit_id);
7020 6458efa5 2020-11-24 stsp return err;
7023 ce5b7c56 2019-07-09 stsp static void
7024 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
7026 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
7027 34ba6917 2020-11-30 stsp int i = 0;
7029 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7032 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
7033 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
7034 34ba6917 2020-11-30 stsp if (re == NULL)
7036 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
7037 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7041 a5d43cac 2022-07-01 thomas static const struct got_error *
7042 a5d43cac 2022-07-01 thomas ref_scroll_down(struct tog_view *view, int maxscroll)
7044 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7045 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
7046 6458efa5 2020-11-24 stsp int n = 0;
7048 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
7049 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
7051 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
7053 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
7054 a5d43cac 2022-07-01 thomas while (next && n++ < maxscroll) {
7055 a5d43cac 2022-07-01 thomas if (last)
7056 a5d43cac 2022-07-01 thomas last = TAILQ_NEXT(last, entry);
7057 a5d43cac 2022-07-01 thomas if (last || (view->mode == TOG_VIEW_SPLIT_HRZN)) {
7058 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
7059 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
7063 a5d43cac 2022-07-01 thomas return NULL;
7066 6458efa5 2020-11-24 stsp static const struct got_error *
7067 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
7069 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7071 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
7072 6458efa5 2020-11-24 stsp return NULL;
7075 6458efa5 2020-11-24 stsp static int
7076 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
7078 6458efa5 2020-11-24 stsp regmatch_t regmatch;
7080 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
7084 6458efa5 2020-11-24 stsp static const struct got_error *
7085 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
7087 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7088 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
7090 6458efa5 2020-11-24 stsp if (!view->searching) {
7091 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7092 6458efa5 2020-11-24 stsp return NULL;
7095 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7096 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
7097 6458efa5 2020-11-24 stsp if (s->selected_entry)
7098 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
7100 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7101 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7103 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
7104 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7106 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
7107 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
7110 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
7111 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
7112 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
7113 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7115 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7118 6458efa5 2020-11-24 stsp while (1) {
7119 6458efa5 2020-11-24 stsp if (re == NULL) {
7120 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
7121 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7122 6458efa5 2020-11-24 stsp return NULL;
7124 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7125 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
7127 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
7130 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
7131 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
7132 6458efa5 2020-11-24 stsp s->matched_entry = re;
7136 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
7137 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7139 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
7142 6458efa5 2020-11-24 stsp if (s->matched_entry) {
7143 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
7144 6458efa5 2020-11-24 stsp s->selected = 0;
7147 6458efa5 2020-11-24 stsp return NULL;
7150 6458efa5 2020-11-24 stsp static const struct got_error *
7151 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
7153 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7154 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7155 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
7156 6458efa5 2020-11-24 stsp char *line = NULL;
7157 6458efa5 2020-11-24 stsp wchar_t *wline;
7158 6458efa5 2020-11-24 stsp struct tog_color *tc;
7159 6458efa5 2020-11-24 stsp int width, n;
7160 6458efa5 2020-11-24 stsp int limit = view->nlines;
7162 6458efa5 2020-11-24 stsp werase(view->window);
7164 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
7165 444d5325 2022-07-03 thomas if (view_is_hsplit_top(view))
7166 a5d43cac 2022-07-01 thomas --limit; /* border */
7168 6458efa5 2020-11-24 stsp if (limit == 0)
7169 6458efa5 2020-11-24 stsp return NULL;
7171 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
7173 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
7174 6458efa5 2020-11-24 stsp s->nrefs) == -1)
7175 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7177 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
7178 6458efa5 2020-11-24 stsp if (err) {
7179 6458efa5 2020-11-24 stsp free(line);
7180 6458efa5 2020-11-24 stsp return err;
7182 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7183 6458efa5 2020-11-24 stsp wstandout(view->window);
7184 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7185 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
7186 6458efa5 2020-11-24 stsp wstandend(view->window);
7187 6458efa5 2020-11-24 stsp free(wline);
7188 6458efa5 2020-11-24 stsp wline = NULL;
7189 6458efa5 2020-11-24 stsp free(line);
7190 6458efa5 2020-11-24 stsp line = NULL;
7191 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7192 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7193 6458efa5 2020-11-24 stsp if (--limit <= 0)
7194 6458efa5 2020-11-24 stsp return NULL;
7197 6458efa5 2020-11-24 stsp while (re && limit > 0) {
7198 6458efa5 2020-11-24 stsp char *line = NULL;
7199 84227eb1 2022-06-23 thomas char ymd[13]; /* YYYY-MM-DD + " " + NUL */
7201 84227eb1 2022-06-23 thomas if (s->show_date) {
7202 84227eb1 2022-06-23 thomas struct got_commit_object *ci;
7203 84227eb1 2022-06-23 thomas struct got_tag_object *tag;
7204 84227eb1 2022-06-23 thomas struct got_object_id *id;
7205 84227eb1 2022-06-23 thomas struct tm tm;
7206 84227eb1 2022-06-23 thomas time_t t;
7208 84227eb1 2022-06-23 thomas err = got_ref_resolve(&id, s->repo, re->ref);
7209 84227eb1 2022-06-23 thomas if (err)
7210 84227eb1 2022-06-23 thomas return err;
7211 84227eb1 2022-06-23 thomas err = got_object_open_as_tag(&tag, s->repo, id);
7212 84227eb1 2022-06-23 thomas if (err) {
7213 84227eb1 2022-06-23 thomas if (err->code != GOT_ERR_OBJ_TYPE) {
7214 84227eb1 2022-06-23 thomas free(id);
7215 84227eb1 2022-06-23 thomas return err;
7217 84227eb1 2022-06-23 thomas err = got_object_open_as_commit(&ci, s->repo,
7219 84227eb1 2022-06-23 thomas if (err) {
7220 84227eb1 2022-06-23 thomas free(id);
7221 84227eb1 2022-06-23 thomas return err;
7223 84227eb1 2022-06-23 thomas t = got_object_commit_get_committer_time(ci);
7224 84227eb1 2022-06-23 thomas got_object_commit_close(ci);
7225 84227eb1 2022-06-23 thomas } else {
7226 84227eb1 2022-06-23 thomas t = got_object_tag_get_tagger_time(tag);
7227 84227eb1 2022-06-23 thomas got_object_tag_close(tag);
7229 84227eb1 2022-06-23 thomas free(id);
7230 84227eb1 2022-06-23 thomas if (gmtime_r(&t, &tm) == NULL)
7231 84227eb1 2022-06-23 thomas return got_error_from_errno("gmtime_r");
7232 84227eb1 2022-06-23 thomas if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
7233 84227eb1 2022-06-23 thomas return got_error(GOT_ERR_NO_SPACE);
7235 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
7236 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s -> %s", s->show_date ?
7237 84227eb1 2022-06-23 thomas ymd : "", got_ref_get_name(re->ref),
7238 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
7239 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
7240 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
7241 6458efa5 2020-11-24 stsp struct got_object_id *id;
7242 6458efa5 2020-11-24 stsp char *id_str;
7243 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
7245 6458efa5 2020-11-24 stsp return err;
7246 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
7247 6458efa5 2020-11-24 stsp if (err) {
7249 6458efa5 2020-11-24 stsp return err;
7251 84227eb1 2022-06-23 thomas if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
7252 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
7253 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
7255 6458efa5 2020-11-24 stsp free(id_str);
7256 6458efa5 2020-11-24 stsp return err;
7259 6458efa5 2020-11-24 stsp free(id_str);
7260 84227eb1 2022-06-23 thomas } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
7261 84227eb1 2022-06-23 thomas got_ref_get_name(re->ref)) == -1)
7262 84227eb1 2022-06-23 thomas return got_error_from_errno("asprintf");
7264 f91a2b48 2022-06-23 thomas err = format_line(&wline, &width, NULL, line, 0, view->ncols,
7266 6458efa5 2020-11-24 stsp if (err) {
7267 6458efa5 2020-11-24 stsp free(line);
7268 6458efa5 2020-11-24 stsp return err;
7270 6458efa5 2020-11-24 stsp if (n == s->selected) {
7271 6458efa5 2020-11-24 stsp if (view->focussed)
7272 6458efa5 2020-11-24 stsp wstandout(view->window);
7273 6458efa5 2020-11-24 stsp s->selected_entry = re;
7275 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
7277 6458efa5 2020-11-24 stsp wattr_on(view->window,
7278 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7279 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
7281 6458efa5 2020-11-24 stsp wattr_off(view->window,
7282 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
7283 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
7284 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
7285 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
7286 6458efa5 2020-11-24 stsp wstandend(view->window);
7287 6458efa5 2020-11-24 stsp free(line);
7288 6458efa5 2020-11-24 stsp free(wline);
7289 6458efa5 2020-11-24 stsp wline = NULL;
7291 6458efa5 2020-11-24 stsp s->ndisplayed++;
7292 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
7295 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
7298 a5d43cac 2022-07-01 thomas view_border(view);
7299 6458efa5 2020-11-24 stsp return err;
7302 6458efa5 2020-11-24 stsp static const struct got_error *
7303 444d5325 2022-07-03 thomas browse_ref_tree(struct tog_view **new_view, int begin_y, int begin_x,
7304 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
7306 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
7307 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
7308 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
7310 c42c9805 2020-11-24 stsp *new_view = NULL;
7312 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
7313 c42c9805 2020-11-24 stsp if (err) {
7314 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
7315 c42c9805 2020-11-24 stsp return err;
7317 c42c9805 2020-11-24 stsp return NULL;
7321 444d5325 2022-07-03 thomas tree_view = view_open(0, 0, begin_y, begin_x, TOG_VIEW_TREE);
7322 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
7323 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
7324 c42c9805 2020-11-24 stsp goto done;
7327 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
7328 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
7330 c42c9805 2020-11-24 stsp goto done;
7332 c42c9805 2020-11-24 stsp *new_view = tree_view;
7334 c42c9805 2020-11-24 stsp free(commit_id);
7335 c42c9805 2020-11-24 stsp return err;
7337 c42c9805 2020-11-24 stsp static const struct got_error *
7338 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
7340 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
7341 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
7342 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
7343 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
7344 a5d43cac 2022-07-01 thomas int begin_y = 0, begin_x = 0, n, nscroll = view->nlines - 1;
7346 6458efa5 2020-11-24 stsp switch (ch) {
7348 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
7349 07b0611c 2022-06-23 thomas view->count = 0;
7351 84227eb1 2022-06-23 thomas case 'm':
7352 84227eb1 2022-06-23 thomas s->show_date = !s->show_date;
7353 07b0611c 2022-06-23 thomas view->count = 0;
7355 98182bd0 2021-11-20 thomas case 'o':
7356 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
7357 07b0611c 2022-06-23 thomas view->count = 0;
7358 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
7359 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
7360 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
7361 c591440f 2021-11-20 thomas if (err)
7363 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
7364 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
7365 c591440f 2021-11-20 thomas &tog_refs, s->repo);
7366 3bfadbd4 2021-11-20 thomas if (err)
7368 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
7369 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
7371 6458efa5 2020-11-24 stsp case KEY_ENTER:
7372 6458efa5 2020-11-24 stsp case '\r':
7373 07b0611c 2022-06-23 thomas view->count = 0;
7374 6458efa5 2020-11-24 stsp if (!s->selected_entry)
7376 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
7377 a5d43cac 2022-07-01 thomas view_get_split(view, &begin_y, &begin_x);
7379 a5d43cac 2022-07-01 thomas err = log_ref_entry(&log_view, begin_y, begin_x,
7380 a5d43cac 2022-07-01 thomas s->selected_entry, s->repo);
7381 a5d43cac 2022-07-01 thomas if (err)
7384 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7385 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7386 a5d43cac 2022-07-01 thomas err = view_init_hsplit(view, begin_y);
7387 a5d43cac 2022-07-01 thomas if (err)
7391 e78dc838 2020-12-04 stsp view->focussed = 0;
7392 e78dc838 2020-12-04 stsp log_view->focussed = 1;
7393 a5d43cac 2022-07-01 thomas log_view->mode = view->mode;
7394 a5d43cac 2022-07-01 thomas log_view->nlines = view->lines - begin_y;
7395 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
7396 6458efa5 2020-11-24 stsp err = view_close_child(view);
7398 6458efa5 2020-11-24 stsp return err;
7399 40236d76 2022-06-23 thomas err = view_set_child(view, log_view);
7400 40236d76 2022-06-23 thomas if (err)
7401 40236d76 2022-06-23 thomas return err;
7402 e78dc838 2020-12-04 stsp view->focus_child = 1;
7404 6458efa5 2020-11-24 stsp *new_view = log_view;
7407 07b0611c 2022-06-23 thomas view->count = 0;
7408 c42c9805 2020-11-24 stsp if (!s->selected_entry)
7410 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
7411 444d5325 2022-07-03 thomas view_get_split(view, &begin_y, &begin_x);
7412 444d5325 2022-07-03 thomas err = browse_ref_tree(&tree_view, begin_y, begin_x,
7413 444d5325 2022-07-03 thomas s->selected_entry, s->repo);
7414 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
7416 444d5325 2022-07-03 thomas if (view_is_parent_view(view) &&
7417 444d5325 2022-07-03 thomas view->mode == TOG_VIEW_SPLIT_HRZN) {
7418 444d5325 2022-07-03 thomas err = view_init_hsplit(view, begin_y);
7419 444d5325 2022-07-03 thomas if (err)
7422 e78dc838 2020-12-04 stsp view->focussed = 0;
7423 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
7424 444d5325 2022-07-03 thomas tree_view->mode = view->mode;
7425 444d5325 2022-07-03 thomas tree_view->nlines = view->lines - begin_y;
7426 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
7427 c42c9805 2020-11-24 stsp err = view_close_child(view);
7429 c42c9805 2020-11-24 stsp return err;
7430 40236d76 2022-06-23 thomas err = view_set_child(view, tree_view);
7431 40236d76 2022-06-23 thomas if (err)
7432 40236d76 2022-06-23 thomas return err;
7433 e78dc838 2020-12-04 stsp view->focus_child = 1;
7435 c42c9805 2020-11-24 stsp *new_view = tree_view;
7437 e4526bf5 2021-09-03 naddy case 'g':
7438 e4526bf5 2021-09-03 naddy case KEY_HOME:
7439 e4526bf5 2021-09-03 naddy s->selected = 0;
7440 07b0611c 2022-06-23 thomas view->count = 0;
7441 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7443 e4526bf5 2021-09-03 naddy case 'G':
7444 a5d43cac 2022-07-01 thomas case KEY_END: {
7445 a5d43cac 2022-07-01 thomas int eos = view->nlines - 1;
7447 a5d43cac 2022-07-01 thomas if (view->mode == TOG_VIEW_SPLIT_HRZN)
7448 a5d43cac 2022-07-01 thomas --eos; /* border */
7449 e4526bf5 2021-09-03 naddy s->selected = 0;
7450 07b0611c 2022-06-23 thomas view->count = 0;
7451 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
7452 a5d43cac 2022-07-01 thomas for (n = 0; n < eos; n++) {
7453 e4526bf5 2021-09-03 naddy if (re == NULL)
7455 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
7456 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
7458 e4526bf5 2021-09-03 naddy if (n > 0)
7459 e4526bf5 2021-09-03 naddy s->selected = n - 1;
7463 6458efa5 2020-11-24 stsp case KEY_UP:
7464 f7140bf5 2021-10-17 thomas case CTRL('p'):
7465 6458efa5 2020-11-24 stsp if (s->selected > 0) {
7466 6458efa5 2020-11-24 stsp s->selected--;
7469 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
7470 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7471 07b0611c 2022-06-23 thomas view->count = 0;
7473 70f17a53 2022-06-13 thomas case CTRL('u'):
7474 23427b14 2022-06-23 thomas case 'u':
7475 70f17a53 2022-06-13 thomas nscroll /= 2;
7476 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7477 6458efa5 2020-11-24 stsp case KEY_PPAGE:
7478 6458efa5 2020-11-24 stsp case CTRL('b'):
7479 1c5e5faa 2022-06-23 thomas case 'b':
7480 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7481 70f17a53 2022-06-13 thomas s->selected -= MIN(nscroll, s->selected);
7482 70f17a53 2022-06-13 thomas ref_scroll_up(s, MAX(0, nscroll));
7483 07b0611c 2022-06-23 thomas if (s->selected_entry == TAILQ_FIRST(&s->refs))
7484 07b0611c 2022-06-23 thomas view->count = 0;
7487 6458efa5 2020-11-24 stsp case KEY_DOWN:
7488 f7140bf5 2021-10-17 thomas case CTRL('n'):
7489 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
7490 6458efa5 2020-11-24 stsp s->selected++;
7493 07b0611c 2022-06-23 thomas if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7494 6458efa5 2020-11-24 stsp /* can't scroll any further */
7495 07b0611c 2022-06-23 thomas view->count = 0;
7498 a5d43cac 2022-07-01 thomas ref_scroll_down(view, 1);
7500 70f17a53 2022-06-13 thomas case CTRL('d'):
7501 23427b14 2022-06-23 thomas case 'd':
7502 70f17a53 2022-06-13 thomas nscroll /= 2;
7503 70f17a53 2022-06-13 thomas /* FALL THROUGH */
7504 6458efa5 2020-11-24 stsp case KEY_NPAGE:
7505 6458efa5 2020-11-24 stsp case CTRL('f'):
7506 1c5e5faa 2022-06-23 thomas case 'f':
7507 4c2d69cb 2022-06-23 thomas case ' ':
7508 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7509 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
7510 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
7511 70f17a53 2022-06-13 thomas s->selected += MIN(nscroll,
7512 70f17a53 2022-06-13 thomas s->ndisplayed - s->selected - 1);
7513 07b0611c 2022-06-23 thomas if (view->count > 1 && s->selected < s->ndisplayed - 1)
7514 07b0611c 2022-06-23 thomas s->selected += s->ndisplayed - s->selected - 1;
7515 07b0611c 2022-06-23 thomas view->count = 0;
7518 a5d43cac 2022-07-01 thomas ref_scroll_down(view, nscroll);
7520 6458efa5 2020-11-24 stsp case CTRL('l'):
7521 07b0611c 2022-06-23 thomas view->count = 0;
7522 8924d611 2020-12-26 stsp tog_free_refs();
7523 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
7526 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
7527 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
7529 6458efa5 2020-11-24 stsp case KEY_RESIZE:
7530 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7531 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
7534 07b0611c 2022-06-23 thomas view->count = 0;
7538 6458efa5 2020-11-24 stsp return err;
7541 6458efa5 2020-11-24 stsp __dead static void
7542 6458efa5 2020-11-24 stsp usage_ref(void)
7545 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7546 6458efa5 2020-11-24 stsp getprogname());
7550 6458efa5 2020-11-24 stsp static const struct got_error *
7551 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
7553 6458efa5 2020-11-24 stsp const struct got_error *error;
7554 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
7555 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
7556 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
7558 6458efa5 2020-11-24 stsp struct tog_view *view;
7559 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7561 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
7562 6458efa5 2020-11-24 stsp switch (ch) {
7564 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
7565 6458efa5 2020-11-24 stsp if (repo_path == NULL)
7566 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
7570 e99e2d15 2020-11-24 naddy usage_ref();
7571 6458efa5 2020-11-24 stsp /* NOTREACHED */
7575 6458efa5 2020-11-24 stsp argc -= optind;
7576 6458efa5 2020-11-24 stsp argv += optind;
7578 6458efa5 2020-11-24 stsp if (argc > 1)
7579 e99e2d15 2020-11-24 naddy usage_ref();
7581 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7582 7cd52833 2022-06-23 thomas if (error != NULL)
7583 7cd52833 2022-06-23 thomas goto done;
7585 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
7586 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
7587 c156c7a4 2020-12-18 stsp if (cwd == NULL)
7588 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
7589 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
7590 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7591 c156c7a4 2020-12-18 stsp goto done;
7592 6458efa5 2020-11-24 stsp if (worktree)
7593 6458efa5 2020-11-24 stsp repo_path =
7594 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
7596 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
7597 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
7598 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
7599 c156c7a4 2020-12-18 stsp goto done;
7603 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7604 6458efa5 2020-11-24 stsp if (error != NULL)
7605 6458efa5 2020-11-24 stsp goto done;
7607 6458efa5 2020-11-24 stsp init_curses();
7609 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
7610 51a10b52 2020-12-26 stsp if (error)
7611 51a10b52 2020-12-26 stsp goto done;
7613 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7614 6458efa5 2020-11-24 stsp if (error)
7615 6458efa5 2020-11-24 stsp goto done;
7617 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7618 6458efa5 2020-11-24 stsp if (view == NULL) {
7619 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
7620 6458efa5 2020-11-24 stsp goto done;
7623 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
7624 6458efa5 2020-11-24 stsp if (error)
7625 6458efa5 2020-11-24 stsp goto done;
7627 6458efa5 2020-11-24 stsp if (worktree) {
7628 6458efa5 2020-11-24 stsp /* Release work tree lock. */
7629 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
7630 6458efa5 2020-11-24 stsp worktree = NULL;
7632 6458efa5 2020-11-24 stsp error = view_loop(view);
7634 6458efa5 2020-11-24 stsp free(repo_path);
7635 6458efa5 2020-11-24 stsp free(cwd);
7636 1d0f4054 2021-06-17 stsp if (repo) {
7637 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7638 1d0f4054 2021-06-17 stsp if (close_err)
7639 1d0f4054 2021-06-17 stsp error = close_err;
7641 7cd52833 2022-06-23 thomas if (pack_fds) {
7642 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7643 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7644 7cd52833 2022-06-23 thomas if (error == NULL)
7645 7cd52833 2022-06-23 thomas error = pack_err;
7647 51a10b52 2020-12-26 stsp tog_free_refs();
7648 6458efa5 2020-11-24 stsp return error;
7652 a5d43cac 2022-07-01 thomas * If view was scrolled down to move the selected line into view when opening a
7653 a5d43cac 2022-07-01 thomas * horizontal split, scroll back up when closing the split/toggling fullscreen.
7655 6458efa5 2020-11-24 stsp static void
7656 a5d43cac 2022-07-01 thomas offset_selection_up(struct tog_view *view)
7658 a5d43cac 2022-07-01 thomas switch (view->type) {
7659 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
7660 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
7661 a5d43cac 2022-07-01 thomas if (s->first_displayed_line == 1) {
7662 a5d43cac 2022-07-01 thomas s->selected_line = MAX(s->selected_line - view->offset,
7666 a5d43cac 2022-07-01 thomas if (s->first_displayed_line > view->offset)
7667 a5d43cac 2022-07-01 thomas s->first_displayed_line -= view->offset;
7669 a5d43cac 2022-07-01 thomas s->first_displayed_line = 1;
7670 a5d43cac 2022-07-01 thomas s->selected_line += view->offset;
7673 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG:
7674 a5d43cac 2022-07-01 thomas log_scroll_up(&view->state.log, view->offset);
7675 a5d43cac 2022-07-01 thomas view->state.log.selected += view->offset;
7677 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF:
7678 a5d43cac 2022-07-01 thomas ref_scroll_up(&view->state.ref, view->offset);
7679 a5d43cac 2022-07-01 thomas view->state.ref.selected += view->offset;
7681 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE:
7682 a5d43cac 2022-07-01 thomas tree_scroll_up(&view->state.tree, view->offset);
7683 a5d43cac 2022-07-01 thomas view->state.tree.selected += view->offset;
7685 a5d43cac 2022-07-01 thomas default:
7689 a5d43cac 2022-07-01 thomas view->offset = 0;
7693 a5d43cac 2022-07-01 thomas * If the selected line is in the section of screen covered by the bottom split,
7694 a5d43cac 2022-07-01 thomas * scroll down offset lines to move it into view and index its new position.
7696 a5d43cac 2022-07-01 thomas static const struct got_error *
7697 a5d43cac 2022-07-01 thomas offset_selection_down(struct tog_view *view)
7699 a5d43cac 2022-07-01 thomas const struct got_error *err = NULL;
7700 a5d43cac 2022-07-01 thomas const struct got_error *(*scrolld)(struct tog_view *, int);
7701 a5d43cac 2022-07-01 thomas int *selected = NULL;
7702 a5d43cac 2022-07-01 thomas int header, offset;
7704 a5d43cac 2022-07-01 thomas switch (view->type) {
7705 a5d43cac 2022-07-01 thomas case TOG_VIEW_BLAME: {
7706 a5d43cac 2022-07-01 thomas struct tog_blame_view_state *s = &view->state.blame;
7707 a5d43cac 2022-07-01 thomas header = 3;
7708 a5d43cac 2022-07-01 thomas scrolld = NULL;
7709 a5d43cac 2022-07-01 thomas if (s->selected_line > view->nlines - header) {
7710 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - s->selected_line - header);
7711 a5d43cac 2022-07-01 thomas s->first_displayed_line += offset;
7712 a5d43cac 2022-07-01 thomas s->selected_line -= offset;
7713 a5d43cac 2022-07-01 thomas view->offset = offset;
7717 a5d43cac 2022-07-01 thomas case TOG_VIEW_LOG: {
7718 a5d43cac 2022-07-01 thomas struct tog_log_view_state *s = &view->state.log;
7719 a5d43cac 2022-07-01 thomas scrolld = &log_scroll_down;
7720 a5d43cac 2022-07-01 thomas header = 3;
7721 a5d43cac 2022-07-01 thomas selected = &s->selected;
7724 a5d43cac 2022-07-01 thomas case TOG_VIEW_REF: {
7725 a5d43cac 2022-07-01 thomas struct tog_ref_view_state *s = &view->state.ref;
7726 a5d43cac 2022-07-01 thomas scrolld = &ref_scroll_down;
7727 a5d43cac 2022-07-01 thomas header = 3;
7728 a5d43cac 2022-07-01 thomas selected = &s->selected;
7731 a5d43cac 2022-07-01 thomas case TOG_VIEW_TREE: {
7732 a5d43cac 2022-07-01 thomas struct tog_tree_view_state *s = &view->state.tree;
7733 a5d43cac 2022-07-01 thomas scrolld = &tree_scroll_down;
7734 a5d43cac 2022-07-01 thomas header = 5;
7735 a5d43cac 2022-07-01 thomas selected = &s->selected;
7738 a5d43cac 2022-07-01 thomas default:
7739 a5d43cac 2022-07-01 thomas selected = NULL;
7740 a5d43cac 2022-07-01 thomas scrolld = NULL;
7741 a5d43cac 2022-07-01 thomas header = 0;
7745 a5d43cac 2022-07-01 thomas if (selected && *selected > view->nlines - header) {
7746 a5d43cac 2022-07-01 thomas offset = abs(view->nlines - *selected - header);
7747 a5d43cac 2022-07-01 thomas view->offset = offset;
7748 a5d43cac 2022-07-01 thomas if (scrolld && offset) {
7749 a5d43cac 2022-07-01 thomas err = scrolld(view, offset);
7750 a5d43cac 2022-07-01 thomas *selected -= offset;
7754 a5d43cac 2022-07-01 thomas return err;
7757 a5d43cac 2022-07-01 thomas static void
7758 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
7762 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
7763 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
7764 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
7765 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
7767 6879ba42 2020-10-01 naddy fputc('\n', fp);
7770 4ed7e80c 2018-05-20 stsp __dead static void
7771 6879ba42 2020-10-01 naddy usage(int hflag, int status)
7773 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
7775 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7776 6879ba42 2020-10-01 naddy getprogname());
7777 6879ba42 2020-10-01 naddy if (hflag) {
7778 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
7779 6879ba42 2020-10-01 naddy list_commands(fp);
7781 6879ba42 2020-10-01 naddy exit(status);
7784 c2301be8 2018-04-30 stsp static char **
7785 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
7787 ee85c5e8 2020-02-29 stsp va_list ap;
7788 c2301be8 2018-04-30 stsp char **argv;
7791 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
7793 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
7794 c2301be8 2018-04-30 stsp if (argv == NULL)
7795 c2301be8 2018-04-30 stsp err(1, "calloc");
7796 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
7797 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
7798 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
7799 e10c916e 2019-09-15 hiltjo err(1, "strdup");
7802 ee85c5e8 2020-02-29 stsp va_end(ap);
7803 c2301be8 2018-04-30 stsp return argv;
7807 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
7808 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
7809 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
7810 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
7811 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
7813 ee85c5e8 2020-02-29 stsp static const struct got_error *
7814 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
7816 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
7817 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
7818 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
7819 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
7820 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
7821 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
7822 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7823 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
7824 7cd52833 2022-06-23 thomas int *pack_fds = NULL;
7826 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
7827 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
7828 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
7830 7cd52833 2022-06-23 thomas error = got_repo_pack_fds_open(&pack_fds);
7831 7cd52833 2022-06-23 thomas if (error != NULL)
7832 7cd52833 2022-06-23 thomas goto done;
7834 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
7835 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7836 ee85c5e8 2020-02-29 stsp goto done;
7838 ee85c5e8 2020-02-29 stsp if (worktree)
7839 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
7841 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
7842 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
7843 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
7844 ee85c5e8 2020-02-29 stsp goto done;
7847 7cd52833 2022-06-23 thomas error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7848 ee85c5e8 2020-02-29 stsp if (error != NULL)
7849 ee85c5e8 2020-02-29 stsp goto done;
7851 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7852 ee85c5e8 2020-02-29 stsp repo, worktree);
7853 ee85c5e8 2020-02-29 stsp if (error)
7854 ee85c5e8 2020-02-29 stsp goto done;
7856 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
7857 84de9106 2020-12-26 stsp if (error)
7858 84de9106 2020-12-26 stsp goto done;
7859 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7860 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7861 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7862 ee85c5e8 2020-02-29 stsp if (error)
7863 ee85c5e8 2020-02-29 stsp goto done;
7865 ee85c5e8 2020-02-29 stsp if (worktree) {
7866 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7867 ee85c5e8 2020-02-29 stsp worktree = NULL;
7870 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
7871 945f9229 2022-04-16 thomas if (error)
7872 945f9229 2022-04-16 thomas goto done;
7874 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7875 ee85c5e8 2020-02-29 stsp if (error) {
7876 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
7877 ee85c5e8 2020-02-29 stsp goto done;
7878 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
7879 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
7880 6879ba42 2020-10-01 naddy usage(1, 1);
7881 ee85c5e8 2020-02-29 stsp /* not reached */
7884 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7885 1d0f4054 2021-06-17 stsp if (error == NULL)
7886 1d0f4054 2021-06-17 stsp error = close_err;
7887 ee85c5e8 2020-02-29 stsp repo = NULL;
7889 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
7890 ee85c5e8 2020-02-29 stsp if (error)
7891 ee85c5e8 2020-02-29 stsp goto done;
7893 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
7895 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7896 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
7898 1d0f4054 2021-06-17 stsp if (repo) {
7899 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
7900 1d0f4054 2021-06-17 stsp if (error == NULL)
7901 1d0f4054 2021-06-17 stsp error = close_err;
7903 945f9229 2022-04-16 thomas if (commit)
7904 945f9229 2022-04-16 thomas got_object_commit_close(commit);
7905 ee85c5e8 2020-02-29 stsp if (worktree)
7906 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
7907 7cd52833 2022-06-23 thomas if (pack_fds) {
7908 7cd52833 2022-06-23 thomas const struct got_error *pack_err =
7909 7cd52833 2022-06-23 thomas got_repo_pack_fds_close(pack_fds);
7910 7cd52833 2022-06-23 thomas if (error == NULL)
7911 7cd52833 2022-06-23 thomas error = pack_err;
7914 ee85c5e8 2020-02-29 stsp free(commit_id_str);
7915 ee85c5e8 2020-02-29 stsp free(commit_id);
7916 ee85c5e8 2020-02-29 stsp free(cwd);
7917 ee85c5e8 2020-02-29 stsp free(repo_path);
7918 ee85c5e8 2020-02-29 stsp free(in_repo_path);
7919 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
7921 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
7922 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
7923 ee85c5e8 2020-02-29 stsp free(cmd_argv);
7925 87670572 2020-12-26 naddy tog_free_refs();
7926 ee85c5e8 2020-02-29 stsp return error;
7930 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
7932 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
7933 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
7934 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
7935 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
7936 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
7937 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
7938 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
7940 adf4c9e0 2022-07-03 thomas char *diff_algo_str = NULL;
7942 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
7944 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7945 9f7d7167 2018-04-29 stsp switch (ch) {
7947 9f7d7167 2018-04-29 stsp hflag = 1;
7950 53ccebc2 2019-07-30 stsp Vflag = 1;
7953 6879ba42 2020-10-01 naddy usage(hflag, 1);
7954 9f7d7167 2018-04-29 stsp /* NOTREACHED */
7958 9f7d7167 2018-04-29 stsp argc -= optind;
7959 9f7d7167 2018-04-29 stsp argv += optind;
7960 9814e6a3 2020-09-27 naddy optind = 1;
7961 c2301be8 2018-04-30 stsp optreset = 1;
7963 53ccebc2 2019-07-30 stsp if (Vflag) {
7964 53ccebc2 2019-07-30 stsp got_version_print_str();
7965 6879ba42 2020-10-01 naddy return 0;
7968 4010e238 2020-12-04 stsp #ifndef PROFILE
7969 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7970 4010e238 2020-12-04 stsp NULL) == -1)
7971 4010e238 2020-12-04 stsp err(1, "pledge");
7974 c2301be8 2018-04-30 stsp if (argc == 0) {
7975 f29d3e89 2018-06-23 stsp if (hflag)
7976 6879ba42 2020-10-01 naddy usage(hflag, 0);
7977 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
7978 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
7980 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
7984 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
7985 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
7986 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
7987 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
7988 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
7994 adf4c9e0 2022-07-03 thomas diff_algo_str = getenv("TOG_DIFF_ALGORITHM");
7995 adf4c9e0 2022-07-03 thomas if (diff_algo_str) {
7996 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "patience") == 0)
7997 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
7998 adf4c9e0 2022-07-03 thomas if (strcasecmp(diff_algo_str, "myers") == 0)
7999 adf4c9e0 2022-07-03 thomas tog_diff_algo = GOT_DIFF_ALGORITHM_MYERS;
8002 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
8003 ee85c5e8 2020-02-29 stsp if (argc != 1)
8004 6879ba42 2020-10-01 naddy usage(0, 1);
8005 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
8006 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
8008 ee85c5e8 2020-02-29 stsp if (hflag)
8009 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
8011 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
8015 b46c1e04 2020-09-20 naddy putchar('\n');
8016 a2f4a359 2020-02-28 stsp if (cmd_argv) {
8018 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
8019 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
8020 a2f4a359 2020-02-28 stsp free(cmd_argv);
8023 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
8024 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);