2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
18 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
20 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
21 31120ada 2018-04-30 stsp #include <errno.h>
22 d24ddaa6 2022-02-26 thomas #if defined(__FreeBSD__) || defined(__APPLE__)
23 def2f970 2021-09-28 thomas #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
25 9f7d7167 2018-04-29 stsp #include <curses.h>
26 9f7d7167 2018-04-29 stsp #include <panel.h>
27 9f7d7167 2018-04-29 stsp #include <locale.h>
28 9a267125 2022-05-31 thomas #include <sha1.h>
29 61266923 2020-01-14 stsp #include <signal.h>
30 9f7d7167 2018-04-29 stsp #include <stdlib.h>
31 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
32 26ed57b2 2018-05-19 stsp #include <stdio.h>
33 9f7d7167 2018-04-29 stsp #include <getopt.h>
34 9f7d7167 2018-04-29 stsp #include <string.h>
35 9f7d7167 2018-04-29 stsp #include <err.h>
36 80ddbec8 2018-04-29 stsp #include <unistd.h>
37 26ed57b2 2018-05-19 stsp #include <limits.h>
38 61e69b96 2018-05-20 stsp #include <wchar.h>
39 788c352e 2018-06-16 stsp #include <time.h>
40 84451b3e 2018-07-10 stsp #include <pthread.h>
41 5036bf37 2018-09-24 stsp #include <libgen.h>
42 60493ae3 2019-06-20 stsp #include <regex.h>
43 3da8ef85 2021-09-21 stsp #include <sched.h>
45 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
47 53ccebc2 2019-07-30 stsp #include "got_version.h"
48 9f7d7167 2018-04-29 stsp #include "got_error.h"
49 80ddbec8 2018-04-29 stsp #include "got_object.h"
50 80ddbec8 2018-04-29 stsp #include "got_reference.h"
51 80ddbec8 2018-04-29 stsp #include "got_repository.h"
52 80ddbec8 2018-04-29 stsp #include "got_diff.h"
53 511a516b 2018-05-19 stsp #include "got_opentemp.h"
54 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
55 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
56 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
57 a70480e0 2018-06-23 stsp #include "got_blame.h"
58 c2db6724 2019-01-04 stsp #include "got_privsep.h"
59 1dd54920 2019-05-11 stsp #include "got_path.h"
60 b7165be3 2019-02-05 stsp #include "got_worktree.h"
62 dd038bc6 2021-09-21 thomas.ad //#define update_panels() (0)
63 dd038bc6 2021-09-21 thomas.ad //#define doupdate() (0)
66 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
70 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
73 acf52a76 2021-09-21 thomas.ad #ifndef CTRL
74 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
77 9f7d7167 2018-04-29 stsp #ifndef nitems
78 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
81 9f7d7167 2018-04-29 stsp struct tog_cmd {
82 c2301be8 2018-04-30 stsp const char *name;
83 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
84 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
87 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
88 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
89 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
90 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
91 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
92 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
94 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
95 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
96 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
97 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
98 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
100 641a8ee6 2022-02-16 thomas static const struct tog_cmd tog_commands[] = {
101 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
102 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
103 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
104 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
105 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
108 d6b05b5b 2018-08-04 stsp enum tog_view_type {
109 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
110 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
111 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
112 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
113 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
116 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
118 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
119 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
120 ba4f502b 2018-08-04 stsp struct got_object_id *id;
121 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
124 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
125 ba4f502b 2018-08-04 stsp struct commit_queue {
126 ba4f502b 2018-08-04 stsp int ncommits;
127 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
130 f26dddb7 2019-11-08 stsp struct tog_color {
131 dbdddfee 2021-06-23 naddy STAILQ_ENTRY(tog_color) entry;
132 6d17833f 2019-11-08 stsp regex_t regex;
133 6d17833f 2019-11-08 stsp short colorpair;
135 dbdddfee 2021-06-23 naddy STAILQ_HEAD(tog_colors, tog_color);
137 d9dff0e5 2020-12-26 stsp static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
138 51a10b52 2020-12-26 stsp static struct got_reflist_object_id_map *tog_refs_idmap;
140 2183bbf6 2022-01-23 thomas static const struct got_error *
141 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
142 2183bbf6 2022-01-23 thomas struct got_reference* re2)
144 2183bbf6 2022-01-23 thomas const char *name1 = got_ref_get_name(re1);
145 2183bbf6 2022-01-23 thomas const char *name2 = got_ref_get_name(re2);
146 2183bbf6 2022-01-23 thomas int isbackup1, isbackup2;
148 2183bbf6 2022-01-23 thomas /* Sort backup refs towards the bottom of the list. */
149 2183bbf6 2022-01-23 thomas isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
150 2183bbf6 2022-01-23 thomas isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
151 2183bbf6 2022-01-23 thomas if (!isbackup1 && isbackup2) {
152 2183bbf6 2022-01-23 thomas *cmp = -1;
153 2183bbf6 2022-01-23 thomas return NULL;
154 2183bbf6 2022-01-23 thomas } else if (isbackup1 && !isbackup2) {
155 2183bbf6 2022-01-23 thomas *cmp = 1;
156 2183bbf6 2022-01-23 thomas return NULL;
159 2183bbf6 2022-01-23 thomas *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
160 2183bbf6 2022-01-23 thomas return NULL;
163 11b20872 2019-11-08 stsp static const struct got_error *
164 3bfadbd4 2021-11-20 thomas tog_load_refs(struct got_repository *repo, int sort_by_date)
166 51a10b52 2020-12-26 stsp const struct got_error *err;
168 3bfadbd4 2021-11-20 thomas err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
169 2183bbf6 2022-01-23 thomas got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
172 51a10b52 2020-12-26 stsp return err;
174 51a10b52 2020-12-26 stsp return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
178 51a10b52 2020-12-26 stsp static void
179 51a10b52 2020-12-26 stsp tog_free_refs(void)
181 51a10b52 2020-12-26 stsp if (tog_refs_idmap) {
182 f193b038 2020-12-26 stsp got_reflist_object_id_map_free(tog_refs_idmap);
183 51a10b52 2020-12-26 stsp tog_refs_idmap = NULL;
185 51a10b52 2020-12-26 stsp got_ref_list_free(&tog_refs);
188 51a10b52 2020-12-26 stsp static const struct got_error *
189 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
190 11b20872 2019-11-08 stsp int idx, short color)
192 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
193 11b20872 2019-11-08 stsp struct tog_color *tc;
194 11b20872 2019-11-08 stsp int regerr = 0;
196 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
197 11b20872 2019-11-08 stsp return NULL;
199 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
201 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
202 11b20872 2019-11-08 stsp if (tc == NULL)
203 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
204 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
205 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
206 11b20872 2019-11-08 stsp if (regerr) {
207 11b20872 2019-11-08 stsp static char regerr_msg[512];
208 11b20872 2019-11-08 stsp static char err_msg[512];
209 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
210 11b20872 2019-11-08 stsp sizeof(regerr_msg));
211 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
212 11b20872 2019-11-08 stsp regerr_msg);
213 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
215 11b20872 2019-11-08 stsp return err;
217 11b20872 2019-11-08 stsp tc->colorpair = idx;
218 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(colors, tc, entry);
219 11b20872 2019-11-08 stsp return NULL;
222 11b20872 2019-11-08 stsp static void
223 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
225 11b20872 2019-11-08 stsp struct tog_color *tc;
227 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(colors)) {
228 dbdddfee 2021-06-23 naddy tc = STAILQ_FIRST(colors);
229 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(colors, entry);
230 11b20872 2019-11-08 stsp regfree(&tc->regex);
235 11b20872 2019-11-08 stsp struct tog_color *
236 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
238 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
240 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
241 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
245 11b20872 2019-11-08 stsp return NULL;
249 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
251 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
252 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
253 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
254 11b20872 2019-11-08 stsp return COLOR_CYAN;
255 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
256 11b20872 2019-11-08 stsp return COLOR_YELLOW;
257 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
258 11b20872 2019-11-08 stsp return COLOR_GREEN;
259 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
260 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
261 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
262 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
263 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
264 91b8c405 2020-01-25 stsp return COLOR_CYAN;
265 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
266 11b20872 2019-11-08 stsp return COLOR_GREEN;
267 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
268 11b20872 2019-11-08 stsp return COLOR_GREEN;
269 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
270 11b20872 2019-11-08 stsp return COLOR_CYAN;
271 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
272 11b20872 2019-11-08 stsp return COLOR_YELLOW;
273 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
274 6458efa5 2020-11-24 stsp return COLOR_GREEN;
275 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
276 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
277 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
278 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
279 2183bbf6 2022-01-23 thomas if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
280 2183bbf6 2022-01-23 thomas return COLOR_CYAN;
286 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
288 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
290 11b20872 2019-11-08 stsp if (val == NULL)
291 11b20872 2019-11-08 stsp return default_color_value(envvar);
293 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
294 11b20872 2019-11-08 stsp return COLOR_BLACK;
295 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
296 11b20872 2019-11-08 stsp return COLOR_RED;
297 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
298 11b20872 2019-11-08 stsp return COLOR_GREEN;
299 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
300 11b20872 2019-11-08 stsp return COLOR_YELLOW;
301 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
302 11b20872 2019-11-08 stsp return COLOR_BLUE;
303 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
304 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
305 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
306 11b20872 2019-11-08 stsp return COLOR_CYAN;
307 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
308 11b20872 2019-11-08 stsp return COLOR_WHITE;
309 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
312 11b20872 2019-11-08 stsp return default_color_value(envvar);
316 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
317 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
318 3dbaef42 2020-11-24 stsp const char *label1, *label2;
320 15a087fe 2019-02-21 stsp int first_displayed_line;
321 15a087fe 2019-02-21 stsp int last_displayed_line;
323 15a087fe 2019-02-21 stsp int diff_context;
324 3dbaef42 2020-11-24 stsp int ignore_whitespace;
325 64453f7e 2020-11-21 stsp int force_text_diff;
326 15a087fe 2019-02-21 stsp struct got_repository *repo;
327 bddb1296 2019-11-08 stsp struct tog_colors colors;
328 fe621944 2020-11-10 stsp size_t nlines;
329 f44b1f58 2020-02-02 tracey off_t *line_offsets;
330 f44b1f58 2020-02-02 tracey int matched_line;
331 f44b1f58 2020-02-02 tracey int selected_line;
333 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
334 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
337 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
339 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
340 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
341 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
342 1a76625f 2018-10-22 stsp int commits_needed;
343 fb280deb 2021-08-30 stsp int load_all;
344 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
345 1a76625f 2018-10-22 stsp struct commit_queue *commits;
346 1a76625f 2018-10-22 stsp const char *in_repo_path;
347 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
348 1a76625f 2018-10-22 stsp struct got_repository *repo;
349 1a76625f 2018-10-22 stsp int log_complete;
350 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
351 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
352 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
353 13add988 2019-10-15 stsp int *searching;
354 13add988 2019-10-15 stsp int *search_next_done;
355 13add988 2019-10-15 stsp regex_t *regex;
358 1a76625f 2018-10-22 stsp struct tog_log_view_state {
359 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
360 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
361 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
362 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
363 b01e7d3b 2018-08-04 stsp int selected;
364 b01e7d3b 2018-08-04 stsp char *in_repo_path;
365 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
366 b672a97a 2020-01-27 stsp int log_branches;
367 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
368 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
369 1a76625f 2018-10-22 stsp sig_atomic_t quit;
370 1a76625f 2018-10-22 stsp pthread_t thread;
371 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
372 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
373 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
374 11b20872 2019-11-08 stsp struct tog_colors colors;
377 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
378 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
379 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
380 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
381 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
382 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
383 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
384 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
385 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
386 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
387 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
388 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
389 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
390 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
391 2183bbf6 2022-01-23 thomas #define TOG_COLOR_REFS_BACKUP 15
393 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
394 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
395 e9424729 2018-08-04 stsp int nlines;
397 e9424729 2018-08-04 stsp struct tog_view *view;
398 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
402 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
403 e9424729 2018-08-04 stsp const char *path;
404 e9424729 2018-08-04 stsp struct got_repository *repo;
405 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
406 e9424729 2018-08-04 stsp int *complete;
407 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
408 fc06ba56 2019-08-22 stsp void *cancel_arg;
411 e9424729 2018-08-04 stsp struct tog_blame {
413 be659d10 2020-11-18 stsp off_t filesize;
414 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
415 6fcac457 2018-11-19 stsp int nlines;
416 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
417 e9424729 2018-08-04 stsp pthread_t thread;
418 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
419 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
420 e9424729 2018-08-04 stsp const char *path;
423 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
424 7cbe629d 2018-08-04 stsp int first_displayed_line;
425 7cbe629d 2018-08-04 stsp int last_displayed_line;
426 7cbe629d 2018-08-04 stsp int selected_line;
427 7cbe629d 2018-08-04 stsp int blame_complete;
430 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
431 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
432 e5a0f69f 2018-08-18 stsp char *path;
433 7cbe629d 2018-08-04 stsp struct got_repository *repo;
434 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
435 e9424729 2018-08-04 stsp struct tog_blame blame;
436 6c4c42e0 2019-06-24 stsp int matched_line;
437 11b20872 2019-11-08 stsp struct tog_colors colors;
440 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
441 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
442 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
443 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
444 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
445 ad80ab7b 2018-08-04 stsp int selected;
448 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
450 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
451 ad80ab7b 2018-08-04 stsp char *tree_label;
452 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id;/* commit which this tree belongs to */
453 bc573f3b 2021-07-10 stsp struct got_tree_object *root; /* the commit's root tree entry */
454 bc573f3b 2021-07-10 stsp struct got_tree_object *tree; /* currently displayed (sub-)tree */
455 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
456 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
457 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
458 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
459 bc573f3b 2021-07-10 stsp struct tog_parent_trees parents; /* parent trees of current sub-tree */
460 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
461 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
462 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
463 6458efa5 2020-11-24 stsp struct tog_colors colors;
466 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
467 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
468 6458efa5 2020-11-24 stsp struct got_reference *ref;
472 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
474 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
475 dae613fa 2020-12-26 stsp struct tog_reflist_head refs;
476 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
477 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
478 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
479 3bfadbd4 2021-11-20 thomas int nrefs, ndisplayed, selected, show_ids, sort_by_date;
480 6458efa5 2020-11-24 stsp struct got_repository *repo;
481 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
482 bddb1296 2019-11-08 stsp struct tog_colors colors;
486 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
488 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
489 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
490 669b5ffa 2018-10-07 stsp * there is enough screen estate.
492 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
493 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
495 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
496 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
497 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
499 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
500 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
502 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
504 cc3c9aac 2018-08-01 stsp struct tog_view {
505 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
506 26ed57b2 2018-05-19 stsp WINDOW *window;
507 26ed57b2 2018-05-19 stsp PANEL *panel;
508 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
509 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
510 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
512 669b5ffa 2018-10-07 stsp struct tog_view *parent;
513 669b5ffa 2018-10-07 stsp struct tog_view *child;
516 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
517 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
518 e78dc838 2020-12-04 stsp * between parent and child.
519 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
520 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
521 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
522 e78dc838 2020-12-04 stsp * situations.
524 e78dc838 2020-12-04 stsp int focus_child;
526 5dc9f4bc 2018-08-04 stsp /* type-specific state */
527 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
529 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
530 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
531 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
532 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
533 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
536 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
537 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
538 e78dc838 2020-12-04 stsp struct tog_view *, int);
539 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
541 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
542 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
543 c0c4acc8 2021-01-24 stsp int search_started;
544 60493ae3 2019-06-20 stsp int searching;
545 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
546 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
547 60493ae3 2019-06-20 stsp int search_next_done;
548 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
549 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
550 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
551 1803e47f 2019-06-22 stsp regex_t regex;
552 41605754 2020-11-12 stsp regmatch_t regmatch;
555 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
556 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
557 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
558 78756c87 2020-11-24 stsp struct got_repository *);
559 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
560 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
561 e78dc838 2020-12-04 stsp struct tog_view *, int);
562 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
563 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
564 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
566 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
567 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
568 78756c87 2020-11-24 stsp const char *, const char *, int);
569 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
570 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
571 e78dc838 2020-12-04 stsp struct tog_view *, int);
572 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
573 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
574 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
576 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
577 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
578 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
579 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
580 e78dc838 2020-12-04 stsp struct tog_view *, int);
581 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
582 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
583 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
585 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
586 bc573f3b 2021-07-10 stsp struct got_object_id *, const char *, struct got_repository *);
587 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
588 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
589 e78dc838 2020-12-04 stsp struct tog_view *, int);
590 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
591 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
592 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
594 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
595 6458efa5 2020-11-24 stsp struct got_repository *);
596 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
597 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
598 e78dc838 2020-12-04 stsp struct tog_view *, int);
599 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
600 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
601 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
603 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
604 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
605 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
607 25791caa 2018-10-24 stsp static void
608 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
610 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
613 83baff54 2019-08-12 stsp static void
614 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
616 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
619 61266923 2020-01-14 stsp static void
620 61266923 2020-01-14 stsp tog_sigcont(int signo)
622 61266923 2020-01-14 stsp tog_sigcont_received = 1;
625 e5a0f69f 2018-08-18 stsp static const struct got_error *
626 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
628 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
630 669b5ffa 2018-10-07 stsp if (view->child) {
631 669b5ffa 2018-10-07 stsp view_close(view->child);
632 669b5ffa 2018-10-07 stsp view->child = NULL;
634 e5a0f69f 2018-08-18 stsp if (view->close)
635 e5a0f69f 2018-08-18 stsp err = view->close(view);
636 ea5e7bb5 2018-08-01 stsp if (view->panel)
637 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
638 ea5e7bb5 2018-08-01 stsp if (view->window)
639 ea5e7bb5 2018-08-01 stsp delwin(view->window);
640 ea5e7bb5 2018-08-01 stsp free(view);
641 e5a0f69f 2018-08-18 stsp return err;
644 ea5e7bb5 2018-08-01 stsp static struct tog_view *
645 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
646 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
648 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
650 ea5e7bb5 2018-08-01 stsp if (view == NULL)
651 ea5e7bb5 2018-08-01 stsp return NULL;
653 d6b05b5b 2018-08-04 stsp view->type = type;
654 f7d12f7e 2018-08-01 stsp view->lines = LINES;
655 f7d12f7e 2018-08-01 stsp view->cols = COLS;
656 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
657 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
658 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
659 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
660 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
661 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
662 96a765a8 2018-08-04 stsp view_close(view);
663 ea5e7bb5 2018-08-01 stsp return NULL;
665 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
666 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
667 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
668 96a765a8 2018-08-04 stsp view_close(view);
669 ea5e7bb5 2018-08-01 stsp return NULL;
672 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
673 ea5e7bb5 2018-08-01 stsp return view;
677 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
679 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
681 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
684 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
686 5c60c32a 2018-10-18 stsp static const struct got_error *
687 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
689 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
691 5c60c32a 2018-10-18 stsp view->begin_y = 0;
692 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
693 5c60c32a 2018-10-18 stsp view->nlines = LINES;
694 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
695 5c60c32a 2018-10-18 stsp view->lines = LINES;
696 5c60c32a 2018-10-18 stsp view->cols = COLS;
697 5c60c32a 2018-10-18 stsp err = view_resize(view);
699 5c60c32a 2018-10-18 stsp return err;
701 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
702 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
704 5c60c32a 2018-10-18 stsp return NULL;
707 5c60c32a 2018-10-18 stsp static const struct got_error *
708 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
710 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
712 5c60c32a 2018-10-18 stsp view->begin_x = 0;
713 5c60c32a 2018-10-18 stsp view->begin_y = 0;
714 5c60c32a 2018-10-18 stsp view->nlines = LINES;
715 5c60c32a 2018-10-18 stsp view->ncols = COLS;
716 5c60c32a 2018-10-18 stsp view->lines = LINES;
717 5c60c32a 2018-10-18 stsp view->cols = COLS;
718 5c60c32a 2018-10-18 stsp err = view_resize(view);
720 5c60c32a 2018-10-18 stsp return err;
722 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
723 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
725 5c60c32a 2018-10-18 stsp return NULL;
729 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
731 5c60c32a 2018-10-18 stsp return view->parent == NULL;
734 4d8c2215 2018-08-19 stsp static const struct got_error *
735 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
737 f7d12f7e 2018-08-01 stsp int nlines, ncols;
739 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
740 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
742 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
744 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
745 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
747 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
749 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
750 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
751 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
752 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
753 25791caa 2018-10-24 stsp wclear(view->window);
755 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
756 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
757 0cf4efb1 2018-09-29 stsp view->lines = LINES;
758 0cf4efb1 2018-09-29 stsp view->cols = COLS;
760 6e3e5d9c 2018-10-18 stsp if (view->child) {
761 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
762 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
763 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
764 5c60c32a 2018-10-18 stsp if (view->child->focussed)
765 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
767 5c60c32a 2018-10-18 stsp show_panel(view->panel);
769 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
770 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
774 5c60c32a 2018-10-18 stsp return NULL;
777 669b5ffa 2018-10-07 stsp static const struct got_error *
778 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
780 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
782 669b5ffa 2018-10-07 stsp if (view->child == NULL)
783 669b5ffa 2018-10-07 stsp return NULL;
785 669b5ffa 2018-10-07 stsp err = view_close(view->child);
786 669b5ffa 2018-10-07 stsp view->child = NULL;
787 669b5ffa 2018-10-07 stsp return err;
790 72a9cb46 2020-12-03 stsp static void
791 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
793 669b5ffa 2018-10-07 stsp view->child = child;
794 669b5ffa 2018-10-07 stsp child->parent = view;
798 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
800 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
803 34bc9ec9 2019-02-22 stsp static void
804 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
806 25791caa 2018-10-24 stsp int cols, lines;
807 25791caa 2018-10-24 stsp struct winsize size;
809 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
810 25791caa 2018-10-24 stsp cols = 80; /* Default */
811 25791caa 2018-10-24 stsp lines = 24;
813 25791caa 2018-10-24 stsp cols = size.ws_col;
814 25791caa 2018-10-24 stsp lines = size.ws_row;
816 25791caa 2018-10-24 stsp resize_term(lines, cols);
819 2b49a8ae 2019-06-22 stsp static const struct got_error *
820 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
822 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
823 2b49a8ae 2019-06-22 stsp char pattern[1024];
826 c0c4acc8 2021-01-24 stsp if (view->search_started) {
827 c0c4acc8 2021-01-24 stsp regfree(&view->regex);
828 c0c4acc8 2021-01-24 stsp view->searching = 0;
829 c0c4acc8 2021-01-24 stsp memset(&view->regmatch, 0, sizeof(view->regmatch));
831 c0c4acc8 2021-01-24 stsp view->search_started = 0;
833 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
834 2b49a8ae 2019-06-22 stsp return NULL;
836 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
837 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
839 2b49a8ae 2019-06-22 stsp nocbreak();
841 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
844 2b49a8ae 2019-06-22 stsp if (ret == ERR)
845 2b49a8ae 2019-06-22 stsp return NULL;
847 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
848 7c32bd05 2019-06-22 stsp err = view->search_start(view);
850 7c32bd05 2019-06-22 stsp regfree(&view->regex);
851 7c32bd05 2019-06-22 stsp return err;
853 c0c4acc8 2021-01-24 stsp view->search_started = 1;
854 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
855 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
856 2b49a8ae 2019-06-22 stsp view->search_next(view);
859 2b49a8ae 2019-06-22 stsp return NULL;
862 0cf4efb1 2018-09-29 stsp static const struct got_error *
863 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
864 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
866 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
867 669b5ffa 2018-10-07 stsp struct tog_view *v;
868 1a76625f 2018-10-22 stsp int ch, errcode;
870 e5a0f69f 2018-08-18 stsp *new = NULL;
872 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
873 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
874 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
875 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
877 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
878 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
879 82954512 2020-02-03 stsp if (errcode)
880 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
881 82954512 2020-02-03 stsp "pthread_mutex_unlock");
882 3da8ef85 2021-09-21 stsp sched_yield();
883 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
884 82954512 2020-02-03 stsp if (errcode)
885 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
886 82954512 2020-02-03 stsp "pthread_mutex_lock");
887 60493ae3 2019-06-20 stsp view->search_next(view);
888 60493ae3 2019-06-20 stsp return NULL;
891 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
892 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
893 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
894 1a76625f 2018-10-22 stsp if (errcode)
895 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
896 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
897 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
898 1a76625f 2018-10-22 stsp if (errcode)
899 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
900 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
902 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
903 25791caa 2018-10-24 stsp tog_resizeterm();
904 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
905 61266923 2020-01-14 stsp tog_sigcont_received = 0;
906 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
907 25791caa 2018-10-24 stsp err = view_resize(v);
909 25791caa 2018-10-24 stsp return err;
910 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
912 25791caa 2018-10-24 stsp return err;
913 cdfcfb03 2020-12-06 stsp if (v->child) {
914 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
916 cdfcfb03 2020-12-06 stsp return err;
917 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
918 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
920 cdfcfb03 2020-12-06 stsp return err;
925 e5a0f69f 2018-08-18 stsp switch (ch) {
927 1e37a5c2 2019-05-12 jcs if (view->child) {
928 e78dc838 2020-12-04 stsp view->focussed = 0;
929 e78dc838 2020-12-04 stsp view->child->focussed = 1;
930 e78dc838 2020-12-04 stsp view->focus_child = 1;
931 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
932 e78dc838 2020-12-04 stsp view->focussed = 0;
933 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
934 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
938 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
939 9970f7fc 2020-12-03 stsp view->dying = 1;
945 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
946 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
948 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
949 e78dc838 2020-12-04 stsp view->focussed = 0;
950 e78dc838 2020-12-04 stsp view->child->focussed = 1;
951 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
953 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
956 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
957 9970f7fc 2020-12-03 stsp KEY_RESIZE);
959 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
960 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
961 e78dc838 2020-12-04 stsp view->focussed = 1;
962 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
964 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
968 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
971 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
974 60493ae3 2019-06-20 stsp if (view->search_start)
975 2b49a8ae 2019-06-22 stsp view_search_start(view);
977 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
981 c0c4acc8 2021-01-24 stsp if (view->search_started && view->search_next) {
982 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
983 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
984 60493ae3 2019-06-20 stsp view->search_next_done = 0;
985 60493ae3 2019-06-20 stsp view->search_next(view);
987 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
990 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
994 e5a0f69f 2018-08-18 stsp return err;
998 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
1000 0cf4efb1 2018-09-29 stsp PANEL *panel;
1001 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
1003 669b5ffa 2018-10-07 stsp if (view->parent)
1004 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
1006 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
1007 0cf4efb1 2018-09-29 stsp if (panel == NULL)
1010 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
1011 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1012 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1016 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
1018 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1019 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
1021 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
1023 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
1026 669b5ffa 2018-10-07 stsp return view->focussed;
1029 bcbd79e2 2018-08-19 stsp static const struct got_error *
1030 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
1032 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1033 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
1034 fb59748f 2020-12-05 stsp struct tog_view *new_view;
1035 fd823528 2018-10-22 stsp int fast_refresh = 10;
1036 1a76625f 2018-10-22 stsp int done = 0, errcode;
1038 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1039 1a76625f 2018-10-22 stsp if (errcode)
1040 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
1042 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
1043 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
1045 1004088d 2018-09-29 stsp view->focussed = 1;
1046 878940b7 2018-09-29 stsp err = view->show(view);
1048 0cf4efb1 2018-09-29 stsp return err;
1049 0cf4efb1 2018-09-29 stsp update_panels();
1050 0cf4efb1 2018-09-29 stsp doupdate();
1051 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
1052 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
1053 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
1054 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
1056 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
1059 9970f7fc 2020-12-03 stsp if (view->dying) {
1060 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1062 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1063 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1065 e78dc838 2020-12-04 stsp else if (view->parent)
1066 669b5ffa 2018-10-07 stsp prev = view->parent;
1068 e78dc838 2020-12-04 stsp if (view->parent) {
1069 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1070 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1072 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1074 9970f7fc 2020-12-03 stsp err = view_close(view);
1076 e5a0f69f 2018-08-18 stsp goto done;
1078 e78dc838 2020-12-04 stsp view = NULL;
1079 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1080 e78dc838 2020-12-04 stsp if (v->focussed)
1083 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1084 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1086 e78dc838 2020-12-04 stsp view = prev;
1087 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1088 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1089 e78dc838 2020-12-04 stsp tog_view_list_head);
1091 e78dc838 2020-12-04 stsp if (view) {
1092 e78dc838 2020-12-04 stsp if (view->focus_child) {
1093 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1094 e78dc838 2020-12-04 stsp view = view->child;
1096 e78dc838 2020-12-04 stsp view->focussed = 1;
1100 bcbd79e2 2018-08-19 stsp if (new_view) {
1101 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1102 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1103 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1104 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1106 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1107 86c66b02 2018-10-18 stsp err = view_close(v);
1109 86c66b02 2018-10-18 stsp goto done;
1112 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1113 fed7eaa8 2018-10-24 stsp view = new_view;
1115 669b5ffa 2018-10-07 stsp if (view) {
1116 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1117 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1118 e78dc838 2020-12-04 stsp view = view->child;
1120 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1121 e78dc838 2020-12-04 stsp view = view->parent;
1123 e78dc838 2020-12-04 stsp show_panel(view->panel);
1124 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1125 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1126 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1127 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1129 1a76625f 2018-10-22 stsp goto done;
1131 669b5ffa 2018-10-07 stsp err = view->show(view);
1133 1a76625f 2018-10-22 stsp goto done;
1134 669b5ffa 2018-10-07 stsp if (view->child) {
1135 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1137 1a76625f 2018-10-22 stsp goto done;
1139 1a76625f 2018-10-22 stsp update_panels();
1140 1a76625f 2018-10-22 stsp doupdate();
1144 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1145 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1146 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1147 e5a0f69f 2018-08-18 stsp view_close(view);
1150 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1151 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1152 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1154 e5a0f69f 2018-08-18 stsp return err;
1157 4ed7e80c 2018-05-20 stsp __dead static void
1158 9f7d7167 2018-04-29 stsp usage_log(void)
1161 c70c5802 2018-08-01 stsp fprintf(stderr,
1162 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1163 9f7d7167 2018-04-29 stsp getprogname());
1167 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1168 80ddbec8 2018-04-29 stsp static const struct got_error *
1169 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1171 00dfcb92 2018-06-11 stsp char *vis = NULL;
1172 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1174 963b370f 2018-05-20 stsp *ws = NULL;
1175 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1176 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1177 00dfcb92 2018-06-11 stsp int vislen;
1178 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1179 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1181 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1182 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1184 00dfcb92 2018-06-11 stsp return err;
1185 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1186 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1187 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1188 a7f50699 2018-06-11 stsp goto done;
1192 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1193 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1194 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1195 a7f50699 2018-06-11 stsp goto done;
1198 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1199 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1201 00dfcb92 2018-06-11 stsp free(vis);
1202 963b370f 2018-05-20 stsp if (err) {
1203 963b370f 2018-05-20 stsp free(*ws);
1204 963b370f 2018-05-20 stsp *ws = NULL;
1205 963b370f 2018-05-20 stsp *wlen = 0;
1207 963b370f 2018-05-20 stsp return err;
1210 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1211 963b370f 2018-05-20 stsp static const struct got_error *
1212 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1213 27a741e5 2019-09-11 stsp int col_tab_align)
1215 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1216 963b370f 2018-05-20 stsp int cols = 0;
1217 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1218 963b370f 2018-05-20 stsp size_t wlen;
1221 963b370f 2018-05-20 stsp *wlinep = NULL;
1222 b700b5d6 2018-07-10 stsp *widthp = 0;
1224 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1226 963b370f 2018-05-20 stsp return err;
1228 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1229 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1232 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1233 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1238 27a741e5 2019-09-11 stsp while (i < wlen) {
1239 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1241 27a741e5 2019-09-11 stsp if (width == 0) {
1246 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1247 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1249 27a741e5 2019-09-11 stsp cols += width;
1251 27a741e5 2019-09-11 stsp } else if (width == -1) {
1252 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1253 27a741e5 2019-09-11 stsp width = TABSIZE -
1254 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1256 748d5cab 2020-12-14 naddy width = 1;
1257 748d5cab 2020-12-14 naddy wline[i] = L'.';
1259 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1261 748d5cab 2020-12-14 naddy cols += width;
1264 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1265 963b370f 2018-05-20 stsp goto done;
1268 963b370f 2018-05-20 stsp wline[i] = L'\0';
1269 b700b5d6 2018-07-10 stsp if (widthp)
1270 b700b5d6 2018-07-10 stsp *widthp = cols;
1273 963b370f 2018-05-20 stsp free(wline);
1275 963b370f 2018-05-20 stsp *wlinep = wline;
1276 963b370f 2018-05-20 stsp return err;
1279 8b473291 2019-02-21 stsp static const struct got_error*
1280 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1281 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1283 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1284 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1286 8b473291 2019-02-21 stsp const char *name;
1288 8b473291 2019-02-21 stsp *refs_str = NULL;
1290 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
1291 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1292 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1295 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1296 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1298 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1299 8b473291 2019-02-21 stsp name += 5;
1300 2183bbf6 2022-01-23 thomas if (strncmp(name, "got/", 4) == 0 &&
1301 2183bbf6 2022-01-23 thomas strncmp(name, "got/backup/", 11) != 0)
1303 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1304 8b473291 2019-02-21 stsp name += 6;
1305 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1306 8b473291 2019-02-21 stsp name += 8;
1307 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1308 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1311 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1314 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1315 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1316 5d844a1e 2019-08-13 stsp if (err) {
1317 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1318 48cae60d 2020-09-22 stsp free(ref_id);
1321 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1322 5d844a1e 2019-08-13 stsp err = NULL;
1323 5d844a1e 2019-08-13 stsp tag = NULL;
1326 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1327 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1328 48cae60d 2020-09-22 stsp free(ref_id);
1330 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1331 52b5abe1 2019-08-13 stsp if (cmp != 0)
1333 8b473291 2019-02-21 stsp s = *refs_str;
1334 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1335 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1336 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1338 8b473291 2019-02-21 stsp *refs_str = NULL;
1344 8b473291 2019-02-21 stsp return err;
1347 963b370f 2018-05-20 stsp static const struct got_error *
1348 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1349 27a741e5 2019-09-11 stsp int col_tab_align)
1351 e6b8b890 2020-12-29 naddy char *smallerthan;
1353 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1354 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1355 5813d178 2019-03-09 stsp author = smallerthan + 1;
1356 e6b8b890 2020-12-29 naddy author[strcspn(author, "@>")] = '\0';
1357 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1360 5813d178 2019-03-09 stsp static const struct got_error *
1361 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1362 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1363 8fdc79fe 2020-12-01 naddy int author_display_cols)
1365 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1366 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1367 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1368 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1369 5813d178 2019-03-09 stsp char *author = NULL;
1370 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1371 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1372 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1373 bb737323 2018-05-20 stsp int col, limit;
1374 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1375 ccb26ccd 2018-11-05 stsp struct tm tm;
1376 45d799e2 2018-12-23 stsp time_t committer_time;
1377 11b20872 2019-11-08 stsp struct tog_color *tc;
1379 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1380 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
1381 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
1382 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1383 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1385 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1386 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1388 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1389 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1391 11b20872 2019-11-08 stsp wattr_on(view->window,
1392 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1393 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1395 11b20872 2019-11-08 stsp wattr_off(view->window,
1396 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1397 27a741e5 2019-09-11 stsp col = limit;
1398 b39d25c7 2018-07-10 stsp if (col > avail)
1399 b39d25c7 2018-07-10 stsp goto done;
1401 6570a66d 2019-11-08 stsp if (avail >= 120) {
1402 6570a66d 2019-11-08 stsp char *id_str;
1403 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1405 6570a66d 2019-11-08 stsp goto done;
1406 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1408 11b20872 2019-11-08 stsp wattr_on(view->window,
1409 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1410 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1412 11b20872 2019-11-08 stsp wattr_off(view->window,
1413 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1414 6570a66d 2019-11-08 stsp free(id_str);
1416 6570a66d 2019-11-08 stsp if (col > avail)
1417 6570a66d 2019-11-08 stsp goto done;
1420 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1421 5813d178 2019-03-09 stsp if (author == NULL) {
1422 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1423 80ddbec8 2018-04-29 stsp goto done;
1425 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1427 bb737323 2018-05-20 stsp goto done;
1428 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1430 11b20872 2019-11-08 stsp wattr_on(view->window,
1431 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1432 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1434 11b20872 2019-11-08 stsp wattr_off(view->window,
1435 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1436 bb737323 2018-05-20 stsp col += author_width;
1437 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1438 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1440 bb737323 2018-05-20 stsp author_width++;
1442 9c2eaf34 2018-05-20 stsp if (col > avail)
1443 9c2eaf34 2018-05-20 stsp goto done;
1445 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1447 6d9fbc00 2018-04-29 stsp goto done;
1448 bb737323 2018-05-20 stsp logmsg = logmsg0;
1449 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1451 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1452 bb737323 2018-05-20 stsp if (newline)
1453 bb737323 2018-05-20 stsp *newline = '\0';
1454 bb737323 2018-05-20 stsp limit = avail - col;
1455 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1457 bb737323 2018-05-20 stsp goto done;
1458 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1459 bb737323 2018-05-20 stsp col += logmsg_width;
1460 27a741e5 2019-09-11 stsp while (col < avail) {
1461 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1465 80ddbec8 2018-04-29 stsp free(logmsg0);
1466 bb737323 2018-05-20 stsp free(wlogmsg);
1467 5813d178 2019-03-09 stsp free(author);
1468 bb737323 2018-05-20 stsp free(wauthor);
1469 80ddbec8 2018-04-29 stsp free(line);
1470 80ddbec8 2018-04-29 stsp return err;
1473 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1474 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1475 899d86c2 2018-05-10 stsp struct got_object_id *id)
1477 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1479 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1480 80ddbec8 2018-04-29 stsp if (entry == NULL)
1481 899d86c2 2018-05-10 stsp return NULL;
1483 899d86c2 2018-05-10 stsp entry->id = id;
1484 99db9666 2018-05-07 stsp entry->commit = commit;
1485 899d86c2 2018-05-10 stsp return entry;
1488 99db9666 2018-05-07 stsp static void
1489 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1491 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1493 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1494 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1495 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1496 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1497 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1498 99db9666 2018-05-07 stsp free(entry);
1501 99db9666 2018-05-07 stsp static void
1502 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1504 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1505 99db9666 2018-05-07 stsp pop_commit(commits);
1508 c4972b91 2018-05-07 stsp static const struct got_error *
1509 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1510 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1512 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1513 13add988 2019-10-15 stsp regmatch_t regmatch;
1514 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1516 13add988 2019-10-15 stsp *have_match = 0;
1518 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1520 13add988 2019-10-15 stsp return err;
1522 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1524 13add988 2019-10-15 stsp goto done;
1526 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1527 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1528 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1529 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1530 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1531 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1532 13add988 2019-10-15 stsp *have_match = 1;
1534 13add988 2019-10-15 stsp free(id_str);
1535 13add988 2019-10-15 stsp free(logmsg);
1536 13add988 2019-10-15 stsp return err;
1539 13add988 2019-10-15 stsp static const struct got_error *
1540 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1542 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1545 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1546 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1547 1a76625f 2018-10-22 stsp * while updating the display.
1550 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1551 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1552 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1553 1a76625f 2018-10-22 stsp int errcode;
1555 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1556 4e0d2870 2020-12-07 naddy NULL, NULL);
1557 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1560 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1563 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1564 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1565 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1569 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1570 1a76625f 2018-10-22 stsp if (errcode) {
1571 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1572 13add988 2019-10-15 stsp "pthread_mutex_lock");
1576 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1577 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1578 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1580 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1581 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1582 7c1452c1 2020-03-26 stsp int have_match;
1583 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1586 7c1452c1 2020-03-26 stsp if (have_match)
1587 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1590 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1591 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1592 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1593 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1596 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1598 9ba79e04 2018-06-11 stsp return err;
1601 2b779855 2020-12-05 naddy static void
1602 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1604 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1605 2b779855 2020-12-05 naddy int ncommits = 0;
1607 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1608 2b779855 2020-12-05 naddy while (entry) {
1609 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1610 2b779855 2020-12-05 naddy s->selected_entry = entry;
1613 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1614 2b779855 2020-12-05 naddy ncommits++;
1618 0553a4e3 2018-04-30 stsp static const struct got_error *
1619 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1621 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1622 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1623 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1624 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1625 60493ae3 2019-06-20 stsp int width;
1626 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1627 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1628 8b473291 2019-02-21 stsp char *refs_str = NULL;
1629 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1630 11b20872 2019-11-08 stsp struct tog_color *tc;
1631 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1633 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1634 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1635 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
1636 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1638 ecb28ae0 2018-07-16 stsp return err;
1639 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1640 d2075bf3 2020-12-25 stsp s->selected_entry->id);
1641 d2075bf3 2020-12-25 stsp if (refs) {
1642 d2075bf3 2020-12-25 stsp err = build_refs_str(&refs_str, refs,
1643 d2075bf3 2020-12-25 stsp s->selected_entry->id, s->repo);
1645 d2075bf3 2020-12-25 stsp goto done;
1649 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1650 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1652 fb280deb 2021-08-30 stsp if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1653 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1654 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1655 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1656 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1657 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1658 8f4ed634 2020-03-26 stsp goto done;
1661 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1663 f9686aa5 2020-03-27 stsp if (view->searching) {
1664 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1665 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1666 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1667 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1668 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1669 f9686aa5 2020-03-27 stsp search_str = "searching...";
1672 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1673 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1674 f9686aa5 2020-03-27 stsp search_str ? search_str :
1675 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1676 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1677 8f4ed634 2020-03-26 stsp goto done;
1681 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1682 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1683 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1684 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1685 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1686 1a76625f 2018-10-22 stsp header = NULL;
1687 1a76625f 2018-10-22 stsp goto done;
1689 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1690 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1691 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1692 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1693 1a76625f 2018-10-22 stsp header = NULL;
1694 1a76625f 2018-10-22 stsp goto done;
1696 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1698 1a76625f 2018-10-22 stsp goto done;
1700 2814baeb 2018-08-01 stsp werase(view->window);
1702 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1703 a3404814 2018-09-02 stsp wstandout(view->window);
1704 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1706 11b20872 2019-11-08 stsp wattr_on(view->window,
1707 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1708 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1710 11b20872 2019-11-08 stsp wattr_off(view->window,
1711 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1712 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1713 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1716 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1717 a3404814 2018-09-02 stsp wstandend(view->window);
1718 ecb28ae0 2018-07-16 stsp free(wline);
1719 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1720 1a76625f 2018-10-22 stsp goto done;
1722 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1723 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1724 5813d178 2019-03-09 stsp ncommits = 0;
1725 5813d178 2019-03-09 stsp while (entry) {
1726 5813d178 2019-03-09 stsp char *author;
1727 5813d178 2019-03-09 stsp wchar_t *wauthor;
1728 5813d178 2019-03-09 stsp int width;
1729 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1731 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1732 5813d178 2019-03-09 stsp if (author == NULL) {
1733 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1734 5813d178 2019-03-09 stsp goto done;
1736 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1737 27a741e5 2019-09-11 stsp date_display_cols);
1738 5813d178 2019-03-09 stsp if (author_cols < width)
1739 5813d178 2019-03-09 stsp author_cols = width;
1740 5813d178 2019-03-09 stsp free(wauthor);
1741 5813d178 2019-03-09 stsp free(author);
1742 7ca04879 2019-10-19 stsp ncommits++;
1743 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1746 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1747 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1748 867c6645 2018-07-10 stsp ncommits = 0;
1749 899d86c2 2018-05-10 stsp while (entry) {
1750 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1752 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1753 2814baeb 2018-08-01 stsp wstandout(view->window);
1754 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1755 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1756 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1757 2814baeb 2018-08-01 stsp wstandend(view->window);
1759 60493ae3 2019-06-20 stsp goto done;
1760 0553a4e3 2018-04-30 stsp ncommits++;
1761 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1762 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1765 1a57306a 2018-09-02 stsp view_vborder(view);
1766 dd038bc6 2021-09-21 thomas.ad update_panels();
1767 dd038bc6 2021-09-21 thomas.ad doupdate();
1769 1a76625f 2018-10-22 stsp free(id_str);
1770 8b473291 2019-02-21 stsp free(refs_str);
1771 1a76625f 2018-10-22 stsp free(ncommits_str);
1772 1a76625f 2018-10-22 stsp free(header);
1773 80ddbec8 2018-04-29 stsp return err;
1776 07b55e75 2018-05-10 stsp static void
1777 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1779 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1780 07b55e75 2018-05-10 stsp int nscrolled = 0;
1782 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1783 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1786 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1787 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1788 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1789 07b55e75 2018-05-10 stsp if (entry) {
1790 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1791 07b55e75 2018-05-10 stsp nscrolled++;
1796 aa075928 2018-05-10 stsp static const struct got_error *
1797 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1799 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1800 5e224a3e 2019-02-22 stsp int errcode;
1802 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1804 fb280deb 2021-08-30 stsp while (ta->commits_needed > 0 || ta->load_all) {
1805 ffe38506 2020-12-01 naddy if (ta->log_complete)
1808 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1809 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1810 7aafa0d1 2019-02-22 stsp if (errcode)
1811 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1812 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1815 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1816 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1818 7c1452c1 2020-03-26 stsp if (!wait)
1821 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1822 ffe38506 2020-12-01 naddy show_log_view(view);
1823 7c1452c1 2020-03-26 stsp update_panels();
1824 7c1452c1 2020-03-26 stsp doupdate();
1826 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1827 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1828 82954512 2020-02-03 stsp if (errcode)
1829 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1830 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1832 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1833 ffe38506 2020-12-01 naddy show_log_view(view);
1834 7c1452c1 2020-03-26 stsp update_panels();
1835 7c1452c1 2020-03-26 stsp doupdate();
1838 5e224a3e 2019-02-22 stsp return NULL;
1841 5e224a3e 2019-02-22 stsp static const struct got_error *
1842 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1844 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1845 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1846 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1847 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1849 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1850 5e224a3e 2019-02-22 stsp return NULL;
1852 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1853 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1854 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1856 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1858 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1859 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1861 5e224a3e 2019-02-22 stsp return err;
1865 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1866 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1869 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1871 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1872 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1874 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1875 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1877 dd0a52c1 2018-05-20 stsp return err;
1880 cd0acaa7 2018-05-20 stsp static const struct got_error *
1881 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1882 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1883 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1885 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1886 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1887 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1889 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1890 15a94983 2018-12-23 stsp if (diff_view == NULL)
1891 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1893 dbdddfee 2021-06-23 naddy parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
1894 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
1895 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1896 e5a0f69f 2018-08-18 stsp if (err == NULL)
1897 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1898 cd0acaa7 2018-05-20 stsp return err;
1901 80ddbec8 2018-04-29 stsp static const struct got_error *
1902 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1903 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1905 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1907 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1908 941e9f74 2019-05-21 stsp if (parent == NULL)
1909 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1911 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1912 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1913 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1914 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1915 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1916 941e9f74 2019-05-21 stsp s->tree = subtree;
1917 941e9f74 2019-05-21 stsp s->selected = 0;
1918 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1919 941e9f74 2019-05-21 stsp return NULL;
1922 941e9f74 2019-05-21 stsp static const struct got_error *
1923 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1924 945f9229 2022-04-16 thomas struct got_commit_object *commit, const char *path)
1926 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1927 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1928 941e9f74 2019-05-21 stsp const char *p;
1929 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1931 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1933 941e9f74 2019-05-21 stsp while (*p) {
1934 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1935 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1936 56e0773d 2019-11-28 stsp char *te_name;
1938 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1941 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1942 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1943 941e9f74 2019-05-21 stsp if (slash == NULL)
1944 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1946 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1947 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1948 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1951 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1952 56e0773d 2019-11-28 stsp if (te == NULL) {
1953 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1954 56e0773d 2019-11-28 stsp free(te_name);
1957 56e0773d 2019-11-28 stsp free(te_name);
1958 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1960 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1961 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1963 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1964 941e9f74 2019-05-21 stsp if (slash)
1965 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1967 941e9f74 2019-05-21 stsp subpath = strdup(path);
1968 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1969 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1973 945f9229 2022-04-16 thomas err = got_object_id_by_path(&tree_id, s->repo, commit,
1978 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
1979 941e9f74 2019-05-21 stsp free(tree_id);
1983 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
1984 941e9f74 2019-05-21 stsp if (err) {
1985 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1988 941e9f74 2019-05-21 stsp if (slash == NULL)
1990 941e9f74 2019-05-21 stsp free(subpath);
1991 941e9f74 2019-05-21 stsp subpath = NULL;
1992 941e9f74 2019-05-21 stsp p = slash;
1995 941e9f74 2019-05-21 stsp free(subpath);
1996 1a76625f 2018-10-22 stsp return err;
1999 61266923 2020-01-14 stsp static const struct got_error *
2000 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
2001 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
2002 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
2004 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
2005 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
2006 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
2008 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2009 55cccc34 2020-02-20 stsp if (tree_view == NULL)
2010 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
2012 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2014 55cccc34 2020-02-20 stsp return err;
2015 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
2017 55cccc34 2020-02-20 stsp *new_view = tree_view;
2019 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
2020 55cccc34 2020-02-20 stsp return NULL;
2022 945f9229 2022-04-16 thomas return tree_view_walk_path(s, entry->commit, path);
2025 55cccc34 2020-02-20 stsp static const struct got_error *
2026 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
2028 61266923 2020-01-14 stsp sigset_t sigset;
2029 61266923 2020-01-14 stsp int errcode;
2031 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
2032 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
2034 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
2035 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
2036 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2037 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
2038 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2040 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
2041 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
2042 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
2044 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2045 61266923 2020-01-14 stsp if (errcode)
2046 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
2048 61266923 2020-01-14 stsp return NULL;
2051 1a76625f 2018-10-22 stsp static void *
2052 1a76625f 2018-10-22 stsp log_thread(void *arg)
2054 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2055 1a76625f 2018-10-22 stsp int errcode = 0;
2056 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
2057 1a76625f 2018-10-22 stsp int done = 0;
2059 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2061 61266923 2020-01-14 stsp return (void *)err;
2063 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
2064 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2065 1a76625f 2018-10-22 stsp if (err) {
2066 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2067 1a76625f 2018-10-22 stsp return (void *)err;
2068 1a76625f 2018-10-22 stsp err = NULL;
2070 fb280deb 2021-08-30 stsp } else if (a->commits_needed > 0 && !a->load_all)
2071 1a76625f 2018-10-22 stsp a->commits_needed--;
2073 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2074 3abe8080 2019-04-10 stsp if (errcode) {
2075 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2076 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2078 3abe8080 2019-04-10 stsp } else if (*a->quit)
2080 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2081 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2082 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2083 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2086 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2087 7c1452c1 2020-03-26 stsp if (errcode) {
2088 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2089 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2090 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2095 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2097 fb280deb 2021-08-30 stsp if (a->commits_needed == 0 && !a->load_all) {
2098 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2099 7c1452c1 2020-03-26 stsp &tog_mutex);
2100 7c1452c1 2020-03-26 stsp if (errcode)
2101 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2102 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2103 21355643 2020-12-06 stsp if (*a->quit)
2108 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2109 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2110 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2111 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2113 3abe8080 2019-04-10 stsp a->log_complete = 1;
2114 1a76625f 2018-10-22 stsp return (void *)err;
2117 1a76625f 2018-10-22 stsp static const struct got_error *
2118 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2120 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2121 1a76625f 2018-10-22 stsp int errcode;
2123 1a76625f 2018-10-22 stsp if (s->thread) {
2124 1a76625f 2018-10-22 stsp s->quit = 1;
2125 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2126 1a76625f 2018-10-22 stsp if (errcode)
2127 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2128 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2129 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2130 1a76625f 2018-10-22 stsp if (errcode)
2131 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2132 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2133 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2134 1a76625f 2018-10-22 stsp if (errcode)
2135 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2136 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2137 1a76625f 2018-10-22 stsp if (errcode)
2138 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2139 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2140 dd038bc6 2021-09-21 thomas.ad s->thread = 0; //NULL;
2143 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2144 1d0f4054 2021-06-17 stsp err = got_repo_close(s->thread_args.repo);
2145 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2148 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2149 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2150 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2153 9343a5fb 2018-06-23 stsp return err;
2156 9343a5fb 2018-06-23 stsp static const struct got_error *
2157 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2159 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2160 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2161 276b94a1 2020-11-13 naddy int errcode;
2163 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2165 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2166 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2167 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2169 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2170 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2171 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2173 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2174 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2175 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2176 1a76625f 2018-10-22 stsp free(s->start_id);
2177 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2178 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2179 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2180 1a76625f 2018-10-22 stsp return err;
2183 1a76625f 2018-10-22 stsp static const struct got_error *
2184 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2186 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2188 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2189 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2190 60493ae3 2019-06-20 stsp return NULL;
2193 60493ae3 2019-06-20 stsp static const struct got_error *
2194 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2196 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2197 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2198 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2200 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2201 f9686aa5 2020-03-27 stsp show_log_view(view);
2202 f9686aa5 2020-03-27 stsp update_panels();
2203 f9686aa5 2020-03-27 stsp doupdate();
2205 96e2b566 2019-07-08 stsp if (s->search_entry) {
2206 13add988 2019-10-15 stsp int errcode, ch;
2207 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2208 13add988 2019-10-15 stsp if (errcode)
2209 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2210 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2211 13add988 2019-10-15 stsp ch = wgetch(view->window);
2212 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2213 13add988 2019-10-15 stsp if (errcode)
2214 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2215 13add988 2019-10-15 stsp "pthread_mutex_lock");
2216 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2217 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2218 678cbce5 2019-07-28 stsp return NULL;
2220 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2221 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2223 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2224 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2225 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2226 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2227 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2229 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2230 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2232 de0d3ad4 2021-12-10 thomas entry = s->selected_entry;
2235 60493ae3 2019-06-20 stsp while (1) {
2236 13add988 2019-10-15 stsp int have_match = 0;
2238 60493ae3 2019-06-20 stsp if (entry == NULL) {
2239 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2240 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2241 f9967bca 2020-03-27 stsp view->search_next_done =
2242 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2243 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2244 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2245 f801134a 2019-06-25 stsp return NULL;
2248 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2249 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2250 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2252 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2253 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2256 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2257 13add988 2019-10-15 stsp &view->regex);
2260 13add988 2019-10-15 stsp if (have_match) {
2261 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2262 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2266 96e2b566 2019-07-08 stsp s->search_entry = entry;
2267 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2268 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2270 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2273 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2274 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2275 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2276 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2278 60493ae3 2019-06-20 stsp return err;
2281 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2282 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2284 60493ae3 2019-06-20 stsp return err;
2289 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2291 60493ae3 2019-06-20 stsp return NULL;
2294 60493ae3 2019-06-20 stsp static const struct got_error *
2295 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2296 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2297 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2299 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2300 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2301 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2302 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2303 1a76625f 2018-10-22 stsp int errcode;
2305 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2306 f135c941 2020-02-20 stsp free(s->in_repo_path);
2307 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2308 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2309 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2312 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2313 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2314 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2316 fb2756b9 2018-08-04 stsp s->repo = repo;
2317 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2318 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2319 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2320 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2321 9cd7cbd1 2020-12-07 stsp goto done;
2324 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2325 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2326 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2327 5036bf37 2018-09-24 stsp goto done;
2329 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2331 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
2332 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2333 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2334 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2336 11b20872 2019-11-08 stsp goto done;
2337 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2338 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2339 11b20872 2019-11-08 stsp if (err) {
2340 11b20872 2019-11-08 stsp free_colors(&s->colors);
2341 11b20872 2019-11-08 stsp goto done;
2343 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2344 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2345 11b20872 2019-11-08 stsp if (err) {
2346 11b20872 2019-11-08 stsp free_colors(&s->colors);
2347 11b20872 2019-11-08 stsp goto done;
2351 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2352 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2353 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2354 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2355 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2357 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2359 1a76625f 2018-10-22 stsp goto done;
2360 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2361 b672a97a 2020-01-27 stsp !s->log_branches);
2363 1a76625f 2018-10-22 stsp goto done;
2364 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2365 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2367 c5b78334 2020-01-12 stsp goto done;
2369 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2370 1a76625f 2018-10-22 stsp if (errcode) {
2371 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2372 1a76625f 2018-10-22 stsp goto done;
2374 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2375 7c1452c1 2020-03-26 stsp if (errcode) {
2376 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2377 7c1452c1 2020-03-26 stsp goto done;
2380 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2381 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2382 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2383 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2384 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2385 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2386 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2387 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2388 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2389 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2390 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2391 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2392 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2395 1a76625f 2018-10-22 stsp close_log_view(view);
2396 ba4f502b 2018-08-04 stsp return err;
2399 e5a0f69f 2018-08-18 stsp static const struct got_error *
2400 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2402 f2f6d207 2020-11-24 stsp const struct got_error *err;
2403 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2405 dd038bc6 2021-09-21 thomas.ad if (s->thread == 0) { //NULL) {
2406 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2407 2b380cc8 2018-10-24 stsp &s->thread_args);
2408 2b380cc8 2018-10-24 stsp if (errcode)
2409 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2410 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2411 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2413 f2f6d207 2020-11-24 stsp return err;
2417 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2420 e5a0f69f 2018-08-18 stsp static const struct got_error *
2421 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2423 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2424 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2425 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2426 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2427 f3bc9f1d 2021-09-05 naddy struct commit_queue_entry *entry;
2428 f3bc9f1d 2021-09-05 naddy int begin_x = 0, n;
2430 528dedf3 2021-08-30 stsp if (s->thread_args.load_all) {
2431 528dedf3 2021-08-30 stsp if (ch == KEY_BACKSPACE)
2432 fb280deb 2021-08-30 stsp s->thread_args.load_all = 0;
2433 528dedf3 2021-08-30 stsp else if (s->thread_args.log_complete) {
2434 528dedf3 2021-08-30 stsp s->thread_args.load_all = 0;
2435 fb280deb 2021-08-30 stsp log_scroll_down(view, s->commits.ncommits);
2436 fb280deb 2021-08-30 stsp s->selected = MIN(view->nlines - 2,
2437 fb280deb 2021-08-30 stsp s->commits.ncommits - 1);
2438 fb280deb 2021-08-30 stsp select_commit(s);
2440 528dedf3 2021-08-30 stsp return NULL;
2443 528dedf3 2021-08-30 stsp switch (ch) {
2445 1e37a5c2 2019-05-12 jcs s->quit = 1;
2448 1e37a5c2 2019-05-12 jcs case KEY_UP:
2451 f7140bf5 2021-10-17 thomas case CTRL('p'):
2452 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2454 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2455 1e37a5c2 2019-05-12 jcs s->selected--;
2457 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2458 912a3f79 2021-08-30 j select_commit(s);
2461 912a3f79 2021-08-30 j case KEY_HOME:
2462 912a3f79 2021-08-30 j s->selected = 0;
2463 ea66598a 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2464 2b779855 2020-12-05 naddy select_commit(s);
2466 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2467 a4292ac5 2019-05-12 jcs case CTRL('b'):
2468 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2470 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2471 1e37a5c2 2019-05-12 jcs s->selected = 0;
2473 2b779855 2020-12-05 naddy log_scroll_up(s, view->nlines - 1);
2474 2b779855 2020-12-05 naddy select_commit(s);
2477 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2480 f7140bf5 2021-10-17 thomas case CTRL('n'):
2481 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2483 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2484 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2485 1e37a5c2 2019-05-12 jcs s->selected++;
2487 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2491 912a3f79 2021-08-30 j select_commit(s);
2494 912a3f79 2021-08-30 j case KEY_END: {
2495 912a3f79 2021-08-30 j /* We don't know yet how many commits, so we're forced to
2496 912a3f79 2021-08-30 j * traverse them all. */
2497 fb280deb 2021-08-30 stsp if (!s->thread_args.log_complete) {
2498 fb280deb 2021-08-30 stsp s->thread_args.load_all = 1;
2499 fb280deb 2021-08-30 stsp return trigger_log_thread(view, 0);
2502 f3bc9f1d 2021-09-05 naddy s->selected = 0;
2503 f3bc9f1d 2021-09-05 naddy entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2504 f3bc9f1d 2021-09-05 naddy for (n = 0; n < view->nlines - 1; n++) {
2505 f3bc9f1d 2021-09-05 naddy if (entry == NULL)
2507 f3bc9f1d 2021-09-05 naddy s->first_displayed_entry = entry;
2508 f3bc9f1d 2021-09-05 naddy entry = TAILQ_PREV(entry, commit_queue_head, entry);
2510 f3bc9f1d 2021-09-05 naddy if (n > 0)
2511 f3bc9f1d 2021-09-05 naddy s->selected = n - 1;
2512 2b779855 2020-12-05 naddy select_commit(s);
2515 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2516 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2517 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2518 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2519 1e37a5c2 2019-05-12 jcs if (first == NULL)
2521 ffe38506 2020-12-01 naddy err = log_scroll_down(view, view->nlines - 1);
2524 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2525 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2526 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2527 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2528 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2529 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2531 2b779855 2020-12-05 naddy select_commit(s);
2534 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2535 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2536 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2537 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2538 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2539 2b779855 2020-12-05 naddy select_commit(s);
2540 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2541 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2542 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2543 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2544 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2547 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2550 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2552 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2553 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2554 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2555 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2556 78756c87 2020-11-24 stsp view, s->repo);
2559 e78dc838 2020-12-04 stsp view->focussed = 0;
2560 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2561 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2562 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2564 1e37a5c2 2019-05-12 jcs return err;
2565 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2566 e78dc838 2020-12-04 stsp view->focus_child = 1;
2568 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2571 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2573 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2574 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2575 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2576 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2580 e78dc838 2020-12-04 stsp view->focussed = 0;
2581 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2582 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2583 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2585 1e37a5c2 2019-05-12 jcs return err;
2586 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2587 e78dc838 2020-12-04 stsp view->focus_child = 1;
2589 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2591 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2592 21355643 2020-12-06 stsp case CTRL('l'):
2594 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2595 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2597 21355643 2020-12-06 stsp err = stop_log_thread(s);
2599 74cfe85e 2020-10-20 stsp return err;
2600 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2601 21355643 2020-12-06 stsp char *parent_path;
2602 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2604 21355643 2020-12-06 stsp return err;
2605 21355643 2020-12-06 stsp free(s->in_repo_path);
2606 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2607 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2608 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2609 21355643 2020-12-06 stsp struct got_object_id *start_id;
2610 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2611 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2612 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2614 21355643 2020-12-06 stsp return err;
2615 21355643 2020-12-06 stsp free(s->start_id);
2616 21355643 2020-12-06 stsp s->start_id = start_id;
2617 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2618 21355643 2020-12-06 stsp } else /* 'B' */
2619 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2621 21355643 2020-12-06 stsp err = got_repo_open(&s->thread_args.repo,
2622 21355643 2020-12-06 stsp got_repo_get_path(s->repo), NULL);
2624 21355643 2020-12-06 stsp return err;
2625 51a10b52 2020-12-26 stsp tog_free_refs();
2626 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, 0);
2628 ca51c541 2020-12-07 stsp return err;
2629 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2630 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2632 d01904d4 2019-06-25 stsp return err;
2633 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2634 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2636 b672a97a 2020-01-27 stsp return err;
2637 21355643 2020-12-06 stsp free_commits(&s->commits);
2638 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2639 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2640 21355643 2020-12-06 stsp s->selected_entry = NULL;
2641 21355643 2020-12-06 stsp s->selected = 0;
2642 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2643 21355643 2020-12-06 stsp s->quit = 0;
2644 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2647 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2648 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2649 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2650 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2651 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2652 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2653 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2654 6458efa5 2020-11-24 stsp if (err) {
2655 6458efa5 2020-11-24 stsp view_close(ref_view);
2656 6458efa5 2020-11-24 stsp return err;
2658 e78dc838 2020-12-04 stsp view->focussed = 0;
2659 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2660 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2661 6458efa5 2020-11-24 stsp err = view_close_child(view);
2663 6458efa5 2020-11-24 stsp return err;
2664 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2665 e78dc838 2020-12-04 stsp view->focus_child = 1;
2667 6458efa5 2020-11-24 stsp *new_view = ref_view;
2673 80ddbec8 2018-04-29 stsp return err;
2676 4ed7e80c 2018-05-20 stsp static const struct got_error *
2677 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2679 c2db6724 2019-01-04 stsp const struct got_error *error;
2681 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2682 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2683 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2685 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2686 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2688 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2689 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2691 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2692 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2694 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2695 c2db6724 2019-01-04 stsp if (error != NULL)
2696 c2db6724 2019-01-04 stsp return error;
2698 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2699 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2701 c2db6724 2019-01-04 stsp return NULL;
2704 a915003a 2019-02-05 stsp static void
2705 a915003a 2019-02-05 stsp init_curses(void)
2707 a915003a 2019-02-05 stsp initscr();
2709 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2712 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2713 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2714 a915003a 2019-02-05 stsp curs_set(0);
2715 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2716 6d17833f 2019-11-08 stsp start_color();
2717 6d17833f 2019-11-08 stsp use_default_colors();
2719 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2720 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2721 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2724 c2db6724 2019-01-04 stsp static const struct got_error *
2725 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2726 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2728 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2730 f135c941 2020-02-20 stsp if (argc == 0) {
2731 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2732 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2733 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2734 f135c941 2020-02-20 stsp return NULL;
2737 f135c941 2020-02-20 stsp if (worktree) {
2738 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2741 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2743 f135c941 2020-02-20 stsp return err;
2744 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2745 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2746 bfd61697 2020-11-14 stsp p) == -1) {
2747 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2748 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2752 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2754 f135c941 2020-02-20 stsp return err;
2757 f135c941 2020-02-20 stsp static const struct got_error *
2758 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2760 80ddbec8 2018-04-29 stsp const struct got_error *error;
2761 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2762 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2763 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2764 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2765 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2766 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2767 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2768 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2769 04cc582a 2018-08-01 stsp struct tog_view *view;
2771 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2772 80ddbec8 2018-04-29 stsp switch (ch) {
2774 b672a97a 2020-01-27 stsp log_branches = 1;
2777 80ddbec8 2018-04-29 stsp start_commit = optarg;
2780 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2781 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2782 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2786 17020d27 2019-03-07 stsp usage_log();
2787 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2791 80ddbec8 2018-04-29 stsp argc -= optind;
2792 80ddbec8 2018-04-29 stsp argv += optind;
2794 f135c941 2020-02-20 stsp if (argc > 1)
2795 f135c941 2020-02-20 stsp usage_log();
2797 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2798 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
2799 c156c7a4 2020-12-18 stsp if (cwd == NULL)
2800 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
2801 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
2802 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2803 c156c7a4 2020-12-18 stsp goto done;
2804 a1fbf39a 2019-08-11 stsp if (worktree)
2805 6962eb72 2020-02-20 stsp repo_path =
2806 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2808 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2809 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
2810 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
2811 c156c7a4 2020-12-18 stsp goto done;
2815 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2816 80ddbec8 2018-04-29 stsp if (error != NULL)
2817 ecb28ae0 2018-07-16 stsp goto done;
2819 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2820 f135c941 2020-02-20 stsp repo, worktree);
2821 f135c941 2020-02-20 stsp if (error)
2822 f135c941 2020-02-20 stsp goto done;
2824 f135c941 2020-02-20 stsp init_curses();
2826 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2827 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2828 c02c541e 2019-03-29 stsp if (error)
2829 c02c541e 2019-03-29 stsp goto done;
2831 87670572 2020-12-26 naddy /* already loaded by tog_log_with_path()? */
2832 d9dff0e5 2020-12-26 stsp if (TAILQ_EMPTY(&tog_refs)) {
2833 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
2834 87670572 2020-12-26 naddy if (error)
2835 87670572 2020-12-26 naddy goto done;
2838 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2839 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2840 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2841 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2842 d8f38dc4 2020-12-05 stsp if (error)
2843 d8f38dc4 2020-12-05 stsp goto done;
2844 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2846 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2847 d8f38dc4 2020-12-05 stsp if (error == NULL)
2848 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2849 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2850 d8f38dc4 2020-12-05 stsp goto done;
2851 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2852 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
2853 d8f38dc4 2020-12-05 stsp if (error)
2854 d8f38dc4 2020-12-05 stsp goto done;
2857 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2858 04cc582a 2018-08-01 stsp if (view == NULL) {
2859 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2860 04cc582a 2018-08-01 stsp goto done;
2862 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2863 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2864 ba4f502b 2018-08-04 stsp if (error)
2865 ba4f502b 2018-08-04 stsp goto done;
2866 2fc00ff4 2019-08-31 stsp if (worktree) {
2867 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2868 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2869 2fc00ff4 2019-08-31 stsp worktree = NULL;
2871 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2873 f135c941 2020-02-20 stsp free(in_repo_path);
2874 ecb28ae0 2018-07-16 stsp free(repo_path);
2875 ecb28ae0 2018-07-16 stsp free(cwd);
2876 899d86c2 2018-05-10 stsp free(start_id);
2877 d8f38dc4 2020-12-05 stsp free(label);
2879 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2880 1d0f4054 2021-06-17 stsp if (repo) {
2881 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2882 1d0f4054 2021-06-17 stsp if (error == NULL)
2883 1d0f4054 2021-06-17 stsp error = close_err;
2885 ec142235 2019-03-07 stsp if (worktree)
2886 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2887 51a10b52 2020-12-26 stsp tog_free_refs();
2888 80ddbec8 2018-04-29 stsp return error;
2891 4ed7e80c 2018-05-20 stsp __dead static void
2892 9f7d7167 2018-04-29 stsp usage_diff(void)
2895 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2896 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2900 6d17833f 2019-11-08 stsp static int
2901 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2902 41605754 2020-11-12 stsp regmatch_t *regmatch)
2904 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2907 f26dddb7 2019-11-08 stsp struct tog_color *
2908 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2910 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2912 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(tc, colors, entry) {
2913 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2914 6d17833f 2019-11-08 stsp return tc;
2917 6d17833f 2019-11-08 stsp return NULL;
2920 4ed7e80c 2018-05-20 stsp static const struct got_error *
2921 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2922 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2924 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2925 41605754 2020-11-12 stsp wchar_t *wline;
2926 41605754 2020-11-12 stsp int width;
2929 41605754 2020-11-12 stsp *wtotal = 0;
2931 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2932 41605754 2020-11-12 stsp if (s == NULL)
2933 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2935 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2936 41605754 2020-11-12 stsp if (err) {
2938 41605754 2020-11-12 stsp return err;
2940 41605754 2020-11-12 stsp waddwstr(window, wline);
2941 41605754 2020-11-12 stsp free(wline);
2943 41605754 2020-11-12 stsp wlimit -= width;
2944 41605754 2020-11-12 stsp *wtotal += width;
2946 41605754 2020-11-12 stsp if (wlimit > 0) {
2947 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2948 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2949 41605754 2020-11-12 stsp if (s == NULL) {
2950 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2952 41605754 2020-11-12 stsp return err;
2954 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2955 41605754 2020-11-12 stsp if (err) {
2957 41605754 2020-11-12 stsp return err;
2959 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2960 41605754 2020-11-12 stsp waddwstr(window, wline);
2961 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2962 41605754 2020-11-12 stsp free(wline);
2964 41605754 2020-11-12 stsp wlimit -= width;
2965 41605754 2020-11-12 stsp *wtotal += width;
2968 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2969 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2970 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
2972 41605754 2020-11-12 stsp return err;
2973 41605754 2020-11-12 stsp waddwstr(window, wline);
2974 41605754 2020-11-12 stsp free(wline);
2975 41605754 2020-11-12 stsp *wtotal += width;
2978 41605754 2020-11-12 stsp return NULL;
2981 41605754 2020-11-12 stsp static const struct got_error *
2982 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
2984 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
2985 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
2986 61e69b96 2018-05-20 stsp const struct got_error *err;
2987 fe621944 2020-11-10 stsp int nprinted = 0;
2988 b304db33 2018-05-20 stsp char *line;
2989 826082fe 2020-12-10 stsp size_t linesize = 0;
2990 826082fe 2020-12-10 stsp ssize_t linelen;
2991 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2992 61e69b96 2018-05-20 stsp wchar_t *wline;
2993 e0b650dd 2018-05-20 stsp int width;
2994 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
2995 89f1a395 2020-12-01 naddy int nlines = s->nlines;
2996 fe621944 2020-11-10 stsp off_t line_offset;
2998 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
2999 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3000 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
3002 f7d12f7e 2018-08-01 stsp werase(view->window);
3004 a3404814 2018-09-02 stsp if (header) {
3005 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
3006 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
3007 135a2da0 2020-11-11 stsp header) == -1)
3008 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
3009 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3010 135a2da0 2020-11-11 stsp free(line);
3012 a3404814 2018-09-02 stsp return err;
3014 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3015 a3404814 2018-09-02 stsp wstandout(view->window);
3016 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
3017 e54cc94a 2020-11-11 stsp free(wline);
3018 e54cc94a 2020-11-11 stsp wline = NULL;
3019 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3020 a3404814 2018-09-02 stsp wstandend(view->window);
3021 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3022 a3404814 2018-09-02 stsp waddch(view->window, '\n');
3024 a3404814 2018-09-02 stsp if (max_lines <= 1)
3025 a3404814 2018-09-02 stsp return NULL;
3026 a3404814 2018-09-02 stsp max_lines--;
3029 89f1a395 2020-12-01 naddy s->eof = 0;
3030 826082fe 2020-12-10 stsp line = NULL;
3031 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
3032 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3033 826082fe 2020-12-10 stsp if (linelen == -1) {
3034 826082fe 2020-12-10 stsp if (feof(s->f)) {
3035 826082fe 2020-12-10 stsp s->eof = 1;
3038 826082fe 2020-12-10 stsp free(line);
3039 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
3042 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
3044 f26dddb7 2019-11-08 stsp wattr_on(view->window,
3045 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3046 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
3047 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3048 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
3049 41605754 2020-11-12 stsp view->window, regmatch);
3050 41605754 2020-11-12 stsp if (err) {
3051 41605754 2020-11-12 stsp free(line);
3052 41605754 2020-11-12 stsp return err;
3055 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3056 41605754 2020-11-12 stsp if (err) {
3057 41605754 2020-11-12 stsp free(line);
3058 41605754 2020-11-12 stsp return err;
3060 41605754 2020-11-12 stsp waddwstr(view->window, wline);
3061 41605754 2020-11-12 stsp free(wline);
3062 41605754 2020-11-12 stsp wline = NULL;
3065 6d17833f 2019-11-08 stsp wattr_off(view->window,
3066 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3067 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
3068 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3069 fe621944 2020-11-10 stsp nprinted++;
3071 826082fe 2020-12-10 stsp free(line);
3072 fe621944 2020-11-10 stsp if (nprinted >= 1)
3073 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
3074 89f1a395 2020-12-01 naddy (nprinted - 1);
3076 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
3078 1a57306a 2018-09-02 stsp view_vborder(view);
3080 89f1a395 2020-12-01 naddy if (s->eof) {
3081 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
3082 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
3083 c3e9aa98 2019-05-13 jcs nprinted++;
3086 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
3088 c3e9aa98 2019-05-13 jcs return err;
3091 c3e9aa98 2019-05-13 jcs wstandout(view->window);
3092 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
3093 e54cc94a 2020-11-11 stsp free(wline);
3094 e54cc94a 2020-11-11 stsp wline = NULL;
3095 c3e9aa98 2019-05-13 jcs wstandend(view->window);
3098 26ed57b2 2018-05-19 stsp return NULL;
3101 abd2672a 2018-12-23 stsp static char *
3102 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3104 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3105 09867e48 2019-08-13 stsp char *p, *s;
3107 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3108 09867e48 2019-08-13 stsp if (tm == NULL)
3109 09867e48 2019-08-13 stsp return NULL;
3110 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3111 09867e48 2019-08-13 stsp if (s == NULL)
3112 09867e48 2019-08-13 stsp return NULL;
3113 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3115 abd2672a 2018-12-23 stsp *p = '\0';
3119 4ed7e80c 2018-05-20 stsp static const struct got_error *
3120 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3121 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3123 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3124 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3125 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3126 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3128 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3129 0208f208 2020-05-05 stsp if (qid != NULL) {
3130 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3131 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3132 ec242592 2022-04-22 thomas &qid->id);
3134 0208f208 2020-05-05 stsp return err;
3136 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3137 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3138 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3139 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3140 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3142 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3146 0208f208 2020-05-05 stsp if (tree_id1) {
3147 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3149 0208f208 2020-05-05 stsp goto done;
3152 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3153 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3155 0208f208 2020-05-05 stsp goto done;
3157 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3158 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3160 0208f208 2020-05-05 stsp if (tree1)
3161 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3162 0208f208 2020-05-05 stsp if (tree2)
3163 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3164 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3165 0208f208 2020-05-05 stsp return err;
3168 0208f208 2020-05-05 stsp static const struct got_error *
3169 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3173 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3174 fe621944 2020-11-10 stsp if (p == NULL)
3175 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3176 fe621944 2020-11-10 stsp *line_offsets = p;
3177 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3178 fe621944 2020-11-10 stsp (*nlines)++;
3179 fe621944 2020-11-10 stsp return NULL;
3182 fe621944 2020-11-10 stsp static const struct got_error *
3183 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3184 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3185 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3187 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3188 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3189 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3190 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3191 45d799e2 2018-12-23 stsp time_t committer_time;
3192 45d799e2 2018-12-23 stsp const char *author, *committer;
3193 8b473291 2019-02-21 stsp char *refs_str = NULL;
3194 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3195 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3196 fe621944 2020-11-10 stsp off_t outoff = 0;
3199 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3201 8b473291 2019-02-21 stsp if (refs) {
3202 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3204 8b473291 2019-02-21 stsp return err;
3207 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3209 abd2672a 2018-12-23 stsp return err;
3211 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3212 15a94983 2018-12-23 stsp if (err) {
3213 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3214 15a94983 2018-12-23 stsp goto done;
3217 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3219 fe621944 2020-11-10 stsp goto done;
3221 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3222 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3223 fe621944 2020-11-10 stsp if (n < 0) {
3224 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3225 abd2672a 2018-12-23 stsp goto done;
3227 fe621944 2020-11-10 stsp outoff += n;
3228 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3230 fe621944 2020-11-10 stsp goto done;
3232 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3233 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3234 fe621944 2020-11-10 stsp if (n < 0) {
3235 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3236 abd2672a 2018-12-23 stsp goto done;
3238 fe621944 2020-11-10 stsp outoff += n;
3239 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3241 fe621944 2020-11-10 stsp goto done;
3243 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3244 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3245 fe621944 2020-11-10 stsp if (datestr) {
3246 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3247 fe621944 2020-11-10 stsp if (n < 0) {
3248 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3249 fe621944 2020-11-10 stsp goto done;
3251 fe621944 2020-11-10 stsp outoff += n;
3252 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3254 fe621944 2020-11-10 stsp goto done;
3256 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3257 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3258 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3259 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3260 fe621944 2020-11-10 stsp if (n < 0) {
3261 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3262 fe621944 2020-11-10 stsp goto done;
3264 fe621944 2020-11-10 stsp outoff += n;
3265 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3267 fe621944 2020-11-10 stsp goto done;
3269 ac4dc263 2021-09-24 thomas if (got_object_commit_get_nparents(commit) > 1) {
3270 ac4dc263 2021-09-24 thomas const struct got_object_id_queue *parent_ids;
3271 ac4dc263 2021-09-24 thomas struct got_object_qid *qid;
3272 ac4dc263 2021-09-24 thomas int pn = 1;
3273 ac4dc263 2021-09-24 thomas parent_ids = got_object_commit_get_parent_ids(commit);
3274 ac4dc263 2021-09-24 thomas STAILQ_FOREACH(qid, parent_ids, entry) {
3275 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &qid->id);
3276 ac4dc263 2021-09-24 thomas if (err)
3277 ac4dc263 2021-09-24 thomas goto done;
3278 ac4dc263 2021-09-24 thomas n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3279 ac4dc263 2021-09-24 thomas if (n < 0) {
3280 ac4dc263 2021-09-24 thomas err = got_error_from_errno("fprintf");
3281 ac4dc263 2021-09-24 thomas goto done;
3283 ac4dc263 2021-09-24 thomas outoff += n;
3284 ac4dc263 2021-09-24 thomas err = add_line_offset(line_offsets, nlines, outoff);
3285 ac4dc263 2021-09-24 thomas if (err)
3286 ac4dc263 2021-09-24 thomas goto done;
3287 ac4dc263 2021-09-24 thomas free(id_str);
3288 ac4dc263 2021-09-24 thomas id_str = NULL;
3292 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3294 5943eee2 2019-08-13 stsp goto done;
3295 fe621944 2020-11-10 stsp s = logmsg;
3296 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3297 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3298 fe621944 2020-11-10 stsp if (n < 0) {
3299 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3300 fe621944 2020-11-10 stsp goto done;
3302 fe621944 2020-11-10 stsp outoff += n;
3303 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3305 fe621944 2020-11-10 stsp goto done;
3308 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3310 0208f208 2020-05-05 stsp goto done;
3311 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3312 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3313 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3314 fe621944 2020-11-10 stsp if (n < 0) {
3315 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3316 fe621944 2020-11-10 stsp goto done;
3318 fe621944 2020-11-10 stsp outoff += n;
3319 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3321 fe621944 2020-11-10 stsp goto done;
3322 0208f208 2020-05-05 stsp free((char *)pe->path);
3323 0208f208 2020-05-05 stsp free(pe->data);
3326 0208f208 2020-05-05 stsp fputc('\n', outfile);
3328 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3330 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3331 abd2672a 2018-12-23 stsp free(id_str);
3332 5943eee2 2019-08-13 stsp free(logmsg);
3333 8b473291 2019-02-21 stsp free(refs_str);
3334 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3335 7510f233 2020-08-09 stsp if (err) {
3336 ae6a6978 2020-08-09 stsp free(*line_offsets);
3337 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3338 ae6a6978 2020-08-09 stsp *nlines = 0;
3340 fe621944 2020-11-10 stsp return err;
3343 abd2672a 2018-12-23 stsp static const struct got_error *
3344 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3346 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3347 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3348 15a94983 2018-12-23 stsp int obj_type;
3350 fe621944 2020-11-10 stsp free(s->line_offsets);
3351 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3352 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3353 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3354 fe621944 2020-11-10 stsp s->nlines = 0;
3356 511a516b 2018-05-19 stsp f = got_opentemp();
3357 48ae06ee 2018-10-18 stsp if (f == NULL) {
3358 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3359 48ae06ee 2018-10-18 stsp goto done;
3361 56b63ca4 2021-01-22 stsp if (s->f && fclose(s->f) == EOF) {
3362 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3363 fb43ecf1 2019-02-11 stsp goto done;
3367 15a94983 2018-12-23 stsp if (s->id1)
3368 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3370 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3372 15a94983 2018-12-23 stsp goto done;
3374 15a94983 2018-12-23 stsp switch (obj_type) {
3375 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3376 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3377 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->label1, s->label2, s->diff_context,
3378 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3380 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3381 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3382 cc8021af 2021-10-12 thomas s->id1, s->id2, NULL, "", "", s->diff_context,
3383 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3385 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3386 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3387 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3388 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3389 d2075bf3 2020-12-25 stsp struct got_reflist_head *refs;
3391 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3393 3ffacbe1 2020-02-02 tracey goto done;
3394 51a10b52 2020-12-26 stsp refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3395 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3396 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3397 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3398 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3399 f44b1f58 2020-02-02 tracey if (err)
3400 f44b1f58 2020-02-02 tracey goto done;
3401 f44b1f58 2020-02-02 tracey } else {
3402 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3403 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
3404 ec242592 2022-04-22 thomas if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3405 fe621944 2020-11-10 stsp err = write_commit_info(
3406 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3407 d2075bf3 2020-12-25 stsp s->id2, refs, s->repo, s->f);
3408 f44b1f58 2020-02-02 tracey if (err)
3409 f44b1f58 2020-02-02 tracey goto done;
3414 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3416 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3417 cc8021af 2021-10-12 thomas s->id1, s->id2, NULL, s->diff_context, s->ignore_whitespace,
3418 3dbaef42 2020-11-24 stsp s->force_text_diff, s->repo, s->f);
3422 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3425 f44b1f58 2020-02-02 tracey if (err)
3426 f44b1f58 2020-02-02 tracey goto done;
3428 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3429 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3430 48ae06ee 2018-10-18 stsp return err;
3433 f5215bb9 2019-02-22 stsp static void
3434 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3436 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3437 f5215bb9 2019-02-22 stsp update_panels();
3438 f5215bb9 2019-02-22 stsp doupdate();
3441 f44b1f58 2020-02-02 tracey static const struct got_error *
3442 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3444 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3446 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3447 f44b1f58 2020-02-02 tracey return NULL;
3450 f44b1f58 2020-02-02 tracey static const struct got_error *
3451 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3453 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3454 f44b1f58 2020-02-02 tracey int lineno;
3455 826082fe 2020-12-10 stsp char *line = NULL;
3456 826082fe 2020-12-10 stsp size_t linesize = 0;
3457 826082fe 2020-12-10 stsp ssize_t linelen;
3459 f44b1f58 2020-02-02 tracey if (!view->searching) {
3460 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3461 f44b1f58 2020-02-02 tracey return NULL;
3464 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3465 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3466 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3468 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3470 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line;
3472 f44b1f58 2020-02-02 tracey while (1) {
3473 f44b1f58 2020-02-02 tracey off_t offset;
3475 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3476 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3477 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3481 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3482 f44b1f58 2020-02-02 tracey lineno = 1;
3484 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3487 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3488 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3489 f44b1f58 2020-02-02 tracey free(line);
3490 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3492 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3493 826082fe 2020-12-10 stsp if (linelen != -1 &&
3494 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3495 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3496 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3499 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3500 f44b1f58 2020-02-02 tracey lineno++;
3502 f44b1f58 2020-02-02 tracey lineno--;
3504 826082fe 2020-12-10 stsp free(line);
3506 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3507 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3508 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3511 f44b1f58 2020-02-02 tracey return NULL;
3514 48ae06ee 2018-10-18 stsp static const struct got_error *
3515 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3516 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3517 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3518 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3520 48ae06ee 2018-10-18 stsp const struct got_error *err;
3521 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3523 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3524 15a94983 2018-12-23 stsp int type1, type2;
3525 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3527 15a94983 2018-12-23 stsp return err;
3528 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3530 15a94983 2018-12-23 stsp return err;
3532 15a94983 2018-12-23 stsp if (type1 != type2)
3533 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3535 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3536 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3537 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3538 f44b1f58 2020-02-02 tracey s->repo = repo;
3539 f44b1f58 2020-02-02 tracey s->id1 = id1;
3540 f44b1f58 2020-02-02 tracey s->id2 = id2;
3541 3dbaef42 2020-11-24 stsp s->label1 = label1;
3542 3dbaef42 2020-11-24 stsp s->label2 = label2;
3544 15a94983 2018-12-23 stsp if (id1) {
3545 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3546 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3547 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3549 5465d566 2020-02-01 tracey s->id1 = NULL;
3551 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3552 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3553 5465d566 2020-02-01 tracey free(s->id1);
3554 5465d566 2020-02-01 tracey s->id1 = NULL;
3555 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3557 5465d566 2020-02-01 tracey s->f = NULL;
3558 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3559 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3560 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3561 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3562 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3563 5465d566 2020-02-01 tracey s->log_view = log_view;
3564 5465d566 2020-02-01 tracey s->repo = repo;
3566 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
3567 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3568 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3569 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3570 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3572 6d17833f 2019-11-08 stsp return err;
3573 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3574 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3575 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3576 6d17833f 2019-11-08 stsp if (err) {
3577 5465d566 2020-02-01 tracey free_colors(&s->colors);
3578 6d17833f 2019-11-08 stsp return err;
3580 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3581 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3582 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3583 6d17833f 2019-11-08 stsp if (err) {
3584 5465d566 2020-02-01 tracey free_colors(&s->colors);
3585 6d17833f 2019-11-08 stsp return err;
3588 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3589 ac4dc263 2021-09-24 thomas "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3590 ac4dc263 2021-09-24 thomas "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3591 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3592 6d17833f 2019-11-08 stsp if (err) {
3593 5465d566 2020-02-01 tracey free_colors(&s->colors);
3594 6d17833f 2019-11-08 stsp return err;
3597 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3598 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3599 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3600 11b20872 2019-11-08 stsp if (err) {
3601 5465d566 2020-02-01 tracey free_colors(&s->colors);
3602 11b20872 2019-11-08 stsp return err;
3605 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3606 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3607 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3608 11b20872 2019-11-08 stsp if (err) {
3609 5465d566 2020-02-01 tracey free_colors(&s->colors);
3610 11b20872 2019-11-08 stsp return err;
3614 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3615 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3616 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3618 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3619 fe621944 2020-11-10 stsp s->nlines = 0;
3620 5465d566 2020-02-01 tracey err = create_diff(s);
3621 48ae06ee 2018-10-18 stsp if (err) {
3622 5465d566 2020-02-01 tracey free(s->id1);
3623 5465d566 2020-02-01 tracey s->id1 = NULL;
3624 5465d566 2020-02-01 tracey free(s->id2);
3625 5465d566 2020-02-01 tracey s->id2 = NULL;
3626 78756c87 2020-11-24 stsp free_colors(&s->colors);
3627 48ae06ee 2018-10-18 stsp return err;
3630 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3631 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3632 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3633 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3634 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3636 5dc9f4bc 2018-08-04 stsp return NULL;
3639 e5a0f69f 2018-08-18 stsp static const struct got_error *
3640 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3642 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3643 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3645 5465d566 2020-02-01 tracey free(s->id1);
3646 5465d566 2020-02-01 tracey s->id1 = NULL;
3647 5465d566 2020-02-01 tracey free(s->id2);
3648 5465d566 2020-02-01 tracey s->id2 = NULL;
3649 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3650 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3651 5465d566 2020-02-01 tracey free_colors(&s->colors);
3652 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3653 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3654 fe621944 2020-11-10 stsp s->nlines = 0;
3655 e5a0f69f 2018-08-18 stsp return err;
3658 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3659 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3661 a3404814 2018-09-02 stsp const struct got_error *err;
3662 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3663 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3664 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3666 a3404814 2018-09-02 stsp if (s->id1) {
3667 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3669 a3404814 2018-09-02 stsp return err;
3670 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3672 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3674 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3676 a3404814 2018-09-02 stsp return err;
3677 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3679 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3680 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3681 a3404814 2018-09-02 stsp free(id_str1);
3682 a3404814 2018-09-02 stsp free(id_str2);
3683 a3404814 2018-09-02 stsp return err;
3685 a3404814 2018-09-02 stsp free(id_str1);
3686 a3404814 2018-09-02 stsp free(id_str2);
3688 267bb3b8 2021-08-01 stsp err = draw_file(view, header);
3689 267bb3b8 2021-08-01 stsp free(header);
3690 267bb3b8 2021-08-01 stsp return err;
3693 15a087fe 2019-02-21 stsp static const struct got_error *
3694 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3695 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3697 d7a04538 2019-02-21 stsp const struct got_error *err;
3698 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3699 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3700 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3702 15a087fe 2019-02-21 stsp free(s->id2);
3703 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3704 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3705 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3707 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3709 d7a04538 2019-02-21 stsp return err;
3710 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3711 15a087fe 2019-02-21 stsp free(s->id1);
3712 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
3713 ec242592 2022-04-22 thomas s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3714 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3715 15a087fe 2019-02-21 stsp return NULL;
3718 0cf4efb1 2018-09-29 stsp static const struct got_error *
3719 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3721 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3722 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3723 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3724 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3725 826082fe 2020-12-10 stsp char *line = NULL;
3726 826082fe 2020-12-10 stsp size_t linesize = 0;
3727 826082fe 2020-12-10 stsp ssize_t linelen;
3730 e5a0f69f 2018-08-18 stsp switch (ch) {
3733 3dbaef42 2020-11-24 stsp if (ch == 'a')
3734 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3735 3dbaef42 2020-11-24 stsp if (ch == 'w')
3736 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3737 64453f7e 2020-11-21 stsp wclear(view->window);
3738 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3739 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3740 aa61903a 2021-12-31 thomas s->matched_line = 0;
3741 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3742 64453f7e 2020-11-21 stsp err = create_diff(s);
3745 912a3f79 2021-08-30 j case KEY_HOME:
3746 912a3f79 2021-08-30 j s->first_displayed_line = 1;
3749 912a3f79 2021-08-30 j case KEY_END:
3753 912a3f79 2021-08-30 j s->first_displayed_line = (s->nlines - view->nlines) + 2;
3757 1e37a5c2 2019-05-12 jcs case KEY_UP:
3758 f7140bf5 2021-10-17 thomas case CTRL('p'):
3759 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3760 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3762 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3763 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3764 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3767 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3768 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3769 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3772 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3773 f7140bf5 2021-10-17 thomas case CTRL('n'):
3774 1e37a5c2 2019-05-12 jcs if (!s->eof)
3775 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3777 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3778 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3780 00ba99a7 2019-05-12 jcs if (s->eof)
3783 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3784 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3785 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3786 826082fe 2020-12-10 stsp if (linelen == -1) {
3787 826082fe 2020-12-10 stsp if (feof(s->f)) {
3788 826082fe 2020-12-10 stsp s->eof = 1;
3790 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
3794 826082fe 2020-12-10 stsp free(line);
3797 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3798 1e37a5c2 2019-05-12 jcs s->diff_context--;
3799 aa61903a 2021-12-31 thomas s->matched_line = 0;
3800 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3801 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3802 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3803 27829c9e 2020-11-21 stsp s->nlines) {
3804 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3805 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3810 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3811 1e37a5c2 2019-05-12 jcs s->diff_context++;
3812 aa61903a 2021-12-31 thomas s->matched_line = 0;
3813 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3814 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3819 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3821 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3822 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3824 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3828 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3831 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3835 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3836 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3837 aa61903a 2021-12-31 thomas s->matched_line = 0;
3839 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3840 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3844 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3846 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3847 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3849 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3853 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3856 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3860 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3861 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3862 aa61903a 2021-12-31 thomas s->matched_line = 0;
3864 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3865 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3871 bcbd79e2 2018-08-19 stsp return err;
3874 4ed7e80c 2018-05-20 stsp static const struct got_error *
3875 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3877 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3878 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3879 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3880 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3881 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3882 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3883 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3884 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3885 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3886 3dbaef42 2020-11-24 stsp const char *errstr;
3887 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3889 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3890 26ed57b2 2018-05-19 stsp switch (ch) {
3892 64453f7e 2020-11-21 stsp force_text_diff = 1;
3895 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3897 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3898 8f666e67 2022-02-12 thomas errx(1, "number of context lines is %s: %s",
3899 8f666e67 2022-02-12 thomas errstr, errstr);
3901 09b5bff8 2020-02-23 naddy case 'r':
3902 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3903 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3904 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3906 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3909 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3912 17020d27 2019-03-07 stsp usage_diff();
3913 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3917 26ed57b2 2018-05-19 stsp argc -= optind;
3918 26ed57b2 2018-05-19 stsp argv += optind;
3920 26ed57b2 2018-05-19 stsp if (argc == 0) {
3921 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3922 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3923 15a94983 2018-12-23 stsp id_str1 = argv[0];
3924 15a94983 2018-12-23 stsp id_str2 = argv[1];
3926 26ed57b2 2018-05-19 stsp usage_diff();
3928 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3929 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
3930 c156c7a4 2020-12-18 stsp if (cwd == NULL)
3931 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
3932 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
3933 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3934 c156c7a4 2020-12-18 stsp goto done;
3935 a273ac94 2020-02-23 naddy if (worktree)
3936 a273ac94 2020-02-23 naddy repo_path =
3937 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3939 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3940 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
3941 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
3942 c156c7a4 2020-12-18 stsp goto done;
3946 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3947 eb6600df 2019-01-04 stsp if (error)
3948 eb6600df 2019-01-04 stsp goto done;
3950 a273ac94 2020-02-23 naddy init_curses();
3952 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3953 51a10b52 2020-12-26 stsp if (error)
3954 51a10b52 2020-12-26 stsp goto done;
3956 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
3957 26ed57b2 2018-05-19 stsp if (error)
3958 26ed57b2 2018-05-19 stsp goto done;
3960 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3961 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3962 26ed57b2 2018-05-19 stsp if (error)
3963 26ed57b2 2018-05-19 stsp goto done;
3965 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3966 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &tog_refs, repo);
3967 26ed57b2 2018-05-19 stsp if (error)
3968 26ed57b2 2018-05-19 stsp goto done;
3970 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3971 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3972 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3973 ea5e7bb5 2018-08-01 stsp goto done;
3975 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3976 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
3977 5dc9f4bc 2018-08-04 stsp if (error)
3978 5dc9f4bc 2018-08-04 stsp goto done;
3979 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3981 3dbaef42 2020-11-24 stsp free(label1);
3982 3dbaef42 2020-11-24 stsp free(label2);
3983 c02c541e 2019-03-29 stsp free(repo_path);
3984 a273ac94 2020-02-23 naddy free(cwd);
3985 1d0f4054 2021-06-17 stsp if (repo) {
3986 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
3987 1d0f4054 2021-06-17 stsp if (error == NULL)
3988 1d0f4054 2021-06-17 stsp error = close_err;
3990 a273ac94 2020-02-23 naddy if (worktree)
3991 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3992 51a10b52 2020-12-26 stsp tog_free_refs();
3993 26ed57b2 2018-05-19 stsp return error;
3996 4ed7e80c 2018-05-20 stsp __dead static void
3997 9f7d7167 2018-04-29 stsp usage_blame(void)
4000 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
4001 9f7d7167 2018-04-29 stsp getprogname());
4005 84451b3e 2018-07-10 stsp struct tog_blame_line {
4006 84451b3e 2018-07-10 stsp int annotated;
4007 84451b3e 2018-07-10 stsp struct got_object_id *id;
4010 4ed7e80c 2018-05-20 stsp static const struct got_error *
4011 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
4013 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4014 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4015 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
4016 84451b3e 2018-07-10 stsp const struct got_error *err;
4017 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
4018 826082fe 2020-12-10 stsp char *line = NULL;
4019 826082fe 2020-12-10 stsp size_t linesize = 0;
4020 826082fe 2020-12-10 stsp ssize_t linelen;
4021 84451b3e 2018-07-10 stsp wchar_t *wline;
4022 27a741e5 2019-09-11 stsp int width;
4023 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
4024 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
4025 ab089a2a 2018-07-12 stsp char *id_str;
4026 11b20872 2019-11-08 stsp struct tog_color *tc;
4028 ec242592 2022-04-22 thomas err = got_object_id_str(&id_str, &s->blamed_commit->id);
4030 ab089a2a 2018-07-12 stsp return err;
4032 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
4033 f7d12f7e 2018-08-01 stsp werase(view->window);
4035 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
4036 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4037 ab089a2a 2018-07-12 stsp free(id_str);
4038 ab089a2a 2018-07-12 stsp return err;
4041 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4042 ab089a2a 2018-07-12 stsp free(line);
4043 2550e4c3 2018-07-13 stsp line = NULL;
4045 1cae65b4 2019-09-22 stsp return err;
4046 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4047 a3404814 2018-09-02 stsp wstandout(view->window);
4048 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4050 11b20872 2019-11-08 stsp wattr_on(view->window,
4051 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4052 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4054 11b20872 2019-11-08 stsp wattr_off(view->window,
4055 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4056 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4057 a3404814 2018-09-02 stsp wstandend(view->window);
4058 2550e4c3 2018-07-13 stsp free(wline);
4059 2550e4c3 2018-07-13 stsp wline = NULL;
4060 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4061 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4063 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
4064 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4065 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4066 ab089a2a 2018-07-12 stsp free(id_str);
4067 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4069 ab089a2a 2018-07-12 stsp free(id_str);
4070 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4071 3f60a8ef 2018-07-10 stsp free(line);
4072 2550e4c3 2018-07-13 stsp line = NULL;
4074 3f60a8ef 2018-07-10 stsp return err;
4075 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4076 2550e4c3 2018-07-13 stsp free(wline);
4077 2550e4c3 2018-07-13 stsp wline = NULL;
4078 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4079 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4081 4f7c3e5e 2020-12-01 naddy s->eof = 0;
4082 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
4083 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
4084 826082fe 2020-12-10 stsp if (linelen == -1) {
4085 826082fe 2020-12-10 stsp if (feof(blame->f)) {
4086 826082fe 2020-12-10 stsp s->eof = 1;
4089 84451b3e 2018-07-10 stsp free(line);
4090 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
4092 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
4095 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4096 f7d12f7e 2018-08-01 stsp wstandout(view->window);
4098 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
4099 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
4100 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
4101 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4102 27a741e5 2019-09-11 stsp !(view->focussed &&
4103 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
4104 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4105 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
4106 8d0fe45a 2019-08-12 stsp char *id_str;
4107 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
4108 8d0fe45a 2019-08-12 stsp if (err) {
4109 8d0fe45a 2019-08-12 stsp free(line);
4110 8d0fe45a 2019-08-12 stsp return err;
4112 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4114 11b20872 2019-11-08 stsp wattr_on(view->window,
4115 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4116 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
4118 11b20872 2019-11-08 stsp wattr_off(view->window,
4119 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4120 8d0fe45a 2019-08-12 stsp free(id_str);
4121 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
4123 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4124 8d0fe45a 2019-08-12 stsp prev_id = NULL;
4127 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
4128 ee41ec32 2018-07-10 stsp prev_id = NULL;
4131 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
4132 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4133 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
4135 41605754 2020-11-12 stsp if (view->ncols <= 9) {
4136 41605754 2020-11-12 stsp width = 9;
4137 41605754 2020-11-12 stsp wline = wcsdup(L"");
4138 41605754 2020-11-12 stsp if (wline == NULL) {
4139 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
4140 41605754 2020-11-12 stsp free(line);
4141 41605754 2020-11-12 stsp return err;
4143 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4144 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4145 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4146 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4147 41605754 2020-11-12 stsp view->window, regmatch);
4148 41605754 2020-11-12 stsp if (err) {
4149 41605754 2020-11-12 stsp free(line);
4150 41605754 2020-11-12 stsp return err;
4152 41605754 2020-11-12 stsp width += 9;
4154 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4155 41605754 2020-11-12 stsp view->ncols - 9, 9);
4156 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4157 41605754 2020-11-12 stsp free(wline);
4158 41605754 2020-11-12 stsp wline = NULL;
4159 41605754 2020-11-12 stsp width += 9;
4162 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4163 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4164 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4165 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4167 826082fe 2020-12-10 stsp free(line);
4168 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4170 1a57306a 2018-09-02 stsp view_vborder(view);
4172 84451b3e 2018-07-10 stsp return NULL;
4175 84451b3e 2018-07-10 stsp static const struct got_error *
4176 10f173fe 2022-04-16 thomas blame_cb(void *arg, int nlines, int lineno,
4177 10f173fe 2022-04-16 thomas struct got_commit_object *commit, struct got_object_id *id)
4179 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4180 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4181 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4182 1a76625f 2018-10-22 stsp int errcode;
4184 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4185 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4186 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4188 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4189 1a76625f 2018-10-22 stsp if (errcode)
4190 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4192 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4193 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4194 d68a0a7d 2018-07-10 stsp goto done;
4197 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4198 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4200 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4201 d68a0a7d 2018-07-10 stsp if (line->annotated)
4202 d68a0a7d 2018-07-10 stsp goto done;
4204 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4205 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4206 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4207 84451b3e 2018-07-10 stsp goto done;
4209 84451b3e 2018-07-10 stsp line->annotated = 1;
4211 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4212 1a76625f 2018-10-22 stsp if (errcode)
4213 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4214 84451b3e 2018-07-10 stsp return err;
4217 84451b3e 2018-07-10 stsp static void *
4218 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4220 1d0f4054 2021-06-17 stsp const struct got_error *err, *close_err;
4221 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4222 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4223 1a76625f 2018-10-22 stsp int errcode;
4225 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4227 61266923 2020-01-14 stsp return (void *)err;
4229 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4230 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4231 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4232 fc06ba56 2019-08-22 stsp err = NULL;
4234 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4235 1a76625f 2018-10-22 stsp if (errcode)
4236 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4237 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4239 1d0f4054 2021-06-17 stsp close_err = got_repo_close(ta->repo);
4240 1d0f4054 2021-06-17 stsp if (err == NULL)
4241 1d0f4054 2021-06-17 stsp err = close_err;
4242 c9beca56 2018-07-22 stsp ta->repo = NULL;
4243 c9beca56 2018-07-22 stsp *ta->complete = 1;
4245 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4246 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4247 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4249 18430de3 2018-07-10 stsp return (void *)err;
4252 245d91c1 2018-07-12 stsp static struct got_object_id *
4253 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4254 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4256 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4258 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4259 8d0fe45a 2019-08-12 stsp return NULL;
4261 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4262 245d91c1 2018-07-12 stsp if (!line->annotated)
4263 245d91c1 2018-07-12 stsp return NULL;
4265 245d91c1 2018-07-12 stsp return line->id;
4268 b880a918 2018-07-10 stsp static const struct got_error *
4269 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4271 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4274 245d91c1 2018-07-12 stsp if (blame->thread) {
4275 1a76625f 2018-10-22 stsp int errcode;
4276 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4277 1a76625f 2018-10-22 stsp if (errcode)
4278 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4279 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4280 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4281 1a76625f 2018-10-22 stsp if (errcode)
4282 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4283 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4284 1a76625f 2018-10-22 stsp if (errcode)
4285 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4286 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4287 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4288 245d91c1 2018-07-12 stsp err = NULL;
4289 dd038bc6 2021-09-21 thomas.ad blame->thread = 0; //NULL;
4291 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4292 1d0f4054 2021-06-17 stsp const struct got_error *close_err;
4293 1d0f4054 2021-06-17 stsp close_err = got_repo_close(blame->thread_args.repo);
4294 1d0f4054 2021-06-17 stsp if (err == NULL)
4295 1d0f4054 2021-06-17 stsp err = close_err;
4296 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4298 245d91c1 2018-07-12 stsp if (blame->f) {
4299 56b63ca4 2021-01-22 stsp if (fclose(blame->f) == EOF && err == NULL)
4300 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4301 245d91c1 2018-07-12 stsp blame->f = NULL;
4303 57670559 2018-12-23 stsp if (blame->lines) {
4304 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4305 57670559 2018-12-23 stsp free(blame->lines[i].id);
4306 57670559 2018-12-23 stsp free(blame->lines);
4307 57670559 2018-12-23 stsp blame->lines = NULL;
4309 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4310 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4312 245d91c1 2018-07-12 stsp return err;
4315 245d91c1 2018-07-12 stsp static const struct got_error *
4316 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4318 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4319 fc06ba56 2019-08-22 stsp int *done = arg;
4320 fc06ba56 2019-08-22 stsp int errcode;
4322 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4323 fc06ba56 2019-08-22 stsp if (errcode)
4324 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4325 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4327 fc06ba56 2019-08-22 stsp if (*done)
4328 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4330 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4331 fc06ba56 2019-08-22 stsp if (errcode)
4332 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4333 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4335 fc06ba56 2019-08-22 stsp return err;
4338 fc06ba56 2019-08-22 stsp static const struct got_error *
4339 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4341 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4342 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4343 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4344 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
4345 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4346 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4347 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4348 15a94983 2018-12-23 stsp int obj_type;
4350 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&commit, s->repo,
4351 ec242592 2022-04-22 thomas &s->blamed_commit->id);
4353 15a94983 2018-12-23 stsp return err;
4355 945f9229 2022-04-16 thomas err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4356 945f9229 2022-04-16 thomas if (err)
4357 945f9229 2022-04-16 thomas goto done;
4359 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4361 84451b3e 2018-07-10 stsp goto done;
4363 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4364 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4365 84451b3e 2018-07-10 stsp goto done;
4368 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4370 a70480e0 2018-06-23 stsp goto done;
4371 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4372 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4373 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4374 84451b3e 2018-07-10 stsp goto done;
4376 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4377 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4379 1fddf795 2021-01-20 stsp goto done;
4380 1fddf795 2021-01-20 stsp if (blame->nlines == 0) {
4381 1fddf795 2021-01-20 stsp s->blame_complete = 1;
4382 84451b3e 2018-07-10 stsp goto done;
4385 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4386 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4387 b02560ec 2019-08-19 stsp blame->nlines--;
4389 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4390 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4391 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4392 84451b3e 2018-07-10 stsp goto done;
4395 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4397 bd24772e 2018-07-11 stsp goto done;
4399 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4400 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4401 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4402 ec242592 2022-04-22 thomas blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4403 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4404 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4405 245d91c1 2018-07-12 stsp goto done;
4407 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4409 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4410 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4411 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4412 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4413 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4414 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4415 a5388363 2020-12-01 naddy s->blame_complete = 0;
4417 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4418 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4419 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4420 f5a09613 2020-12-13 naddy s->selected_line = 1;
4422 aa61903a 2021-12-31 thomas s->matched_line = 0;
4425 945f9229 2022-04-16 thomas if (commit)
4426 945f9229 2022-04-16 thomas got_object_commit_close(commit);
4428 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4429 27d434c2 2018-09-15 stsp free(obj_id);
4431 245d91c1 2018-07-12 stsp stop_blame(blame);
4432 245d91c1 2018-07-12 stsp return err;
4435 245d91c1 2018-07-12 stsp static const struct got_error *
4436 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4437 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4439 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4440 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4442 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->blamed_commits);
4444 c4843652 2019-08-12 stsp s->path = strdup(path);
4445 c4843652 2019-08-12 stsp if (s->path == NULL)
4446 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4448 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4449 c4843652 2019-08-12 stsp if (err) {
4450 c4843652 2019-08-12 stsp free(s->path);
4451 7cbe629d 2018-08-04 stsp return err;
4454 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4455 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4456 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4457 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4458 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4459 fb2756b9 2018-08-04 stsp s->repo = repo;
4460 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4461 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4463 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
4464 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4465 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4466 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4468 11b20872 2019-11-08 stsp return err;
4471 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4472 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4473 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4474 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4475 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4477 a5388363 2020-12-01 naddy return run_blame(view);
4480 e5a0f69f 2018-08-18 stsp static const struct got_error *
4481 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4483 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4484 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4486 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4487 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4489 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&s->blamed_commits)) {
4490 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4491 dbdddfee 2021-06-23 naddy blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4492 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4493 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4496 e5a0f69f 2018-08-18 stsp free(s->path);
4497 11b20872 2019-11-08 stsp free_colors(&s->colors);
4499 e5a0f69f 2018-08-18 stsp return err;
4502 7cbe629d 2018-08-04 stsp static const struct got_error *
4503 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4505 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4507 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4508 6c4c42e0 2019-06-24 stsp return NULL;
4511 6c4c42e0 2019-06-24 stsp static const struct got_error *
4512 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4514 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4515 6c4c42e0 2019-06-24 stsp int lineno;
4516 826082fe 2020-12-10 stsp char *line = NULL;
4517 826082fe 2020-12-10 stsp size_t linesize = 0;
4518 826082fe 2020-12-10 stsp ssize_t linelen;
4520 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4521 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4522 6c4c42e0 2019-06-24 stsp return NULL;
4525 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4526 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4527 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4529 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4531 4b3f9dac 2021-12-17 thomas lineno = s->first_displayed_line - 1 + s->selected_line;
4533 6c4c42e0 2019-06-24 stsp while (1) {
4534 6c4c42e0 2019-06-24 stsp off_t offset;
4536 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4537 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4538 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4542 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4543 6c4c42e0 2019-06-24 stsp lineno = 1;
4545 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4548 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4549 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4550 6c4c42e0 2019-06-24 stsp free(line);
4551 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4553 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4554 826082fe 2020-12-10 stsp if (linelen != -1 &&
4555 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4556 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4557 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4560 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4565 826082fe 2020-12-10 stsp free(line);
4567 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4568 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4569 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4572 6c4c42e0 2019-06-24 stsp return NULL;
4575 6c4c42e0 2019-06-24 stsp static const struct got_error *
4576 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4578 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4579 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4580 2b380cc8 2018-10-24 stsp int errcode;
4582 89a927a3 2021-09-21 thomas.ad if (s->blame.thread == 0 && !s->blame_complete) {
4583 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4584 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4585 2b380cc8 2018-10-24 stsp if (errcode)
4586 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4588 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4591 51fe7530 2019-08-19 stsp if (s->blame_complete)
4592 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4594 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4596 669b5ffa 2018-10-07 stsp view_vborder(view);
4597 e5a0f69f 2018-08-18 stsp return err;
4600 e5a0f69f 2018-08-18 stsp static const struct got_error *
4601 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4603 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4604 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4605 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4606 669b5ffa 2018-10-07 stsp int begin_x = 0;
4608 e5a0f69f 2018-08-18 stsp switch (ch) {
4610 1e37a5c2 2019-05-12 jcs s->done = 1;
4612 4deef56f 2021-09-02 naddy case 'g':
4613 4deef56f 2021-09-02 naddy case KEY_HOME:
4614 4deef56f 2021-09-02 naddy s->selected_line = 1;
4615 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4617 4deef56f 2021-09-02 naddy case 'G':
4618 4deef56f 2021-09-02 naddy case KEY_END:
4619 4deef56f 2021-09-02 naddy if (s->blame.nlines < view->nlines - 2) {
4620 4deef56f 2021-09-02 naddy s->selected_line = s->blame.nlines;
4621 4deef56f 2021-09-02 naddy s->first_displayed_line = 1;
4623 4deef56f 2021-09-02 naddy s->selected_line = view->nlines - 2;
4624 4deef56f 2021-09-02 naddy s->first_displayed_line = s->blame.nlines -
4625 4deef56f 2021-09-02 naddy (view->nlines - 3);
4629 1e37a5c2 2019-05-12 jcs case KEY_UP:
4630 f7140bf5 2021-10-17 thomas case CTRL('p'):
4631 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4632 1e37a5c2 2019-05-12 jcs s->selected_line--;
4633 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4634 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4635 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4637 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4638 ea025d1d 2020-02-22 naddy case CTRL('b'):
4639 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4640 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4643 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4644 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4645 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4647 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4650 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4651 f7140bf5 2021-10-17 thomas case CTRL('n'):
4652 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4653 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4654 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4655 1e37a5c2 2019-05-12 jcs s->selected_line++;
4656 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4657 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4658 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4661 1e37a5c2 2019-05-12 jcs case 'p': {
4662 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4663 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4664 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4665 1e37a5c2 2019-05-12 jcs if (id == NULL)
4667 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4668 945f9229 2022-04-16 thomas struct got_commit_object *commit, *pcommit;
4669 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4670 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4671 1e37a5c2 2019-05-12 jcs int obj_type;
4672 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4673 1e37a5c2 2019-05-12 jcs s->repo, id);
4676 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4677 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4678 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4679 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4682 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4683 945f9229 2022-04-16 thomas err = got_object_open_as_commit(&pcommit,
4684 ec242592 2022-04-22 thomas s->repo, &pid->id);
4685 945f9229 2022-04-16 thomas if (err)
4687 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4688 945f9229 2022-04-16 thomas pcommit, s->path);
4689 945f9229 2022-04-16 thomas got_object_commit_close(pcommit);
4690 e5a0f69f 2018-08-18 stsp if (err) {
4691 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4692 1e37a5c2 2019-05-12 jcs err = NULL;
4693 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4696 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4698 1e37a5c2 2019-05-12 jcs free(blob_id);
4699 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4700 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4701 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4704 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4705 ec242592 2022-04-22 thomas &pid->id);
4706 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4708 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4709 ec242592 2022-04-22 thomas &s->blamed_commit->id) == 0)
4711 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4716 1e37a5c2 2019-05-12 jcs s->done = 1;
4717 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4718 1e37a5c2 2019-05-12 jcs s->done = 0;
4719 1e37a5c2 2019-05-12 jcs if (thread_err)
4721 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&s->blamed_commits,
4722 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4723 a5388363 2020-12-01 naddy err = run_blame(view);
4728 1e37a5c2 2019-05-12 jcs case 'B': {
4729 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4730 dbdddfee 2021-06-23 naddy first = STAILQ_FIRST(&s->blamed_commits);
4731 ec242592 2022-04-22 thomas if (!got_object_id_cmp(&first->id, s->commit_id))
4733 1e37a5c2 2019-05-12 jcs s->done = 1;
4734 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4735 1e37a5c2 2019-05-12 jcs s->done = 0;
4736 1e37a5c2 2019-05-12 jcs if (thread_err)
4738 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4739 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4740 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4741 dbdddfee 2021-06-23 naddy STAILQ_FIRST(&s->blamed_commits);
4742 a5388363 2020-12-01 naddy err = run_blame(view);
4747 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4748 1e37a5c2 2019-05-12 jcs case '\r': {
4749 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4750 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4751 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4752 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4753 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4754 1e37a5c2 2019-05-12 jcs if (id == NULL)
4756 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4759 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(
4760 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4761 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4762 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4763 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4764 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4765 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4766 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4769 ec242592 2022-04-22 thomas err = open_diff_view(diff_view, pid ? &pid->id : NULL,
4770 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4771 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4773 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4776 e78dc838 2020-12-04 stsp view->focussed = 0;
4777 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4778 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4779 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4782 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4783 e78dc838 2020-12-04 stsp view->focus_child = 1;
4785 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4790 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4791 ea025d1d 2020-02-22 naddy case CTRL('f'):
4793 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4794 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4795 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4798 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4799 1e37a5c2 2019-05-12 jcs 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);
4804 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4805 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4806 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4807 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4809 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4810 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4811 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4813 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4814 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4815 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4816 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4822 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4825 a70480e0 2018-06-23 stsp static const struct got_error *
4826 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4828 a70480e0 2018-06-23 stsp const struct got_error *error;
4829 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4830 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4831 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4832 0587e10c 2020-07-23 stsp char *link_target = NULL;
4833 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4834 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
4835 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4837 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4839 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4840 a70480e0 2018-06-23 stsp switch (ch) {
4842 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4845 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4846 69069811 2018-08-02 stsp if (repo_path == NULL)
4847 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4851 17020d27 2019-03-07 stsp usage_blame();
4852 a70480e0 2018-06-23 stsp /* NOTREACHED */
4856 a70480e0 2018-06-23 stsp argc -= optind;
4857 a70480e0 2018-06-23 stsp argv += optind;
4859 f135c941 2020-02-20 stsp if (argc != 1)
4860 a70480e0 2018-06-23 stsp usage_blame();
4862 69069811 2018-08-02 stsp if (repo_path == NULL) {
4863 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
4864 c156c7a4 2020-12-18 stsp if (cwd == NULL)
4865 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
4866 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
4867 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4868 c156c7a4 2020-12-18 stsp goto done;
4869 f135c941 2020-02-20 stsp if (worktree)
4870 eb41ed75 2019-02-05 stsp repo_path =
4871 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4873 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4874 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
4875 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
4876 c156c7a4 2020-12-18 stsp goto done;
4880 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4881 c02c541e 2019-03-29 stsp if (error != NULL)
4882 8e94dd5b 2019-01-04 stsp goto done;
4884 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4885 f135c941 2020-02-20 stsp worktree);
4886 c02c541e 2019-03-29 stsp if (error)
4887 92205607 2019-01-04 stsp goto done;
4889 f135c941 2020-02-20 stsp init_curses();
4891 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4892 51a10b52 2020-12-26 stsp if (error)
4893 51a10b52 2020-12-26 stsp goto done;
4895 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
4896 eb41ed75 2019-02-05 stsp if (error)
4897 69069811 2018-08-02 stsp goto done;
4899 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4900 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4901 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4902 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4903 a70480e0 2018-06-23 stsp if (error != NULL)
4904 66b4983c 2018-06-23 stsp goto done;
4905 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4906 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4908 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4909 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
4911 a19e88aa 2018-06-23 stsp if (error != NULL)
4912 8b473291 2019-02-21 stsp goto done;
4914 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4915 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4916 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4917 e1cd8fed 2018-08-01 stsp goto done;
4920 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
4921 945f9229 2022-04-16 thomas if (error)
4922 945f9229 2022-04-16 thomas goto done;
4924 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4925 945f9229 2022-04-16 thomas commit, repo);
4926 7cbe629d 2018-08-04 stsp if (error)
4927 7cbe629d 2018-08-04 stsp goto done;
4929 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4930 78756c87 2020-11-24 stsp commit_id, repo);
4931 0587e10c 2020-07-23 stsp if (error)
4932 0587e10c 2020-07-23 stsp goto done;
4933 12314ad4 2019-08-31 stsp if (worktree) {
4934 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4935 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4936 12314ad4 2019-08-31 stsp worktree = NULL;
4938 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4940 69069811 2018-08-02 stsp free(repo_path);
4941 f135c941 2020-02-20 stsp free(in_repo_path);
4942 0587e10c 2020-07-23 stsp free(link_target);
4943 69069811 2018-08-02 stsp free(cwd);
4944 a70480e0 2018-06-23 stsp free(commit_id);
4945 945f9229 2022-04-16 thomas if (commit)
4946 945f9229 2022-04-16 thomas got_object_commit_close(commit);
4947 eb41ed75 2019-02-05 stsp if (worktree)
4948 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4949 1d0f4054 2021-06-17 stsp if (repo) {
4950 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4951 1d0f4054 2021-06-17 stsp if (error == NULL)
4952 1d0f4054 2021-06-17 stsp error = close_err;
4954 51a10b52 2020-12-26 stsp tog_free_refs();
4955 a70480e0 2018-06-23 stsp return error;
4958 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4959 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
4961 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4962 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4963 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4964 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4965 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4966 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4967 d86d3b18 2020-12-01 naddy int limit = view->nlines;
4969 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
4971 f7d12f7e 2018-08-01 stsp werase(view->window);
4973 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4974 ffd1d5e5 2018-06-23 stsp return NULL;
4976 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4978 ffd1d5e5 2018-06-23 stsp return err;
4979 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4980 a3404814 2018-09-02 stsp wstandout(view->window);
4981 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4983 11b20872 2019-11-08 stsp wattr_on(view->window,
4984 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4985 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4987 11b20872 2019-11-08 stsp wattr_off(view->window,
4988 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4989 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4990 a3404814 2018-09-02 stsp wstandend(view->window);
4991 2550e4c3 2018-07-13 stsp free(wline);
4992 2550e4c3 2018-07-13 stsp wline = NULL;
4993 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4994 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4995 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4996 ffd1d5e5 2018-06-23 stsp return NULL;
4997 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4999 ce52c690 2018-06-23 stsp return err;
5000 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5001 2550e4c3 2018-07-13 stsp free(wline);
5002 2550e4c3 2018-07-13 stsp wline = NULL;
5003 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5004 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5005 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5006 ffd1d5e5 2018-06-23 stsp return NULL;
5007 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5008 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
5009 a1eca9bb 2018-06-23 stsp return NULL;
5011 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
5012 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
5013 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
5014 0cf4efb1 2018-09-29 stsp if (view->focussed)
5015 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5016 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
5018 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
5019 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
5020 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5021 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5022 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5023 ffd1d5e5 2018-06-23 stsp return NULL;
5027 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
5030 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
5031 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5032 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
5033 848d6979 2019-08-12 stsp const char *modestr = "";
5034 56e0773d 2019-11-28 stsp mode_t mode;
5036 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
5037 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
5039 d86d3b18 2020-12-01 naddy if (s->show_ids) {
5040 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5041 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5043 638f9024 2019-05-13 stsp return got_error_from_errno(
5044 230a42bd 2019-05-11 jcs "got_object_id_str");
5046 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5047 63c5ca5d 2019-08-24 stsp modestr = "$";
5048 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5051 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
5052 d86d3b18 2020-12-01 naddy te, s->repo);
5053 0d6c6ee3 2020-05-20 stsp if (err) {
5054 0d6c6ee3 2020-05-20 stsp free(id_str);
5055 0d6c6ee3 2020-05-20 stsp return err;
5057 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5058 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5059 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5061 848d6979 2019-08-12 stsp modestr = "@";
5063 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5064 848d6979 2019-08-12 stsp modestr = "/";
5065 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5066 848d6979 2019-08-12 stsp modestr = "*";
5067 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5068 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
5069 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
5070 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
5071 1d13200f 2018-07-12 stsp free(id_str);
5072 0d6c6ee3 2020-05-20 stsp free(link_target);
5073 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5075 1d13200f 2018-07-12 stsp free(id_str);
5076 0d6c6ee3 2020-05-20 stsp free(link_target);
5077 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5078 ffd1d5e5 2018-06-23 stsp if (err) {
5079 ffd1d5e5 2018-06-23 stsp free(line);
5082 d86d3b18 2020-12-01 naddy if (n == s->selected) {
5083 0cf4efb1 2018-09-29 stsp if (view->focussed)
5084 0cf4efb1 2018-09-29 stsp wstandout(view->window);
5085 d86d3b18 2020-12-01 naddy s->selected_entry = te;
5087 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
5089 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
5090 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5091 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
5093 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
5094 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
5095 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
5096 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
5097 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
5098 f7d12f7e 2018-08-01 stsp wstandend(view->window);
5099 ffd1d5e5 2018-06-23 stsp free(line);
5100 2550e4c3 2018-07-13 stsp free(wline);
5101 2550e4c3 2018-07-13 stsp wline = NULL;
5103 d86d3b18 2020-12-01 naddy s->ndisplayed++;
5104 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
5105 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
5109 ffd1d5e5 2018-06-23 stsp return err;
5112 ffd1d5e5 2018-06-23 stsp static void
5113 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5115 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5116 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
5117 fa86c4bf 2020-11-29 stsp int i = 0;
5119 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
5122 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5123 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
5124 fa86c4bf 2020-11-29 stsp if (te == NULL) {
5125 fa86c4bf 2020-11-29 stsp if (!isroot)
5126 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
5129 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
5130 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
5134 fa86c4bf 2020-11-29 stsp static void
5135 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5137 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
5138 ffd1d5e5 2018-06-23 stsp int n = 0;
5140 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5141 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
5142 694d3271 2020-12-01 naddy s->first_displayed_entry);
5144 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
5146 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5147 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
5148 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
5149 768394f3 2019-01-24 stsp if (last) {
5150 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5151 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
5156 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5157 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
5158 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
5160 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
5161 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
5162 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
5164 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
5165 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
5166 56e0773d 2019-11-28 stsp + 1 /* slash */;
5168 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
5170 ce52c690 2018-06-23 stsp *path = calloc(1, len);
5171 ffd1d5e5 2018-06-23 stsp if (path == NULL)
5172 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
5174 ce52c690 2018-06-23 stsp (*path)[0] = '/';
5175 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
5176 d9765a41 2018-06-23 stsp while (pt) {
5177 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
5178 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
5179 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5180 cb2ebc8a 2018-06-23 stsp goto done;
5182 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
5183 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5184 cb2ebc8a 2018-06-23 stsp goto done;
5186 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5189 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5190 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
5191 ce52c690 2018-06-23 stsp goto done;
5195 ce52c690 2018-06-23 stsp if (err) {
5196 ce52c690 2018-06-23 stsp free(*path);
5197 ce52c690 2018-06-23 stsp *path = NULL;
5199 ce52c690 2018-06-23 stsp return err;
5202 ce52c690 2018-06-23 stsp static const struct got_error *
5203 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5204 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5205 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5207 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5208 ce52c690 2018-06-23 stsp char *path;
5209 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5211 a0de39f3 2019-08-09 stsp *new_view = NULL;
5213 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5215 ce52c690 2018-06-23 stsp return err;
5217 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5218 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5219 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5220 83ce39e3 2019-08-12 stsp goto done;
5223 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5224 e5a0f69f 2018-08-18 stsp if (err) {
5225 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5226 fc06ba56 2019-08-22 stsp err = NULL;
5227 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5229 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5231 83ce39e3 2019-08-12 stsp free(path);
5232 69efd4c4 2018-07-18 stsp return err;
5235 69efd4c4 2018-07-18 stsp static const struct got_error *
5236 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5237 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5239 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5240 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5241 69efd4c4 2018-07-18 stsp char *path;
5243 a0de39f3 2019-08-09 stsp *new_view = NULL;
5245 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5246 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5247 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5249 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5251 69efd4c4 2018-07-18 stsp return err;
5253 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5256 e5a0f69f 2018-08-18 stsp view_close(log_view);
5258 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5259 cb2ebc8a 2018-06-23 stsp free(path);
5260 cb2ebc8a 2018-06-23 stsp return err;
5263 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5264 bc573f3b 2021-07-10 stsp open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5265 bc573f3b 2021-07-10 stsp const char *head_ref_name, struct got_repository *repo)
5267 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5268 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5269 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5270 bc573f3b 2021-07-10 stsp struct got_commit_object *commit = NULL;
5272 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5273 bc573f3b 2021-07-10 stsp STAILQ_INIT(&s->colors);
5275 bc573f3b 2021-07-10 stsp s->commit_id = got_object_id_dup(commit_id);
5276 bc573f3b 2021-07-10 stsp if (s->commit_id == NULL)
5277 bc573f3b 2021-07-10 stsp return got_error_from_errno("got_object_id_dup");
5279 bc573f3b 2021-07-10 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5281 bc573f3b 2021-07-10 stsp goto done;
5284 bc573f3b 2021-07-10 stsp * The root is opened here and will be closed when the view is closed.
5285 bc573f3b 2021-07-10 stsp * Any visited subtrees and their path-wise parents are opened and
5286 bc573f3b 2021-07-10 stsp * closed on demand.
5288 bc573f3b 2021-07-10 stsp err = got_object_open_as_tree(&s->root, repo,
5289 bc573f3b 2021-07-10 stsp got_object_commit_get_tree_id(commit));
5291 bc573f3b 2021-07-10 stsp goto done;
5292 bc573f3b 2021-07-10 stsp s->tree = s->root;
5294 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5295 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5296 ffd1d5e5 2018-06-23 stsp goto done;
5298 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5299 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5300 ffd1d5e5 2018-06-23 stsp goto done;
5303 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5304 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5305 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5306 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5307 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5308 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5309 9cd7cbd1 2020-12-07 stsp goto done;
5312 fb2756b9 2018-08-04 stsp s->repo = repo;
5314 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5315 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5316 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5317 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5319 c0b01bdb 2019-11-08 stsp goto done;
5320 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5321 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5323 c0b01bdb 2019-11-08 stsp goto done;
5324 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5325 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5326 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5328 c0b01bdb 2019-11-08 stsp goto done;
5330 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5331 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5332 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5334 11b20872 2019-11-08 stsp goto done;
5336 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5337 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5339 c0b01bdb 2019-11-08 stsp goto done;
5342 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5343 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5344 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5345 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5346 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5348 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5349 bc573f3b 2021-07-10 stsp if (commit)
5350 bc573f3b 2021-07-10 stsp got_object_commit_close(commit);
5352 bc573f3b 2021-07-10 stsp close_tree_view(view);
5353 ad80ab7b 2018-08-04 stsp return err;
5356 e5a0f69f 2018-08-18 stsp static const struct got_error *
5357 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5359 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5361 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5362 fb2756b9 2018-08-04 stsp free(s->tree_label);
5363 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5364 6484ec90 2018-09-29 stsp free(s->commit_id);
5365 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5366 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5367 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5368 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5369 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5370 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5371 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5372 bc573f3b 2021-07-10 stsp if (parent->tree != s->root)
5373 bc573f3b 2021-07-10 stsp got_object_tree_close(parent->tree);
5374 ad80ab7b 2018-08-04 stsp free(parent);
5377 bc573f3b 2021-07-10 stsp if (s->tree != NULL && s->tree != s->root)
5378 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5379 bc573f3b 2021-07-10 stsp if (s->root)
5380 bc573f3b 2021-07-10 stsp got_object_tree_close(s->root);
5381 7c32bd05 2019-06-22 stsp return NULL;
5384 7c32bd05 2019-06-22 stsp static const struct got_error *
5385 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5387 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5389 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5390 7c32bd05 2019-06-22 stsp return NULL;
5393 7c32bd05 2019-06-22 stsp static int
5394 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5396 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5398 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5402 7c32bd05 2019-06-22 stsp static const struct got_error *
5403 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5405 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5406 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5408 7c32bd05 2019-06-22 stsp if (!view->searching) {
5409 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5410 7c32bd05 2019-06-22 stsp return NULL;
5413 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5414 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5415 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5416 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5417 56e0773d 2019-11-28 stsp s->selected_entry);
5419 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5421 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5422 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5424 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5425 56e0773d 2019-11-28 stsp s->selected_entry);
5428 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
5429 4b3f9dac 2021-12-17 thomas te = s->selected_entry;
5430 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
5431 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5433 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5436 7c32bd05 2019-06-22 stsp while (1) {
5437 56e0773d 2019-11-28 stsp if (te == NULL) {
5438 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5439 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5440 ac66afb8 2019-06-24 stsp return NULL;
5442 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5443 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5445 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5448 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5449 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5450 56e0773d 2019-11-28 stsp s->matched_entry = te;
5454 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5455 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5457 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5460 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5461 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5462 7c32bd05 2019-06-22 stsp s->selected = 0;
5465 e5a0f69f 2018-08-18 stsp return NULL;
5468 ad80ab7b 2018-08-04 stsp static const struct got_error *
5469 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5471 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5472 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5473 e5a0f69f 2018-08-18 stsp char *parent_path;
5475 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5477 e5a0f69f 2018-08-18 stsp return err;
5479 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5480 e5a0f69f 2018-08-18 stsp free(parent_path);
5482 669b5ffa 2018-10-07 stsp view_vborder(view);
5483 e5a0f69f 2018-08-18 stsp return err;
5486 e5a0f69f 2018-08-18 stsp static const struct got_error *
5487 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5489 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5490 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5491 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5492 e4526bf5 2021-09-03 naddy struct got_tree_entry *te;
5493 e4526bf5 2021-09-03 naddy int begin_x = 0, n;
5495 e5a0f69f 2018-08-18 stsp switch (ch) {
5497 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5500 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5502 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5503 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5504 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5505 e78dc838 2020-12-04 stsp view->focussed = 0;
5506 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5507 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5508 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5510 1e37a5c2 2019-05-12 jcs return err;
5511 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5512 e78dc838 2020-12-04 stsp view->focus_child = 1;
5514 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5517 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5518 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5519 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5520 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5521 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5522 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5523 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5524 152c1c93 2020-11-29 stsp if (err) {
5525 152c1c93 2020-11-29 stsp view_close(ref_view);
5526 152c1c93 2020-11-29 stsp return err;
5528 e78dc838 2020-12-04 stsp view->focussed = 0;
5529 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5530 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5531 152c1c93 2020-11-29 stsp err = view_close_child(view);
5533 152c1c93 2020-11-29 stsp return err;
5534 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5535 e78dc838 2020-12-04 stsp view->focus_child = 1;
5537 152c1c93 2020-11-29 stsp *new_view = ref_view;
5539 e4526bf5 2021-09-03 naddy case 'g':
5540 e4526bf5 2021-09-03 naddy case KEY_HOME:
5541 e4526bf5 2021-09-03 naddy s->selected = 0;
5542 e4526bf5 2021-09-03 naddy if (s->tree == s->root)
5543 e4526bf5 2021-09-03 naddy s->first_displayed_entry =
5544 e4526bf5 2021-09-03 naddy got_object_tree_get_first_entry(s->tree);
5546 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5548 e4526bf5 2021-09-03 naddy case 'G':
5549 e4526bf5 2021-09-03 naddy case KEY_END:
5550 e4526bf5 2021-09-03 naddy s->selected = 0;
5551 e4526bf5 2021-09-03 naddy te = got_object_tree_get_last_entry(s->tree);
5552 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 3; n++) {
5553 e4526bf5 2021-09-03 naddy if (te == NULL) {
5554 e4526bf5 2021-09-03 naddy if(s->tree != s->root) {
5555 e4526bf5 2021-09-03 naddy s->first_displayed_entry = NULL;
5560 e4526bf5 2021-09-03 naddy s->first_displayed_entry = te;
5561 e4526bf5 2021-09-03 naddy te = got_tree_entry_get_prev(s->tree, te);
5563 e4526bf5 2021-09-03 naddy if (n > 0)
5564 e4526bf5 2021-09-03 naddy s->selected = n - 1;
5567 1e37a5c2 2019-05-12 jcs case KEY_UP:
5568 f7140bf5 2021-10-17 thomas case CTRL('p'):
5569 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5570 1e37a5c2 2019-05-12 jcs s->selected--;
5573 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5575 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5576 ea025d1d 2020-02-22 naddy case CTRL('b'):
5577 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5578 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5579 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5580 fa86c4bf 2020-11-29 stsp s->selected = 0;
5582 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5583 fa86c4bf 2020-11-29 stsp s->selected = 0;
5585 3e135950 2020-12-01 naddy tree_scroll_up(s, MAX(0, view->nlines - 3));
5588 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5589 f7140bf5 2021-10-17 thomas case CTRL('n'):
5590 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5591 1e37a5c2 2019-05-12 jcs s->selected++;
5594 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5596 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5598 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5600 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5601 ea025d1d 2020-02-22 naddy case CTRL('f'):
5602 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5604 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5605 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5606 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5609 3e135950 2020-12-01 naddy tree_scroll_down(s, view->nlines - 3);
5611 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5613 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5614 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5615 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5616 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5617 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5619 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5620 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5622 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5623 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5624 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5625 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5626 1e37a5c2 2019-05-12 jcs s->selected_entry =
5627 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5628 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5629 1e37a5c2 2019-05-12 jcs free(parent);
5630 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5631 56e0773d 2019-11-28 stsp s->selected_entry))) {
5632 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5633 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5634 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5637 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5638 941e9f74 2019-05-21 stsp if (err) {
5639 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5642 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5643 56e0773d 2019-11-28 stsp s->selected_entry))) {
5644 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5645 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5646 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5648 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5649 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5650 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5653 e78dc838 2020-12-04 stsp view->focussed = 0;
5654 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5655 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5656 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5658 669b5ffa 2018-10-07 stsp return err;
5659 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5660 e78dc838 2020-12-04 stsp view->focus_child = 1;
5662 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5665 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5666 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5667 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5673 ffd1d5e5 2018-06-23 stsp return err;
5676 ffd1d5e5 2018-06-23 stsp __dead static void
5677 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5680 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5681 ffd1d5e5 2018-06-23 stsp getprogname());
5685 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5686 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5688 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5689 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5690 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5691 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5692 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5693 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
5694 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5695 4e97c21c 2020-12-06 stsp char *label = NULL;
5696 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5697 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5699 5221c383 2018-08-01 stsp struct tog_view *view;
5701 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5702 ffd1d5e5 2018-06-23 stsp switch (ch) {
5704 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5707 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5708 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5709 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5713 e99e2d15 2020-11-24 naddy usage_tree();
5714 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5718 ffd1d5e5 2018-06-23 stsp argc -= optind;
5719 ffd1d5e5 2018-06-23 stsp argv += optind;
5721 55cccc34 2020-02-20 stsp if (argc > 1)
5722 e99e2d15 2020-11-24 naddy usage_tree();
5724 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5725 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
5726 c156c7a4 2020-12-18 stsp if (cwd == NULL)
5727 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
5728 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
5729 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5730 c156c7a4 2020-12-18 stsp goto done;
5731 55cccc34 2020-02-20 stsp if (worktree)
5732 52185f70 2019-02-05 stsp repo_path =
5733 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5735 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5736 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
5737 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
5738 c156c7a4 2020-12-18 stsp goto done;
5742 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5743 c02c541e 2019-03-29 stsp if (error != NULL)
5744 52185f70 2019-02-05 stsp goto done;
5746 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5747 55cccc34 2020-02-20 stsp repo, worktree);
5748 55cccc34 2020-02-20 stsp if (error)
5749 55cccc34 2020-02-20 stsp goto done;
5751 55cccc34 2020-02-20 stsp init_curses();
5753 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5754 c02c541e 2019-03-29 stsp if (error)
5755 52185f70 2019-02-05 stsp goto done;
5757 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
5758 51a10b52 2020-12-26 stsp if (error)
5759 51a10b52 2020-12-26 stsp goto done;
5761 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5762 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5763 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5764 84de9106 2020-12-26 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5765 4e97c21c 2020-12-06 stsp if (error)
5766 4e97c21c 2020-12-06 stsp goto done;
5767 4e97c21c 2020-12-06 stsp head_ref_name = label;
5769 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5770 4e97c21c 2020-12-06 stsp if (error == NULL)
5771 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5772 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5773 4e97c21c 2020-12-06 stsp goto done;
5774 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5775 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5776 4e97c21c 2020-12-06 stsp if (error)
5777 4e97c21c 2020-12-06 stsp goto done;
5780 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
5781 945f9229 2022-04-16 thomas if (error)
5782 945f9229 2022-04-16 thomas goto done;
5784 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5785 5221c383 2018-08-01 stsp if (view == NULL) {
5786 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5787 5221c383 2018-08-01 stsp goto done;
5789 bc573f3b 2021-07-10 stsp error = open_tree_view(view, commit_id, head_ref_name, repo);
5790 ad80ab7b 2018-08-04 stsp if (error)
5791 ad80ab7b 2018-08-04 stsp goto done;
5792 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5793 945f9229 2022-04-16 thomas error = tree_view_walk_path(&view->state.tree, commit,
5794 d91faf3b 2020-12-01 naddy in_repo_path);
5795 55cccc34 2020-02-20 stsp if (error)
5796 55cccc34 2020-02-20 stsp goto done;
5799 55cccc34 2020-02-20 stsp if (worktree) {
5800 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5801 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5802 55cccc34 2020-02-20 stsp worktree = NULL;
5804 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5806 52185f70 2019-02-05 stsp free(repo_path);
5807 e4a0e26d 2020-02-20 stsp free(cwd);
5808 ffd1d5e5 2018-06-23 stsp free(commit_id);
5809 4e97c21c 2020-12-06 stsp free(label);
5811 486cd271 2020-12-06 stsp got_ref_close(ref);
5812 1d0f4054 2021-06-17 stsp if (repo) {
5813 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5814 1d0f4054 2021-06-17 stsp if (error == NULL)
5815 1d0f4054 2021-06-17 stsp error = close_err;
5817 51a10b52 2020-12-26 stsp tog_free_refs();
5818 ffd1d5e5 2018-06-23 stsp return error;
5821 6458efa5 2020-11-24 stsp static const struct got_error *
5822 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5824 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5825 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5827 6458efa5 2020-11-24 stsp s->nrefs = 0;
5828 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(sre, &tog_refs, entry) {
5829 2183bbf6 2022-01-23 thomas if (strncmp(got_ref_get_name(sre->ref),
5830 2183bbf6 2022-01-23 thomas "refs/got/", 9) == 0 &&
5831 2183bbf6 2022-01-23 thomas strncmp(got_ref_get_name(sre->ref),
5832 2183bbf6 2022-01-23 thomas "refs/got/backup/", 16) != 0)
5835 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5836 6458efa5 2020-11-24 stsp if (re == NULL)
5837 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5839 8924d611 2020-12-26 stsp re->ref = got_ref_dup(sre->ref);
5840 8924d611 2020-12-26 stsp if (re->ref == NULL)
5841 8924d611 2020-12-26 stsp return got_error_from_errno("got_ref_dup");
5842 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5843 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5846 8924d611 2020-12-26 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5847 6458efa5 2020-11-24 stsp return NULL;
5851 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5853 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5855 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5856 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5857 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5858 8924d611 2020-12-26 stsp got_ref_close(re->ref);
5863 6458efa5 2020-11-24 stsp static const struct got_error *
5864 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5866 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5867 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5869 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5870 6458efa5 2020-11-24 stsp s->repo = repo;
5872 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5873 dbdddfee 2021-06-23 naddy STAILQ_INIT(&s->colors);
5875 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5877 6458efa5 2020-11-24 stsp return err;
5879 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5880 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5881 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5882 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5884 6458efa5 2020-11-24 stsp goto done;
5886 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5887 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5888 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5890 6458efa5 2020-11-24 stsp goto done;
5892 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5893 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5894 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5895 2183bbf6 2022-01-23 thomas if (err)
5896 2183bbf6 2022-01-23 thomas goto done;
5898 2183bbf6 2022-01-23 thomas err = add_color(&s->colors, "^refs/got/backup/",
5899 2183bbf6 2022-01-23 thomas TOG_COLOR_REFS_BACKUP,
5900 2183bbf6 2022-01-23 thomas get_color_value("TOG_COLOR_REFS_BACKUP"));
5902 6458efa5 2020-11-24 stsp goto done;
5905 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5906 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5907 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5908 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5909 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5912 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5913 6458efa5 2020-11-24 stsp return err;
5916 6458efa5 2020-11-24 stsp static const struct got_error *
5917 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5919 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5921 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5922 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5924 6458efa5 2020-11-24 stsp return NULL;
5927 6458efa5 2020-11-24 stsp static const struct got_error *
5928 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5929 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5931 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5932 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5933 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5934 6458efa5 2020-11-24 stsp int obj_type;
5936 c42c9805 2020-11-24 stsp *commit_id = NULL;
5938 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5940 6458efa5 2020-11-24 stsp return err;
5942 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5944 6458efa5 2020-11-24 stsp goto done;
5946 6458efa5 2020-11-24 stsp switch (obj_type) {
5947 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
5948 c42c9805 2020-11-24 stsp *commit_id = obj_id;
5950 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
5951 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
5953 6458efa5 2020-11-24 stsp goto done;
5954 c42c9805 2020-11-24 stsp free(obj_id);
5955 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
5956 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5958 6458efa5 2020-11-24 stsp goto done;
5959 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5960 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5961 c42c9805 2020-11-24 stsp goto done;
5963 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
5964 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5965 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
5966 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
5967 c42c9805 2020-11-24 stsp goto done;
5971 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5977 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
5978 c42c9805 2020-11-24 stsp if (err) {
5979 c42c9805 2020-11-24 stsp free(*commit_id);
5980 c42c9805 2020-11-24 stsp *commit_id = NULL;
5982 c42c9805 2020-11-24 stsp return err;
5985 c42c9805 2020-11-24 stsp static const struct got_error *
5986 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
5987 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5989 c42c9805 2020-11-24 stsp struct tog_view *log_view;
5990 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
5991 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
5993 c42c9805 2020-11-24 stsp *new_view = NULL;
5995 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
5996 c42c9805 2020-11-24 stsp if (err) {
5997 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
5998 c42c9805 2020-11-24 stsp return err;
6000 c42c9805 2020-11-24 stsp return NULL;
6003 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6004 6458efa5 2020-11-24 stsp if (log_view == NULL) {
6005 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
6006 6458efa5 2020-11-24 stsp goto done;
6009 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
6010 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
6013 6458efa5 2020-11-24 stsp view_close(log_view);
6015 6458efa5 2020-11-24 stsp *new_view = log_view;
6016 c42c9805 2020-11-24 stsp free(commit_id);
6017 6458efa5 2020-11-24 stsp return err;
6020 ce5b7c56 2019-07-09 stsp static void
6021 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6023 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
6024 34ba6917 2020-11-30 stsp int i = 0;
6026 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6029 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6030 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
6031 34ba6917 2020-11-30 stsp if (re == NULL)
6033 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
6034 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6038 34ba6917 2020-11-30 stsp static void
6039 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6041 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
6042 6458efa5 2020-11-24 stsp int n = 0;
6044 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
6045 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
6047 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
6049 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
6050 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
6051 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
6052 6458efa5 2020-11-24 stsp if (last) {
6053 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
6054 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
6059 6458efa5 2020-11-24 stsp static const struct got_error *
6060 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
6062 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6064 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
6065 6458efa5 2020-11-24 stsp return NULL;
6068 6458efa5 2020-11-24 stsp static int
6069 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6071 6458efa5 2020-11-24 stsp regmatch_t regmatch;
6073 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6077 6458efa5 2020-11-24 stsp static const struct got_error *
6078 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
6080 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6081 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
6083 6458efa5 2020-11-24 stsp if (!view->searching) {
6084 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6085 6458efa5 2020-11-24 stsp return NULL;
6088 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6089 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
6090 6458efa5 2020-11-24 stsp if (s->selected_entry)
6091 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
6093 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6094 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6096 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
6097 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6099 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
6100 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
6103 4b3f9dac 2021-12-17 thomas if (s->selected_entry)
6104 4b3f9dac 2021-12-17 thomas re = s->selected_entry;
6105 4b3f9dac 2021-12-17 thomas else if (view->searching == TOG_SEARCH_FORWARD)
6106 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6108 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6111 6458efa5 2020-11-24 stsp while (1) {
6112 6458efa5 2020-11-24 stsp if (re == NULL) {
6113 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
6114 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6115 6458efa5 2020-11-24 stsp return NULL;
6117 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6118 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
6120 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
6123 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
6124 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
6125 6458efa5 2020-11-24 stsp s->matched_entry = re;
6129 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
6130 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6132 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
6135 6458efa5 2020-11-24 stsp if (s->matched_entry) {
6136 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
6137 6458efa5 2020-11-24 stsp s->selected = 0;
6140 6458efa5 2020-11-24 stsp return NULL;
6143 6458efa5 2020-11-24 stsp static const struct got_error *
6144 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
6146 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6147 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6148 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
6149 6458efa5 2020-11-24 stsp char *line = NULL;
6150 6458efa5 2020-11-24 stsp wchar_t *wline;
6151 6458efa5 2020-11-24 stsp struct tog_color *tc;
6152 6458efa5 2020-11-24 stsp int width, n;
6153 6458efa5 2020-11-24 stsp int limit = view->nlines;
6155 6458efa5 2020-11-24 stsp werase(view->window);
6157 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
6159 6458efa5 2020-11-24 stsp if (limit == 0)
6160 6458efa5 2020-11-24 stsp return NULL;
6162 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
6164 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6165 6458efa5 2020-11-24 stsp s->nrefs) == -1)
6166 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6168 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6169 6458efa5 2020-11-24 stsp if (err) {
6170 6458efa5 2020-11-24 stsp free(line);
6171 6458efa5 2020-11-24 stsp return err;
6173 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6174 6458efa5 2020-11-24 stsp wstandout(view->window);
6175 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6176 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
6177 6458efa5 2020-11-24 stsp wstandend(view->window);
6178 6458efa5 2020-11-24 stsp free(wline);
6179 6458efa5 2020-11-24 stsp wline = NULL;
6180 6458efa5 2020-11-24 stsp free(line);
6181 6458efa5 2020-11-24 stsp line = NULL;
6182 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6183 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6184 6458efa5 2020-11-24 stsp if (--limit <= 0)
6185 6458efa5 2020-11-24 stsp return NULL;
6188 6458efa5 2020-11-24 stsp while (re && limit > 0) {
6189 6458efa5 2020-11-24 stsp char *line = NULL;
6191 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
6192 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
6193 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
6194 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
6195 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
6196 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
6197 6458efa5 2020-11-24 stsp struct got_object_id *id;
6198 6458efa5 2020-11-24 stsp char *id_str;
6199 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
6201 6458efa5 2020-11-24 stsp return err;
6202 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
6203 6458efa5 2020-11-24 stsp if (err) {
6205 6458efa5 2020-11-24 stsp return err;
6207 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
6208 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
6209 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
6211 6458efa5 2020-11-24 stsp free(id_str);
6212 6458efa5 2020-11-24 stsp return err;
6215 6458efa5 2020-11-24 stsp free(id_str);
6217 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
6218 6458efa5 2020-11-24 stsp if (line == NULL)
6219 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
6222 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
6223 6458efa5 2020-11-24 stsp if (err) {
6224 6458efa5 2020-11-24 stsp free(line);
6225 6458efa5 2020-11-24 stsp return err;
6227 6458efa5 2020-11-24 stsp if (n == s->selected) {
6228 6458efa5 2020-11-24 stsp if (view->focussed)
6229 6458efa5 2020-11-24 stsp wstandout(view->window);
6230 6458efa5 2020-11-24 stsp s->selected_entry = re;
6232 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6234 6458efa5 2020-11-24 stsp wattr_on(view->window,
6235 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6236 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6238 6458efa5 2020-11-24 stsp wattr_off(view->window,
6239 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6240 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6241 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6242 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6243 6458efa5 2020-11-24 stsp wstandend(view->window);
6244 6458efa5 2020-11-24 stsp free(line);
6245 6458efa5 2020-11-24 stsp free(wline);
6246 6458efa5 2020-11-24 stsp wline = NULL;
6248 6458efa5 2020-11-24 stsp s->ndisplayed++;
6249 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6252 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6255 6458efa5 2020-11-24 stsp view_vborder(view);
6256 6458efa5 2020-11-24 stsp return err;
6259 6458efa5 2020-11-24 stsp static const struct got_error *
6260 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6261 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6263 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6264 bc573f3b 2021-07-10 stsp struct got_object_id *commit_id = NULL;
6265 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6267 c42c9805 2020-11-24 stsp *new_view = NULL;
6269 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6270 c42c9805 2020-11-24 stsp if (err) {
6271 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6272 c42c9805 2020-11-24 stsp return err;
6274 c42c9805 2020-11-24 stsp return NULL;
6278 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6279 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6280 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6281 c42c9805 2020-11-24 stsp goto done;
6284 bc573f3b 2021-07-10 stsp err = open_tree_view(tree_view, commit_id,
6285 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6287 c42c9805 2020-11-24 stsp goto done;
6289 c42c9805 2020-11-24 stsp *new_view = tree_view;
6291 c42c9805 2020-11-24 stsp free(commit_id);
6292 c42c9805 2020-11-24 stsp return err;
6294 c42c9805 2020-11-24 stsp static const struct got_error *
6295 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6297 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6298 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6299 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6300 e4526bf5 2021-09-03 naddy struct tog_reflist_entry *re;
6301 e4526bf5 2021-09-03 naddy int begin_x = 0, n;
6303 6458efa5 2020-11-24 stsp switch (ch) {
6305 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6307 98182bd0 2021-11-20 thomas case 'o':
6308 3bfadbd4 2021-11-20 thomas s->sort_by_date = !s->sort_by_date;
6309 c591440f 2021-11-20 thomas err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6310 c591440f 2021-11-20 thomas got_ref_cmp_by_commit_timestamp_descending :
6311 2183bbf6 2022-01-23 thomas tog_ref_cmp_by_name, s->repo);
6312 c591440f 2021-11-20 thomas if (err)
6314 c591440f 2021-11-20 thomas got_reflist_object_id_map_free(tog_refs_idmap);
6315 c591440f 2021-11-20 thomas err = got_reflist_object_id_map_create(&tog_refs_idmap,
6316 c591440f 2021-11-20 thomas &tog_refs, s->repo);
6317 3bfadbd4 2021-11-20 thomas if (err)
6319 3bfadbd4 2021-11-20 thomas ref_view_free_refs(s);
6320 3bfadbd4 2021-11-20 thomas err = ref_view_load_refs(s);
6322 6458efa5 2020-11-24 stsp case KEY_ENTER:
6323 6458efa5 2020-11-24 stsp case '\r':
6324 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6326 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6327 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6328 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6330 e78dc838 2020-12-04 stsp view->focussed = 0;
6331 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6332 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6333 6458efa5 2020-11-24 stsp err = view_close_child(view);
6335 6458efa5 2020-11-24 stsp return err;
6336 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6337 e78dc838 2020-12-04 stsp view->focus_child = 1;
6339 6458efa5 2020-11-24 stsp *new_view = log_view;
6342 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6344 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6345 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6346 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6348 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6350 e78dc838 2020-12-04 stsp view->focussed = 0;
6351 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6352 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6353 c42c9805 2020-11-24 stsp err = view_close_child(view);
6355 c42c9805 2020-11-24 stsp return err;
6356 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6357 e78dc838 2020-12-04 stsp view->focus_child = 1;
6359 c42c9805 2020-11-24 stsp *new_view = tree_view;
6361 e4526bf5 2021-09-03 naddy case 'g':
6362 e4526bf5 2021-09-03 naddy case KEY_HOME:
6363 e4526bf5 2021-09-03 naddy s->selected = 0;
6364 e4526bf5 2021-09-03 naddy s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6366 e4526bf5 2021-09-03 naddy case 'G':
6367 e4526bf5 2021-09-03 naddy case KEY_END:
6368 e4526bf5 2021-09-03 naddy s->selected = 0;
6369 e4526bf5 2021-09-03 naddy re = TAILQ_LAST(&s->refs, tog_reflist_head);
6370 e4526bf5 2021-09-03 naddy for (n = 0; n < view->nlines - 1; n++) {
6371 e4526bf5 2021-09-03 naddy if (re == NULL)
6373 e4526bf5 2021-09-03 naddy s->first_displayed_entry = re;
6374 e4526bf5 2021-09-03 naddy re = TAILQ_PREV(re, tog_reflist_head, entry);
6376 e4526bf5 2021-09-03 naddy if (n > 0)
6377 e4526bf5 2021-09-03 naddy s->selected = n - 1;
6380 6458efa5 2020-11-24 stsp case KEY_UP:
6381 f7140bf5 2021-10-17 thomas case CTRL('p'):
6382 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6383 6458efa5 2020-11-24 stsp s->selected--;
6386 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6388 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6389 6458efa5 2020-11-24 stsp case CTRL('b'):
6390 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6391 34ba6917 2020-11-30 stsp s->selected = 0;
6392 3e135950 2020-12-01 naddy ref_scroll_up(s, MAX(0, view->nlines - 1));
6395 6458efa5 2020-11-24 stsp case KEY_DOWN:
6396 f7140bf5 2021-10-17 thomas case CTRL('n'):
6397 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6398 6458efa5 2020-11-24 stsp s->selected++;
6401 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6402 6458efa5 2020-11-24 stsp /* can't scroll any further */
6404 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6406 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6407 6458efa5 2020-11-24 stsp case CTRL('f'):
6408 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6409 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6410 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6411 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6414 3e135950 2020-12-01 naddy ref_scroll_down(s, view->nlines - 1);
6416 6458efa5 2020-11-24 stsp case CTRL('l'):
6417 8924d611 2020-12-26 stsp tog_free_refs();
6418 3bfadbd4 2021-11-20 thomas err = tog_load_refs(s->repo, s->sort_by_date);
6421 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6422 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6424 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6425 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6426 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6432 6458efa5 2020-11-24 stsp return err;
6435 6458efa5 2020-11-24 stsp __dead static void
6436 6458efa5 2020-11-24 stsp usage_ref(void)
6439 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6440 6458efa5 2020-11-24 stsp getprogname());
6444 6458efa5 2020-11-24 stsp static const struct got_error *
6445 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6447 6458efa5 2020-11-24 stsp const struct got_error *error;
6448 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6449 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6450 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6452 6458efa5 2020-11-24 stsp struct tog_view *view;
6454 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6455 6458efa5 2020-11-24 stsp switch (ch) {
6457 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6458 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6459 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6463 e99e2d15 2020-11-24 naddy usage_ref();
6464 6458efa5 2020-11-24 stsp /* NOTREACHED */
6468 6458efa5 2020-11-24 stsp argc -= optind;
6469 6458efa5 2020-11-24 stsp argv += optind;
6471 6458efa5 2020-11-24 stsp if (argc > 1)
6472 e99e2d15 2020-11-24 naddy usage_ref();
6474 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6475 c156c7a4 2020-12-18 stsp cwd = getcwd(NULL, 0);
6476 c156c7a4 2020-12-18 stsp if (cwd == NULL)
6477 c156c7a4 2020-12-18 stsp return got_error_from_errno("getcwd");
6478 c156c7a4 2020-12-18 stsp error = got_worktree_open(&worktree, cwd);
6479 c156c7a4 2020-12-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6480 c156c7a4 2020-12-18 stsp goto done;
6481 6458efa5 2020-11-24 stsp if (worktree)
6482 6458efa5 2020-11-24 stsp repo_path =
6483 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6485 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6486 c156c7a4 2020-12-18 stsp if (repo_path == NULL) {
6487 c156c7a4 2020-12-18 stsp error = got_error_from_errno("strdup");
6488 c156c7a4 2020-12-18 stsp goto done;
6492 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6493 6458efa5 2020-11-24 stsp if (error != NULL)
6494 6458efa5 2020-11-24 stsp goto done;
6496 6458efa5 2020-11-24 stsp init_curses();
6498 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6499 51a10b52 2020-12-26 stsp if (error)
6500 51a10b52 2020-12-26 stsp goto done;
6502 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6503 6458efa5 2020-11-24 stsp if (error)
6504 6458efa5 2020-11-24 stsp goto done;
6506 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6507 6458efa5 2020-11-24 stsp if (view == NULL) {
6508 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6509 6458efa5 2020-11-24 stsp goto done;
6512 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6513 6458efa5 2020-11-24 stsp if (error)
6514 6458efa5 2020-11-24 stsp goto done;
6516 6458efa5 2020-11-24 stsp if (worktree) {
6517 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6518 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6519 6458efa5 2020-11-24 stsp worktree = NULL;
6521 6458efa5 2020-11-24 stsp error = view_loop(view);
6523 6458efa5 2020-11-24 stsp free(repo_path);
6524 6458efa5 2020-11-24 stsp free(cwd);
6525 1d0f4054 2021-06-17 stsp if (repo) {
6526 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6527 1d0f4054 2021-06-17 stsp if (close_err)
6528 1d0f4054 2021-06-17 stsp error = close_err;
6530 51a10b52 2020-12-26 stsp tog_free_refs();
6531 6458efa5 2020-11-24 stsp return error;
6534 6458efa5 2020-11-24 stsp static void
6535 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6539 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6540 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6541 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = &tog_commands[i];
6542 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6544 6879ba42 2020-10-01 naddy fputc('\n', fp);
6547 4ed7e80c 2018-05-20 stsp __dead static void
6548 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6550 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6552 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6553 6879ba42 2020-10-01 naddy getprogname());
6554 6879ba42 2020-10-01 naddy if (hflag) {
6555 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6556 6879ba42 2020-10-01 naddy list_commands(fp);
6558 6879ba42 2020-10-01 naddy exit(status);
6561 c2301be8 2018-04-30 stsp static char **
6562 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6564 ee85c5e8 2020-02-29 stsp va_list ap;
6565 c2301be8 2018-04-30 stsp char **argv;
6568 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6570 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6571 c2301be8 2018-04-30 stsp if (argv == NULL)
6572 c2301be8 2018-04-30 stsp err(1, "calloc");
6573 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6574 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6575 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6576 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6579 ee85c5e8 2020-02-29 stsp va_end(ap);
6580 c2301be8 2018-04-30 stsp return argv;
6584 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6585 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6586 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6587 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6588 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6590 ee85c5e8 2020-02-29 stsp static const struct got_error *
6591 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6593 1d0f4054 2021-06-17 stsp const struct got_error *error = NULL, *close_err;
6594 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
6595 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6596 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6597 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6598 945f9229 2022-04-16 thomas struct got_commit_object *commit = NULL;
6599 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6600 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6602 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6603 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6604 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6606 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6607 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6608 ee85c5e8 2020-02-29 stsp goto done;
6610 ee85c5e8 2020-02-29 stsp if (worktree)
6611 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6613 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6614 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6615 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6616 ee85c5e8 2020-02-29 stsp goto done;
6619 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6620 ee85c5e8 2020-02-29 stsp if (error != NULL)
6621 ee85c5e8 2020-02-29 stsp goto done;
6623 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6624 ee85c5e8 2020-02-29 stsp repo, worktree);
6625 ee85c5e8 2020-02-29 stsp if (error)
6626 ee85c5e8 2020-02-29 stsp goto done;
6628 3bfadbd4 2021-11-20 thomas error = tog_load_refs(repo, 0);
6629 84de9106 2020-12-26 stsp if (error)
6630 84de9106 2020-12-26 stsp goto done;
6631 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6632 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6633 87670572 2020-12-26 naddy GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6634 ee85c5e8 2020-02-29 stsp if (error)
6635 ee85c5e8 2020-02-29 stsp goto done;
6637 ee85c5e8 2020-02-29 stsp if (worktree) {
6638 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6639 ee85c5e8 2020-02-29 stsp worktree = NULL;
6642 945f9229 2022-04-16 thomas error = got_object_open_as_commit(&commit, repo, commit_id);
6643 945f9229 2022-04-16 thomas if (error)
6644 945f9229 2022-04-16 thomas goto done;
6646 945f9229 2022-04-16 thomas error = got_object_id_by_path(&id, repo, commit, in_repo_path);
6647 ee85c5e8 2020-02-29 stsp if (error) {
6648 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6649 ee85c5e8 2020-02-29 stsp goto done;
6650 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6651 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6652 6879ba42 2020-10-01 naddy usage(1, 1);
6653 ee85c5e8 2020-02-29 stsp /* not reached */
6656 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6657 1d0f4054 2021-06-17 stsp if (error == NULL)
6658 1d0f4054 2021-06-17 stsp error = close_err;
6659 ee85c5e8 2020-02-29 stsp repo = NULL;
6661 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6662 ee85c5e8 2020-02-29 stsp if (error)
6663 ee85c5e8 2020-02-29 stsp goto done;
6665 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6667 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6668 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6670 1d0f4054 2021-06-17 stsp if (repo) {
6671 1d0f4054 2021-06-17 stsp close_err = got_repo_close(repo);
6672 1d0f4054 2021-06-17 stsp if (error == NULL)
6673 1d0f4054 2021-06-17 stsp error = close_err;
6675 945f9229 2022-04-16 thomas if (commit)
6676 945f9229 2022-04-16 thomas got_object_commit_close(commit);
6677 ee85c5e8 2020-02-29 stsp if (worktree)
6678 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6680 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6681 ee85c5e8 2020-02-29 stsp free(commit_id);
6682 ee85c5e8 2020-02-29 stsp free(cwd);
6683 ee85c5e8 2020-02-29 stsp free(repo_path);
6684 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6685 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6687 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6688 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6689 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6691 87670572 2020-12-26 naddy tog_free_refs();
6692 ee85c5e8 2020-02-29 stsp return error;
6696 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6698 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6699 641a8ee6 2022-02-16 thomas const struct tog_cmd *cmd = NULL;
6700 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6701 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6702 641a8ee6 2022-02-16 thomas static const struct option longopts[] = {
6703 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6704 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6707 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6709 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6710 9f7d7167 2018-04-29 stsp switch (ch) {
6712 9f7d7167 2018-04-29 stsp hflag = 1;
6715 53ccebc2 2019-07-30 stsp Vflag = 1;
6718 6879ba42 2020-10-01 naddy usage(hflag, 1);
6719 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6723 9f7d7167 2018-04-29 stsp argc -= optind;
6724 9f7d7167 2018-04-29 stsp argv += optind;
6725 9814e6a3 2020-09-27 naddy optind = 1;
6726 c2301be8 2018-04-30 stsp optreset = 1;
6728 53ccebc2 2019-07-30 stsp if (Vflag) {
6729 53ccebc2 2019-07-30 stsp got_version_print_str();
6730 6879ba42 2020-10-01 naddy return 0;
6733 4010e238 2020-12-04 stsp #ifndef PROFILE
6734 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6735 4010e238 2020-12-04 stsp NULL) == -1)
6736 4010e238 2020-12-04 stsp err(1, "pledge");
6739 c2301be8 2018-04-30 stsp if (argc == 0) {
6740 f29d3e89 2018-06-23 stsp if (hflag)
6741 6879ba42 2020-10-01 naddy usage(hflag, 0);
6742 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6743 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6745 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6749 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6750 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6751 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6752 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6753 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6759 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6760 ee85c5e8 2020-02-29 stsp if (argc != 1)
6761 6879ba42 2020-10-01 naddy usage(0, 1);
6762 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6763 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6765 ee85c5e8 2020-02-29 stsp if (hflag)
6766 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6768 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6772 b46c1e04 2020-09-20 naddy putchar('\n');
6773 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6775 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6776 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6777 a2f4a359 2020-02-28 stsp free(cmd_argv);
6780 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6781 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);