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 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
117 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
118 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
119 ba4f502b 2018-08-04 stsp struct got_object_id *id;
120 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
123 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
124 ba4f502b 2018-08-04 stsp struct commit_queue {
125 ba4f502b 2018-08-04 stsp int ncommits;
126 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
129 f26dddb7 2019-11-08 stsp struct tog_color {
130 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
131 6d17833f 2019-11-08 stsp regex_t regex;
132 6d17833f 2019-11-08 stsp short colorpair;
134 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
136 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
137 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
139 2183bbf6 2022-01-23 thomas static const struct got_error *
140 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
141 2183bbf6 2022-01-23 thomas struct got_reference* re2)
143 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
144 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
145 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
147 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
148 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
149 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
150 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
151 2183bbf6 2022-01-23 thomas *cmp = -1;
152 2183bbf6 2022-01-23 thomas return NULL;
153 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
154 2183bbf6 2022-01-23 thomas *cmp = 1;
155 2183bbf6 2022-01-23 thomas return NULL;
158 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
159 2183bbf6 2022-01-23 thomas return NULL;
162 11b20872 2019-11-08 stsp static const struct got_error *
163 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
165 51a10b52 2020-12-26 stsp const struct got_error *err;
167 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
168 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
171 51a10b52 2020-12-26 stsp return err;
173 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
177 51a10b52 2020-12-26 stsp static void
178 51a10b52 2020-12-26 stsp tog_free_refs(void)
180 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
181 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
182 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
184 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
187 51a10b52 2020-12-26 stsp static const struct got_error *
188 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
189 11b20872 2019-11-08 stsp int idx, short color)
191 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
192 11b20872 2019-11-08 stsp struct tog_color *tc;
193 11b20872 2019-11-08 stsp int regerr = 0;
195 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
196 11b20872 2019-11-08 stsp return NULL;
198 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
200 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
201 11b20872 2019-11-08 stsp if (tc == NULL)
202 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
203 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
204 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
205 11b20872 2019-11-08 stsp if (regerr) {
206 11b20872 2019-11-08 stsp static char regerr_msg[512];
207 11b20872 2019-11-08 stsp static char err_msg[512];
208 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
209 11b20872 2019-11-08 stsp sizeof(regerr_msg));
210 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
211 11b20872 2019-11-08 stsp regerr_msg);
212 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
214 11b20872 2019-11-08 stsp return err;
216 11b20872 2019-11-08 stsp tc->colorpair = idx;
217 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
218 11b20872 2019-11-08 stsp return NULL;
221 11b20872 2019-11-08 stsp static void
222 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
224 11b20872 2019-11-08 stsp struct tog_color *tc;
226 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
227 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
228 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
229 11b20872 2019-11-08 stsp regfree(&tc->regex);
234 11b20872 2019-11-08 stsp struct tog_color *
235 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
237 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
239 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
240 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
244 11b20872 2019-11-08 stsp return NULL;
248 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
250 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
251 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
252 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
253 11b20872 2019-11-08 stsp return COLOR_CYAN;
254 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
255 11b20872 2019-11-08 stsp return COLOR_YELLOW;
256 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
257 11b20872 2019-11-08 stsp return COLOR_GREEN;
258 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
259 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
260 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
261 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
262 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
263 91b8c405 2020-01-25 stsp return COLOR_CYAN;
264 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
265 11b20872 2019-11-08 stsp return COLOR_GREEN;
266 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
267 11b20872 2019-11-08 stsp return COLOR_GREEN;
268 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
269 11b20872 2019-11-08 stsp return COLOR_CYAN;
270 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
271 11b20872 2019-11-08 stsp return COLOR_YELLOW;
272 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
273 6458efa5 2020-11-24 stsp return COLOR_GREEN;
274 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
275 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
276 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
277 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
278 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
279 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
285 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
287 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
289 11b20872 2019-11-08 stsp if (val == NULL)
290 11b20872 2019-11-08 stsp return default_color_value(envvar);
292 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
293 11b20872 2019-11-08 stsp return COLOR_BLACK;
294 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
295 11b20872 2019-11-08 stsp return COLOR_RED;
296 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
297 11b20872 2019-11-08 stsp return COLOR_GREEN;
298 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
299 11b20872 2019-11-08 stsp return COLOR_YELLOW;
300 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
301 11b20872 2019-11-08 stsp return COLOR_BLUE;
302 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
303 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
304 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
305 11b20872 2019-11-08 stsp return COLOR_CYAN;
306 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
307 11b20872 2019-11-08 stsp return COLOR_WHITE;
308 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
311 11b20872 2019-11-08 stsp return default_color_value(envvar);
315 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
316 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
317 3dbaef42 2020-11-24 stsp const char *label1, *label2;
319 15a087fe 2019-02-21 stsp int first_displayed_line;
320 15a087fe 2019-02-21 stsp int last_displayed_line;
322 15a087fe 2019-02-21 stsp int diff_context;
323 3dbaef42 2020-11-24 stsp int ignore_whitespace;
324 64453f7e 2020-11-21 stsp int force_text_diff;
325 15a087fe 2019-02-21 stsp struct got_repository *repo;
326 bddb1296 2019-11-08 stsp struct tog_colors colors;
327 fe621944 2020-11-10 stsp size_t nlines;
328 f44b1f58 2020-02-02 tracey off_t *line_offsets;
329 f44b1f58 2020-02-02 tracey int matched_line;
330 f44b1f58 2020-02-02 tracey int selected_line;
332 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
333 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
336 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
338 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
339 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
340 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
341 1a76625f 2018-10-22 stsp int commits_needed;
342 fb280deb 2021-08-30 stsp int load_all;
343 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
344 1a76625f 2018-10-22 stsp struct commit_queue *commits;
345 1a76625f 2018-10-22 stsp const char *in_repo_path;
346 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
347 1a76625f 2018-10-22 stsp struct got_repository *repo;
348 1a76625f 2018-10-22 stsp int log_complete;
349 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
350 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
351 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
352 13add988 2019-10-15 stsp int *searching;
353 13add988 2019-10-15 stsp int *search_next_done;
354 13add988 2019-10-15 stsp regex_t *regex;
357 1a76625f 2018-10-22 stsp struct tog_log_view_state {
358 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
359 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
360 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
361 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
362 b01e7d3b 2018-08-04 stsp int selected;
363 b01e7d3b 2018-08-04 stsp char *in_repo_path;
364 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
365 b672a97a 2020-01-27 stsp int log_branches;
366 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
367 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
368 1a76625f 2018-10-22 stsp sig_atomic_t quit;
369 1a76625f 2018-10-22 stsp pthread_t thread;
370 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
371 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
372 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
373 11b20872 2019-11-08 stsp struct tog_colors colors;
376 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
377 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
378 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
379 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
380 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
381 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
382 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
383 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
384 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
385 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
386 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
387 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
388 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
389 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
390 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
392 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
393 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
394 e9424729 2018-08-04 stsp int nlines;
396 e9424729 2018-08-04 stsp struct tog_view *view;
397 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
401 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
402 e9424729 2018-08-04 stsp const char *path;
403 e9424729 2018-08-04 stsp struct got_repository *repo;
404 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
405 e9424729 2018-08-04 stsp int *complete;
406 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
407 fc06ba56 2019-08-22 stsp void *cancel_arg;
410 e9424729 2018-08-04 stsp struct tog_blame {
412 be659d10 2020-11-18 stsp off_t filesize;
413 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
414 6fcac457 2018-11-19 stsp int nlines;
415 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
416 e9424729 2018-08-04 stsp pthread_t thread;
417 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
418 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
419 e9424729 2018-08-04 stsp const char *path;
422 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
423 7cbe629d 2018-08-04 stsp int first_displayed_line;
424 7cbe629d 2018-08-04 stsp int last_displayed_line;
425 7cbe629d 2018-08-04 stsp int selected_line;
426 7cbe629d 2018-08-04 stsp int blame_complete;
429 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
430 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
431 e5a0f69f 2018-08-18 stsp char *path;
432 7cbe629d 2018-08-04 stsp struct got_repository *repo;
433 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
434 e9424729 2018-08-04 stsp struct tog_blame blame;
435 6c4c42e0 2019-06-24 stsp int matched_line;
436 11b20872 2019-11-08 stsp struct tog_colors colors;
439 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
440 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
441 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
442 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
443 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
444 ad80ab7b 2018-08-04 stsp int selected;
447 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
449 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
450 ad80ab7b 2018-08-04 stsp char *tree_label;
451 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
452 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
453 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
454 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
455 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
456 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
457 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
458 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
459 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
460 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
461 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
462 6458efa5 2020-11-24 stsp struct tog_colors colors;
465 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
466 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
467 6458efa5 2020-11-24 stsp struct got_reference *ref;
471 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
473 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
474 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
475 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
476 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
477 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
478 3bfadbd4 2021-11-20 thomas int nrefs, ndisplayed, selected, show_ids, sort_by_date;
479 6458efa5 2020-11-24 stsp struct got_repository *repo;
480 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
481 bddb1296 2019-11-08 stsp struct tog_colors colors;
485 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
487 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
488 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
489 669b5ffa 2018-10-07 stsp * there is enough screen estate.
491 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
492 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
494 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
495 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
496 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
498 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
499 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
501 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
503 cc3c9aac 2018-08-01 stsp struct tog_view {
504 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
505 26ed57b2 2018-05-19 stsp WINDOW *window;
506 26ed57b2 2018-05-19 stsp PANEL *panel;
507 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
508 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
509 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
511 669b5ffa 2018-10-07 stsp struct tog_view *parent;
512 669b5ffa 2018-10-07 stsp struct tog_view *child;
515 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
516 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
517 e78dc838 2020-12-04 stsp * between parent and child.
518 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
519 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
520 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
521 e78dc838 2020-12-04 stsp * situations.
523 e78dc838 2020-12-04 stsp int focus_child;
525 5dc9f4bc 2018-08-04 stsp /* type-specific state */
526 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
528 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
529 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
530 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
531 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
532 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
535 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
536 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
537 e78dc838 2020-12-04 stsp struct tog_view *, int);
538 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
540 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
541 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
542 c0c4acc8 2021-01-24 stsp int search_started;
543 60493ae3 2019-06-20 stsp int searching;
544 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
545 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
546 60493ae3 2019-06-20 stsp int search_next_done;
547 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
548 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
549 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
550 1803e47f 2019-06-22 stsp regex_t regex;
551 41605754 2020-11-12 stsp regmatch_t regmatch;
554 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
555 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
556 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
557 78756c87 2020-11-24 stsp struct got_repository *);
558 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
559 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
560 e78dc838 2020-12-04 stsp struct tog_view *, int);
561 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
562 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
563 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
565 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
566 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
567 78756c87 2020-11-24 stsp const char *, const char *, int);
568 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
569 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
570 e78dc838 2020-12-04 stsp struct tog_view *, int);
571 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
572 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
573 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
575 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
576 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
577 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
578 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
579 e78dc838 2020-12-04 stsp struct tog_view *, int);
580 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
581 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
582 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
584 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
585 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
586 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
587 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_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_tree_view(struct tog_view *);
590 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
591 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
593 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
594 6458efa5 2020-11-24 stsp struct got_repository *);
595 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
596 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
597 e78dc838 2020-12-04 stsp struct tog_view *, int);
598 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
599 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
600 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
602 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
603 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
604 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
606 25791caa 2018-10-24 stsp static void
607 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
609 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
612 83baff54 2019-08-12 stsp static void
613 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
615 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
618 61266923 2020-01-14 stsp static void
619 61266923 2020-01-14 stsp tog_sigcont(int signo)
621 61266923 2020-01-14 stsp tog_sigcont_received = 1;
624 e5a0f69f 2018-08-18 stsp static const struct got_error *
625 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
627 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
629 669b5ffa 2018-10-07 stsp if (view->child) {
630 669b5ffa 2018-10-07 stsp view_close(view->child);
631 669b5ffa 2018-10-07 stsp view->child = NULL;
633 e5a0f69f 2018-08-18 stsp if (view->close)
634 e5a0f69f 2018-08-18 stsp err = view->close(view);
635 ea5e7bb5 2018-08-01 stsp if (view->panel)
636 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
637 ea5e7bb5 2018-08-01 stsp if (view->window)
638 ea5e7bb5 2018-08-01 stsp delwin(view->window);
639 ea5e7bb5 2018-08-01 stsp free(view);
640 e5a0f69f 2018-08-18 stsp return err;
643 ea5e7bb5 2018-08-01 stsp static struct tog_view *
644 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
645 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
647 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
649 ea5e7bb5 2018-08-01 stsp if (view == NULL)
650 ea5e7bb5 2018-08-01 stsp return NULL;
652 d6b05b5b 2018-08-04 stsp view->type = type;
653 f7d12f7e 2018-08-01 stsp view->lines = LINES;
654 f7d12f7e 2018-08-01 stsp view->cols = COLS;
655 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
656 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
657 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
658 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
659 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
660 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
661 96a765a8 2018-08-04 stsp view_close(view);
662 ea5e7bb5 2018-08-01 stsp return NULL;
664 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
665 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
666 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
667 96a765a8 2018-08-04 stsp view_close(view);
668 ea5e7bb5 2018-08-01 stsp return NULL;
671 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
672 ea5e7bb5 2018-08-01 stsp return view;
676 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
678 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
680 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
683 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
685 5c60c32a 2018-10-18 stsp static const struct got_error *
686 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
688 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
690 5c60c32a 2018-10-18 stsp view->begin_y = 0;
691 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
692 5c60c32a 2018-10-18 stsp view->nlines = LINES;
693 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
694 5c60c32a 2018-10-18 stsp view->lines = LINES;
695 5c60c32a 2018-10-18 stsp view->cols = COLS;
696 5c60c32a 2018-10-18 stsp err = view_resize(view);
698 5c60c32a 2018-10-18 stsp return err;
700 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
701 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
703 5c60c32a 2018-10-18 stsp return NULL;
706 5c60c32a 2018-10-18 stsp static const struct got_error *
707 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
709 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
711 5c60c32a 2018-10-18 stsp view->begin_x = 0;
712 5c60c32a 2018-10-18 stsp view->begin_y = 0;
713 5c60c32a 2018-10-18 stsp view->nlines = LINES;
714 5c60c32a 2018-10-18 stsp view->ncols = COLS;
715 5c60c32a 2018-10-18 stsp view->lines = LINES;
716 5c60c32a 2018-10-18 stsp view->cols = COLS;
717 5c60c32a 2018-10-18 stsp err = view_resize(view);
719 5c60c32a 2018-10-18 stsp return err;
721 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
722 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
724 5c60c32a 2018-10-18 stsp return NULL;
728 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
730 5c60c32a 2018-10-18 stsp return view->parent == NULL;
733 4d8c2215 2018-08-19 stsp static const struct got_error *
734 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
736 f7d12f7e 2018-08-01 stsp int nlines, ncols;
738 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
739 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
741 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
743 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
744 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
746 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
748 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
749 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
750 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
751 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
752 25791caa 2018-10-24 stsp wclear(view->window);
754 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
755 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
756 0cf4efb1 2018-09-29 stsp view->lines = LINES;
757 0cf4efb1 2018-09-29 stsp view->cols = COLS;
759 6e3e5d9c 2018-10-18 stsp if (view->child) {
760 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
761 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
762 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
763 5c60c32a 2018-10-18 stsp if (view->child->focussed)
764 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
766 5c60c32a 2018-10-18 stsp show_panel(view->panel);
768 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
769 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
773 5c60c32a 2018-10-18 stsp return NULL;
776 669b5ffa 2018-10-07 stsp static const struct got_error *
777 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
779 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
781 669b5ffa 2018-10-07 stsp if (view->child == NULL)
782 669b5ffa 2018-10-07 stsp return NULL;
784 669b5ffa 2018-10-07 stsp err = view_close(view->child);
785 669b5ffa 2018-10-07 stsp view->child = NULL;
786 669b5ffa 2018-10-07 stsp return err;
789 72a9cb46 2020-12-03 stsp static void
790 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
792 669b5ffa 2018-10-07 stsp view->child = child;
793 669b5ffa 2018-10-07 stsp child->parent = view;
797 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
799 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
802 34bc9ec9 2019-02-22 stsp static void
803 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
805 25791caa 2018-10-24 stsp int cols, lines;
806 25791caa 2018-10-24 stsp struct winsize size;
808 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
809 25791caa 2018-10-24 stsp cols = 80; /* Default */
810 25791caa 2018-10-24 stsp lines = 24;
812 25791caa 2018-10-24 stsp cols = size.ws_col;
813 25791caa 2018-10-24 stsp lines = size.ws_row;
815 25791caa 2018-10-24 stsp resize_term(lines, cols);
818 2b49a8ae 2019-06-22 stsp static const struct got_error *
819 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
821 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
822 2b49a8ae 2019-06-22 stsp char pattern[1024];
825 c0c4acc8 2021-01-24 stsp if (view->search_started) {
826 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
827 c0c4acc8 2021-01-24 stsp view->searching = 0;
828 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
830 c0c4acc8 2021-01-24 stsp view->search_started = 0;
832 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
833 2b49a8ae 2019-06-22 stsp return NULL;
835 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
836 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
838 2b49a8ae 2019-06-22 stsp nocbreak();
840 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
843 2b49a8ae 2019-06-22 stsp if (ret == ERR)
844 2b49a8ae 2019-06-22 stsp return NULL;
846 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
847 7c32bd05 2019-06-22 stsp err = view->search_start(view);
849 7c32bd05 2019-06-22 stsp regfree(&view->regex);
850 7c32bd05 2019-06-22 stsp return err;
852 c0c4acc8 2021-01-24 stsp view->search_started = 1;
853 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
854 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
855 2b49a8ae 2019-06-22 stsp view->search_next(view);
858 2b49a8ae 2019-06-22 stsp return NULL;
861 0cf4efb1 2018-09-29 stsp static const struct got_error *
862 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
863 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
865 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
866 669b5ffa 2018-10-07 stsp struct tog_view *v;
867 1a76625f 2018-10-22 stsp int ch, errcode;
869 e5a0f69f 2018-08-18 stsp *new = NULL;
871 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
872 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
873 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
874 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
876 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
877 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
878 82954512 2020-02-03 stsp if (errcode)
879 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
880 82954512 2020-02-03 stsp "pthread_mutex_unlock");
881 3da8ef85 2021-09-21 stsp sched_yield();
882 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
883 82954512 2020-02-03 stsp if (errcode)
884 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
885 82954512 2020-02-03 stsp "pthread_mutex_lock");
886 60493ae3 2019-06-20 stsp view->search_next(view);
887 60493ae3 2019-06-20 stsp return NULL;
890 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
891 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
892 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
893 1a76625f 2018-10-22 stsp if (errcode)
894 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
895 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
896 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
897 1a76625f 2018-10-22 stsp if (errcode)
898 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
899 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
901 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
902 25791caa 2018-10-24 stsp tog_resizeterm();
903 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
904 61266923 2020-01-14 stsp tog_sigcont_received = 0;
905 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
906 25791caa 2018-10-24 stsp err = view_resize(v);
908 25791caa 2018-10-24 stsp return err;
909 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
911 25791caa 2018-10-24 stsp return err;
912 cdfcfb03 2020-12-06 stsp if (v->child) {
913 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
915 cdfcfb03 2020-12-06 stsp return err;
916 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
917 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
919 cdfcfb03 2020-12-06 stsp return err;
924 e5a0f69f 2018-08-18 stsp switch (ch) {
926 1e37a5c2 2019-05-12 jcs if (view->child) {
927 e78dc838 2020-12-04 stsp view->focussed = 0;
928 e78dc838 2020-12-04 stsp view->child->focussed = 1;
929 e78dc838 2020-12-04 stsp view->focus_child = 1;
930 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
931 e78dc838 2020-12-04 stsp view->focussed = 0;
932 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
933 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
937 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
938 9970f7fc 2020-12-03 stsp view->dying = 1;
944 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
945 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
947 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
948 e78dc838 2020-12-04 stsp view->focussed = 0;
949 e78dc838 2020-12-04 stsp view->child->focussed = 1;
950 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
952 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
955 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
956 9970f7fc 2020-12-03 stsp KEY_RESIZE);
958 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
959 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
960 e78dc838 2020-12-04 stsp view->focussed = 1;
961 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
963 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
967 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
970 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
973 60493ae3 2019-06-20 stsp if (view->search_start)
974 2b49a8ae 2019-06-22 stsp view_search_start(view);
976 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
980 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
981 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
982 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
983 60493ae3 2019-06-20 stsp view->search_next_done = 0;
984 60493ae3 2019-06-20 stsp view->search_next(view);
986 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
989 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
993 e5a0f69f 2018-08-18 stsp return err;
997 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
999 0cf4efb1 2018-09-29 stsp PANEL *panel;
1000 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
1002 669b5ffa 2018-10-07 stsp if (view->parent)
1003 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
1005 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
1006 0cf4efb1 2018-09-29 stsp if (panel == NULL)
1009 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
1010 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1011 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1015 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1017 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1018 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1020 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1022 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1025 669b5ffa 2018-10-07 stsp return view->focussed;
1028 bcbd79e2 2018-08-19 stsp static const struct got_error *
1029 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1031 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1032 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1033 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1034 fd823528 2018-10-22 stsp int fast_refresh = 10;
1035 1a76625f 2018-10-22 stsp int done = 0, errcode;
1037 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1038 1a76625f 2018-10-22 stsp if (errcode)
1039 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1041 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1042 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1044 1004088d 2018-09-29 stsp view->focussed = 1;
1045 878940b7 2018-09-29 stsp err = view->show(view);
1047 0cf4efb1 2018-09-29 stsp return err;
1048 0cf4efb1 2018-09-29 stsp update_panels();
1049 0cf4efb1 2018-09-29 stsp doupdate();
1050 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1051 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1052 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1053 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1055 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1058 9970f7fc 2020-12-03 stsp if (view->dying) {
1059 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1061 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1062 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1064 e78dc838 2020-12-04 stsp else if (view->parent)
1065 669b5ffa 2018-10-07 stsp prev = view->parent;
1067 e78dc838 2020-12-04 stsp if (view->parent) {
1068 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1069 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1071 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1073 9970f7fc 2020-12-03 stsp err = view_close(view);
1075 e5a0f69f 2018-08-18 stsp goto done;
1077 e78dc838 2020-12-04 stsp view = NULL;
1078 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1079 e78dc838 2020-12-04 stsp if (v->focussed)
1082 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1083 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1085 e78dc838 2020-12-04 stsp view = prev;
1086 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1087 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1088 e78dc838 2020-12-04 stsp tog_view_list_head);
1090 e78dc838 2020-12-04 stsp if (view) {
1091 e78dc838 2020-12-04 stsp if (view->focus_child) {
1092 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1093 e78dc838 2020-12-04 stsp view = view->child;
1095 e78dc838 2020-12-04 stsp view->focussed = 1;
1099 bcbd79e2 2018-08-19 stsp if (new_view) {
1100 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1101 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1102 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1103 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1105 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1106 86c66b02 2018-10-18 stsp err = view_close(v);
1108 86c66b02 2018-10-18 stsp goto done;
1111 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1112 fed7eaa8 2018-10-24 stsp view = new_view;
1114 669b5ffa 2018-10-07 stsp if (view) {
1115 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1116 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1117 e78dc838 2020-12-04 stsp view = view->child;
1119 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1120 e78dc838 2020-12-04 stsp view = view->parent;
1122 e78dc838 2020-12-04 stsp show_panel(view->panel);
1123 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1124 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1125 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1126 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1128 1a76625f 2018-10-22 stsp goto done;
1130 669b5ffa 2018-10-07 stsp err = view->show(view);
1132 1a76625f 2018-10-22 stsp goto done;
1133 669b5ffa 2018-10-07 stsp if (view->child) {
1134 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1136 1a76625f 2018-10-22 stsp goto done;
1138 1a76625f 2018-10-22 stsp update_panels();
1139 1a76625f 2018-10-22 stsp doupdate();
1143 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1144 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1145 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1146 e5a0f69f 2018-08-18 stsp view_close(view);
1149 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1150 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1151 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1153 e5a0f69f 2018-08-18 stsp return err;
1156 4ed7e80c 2018-05-20 stsp __dead static void
1157 9f7d7167 2018-04-29 stsp usage_log(void)
1160 c70c5802 2018-08-01 stsp fprintf(stderr,
1161 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1162 9f7d7167 2018-04-29 stsp getprogname());
1166 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1167 80ddbec8 2018-04-29 stsp static const struct got_error *
1168 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1170 00dfcb92 2018-06-11 stsp char *vis = NULL;
1171 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1173 963b370f 2018-05-20 stsp *ws = NULL;
1174 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1175 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1176 00dfcb92 2018-06-11 stsp int vislen;
1177 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1178 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1180 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1181 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1183 00dfcb92 2018-06-11 stsp return err;
1184 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1185 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1186 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1187 a7f50699 2018-06-11 stsp goto done;
1191 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1192 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1193 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1194 a7f50699 2018-06-11 stsp goto done;
1197 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1198 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1200 00dfcb92 2018-06-11 stsp free(vis);
1201 963b370f 2018-05-20 stsp if (err) {
1202 963b370f 2018-05-20 stsp free(*ws);
1203 963b370f 2018-05-20 stsp *ws = NULL;
1204 963b370f 2018-05-20 stsp *wlen = 0;
1206 963b370f 2018-05-20 stsp return err;
1209 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1210 963b370f 2018-05-20 stsp static const struct got_error *
1211 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1212 27a741e5 2019-09-11 stsp int col_tab_align)
1214 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1215 963b370f 2018-05-20 stsp int cols = 0;
1216 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1217 963b370f 2018-05-20 stsp size_t wlen;
1220 963b370f 2018-05-20 stsp *wlinep = NULL;
1221 b700b5d6 2018-07-10 stsp *widthp = 0;
1223 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1225 963b370f 2018-05-20 stsp return err;
1227 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1228 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1231 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1232 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1237 27a741e5 2019-09-11 stsp while (i < wlen) {
1238 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1240 27a741e5 2019-09-11 stsp if (width == 0) {
1245 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1246 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1248 27a741e5 2019-09-11 stsp cols += width;
1250 27a741e5 2019-09-11 stsp } else if (width == -1) {
1251 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1252 27a741e5 2019-09-11 stsp width = TABSIZE -
1253 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1255 748d5cab 2020-12-14 naddy width = 1;
1256 748d5cab 2020-12-14 naddy wline[i] = L'.';
1258 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1260 748d5cab 2020-12-14 naddy cols += width;
1263 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1264 963b370f 2018-05-20 stsp goto done;
1267 963b370f 2018-05-20 stsp wline[i] = L'\0';
1268 b700b5d6 2018-07-10 stsp if (widthp)
1269 b700b5d6 2018-07-10 stsp *widthp = cols;
1272 963b370f 2018-05-20 stsp free(wline);
1274 963b370f 2018-05-20 stsp *wlinep = wline;
1275 963b370f 2018-05-20 stsp return err;
1278 8b473291 2019-02-21 stsp static const struct got_error*
1279 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1280 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1282 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1283 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1285 8b473291 2019-02-21 stsp const char *name;
1287 8b473291 2019-02-21 stsp *refs_str = NULL;
1289 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1290 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1291 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1294 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1295 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1297 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1298 8b473291 2019-02-21 stsp name += 5;
1299 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1300 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1302 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1303 8b473291 2019-02-21 stsp name += 6;
1304 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1305 8b473291 2019-02-21 stsp name += 8;
1306 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1307 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1310 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1313 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1314 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1315 5d844a1e 2019-08-13 stsp if (err) {
1316 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1317 48cae60d 2020-09-22 stsp free(ref_id);
1320 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1321 5d844a1e 2019-08-13 stsp err = NULL;
1322 5d844a1e 2019-08-13 stsp tag = NULL;
1325 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1326 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1327 48cae60d 2020-09-22 stsp free(ref_id);
1329 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1330 52b5abe1 2019-08-13 stsp if (cmp != 0)
1332 8b473291 2019-02-21 stsp s = *refs_str;
1333 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1334 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1335 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1337 8b473291 2019-02-21 stsp *refs_str = NULL;
1343 8b473291 2019-02-21 stsp return err;
1346 963b370f 2018-05-20 stsp static const struct got_error *
1347 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1348 27a741e5 2019-09-11 stsp int col_tab_align)
1350 e6b8b890 2020-12-29 naddy char *smallerthan;
1352 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1353 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1354 5813d178 2019-03-09 stsp author = smallerthan + 1;
1355 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1356 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1359 5813d178 2019-03-09 stsp static const struct got_error *
1360 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1361 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1362 8fdc79fe 2020-12-01 naddy int author_display_cols)
1364 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1365 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1366 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1367 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1368 5813d178 2019-03-09 stsp char *author = NULL;
1369 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1370 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1371 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1372 bb737323 2018-05-20 stsp int col, limit;
1373 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1374 ccb26ccd 2018-11-05 stsp struct tm tm;
1375 45d799e2 2018-12-23 stsp time_t committer_time;
1376 11b20872 2019-11-08 stsp struct tog_color *tc;
1378 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1379 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1380 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1381 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1382 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1384 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1385 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1387 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1388 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1390 11b20872 2019-11-08 stsp wattr_on(view->window,
1391 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1392 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1394 11b20872 2019-11-08 stsp wattr_off(view->window,
1395 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1396 27a741e5 2019-09-11 stsp col = limit;
1397 b39d25c7 2018-07-10 stsp if (col > avail)
1398 b39d25c7 2018-07-10 stsp goto done;
1400 6570a66d 2019-11-08 stsp if (avail >= 120) {
1401 6570a66d 2019-11-08 stsp char *id_str;
1402 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1404 6570a66d 2019-11-08 stsp goto done;
1405 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1407 11b20872 2019-11-08 stsp wattr_on(view->window,
1408 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1409 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1411 11b20872 2019-11-08 stsp wattr_off(view->window,
1412 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1413 6570a66d 2019-11-08 stsp free(id_str);
1415 6570a66d 2019-11-08 stsp if (col > avail)
1416 6570a66d 2019-11-08 stsp goto done;
1419 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1420 5813d178 2019-03-09 stsp if (author == NULL) {
1421 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1422 80ddbec8 2018-04-29 stsp goto done;
1424 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1426 bb737323 2018-05-20 stsp goto done;
1427 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1429 11b20872 2019-11-08 stsp wattr_on(view->window,
1430 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1431 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1433 11b20872 2019-11-08 stsp wattr_off(view->window,
1434 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1435 bb737323 2018-05-20 stsp col += author_width;
1436 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1437 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1439 bb737323 2018-05-20 stsp author_width++;
1441 9c2eaf34 2018-05-20 stsp if (col > avail)
1442 9c2eaf34 2018-05-20 stsp goto done;
1444 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1446 6d9fbc00 2018-04-29 stsp goto done;
1447 bb737323 2018-05-20 stsp logmsg = logmsg0;
1448 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1450 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1451 bb737323 2018-05-20 stsp if (newline)
1452 bb737323 2018-05-20 stsp *newline = '\0';
1453 bb737323 2018-05-20 stsp limit = avail - col;
1454 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1456 bb737323 2018-05-20 stsp goto done;
1457 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1458 bb737323 2018-05-20 stsp col += logmsg_width;
1459 27a741e5 2019-09-11 stsp while (col < avail) {
1460 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1464 80ddbec8 2018-04-29 stsp free(logmsg0);
1465 bb737323 2018-05-20 stsp free(wlogmsg);
1466 5813d178 2019-03-09 stsp free(author);
1467 bb737323 2018-05-20 stsp free(wauthor);
1468 80ddbec8 2018-04-29 stsp free(line);
1469 80ddbec8 2018-04-29 stsp return err;
1472 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1473 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1474 899d86c2 2018-05-10 stsp struct got_object_id *id)
1476 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1478 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1479 80ddbec8 2018-04-29 stsp if (entry == NULL)
1480 899d86c2 2018-05-10 stsp return NULL;
1482 899d86c2 2018-05-10 stsp entry->id = id;
1483 99db9666 2018-05-07 stsp entry->commit = commit;
1484 899d86c2 2018-05-10 stsp return entry;
1487 99db9666 2018-05-07 stsp static void
1488 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1490 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1492 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1493 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1494 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1495 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1496 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1497 99db9666 2018-05-07 stsp free(entry);
1500 99db9666 2018-05-07 stsp static void
1501 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1503 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1504 99db9666 2018-05-07 stsp pop_commit(commits);
1507 c4972b91 2018-05-07 stsp static const struct got_error *
1508 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1509 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1511 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1512 13add988 2019-10-15 stsp regmatch_t regmatch;
1513 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1515 13add988 2019-10-15 stsp *have_match = 0;
1517 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1519 13add988 2019-10-15 stsp return err;
1521 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1523 13add988 2019-10-15 stsp goto done;
1525 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1526 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1527 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1528 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1529 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1530 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1531 13add988 2019-10-15 stsp *have_match = 1;
1533 13add988 2019-10-15 stsp free(id_str);
1534 13add988 2019-10-15 stsp free(logmsg);
1535 13add988 2019-10-15 stsp return err;
1538 13add988 2019-10-15 stsp static const struct got_error *
1539 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1541 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1544 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1545 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1546 1a76625f 2018-10-22 stsp * while updating the display.
1549 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1550 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1551 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1552 1a76625f 2018-10-22 stsp int errcode;
1554 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1555 4e0d2870 2020-12-07 naddy NULL, NULL);
1556 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1559 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1562 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1563 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1564 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1568 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1569 1a76625f 2018-10-22 stsp if (errcode) {
1570 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1571 13add988 2019-10-15 stsp "pthread_mutex_lock");
1575 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1576 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1577 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1579 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1580 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1581 7c1452c1 2020-03-26 stsp int have_match;
1582 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1585 7c1452c1 2020-03-26 stsp if (have_match)
1586 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1589 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1590 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1591 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1592 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1595 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1597 9ba79e04 2018-06-11 stsp return err;
1600 2b779855 2020-12-05 naddy static void
1601 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1603 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1604 2b779855 2020-12-05 naddy int ncommits = 0;
1606 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1607 2b779855 2020-12-05 naddy while (entry) {
1608 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1609 2b779855 2020-12-05 naddy s->selected_entry = entry;
1612 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1613 2b779855 2020-12-05 naddy ncommits++;
1617 0553a4e3 2018-04-30 stsp static const struct got_error *
1618 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1620 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1621 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1622 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1623 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1624 60493ae3 2019-06-20 stsp int width;
1625 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1626 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1627 8b473291 2019-02-21 stsp char *refs_str = NULL;
1628 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1629 11b20872 2019-11-08 stsp struct tog_color *tc;
1630 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1632 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1633 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1634 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1635 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1637 ecb28ae0 2018-07-16 stsp return err;
1638 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1639 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1640 d2075bf3 2020-12-25 stsp if (refs) {
1641 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1642 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1644 d2075bf3 2020-12-25 stsp goto done;
1648 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1649 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1651 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1652 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1653 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1654 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1655 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1656 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1657 8f4ed634 2020-03-26 stsp goto done;
1660 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1662 f9686aa5 2020-03-27 stsp if (view->searching) {
1663 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1664 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1665 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1666 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1667 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1668 f9686aa5 2020-03-27 stsp search_str = "searching...";
1671 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1672 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1673 f9686aa5 2020-03-27 stsp search_str ? search_str :
1674 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1675 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1676 8f4ed634 2020-03-26 stsp goto done;
1680 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1681 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1682 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1683 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1684 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1685 1a76625f 2018-10-22 stsp header = NULL;
1686 1a76625f 2018-10-22 stsp goto done;
1688 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1689 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1690 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1691 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1692 1a76625f 2018-10-22 stsp header = NULL;
1693 1a76625f 2018-10-22 stsp goto done;
1695 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1697 1a76625f 2018-10-22 stsp goto done;
1699 2814baeb 2018-08-01 stsp werase(view->window);
1701 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1702 a3404814 2018-09-02 stsp wstandout(view->window);
1703 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1705 11b20872 2019-11-08 stsp wattr_on(view->window,
1706 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1707 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1709 11b20872 2019-11-08 stsp wattr_off(view->window,
1710 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1711 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1712 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1715 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1716 a3404814 2018-09-02 stsp wstandend(view->window);
1717 ecb28ae0 2018-07-16 stsp free(wline);
1718 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1719 1a76625f 2018-10-22 stsp goto done;
1721 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1722 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1723 5813d178 2019-03-09 stsp ncommits = 0;
1724 5813d178 2019-03-09 stsp while (entry) {
1725 5813d178 2019-03-09 stsp char *author;
1726 5813d178 2019-03-09 stsp wchar_t *wauthor;
1727 5813d178 2019-03-09 stsp int width;
1728 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1730 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1731 5813d178 2019-03-09 stsp if (author == NULL) {
1732 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1733 5813d178 2019-03-09 stsp goto done;
1735 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1736 27a741e5 2019-09-11 stsp date_display_cols);
1737 5813d178 2019-03-09 stsp if (author_cols < width)
1738 5813d178 2019-03-09 stsp author_cols = width;
1739 5813d178 2019-03-09 stsp free(wauthor);
1740 5813d178 2019-03-09 stsp free(author);
1741 7ca04879 2019-10-19 stsp ncommits++;
1742 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1745 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1746 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1747 867c6645 2018-07-10 stsp ncommits = 0;
1748 899d86c2 2018-05-10 stsp while (entry) {
1749 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1751 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1752 2814baeb 2018-08-01 stsp wstandout(view->window);
1753 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1754 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1755 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1756 2814baeb 2018-08-01 stsp wstandend(view->window);
1758 60493ae3 2019-06-20 stsp goto done;
1759 0553a4e3 2018-04-30 stsp ncommits++;
1760 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1761 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1764 1a57306a 2018-09-02 stsp view_vborder(view);
1765 dd038bc6 2021-09-21 thomas.ad update_panels();
1766 dd038bc6 2021-09-21 thomas.ad doupdate();
1768 1a76625f 2018-10-22 stsp free(id_str);
1769 8b473291 2019-02-21 stsp free(refs_str);
1770 1a76625f 2018-10-22 stsp free(ncommits_str);
1771 1a76625f 2018-10-22 stsp free(header);
1772 80ddbec8 2018-04-29 stsp return err;
1775 07b55e75 2018-05-10 stsp static void
1776 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1778 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1779 07b55e75 2018-05-10 stsp int nscrolled = 0;
1781 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1782 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1785 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1786 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1787 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1788 07b55e75 2018-05-10 stsp if (entry) {
1789 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1790 07b55e75 2018-05-10 stsp nscrolled++;
1795 aa075928 2018-05-10 stsp static const struct got_error *
1796 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1798 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1799 5e224a3e 2019-02-22 stsp int errcode;
1801 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1803 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
1804 ffe38506 2020-12-01 naddy if (ta->log_complete)
1807 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1808 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1809 7aafa0d1 2019-02-22 stsp if (errcode)
1810 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1811 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1814 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1815 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1817 7c1452c1 2020-03-26 stsp if (!wait)
1820 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1821 ffe38506 2020-12-01 naddy show_log_view(view);
1822 7c1452c1 2020-03-26 stsp update_panels();
1823 7c1452c1 2020-03-26 stsp doupdate();
1825 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1826 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1827 82954512 2020-02-03 stsp if (errcode)
1828 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1829 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1831 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1832 ffe38506 2020-12-01 naddy show_log_view(view);
1833 7c1452c1 2020-03-26 stsp update_panels();
1834 7c1452c1 2020-03-26 stsp doupdate();
1837 5e224a3e 2019-02-22 stsp return NULL;
1840 5e224a3e 2019-02-22 stsp static const struct got_error *
1841 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1843 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1844 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1845 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1846 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1848 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1849 5e224a3e 2019-02-22 stsp return NULL;
1851 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1852 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1853 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1855 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1857 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1858 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1860 5e224a3e 2019-02-22 stsp return err;
1864 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1865 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1868 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1870 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1871 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1873 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1874 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1876 dd0a52c1 2018-05-20 stsp return err;
1879 cd0acaa7 2018-05-20 stsp static const struct got_error *
1880 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1881 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1882 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1884 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1885 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1886 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1888 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1889 15a94983 2018-12-23 stsp if (diff_view == NULL)
1890 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1892 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1893 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1894 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1895 e5a0f69f 2018-08-18 stsp if (err == NULL)
1896 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1897 cd0acaa7 2018-05-20 stsp return err;
1900 80ddbec8 2018-04-29 stsp static const struct got_error *
1901 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1902 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1904 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1906 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1907 941e9f74 2019-05-21 stsp if (parent == NULL)
1908 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1910 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1911 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1912 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1913 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1914 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1915 941e9f74 2019-05-21 stsp s->tree = subtree;
1916 941e9f74 2019-05-21 stsp s->selected = 0;
1917 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1918 941e9f74 2019-05-21 stsp return NULL;
1921 941e9f74 2019-05-21 stsp static const struct got_error *
1922 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1923 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
1925 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1926 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1927 941e9f74 2019-05-21 stsp const char *p;
1928 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1930 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1932 941e9f74 2019-05-21 stsp while (*p) {
1933 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1934 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1935 56e0773d 2019-11-28 stsp char *te_name;
1937 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1940 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1941 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1942 941e9f74 2019-05-21 stsp if (slash == NULL)
1943 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1945 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1946 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1947 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1950 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1951 56e0773d 2019-11-28 stsp if (te == NULL) {
1952 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1953 56e0773d 2019-11-28 stsp free(te_name);
1956 56e0773d 2019-11-28 stsp free(te_name);
1957 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1959 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1960 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1962 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1963 941e9f74 2019-05-21 stsp if (slash)
1964 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1966 941e9f74 2019-05-21 stsp subpath = strdup(path);
1967 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1968 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1972 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
1977 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
1978 941e9f74 2019-05-21 stsp free(tree_id);
1982 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
1983 941e9f74 2019-05-21 stsp if (err) {
1984 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1987 941e9f74 2019-05-21 stsp if (slash == NULL)
1989 941e9f74 2019-05-21 stsp free(subpath);
1990 941e9f74 2019-05-21 stsp subpath = NULL;
1991 941e9f74 2019-05-21 stsp p = slash;
1994 941e9f74 2019-05-21 stsp free(subpath);
1995 1a76625f 2018-10-22 stsp return err;
1998 61266923 2020-01-14 stsp static const struct got_error *
1999 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2000 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2001 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2003 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2004 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2005 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2007 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2008 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2009 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2011 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2013 55cccc34 2020-02-20 stsp return err;
2014 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2016 55cccc34 2020-02-20 stsp *new_view = tree_view;
2018 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2019 55cccc34 2020-02-20 stsp return NULL;
2021 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2024 55cccc34 2020-02-20 stsp static const struct got_error *
2025 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2027 61266923 2020-01-14 stsp sigset_t sigset;
2028 61266923 2020-01-14 stsp int errcode;
2030 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2031 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2033 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
2034 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2035 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2036 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2037 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2039 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2040 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2041 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2043 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2044 61266923 2020-01-14 stsp if (errcode)
2045 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2047 61266923 2020-01-14 stsp return NULL;
2050 1a76625f 2018-10-22 stsp static void *
2051 1a76625f 2018-10-22 stsp log_thread(void *arg)
2053 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2054 1a76625f 2018-10-22 stsp int errcode = 0;
2055 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2056 1a76625f 2018-10-22 stsp int done = 0;
2058 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2060 61266923 2020-01-14 stsp return (void *)err;
2062 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
2063 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2064 1a76625f 2018-10-22 stsp if (err) {
2065 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2066 1a76625f 2018-10-22 stsp return (void *)err;
2067 1a76625f 2018-10-22 stsp err = NULL;
2069 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2070 1a76625f 2018-10-22 stsp a->commits_needed--;
2072 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2073 3abe8080 2019-04-10 stsp if (errcode) {
2074 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2075 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2077 3abe8080 2019-04-10 stsp } else if (*a->quit)
2079 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2080 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2081 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2082 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2085 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2086 7c1452c1 2020-03-26 stsp if (errcode) {
2087 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2088 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2089 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2094 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2096 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2097 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2098 7c1452c1 2020-03-26 stsp &tog_mutex);
2099 7c1452c1 2020-03-26 stsp if (errcode)
2100 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2101 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2102 21355643 2020-12-06 stsp if (*a->quit)
2107 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2108 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2109 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2110 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2112 3abe8080 2019-04-10 stsp a->log_complete = 1;
2113 1a76625f 2018-10-22 stsp return (void *)err;
2116 1a76625f 2018-10-22 stsp static const struct got_error *
2117 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2119 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2120 1a76625f 2018-10-22 stsp int errcode;
2122 1a76625f 2018-10-22 stsp if (s->thread) {
2123 1a76625f 2018-10-22 stsp s->quit = 1;
2124 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2125 1a76625f 2018-10-22 stsp if (errcode)
2126 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2127 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2128 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2129 1a76625f 2018-10-22 stsp if (errcode)
2130 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2131 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2132 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2133 1a76625f 2018-10-22 stsp if (errcode)
2134 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2135 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2136 1a76625f 2018-10-22 stsp if (errcode)
2137 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2138 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2139 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2142 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2143 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2144 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2147 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2148 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2149 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2152 9343a5fb 2018-06-23 stsp return err;
2155 9343a5fb 2018-06-23 stsp static const struct got_error *
2156 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2158 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2159 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2160 276b94a1 2020-11-13 naddy int errcode;
2162 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2164 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2165 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2166 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2168 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2169 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2170 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2172 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2173 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2174 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2175 1a76625f 2018-10-22 stsp free(s->start_id);
2176 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2177 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2178 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2179 1a76625f 2018-10-22 stsp return err;
2182 1a76625f 2018-10-22 stsp static const struct got_error *
2183 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2185 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2187 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2188 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2189 60493ae3 2019-06-20 stsp return NULL;
2192 60493ae3 2019-06-20 stsp static const struct got_error *
2193 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2195 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2196 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2197 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2199 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2200 f9686aa5 2020-03-27 stsp show_log_view(view);
2201 f9686aa5 2020-03-27 stsp update_panels();
2202 f9686aa5 2020-03-27 stsp doupdate();
2204 96e2b566 2019-07-08 stsp if (s->search_entry) {
2205 13add988 2019-10-15 stsp int errcode, ch;
2206 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2207 13add988 2019-10-15 stsp if (errcode)
2208 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2209 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2210 13add988 2019-10-15 stsp ch = wgetch(view->window);
2211 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2212 13add988 2019-10-15 stsp if (errcode)
2213 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2214 13add988 2019-10-15 stsp "pthread_mutex_lock");
2215 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2216 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2217 678cbce5 2019-07-28 stsp return NULL;
2219 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2220 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2222 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2223 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2224 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2225 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2226 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2228 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2229 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2231 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2234 60493ae3 2019-06-20 stsp while (1) {
2235 13add988 2019-10-15 stsp int have_match = 0;
2237 60493ae3 2019-06-20 stsp if (entry == NULL) {
2238 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2239 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2240 f9967bca 2020-03-27 stsp view->search_next_done =
2241 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2242 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2243 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2244 f801134a 2019-06-25 stsp return NULL;
2247 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2248 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2249 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2251 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2252 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2255 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2256 13add988 2019-10-15 stsp &view->regex);
2259 13add988 2019-10-15 stsp if (have_match) {
2260 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2261 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2265 96e2b566 2019-07-08 stsp s->search_entry = entry;
2266 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2267 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2269 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2272 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2273 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2274 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2275 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2277 60493ae3 2019-06-20 stsp return err;
2280 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2281 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2283 60493ae3 2019-06-20 stsp return err;
2288 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2290 60493ae3 2019-06-20 stsp return NULL;
2293 60493ae3 2019-06-20 stsp static const struct got_error *
2294 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2295 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2296 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2298 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2299 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2300 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2301 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2302 1a76625f 2018-10-22 stsp int errcode;
2304 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2305 f135c941 2020-02-20 stsp free(s->in_repo_path);
2306 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2307 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2308 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2311 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2312 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2313 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2315 fb2756b9 2018-08-04 stsp s->repo = repo;
2316 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2317 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2318 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2319 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2320 9cd7cbd1 2020-12-07 stsp goto done;
2323 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2324 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2325 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2326 5036bf37 2018-09-24 stsp goto done;
2328 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2330 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2331 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2332 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2333 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2335 11b20872 2019-11-08 stsp goto done;
2336 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2337 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2338 11b20872 2019-11-08 stsp if (err) {
2339 11b20872 2019-11-08 stsp free_colors(&s->colors);
2340 11b20872 2019-11-08 stsp goto done;
2342 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2343 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2344 11b20872 2019-11-08 stsp if (err) {
2345 11b20872 2019-11-08 stsp free_colors(&s->colors);
2346 11b20872 2019-11-08 stsp goto done;
2350 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2351 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2352 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2353 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2354 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2356 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2358 1a76625f 2018-10-22 stsp goto done;
2359 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2360 b672a97a 2020-01-27 stsp !s->log_branches);
2362 1a76625f 2018-10-22 stsp goto done;
2363 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2364 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2366 c5b78334 2020-01-12 stsp goto done;
2368 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2369 1a76625f 2018-10-22 stsp if (errcode) {
2370 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2371 1a76625f 2018-10-22 stsp goto done;
2373 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2374 7c1452c1 2020-03-26 stsp if (errcode) {
2375 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2376 7c1452c1 2020-03-26 stsp goto done;
2379 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2380 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2381 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2382 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2383 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2384 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2385 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2386 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2387 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2388 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2389 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2390 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2391 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2394 1a76625f 2018-10-22 stsp close_log_view(view);
2395 ba4f502b 2018-08-04 stsp return err;
2398 e5a0f69f 2018-08-18 stsp static const struct got_error *
2399 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2401 f2f6d207 2020-11-24 stsp const struct got_error *err;
2402 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2404 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
2405 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2406 2b380cc8 2018-10-24 stsp &s->thread_args);
2407 2b380cc8 2018-10-24 stsp if (errcode)
2408 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2409 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2410 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2412 f2f6d207 2020-11-24 stsp return err;
2416 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2419 e5a0f69f 2018-08-18 stsp static const struct got_error *
2420 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2422 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2423 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2424 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2425 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2426 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2427 f3bc9f1d 2021-09-05 naddy int begin_x = 0, n;
2429 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2430 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2431 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2432 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2433 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2434 fb280deb 2021-08-30 stsp log_scroll_down(view, s->commits.ncommits);
2435 fb280deb 2021-08-30 stsp s->selected = MIN(view->nlines - 2,
2436 fb280deb 2021-08-30 stsp s->commits.ncommits - 1);
2437 fb280deb 2021-08-30 stsp select_commit(s);
2439 528dedf3 2021-08-30 stsp return NULL;
2442 528dedf3 2021-08-30 stsp switch (ch) {
2444 1e37a5c2 2019-05-12 jcs s->quit = 1;
2447 1e37a5c2 2019-05-12 jcs case KEY_UP:
2450 f7140bf5 2021-10-17 thomas case CTRL('p'):
2451 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2453 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2454 1e37a5c2 2019-05-12 jcs s->selected--;
2456 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2457 912a3f79 2021-08-30 j select_commit(s);
2460 912a3f79 2021-08-30 j case KEY_HOME:
2461 912a3f79 2021-08-30 j s->selected = 0;
2462 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2463 2b779855 2020-12-05 naddy select_commit(s);
2465 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2466 a4292ac5 2019-05-12 jcs case CTRL('b'):
2467 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2469 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2470 1e37a5c2 2019-05-12 jcs s->selected = 0;
2472 2b779855 2020-12-05 naddy log_scroll_up(s, view->nlines - 1);
2473 2b779855 2020-12-05 naddy select_commit(s);
2476 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2479 f7140bf5 2021-10-17 thomas case CTRL('n'):
2480 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2482 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2483 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2484 1e37a5c2 2019-05-12 jcs s->selected++;
2486 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2490 912a3f79 2021-08-30 j select_commit(s);
2493 912a3f79 2021-08-30 j case KEY_END: {
2494 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2495 912a3f79 2021-08-30 j * traverse them all. */
2496 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2497 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2498 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2501 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2502 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2503 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2504 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2506 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2507 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2509 f3bc9f1d 2021-09-05 naddy if (n > 0)
2510 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2511 2b779855 2020-12-05 naddy select_commit(s);
2514 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2515 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2516 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2517 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2518 1e37a5c2 2019-05-12 jcs if (first == NULL)
2520 ffe38506 2020-12-01 naddy err = log_scroll_down(view, view->nlines - 1);
2523 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2524 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2525 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2526 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2527 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2528 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2530 2b779855 2020-12-05 naddy select_commit(s);
2533 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2534 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2535 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2536 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2537 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2538 2b779855 2020-12-05 naddy select_commit(s);
2539 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2540 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2541 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2542 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2543 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2546 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2549 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2551 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2552 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2553 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2554 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2555 78756c87 2020-11-24 stsp view, s->repo);
2558 e78dc838 2020-12-04 stsp view->focussed = 0;
2559 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2560 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2561 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2563 1e37a5c2 2019-05-12 jcs return err;
2564 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2565 e78dc838 2020-12-04 stsp view->focus_child = 1;
2567 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2570 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2572 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2573 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2574 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2575 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2579 e78dc838 2020-12-04 stsp view->focussed = 0;
2580 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2581 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2582 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2584 1e37a5c2 2019-05-12 jcs return err;
2585 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2586 e78dc838 2020-12-04 stsp view->focus_child = 1;
2588 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2590 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2591 21355643 2020-12-06 stsp case CTRL('l'):
2593 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2594 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2596 21355643 2020-12-06 stsp err = stop_log_thread(s);
2598 74cfe85e 2020-10-20 stsp return err;
2599 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2600 21355643 2020-12-06 stsp char *parent_path;
2601 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2603 21355643 2020-12-06 stsp return err;
2604 21355643 2020-12-06 stsp free(s->in_repo_path);
2605 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2606 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2607 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2608 21355643 2020-12-06 stsp struct got_object_id *start_id;
2609 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2610 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2611 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2613 21355643 2020-12-06 stsp return err;
2614 21355643 2020-12-06 stsp free(s->start_id);
2615 21355643 2020-12-06 stsp s->start_id = start_id;
2616 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2617 21355643 2020-12-06 stsp } else /* 'B' */
2618 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2620 21355643 2020-12-06 stsp err = got_repo_open(&s->thread_args.repo,
2621 21355643 2020-12-06 stsp got_repo_get_path(s->repo), NULL);
2623 21355643 2020-12-06 stsp return err;
2624 51a10b52 2020-12-26 stsp tog_free_refs();
2625 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
2627 ca51c541 2020-12-07 stsp return err;
2628 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2629 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2631 d01904d4 2019-06-25 stsp return err;
2632 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2633 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2635 b672a97a 2020-01-27 stsp return err;
2636 21355643 2020-12-06 stsp free_commits(&s->commits);
2637 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2638 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2639 21355643 2020-12-06 stsp s->selected_entry = NULL;
2640 21355643 2020-12-06 stsp s->selected = 0;
2641 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2642 21355643 2020-12-06 stsp s->quit = 0;
2643 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2646 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2647 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2648 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2649 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2650 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2651 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2652 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2653 6458efa5 2020-11-24 stsp if (err) {
2654 6458efa5 2020-11-24 stsp view_close(ref_view);
2655 6458efa5 2020-11-24 stsp return err;
2657 e78dc838 2020-12-04 stsp view->focussed = 0;
2658 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2659 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2660 6458efa5 2020-11-24 stsp err = view_close_child(view);
2662 6458efa5 2020-11-24 stsp return err;
2663 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2664 e78dc838 2020-12-04 stsp view->focus_child = 1;
2666 6458efa5 2020-11-24 stsp *new_view = ref_view;
2672 80ddbec8 2018-04-29 stsp return err;
2675 4ed7e80c 2018-05-20 stsp static const struct got_error *
2676 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2678 c2db6724 2019-01-04 stsp const struct got_error *error;
2680 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2681 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2682 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2684 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2685 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2687 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2688 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2690 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2691 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2693 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2694 c2db6724 2019-01-04 stsp if (error != NULL)
2695 c2db6724 2019-01-04 stsp return error;
2697 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2698 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2700 c2db6724 2019-01-04 stsp return NULL;
2703 a915003a 2019-02-05 stsp static void
2704 a915003a 2019-02-05 stsp init_curses(void)
2706 a915003a 2019-02-05 stsp initscr();
2708 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2711 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2712 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2713 a915003a 2019-02-05 stsp curs_set(0);
2714 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2715 6d17833f 2019-11-08 stsp start_color();
2716 6d17833f 2019-11-08 stsp use_default_colors();
2718 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2719 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2720 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2723 c2db6724 2019-01-04 stsp static const struct got_error *
2724 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2725 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2727 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2729 f135c941 2020-02-20 stsp if (argc == 0) {
2730 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2731 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2732 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2733 f135c941 2020-02-20 stsp return NULL;
2736 f135c941 2020-02-20 stsp if (worktree) {
2737 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2740 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2742 f135c941 2020-02-20 stsp return err;
2743 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2744 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2745 bfd61697 2020-11-14 stsp p) == -1) {
2746 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2747 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2751 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2753 f135c941 2020-02-20 stsp return err;
2756 f135c941 2020-02-20 stsp static const struct got_error *
2757 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2759 80ddbec8 2018-04-29 stsp const struct got_error *error;
2760 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2761 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2762 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2763 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2764 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2765 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2766 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2767 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2768 04cc582a 2018-08-01 stsp struct tog_view *view;
2770 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2771 80ddbec8 2018-04-29 stsp switch (ch) {
2773 b672a97a 2020-01-27 stsp log_branches = 1;
2776 80ddbec8 2018-04-29 stsp start_commit = optarg;
2779 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2780 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2781 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2785 17020d27 2019-03-07 stsp usage_log();
2786 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2790 80ddbec8 2018-04-29 stsp argc -= optind;
2791 80ddbec8 2018-04-29 stsp argv += optind;
2793 f135c941 2020-02-20 stsp if (argc > 1)
2794 f135c941 2020-02-20 stsp usage_log();
2796 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2797 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2798 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2799 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2800 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2801 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2802 c156c7a4 2020-12-18 stsp goto done;
2803 a1fbf39a 2019-08-11 stsp if (worktree)
2804 6962eb72 2020-02-20 stsp repo_path =
2805 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2807 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2808 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
2809 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
2810 c156c7a4 2020-12-18 stsp goto done;
2814 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2815 80ddbec8 2018-04-29 stsp if (error != NULL)
2816 ecb28ae0 2018-07-16 stsp goto done;
2818 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2819 f135c941 2020-02-20 stsp repo, worktree);
2820 f135c941 2020-02-20 stsp if (error)
2821 f135c941 2020-02-20 stsp goto done;
2823 f135c941 2020-02-20 stsp init_curses();
2825 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2826 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2827 c02c541e 2019-03-29 stsp if (error)
2828 c02c541e 2019-03-29 stsp goto done;
2830 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
2831 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
2832 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
2833 87670572 2020-12-26 naddy if (error)
2834 87670572 2020-12-26 naddy goto done;
2837 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2838 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2839 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2840 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2841 d8f38dc4 2020-12-05 stsp if (error)
2842 d8f38dc4 2020-12-05 stsp goto done;
2843 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2845 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2846 d8f38dc4 2020-12-05 stsp if (error == NULL)
2847 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2848 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2849 d8f38dc4 2020-12-05 stsp goto done;
2850 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2851 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2852 d8f38dc4 2020-12-05 stsp if (error)
2853 d8f38dc4 2020-12-05 stsp goto done;
2856 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2857 04cc582a 2018-08-01 stsp if (view == NULL) {
2858 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2859 04cc582a 2018-08-01 stsp goto done;
2861 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2862 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2863 ba4f502b 2018-08-04 stsp if (error)
2864 ba4f502b 2018-08-04 stsp goto done;
2865 2fc00ff4 2019-08-31 stsp if (worktree) {
2866 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2867 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2868 2fc00ff4 2019-08-31 stsp worktree = NULL;
2870 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2872 f135c941 2020-02-20 stsp free(in_repo_path);
2873 ecb28ae0 2018-07-16 stsp free(repo_path);
2874 ecb28ae0 2018-07-16 stsp free(cwd);
2875 899d86c2 2018-05-10 stsp free(start_id);
2876 d8f38dc4 2020-12-05 stsp free(label);
2878 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2879 1d0f4054 2021-06-17 stsp if (repo) {
2880 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2881 1d0f4054 2021-06-17 stsp if (error == NULL)
2882 1d0f4054 2021-06-17 stsp error = close_err;
2884 ec142235 2019-03-07 stsp if (worktree)
2885 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2886 51a10b52 2020-12-26 stsp tog_free_refs();
2887 80ddbec8 2018-04-29 stsp return error;
2890 4ed7e80c 2018-05-20 stsp __dead static void
2891 9f7d7167 2018-04-29 stsp usage_diff(void)
2894 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2895 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2899 6d17833f 2019-11-08 stsp static int
2900 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2901 41605754 2020-11-12 stsp regmatch_t *regmatch)
2903 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2906 f26dddb7 2019-11-08 stsp struct tog_color *
2907 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2909 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2911 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
2912 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2913 6d17833f 2019-11-08 stsp return tc;
2916 6d17833f 2019-11-08 stsp return NULL;
2919 4ed7e80c 2018-05-20 stsp static const struct got_error *
2920 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2921 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2923 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2924 41605754 2020-11-12 stsp wchar_t *wline;
2925 41605754 2020-11-12 stsp int width;
2928 41605754 2020-11-12 stsp *wtotal = 0;
2930 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2931 41605754 2020-11-12 stsp if (s == NULL)
2932 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2934 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2935 41605754 2020-11-12 stsp if (err) {
2937 41605754 2020-11-12 stsp return err;
2939 41605754 2020-11-12 stsp waddwstr(window, wline);
2940 41605754 2020-11-12 stsp free(wline);
2942 41605754 2020-11-12 stsp wlimit -= width;
2943 41605754 2020-11-12 stsp *wtotal += width;
2945 41605754 2020-11-12 stsp if (wlimit > 0) {
2946 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2947 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2948 41605754 2020-11-12 stsp if (s == NULL) {
2949 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2951 41605754 2020-11-12 stsp return err;
2953 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2954 41605754 2020-11-12 stsp if (err) {
2956 41605754 2020-11-12 stsp return err;
2958 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2959 41605754 2020-11-12 stsp waddwstr(window, wline);
2960 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2961 41605754 2020-11-12 stsp free(wline);
2963 41605754 2020-11-12 stsp wlimit -= width;
2964 41605754 2020-11-12 stsp *wtotal += width;
2967 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2968 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2969 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
2971 41605754 2020-11-12 stsp return err;
2972 41605754 2020-11-12 stsp waddwstr(window, wline);
2973 41605754 2020-11-12 stsp free(wline);
2974 41605754 2020-11-12 stsp *wtotal += width;
2977 41605754 2020-11-12 stsp return NULL;
2980 41605754 2020-11-12 stsp static const struct got_error *
2981 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
2983 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
2984 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
2985 61e69b96 2018-05-20 stsp const struct got_error *err;
2986 fe621944 2020-11-10 stsp int nprinted = 0;
2987 b304db33 2018-05-20 stsp char *line;
2988 826082fe 2020-12-10 stsp size_t linesize = 0;
2989 826082fe 2020-12-10 stsp ssize_t linelen;
2990 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2991 61e69b96 2018-05-20 stsp wchar_t *wline;
2992 e0b650dd 2018-05-20 stsp int width;
2993 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
2994 89f1a395 2020-12-01 naddy int nlines = s->nlines;
2995 fe621944 2020-11-10 stsp off_t line_offset;
2997 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
2998 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2999 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3001 f7d12f7e 2018-08-01 stsp werase(view->window);
3003 a3404814 2018-09-02 stsp if (header) {
3004 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3005 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3006 135a2da0 2020-11-11 stsp header) == -1)
3007 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3008 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3009 135a2da0 2020-11-11 stsp free(line);
3011 a3404814 2018-09-02 stsp return err;
3013 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3014 a3404814 2018-09-02 stsp wstandout(view->window);
3015 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3016 e54cc94a 2020-11-11 stsp free(wline);
3017 e54cc94a 2020-11-11 stsp wline = NULL;
3018 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3019 a3404814 2018-09-02 stsp wstandend(view->window);
3020 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3021 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3023 a3404814 2018-09-02 stsp if (max_lines <= 1)
3024 a3404814 2018-09-02 stsp return NULL;
3025 a3404814 2018-09-02 stsp max_lines--;
3028 89f1a395 2020-12-01 naddy s->eof = 0;
3029 826082fe 2020-12-10 stsp line = NULL;
3030 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3031 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3032 826082fe 2020-12-10 stsp if (linelen == -1) {
3033 826082fe 2020-12-10 stsp if (feof(s->f)) {
3034 826082fe 2020-12-10 stsp s->eof = 1;
3037 826082fe 2020-12-10 stsp free(line);
3038 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3041 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3043 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3044 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3045 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3046 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3047 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3048 41605754 2020-11-12 stsp view->window, regmatch);
3049 41605754 2020-11-12 stsp if (err) {
3050 41605754 2020-11-12 stsp free(line);
3051 41605754 2020-11-12 stsp return err;
3054 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3055 41605754 2020-11-12 stsp if (err) {
3056 41605754 2020-11-12 stsp free(line);
3057 41605754 2020-11-12 stsp return err;
3059 41605754 2020-11-12 stsp waddwstr(view->window, wline);
3060 41605754 2020-11-12 stsp free(wline);
3061 41605754 2020-11-12 stsp wline = NULL;
3064 6d17833f 2019-11-08 stsp wattr_off(view->window,
3065 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3066 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3067 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3068 fe621944 2020-11-10 stsp nprinted++;
3070 826082fe 2020-12-10 stsp free(line);
3071 fe621944 2020-11-10 stsp if (nprinted >= 1)
3072 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3073 89f1a395 2020-12-01 naddy (nprinted - 1);
3075 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3077 1a57306a 2018-09-02 stsp view_vborder(view);
3079 89f1a395 2020-12-01 naddy if (s->eof) {
3080 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3081 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3082 c3e9aa98 2019-05-13 jcs nprinted++;
3085 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3087 c3e9aa98 2019-05-13 jcs return err;
3090 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3091 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3092 e54cc94a 2020-11-11 stsp free(wline);
3093 e54cc94a 2020-11-11 stsp wline = NULL;
3094 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3097 26ed57b2 2018-05-19 stsp return NULL;
3100 abd2672a 2018-12-23 stsp static char *
3101 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3103 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3104 09867e48 2019-08-13 stsp char *p, *s;
3106 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3107 09867e48 2019-08-13 stsp if (tm == NULL)
3108 09867e48 2019-08-13 stsp return NULL;
3109 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3110 09867e48 2019-08-13 stsp if (s == NULL)
3111 09867e48 2019-08-13 stsp return NULL;
3112 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3114 abd2672a 2018-12-23 stsp *p = '\0';
3118 4ed7e80c 2018-05-20 stsp static const struct got_error *
3119 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3120 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3122 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3123 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3124 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3125 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3127 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3128 0208f208 2020-05-05 stsp if (qid != NULL) {
3129 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3130 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3131 ec242592 2022-04-22 thomas &qid->id);
3133 0208f208 2020-05-05 stsp return err;
3135 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3136 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3137 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3138 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3139 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3141 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3145 0208f208 2020-05-05 stsp if (tree_id1) {
3146 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3148 0208f208 2020-05-05 stsp goto done;
3151 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3152 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3154 0208f208 2020-05-05 stsp goto done;
3156 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3157 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3159 0208f208 2020-05-05 stsp if (tree1)
3160 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3161 0208f208 2020-05-05 stsp if (tree2)
3162 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3163 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3164 0208f208 2020-05-05 stsp return err;
3167 0208f208 2020-05-05 stsp static const struct got_error *
3168 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3172 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3173 fe621944 2020-11-10 stsp if (p == NULL)
3174 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3175 fe621944 2020-11-10 stsp *line_offsets = p;
3176 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3177 fe621944 2020-11-10 stsp (*nlines)++;
3178 fe621944 2020-11-10 stsp return NULL;
3181 fe621944 2020-11-10 stsp static const struct got_error *
3182 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3183 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3184 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3186 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3187 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3188 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3189 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3190 45d799e2 2018-12-23 stsp time_t committer_time;
3191 45d799e2 2018-12-23 stsp const char *author, *committer;
3192 8b473291 2019-02-21 stsp char *refs_str = NULL;
3193 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3194 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3195 fe621944 2020-11-10 stsp off_t outoff = 0;
3198 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3200 8b473291 2019-02-21 stsp if (refs) {
3201 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3203 8b473291 2019-02-21 stsp return err;
3206 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3208 abd2672a 2018-12-23 stsp return err;
3210 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3211 15a94983 2018-12-23 stsp if (err) {
3212 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3213 15a94983 2018-12-23 stsp goto done;
3216 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3218 fe621944 2020-11-10 stsp goto done;
3220 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3221 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3222 fe621944 2020-11-10 stsp if (n < 0) {
3223 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3224 abd2672a 2018-12-23 stsp goto done;
3226 fe621944 2020-11-10 stsp outoff += n;
3227 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3229 fe621944 2020-11-10 stsp goto done;
3231 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3232 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3233 fe621944 2020-11-10 stsp if (n < 0) {
3234 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3235 abd2672a 2018-12-23 stsp goto done;
3237 fe621944 2020-11-10 stsp outoff += n;
3238 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3240 fe621944 2020-11-10 stsp goto done;
3242 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3243 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3244 fe621944 2020-11-10 stsp if (datestr) {
3245 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3246 fe621944 2020-11-10 stsp if (n < 0) {
3247 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3248 fe621944 2020-11-10 stsp goto done;
3250 fe621944 2020-11-10 stsp outoff += n;
3251 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3253 fe621944 2020-11-10 stsp goto done;
3255 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3256 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3257 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3258 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3259 fe621944 2020-11-10 stsp if (n < 0) {
3260 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3261 fe621944 2020-11-10 stsp goto done;
3263 fe621944 2020-11-10 stsp outoff += n;
3264 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3266 fe621944 2020-11-10 stsp goto done;
3268 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
3269 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
3270 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
3271 ac4dc263 2021-09-24 thomas int pn = 1;
3272 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
3273 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
3274 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
3275 ac4dc263 2021-09-24 thomas if (err)
3276 ac4dc263 2021-09-24 thomas goto done;
3277 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3278 ac4dc263 2021-09-24 thomas if (n < 0) {
3279 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
3280 ac4dc263 2021-09-24 thomas goto done;
3282 ac4dc263 2021-09-24 thomas outoff += n;
3283 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
3284 ac4dc263 2021-09-24 thomas if (err)
3285 ac4dc263 2021-09-24 thomas goto done;
3286 ac4dc263 2021-09-24 thomas free(id_str);
3287 ac4dc263 2021-09-24 thomas id_str = NULL;
3291 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3293 5943eee2 2019-08-13 stsp goto done;
3294 fe621944 2020-11-10 stsp s = logmsg;
3295 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3296 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3297 fe621944 2020-11-10 stsp if (n < 0) {
3298 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3299 fe621944 2020-11-10 stsp goto done;
3301 fe621944 2020-11-10 stsp outoff += n;
3302 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3304 fe621944 2020-11-10 stsp goto done;
3307 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3309 0208f208 2020-05-05 stsp goto done;
3310 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3311 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3312 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3313 fe621944 2020-11-10 stsp if (n < 0) {
3314 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3315 fe621944 2020-11-10 stsp goto done;
3317 fe621944 2020-11-10 stsp outoff += n;
3318 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3320 fe621944 2020-11-10 stsp goto done;
3321 0208f208 2020-05-05 stsp free((char *)pe->path);
3322 0208f208 2020-05-05 stsp free(pe->data);
3325 0208f208 2020-05-05 stsp fputc('\n', outfile);
3327 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3329 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3330 abd2672a 2018-12-23 stsp free(id_str);
3331 5943eee2 2019-08-13 stsp free(logmsg);
3332 8b473291 2019-02-21 stsp free(refs_str);
3333 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3334 7510f233 2020-08-09 stsp if (err) {
3335 ae6a6978 2020-08-09 stsp free(*line_offsets);
3336 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3337 ae6a6978 2020-08-09 stsp *nlines = 0;
3339 fe621944 2020-11-10 stsp return err;
3342 abd2672a 2018-12-23 stsp static const struct got_error *
3343 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3345 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3346 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3347 15a94983 2018-12-23 stsp int obj_type;
3349 fe621944 2020-11-10 stsp free(s->line_offsets);
3350 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3351 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3352 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3353 fe621944 2020-11-10 stsp s->nlines = 0;
3355 511a516b 2018-05-19 stsp f = got_opentemp();
3356 48ae06ee 2018-10-18 stsp if (f == NULL) {
3357 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3358 48ae06ee 2018-10-18 stsp goto done;
3360 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3361 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3362 fb43ecf1 2019-02-11 stsp goto done;
3366 15a94983 2018-12-23 stsp if (s->id1)
3367 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3369 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3371 15a94983 2018-12-23 stsp goto done;
3373 15a94983 2018-12-23 stsp switch (obj_type) {
3374 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3375 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3376 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->label1, s->label2, s->diff_context,
3377 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3379 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3380 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3381 cc8021af 2021-10-12 thomas s->id1, s->id2, NULL, "", "", s->diff_context,
3382 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3384 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3385 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3386 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3387 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3388 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3390 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3392 3ffacbe1 2020-02-02 tracey goto done;
3393 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3394 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3395 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3396 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3397 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3398 f44b1f58 2020-02-02 tracey if (err)
3399 f44b1f58 2020-02-02 tracey goto done;
3400 f44b1f58 2020-02-02 tracey } else {
3401 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3402 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3403 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3404 fe621944 2020-11-10 stsp err = write_commit_info(
3405 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3406 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3407 f44b1f58 2020-02-02 tracey if (err)
3408 f44b1f58 2020-02-02 tracey goto done;
3413 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3415 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3416 cc8021af 2021-10-12 thomas s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3417 3dbaef42 2020-11-24 stsp s->force_text_diff, s->repo, s->f);
3421 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3424 f44b1f58 2020-02-02 tracey if (err)
3425 f44b1f58 2020-02-02 tracey goto done;
3427 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3428 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3429 48ae06ee 2018-10-18 stsp return err;
3432 f5215bb9 2019-02-22 stsp static void
3433 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3435 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3436 f5215bb9 2019-02-22 stsp update_panels();
3437 f5215bb9 2019-02-22 stsp doupdate();
3440 f44b1f58 2020-02-02 tracey static const struct got_error *
3441 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3443 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3445 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3446 f44b1f58 2020-02-02 tracey return NULL;
3449 f44b1f58 2020-02-02 tracey static const struct got_error *
3450 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3452 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3453 f44b1f58 2020-02-02 tracey int lineno;
3454 826082fe 2020-12-10 stsp char *line = NULL;
3455 826082fe 2020-12-10 stsp size_t linesize = 0;
3456 826082fe 2020-12-10 stsp ssize_t linelen;
3458 f44b1f58 2020-02-02 tracey if (!view->searching) {
3459 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3460 f44b1f58 2020-02-02 tracey return NULL;
3463 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3464 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3465 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3467 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3469 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
3471 f44b1f58 2020-02-02 tracey while (1) {
3472 f44b1f58 2020-02-02 tracey off_t offset;
3474 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3475 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3476 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3480 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3481 f44b1f58 2020-02-02 tracey lineno = 1;
3483 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3486 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3487 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3488 f44b1f58 2020-02-02 tracey free(line);
3489 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3491 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3492 826082fe 2020-12-10 stsp if (linelen != -1 &&
3493 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3494 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3495 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3498 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3499 f44b1f58 2020-02-02 tracey lineno++;
3501 f44b1f58 2020-02-02 tracey lineno--;
3503 826082fe 2020-12-10 stsp free(line);
3505 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3506 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3507 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3510 f44b1f58 2020-02-02 tracey return NULL;
3513 48ae06ee 2018-10-18 stsp static const struct got_error *
3514 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3515 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3516 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3517 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3519 48ae06ee 2018-10-18 stsp const struct got_error *err;
3520 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3522 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3523 15a94983 2018-12-23 stsp int type1, type2;
3524 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3526 15a94983 2018-12-23 stsp return err;
3527 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3529 15a94983 2018-12-23 stsp return err;
3531 15a94983 2018-12-23 stsp if (type1 != type2)
3532 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3534 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3535 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3536 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3537 f44b1f58 2020-02-02 tracey s->repo = repo;
3538 f44b1f58 2020-02-02 tracey s->id1 = id1;
3539 f44b1f58 2020-02-02 tracey s->id2 = id2;
3540 3dbaef42 2020-11-24 stsp s->label1 = label1;
3541 3dbaef42 2020-11-24 stsp s->label2 = label2;
3543 15a94983 2018-12-23 stsp if (id1) {
3544 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3545 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3546 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3548 5465d566 2020-02-01 tracey s->id1 = NULL;
3550 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3551 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3552 5465d566 2020-02-01 tracey free(s->id1);
3553 5465d566 2020-02-01 tracey s->id1 = NULL;
3554 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3556 5465d566 2020-02-01 tracey s->f = NULL;
3557 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3558 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3559 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3560 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3561 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3562 5465d566 2020-02-01 tracey s->log_view = log_view;
3563 5465d566 2020-02-01 tracey s->repo = repo;
3565 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3566 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3567 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3568 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3569 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3571 6d17833f 2019-11-08 stsp return err;
3572 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3573 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3574 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3575 6d17833f 2019-11-08 stsp if (err) {
3576 5465d566 2020-02-01 tracey free_colors(&s->colors);
3577 6d17833f 2019-11-08 stsp return err;
3579 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3580 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3581 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3582 6d17833f 2019-11-08 stsp if (err) {
3583 5465d566 2020-02-01 tracey free_colors(&s->colors);
3584 6d17833f 2019-11-08 stsp return err;
3587 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3588 ac4dc263 2021-09-24 thomas "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3589 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3590 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3591 6d17833f 2019-11-08 stsp if (err) {
3592 5465d566 2020-02-01 tracey free_colors(&s->colors);
3593 6d17833f 2019-11-08 stsp return err;
3596 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3597 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3598 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3599 11b20872 2019-11-08 stsp if (err) {
3600 5465d566 2020-02-01 tracey free_colors(&s->colors);
3601 11b20872 2019-11-08 stsp return err;
3604 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3605 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3606 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3607 11b20872 2019-11-08 stsp if (err) {
3608 5465d566 2020-02-01 tracey free_colors(&s->colors);
3609 11b20872 2019-11-08 stsp return err;
3613 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3614 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3615 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3617 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3618 fe621944 2020-11-10 stsp s->nlines = 0;
3619 5465d566 2020-02-01 tracey err = create_diff(s);
3620 48ae06ee 2018-10-18 stsp if (err) {
3621 5465d566 2020-02-01 tracey free(s->id1);
3622 5465d566 2020-02-01 tracey s->id1 = NULL;
3623 5465d566 2020-02-01 tracey free(s->id2);
3624 5465d566 2020-02-01 tracey s->id2 = NULL;
3625 78756c87 2020-11-24 stsp free_colors(&s->colors);
3626 48ae06ee 2018-10-18 stsp return err;
3629 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3630 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3631 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3632 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3633 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3635 5dc9f4bc 2018-08-04 stsp return NULL;
3638 e5a0f69f 2018-08-18 stsp static const struct got_error *
3639 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3641 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3642 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3644 5465d566 2020-02-01 tracey free(s->id1);
3645 5465d566 2020-02-01 tracey s->id1 = NULL;
3646 5465d566 2020-02-01 tracey free(s->id2);
3647 5465d566 2020-02-01 tracey s->id2 = NULL;
3648 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3649 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3650 5465d566 2020-02-01 tracey free_colors(&s->colors);
3651 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3652 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3653 fe621944 2020-11-10 stsp s->nlines = 0;
3654 e5a0f69f 2018-08-18 stsp return err;
3657 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3658 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3660 a3404814 2018-09-02 stsp const struct got_error *err;
3661 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3662 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3663 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3665 a3404814 2018-09-02 stsp if (s->id1) {
3666 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3668 a3404814 2018-09-02 stsp return err;
3669 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3671 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3673 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3675 a3404814 2018-09-02 stsp return err;
3676 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3678 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3679 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3680 a3404814 2018-09-02 stsp free(id_str1);
3681 a3404814 2018-09-02 stsp free(id_str2);
3682 a3404814 2018-09-02 stsp return err;
3684 a3404814 2018-09-02 stsp free(id_str1);
3685 a3404814 2018-09-02 stsp free(id_str2);
3687 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3688 267bb3b8 2021-08-01 stsp free(header);
3689 267bb3b8 2021-08-01 stsp return err;
3692 15a087fe 2019-02-21 stsp static const struct got_error *
3693 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3694 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3696 d7a04538 2019-02-21 stsp const struct got_error *err;
3697 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3698 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3699 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3701 15a087fe 2019-02-21 stsp free(s->id2);
3702 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3703 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3704 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3706 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3708 d7a04538 2019-02-21 stsp return err;
3709 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3710 15a087fe 2019-02-21 stsp free(s->id1);
3711 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3712 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3713 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3714 15a087fe 2019-02-21 stsp return NULL;
3717 0cf4efb1 2018-09-29 stsp static const struct got_error *
3718 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3720 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3721 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3722 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3723 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3724 826082fe 2020-12-10 stsp char *line = NULL;
3725 826082fe 2020-12-10 stsp size_t linesize = 0;
3726 826082fe 2020-12-10 stsp ssize_t linelen;
3729 e5a0f69f 2018-08-18 stsp switch (ch) {
3732 3dbaef42 2020-11-24 stsp if (ch == 'a')
3733 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3734 3dbaef42 2020-11-24 stsp if (ch == 'w')
3735 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3736 64453f7e 2020-11-21 stsp wclear(view->window);
3737 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3738 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3739 aa61903a 2021-12-31 thomas s->matched_line = 0;
3740 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3741 64453f7e 2020-11-21 stsp err = create_diff(s);
3744 912a3f79 2021-08-30 j case KEY_HOME:
3745 912a3f79 2021-08-30 j s->first_displayed_line = 1;
3748 912a3f79 2021-08-30 j case KEY_END:
3752 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
3756 1e37a5c2 2019-05-12 jcs case KEY_UP:
3757 f7140bf5 2021-10-17 thomas case CTRL('p'):
3758 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3759 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3761 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3762 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3763 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3766 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3767 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3768 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3771 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3772 f7140bf5 2021-10-17 thomas case CTRL('n'):
3773 1e37a5c2 2019-05-12 jcs if (!s->eof)
3774 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3776 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3777 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3779 00ba99a7 2019-05-12 jcs if (s->eof)
3782 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3783 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3784 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3785 826082fe 2020-12-10 stsp if (linelen == -1) {
3786 826082fe 2020-12-10 stsp if (feof(s->f)) {
3787 826082fe 2020-12-10 stsp s->eof = 1;
3789 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
3793 826082fe 2020-12-10 stsp free(line);
3796 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3797 1e37a5c2 2019-05-12 jcs s->diff_context--;
3798 aa61903a 2021-12-31 thomas s->matched_line = 0;
3799 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3800 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3801 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3802 27829c9e 2020-11-21 stsp s->nlines) {
3803 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3804 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3809 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3810 1e37a5c2 2019-05-12 jcs s->diff_context++;
3811 aa61903a 2021-12-31 thomas s->matched_line = 0;
3812 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3813 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3818 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3820 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3821 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3823 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3827 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3830 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3834 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3835 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3836 aa61903a 2021-12-31 thomas s->matched_line = 0;
3838 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3839 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3843 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3845 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3846 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3848 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3852 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3855 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3859 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3860 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3861 aa61903a 2021-12-31 thomas s->matched_line = 0;
3863 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3864 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3870 bcbd79e2 2018-08-19 stsp return err;
3873 4ed7e80c 2018-05-20 stsp static const struct got_error *
3874 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3876 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3877 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3878 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3879 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3880 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3881 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3882 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3883 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3884 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3885 3dbaef42 2020-11-24 stsp const char *errstr;
3886 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3888 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3889 26ed57b2 2018-05-19 stsp switch (ch) {
3891 64453f7e 2020-11-21 stsp force_text_diff = 1;
3894 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3896 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3897 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
3898 8f666e67 2022-02-12 thomas errstr, errstr);
3900 09b5bff8 2020-02-23 naddy case 'r':
3901 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3902 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3903 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3905 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3908 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3911 17020d27 2019-03-07 stsp usage_diff();
3912 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3916 26ed57b2 2018-05-19 stsp argc -= optind;
3917 26ed57b2 2018-05-19 stsp argv += optind;
3919 26ed57b2 2018-05-19 stsp if (argc == 0) {
3920 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3921 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3922 15a94983 2018-12-23 stsp id_str1 = argv[0];
3923 15a94983 2018-12-23 stsp id_str2 = argv[1];
3925 26ed57b2 2018-05-19 stsp usage_diff();
3927 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3928 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3929 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3930 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3931 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3932 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3933 c156c7a4 2020-12-18 stsp goto done;
3934 a273ac94 2020-02-23 naddy if (worktree)
3935 a273ac94 2020-02-23 naddy repo_path =
3936 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3938 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3939 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3940 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3941 c156c7a4 2020-12-18 stsp goto done;
3945 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3946 eb6600df 2019-01-04 stsp if (error)
3947 eb6600df 2019-01-04 stsp goto done;
3949 a273ac94 2020-02-23 naddy init_curses();
3951 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3952 51a10b52 2020-12-26 stsp if (error)
3953 51a10b52 2020-12-26 stsp goto done;
3955 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3956 26ed57b2 2018-05-19 stsp if (error)
3957 26ed57b2 2018-05-19 stsp goto done;
3959 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3960 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3961 26ed57b2 2018-05-19 stsp if (error)
3962 26ed57b2 2018-05-19 stsp goto done;
3964 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3965 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3966 26ed57b2 2018-05-19 stsp if (error)
3967 26ed57b2 2018-05-19 stsp goto done;
3969 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3970 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3971 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3972 ea5e7bb5 2018-08-01 stsp goto done;
3974 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3975 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
3976 5dc9f4bc 2018-08-04 stsp if (error)
3977 5dc9f4bc 2018-08-04 stsp goto done;
3978 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3980 3dbaef42 2020-11-24 stsp free(label1);
3981 3dbaef42 2020-11-24 stsp free(label2);
3982 c02c541e 2019-03-29 stsp free(repo_path);
3983 a273ac94 2020-02-23 naddy free(cwd);
3984 1d0f4054 2021-06-17 stsp if (repo) {
3985 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3986 1d0f4054 2021-06-17 stsp if (error == NULL)
3987 1d0f4054 2021-06-17 stsp error = close_err;
3989 a273ac94 2020-02-23 naddy if (worktree)
3990 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3991 51a10b52 2020-12-26 stsp tog_free_refs();
3992 26ed57b2 2018-05-19 stsp return error;
3995 4ed7e80c 2018-05-20 stsp __dead static void
3996 9f7d7167 2018-04-29 stsp usage_blame(void)
3999 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4000 9f7d7167 2018-04-29 stsp getprogname());
4004 84451b3e 2018-07-10 stsp struct tog_blame_line {
4005 84451b3e 2018-07-10 stsp int annotated;
4006 84451b3e 2018-07-10 stsp struct got_object_id *id;
4009 4ed7e80c 2018-05-20 stsp static const struct got_error *
4010 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4012 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4013 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4014 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4015 84451b3e 2018-07-10 stsp const struct got_error *err;
4016 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
4017 826082fe 2020-12-10 stsp char *line = NULL;
4018 826082fe 2020-12-10 stsp size_t linesize = 0;
4019 826082fe 2020-12-10 stsp ssize_t linelen;
4020 84451b3e 2018-07-10 stsp wchar_t *wline;
4021 27a741e5 2019-09-11 stsp int width;
4022 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4023 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4024 ab089a2a 2018-07-12 stsp char *id_str;
4025 11b20872 2019-11-08 stsp struct tog_color *tc;
4027 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
4029 ab089a2a 2018-07-12 stsp return err;
4031 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4032 f7d12f7e 2018-08-01 stsp werase(view->window);
4034 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4035 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4036 ab089a2a 2018-07-12 stsp free(id_str);
4037 ab089a2a 2018-07-12 stsp return err;
4040 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4041 ab089a2a 2018-07-12 stsp free(line);
4042 2550e4c3 2018-07-13 stsp line = NULL;
4044 1cae65b4 2019-09-22 stsp return err;
4045 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4046 a3404814 2018-09-02 stsp wstandout(view->window);
4047 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4049 11b20872 2019-11-08 stsp wattr_on(view->window,
4050 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4051 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4053 11b20872 2019-11-08 stsp wattr_off(view->window,
4054 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4055 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4056 a3404814 2018-09-02 stsp wstandend(view->window);
4057 2550e4c3 2018-07-13 stsp free(wline);
4058 2550e4c3 2018-07-13 stsp wline = NULL;
4059 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4060 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4062 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4063 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4064 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4065 ab089a2a 2018-07-12 stsp free(id_str);
4066 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4068 ab089a2a 2018-07-12 stsp free(id_str);
4069 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4070 3f60a8ef 2018-07-10 stsp free(line);
4071 2550e4c3 2018-07-13 stsp line = NULL;
4073 3f60a8ef 2018-07-10 stsp return err;
4074 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4075 2550e4c3 2018-07-13 stsp free(wline);
4076 2550e4c3 2018-07-13 stsp wline = NULL;
4077 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4078 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4080 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4081 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4082 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4083 826082fe 2020-12-10 stsp if (linelen == -1) {
4084 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4085 826082fe 2020-12-10 stsp s->eof = 1;
4088 84451b3e 2018-07-10 stsp free(line);
4089 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4091 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4094 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4095 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4097 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4098 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4099 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4100 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4101 27a741e5 2019-09-11 stsp !(view->focussed &&
4102 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4103 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4104 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4105 8d0fe45a 2019-08-12 stsp char *id_str;
4106 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4107 8d0fe45a 2019-08-12 stsp if (err) {
4108 8d0fe45a 2019-08-12 stsp free(line);
4109 8d0fe45a 2019-08-12 stsp return err;
4111 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4113 11b20872 2019-11-08 stsp wattr_on(view->window,
4114 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4115 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4117 11b20872 2019-11-08 stsp wattr_off(view->window,
4118 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4119 8d0fe45a 2019-08-12 stsp free(id_str);
4120 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4122 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4123 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4126 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4127 ee41ec32 2018-07-10 stsp prev_id = NULL;
4130 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4131 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4132 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4134 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4135 41605754 2020-11-12 stsp width = 9;
4136 41605754 2020-11-12 stsp wline = wcsdup(L"");
4137 41605754 2020-11-12 stsp if (wline == NULL) {
4138 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4139 41605754 2020-11-12 stsp free(line);
4140 41605754 2020-11-12 stsp return err;
4142 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4143 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4144 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4145 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4146 41605754 2020-11-12 stsp view->window, regmatch);
4147 41605754 2020-11-12 stsp if (err) {
4148 41605754 2020-11-12 stsp free(line);
4149 41605754 2020-11-12 stsp return err;
4151 41605754 2020-11-12 stsp width += 9;
4153 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4154 41605754 2020-11-12 stsp view->ncols - 9, 9);
4155 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4156 41605754 2020-11-12 stsp free(wline);
4157 41605754 2020-11-12 stsp wline = NULL;
4158 41605754 2020-11-12 stsp width += 9;
4161 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4162 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4163 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4164 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4166 826082fe 2020-12-10 stsp free(line);
4167 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4169 1a57306a 2018-09-02 stsp view_vborder(view);
4171 84451b3e 2018-07-10 stsp return NULL;
4174 84451b3e 2018-07-10 stsp static const struct got_error *
4175 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
4176 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
4178 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4179 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4180 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4181 1a76625f 2018-10-22 stsp int errcode;
4183 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4184 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4185 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4187 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4188 1a76625f 2018-10-22 stsp if (errcode)
4189 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4191 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4192 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4193 d68a0a7d 2018-07-10 stsp goto done;
4196 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4197 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4199 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4200 d68a0a7d 2018-07-10 stsp if (line->annotated)
4201 d68a0a7d 2018-07-10 stsp goto done;
4203 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4204 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4205 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4206 84451b3e 2018-07-10 stsp goto done;
4208 84451b3e 2018-07-10 stsp line->annotated = 1;
4210 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4211 1a76625f 2018-10-22 stsp if (errcode)
4212 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4213 84451b3e 2018-07-10 stsp return err;
4216 84451b3e 2018-07-10 stsp static void *
4217 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4219 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4220 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4221 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4222 1a76625f 2018-10-22 stsp int errcode;
4224 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4226 61266923 2020-01-14 stsp return (void *)err;
4228 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4229 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4230 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4231 fc06ba56 2019-08-22 stsp err = NULL;
4233 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4234 1a76625f 2018-10-22 stsp if (errcode)
4235 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4236 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4238 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4239 1d0f4054 2021-06-17 stsp if (err == NULL)
4240 1d0f4054 2021-06-17 stsp err = close_err;
4241 c9beca56 2018-07-22 stsp ta->repo = NULL;
4242 c9beca56 2018-07-22 stsp *ta->complete = 1;
4244 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4245 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4246 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4248 18430de3 2018-07-10 stsp return (void *)err;
4251 245d91c1 2018-07-12 stsp static struct got_object_id *
4252 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4253 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4255 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4257 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4258 8d0fe45a 2019-08-12 stsp return NULL;
4260 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4261 245d91c1 2018-07-12 stsp if (!line->annotated)
4262 245d91c1 2018-07-12 stsp return NULL;
4264 245d91c1 2018-07-12 stsp return line->id;
4267 b880a918 2018-07-10 stsp static const struct got_error *
4268 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4270 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4273 245d91c1 2018-07-12 stsp if (blame->thread) {
4274 1a76625f 2018-10-22 stsp int errcode;
4275 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4276 1a76625f 2018-10-22 stsp if (errcode)
4277 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4278 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4279 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4280 1a76625f 2018-10-22 stsp if (errcode)
4281 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4282 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4283 1a76625f 2018-10-22 stsp if (errcode)
4284 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4285 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4286 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4287 245d91c1 2018-07-12 stsp err = NULL;
4288 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
4290 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4291 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4292 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4293 1d0f4054 2021-06-17 stsp if (err == NULL)
4294 1d0f4054 2021-06-17 stsp err = close_err;
4295 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4297 245d91c1 2018-07-12 stsp if (blame->f) {
4298 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4299 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4300 245d91c1 2018-07-12 stsp blame->f = NULL;
4302 57670559 2018-12-23 stsp if (blame->lines) {
4303 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4304 57670559 2018-12-23 stsp free(blame->lines[i].id);
4305 57670559 2018-12-23 stsp free(blame->lines);
4306 57670559 2018-12-23 stsp blame->lines = NULL;
4308 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4309 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4311 245d91c1 2018-07-12 stsp return err;
4314 245d91c1 2018-07-12 stsp static const struct got_error *
4315 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4317 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4318 fc06ba56 2019-08-22 stsp int *done = arg;
4319 fc06ba56 2019-08-22 stsp int errcode;
4321 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4322 fc06ba56 2019-08-22 stsp if (errcode)
4323 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4324 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4326 fc06ba56 2019-08-22 stsp if (*done)
4327 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4329 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4330 fc06ba56 2019-08-22 stsp if (errcode)
4331 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4332 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4334 fc06ba56 2019-08-22 stsp return err;
4337 fc06ba56 2019-08-22 stsp static const struct got_error *
4338 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4340 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4341 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4342 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4343 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
4344 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4345 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4346 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4347 15a94983 2018-12-23 stsp int obj_type;
4349 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
4350 ec242592 2022-04-22 thomas &s->blamed_commit->id);
4352 15a94983 2018-12-23 stsp return err;
4354 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4355 945f9229 2022-04-16 thomas if (err)
4356 945f9229 2022-04-16 thomas goto done;
4358 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4360 84451b3e 2018-07-10 stsp goto done;
4362 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4363 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4364 84451b3e 2018-07-10 stsp goto done;
4367 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4369 a70480e0 2018-06-23 stsp goto done;
4370 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4371 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4372 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4373 84451b3e 2018-07-10 stsp goto done;
4375 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4376 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4378 1fddf795 2021-01-20 stsp goto done;
4379 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4380 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4381 84451b3e 2018-07-10 stsp goto done;
4384 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4385 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4386 b02560ec 2019-08-19 stsp blame->nlines--;
4388 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4389 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4390 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4391 84451b3e 2018-07-10 stsp goto done;
4394 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4396 bd24772e 2018-07-11 stsp goto done;
4398 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4399 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4400 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4401 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4402 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4403 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4404 245d91c1 2018-07-12 stsp goto done;
4406 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4408 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4409 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4410 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4411 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4412 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4413 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4414 a5388363 2020-12-01 naddy s->blame_complete = 0;
4416 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4417 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4418 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4419 f5a09613 2020-12-13 naddy s->selected_line = 1;
4421 aa61903a 2021-12-31 thomas s->matched_line = 0;
4424 945f9229 2022-04-16 thomas if (commit)
4425 945f9229 2022-04-16 thomas got_object_commit_close(commit);
4427 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4428 27d434c2 2018-09-15 stsp free(obj_id);
4430 245d91c1 2018-07-12 stsp stop_blame(blame);
4431 245d91c1 2018-07-12 stsp return err;
4434 245d91c1 2018-07-12 stsp static const struct got_error *
4435 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4436 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4438 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4439 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4441 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4443 c4843652 2019-08-12 stsp s->path = strdup(path);
4444 c4843652 2019-08-12 stsp if (s->path == NULL)
4445 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4447 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4448 c4843652 2019-08-12 stsp if (err) {
4449 c4843652 2019-08-12 stsp free(s->path);
4450 7cbe629d 2018-08-04 stsp return err;
4453 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4454 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4455 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4456 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4457 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4458 fb2756b9 2018-08-04 stsp s->repo = repo;
4459 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4460 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4462 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4463 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4464 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4465 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4467 11b20872 2019-11-08 stsp return err;
4470 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4471 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4472 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4473 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4474 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4476 a5388363 2020-12-01 naddy return run_blame(view);
4479 e5a0f69f 2018-08-18 stsp static const struct got_error *
4480 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4482 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4483 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4485 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4486 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4488 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4489 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4490 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4491 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4492 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4495 e5a0f69f 2018-08-18 stsp free(s->path);
4496 11b20872 2019-11-08 stsp free_colors(&s->colors);
4498 e5a0f69f 2018-08-18 stsp return err;
4501 7cbe629d 2018-08-04 stsp static const struct got_error *
4502 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4504 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4506 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4507 6c4c42e0 2019-06-24 stsp return NULL;
4510 6c4c42e0 2019-06-24 stsp static const struct got_error *
4511 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4513 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4514 6c4c42e0 2019-06-24 stsp int lineno;
4515 826082fe 2020-12-10 stsp char *line = NULL;
4516 826082fe 2020-12-10 stsp size_t linesize = 0;
4517 826082fe 2020-12-10 stsp ssize_t linelen;
4519 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4520 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4521 6c4c42e0 2019-06-24 stsp return NULL;
4524 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4525 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4526 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4528 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4530 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
4532 6c4c42e0 2019-06-24 stsp while (1) {
4533 6c4c42e0 2019-06-24 stsp off_t offset;
4535 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4536 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4537 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4541 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4542 6c4c42e0 2019-06-24 stsp lineno = 1;
4544 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4547 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4548 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4549 6c4c42e0 2019-06-24 stsp free(line);
4550 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4552 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4553 826082fe 2020-12-10 stsp if (linelen != -1 &&
4554 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4555 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4556 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4559 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4564 826082fe 2020-12-10 stsp free(line);
4566 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4567 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4568 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4571 6c4c42e0 2019-06-24 stsp return NULL;
4574 6c4c42e0 2019-06-24 stsp static const struct got_error *
4575 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4577 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4578 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4579 2b380cc8 2018-10-24 stsp int errcode;
4581 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
4582 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4583 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4584 2b380cc8 2018-10-24 stsp if (errcode)
4585 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4587 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4590 51fe7530 2019-08-19 stsp if (s->blame_complete)
4591 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4593 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4595 669b5ffa 2018-10-07 stsp view_vborder(view);
4596 e5a0f69f 2018-08-18 stsp return err;
4599 e5a0f69f 2018-08-18 stsp static const struct got_error *
4600 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4602 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4603 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4604 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4605 669b5ffa 2018-10-07 stsp int begin_x = 0;
4607 e5a0f69f 2018-08-18 stsp switch (ch) {
4609 1e37a5c2 2019-05-12 jcs s->done = 1;
4611 4deef56f 2021-09-02 naddy case 'g':
4612 4deef56f 2021-09-02 naddy case KEY_HOME:
4613 4deef56f 2021-09-02 naddy s->selected_line = 1;
4614 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4616 4deef56f 2021-09-02 naddy case 'G':
4617 4deef56f 2021-09-02 naddy case KEY_END:
4618 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4619 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4620 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4622 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4623 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4624 4deef56f 2021-09-02 naddy (view->nlines - 3);
4628 1e37a5c2 2019-05-12 jcs case KEY_UP:
4629 f7140bf5 2021-10-17 thomas case CTRL('p'):
4630 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4631 1e37a5c2 2019-05-12 jcs s->selected_line--;
4632 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4633 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4634 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4636 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4637 ea025d1d 2020-02-22 naddy case CTRL('b'):
4638 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4639 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4642 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4643 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4644 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4646 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4649 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4650 f7140bf5 2021-10-17 thomas case CTRL('n'):
4651 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4652 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4653 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4654 1e37a5c2 2019-05-12 jcs s->selected_line++;
4655 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4656 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4657 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4660 1e37a5c2 2019-05-12 jcs case 'p': {
4661 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4662 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4663 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4664 1e37a5c2 2019-05-12 jcs if (id == NULL)
4666 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4667 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
4668 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4669 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4670 1e37a5c2 2019-05-12 jcs int obj_type;
4671 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4672 1e37a5c2 2019-05-12 jcs s->repo, id);
4675 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4676 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4677 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4678 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4681 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4682 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
4683 ec242592 2022-04-22 thomas s->repo, &pid->id);
4684 945f9229 2022-04-16 thomas if (err)
4686 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4687 945f9229 2022-04-16 thomas pcommit, s->path);
4688 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
4689 e5a0f69f 2018-08-18 stsp if (err) {
4690 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4691 1e37a5c2 2019-05-12 jcs err = NULL;
4692 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4695 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4697 1e37a5c2 2019-05-12 jcs free(blob_id);
4698 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4699 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4700 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4703 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4704 ec242592 2022-04-22 thomas &pid->id);
4705 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4707 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4708 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
4710 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4715 1e37a5c2 2019-05-12 jcs s->done = 1;
4716 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4717 1e37a5c2 2019-05-12 jcs s->done = 0;
4718 1e37a5c2 2019-05-12 jcs if (thread_err)
4720 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
4721 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4722 a5388363 2020-12-01 naddy err = run_blame(view);
4727 1e37a5c2 2019-05-12 jcs case 'B': {
4728 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4729 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
4730 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
4732 1e37a5c2 2019-05-12 jcs s->done = 1;
4733 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4734 1e37a5c2 2019-05-12 jcs s->done = 0;
4735 1e37a5c2 2019-05-12 jcs if (thread_err)
4737 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4738 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4739 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4740 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
4741 a5388363 2020-12-01 naddy err = run_blame(view);
4746 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4747 1e37a5c2 2019-05-12 jcs case '\r': {
4748 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4749 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4750 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4751 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4752 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4753 1e37a5c2 2019-05-12 jcs if (id == NULL)
4755 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4758 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4759 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4760 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4761 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4762 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4763 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4764 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4765 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4768 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4769 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4770 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4772 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4775 e78dc838 2020-12-04 stsp view->focussed = 0;
4776 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4777 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4778 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4781 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4782 e78dc838 2020-12-04 stsp view->focus_child = 1;
4784 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4789 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4790 ea025d1d 2020-02-22 naddy case CTRL('f'):
4792 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4793 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4794 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4797 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4798 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4799 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4800 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4803 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4804 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4805 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4806 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4808 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4809 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4810 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4812 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4813 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4814 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4815 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4821 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4824 a70480e0 2018-06-23 stsp static const struct got_error *
4825 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4827 a70480e0 2018-06-23 stsp const struct got_error *error;
4828 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4829 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4830 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4831 0587e10c 2020-07-23 stsp char *link_target = NULL;
4832 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4833 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
4834 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4836 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4838 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4839 a70480e0 2018-06-23 stsp switch (ch) {
4841 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4844 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4845 69069811 2018-08-02 stsp if (repo_path == NULL)
4846 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4850 17020d27 2019-03-07 stsp usage_blame();
4851 a70480e0 2018-06-23 stsp /* NOTREACHED */
4855 a70480e0 2018-06-23 stsp argc -= optind;
4856 a70480e0 2018-06-23 stsp argv += optind;
4858 f135c941 2020-02-20 stsp if (argc != 1)
4859 a70480e0 2018-06-23 stsp usage_blame();
4861 69069811 2018-08-02 stsp if (repo_path == NULL) {
4862 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4863 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4864 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4865 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4866 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4867 c156c7a4 2020-12-18 stsp goto done;
4868 f135c941 2020-02-20 stsp if (worktree)
4869 eb41ed75 2019-02-05 stsp repo_path =
4870 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4872 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4873 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4874 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4875 c156c7a4 2020-12-18 stsp goto done;
4879 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4880 c02c541e 2019-03-29 stsp if (error != NULL)
4881 8e94dd5b 2019-01-04 stsp goto done;
4883 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4884 f135c941 2020-02-20 stsp worktree);
4885 c02c541e 2019-03-29 stsp if (error)
4886 92205607 2019-01-04 stsp goto done;
4888 f135c941 2020-02-20 stsp init_curses();
4890 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4891 51a10b52 2020-12-26 stsp if (error)
4892 51a10b52 2020-12-26 stsp goto done;
4894 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4895 eb41ed75 2019-02-05 stsp if (error)
4896 69069811 2018-08-02 stsp goto done;
4898 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4899 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4900 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4901 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4902 a70480e0 2018-06-23 stsp if (error != NULL)
4903 66b4983c 2018-06-23 stsp goto done;
4904 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4905 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4907 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4908 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4910 a19e88aa 2018-06-23 stsp if (error != NULL)
4911 8b473291 2019-02-21 stsp goto done;
4913 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4914 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4915 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4916 e1cd8fed 2018-08-01 stsp goto done;
4919 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
4920 945f9229 2022-04-16 thomas if (error)
4921 945f9229 2022-04-16 thomas goto done;
4923 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4924 945f9229 2022-04-16 thomas commit, repo);
4925 7cbe629d 2018-08-04 stsp if (error)
4926 7cbe629d 2018-08-04 stsp goto done;
4928 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4929 78756c87 2020-11-24 stsp commit_id, repo);
4930 0587e10c 2020-07-23 stsp if (error)
4931 0587e10c 2020-07-23 stsp goto done;
4932 12314ad4 2019-08-31 stsp if (worktree) {
4933 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4934 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4935 12314ad4 2019-08-31 stsp worktree = NULL;
4937 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4939 69069811 2018-08-02 stsp free(repo_path);
4940 f135c941 2020-02-20 stsp free(in_repo_path);
4941 0587e10c 2020-07-23 stsp free(link_target);
4942 69069811 2018-08-02 stsp free(cwd);
4943 a70480e0 2018-06-23 stsp free(commit_id);
4944 945f9229 2022-04-16 thomas if (commit)
4945 945f9229 2022-04-16 thomas got_object_commit_close(commit);
4946 eb41ed75 2019-02-05 stsp if (worktree)
4947 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4948 1d0f4054 2021-06-17 stsp if (repo) {
4949 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4950 1d0f4054 2021-06-17 stsp if (error == NULL)
4951 1d0f4054 2021-06-17 stsp error = close_err;
4953 51a10b52 2020-12-26 stsp tog_free_refs();
4954 a70480e0 2018-06-23 stsp return error;
4957 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4958 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
4960 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4961 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4962 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4963 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4964 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4965 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4966 d86d3b18 2020-12-01 naddy int limit = view->nlines;
4968 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
4970 f7d12f7e 2018-08-01 stsp werase(view->window);
4972 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4973 ffd1d5e5 2018-06-23 stsp return NULL;
4975 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4977 ffd1d5e5 2018-06-23 stsp return err;
4978 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4979 a3404814 2018-09-02 stsp wstandout(view->window);
4980 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4982 11b20872 2019-11-08 stsp wattr_on(view->window,
4983 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4984 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4986 11b20872 2019-11-08 stsp wattr_off(view->window,
4987 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4988 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4989 a3404814 2018-09-02 stsp wstandend(view->window);
4990 2550e4c3 2018-07-13 stsp free(wline);
4991 2550e4c3 2018-07-13 stsp wline = NULL;
4992 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4993 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4994 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4995 ffd1d5e5 2018-06-23 stsp return NULL;
4996 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4998 ce52c690 2018-06-23 stsp return err;
4999 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5000 2550e4c3 2018-07-13 stsp free(wline);
5001 2550e4c3 2018-07-13 stsp wline = NULL;
5002 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5003 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5004 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5005 ffd1d5e5 2018-06-23 stsp return NULL;
5006 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5007 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5008 a1eca9bb 2018-06-23 stsp return NULL;
5010 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5011 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5012 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5013 0cf4efb1 2018-09-29 stsp if (view->focussed)
5014 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5015 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5017 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5018 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5019 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5020 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5021 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5022 ffd1d5e5 2018-06-23 stsp return NULL;
5026 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5029 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5030 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5031 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5032 848d6979 2019-08-12 stsp const char *modestr = "";
5033 56e0773d 2019-11-28 stsp mode_t mode;
5035 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5036 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5038 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5039 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5040 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5042 638f9024 2019-05-13 stsp return got_error_from_errno(
5043 230a42bd 2019-05-11 jcs "got_object_id_str");
5045 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5046 63c5ca5d 2019-08-24 stsp modestr = "$";
5047 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5050 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5051 d86d3b18 2020-12-01 naddy te, s->repo);
5052 0d6c6ee3 2020-05-20 stsp if (err) {
5053 0d6c6ee3 2020-05-20 stsp free(id_str);
5054 0d6c6ee3 2020-05-20 stsp return err;
5056 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5057 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5058 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5060 848d6979 2019-08-12 stsp modestr = "@";
5062 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5063 848d6979 2019-08-12 stsp modestr = "/";
5064 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5065 848d6979 2019-08-12 stsp modestr = "*";
5066 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5067 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5068 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5069 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5070 1d13200f 2018-07-12 stsp free(id_str);
5071 0d6c6ee3 2020-05-20 stsp free(link_target);
5072 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5074 1d13200f 2018-07-12 stsp free(id_str);
5075 0d6c6ee3 2020-05-20 stsp free(link_target);
5076 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5077 ffd1d5e5 2018-06-23 stsp if (err) {
5078 ffd1d5e5 2018-06-23 stsp free(line);
5081 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5082 0cf4efb1 2018-09-29 stsp if (view->focussed)
5083 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5084 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5086 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5088 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5089 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5090 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5092 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5093 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5094 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5095 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5096 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5097 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5098 ffd1d5e5 2018-06-23 stsp free(line);
5099 2550e4c3 2018-07-13 stsp free(wline);
5100 2550e4c3 2018-07-13 stsp wline = NULL;
5102 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5103 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5104 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5108 ffd1d5e5 2018-06-23 stsp return err;
5111 ffd1d5e5 2018-06-23 stsp static void
5112 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5114 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5115 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5116 fa86c4bf 2020-11-29 stsp int i = 0;
5118 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5121 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5122 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5123 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5124 fa86c4bf 2020-11-29 stsp if (!isroot)
5125 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5128 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5129 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5133 fa86c4bf 2020-11-29 stsp static void
5134 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5136 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5137 ffd1d5e5 2018-06-23 stsp int n = 0;
5139 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5140 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5141 694d3271 2020-12-01 naddy s->first_displayed_entry);
5143 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5145 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5146 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5147 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5148 768394f3 2019-01-24 stsp if (last) {
5149 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5150 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5155 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5156 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5157 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5159 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5160 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5161 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5163 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5164 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5165 56e0773d 2019-11-28 stsp + 1 /* slash */;
5167 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5169 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5170 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5171 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5173 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5174 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5175 d9765a41 2018-06-23 stsp while (pt) {
5176 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5177 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5178 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5179 cb2ebc8a 2018-06-23 stsp goto done;
5181 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5182 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5183 cb2ebc8a 2018-06-23 stsp goto done;
5185 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5188 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5189 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5190 ce52c690 2018-06-23 stsp goto done;
5194 ce52c690 2018-06-23 stsp if (err) {
5195 ce52c690 2018-06-23 stsp free(*path);
5196 ce52c690 2018-06-23 stsp *path = NULL;
5198 ce52c690 2018-06-23 stsp return err;
5201 ce52c690 2018-06-23 stsp static const struct got_error *
5202 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5203 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5204 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5206 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5207 ce52c690 2018-06-23 stsp char *path;
5208 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5210 a0de39f3 2019-08-09 stsp *new_view = NULL;
5212 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5214 ce52c690 2018-06-23 stsp return err;
5216 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5217 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5218 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5219 83ce39e3 2019-08-12 stsp goto done;
5222 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5223 e5a0f69f 2018-08-18 stsp if (err) {
5224 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5225 fc06ba56 2019-08-22 stsp err = NULL;
5226 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5228 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5230 83ce39e3 2019-08-12 stsp free(path);
5231 69efd4c4 2018-07-18 stsp return err;
5234 69efd4c4 2018-07-18 stsp static const struct got_error *
5235 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5236 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5238 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5239 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5240 69efd4c4 2018-07-18 stsp char *path;
5242 a0de39f3 2019-08-09 stsp *new_view = NULL;
5244 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5245 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5246 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5248 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5250 69efd4c4 2018-07-18 stsp return err;
5252 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5255 e5a0f69f 2018-08-18 stsp view_close(log_view);
5257 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5258 cb2ebc8a 2018-06-23 stsp free(path);
5259 cb2ebc8a 2018-06-23 stsp return err;
5262 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5263 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5264 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5266 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5267 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5268 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5269 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5271 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5272 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5274 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5275 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5276 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5278 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5280 bc573f3b 2021-07-10 stsp goto done;
5283 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5284 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5285 bc573f3b 2021-07-10 stsp * closed on demand.
5287 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5288 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5290 bc573f3b 2021-07-10 stsp goto done;
5291 bc573f3b 2021-07-10 stsp s->tree = s->root;
5293 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5294 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5295 ffd1d5e5 2018-06-23 stsp goto done;
5297 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5298 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5299 ffd1d5e5 2018-06-23 stsp goto done;
5302 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5303 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5304 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5305 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5306 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5307 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5308 9cd7cbd1 2020-12-07 stsp goto done;
5311 fb2756b9 2018-08-04 stsp s->repo = repo;
5313 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5314 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5315 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5316 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5318 c0b01bdb 2019-11-08 stsp goto done;
5319 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5320 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5322 c0b01bdb 2019-11-08 stsp goto done;
5323 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5324 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5325 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5327 c0b01bdb 2019-11-08 stsp goto done;
5329 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5330 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5331 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5333 11b20872 2019-11-08 stsp goto done;
5335 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5336 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5338 c0b01bdb 2019-11-08 stsp goto done;
5341 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5342 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5343 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5344 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5345 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5347 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5348 bc573f3b 2021-07-10 stsp if (commit)
5349 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5351 bc573f3b 2021-07-10 stsp close_tree_view(view);
5352 ad80ab7b 2018-08-04 stsp return err;
5355 e5a0f69f 2018-08-18 stsp static const struct got_error *
5356 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5358 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5360 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5361 fb2756b9 2018-08-04 stsp free(s->tree_label);
5362 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5363 6484ec90 2018-09-29 stsp free(s->commit_id);
5364 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5365 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5366 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5367 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5368 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5369 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5370 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5371 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5372 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5373 ad80ab7b 2018-08-04 stsp free(parent);
5376 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5377 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5378 bc573f3b 2021-07-10 stsp if (s->root)
5379 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5380 7c32bd05 2019-06-22 stsp return NULL;
5383 7c32bd05 2019-06-22 stsp static const struct got_error *
5384 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5386 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5388 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5389 7c32bd05 2019-06-22 stsp return NULL;
5392 7c32bd05 2019-06-22 stsp static int
5393 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5395 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5397 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5401 7c32bd05 2019-06-22 stsp static const struct got_error *
5402 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5404 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5405 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5407 7c32bd05 2019-06-22 stsp if (!view->searching) {
5408 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5409 7c32bd05 2019-06-22 stsp return NULL;
5412 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5413 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5414 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5415 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5416 56e0773d 2019-11-28 stsp s->selected_entry);
5418 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5420 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5421 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5423 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5424 56e0773d 2019-11-28 stsp s->selected_entry);
5427 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
5428 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
5429 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
5430 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5432 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5435 7c32bd05 2019-06-22 stsp while (1) {
5436 56e0773d 2019-11-28 stsp if (te == NULL) {
5437 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5438 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5439 ac66afb8 2019-06-24 stsp return NULL;
5441 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5442 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5444 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5447 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5448 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5449 56e0773d 2019-11-28 stsp s->matched_entry = te;
5453 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5454 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5456 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5459 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5460 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5461 7c32bd05 2019-06-22 stsp s->selected = 0;
5464 e5a0f69f 2018-08-18 stsp return NULL;
5467 ad80ab7b 2018-08-04 stsp static const struct got_error *
5468 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5470 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5471 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5472 e5a0f69f 2018-08-18 stsp char *parent_path;
5474 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5476 e5a0f69f 2018-08-18 stsp return err;
5478 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5479 e5a0f69f 2018-08-18 stsp free(parent_path);
5481 669b5ffa 2018-10-07 stsp view_vborder(view);
5482 e5a0f69f 2018-08-18 stsp return err;
5485 e5a0f69f 2018-08-18 stsp static const struct got_error *
5486 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5488 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5489 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5490 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5491 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5492 e4526bf5 2021-09-03 naddy int begin_x = 0, n;
5494 e5a0f69f 2018-08-18 stsp switch (ch) {
5496 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5499 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5501 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5502 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5503 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5504 e78dc838 2020-12-04 stsp view->focussed = 0;
5505 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5506 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5507 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5509 1e37a5c2 2019-05-12 jcs return err;
5510 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5511 e78dc838 2020-12-04 stsp view->focus_child = 1;
5513 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5516 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5517 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5518 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5519 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5520 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5521 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5522 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5523 152c1c93 2020-11-29 stsp if (err) {
5524 152c1c93 2020-11-29 stsp view_close(ref_view);
5525 152c1c93 2020-11-29 stsp return err;
5527 e78dc838 2020-12-04 stsp view->focussed = 0;
5528 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5529 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5530 152c1c93 2020-11-29 stsp err = view_close_child(view);
5532 152c1c93 2020-11-29 stsp return err;
5533 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5534 e78dc838 2020-12-04 stsp view->focus_child = 1;
5536 152c1c93 2020-11-29 stsp *new_view = ref_view;
5538 e4526bf5 2021-09-03 naddy case 'g':
5539 e4526bf5 2021-09-03 naddy case KEY_HOME:
5540 e4526bf5 2021-09-03 naddy s->selected = 0;
5541 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5542 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5543 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5545 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5547 e4526bf5 2021-09-03 naddy case 'G':
5548 e4526bf5 2021-09-03 naddy case KEY_END:
5549 e4526bf5 2021-09-03 naddy s->selected = 0;
5550 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5551 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5552 e4526bf5 2021-09-03 naddy if (te == NULL) {
5553 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5554 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5559 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5560 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5562 e4526bf5 2021-09-03 naddy if (n > 0)
5563 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5566 1e37a5c2 2019-05-12 jcs case KEY_UP:
5567 f7140bf5 2021-10-17 thomas case CTRL('p'):
5568 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5569 1e37a5c2 2019-05-12 jcs s->selected--;
5572 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5574 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5575 ea025d1d 2020-02-22 naddy case CTRL('b'):
5576 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5577 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5578 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5579 fa86c4bf 2020-11-29 stsp s->selected = 0;
5581 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5582 fa86c4bf 2020-11-29 stsp s->selected = 0;
5584 3e135950 2020-12-01 naddy tree_scroll_up(s, MAX(0, view->nlines - 3));
5587 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5588 f7140bf5 2021-10-17 thomas case CTRL('n'):
5589 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5590 1e37a5c2 2019-05-12 jcs s->selected++;
5593 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5595 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5597 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5599 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5600 ea025d1d 2020-02-22 naddy case CTRL('f'):
5601 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5603 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5604 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5605 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5608 3e135950 2020-12-01 naddy tree_scroll_down(s, view->nlines - 3);
5610 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5612 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5613 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5614 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5615 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5616 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5618 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5619 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5621 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5622 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5623 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5624 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5625 1e37a5c2 2019-05-12 jcs s->selected_entry =
5626 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5627 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5628 1e37a5c2 2019-05-12 jcs free(parent);
5629 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5630 56e0773d 2019-11-28 stsp s->selected_entry))) {
5631 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5632 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5633 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5636 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5637 941e9f74 2019-05-21 stsp if (err) {
5638 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5641 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5642 56e0773d 2019-11-28 stsp s->selected_entry))) {
5643 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5644 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5645 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5647 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5648 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5649 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5652 e78dc838 2020-12-04 stsp view->focussed = 0;
5653 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5654 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5655 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5657 669b5ffa 2018-10-07 stsp return err;
5658 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5659 e78dc838 2020-12-04 stsp view->focus_child = 1;
5661 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5664 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5665 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5666 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5672 ffd1d5e5 2018-06-23 stsp return err;
5675 ffd1d5e5 2018-06-23 stsp __dead static void
5676 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5679 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5680 ffd1d5e5 2018-06-23 stsp getprogname());
5684 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5685 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5687 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5688 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5689 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5690 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5691 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5692 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5693 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5694 4e97c21c 2020-12-06 stsp char *label = NULL;
5695 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5696 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5698 5221c383 2018-08-01 stsp struct tog_view *view;
5700 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5701 ffd1d5e5 2018-06-23 stsp switch (ch) {
5703 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5706 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5707 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5708 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5712 e99e2d15 2020-11-24 naddy usage_tree();
5713 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5717 ffd1d5e5 2018-06-23 stsp argc -= optind;
5718 ffd1d5e5 2018-06-23 stsp argv += optind;
5720 55cccc34 2020-02-20 stsp if (argc > 1)
5721 e99e2d15 2020-11-24 naddy usage_tree();
5723 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5724 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5725 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5726 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5727 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5728 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5729 c156c7a4 2020-12-18 stsp goto done;
5730 55cccc34 2020-02-20 stsp if (worktree)
5731 52185f70 2019-02-05 stsp repo_path =
5732 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5734 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5735 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5736 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5737 c156c7a4 2020-12-18 stsp goto done;
5741 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5742 c02c541e 2019-03-29 stsp if (error != NULL)
5743 52185f70 2019-02-05 stsp goto done;
5745 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5746 55cccc34 2020-02-20 stsp repo, worktree);
5747 55cccc34 2020-02-20 stsp if (error)
5748 55cccc34 2020-02-20 stsp goto done;
5750 55cccc34 2020-02-20 stsp init_curses();
5752 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5753 c02c541e 2019-03-29 stsp if (error)
5754 52185f70 2019-02-05 stsp goto done;
5756 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5757 51a10b52 2020-12-26 stsp if (error)
5758 51a10b52 2020-12-26 stsp goto done;
5760 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5761 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5762 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5763 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5764 4e97c21c 2020-12-06 stsp if (error)
5765 4e97c21c 2020-12-06 stsp goto done;
5766 4e97c21c 2020-12-06 stsp head_ref_name = label;
5768 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5769 4e97c21c 2020-12-06 stsp if (error == NULL)
5770 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5771 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5772 4e97c21c 2020-12-06 stsp goto done;
5773 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5774 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5775 4e97c21c 2020-12-06 stsp if (error)
5776 4e97c21c 2020-12-06 stsp goto done;
5779 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
5780 945f9229 2022-04-16 thomas if (error)
5781 945f9229 2022-04-16 thomas goto done;
5783 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5784 5221c383 2018-08-01 stsp if (view == NULL) {
5785 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5786 5221c383 2018-08-01 stsp goto done;
5788 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
5789 ad80ab7b 2018-08-04 stsp if (error)
5790 ad80ab7b 2018-08-04 stsp goto done;
5791 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5792 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
5793 d91faf3b 2020-12-01 naddy in_repo_path);
5794 55cccc34 2020-02-20 stsp if (error)
5795 55cccc34 2020-02-20 stsp goto done;
5798 55cccc34 2020-02-20 stsp if (worktree) {
5799 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5800 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5801 55cccc34 2020-02-20 stsp worktree = NULL;
5803 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5805 52185f70 2019-02-05 stsp free(repo_path);
5806 e4a0e26d 2020-02-20 stsp free(cwd);
5807 ffd1d5e5 2018-06-23 stsp free(commit_id);
5808 4e97c21c 2020-12-06 stsp free(label);
5810 486cd271 2020-12-06 stsp got_ref_close(ref);
5811 1d0f4054 2021-06-17 stsp if (repo) {
5812 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5813 1d0f4054 2021-06-17 stsp if (error == NULL)
5814 1d0f4054 2021-06-17 stsp error = close_err;
5816 51a10b52 2020-12-26 stsp tog_free_refs();
5817 ffd1d5e5 2018-06-23 stsp return error;
5820 6458efa5 2020-11-24 stsp static const struct got_error *
5821 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5823 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5824 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5826 6458efa5 2020-11-24 stsp s->nrefs = 0;
5827 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
5828 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
5829 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
5830 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
5831 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
5834 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5835 6458efa5 2020-11-24 stsp if (re == NULL)
5836 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5838 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
5839 8924d611 2020-12-26 stsp if (re->ref == NULL)
5840 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
5841 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5842 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5845 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5846 6458efa5 2020-11-24 stsp return NULL;
5850 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5852 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5854 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5855 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5856 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5857 8924d611 2020-12-26 stsp got_ref_close(re->ref);
5862 6458efa5 2020-11-24 stsp static const struct got_error *
5863 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5865 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5866 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5868 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5869 6458efa5 2020-11-24 stsp s->repo = repo;
5871 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5872 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5874 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5876 6458efa5 2020-11-24 stsp return err;
5878 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5879 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5880 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5881 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5883 6458efa5 2020-11-24 stsp goto done;
5885 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5886 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5887 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5889 6458efa5 2020-11-24 stsp goto done;
5891 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5892 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5893 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5894 2183bbf6 2022-01-23 thomas if (err)
5895 2183bbf6 2022-01-23 thomas goto done;
5897 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
5898 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
5899 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
5901 6458efa5 2020-11-24 stsp goto done;
5904 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5905 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5906 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5907 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5908 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5911 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5912 6458efa5 2020-11-24 stsp return err;
5915 6458efa5 2020-11-24 stsp static const struct got_error *
5916 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5918 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5920 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5921 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5923 6458efa5 2020-11-24 stsp return NULL;
5926 6458efa5 2020-11-24 stsp static const struct got_error *
5927 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5928 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5930 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5931 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5932 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5933 6458efa5 2020-11-24 stsp int obj_type;
5935 c42c9805 2020-11-24 stsp *commit_id = NULL;
5937 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5939 6458efa5 2020-11-24 stsp return err;
5941 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5943 6458efa5 2020-11-24 stsp goto done;
5945 6458efa5 2020-11-24 stsp switch (obj_type) {
5946 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
5947 c42c9805 2020-11-24 stsp *commit_id = obj_id;
5949 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
5950 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
5952 6458efa5 2020-11-24 stsp goto done;
5953 c42c9805 2020-11-24 stsp free(obj_id);
5954 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
5955 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5957 6458efa5 2020-11-24 stsp goto done;
5958 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5959 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5960 c42c9805 2020-11-24 stsp goto done;
5962 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
5963 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5964 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
5965 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
5966 c42c9805 2020-11-24 stsp goto done;
5970 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5976 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
5977 c42c9805 2020-11-24 stsp if (err) {
5978 c42c9805 2020-11-24 stsp free(*commit_id);
5979 c42c9805 2020-11-24 stsp *commit_id = NULL;
5981 c42c9805 2020-11-24 stsp return err;
5984 c42c9805 2020-11-24 stsp static const struct got_error *
5985 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
5986 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5988 c42c9805 2020-11-24 stsp struct tog_view *log_view;
5989 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
5990 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
5992 c42c9805 2020-11-24 stsp *new_view = NULL;
5994 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
5995 c42c9805 2020-11-24 stsp if (err) {
5996 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
5997 c42c9805 2020-11-24 stsp return err;
5999 c42c9805 2020-11-24 stsp return NULL;
6002 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6003 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6004 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6005 6458efa5 2020-11-24 stsp goto done;
6008 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6009 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6012 6458efa5 2020-11-24 stsp view_close(log_view);
6014 6458efa5 2020-11-24 stsp *new_view = log_view;
6015 c42c9805 2020-11-24 stsp free(commit_id);
6016 6458efa5 2020-11-24 stsp return err;
6019 ce5b7c56 2019-07-09 stsp static void
6020 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6022 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6023 34ba6917 2020-11-30 stsp int i = 0;
6025 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6028 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6029 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6030 34ba6917 2020-11-30 stsp if (re == NULL)
6032 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6033 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6037 34ba6917 2020-11-30 stsp static void
6038 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6040 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6041 6458efa5 2020-11-24 stsp int n = 0;
6043 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6044 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6046 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6048 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6049 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
6050 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
6051 6458efa5 2020-11-24 stsp if (last) {
6052 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6053 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6058 6458efa5 2020-11-24 stsp static const struct got_error *
6059 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6061 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6063 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6064 6458efa5 2020-11-24 stsp return NULL;
6067 6458efa5 2020-11-24 stsp static int
6068 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6070 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6072 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6076 6458efa5 2020-11-24 stsp static const struct got_error *
6077 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6079 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6080 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6082 6458efa5 2020-11-24 stsp if (!view->searching) {
6083 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6084 6458efa5 2020-11-24 stsp return NULL;
6087 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6088 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6089 6458efa5 2020-11-24 stsp if (s->selected_entry)
6090 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6092 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6093 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6095 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6096 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6098 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6099 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6102 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6103 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
6104 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6105 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6107 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6110 6458efa5 2020-11-24 stsp while (1) {
6111 6458efa5 2020-11-24 stsp if (re == NULL) {
6112 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6113 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6114 6458efa5 2020-11-24 stsp return NULL;
6116 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6117 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6119 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6122 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6123 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6124 6458efa5 2020-11-24 stsp s->matched_entry = re;
6128 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6129 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6131 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6134 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6135 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6136 6458efa5 2020-11-24 stsp s->selected = 0;
6139 6458efa5 2020-11-24 stsp return NULL;
6142 6458efa5 2020-11-24 stsp static const struct got_error *
6143 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6145 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6146 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6147 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6148 6458efa5 2020-11-24 stsp char *line = NULL;
6149 6458efa5 2020-11-24 stsp wchar_t *wline;
6150 6458efa5 2020-11-24 stsp struct tog_color *tc;
6151 6458efa5 2020-11-24 stsp int width, n;
6152 6458efa5 2020-11-24 stsp int limit = view->nlines;
6154 6458efa5 2020-11-24 stsp werase(view->window);
6156 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6158 6458efa5 2020-11-24 stsp if (limit == 0)
6159 6458efa5 2020-11-24 stsp return NULL;
6161 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6163 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6164 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6165 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6167 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6168 6458efa5 2020-11-24 stsp if (err) {
6169 6458efa5 2020-11-24 stsp free(line);
6170 6458efa5 2020-11-24 stsp return err;
6172 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6173 6458efa5 2020-11-24 stsp wstandout(view->window);
6174 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6175 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6176 6458efa5 2020-11-24 stsp wstandend(view->window);
6177 6458efa5 2020-11-24 stsp free(wline);
6178 6458efa5 2020-11-24 stsp wline = NULL;
6179 6458efa5 2020-11-24 stsp free(line);
6180 6458efa5 2020-11-24 stsp line = NULL;
6181 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6182 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6183 6458efa5 2020-11-24 stsp if (--limit <= 0)
6184 6458efa5 2020-11-24 stsp return NULL;
6187 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6188 6458efa5 2020-11-24 stsp char *line = NULL;
6190 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6191 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
6192 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
6193 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6194 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6195 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6196 6458efa5 2020-11-24 stsp struct got_object_id *id;
6197 6458efa5 2020-11-24 stsp char *id_str;
6198 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6200 6458efa5 2020-11-24 stsp return err;
6201 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6202 6458efa5 2020-11-24 stsp if (err) {
6204 6458efa5 2020-11-24 stsp return err;
6206 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
6207 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6208 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6210 6458efa5 2020-11-24 stsp free(id_str);
6211 6458efa5 2020-11-24 stsp return err;
6214 6458efa5 2020-11-24 stsp free(id_str);
6216 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6217 6458efa5 2020-11-24 stsp if (line == NULL)
6218 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6221 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6222 6458efa5 2020-11-24 stsp if (err) {
6223 6458efa5 2020-11-24 stsp free(line);
6224 6458efa5 2020-11-24 stsp return err;
6226 6458efa5 2020-11-24 stsp if (n == s->selected) {
6227 6458efa5 2020-11-24 stsp if (view->focussed)
6228 6458efa5 2020-11-24 stsp wstandout(view->window);
6229 6458efa5 2020-11-24 stsp s->selected_entry = re;
6231 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6233 6458efa5 2020-11-24 stsp wattr_on(view->window,
6234 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6235 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6237 6458efa5 2020-11-24 stsp wattr_off(view->window,
6238 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6239 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6240 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6241 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6242 6458efa5 2020-11-24 stsp wstandend(view->window);
6243 6458efa5 2020-11-24 stsp free(line);
6244 6458efa5 2020-11-24 stsp free(wline);
6245 6458efa5 2020-11-24 stsp wline = NULL;
6247 6458efa5 2020-11-24 stsp s->ndisplayed++;
6248 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6251 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6254 6458efa5 2020-11-24 stsp view_vborder(view);
6255 6458efa5 2020-11-24 stsp return err;
6258 6458efa5 2020-11-24 stsp static const struct got_error *
6259 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6260 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6262 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6263 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6264 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6266 c42c9805 2020-11-24 stsp *new_view = NULL;
6268 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6269 c42c9805 2020-11-24 stsp if (err) {
6270 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6271 c42c9805 2020-11-24 stsp return err;
6273 c42c9805 2020-11-24 stsp return NULL;
6277 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6278 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6279 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6280 c42c9805 2020-11-24 stsp goto done;
6283 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6284 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6286 c42c9805 2020-11-24 stsp goto done;
6288 c42c9805 2020-11-24 stsp *new_view = tree_view;
6290 c42c9805 2020-11-24 stsp free(commit_id);
6291 c42c9805 2020-11-24 stsp return err;
6293 c42c9805 2020-11-24 stsp static const struct got_error *
6294 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6296 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6297 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6298 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6299 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6300 e4526bf5 2021-09-03 naddy int begin_x = 0, n;
6302 6458efa5 2020-11-24 stsp switch (ch) {
6304 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6306 98182bd0 2021-11-20 thomas case 'o':
6307 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
6308 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6309 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
6310 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
6311 c591440f 2021-11-20 thomas if (err)
6313 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
6314 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
6315 c591440f 2021-11-20 thomas &tog_refs, s->repo);
6316 3bfadbd4 2021-11-20 thomas if (err)
6318 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
6319 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
6321 6458efa5 2020-11-24 stsp case KEY_ENTER:
6322 6458efa5 2020-11-24 stsp case '\r':
6323 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6325 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6326 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6327 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6329 e78dc838 2020-12-04 stsp view->focussed = 0;
6330 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6331 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6332 6458efa5 2020-11-24 stsp err = view_close_child(view);
6334 6458efa5 2020-11-24 stsp return err;
6335 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6336 e78dc838 2020-12-04 stsp view->focus_child = 1;
6338 6458efa5 2020-11-24 stsp *new_view = log_view;
6341 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6343 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6344 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6345 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6347 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6349 e78dc838 2020-12-04 stsp view->focussed = 0;
6350 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6351 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6352 c42c9805 2020-11-24 stsp err = view_close_child(view);
6354 c42c9805 2020-11-24 stsp return err;
6355 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6356 e78dc838 2020-12-04 stsp view->focus_child = 1;
6358 c42c9805 2020-11-24 stsp *new_view = tree_view;
6360 e4526bf5 2021-09-03 naddy case 'g':
6361 e4526bf5 2021-09-03 naddy case KEY_HOME:
6362 e4526bf5 2021-09-03 naddy s->selected = 0;
6363 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6365 e4526bf5 2021-09-03 naddy case 'G':
6366 e4526bf5 2021-09-03 naddy case KEY_END:
6367 e4526bf5 2021-09-03 naddy s->selected = 0;
6368 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6369 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6370 e4526bf5 2021-09-03 naddy if (re == NULL)
6372 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6373 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6375 e4526bf5 2021-09-03 naddy if (n > 0)
6376 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6379 6458efa5 2020-11-24 stsp case KEY_UP:
6380 f7140bf5 2021-10-17 thomas case CTRL('p'):
6381 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6382 6458efa5 2020-11-24 stsp s->selected--;
6385 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6387 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6388 6458efa5 2020-11-24 stsp case CTRL('b'):
6389 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6390 34ba6917 2020-11-30 stsp s->selected = 0;
6391 3e135950 2020-12-01 naddy ref_scroll_up(s, MAX(0, view->nlines - 1));
6394 6458efa5 2020-11-24 stsp case KEY_DOWN:
6395 f7140bf5 2021-10-17 thomas case CTRL('n'):
6396 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6397 6458efa5 2020-11-24 stsp s->selected++;
6400 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6401 6458efa5 2020-11-24 stsp /* can't scroll any further */
6403 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6405 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6406 6458efa5 2020-11-24 stsp case CTRL('f'):
6407 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6408 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6409 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6410 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6413 3e135950 2020-12-01 naddy ref_scroll_down(s, view->nlines - 1);
6415 6458efa5 2020-11-24 stsp case CTRL('l'):
6416 8924d611 2020-12-26 stsp tog_free_refs();
6417 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
6420 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6421 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6423 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6424 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6425 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6431 6458efa5 2020-11-24 stsp return err;
6434 6458efa5 2020-11-24 stsp __dead static void
6435 6458efa5 2020-11-24 stsp usage_ref(void)
6438 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6439 6458efa5 2020-11-24 stsp getprogname());
6443 6458efa5 2020-11-24 stsp static const struct got_error *
6444 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6446 6458efa5 2020-11-24 stsp const struct got_error *error;
6447 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6448 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6449 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6451 6458efa5 2020-11-24 stsp struct tog_view *view;
6453 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6454 6458efa5 2020-11-24 stsp switch (ch) {
6456 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6457 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6458 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6462 e99e2d15 2020-11-24 naddy usage_ref();
6463 6458efa5 2020-11-24 stsp /* NOTREACHED */
6467 6458efa5 2020-11-24 stsp argc -= optind;
6468 6458efa5 2020-11-24 stsp argv += optind;
6470 6458efa5 2020-11-24 stsp if (argc > 1)
6471 e99e2d15 2020-11-24 naddy usage_ref();
6473 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6474 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6475 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6476 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6477 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6478 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6479 c156c7a4 2020-12-18 stsp goto done;
6480 6458efa5 2020-11-24 stsp if (worktree)
6481 6458efa5 2020-11-24 stsp repo_path =
6482 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6484 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6485 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6486 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6487 c156c7a4 2020-12-18 stsp goto done;
6491 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6492 6458efa5 2020-11-24 stsp if (error != NULL)
6493 6458efa5 2020-11-24 stsp goto done;
6495 6458efa5 2020-11-24 stsp init_curses();
6497 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6498 51a10b52 2020-12-26 stsp if (error)
6499 51a10b52 2020-12-26 stsp goto done;
6501 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6502 6458efa5 2020-11-24 stsp if (error)
6503 6458efa5 2020-11-24 stsp goto done;
6505 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6506 6458efa5 2020-11-24 stsp if (view == NULL) {
6507 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6508 6458efa5 2020-11-24 stsp goto done;
6511 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6512 6458efa5 2020-11-24 stsp if (error)
6513 6458efa5 2020-11-24 stsp goto done;
6515 6458efa5 2020-11-24 stsp if (worktree) {
6516 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6517 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6518 6458efa5 2020-11-24 stsp worktree = NULL;
6520 6458efa5 2020-11-24 stsp error = view_loop(view);
6522 6458efa5 2020-11-24 stsp free(repo_path);
6523 6458efa5 2020-11-24 stsp free(cwd);
6524 1d0f4054 2021-06-17 stsp if (repo) {
6525 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6526 1d0f4054 2021-06-17 stsp if (close_err)
6527 1d0f4054 2021-06-17 stsp error = close_err;
6529 51a10b52 2020-12-26 stsp tog_free_refs();
6530 6458efa5 2020-11-24 stsp return error;
6533 6458efa5 2020-11-24 stsp static void
6534 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6538 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6539 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6540 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
6541 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6543 6879ba42 2020-10-01 naddy fputc('\n', fp);
6546 4ed7e80c 2018-05-20 stsp __dead static void
6547 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6549 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6551 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6552 6879ba42 2020-10-01 naddy getprogname());
6553 6879ba42 2020-10-01 naddy if (hflag) {
6554 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6555 6879ba42 2020-10-01 naddy list_commands(fp);
6557 6879ba42 2020-10-01 naddy exit(status);
6560 c2301be8 2018-04-30 stsp static char **
6561 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6563 ee85c5e8 2020-02-29 stsp va_list ap;
6564 c2301be8 2018-04-30 stsp char **argv;
6567 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6569 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6570 c2301be8 2018-04-30 stsp if (argv == NULL)
6571 c2301be8 2018-04-30 stsp err(1, "calloc");
6572 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6573 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6574 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6575 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6578 ee85c5e8 2020-02-29 stsp va_end(ap);
6579 c2301be8 2018-04-30 stsp return argv;
6583 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6584 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6585 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6586 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6587 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6589 ee85c5e8 2020-02-29 stsp static const struct got_error *
6590 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6592 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
6593 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
6594 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6595 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6596 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6597 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6598 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6599 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6601 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6602 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6603 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6605 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6606 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6607 ee85c5e8 2020-02-29 stsp goto done;
6609 ee85c5e8 2020-02-29 stsp if (worktree)
6610 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6612 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6613 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6614 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6615 ee85c5e8 2020-02-29 stsp goto done;
6618 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6619 ee85c5e8 2020-02-29 stsp if (error != NULL)
6620 ee85c5e8 2020-02-29 stsp goto done;
6622 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6623 ee85c5e8 2020-02-29 stsp repo, worktree);
6624 ee85c5e8 2020-02-29 stsp if (error)
6625 ee85c5e8 2020-02-29 stsp goto done;
6627 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6628 84de9106 2020-12-26 stsp if (error)
6629 84de9106 2020-12-26 stsp goto done;
6630 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6631 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6632 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6633 ee85c5e8 2020-02-29 stsp if (error)
6634 ee85c5e8 2020-02-29 stsp goto done;
6636 ee85c5e8 2020-02-29 stsp if (worktree) {
6637 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6638 ee85c5e8 2020-02-29 stsp worktree = NULL;
6641 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6642 945f9229 2022-04-16 thomas if (error)
6643 945f9229 2022-04-16 thomas goto done;
6645 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6646 ee85c5e8 2020-02-29 stsp if (error) {
6647 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6648 ee85c5e8 2020-02-29 stsp goto done;
6649 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6650 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6651 6879ba42 2020-10-01 naddy usage(1, 1);
6652 ee85c5e8 2020-02-29 stsp /* not reached */
6655 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6656 1d0f4054 2021-06-17 stsp if (error == NULL)
6657 1d0f4054 2021-06-17 stsp error = close_err;
6658 ee85c5e8 2020-02-29 stsp repo = NULL;
6660 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6661 ee85c5e8 2020-02-29 stsp if (error)
6662 ee85c5e8 2020-02-29 stsp goto done;
6664 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6666 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6667 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6669 1d0f4054 2021-06-17 stsp if (repo) {
6670 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6671 1d0f4054 2021-06-17 stsp if (error == NULL)
6672 1d0f4054 2021-06-17 stsp error = close_err;
6674 945f9229 2022-04-16 thomas if (commit)
6675 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6676 ee85c5e8 2020-02-29 stsp if (worktree)
6677 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6679 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6680 ee85c5e8 2020-02-29 stsp free(commit_id);
6681 ee85c5e8 2020-02-29 stsp free(cwd);
6682 ee85c5e8 2020-02-29 stsp free(repo_path);
6683 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6684 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6686 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6687 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6688 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6690 87670572 2020-12-26 naddy tog_free_refs();
6691 ee85c5e8 2020-02-29 stsp return error;
6695 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6697 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6698 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
6699 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6700 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6701 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
6702 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6703 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6706 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6708 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6709 9f7d7167 2018-04-29 stsp switch (ch) {
6711 9f7d7167 2018-04-29 stsp hflag = 1;
6714 53ccebc2 2019-07-30 stsp Vflag = 1;
6717 6879ba42 2020-10-01 naddy usage(hflag, 1);
6718 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6722 9f7d7167 2018-04-29 stsp argc -= optind;
6723 9f7d7167 2018-04-29 stsp argv += optind;
6724 9814e6a3 2020-09-27 naddy optind = 1;
6725 c2301be8 2018-04-30 stsp optreset = 1;
6727 53ccebc2 2019-07-30 stsp if (Vflag) {
6728 53ccebc2 2019-07-30 stsp got_version_print_str();
6729 6879ba42 2020-10-01 naddy return 0;
6732 4010e238 2020-12-04 stsp #ifndef PROFILE
6733 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6734 4010e238 2020-12-04 stsp NULL) == -1)
6735 4010e238 2020-12-04 stsp err(1, "pledge");
6738 c2301be8 2018-04-30 stsp if (argc == 0) {
6739 f29d3e89 2018-06-23 stsp if (hflag)
6740 6879ba42 2020-10-01 naddy usage(hflag, 0);
6741 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6742 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6744 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6748 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6749 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6750 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6751 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6752 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6758 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6759 ee85c5e8 2020-02-29 stsp if (argc != 1)
6760 6879ba42 2020-10-01 naddy usage(0, 1);
6761 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6762 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6764 ee85c5e8 2020-02-29 stsp if (hflag)
6765 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6767 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6771 b46c1e04 2020-09-20 naddy putchar('\n');
6772 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6774 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6775 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6776 a2f4a359 2020-02-28 stsp free(cmd_argv);
6779 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6780 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);