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 def2f970 2021-09-28 thomas #if defined(__FreeBSD__)
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 4ed7e80c 2018-05-20 stsp static 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 4acef5ee 2018-12-24 stsp 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 d91faf3b 2020-12-01 naddy struct got_object_id *commit_id, 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 d91faf3b 2020-12-01 naddy err = got_object_id_by_path(&tree_id, s->repo, commit_id,
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 d91faf3b 2020-12-01 naddy return tree_view_walk_path(s, entry->id, 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,
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 ac4dc263 2021-09-24 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 15a087fe 2019-02-21 stsp 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 d7a04538 2019-02-21 stsp 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 3dbaef42 2020-11-24 stsp err(1, "-C option %s", errstr);
3899 09b5bff8 2020-02-23 naddy case 'r':
3900 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3901 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3902 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3904 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3907 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3910 17020d27 2019-03-07 stsp usage_diff();
3911 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3915 26ed57b2 2018-05-19 stsp argc -= optind;
3916 26ed57b2 2018-05-19 stsp argv += optind;
3918 26ed57b2 2018-05-19 stsp if (argc == 0) {
3919 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3920 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3921 15a94983 2018-12-23 stsp id_str1 = argv[0];
3922 15a94983 2018-12-23 stsp id_str2 = argv[1];
3924 26ed57b2 2018-05-19 stsp usage_diff();
3926 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3927 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3928 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3929 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3930 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3931 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3932 c156c7a4 2020-12-18 stsp goto done;
3933 a273ac94 2020-02-23 naddy if (worktree)
3934 a273ac94 2020-02-23 naddy repo_path =
3935 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3937 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3938 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3939 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3940 c156c7a4 2020-12-18 stsp goto done;
3944 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3945 eb6600df 2019-01-04 stsp if (error)
3946 eb6600df 2019-01-04 stsp goto done;
3948 a273ac94 2020-02-23 naddy init_curses();
3950 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3951 51a10b52 2020-12-26 stsp if (error)
3952 51a10b52 2020-12-26 stsp goto done;
3954 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3955 26ed57b2 2018-05-19 stsp if (error)
3956 26ed57b2 2018-05-19 stsp goto done;
3958 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3959 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3960 26ed57b2 2018-05-19 stsp if (error)
3961 26ed57b2 2018-05-19 stsp goto done;
3963 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3964 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3965 26ed57b2 2018-05-19 stsp if (error)
3966 26ed57b2 2018-05-19 stsp goto done;
3968 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3969 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3970 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3971 ea5e7bb5 2018-08-01 stsp goto done;
3973 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3974 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
3975 5dc9f4bc 2018-08-04 stsp if (error)
3976 5dc9f4bc 2018-08-04 stsp goto done;
3977 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3979 3dbaef42 2020-11-24 stsp free(label1);
3980 3dbaef42 2020-11-24 stsp free(label2);
3981 c02c541e 2019-03-29 stsp free(repo_path);
3982 a273ac94 2020-02-23 naddy free(cwd);
3983 1d0f4054 2021-06-17 stsp if (repo) {
3984 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3985 1d0f4054 2021-06-17 stsp if (error == NULL)
3986 1d0f4054 2021-06-17 stsp error = close_err;
3988 a273ac94 2020-02-23 naddy if (worktree)
3989 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3990 51a10b52 2020-12-26 stsp tog_free_refs();
3991 26ed57b2 2018-05-19 stsp return error;
3994 4ed7e80c 2018-05-20 stsp __dead static void
3995 9f7d7167 2018-04-29 stsp usage_blame(void)
3998 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3999 9f7d7167 2018-04-29 stsp getprogname());
4003 84451b3e 2018-07-10 stsp struct tog_blame_line {
4004 84451b3e 2018-07-10 stsp int annotated;
4005 84451b3e 2018-07-10 stsp struct got_object_id *id;
4008 4ed7e80c 2018-05-20 stsp static const struct got_error *
4009 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4011 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4012 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4013 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4014 84451b3e 2018-07-10 stsp const struct got_error *err;
4015 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
4016 826082fe 2020-12-10 stsp char *line = NULL;
4017 826082fe 2020-12-10 stsp size_t linesize = 0;
4018 826082fe 2020-12-10 stsp ssize_t linelen;
4019 84451b3e 2018-07-10 stsp wchar_t *wline;
4020 27a741e5 2019-09-11 stsp int width;
4021 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4022 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4023 ab089a2a 2018-07-12 stsp char *id_str;
4024 11b20872 2019-11-08 stsp struct tog_color *tc;
4026 4f7c3e5e 2020-12-01 naddy err = got_object_id_str(&id_str, s->blamed_commit->id);
4028 ab089a2a 2018-07-12 stsp return err;
4030 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4031 f7d12f7e 2018-08-01 stsp werase(view->window);
4033 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4034 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4035 ab089a2a 2018-07-12 stsp free(id_str);
4036 ab089a2a 2018-07-12 stsp return err;
4039 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4040 ab089a2a 2018-07-12 stsp free(line);
4041 2550e4c3 2018-07-13 stsp line = NULL;
4043 1cae65b4 2019-09-22 stsp return err;
4044 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4045 a3404814 2018-09-02 stsp wstandout(view->window);
4046 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4048 11b20872 2019-11-08 stsp wattr_on(view->window,
4049 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4050 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4052 11b20872 2019-11-08 stsp wattr_off(view->window,
4053 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4054 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4055 a3404814 2018-09-02 stsp wstandend(view->window);
4056 2550e4c3 2018-07-13 stsp free(wline);
4057 2550e4c3 2018-07-13 stsp wline = NULL;
4058 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4059 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4061 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4062 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4063 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4064 ab089a2a 2018-07-12 stsp free(id_str);
4065 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4067 ab089a2a 2018-07-12 stsp free(id_str);
4068 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4069 3f60a8ef 2018-07-10 stsp free(line);
4070 2550e4c3 2018-07-13 stsp line = NULL;
4072 3f60a8ef 2018-07-10 stsp return err;
4073 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4074 2550e4c3 2018-07-13 stsp free(wline);
4075 2550e4c3 2018-07-13 stsp wline = NULL;
4076 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4077 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4079 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4080 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4081 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4082 826082fe 2020-12-10 stsp if (linelen == -1) {
4083 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4084 826082fe 2020-12-10 stsp s->eof = 1;
4087 84451b3e 2018-07-10 stsp free(line);
4088 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4090 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4093 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4094 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4096 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4097 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4098 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4099 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4100 27a741e5 2019-09-11 stsp !(view->focussed &&
4101 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4102 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4103 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4104 8d0fe45a 2019-08-12 stsp char *id_str;
4105 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4106 8d0fe45a 2019-08-12 stsp if (err) {
4107 8d0fe45a 2019-08-12 stsp free(line);
4108 8d0fe45a 2019-08-12 stsp return err;
4110 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4112 11b20872 2019-11-08 stsp wattr_on(view->window,
4113 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4114 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4116 11b20872 2019-11-08 stsp wattr_off(view->window,
4117 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4118 8d0fe45a 2019-08-12 stsp free(id_str);
4119 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4121 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4122 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4125 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4126 ee41ec32 2018-07-10 stsp prev_id = NULL;
4129 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4130 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4131 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4133 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4134 41605754 2020-11-12 stsp width = 9;
4135 41605754 2020-11-12 stsp wline = wcsdup(L"");
4136 41605754 2020-11-12 stsp if (wline == NULL) {
4137 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4138 41605754 2020-11-12 stsp free(line);
4139 41605754 2020-11-12 stsp return err;
4141 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4142 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4143 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4144 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4145 41605754 2020-11-12 stsp view->window, regmatch);
4146 41605754 2020-11-12 stsp if (err) {
4147 41605754 2020-11-12 stsp free(line);
4148 41605754 2020-11-12 stsp return err;
4150 41605754 2020-11-12 stsp width += 9;
4152 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4153 41605754 2020-11-12 stsp view->ncols - 9, 9);
4154 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4155 41605754 2020-11-12 stsp free(wline);
4156 41605754 2020-11-12 stsp wline = NULL;
4157 41605754 2020-11-12 stsp width += 9;
4160 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4161 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4162 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4163 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4165 826082fe 2020-12-10 stsp free(line);
4166 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4168 1a57306a 2018-09-02 stsp view_vborder(view);
4170 84451b3e 2018-07-10 stsp return NULL;
4173 84451b3e 2018-07-10 stsp static const struct got_error *
4174 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4176 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4177 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4178 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4179 1a76625f 2018-10-22 stsp int errcode;
4181 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4182 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4183 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4185 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4186 1a76625f 2018-10-22 stsp if (errcode)
4187 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4189 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4190 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4191 d68a0a7d 2018-07-10 stsp goto done;
4194 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4195 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4197 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4198 d68a0a7d 2018-07-10 stsp if (line->annotated)
4199 d68a0a7d 2018-07-10 stsp goto done;
4201 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4202 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4203 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4204 84451b3e 2018-07-10 stsp goto done;
4206 84451b3e 2018-07-10 stsp line->annotated = 1;
4208 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4209 1a76625f 2018-10-22 stsp if (errcode)
4210 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4211 84451b3e 2018-07-10 stsp return err;
4214 84451b3e 2018-07-10 stsp static void *
4215 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4217 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4218 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4219 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4220 1a76625f 2018-10-22 stsp int errcode;
4222 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4224 61266923 2020-01-14 stsp return (void *)err;
4226 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4227 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4228 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4229 fc06ba56 2019-08-22 stsp err = NULL;
4231 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4232 1a76625f 2018-10-22 stsp if (errcode)
4233 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4234 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4236 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4237 1d0f4054 2021-06-17 stsp if (err == NULL)
4238 1d0f4054 2021-06-17 stsp err = close_err;
4239 c9beca56 2018-07-22 stsp ta->repo = NULL;
4240 c9beca56 2018-07-22 stsp *ta->complete = 1;
4242 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4243 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4244 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4246 18430de3 2018-07-10 stsp return (void *)err;
4249 245d91c1 2018-07-12 stsp static struct got_object_id *
4250 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4251 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4253 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4255 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4256 8d0fe45a 2019-08-12 stsp return NULL;
4258 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4259 245d91c1 2018-07-12 stsp if (!line->annotated)
4260 245d91c1 2018-07-12 stsp return NULL;
4262 245d91c1 2018-07-12 stsp return line->id;
4265 b880a918 2018-07-10 stsp static const struct got_error *
4266 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4268 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4271 245d91c1 2018-07-12 stsp if (blame->thread) {
4272 1a76625f 2018-10-22 stsp int errcode;
4273 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4274 1a76625f 2018-10-22 stsp if (errcode)
4275 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4276 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4277 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4278 1a76625f 2018-10-22 stsp if (errcode)
4279 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4280 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4281 1a76625f 2018-10-22 stsp if (errcode)
4282 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4283 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4284 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4285 245d91c1 2018-07-12 stsp err = NULL;
4286 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
4288 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4289 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4290 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4291 1d0f4054 2021-06-17 stsp if (err == NULL)
4292 1d0f4054 2021-06-17 stsp err = close_err;
4293 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4295 245d91c1 2018-07-12 stsp if (blame->f) {
4296 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4297 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4298 245d91c1 2018-07-12 stsp blame->f = NULL;
4300 57670559 2018-12-23 stsp if (blame->lines) {
4301 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4302 57670559 2018-12-23 stsp free(blame->lines[i].id);
4303 57670559 2018-12-23 stsp free(blame->lines);
4304 57670559 2018-12-23 stsp blame->lines = NULL;
4306 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4307 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4309 245d91c1 2018-07-12 stsp return err;
4312 245d91c1 2018-07-12 stsp static const struct got_error *
4313 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4315 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4316 fc06ba56 2019-08-22 stsp int *done = arg;
4317 fc06ba56 2019-08-22 stsp int errcode;
4319 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4320 fc06ba56 2019-08-22 stsp if (errcode)
4321 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4322 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4324 fc06ba56 2019-08-22 stsp if (*done)
4325 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4327 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4328 fc06ba56 2019-08-22 stsp if (errcode)
4329 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4330 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4332 fc06ba56 2019-08-22 stsp return err;
4335 fc06ba56 2019-08-22 stsp static const struct got_error *
4336 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4338 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4339 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4340 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4341 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4342 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4343 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4344 15a94983 2018-12-23 stsp int obj_type;
4346 a5388363 2020-12-01 naddy err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4347 a5388363 2020-12-01 naddy s->path);
4349 15a94983 2018-12-23 stsp return err;
4351 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4353 84451b3e 2018-07-10 stsp goto done;
4355 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4356 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4357 84451b3e 2018-07-10 stsp goto done;
4360 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4362 a70480e0 2018-06-23 stsp goto done;
4363 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4364 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4365 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4366 84451b3e 2018-07-10 stsp goto done;
4368 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4369 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4371 1fddf795 2021-01-20 stsp goto done;
4372 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4373 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4374 84451b3e 2018-07-10 stsp goto done;
4377 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4378 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4379 b02560ec 2019-08-19 stsp blame->nlines--;
4381 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4382 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4383 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4384 84451b3e 2018-07-10 stsp goto done;
4387 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4389 bd24772e 2018-07-11 stsp goto done;
4391 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4392 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4393 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4394 a5388363 2020-12-01 naddy blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4395 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4396 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4397 245d91c1 2018-07-12 stsp goto done;
4399 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4401 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4402 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4403 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4404 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4405 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4406 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4407 a5388363 2020-12-01 naddy s->blame_complete = 0;
4409 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4410 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4411 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4412 f5a09613 2020-12-13 naddy s->selected_line = 1;
4414 aa61903a 2021-12-31 thomas s->matched_line = 0;
4418 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4419 27d434c2 2018-09-15 stsp free(obj_id);
4421 245d91c1 2018-07-12 stsp stop_blame(blame);
4422 245d91c1 2018-07-12 stsp return err;
4425 245d91c1 2018-07-12 stsp static const struct got_error *
4426 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4427 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4429 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4430 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4432 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4434 c4843652 2019-08-12 stsp s->path = strdup(path);
4435 c4843652 2019-08-12 stsp if (s->path == NULL)
4436 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4438 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4439 c4843652 2019-08-12 stsp if (err) {
4440 c4843652 2019-08-12 stsp free(s->path);
4441 7cbe629d 2018-08-04 stsp return err;
4444 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4445 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4446 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4447 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4448 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4449 fb2756b9 2018-08-04 stsp s->repo = repo;
4450 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4451 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4453 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4454 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4455 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4456 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4458 11b20872 2019-11-08 stsp return err;
4461 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4462 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4463 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4464 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4465 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4467 a5388363 2020-12-01 naddy return run_blame(view);
4470 e5a0f69f 2018-08-18 stsp static const struct got_error *
4471 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4473 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4474 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4476 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4477 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4479 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4480 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4481 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4482 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4483 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4486 e5a0f69f 2018-08-18 stsp free(s->path);
4487 11b20872 2019-11-08 stsp free_colors(&s->colors);
4489 e5a0f69f 2018-08-18 stsp return err;
4492 7cbe629d 2018-08-04 stsp static const struct got_error *
4493 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4495 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4497 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4498 6c4c42e0 2019-06-24 stsp return NULL;
4501 6c4c42e0 2019-06-24 stsp static const struct got_error *
4502 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4504 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4505 6c4c42e0 2019-06-24 stsp int lineno;
4506 826082fe 2020-12-10 stsp char *line = NULL;
4507 826082fe 2020-12-10 stsp size_t linesize = 0;
4508 826082fe 2020-12-10 stsp ssize_t linelen;
4510 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4511 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4512 6c4c42e0 2019-06-24 stsp return NULL;
4515 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4516 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4517 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4519 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4521 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
4523 6c4c42e0 2019-06-24 stsp while (1) {
4524 6c4c42e0 2019-06-24 stsp off_t offset;
4526 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4527 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4528 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4532 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4533 6c4c42e0 2019-06-24 stsp lineno = 1;
4535 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4538 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4539 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4540 6c4c42e0 2019-06-24 stsp free(line);
4541 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4543 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4544 826082fe 2020-12-10 stsp if (linelen != -1 &&
4545 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4546 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4547 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4550 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4555 826082fe 2020-12-10 stsp free(line);
4557 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4558 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4559 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4562 6c4c42e0 2019-06-24 stsp return NULL;
4565 6c4c42e0 2019-06-24 stsp static const struct got_error *
4566 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4568 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4569 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4570 2b380cc8 2018-10-24 stsp int errcode;
4572 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
4573 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4574 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4575 2b380cc8 2018-10-24 stsp if (errcode)
4576 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4578 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4581 51fe7530 2019-08-19 stsp if (s->blame_complete)
4582 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4584 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4586 669b5ffa 2018-10-07 stsp view_vborder(view);
4587 e5a0f69f 2018-08-18 stsp return err;
4590 e5a0f69f 2018-08-18 stsp static const struct got_error *
4591 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4593 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4594 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4595 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4596 669b5ffa 2018-10-07 stsp int begin_x = 0;
4598 e5a0f69f 2018-08-18 stsp switch (ch) {
4600 1e37a5c2 2019-05-12 jcs s->done = 1;
4602 4deef56f 2021-09-02 naddy case 'g':
4603 4deef56f 2021-09-02 naddy case KEY_HOME:
4604 4deef56f 2021-09-02 naddy s->selected_line = 1;
4605 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4607 4deef56f 2021-09-02 naddy case 'G':
4608 4deef56f 2021-09-02 naddy case KEY_END:
4609 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4610 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4611 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4613 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4614 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4615 4deef56f 2021-09-02 naddy (view->nlines - 3);
4619 1e37a5c2 2019-05-12 jcs case KEY_UP:
4620 f7140bf5 2021-10-17 thomas case CTRL('p'):
4621 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4622 1e37a5c2 2019-05-12 jcs s->selected_line--;
4623 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4624 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4625 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4627 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4628 ea025d1d 2020-02-22 naddy case CTRL('b'):
4629 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4630 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4633 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4634 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4635 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4637 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4640 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4641 f7140bf5 2021-10-17 thomas case CTRL('n'):
4642 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4643 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4644 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4645 1e37a5c2 2019-05-12 jcs s->selected_line++;
4646 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4647 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4648 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4651 1e37a5c2 2019-05-12 jcs case 'p': {
4652 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4653 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4654 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4655 1e37a5c2 2019-05-12 jcs if (id == NULL)
4657 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4658 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4659 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4660 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4661 1e37a5c2 2019-05-12 jcs int obj_type;
4662 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4663 1e37a5c2 2019-05-12 jcs s->repo, id);
4666 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4667 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4668 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4669 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4672 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4673 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4674 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4675 e5a0f69f 2018-08-18 stsp if (err) {
4676 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4677 1e37a5c2 2019-05-12 jcs err = NULL;
4678 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4681 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4683 1e37a5c2 2019-05-12 jcs free(blob_id);
4684 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4685 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4686 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4689 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4691 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4693 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4694 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4696 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4701 1e37a5c2 2019-05-12 jcs s->done = 1;
4702 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4703 1e37a5c2 2019-05-12 jcs s->done = 0;
4704 1e37a5c2 2019-05-12 jcs if (thread_err)
4706 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
4707 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4708 a5388363 2020-12-01 naddy err = run_blame(view);
4713 1e37a5c2 2019-05-12 jcs case 'B': {
4714 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4715 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
4716 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4718 1e37a5c2 2019-05-12 jcs s->done = 1;
4719 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4720 1e37a5c2 2019-05-12 jcs s->done = 0;
4721 1e37a5c2 2019-05-12 jcs if (thread_err)
4723 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4724 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4725 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4726 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
4727 a5388363 2020-12-01 naddy err = run_blame(view);
4732 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4733 1e37a5c2 2019-05-12 jcs case '\r': {
4734 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4735 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4736 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4737 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4738 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4739 1e37a5c2 2019-05-12 jcs if (id == NULL)
4741 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4744 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4745 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4746 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4747 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4748 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4749 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4750 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4751 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4754 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4755 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4756 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4758 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4761 e78dc838 2020-12-04 stsp view->focussed = 0;
4762 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4763 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4764 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4767 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4768 e78dc838 2020-12-04 stsp view->focus_child = 1;
4770 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4775 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4776 ea025d1d 2020-02-22 naddy case CTRL('f'):
4778 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4779 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4780 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4783 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4784 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4785 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4786 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4789 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4790 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4791 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4792 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4794 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4795 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4796 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4798 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4799 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4800 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4801 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4807 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4810 a70480e0 2018-06-23 stsp static const struct got_error *
4811 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4813 a70480e0 2018-06-23 stsp const struct got_error *error;
4814 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4815 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4816 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4817 0587e10c 2020-07-23 stsp char *link_target = NULL;
4818 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4819 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4821 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4823 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4824 a70480e0 2018-06-23 stsp switch (ch) {
4826 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4829 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4830 69069811 2018-08-02 stsp if (repo_path == NULL)
4831 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4835 17020d27 2019-03-07 stsp usage_blame();
4836 a70480e0 2018-06-23 stsp /* NOTREACHED */
4840 a70480e0 2018-06-23 stsp argc -= optind;
4841 a70480e0 2018-06-23 stsp argv += optind;
4843 f135c941 2020-02-20 stsp if (argc != 1)
4844 a70480e0 2018-06-23 stsp usage_blame();
4846 69069811 2018-08-02 stsp if (repo_path == NULL) {
4847 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4848 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4849 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4850 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4851 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4852 c156c7a4 2020-12-18 stsp goto done;
4853 f135c941 2020-02-20 stsp if (worktree)
4854 eb41ed75 2019-02-05 stsp repo_path =
4855 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4857 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4858 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4859 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4860 c156c7a4 2020-12-18 stsp goto done;
4864 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4865 c02c541e 2019-03-29 stsp if (error != NULL)
4866 8e94dd5b 2019-01-04 stsp goto done;
4868 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4869 f135c941 2020-02-20 stsp worktree);
4870 c02c541e 2019-03-29 stsp if (error)
4871 92205607 2019-01-04 stsp goto done;
4873 f135c941 2020-02-20 stsp init_curses();
4875 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4876 51a10b52 2020-12-26 stsp if (error)
4877 51a10b52 2020-12-26 stsp goto done;
4879 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4880 eb41ed75 2019-02-05 stsp if (error)
4881 69069811 2018-08-02 stsp goto done;
4883 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4884 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4885 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4886 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4887 a70480e0 2018-06-23 stsp if (error != NULL)
4888 66b4983c 2018-06-23 stsp goto done;
4889 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4890 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4892 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4893 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4895 a19e88aa 2018-06-23 stsp if (error != NULL)
4896 8b473291 2019-02-21 stsp goto done;
4898 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4899 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4900 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4901 e1cd8fed 2018-08-01 stsp goto done;
4904 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4905 0587e10c 2020-07-23 stsp commit_id, repo);
4906 7cbe629d 2018-08-04 stsp if (error)
4907 7cbe629d 2018-08-04 stsp goto done;
4909 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4910 78756c87 2020-11-24 stsp commit_id, repo);
4911 0587e10c 2020-07-23 stsp if (error)
4912 0587e10c 2020-07-23 stsp goto done;
4913 12314ad4 2019-08-31 stsp if (worktree) {
4914 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4915 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4916 12314ad4 2019-08-31 stsp worktree = NULL;
4918 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4920 69069811 2018-08-02 stsp free(repo_path);
4921 f135c941 2020-02-20 stsp free(in_repo_path);
4922 0587e10c 2020-07-23 stsp free(link_target);
4923 69069811 2018-08-02 stsp free(cwd);
4924 a70480e0 2018-06-23 stsp free(commit_id);
4925 eb41ed75 2019-02-05 stsp if (worktree)
4926 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4927 1d0f4054 2021-06-17 stsp if (repo) {
4928 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4929 1d0f4054 2021-06-17 stsp if (error == NULL)
4930 1d0f4054 2021-06-17 stsp error = close_err;
4932 51a10b52 2020-12-26 stsp tog_free_refs();
4933 a70480e0 2018-06-23 stsp return error;
4936 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4937 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
4939 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4940 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4941 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4942 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4943 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4944 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4945 d86d3b18 2020-12-01 naddy int limit = view->nlines;
4947 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
4949 f7d12f7e 2018-08-01 stsp werase(view->window);
4951 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4952 ffd1d5e5 2018-06-23 stsp return NULL;
4954 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4956 ffd1d5e5 2018-06-23 stsp return err;
4957 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4958 a3404814 2018-09-02 stsp wstandout(view->window);
4959 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4961 11b20872 2019-11-08 stsp wattr_on(view->window,
4962 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4963 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4965 11b20872 2019-11-08 stsp wattr_off(view->window,
4966 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4967 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4968 a3404814 2018-09-02 stsp wstandend(view->window);
4969 2550e4c3 2018-07-13 stsp free(wline);
4970 2550e4c3 2018-07-13 stsp wline = NULL;
4971 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4972 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4973 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4974 ffd1d5e5 2018-06-23 stsp return NULL;
4975 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4977 ce52c690 2018-06-23 stsp return err;
4978 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4979 2550e4c3 2018-07-13 stsp free(wline);
4980 2550e4c3 2018-07-13 stsp wline = NULL;
4981 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4982 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4983 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4984 ffd1d5e5 2018-06-23 stsp return NULL;
4985 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4986 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4987 a1eca9bb 2018-06-23 stsp return NULL;
4989 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
4990 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
4991 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
4992 0cf4efb1 2018-09-29 stsp if (view->focussed)
4993 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4994 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
4996 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4997 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
4998 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4999 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5000 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5001 ffd1d5e5 2018-06-23 stsp return NULL;
5005 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5008 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5009 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5010 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5011 848d6979 2019-08-12 stsp const char *modestr = "";
5012 56e0773d 2019-11-28 stsp mode_t mode;
5014 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5015 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5017 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5018 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5019 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5021 638f9024 2019-05-13 stsp return got_error_from_errno(
5022 230a42bd 2019-05-11 jcs "got_object_id_str");
5024 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5025 63c5ca5d 2019-08-24 stsp modestr = "$";
5026 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5029 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5030 d86d3b18 2020-12-01 naddy te, s->repo);
5031 0d6c6ee3 2020-05-20 stsp if (err) {
5032 0d6c6ee3 2020-05-20 stsp free(id_str);
5033 0d6c6ee3 2020-05-20 stsp return err;
5035 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5036 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5037 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5039 848d6979 2019-08-12 stsp modestr = "@";
5041 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5042 848d6979 2019-08-12 stsp modestr = "/";
5043 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5044 848d6979 2019-08-12 stsp modestr = "*";
5045 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5046 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5047 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5048 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5049 1d13200f 2018-07-12 stsp free(id_str);
5050 0d6c6ee3 2020-05-20 stsp free(link_target);
5051 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5053 1d13200f 2018-07-12 stsp free(id_str);
5054 0d6c6ee3 2020-05-20 stsp free(link_target);
5055 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5056 ffd1d5e5 2018-06-23 stsp if (err) {
5057 ffd1d5e5 2018-06-23 stsp free(line);
5060 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5061 0cf4efb1 2018-09-29 stsp if (view->focussed)
5062 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5063 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5065 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5067 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5068 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5069 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5071 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5072 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5073 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5074 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5075 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5076 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5077 ffd1d5e5 2018-06-23 stsp free(line);
5078 2550e4c3 2018-07-13 stsp free(wline);
5079 2550e4c3 2018-07-13 stsp wline = NULL;
5081 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5082 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5083 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5087 ffd1d5e5 2018-06-23 stsp return err;
5090 ffd1d5e5 2018-06-23 stsp static void
5091 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5093 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5094 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5095 fa86c4bf 2020-11-29 stsp int i = 0;
5097 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5100 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5101 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5102 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5103 fa86c4bf 2020-11-29 stsp if (!isroot)
5104 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5107 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5108 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5112 fa86c4bf 2020-11-29 stsp static void
5113 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5115 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5116 ffd1d5e5 2018-06-23 stsp int n = 0;
5118 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5119 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5120 694d3271 2020-12-01 naddy s->first_displayed_entry);
5122 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5124 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5125 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5126 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5127 768394f3 2019-01-24 stsp if (last) {
5128 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5129 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5134 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5135 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5136 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5138 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5139 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5140 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5142 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5143 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5144 56e0773d 2019-11-28 stsp + 1 /* slash */;
5146 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5148 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5149 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5150 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5152 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5153 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5154 d9765a41 2018-06-23 stsp while (pt) {
5155 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5156 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5157 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5158 cb2ebc8a 2018-06-23 stsp goto done;
5160 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5161 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5162 cb2ebc8a 2018-06-23 stsp goto done;
5164 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5167 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5168 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5169 ce52c690 2018-06-23 stsp goto done;
5173 ce52c690 2018-06-23 stsp if (err) {
5174 ce52c690 2018-06-23 stsp free(*path);
5175 ce52c690 2018-06-23 stsp *path = NULL;
5177 ce52c690 2018-06-23 stsp return err;
5180 ce52c690 2018-06-23 stsp static const struct got_error *
5181 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5182 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5183 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5185 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5186 ce52c690 2018-06-23 stsp char *path;
5187 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5189 a0de39f3 2019-08-09 stsp *new_view = NULL;
5191 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5193 ce52c690 2018-06-23 stsp return err;
5195 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5196 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5197 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5198 83ce39e3 2019-08-12 stsp goto done;
5201 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5202 e5a0f69f 2018-08-18 stsp if (err) {
5203 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5204 fc06ba56 2019-08-22 stsp err = NULL;
5205 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5207 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5209 83ce39e3 2019-08-12 stsp free(path);
5210 69efd4c4 2018-07-18 stsp return err;
5213 69efd4c4 2018-07-18 stsp static const struct got_error *
5214 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5215 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5217 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5218 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5219 69efd4c4 2018-07-18 stsp char *path;
5221 a0de39f3 2019-08-09 stsp *new_view = NULL;
5223 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5224 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5225 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5227 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5229 69efd4c4 2018-07-18 stsp return err;
5231 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5234 e5a0f69f 2018-08-18 stsp view_close(log_view);
5236 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5237 cb2ebc8a 2018-06-23 stsp free(path);
5238 cb2ebc8a 2018-06-23 stsp return err;
5241 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5242 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5243 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5245 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5246 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5247 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5248 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5250 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5251 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5253 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5254 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5255 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5257 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5259 bc573f3b 2021-07-10 stsp goto done;
5262 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5263 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5264 bc573f3b 2021-07-10 stsp * closed on demand.
5266 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5267 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5269 bc573f3b 2021-07-10 stsp goto done;
5270 bc573f3b 2021-07-10 stsp s->tree = s->root;
5272 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5273 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5274 ffd1d5e5 2018-06-23 stsp goto done;
5276 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5277 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5278 ffd1d5e5 2018-06-23 stsp goto done;
5281 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5282 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5283 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5284 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5285 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5286 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5287 9cd7cbd1 2020-12-07 stsp goto done;
5290 fb2756b9 2018-08-04 stsp s->repo = repo;
5292 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5293 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5294 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5295 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5297 c0b01bdb 2019-11-08 stsp goto done;
5298 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5299 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5301 c0b01bdb 2019-11-08 stsp goto done;
5302 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5303 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5304 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5306 c0b01bdb 2019-11-08 stsp goto done;
5308 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5309 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5310 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5312 11b20872 2019-11-08 stsp goto done;
5314 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5315 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5317 c0b01bdb 2019-11-08 stsp goto done;
5320 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5321 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5322 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5323 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5324 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5326 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5327 bc573f3b 2021-07-10 stsp if (commit)
5328 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5330 bc573f3b 2021-07-10 stsp close_tree_view(view);
5331 ad80ab7b 2018-08-04 stsp return err;
5334 e5a0f69f 2018-08-18 stsp static const struct got_error *
5335 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5337 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5339 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5340 fb2756b9 2018-08-04 stsp free(s->tree_label);
5341 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5342 6484ec90 2018-09-29 stsp free(s->commit_id);
5343 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5344 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5345 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5346 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5347 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5348 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5349 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5350 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5351 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5352 ad80ab7b 2018-08-04 stsp free(parent);
5355 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5356 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5357 bc573f3b 2021-07-10 stsp if (s->root)
5358 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5359 7c32bd05 2019-06-22 stsp return NULL;
5362 7c32bd05 2019-06-22 stsp static const struct got_error *
5363 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5365 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5367 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5368 7c32bd05 2019-06-22 stsp return NULL;
5371 7c32bd05 2019-06-22 stsp static int
5372 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5374 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5376 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5380 7c32bd05 2019-06-22 stsp static const struct got_error *
5381 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5383 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5384 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5386 7c32bd05 2019-06-22 stsp if (!view->searching) {
5387 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5388 7c32bd05 2019-06-22 stsp return NULL;
5391 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5392 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5393 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5394 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5395 56e0773d 2019-11-28 stsp s->selected_entry);
5397 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5399 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5400 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5402 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5403 56e0773d 2019-11-28 stsp s->selected_entry);
5406 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
5407 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
5408 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
5409 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5411 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5414 7c32bd05 2019-06-22 stsp while (1) {
5415 56e0773d 2019-11-28 stsp if (te == NULL) {
5416 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5417 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5418 ac66afb8 2019-06-24 stsp return NULL;
5420 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5421 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5423 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5426 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5427 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5428 56e0773d 2019-11-28 stsp s->matched_entry = te;
5432 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5433 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5435 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5438 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5439 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5440 7c32bd05 2019-06-22 stsp s->selected = 0;
5443 e5a0f69f 2018-08-18 stsp return NULL;
5446 ad80ab7b 2018-08-04 stsp static const struct got_error *
5447 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5449 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5450 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5451 e5a0f69f 2018-08-18 stsp char *parent_path;
5453 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5455 e5a0f69f 2018-08-18 stsp return err;
5457 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5458 e5a0f69f 2018-08-18 stsp free(parent_path);
5460 669b5ffa 2018-10-07 stsp view_vborder(view);
5461 e5a0f69f 2018-08-18 stsp return err;
5464 e5a0f69f 2018-08-18 stsp static const struct got_error *
5465 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5467 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5468 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5469 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5470 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5471 e4526bf5 2021-09-03 naddy int begin_x = 0, n;
5473 e5a0f69f 2018-08-18 stsp switch (ch) {
5475 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5478 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5480 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5481 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5482 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5483 e78dc838 2020-12-04 stsp view->focussed = 0;
5484 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5485 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5486 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5488 1e37a5c2 2019-05-12 jcs return err;
5489 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5490 e78dc838 2020-12-04 stsp view->focus_child = 1;
5492 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5495 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5496 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5497 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5498 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5499 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5500 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5501 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5502 152c1c93 2020-11-29 stsp if (err) {
5503 152c1c93 2020-11-29 stsp view_close(ref_view);
5504 152c1c93 2020-11-29 stsp return err;
5506 e78dc838 2020-12-04 stsp view->focussed = 0;
5507 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5508 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5509 152c1c93 2020-11-29 stsp err = view_close_child(view);
5511 152c1c93 2020-11-29 stsp return err;
5512 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5513 e78dc838 2020-12-04 stsp view->focus_child = 1;
5515 152c1c93 2020-11-29 stsp *new_view = ref_view;
5517 e4526bf5 2021-09-03 naddy case 'g':
5518 e4526bf5 2021-09-03 naddy case KEY_HOME:
5519 e4526bf5 2021-09-03 naddy s->selected = 0;
5520 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5521 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5522 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5524 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5526 e4526bf5 2021-09-03 naddy case 'G':
5527 e4526bf5 2021-09-03 naddy case KEY_END:
5528 e4526bf5 2021-09-03 naddy s->selected = 0;
5529 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5530 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5531 e4526bf5 2021-09-03 naddy if (te == NULL) {
5532 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5533 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5538 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5539 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5541 e4526bf5 2021-09-03 naddy if (n > 0)
5542 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5545 1e37a5c2 2019-05-12 jcs case KEY_UP:
5546 f7140bf5 2021-10-17 thomas case CTRL('p'):
5547 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5548 1e37a5c2 2019-05-12 jcs s->selected--;
5551 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5553 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5554 ea025d1d 2020-02-22 naddy case CTRL('b'):
5555 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5556 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5557 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5558 fa86c4bf 2020-11-29 stsp s->selected = 0;
5560 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5561 fa86c4bf 2020-11-29 stsp s->selected = 0;
5563 3e135950 2020-12-01 naddy tree_scroll_up(s, MAX(0, view->nlines - 3));
5566 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5567 f7140bf5 2021-10-17 thomas case CTRL('n'):
5568 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5569 1e37a5c2 2019-05-12 jcs s->selected++;
5572 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5574 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5576 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5578 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5579 ea025d1d 2020-02-22 naddy case CTRL('f'):
5580 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5582 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5583 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5584 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5587 3e135950 2020-12-01 naddy tree_scroll_down(s, view->nlines - 3);
5589 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5591 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5592 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5593 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5594 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5595 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5597 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5598 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5600 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5601 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5602 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5603 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5604 1e37a5c2 2019-05-12 jcs s->selected_entry =
5605 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5606 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5607 1e37a5c2 2019-05-12 jcs free(parent);
5608 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5609 56e0773d 2019-11-28 stsp s->selected_entry))) {
5610 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5611 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5612 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5615 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5616 941e9f74 2019-05-21 stsp if (err) {
5617 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5620 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5621 56e0773d 2019-11-28 stsp s->selected_entry))) {
5622 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5623 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5624 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5626 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5627 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5628 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5631 e78dc838 2020-12-04 stsp view->focussed = 0;
5632 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5633 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5634 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5636 669b5ffa 2018-10-07 stsp return err;
5637 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5638 e78dc838 2020-12-04 stsp view->focus_child = 1;
5640 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5643 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5644 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5645 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5651 ffd1d5e5 2018-06-23 stsp return err;
5654 ffd1d5e5 2018-06-23 stsp __dead static void
5655 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5658 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5659 ffd1d5e5 2018-06-23 stsp getprogname());
5663 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5664 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5666 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5667 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5668 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5669 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5670 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5671 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5672 4e97c21c 2020-12-06 stsp char *label = NULL;
5673 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5674 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5676 5221c383 2018-08-01 stsp struct tog_view *view;
5678 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5679 ffd1d5e5 2018-06-23 stsp switch (ch) {
5681 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5684 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5685 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5686 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5690 e99e2d15 2020-11-24 naddy usage_tree();
5691 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5695 ffd1d5e5 2018-06-23 stsp argc -= optind;
5696 ffd1d5e5 2018-06-23 stsp argv += optind;
5698 55cccc34 2020-02-20 stsp if (argc > 1)
5699 e99e2d15 2020-11-24 naddy usage_tree();
5701 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5702 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5703 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5704 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5705 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5706 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5707 c156c7a4 2020-12-18 stsp goto done;
5708 55cccc34 2020-02-20 stsp if (worktree)
5709 52185f70 2019-02-05 stsp repo_path =
5710 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5712 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5713 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5714 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5715 c156c7a4 2020-12-18 stsp goto done;
5719 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5720 c02c541e 2019-03-29 stsp if (error != NULL)
5721 52185f70 2019-02-05 stsp goto done;
5723 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5724 55cccc34 2020-02-20 stsp repo, worktree);
5725 55cccc34 2020-02-20 stsp if (error)
5726 55cccc34 2020-02-20 stsp goto done;
5728 55cccc34 2020-02-20 stsp init_curses();
5730 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5731 c02c541e 2019-03-29 stsp if (error)
5732 52185f70 2019-02-05 stsp goto done;
5734 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5735 51a10b52 2020-12-26 stsp if (error)
5736 51a10b52 2020-12-26 stsp goto done;
5738 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5739 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5740 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5741 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5742 4e97c21c 2020-12-06 stsp if (error)
5743 4e97c21c 2020-12-06 stsp goto done;
5744 4e97c21c 2020-12-06 stsp head_ref_name = label;
5746 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5747 4e97c21c 2020-12-06 stsp if (error == NULL)
5748 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5749 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5750 4e97c21c 2020-12-06 stsp goto done;
5751 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5752 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5753 4e97c21c 2020-12-06 stsp if (error)
5754 4e97c21c 2020-12-06 stsp goto done;
5757 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5758 5221c383 2018-08-01 stsp if (view == NULL) {
5759 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5760 5221c383 2018-08-01 stsp goto done;
5762 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
5763 ad80ab7b 2018-08-04 stsp if (error)
5764 ad80ab7b 2018-08-04 stsp goto done;
5765 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5766 55cccc34 2020-02-20 stsp error = tree_view_walk_path(&view->state.tree, commit_id,
5767 d91faf3b 2020-12-01 naddy in_repo_path);
5768 55cccc34 2020-02-20 stsp if (error)
5769 55cccc34 2020-02-20 stsp goto done;
5772 55cccc34 2020-02-20 stsp if (worktree) {
5773 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5774 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5775 55cccc34 2020-02-20 stsp worktree = NULL;
5777 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5779 52185f70 2019-02-05 stsp free(repo_path);
5780 e4a0e26d 2020-02-20 stsp free(cwd);
5781 ffd1d5e5 2018-06-23 stsp free(commit_id);
5782 4e97c21c 2020-12-06 stsp free(label);
5784 486cd271 2020-12-06 stsp got_ref_close(ref);
5785 1d0f4054 2021-06-17 stsp if (repo) {
5786 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5787 1d0f4054 2021-06-17 stsp if (error == NULL)
5788 1d0f4054 2021-06-17 stsp error = close_err;
5790 51a10b52 2020-12-26 stsp tog_free_refs();
5791 ffd1d5e5 2018-06-23 stsp return error;
5794 6458efa5 2020-11-24 stsp static const struct got_error *
5795 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5797 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5798 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5800 6458efa5 2020-11-24 stsp s->nrefs = 0;
5801 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
5802 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
5803 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
5804 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
5805 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
5808 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5809 6458efa5 2020-11-24 stsp if (re == NULL)
5810 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5812 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
5813 8924d611 2020-12-26 stsp if (re->ref == NULL)
5814 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
5815 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5816 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5819 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5820 6458efa5 2020-11-24 stsp return NULL;
5824 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5826 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5828 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5829 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5830 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5831 8924d611 2020-12-26 stsp got_ref_close(re->ref);
5836 6458efa5 2020-11-24 stsp static const struct got_error *
5837 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5839 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5840 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5842 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5843 6458efa5 2020-11-24 stsp s->repo = repo;
5845 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5846 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5848 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5850 6458efa5 2020-11-24 stsp return err;
5852 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5853 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5854 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5855 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5857 6458efa5 2020-11-24 stsp goto done;
5859 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5860 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5861 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5863 6458efa5 2020-11-24 stsp goto done;
5865 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5866 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5867 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5868 2183bbf6 2022-01-23 thomas if (err)
5869 2183bbf6 2022-01-23 thomas goto done;
5871 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
5872 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
5873 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
5875 6458efa5 2020-11-24 stsp goto done;
5878 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5879 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5880 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5881 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5882 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5885 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5886 6458efa5 2020-11-24 stsp return err;
5889 6458efa5 2020-11-24 stsp static const struct got_error *
5890 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5892 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5894 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5895 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5897 6458efa5 2020-11-24 stsp return NULL;
5900 6458efa5 2020-11-24 stsp static const struct got_error *
5901 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5902 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5904 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5905 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5906 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5907 6458efa5 2020-11-24 stsp int obj_type;
5909 c42c9805 2020-11-24 stsp *commit_id = NULL;
5911 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5913 6458efa5 2020-11-24 stsp return err;
5915 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5917 6458efa5 2020-11-24 stsp goto done;
5919 6458efa5 2020-11-24 stsp switch (obj_type) {
5920 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
5921 c42c9805 2020-11-24 stsp *commit_id = obj_id;
5923 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
5924 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
5926 6458efa5 2020-11-24 stsp goto done;
5927 c42c9805 2020-11-24 stsp free(obj_id);
5928 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
5929 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5931 6458efa5 2020-11-24 stsp goto done;
5932 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5933 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5934 c42c9805 2020-11-24 stsp goto done;
5936 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
5937 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5938 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
5939 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
5940 c42c9805 2020-11-24 stsp goto done;
5944 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5950 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
5951 c42c9805 2020-11-24 stsp if (err) {
5952 c42c9805 2020-11-24 stsp free(*commit_id);
5953 c42c9805 2020-11-24 stsp *commit_id = NULL;
5955 c42c9805 2020-11-24 stsp return err;
5958 c42c9805 2020-11-24 stsp static const struct got_error *
5959 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
5960 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5962 c42c9805 2020-11-24 stsp struct tog_view *log_view;
5963 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
5964 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
5966 c42c9805 2020-11-24 stsp *new_view = NULL;
5968 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
5969 c42c9805 2020-11-24 stsp if (err) {
5970 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
5971 c42c9805 2020-11-24 stsp return err;
5973 c42c9805 2020-11-24 stsp return NULL;
5976 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5977 6458efa5 2020-11-24 stsp if (log_view == NULL) {
5978 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
5979 6458efa5 2020-11-24 stsp goto done;
5982 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
5983 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
5986 6458efa5 2020-11-24 stsp view_close(log_view);
5988 6458efa5 2020-11-24 stsp *new_view = log_view;
5989 c42c9805 2020-11-24 stsp free(commit_id);
5990 6458efa5 2020-11-24 stsp return err;
5993 ce5b7c56 2019-07-09 stsp static void
5994 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5996 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
5997 34ba6917 2020-11-30 stsp int i = 0;
5999 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6002 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6003 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6004 34ba6917 2020-11-30 stsp if (re == NULL)
6006 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6007 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6011 34ba6917 2020-11-30 stsp static void
6012 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6014 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6015 6458efa5 2020-11-24 stsp int n = 0;
6017 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6018 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6020 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6022 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6023 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
6024 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
6025 6458efa5 2020-11-24 stsp if (last) {
6026 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6027 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6032 6458efa5 2020-11-24 stsp static const struct got_error *
6033 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6035 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6037 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6038 6458efa5 2020-11-24 stsp return NULL;
6041 6458efa5 2020-11-24 stsp static int
6042 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6044 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6046 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6050 6458efa5 2020-11-24 stsp static const struct got_error *
6051 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6053 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6054 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6056 6458efa5 2020-11-24 stsp if (!view->searching) {
6057 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6058 6458efa5 2020-11-24 stsp return NULL;
6061 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6062 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6063 6458efa5 2020-11-24 stsp if (s->selected_entry)
6064 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6066 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6067 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6069 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6070 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6072 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6073 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6076 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6077 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
6078 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6079 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6081 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6084 6458efa5 2020-11-24 stsp while (1) {
6085 6458efa5 2020-11-24 stsp if (re == NULL) {
6086 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6087 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6088 6458efa5 2020-11-24 stsp return NULL;
6090 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6091 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6093 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6096 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6097 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6098 6458efa5 2020-11-24 stsp s->matched_entry = re;
6102 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6103 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6105 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6108 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6109 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6110 6458efa5 2020-11-24 stsp s->selected = 0;
6113 6458efa5 2020-11-24 stsp return NULL;
6116 6458efa5 2020-11-24 stsp static const struct got_error *
6117 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6119 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6120 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6121 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6122 6458efa5 2020-11-24 stsp char *line = NULL;
6123 6458efa5 2020-11-24 stsp wchar_t *wline;
6124 6458efa5 2020-11-24 stsp struct tog_color *tc;
6125 6458efa5 2020-11-24 stsp int width, n;
6126 6458efa5 2020-11-24 stsp int limit = view->nlines;
6128 6458efa5 2020-11-24 stsp werase(view->window);
6130 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6132 6458efa5 2020-11-24 stsp if (limit == 0)
6133 6458efa5 2020-11-24 stsp return NULL;
6135 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6137 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6138 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6139 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6141 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6142 6458efa5 2020-11-24 stsp if (err) {
6143 6458efa5 2020-11-24 stsp free(line);
6144 6458efa5 2020-11-24 stsp return err;
6146 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6147 6458efa5 2020-11-24 stsp wstandout(view->window);
6148 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6149 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6150 6458efa5 2020-11-24 stsp wstandend(view->window);
6151 6458efa5 2020-11-24 stsp free(wline);
6152 6458efa5 2020-11-24 stsp wline = NULL;
6153 6458efa5 2020-11-24 stsp free(line);
6154 6458efa5 2020-11-24 stsp line = NULL;
6155 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6156 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6157 6458efa5 2020-11-24 stsp if (--limit <= 0)
6158 6458efa5 2020-11-24 stsp return NULL;
6161 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6162 6458efa5 2020-11-24 stsp char *line = NULL;
6164 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6165 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
6166 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
6167 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6168 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6169 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6170 6458efa5 2020-11-24 stsp struct got_object_id *id;
6171 6458efa5 2020-11-24 stsp char *id_str;
6172 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6174 6458efa5 2020-11-24 stsp return err;
6175 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6176 6458efa5 2020-11-24 stsp if (err) {
6178 6458efa5 2020-11-24 stsp return err;
6180 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
6181 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6182 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6184 6458efa5 2020-11-24 stsp free(id_str);
6185 6458efa5 2020-11-24 stsp return err;
6188 6458efa5 2020-11-24 stsp free(id_str);
6190 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6191 6458efa5 2020-11-24 stsp if (line == NULL)
6192 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6195 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6196 6458efa5 2020-11-24 stsp if (err) {
6197 6458efa5 2020-11-24 stsp free(line);
6198 6458efa5 2020-11-24 stsp return err;
6200 6458efa5 2020-11-24 stsp if (n == s->selected) {
6201 6458efa5 2020-11-24 stsp if (view->focussed)
6202 6458efa5 2020-11-24 stsp wstandout(view->window);
6203 6458efa5 2020-11-24 stsp s->selected_entry = re;
6205 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6207 6458efa5 2020-11-24 stsp wattr_on(view->window,
6208 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6209 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6211 6458efa5 2020-11-24 stsp wattr_off(view->window,
6212 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6213 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6214 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6215 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6216 6458efa5 2020-11-24 stsp wstandend(view->window);
6217 6458efa5 2020-11-24 stsp free(line);
6218 6458efa5 2020-11-24 stsp free(wline);
6219 6458efa5 2020-11-24 stsp wline = NULL;
6221 6458efa5 2020-11-24 stsp s->ndisplayed++;
6222 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6225 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6228 6458efa5 2020-11-24 stsp view_vborder(view);
6229 6458efa5 2020-11-24 stsp return err;
6232 6458efa5 2020-11-24 stsp static const struct got_error *
6233 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6234 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6236 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6237 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6238 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6240 c42c9805 2020-11-24 stsp *new_view = NULL;
6242 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6243 c42c9805 2020-11-24 stsp if (err) {
6244 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6245 c42c9805 2020-11-24 stsp return err;
6247 c42c9805 2020-11-24 stsp return NULL;
6251 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6252 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6253 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6254 c42c9805 2020-11-24 stsp goto done;
6257 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6258 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6260 c42c9805 2020-11-24 stsp goto done;
6262 c42c9805 2020-11-24 stsp *new_view = tree_view;
6264 c42c9805 2020-11-24 stsp free(commit_id);
6265 c42c9805 2020-11-24 stsp return err;
6267 c42c9805 2020-11-24 stsp static const struct got_error *
6268 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6270 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6271 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6272 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6273 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6274 e4526bf5 2021-09-03 naddy int begin_x = 0, n;
6276 6458efa5 2020-11-24 stsp switch (ch) {
6278 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6280 98182bd0 2021-11-20 thomas case 'o':
6281 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
6282 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6283 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
6284 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
6285 c591440f 2021-11-20 thomas if (err)
6287 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
6288 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
6289 c591440f 2021-11-20 thomas &tog_refs, s->repo);
6290 3bfadbd4 2021-11-20 thomas if (err)
6292 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
6293 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
6295 6458efa5 2020-11-24 stsp case KEY_ENTER:
6296 6458efa5 2020-11-24 stsp case '\r':
6297 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6299 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6300 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6301 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6303 e78dc838 2020-12-04 stsp view->focussed = 0;
6304 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6305 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6306 6458efa5 2020-11-24 stsp err = view_close_child(view);
6308 6458efa5 2020-11-24 stsp return err;
6309 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6310 e78dc838 2020-12-04 stsp view->focus_child = 1;
6312 6458efa5 2020-11-24 stsp *new_view = log_view;
6315 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6317 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6318 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6319 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6321 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6323 e78dc838 2020-12-04 stsp view->focussed = 0;
6324 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6325 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6326 c42c9805 2020-11-24 stsp err = view_close_child(view);
6328 c42c9805 2020-11-24 stsp return err;
6329 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6330 e78dc838 2020-12-04 stsp view->focus_child = 1;
6332 c42c9805 2020-11-24 stsp *new_view = tree_view;
6334 e4526bf5 2021-09-03 naddy case 'g':
6335 e4526bf5 2021-09-03 naddy case KEY_HOME:
6336 e4526bf5 2021-09-03 naddy s->selected = 0;
6337 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6339 e4526bf5 2021-09-03 naddy case 'G':
6340 e4526bf5 2021-09-03 naddy case KEY_END:
6341 e4526bf5 2021-09-03 naddy s->selected = 0;
6342 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6343 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6344 e4526bf5 2021-09-03 naddy if (re == NULL)
6346 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6347 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6349 e4526bf5 2021-09-03 naddy if (n > 0)
6350 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6353 6458efa5 2020-11-24 stsp case KEY_UP:
6354 f7140bf5 2021-10-17 thomas case CTRL('p'):
6355 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6356 6458efa5 2020-11-24 stsp s->selected--;
6359 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6361 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6362 6458efa5 2020-11-24 stsp case CTRL('b'):
6363 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6364 34ba6917 2020-11-30 stsp s->selected = 0;
6365 3e135950 2020-12-01 naddy ref_scroll_up(s, MAX(0, view->nlines - 1));
6368 6458efa5 2020-11-24 stsp case KEY_DOWN:
6369 f7140bf5 2021-10-17 thomas case CTRL('n'):
6370 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6371 6458efa5 2020-11-24 stsp s->selected++;
6374 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6375 6458efa5 2020-11-24 stsp /* can't scroll any further */
6377 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6379 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6380 6458efa5 2020-11-24 stsp case CTRL('f'):
6381 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6382 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6383 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6384 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6387 3e135950 2020-12-01 naddy ref_scroll_down(s, view->nlines - 1);
6389 6458efa5 2020-11-24 stsp case CTRL('l'):
6390 8924d611 2020-12-26 stsp tog_free_refs();
6391 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
6394 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6395 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6397 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6398 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6399 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6405 6458efa5 2020-11-24 stsp return err;
6408 6458efa5 2020-11-24 stsp __dead static void
6409 6458efa5 2020-11-24 stsp usage_ref(void)
6412 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6413 6458efa5 2020-11-24 stsp getprogname());
6417 6458efa5 2020-11-24 stsp static const struct got_error *
6418 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6420 6458efa5 2020-11-24 stsp const struct got_error *error;
6421 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6422 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6423 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6425 6458efa5 2020-11-24 stsp struct tog_view *view;
6427 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6428 6458efa5 2020-11-24 stsp switch (ch) {
6430 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6431 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6432 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6436 e99e2d15 2020-11-24 naddy usage_ref();
6437 6458efa5 2020-11-24 stsp /* NOTREACHED */
6441 6458efa5 2020-11-24 stsp argc -= optind;
6442 6458efa5 2020-11-24 stsp argv += optind;
6444 6458efa5 2020-11-24 stsp if (argc > 1)
6445 e99e2d15 2020-11-24 naddy usage_ref();
6447 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6448 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6449 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6450 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6451 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6452 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6453 c156c7a4 2020-12-18 stsp goto done;
6454 6458efa5 2020-11-24 stsp if (worktree)
6455 6458efa5 2020-11-24 stsp repo_path =
6456 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6458 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6459 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6460 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6461 c156c7a4 2020-12-18 stsp goto done;
6465 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6466 6458efa5 2020-11-24 stsp if (error != NULL)
6467 6458efa5 2020-11-24 stsp goto done;
6469 6458efa5 2020-11-24 stsp init_curses();
6471 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6472 51a10b52 2020-12-26 stsp if (error)
6473 51a10b52 2020-12-26 stsp goto done;
6475 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6476 6458efa5 2020-11-24 stsp if (error)
6477 6458efa5 2020-11-24 stsp goto done;
6479 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6480 6458efa5 2020-11-24 stsp if (view == NULL) {
6481 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6482 6458efa5 2020-11-24 stsp goto done;
6485 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6486 6458efa5 2020-11-24 stsp if (error)
6487 6458efa5 2020-11-24 stsp goto done;
6489 6458efa5 2020-11-24 stsp if (worktree) {
6490 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6491 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6492 6458efa5 2020-11-24 stsp worktree = NULL;
6494 6458efa5 2020-11-24 stsp error = view_loop(view);
6496 6458efa5 2020-11-24 stsp free(repo_path);
6497 6458efa5 2020-11-24 stsp free(cwd);
6498 1d0f4054 2021-06-17 stsp if (repo) {
6499 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6500 1d0f4054 2021-06-17 stsp if (close_err)
6501 1d0f4054 2021-06-17 stsp error = close_err;
6503 51a10b52 2020-12-26 stsp tog_free_refs();
6504 6458efa5 2020-11-24 stsp return error;
6507 6458efa5 2020-11-24 stsp static void
6508 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6512 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6513 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6514 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
6515 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6517 6879ba42 2020-10-01 naddy fputc('\n', fp);
6520 4ed7e80c 2018-05-20 stsp __dead static void
6521 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6523 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6525 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6526 6879ba42 2020-10-01 naddy getprogname());
6527 6879ba42 2020-10-01 naddy if (hflag) {
6528 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6529 6879ba42 2020-10-01 naddy list_commands(fp);
6531 6879ba42 2020-10-01 naddy exit(status);
6534 c2301be8 2018-04-30 stsp static char **
6535 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6537 ee85c5e8 2020-02-29 stsp va_list ap;
6538 c2301be8 2018-04-30 stsp char **argv;
6541 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6543 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6544 c2301be8 2018-04-30 stsp if (argv == NULL)
6545 c2301be8 2018-04-30 stsp err(1, "calloc");
6546 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6547 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6548 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6549 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6552 ee85c5e8 2020-02-29 stsp va_end(ap);
6553 c2301be8 2018-04-30 stsp return argv;
6557 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6558 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6559 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6560 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6561 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6563 ee85c5e8 2020-02-29 stsp static const struct got_error *
6564 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6566 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
6567 ee85c5e8 2020-02-29 stsp struct tog_cmd *cmd = NULL;
6568 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6569 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6570 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6571 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6572 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6574 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6575 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6576 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6578 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6579 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6580 ee85c5e8 2020-02-29 stsp goto done;
6582 ee85c5e8 2020-02-29 stsp if (worktree)
6583 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6585 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6586 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6587 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6588 ee85c5e8 2020-02-29 stsp goto done;
6591 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6592 ee85c5e8 2020-02-29 stsp if (error != NULL)
6593 ee85c5e8 2020-02-29 stsp goto done;
6595 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6596 ee85c5e8 2020-02-29 stsp repo, worktree);
6597 ee85c5e8 2020-02-29 stsp if (error)
6598 ee85c5e8 2020-02-29 stsp goto done;
6600 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6601 84de9106 2020-12-26 stsp if (error)
6602 84de9106 2020-12-26 stsp goto done;
6603 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6604 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6605 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6606 ee85c5e8 2020-02-29 stsp if (error)
6607 ee85c5e8 2020-02-29 stsp goto done;
6609 ee85c5e8 2020-02-29 stsp if (worktree) {
6610 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6611 ee85c5e8 2020-02-29 stsp worktree = NULL;
6614 ee85c5e8 2020-02-29 stsp error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6615 ee85c5e8 2020-02-29 stsp if (error) {
6616 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6617 ee85c5e8 2020-02-29 stsp goto done;
6618 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6619 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6620 6879ba42 2020-10-01 naddy usage(1, 1);
6621 ee85c5e8 2020-02-29 stsp /* not reached */
6624 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6625 1d0f4054 2021-06-17 stsp if (error == NULL)
6626 1d0f4054 2021-06-17 stsp error = close_err;
6627 ee85c5e8 2020-02-29 stsp repo = NULL;
6629 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6630 ee85c5e8 2020-02-29 stsp if (error)
6631 ee85c5e8 2020-02-29 stsp goto done;
6633 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6635 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6636 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6638 1d0f4054 2021-06-17 stsp if (repo) {
6639 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6640 1d0f4054 2021-06-17 stsp if (error == NULL)
6641 1d0f4054 2021-06-17 stsp error = close_err;
6643 ee85c5e8 2020-02-29 stsp if (worktree)
6644 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6646 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6647 ee85c5e8 2020-02-29 stsp free(commit_id);
6648 ee85c5e8 2020-02-29 stsp free(cwd);
6649 ee85c5e8 2020-02-29 stsp free(repo_path);
6650 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6651 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6653 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6654 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6655 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6657 87670572 2020-12-26 naddy tog_free_refs();
6658 ee85c5e8 2020-02-29 stsp return error;
6662 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6664 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6665 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
6666 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6667 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6668 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
6669 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6670 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6673 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6675 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6676 9f7d7167 2018-04-29 stsp switch (ch) {
6678 9f7d7167 2018-04-29 stsp hflag = 1;
6681 53ccebc2 2019-07-30 stsp Vflag = 1;
6684 6879ba42 2020-10-01 naddy usage(hflag, 1);
6685 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6689 9f7d7167 2018-04-29 stsp argc -= optind;
6690 9f7d7167 2018-04-29 stsp argv += optind;
6691 9814e6a3 2020-09-27 naddy optind = 1;
6692 c2301be8 2018-04-30 stsp optreset = 1;
6694 53ccebc2 2019-07-30 stsp if (Vflag) {
6695 53ccebc2 2019-07-30 stsp got_version_print_str();
6696 6879ba42 2020-10-01 naddy return 0;
6699 4010e238 2020-12-04 stsp #ifndef PROFILE
6700 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6701 4010e238 2020-12-04 stsp NULL) == -1)
6702 4010e238 2020-12-04 stsp err(1, "pledge");
6705 c2301be8 2018-04-30 stsp if (argc == 0) {
6706 f29d3e89 2018-06-23 stsp if (hflag)
6707 6879ba42 2020-10-01 naddy usage(hflag, 0);
6708 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6709 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6711 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6715 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6716 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6717 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6718 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6719 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6725 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6726 ee85c5e8 2020-02-29 stsp if (argc != 1)
6727 6879ba42 2020-10-01 naddy usage(0, 1);
6728 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6729 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6731 ee85c5e8 2020-02-29 stsp if (hflag)
6732 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6734 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6738 b46c1e04 2020-09-20 naddy putchar('\n');
6739 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6741 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6742 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6743 a2f4a359 2020-02-28 stsp free(cmd_argv);
6746 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6747 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);