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 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
21 0d6c6ee3 2020-05-20 stsp #include <ctype.h>
22 31120ada 2018-04-30 stsp #include <errno.h>
23 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
24 9f7d7167 2018-04-29 stsp #include <curses.h>
25 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
26 9f7d7167 2018-04-29 stsp #include <panel.h>
27 9f7d7167 2018-04-29 stsp #include <locale.h>
28 61266923 2020-01-14 stsp #include <signal.h>
29 9f7d7167 2018-04-29 stsp #include <stdlib.h>
30 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
31 26ed57b2 2018-05-19 stsp #include <stdio.h>
32 9f7d7167 2018-04-29 stsp #include <getopt.h>
33 9f7d7167 2018-04-29 stsp #include <string.h>
34 9f7d7167 2018-04-29 stsp #include <err.h>
35 80ddbec8 2018-04-29 stsp #include <unistd.h>
36 26ed57b2 2018-05-19 stsp #include <limits.h>
37 61e69b96 2018-05-20 stsp #include <wchar.h>
38 788c352e 2018-06-16 stsp #include <time.h>
39 84451b3e 2018-07-10 stsp #include <pthread.h>
40 5036bf37 2018-09-24 stsp #include <libgen.h>
41 60493ae3 2019-06-20 stsp #include <regex.h>
43 53ccebc2 2019-07-30 stsp #include "got_version.h"
44 9f7d7167 2018-04-29 stsp #include "got_error.h"
45 80ddbec8 2018-04-29 stsp #include "got_object.h"
46 80ddbec8 2018-04-29 stsp #include "got_reference.h"
47 80ddbec8 2018-04-29 stsp #include "got_repository.h"
48 80ddbec8 2018-04-29 stsp #include "got_diff.h"
49 511a516b 2018-05-19 stsp #include "got_opentemp.h"
50 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
51 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
52 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
53 a70480e0 2018-06-23 stsp #include "got_blame.h"
54 c2db6724 2019-01-04 stsp #include "got_privsep.h"
55 1dd54920 2019-05-11 stsp #include "got_path.h"
56 b7165be3 2019-02-05 stsp #include "got_worktree.h"
59 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
66 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
68 9f7d7167 2018-04-29 stsp #ifndef nitems
69 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
72 9f7d7167 2018-04-29 stsp struct tog_cmd {
73 c2301be8 2018-04-30 stsp const char *name;
74 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
75 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
78 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
79 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
82 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
83 6458efa5 2020-11-24 stsp __dead static void usage_ref(void);
85 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
88 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
89 6458efa5 2020-11-24 stsp static const struct got_error* cmd_ref(int, char *[]);
91 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
92 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
93 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
94 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
95 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
96 6458efa5 2020-11-24 stsp { "ref", cmd_ref, usage_ref },
99 d6b05b5b 2018-08-04 stsp enum tog_view_type {
100 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
101 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
102 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
103 6458efa5 2020-11-24 stsp TOG_VIEW_TREE,
104 6458efa5 2020-11-24 stsp TOG_VIEW_REF,
107 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
109 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
110 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
111 ba4f502b 2018-08-04 stsp struct got_object_id *id;
112 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
115 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
116 ba4f502b 2018-08-04 stsp struct commit_queue {
117 ba4f502b 2018-08-04 stsp int ncommits;
118 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
121 f26dddb7 2019-11-08 stsp struct tog_color {
122 f26dddb7 2019-11-08 stsp SIMPLEQ_ENTRY(tog_color) entry;
123 6d17833f 2019-11-08 stsp regex_t regex;
124 6d17833f 2019-11-08 stsp short colorpair;
126 f26dddb7 2019-11-08 stsp SIMPLEQ_HEAD(tog_colors, tog_color);
128 11b20872 2019-11-08 stsp static const struct got_error *
129 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
130 11b20872 2019-11-08 stsp int idx, short color)
132 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
133 11b20872 2019-11-08 stsp struct tog_color *tc;
134 11b20872 2019-11-08 stsp int regerr = 0;
136 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
137 11b20872 2019-11-08 stsp return NULL;
139 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
141 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
142 11b20872 2019-11-08 stsp if (tc == NULL)
143 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
144 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
145 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
146 11b20872 2019-11-08 stsp if (regerr) {
147 11b20872 2019-11-08 stsp static char regerr_msg[512];
148 11b20872 2019-11-08 stsp static char err_msg[512];
149 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
150 11b20872 2019-11-08 stsp sizeof(regerr_msg));
151 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
152 11b20872 2019-11-08 stsp regerr_msg);
153 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
155 11b20872 2019-11-08 stsp return err;
157 11b20872 2019-11-08 stsp tc->colorpair = idx;
158 11b20872 2019-11-08 stsp SIMPLEQ_INSERT_HEAD(colors, tc, entry);
159 11b20872 2019-11-08 stsp return NULL;
162 11b20872 2019-11-08 stsp static void
163 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
165 11b20872 2019-11-08 stsp struct tog_color *tc;
167 11b20872 2019-11-08 stsp while (!SIMPLEQ_EMPTY(colors)) {
168 11b20872 2019-11-08 stsp tc = SIMPLEQ_FIRST(colors);
169 11b20872 2019-11-08 stsp SIMPLEQ_REMOVE_HEAD(colors, entry);
170 11b20872 2019-11-08 stsp regfree(&tc->regex);
175 11b20872 2019-11-08 stsp struct tog_color *
176 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
178 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
180 11b20872 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
181 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
185 11b20872 2019-11-08 stsp return NULL;
189 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
191 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
192 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
193 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
194 11b20872 2019-11-08 stsp return COLOR_CYAN;
195 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
196 11b20872 2019-11-08 stsp return COLOR_YELLOW;
197 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
198 11b20872 2019-11-08 stsp return COLOR_GREEN;
199 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
200 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
201 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
202 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
203 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
204 91b8c405 2020-01-25 stsp return COLOR_CYAN;
205 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
206 11b20872 2019-11-08 stsp return COLOR_GREEN;
207 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
208 11b20872 2019-11-08 stsp return COLOR_GREEN;
209 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
210 11b20872 2019-11-08 stsp return COLOR_CYAN;
211 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
212 11b20872 2019-11-08 stsp return COLOR_YELLOW;
213 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
214 6458efa5 2020-11-24 stsp return COLOR_GREEN;
215 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
216 6458efa5 2020-11-24 stsp return COLOR_MAGENTA;
217 6458efa5 2020-11-24 stsp if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
218 6458efa5 2020-11-24 stsp return COLOR_YELLOW;
224 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
226 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
228 11b20872 2019-11-08 stsp if (val == NULL)
229 11b20872 2019-11-08 stsp return default_color_value(envvar);
231 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
232 11b20872 2019-11-08 stsp return COLOR_BLACK;
233 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
234 11b20872 2019-11-08 stsp return COLOR_RED;
235 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
236 11b20872 2019-11-08 stsp return COLOR_GREEN;
237 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
238 11b20872 2019-11-08 stsp return COLOR_YELLOW;
239 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
240 11b20872 2019-11-08 stsp return COLOR_BLUE;
241 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
242 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
243 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
244 11b20872 2019-11-08 stsp return COLOR_CYAN;
245 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
246 11b20872 2019-11-08 stsp return COLOR_WHITE;
247 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
250 11b20872 2019-11-08 stsp return default_color_value(envvar);
254 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
255 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
256 3dbaef42 2020-11-24 stsp const char *label1, *label2;
258 15a087fe 2019-02-21 stsp int first_displayed_line;
259 15a087fe 2019-02-21 stsp int last_displayed_line;
261 15a087fe 2019-02-21 stsp int diff_context;
262 3dbaef42 2020-11-24 stsp int ignore_whitespace;
263 64453f7e 2020-11-21 stsp int force_text_diff;
264 15a087fe 2019-02-21 stsp struct got_repository *repo;
265 78756c87 2020-11-24 stsp struct got_reflist_head refs;
266 bddb1296 2019-11-08 stsp struct tog_colors colors;
267 fe621944 2020-11-10 stsp size_t nlines;
268 f44b1f58 2020-02-02 tracey off_t *line_offsets;
269 f44b1f58 2020-02-02 tracey int matched_line;
270 f44b1f58 2020-02-02 tracey int selected_line;
272 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
273 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
276 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
278 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
279 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
280 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
281 1a76625f 2018-10-22 stsp int commits_needed;
282 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
283 1a76625f 2018-10-22 stsp struct commit_queue *commits;
284 1a76625f 2018-10-22 stsp const char *in_repo_path;
285 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
286 1a76625f 2018-10-22 stsp struct got_repository *repo;
287 1a76625f 2018-10-22 stsp int log_complete;
288 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
289 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
290 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
291 13add988 2019-10-15 stsp int *searching;
292 13add988 2019-10-15 stsp int *search_next_done;
293 13add988 2019-10-15 stsp regex_t *regex;
296 1a76625f 2018-10-22 stsp struct tog_log_view_state {
297 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
298 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
299 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
300 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
301 b01e7d3b 2018-08-04 stsp int selected;
302 b01e7d3b 2018-08-04 stsp char *in_repo_path;
303 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
304 b672a97a 2020-01-27 stsp int log_branches;
305 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
306 78756c87 2020-11-24 stsp struct got_reflist_head refs;
307 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
308 1a76625f 2018-10-22 stsp sig_atomic_t quit;
309 1a76625f 2018-10-22 stsp pthread_t thread;
310 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
311 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
312 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
313 11b20872 2019-11-08 stsp struct tog_colors colors;
316 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
317 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
318 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
319 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
320 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
321 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
322 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
323 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
324 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
325 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
326 bf30f154 2020-12-07 naddy #define TOG_COLOR_DATE 11
327 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_HEADS 12
328 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_TAGS 13
329 6458efa5 2020-11-24 stsp #define TOG_COLOR_REFS_REMOTES 14
331 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
332 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
333 e9424729 2018-08-04 stsp int nlines;
335 e9424729 2018-08-04 stsp struct tog_view *view;
336 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
340 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
341 e9424729 2018-08-04 stsp const char *path;
342 e9424729 2018-08-04 stsp struct got_repository *repo;
343 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
344 e9424729 2018-08-04 stsp int *complete;
345 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
346 fc06ba56 2019-08-22 stsp void *cancel_arg;
349 e9424729 2018-08-04 stsp struct tog_blame {
351 be659d10 2020-11-18 stsp off_t filesize;
352 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
353 6fcac457 2018-11-19 stsp int nlines;
354 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
355 e9424729 2018-08-04 stsp pthread_t thread;
356 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
357 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
358 e9424729 2018-08-04 stsp const char *path;
361 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
362 7cbe629d 2018-08-04 stsp int first_displayed_line;
363 7cbe629d 2018-08-04 stsp int last_displayed_line;
364 7cbe629d 2018-08-04 stsp int selected_line;
365 7cbe629d 2018-08-04 stsp int blame_complete;
368 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
369 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
370 e5a0f69f 2018-08-18 stsp char *path;
371 7cbe629d 2018-08-04 stsp struct got_repository *repo;
372 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
373 e9424729 2018-08-04 stsp struct tog_blame blame;
374 6c4c42e0 2019-06-24 stsp int matched_line;
375 11b20872 2019-11-08 stsp struct tog_colors colors;
378 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
379 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
380 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
381 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
382 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
383 ad80ab7b 2018-08-04 stsp int selected;
386 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
388 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
389 ad80ab7b 2018-08-04 stsp char *tree_label;
390 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
391 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
392 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
393 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
394 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
395 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
396 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
397 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
398 9cd7cbd1 2020-12-07 stsp char *head_ref_name;
399 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
400 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
401 6458efa5 2020-11-24 stsp struct tog_colors colors;
404 6458efa5 2020-11-24 stsp struct tog_reflist_entry {
405 6458efa5 2020-11-24 stsp TAILQ_ENTRY(tog_reflist_entry) entry;
406 6458efa5 2020-11-24 stsp struct got_reference *ref;
410 6458efa5 2020-11-24 stsp TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
412 6458efa5 2020-11-24 stsp struct tog_ref_view_state {
413 6458efa5 2020-11-24 stsp struct got_reflist_head simplerefs; /* SIMPLEQ */
414 6458efa5 2020-11-24 stsp struct tog_reflist_head refs; /* TAILQ */
415 6458efa5 2020-11-24 stsp struct tog_reflist_entry *first_displayed_entry;
416 6458efa5 2020-11-24 stsp struct tog_reflist_entry *last_displayed_entry;
417 6458efa5 2020-11-24 stsp struct tog_reflist_entry *selected_entry;
418 6458efa5 2020-11-24 stsp int nrefs, ndisplayed, selected, show_ids;
419 6458efa5 2020-11-24 stsp struct got_repository *repo;
420 6458efa5 2020-11-24 stsp struct tog_reflist_entry *matched_entry;
421 bddb1296 2019-11-08 stsp struct tog_colors colors;
425 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
427 e78dc838 2020-12-04 stsp * The 'Tab' key switches focus between a parent view and its child view.
428 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
429 669b5ffa 2018-10-07 stsp * there is enough screen estate.
431 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
432 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
434 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
435 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
436 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
438 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
439 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
441 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
443 cc3c9aac 2018-08-01 stsp struct tog_view {
444 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
445 26ed57b2 2018-05-19 stsp WINDOW *window;
446 26ed57b2 2018-05-19 stsp PANEL *panel;
447 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
448 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
449 e78dc838 2020-12-04 stsp int focussed; /* Only set on one parent or child view at a time. */
451 669b5ffa 2018-10-07 stsp struct tog_view *parent;
452 669b5ffa 2018-10-07 stsp struct tog_view *child;
455 e78dc838 2020-12-04 stsp * This flag is initially set on parent views when a new child view
456 e78dc838 2020-12-04 stsp * is created. It gets toggled when the 'Tab' key switches focus
457 e78dc838 2020-12-04 stsp * between parent and child.
458 e78dc838 2020-12-04 stsp * The flag indicates whether focus should be passed on to our child
459 e78dc838 2020-12-04 stsp * view if this parent view gets picked for focus after another parent
460 e78dc838 2020-12-04 stsp * view was closed. This prevents child views from losing focus in such
461 e78dc838 2020-12-04 stsp * situations.
463 e78dc838 2020-12-04 stsp int focus_child;
465 5dc9f4bc 2018-08-04 stsp /* type-specific state */
466 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
468 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
469 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
470 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
471 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
472 6458efa5 2020-11-24 stsp struct tog_ref_view_state ref;
475 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
476 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
477 e78dc838 2020-12-04 stsp struct tog_view *, int);
478 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
480 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
481 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
482 60493ae3 2019-06-20 stsp int searching;
483 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
484 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
485 60493ae3 2019-06-20 stsp int search_next_done;
486 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
487 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
488 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
489 1803e47f 2019-06-22 stsp regex_t regex;
490 41605754 2020-11-12 stsp regmatch_t regmatch;
493 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
494 3dbaef42 2020-11-24 stsp struct got_object_id *, struct got_object_id *,
495 3dbaef42 2020-11-24 stsp const char *, const char *, int, int, int, struct tog_view *,
496 78756c87 2020-11-24 stsp struct got_repository *);
497 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
498 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
499 e78dc838 2020-12-04 stsp struct tog_view *, int);
500 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
501 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
502 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
504 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
505 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *,
506 78756c87 2020-11-24 stsp const char *, const char *, int);
507 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
508 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
509 e78dc838 2020-12-04 stsp struct tog_view *, int);
510 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
511 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
512 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
514 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
515 78756c87 2020-11-24 stsp struct got_object_id *, struct got_repository *);
516 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
517 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
518 e78dc838 2020-12-04 stsp struct tog_view *, int);
519 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
520 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
521 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
523 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
524 4e97c21c 2020-12-06 stsp struct got_tree_object *, struct got_object_id *, const char *,
525 4e97c21c 2020-12-06 stsp struct got_repository *);
526 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
527 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
528 e78dc838 2020-12-04 stsp struct tog_view *, int);
529 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
530 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
531 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
533 6458efa5 2020-11-24 stsp static const struct got_error *open_ref_view(struct tog_view *,
534 6458efa5 2020-11-24 stsp struct got_repository *);
535 6458efa5 2020-11-24 stsp static const struct got_error *show_ref_view(struct tog_view *);
536 6458efa5 2020-11-24 stsp static const struct got_error *input_ref_view(struct tog_view **,
537 e78dc838 2020-12-04 stsp struct tog_view *, int);
538 6458efa5 2020-11-24 stsp static const struct got_error *close_ref_view(struct tog_view *);
539 6458efa5 2020-11-24 stsp static const struct got_error *search_start_ref_view(struct tog_view *);
540 6458efa5 2020-11-24 stsp static const struct got_error *search_next_ref_view(struct tog_view *);
542 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
543 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
544 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
546 25791caa 2018-10-24 stsp static void
547 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
549 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
552 83baff54 2019-08-12 stsp static void
553 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
555 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
558 61266923 2020-01-14 stsp static void
559 61266923 2020-01-14 stsp tog_sigcont(int signo)
561 61266923 2020-01-14 stsp tog_sigcont_received = 1;
564 e5a0f69f 2018-08-18 stsp static const struct got_error *
565 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
567 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
569 669b5ffa 2018-10-07 stsp if (view->child) {
570 669b5ffa 2018-10-07 stsp view_close(view->child);
571 669b5ffa 2018-10-07 stsp view->child = NULL;
573 e5a0f69f 2018-08-18 stsp if (view->close)
574 e5a0f69f 2018-08-18 stsp err = view->close(view);
575 ea5e7bb5 2018-08-01 stsp if (view->panel)
576 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
577 ea5e7bb5 2018-08-01 stsp if (view->window)
578 ea5e7bb5 2018-08-01 stsp delwin(view->window);
579 ea5e7bb5 2018-08-01 stsp free(view);
580 e5a0f69f 2018-08-18 stsp return err;
583 ea5e7bb5 2018-08-01 stsp static struct tog_view *
584 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
585 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
587 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
589 ea5e7bb5 2018-08-01 stsp if (view == NULL)
590 ea5e7bb5 2018-08-01 stsp return NULL;
592 d6b05b5b 2018-08-04 stsp view->type = type;
593 f7d12f7e 2018-08-01 stsp view->lines = LINES;
594 f7d12f7e 2018-08-01 stsp view->cols = COLS;
595 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
596 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
597 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
598 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
599 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
600 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
601 96a765a8 2018-08-04 stsp view_close(view);
602 ea5e7bb5 2018-08-01 stsp return NULL;
604 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
605 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
606 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
607 96a765a8 2018-08-04 stsp view_close(view);
608 ea5e7bb5 2018-08-01 stsp return NULL;
611 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
612 ea5e7bb5 2018-08-01 stsp return view;
616 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
618 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
620 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
623 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
625 5c60c32a 2018-10-18 stsp static const struct got_error *
626 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
628 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
630 5c60c32a 2018-10-18 stsp view->begin_y = 0;
631 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
632 5c60c32a 2018-10-18 stsp view->nlines = LINES;
633 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
634 5c60c32a 2018-10-18 stsp view->lines = LINES;
635 5c60c32a 2018-10-18 stsp view->cols = COLS;
636 5c60c32a 2018-10-18 stsp err = view_resize(view);
638 5c60c32a 2018-10-18 stsp return err;
640 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
641 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
643 5c60c32a 2018-10-18 stsp return NULL;
646 5c60c32a 2018-10-18 stsp static const struct got_error *
647 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
649 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
651 5c60c32a 2018-10-18 stsp view->begin_x = 0;
652 5c60c32a 2018-10-18 stsp view->begin_y = 0;
653 5c60c32a 2018-10-18 stsp view->nlines = LINES;
654 5c60c32a 2018-10-18 stsp view->ncols = COLS;
655 5c60c32a 2018-10-18 stsp view->lines = LINES;
656 5c60c32a 2018-10-18 stsp view->cols = COLS;
657 5c60c32a 2018-10-18 stsp err = view_resize(view);
659 5c60c32a 2018-10-18 stsp return err;
661 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
662 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
664 5c60c32a 2018-10-18 stsp return NULL;
668 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
670 5c60c32a 2018-10-18 stsp return view->parent == NULL;
673 4d8c2215 2018-08-19 stsp static const struct got_error *
674 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
676 f7d12f7e 2018-08-01 stsp int nlines, ncols;
678 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
679 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
681 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
683 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
684 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
686 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
688 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
689 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
690 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
691 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
692 25791caa 2018-10-24 stsp wclear(view->window);
694 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
695 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
696 0cf4efb1 2018-09-29 stsp view->lines = LINES;
697 0cf4efb1 2018-09-29 stsp view->cols = COLS;
699 6e3e5d9c 2018-10-18 stsp if (view->child) {
700 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
701 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
702 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
703 5c60c32a 2018-10-18 stsp if (view->child->focussed)
704 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
706 5c60c32a 2018-10-18 stsp show_panel(view->panel);
708 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
709 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
713 5c60c32a 2018-10-18 stsp return NULL;
716 669b5ffa 2018-10-07 stsp static const struct got_error *
717 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
719 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
721 669b5ffa 2018-10-07 stsp if (view->child == NULL)
722 669b5ffa 2018-10-07 stsp return NULL;
724 669b5ffa 2018-10-07 stsp err = view_close(view->child);
725 669b5ffa 2018-10-07 stsp view->child = NULL;
726 669b5ffa 2018-10-07 stsp return err;
729 72a9cb46 2020-12-03 stsp static void
730 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
732 669b5ffa 2018-10-07 stsp view->child = child;
733 669b5ffa 2018-10-07 stsp child->parent = view;
737 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
739 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
742 34bc9ec9 2019-02-22 stsp static void
743 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
745 25791caa 2018-10-24 stsp int cols, lines;
746 25791caa 2018-10-24 stsp struct winsize size;
748 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
749 25791caa 2018-10-24 stsp cols = 80; /* Default */
750 25791caa 2018-10-24 stsp lines = 24;
752 25791caa 2018-10-24 stsp cols = size.ws_col;
753 25791caa 2018-10-24 stsp lines = size.ws_row;
755 25791caa 2018-10-24 stsp resize_term(lines, cols);
758 2b49a8ae 2019-06-22 stsp static const struct got_error *
759 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
761 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
762 2b49a8ae 2019-06-22 stsp char pattern[1024];
765 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
766 2b49a8ae 2019-06-22 stsp return NULL;
768 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
769 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
771 2b49a8ae 2019-06-22 stsp nocbreak();
773 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
776 2b49a8ae 2019-06-22 stsp if (ret == ERR)
777 2b49a8ae 2019-06-22 stsp return NULL;
779 2b49a8ae 2019-06-22 stsp if (view->searching) {
780 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
781 2b49a8ae 2019-06-22 stsp view->searching = 0;
784 41605754 2020-11-12 stsp if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
785 7c32bd05 2019-06-22 stsp err = view->search_start(view);
787 7c32bd05 2019-06-22 stsp regfree(&view->regex);
788 7c32bd05 2019-06-22 stsp return err;
790 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
791 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
792 2b49a8ae 2019-06-22 stsp view->search_next(view);
795 2b49a8ae 2019-06-22 stsp return NULL;
798 0cf4efb1 2018-09-29 stsp static const struct got_error *
799 e78dc838 2020-12-04 stsp view_input(struct tog_view **new, int *done, struct tog_view *view,
800 e78dc838 2020-12-04 stsp struct tog_view_list_head *views)
802 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
803 669b5ffa 2018-10-07 stsp struct tog_view *v;
804 1a76625f 2018-10-22 stsp int ch, errcode;
806 e5a0f69f 2018-08-18 stsp *new = NULL;
808 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
809 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
810 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
811 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
813 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
814 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
815 82954512 2020-02-03 stsp if (errcode)
816 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
817 82954512 2020-02-03 stsp "pthread_mutex_unlock");
818 82954512 2020-02-03 stsp pthread_yield();
819 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
820 82954512 2020-02-03 stsp if (errcode)
821 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
822 82954512 2020-02-03 stsp "pthread_mutex_lock");
823 60493ae3 2019-06-20 stsp view->search_next(view);
824 60493ae3 2019-06-20 stsp return NULL;
827 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
828 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
829 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
830 1a76625f 2018-10-22 stsp if (errcode)
831 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
832 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
833 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
834 1a76625f 2018-10-22 stsp if (errcode)
835 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
836 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
838 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
839 25791caa 2018-10-24 stsp tog_resizeterm();
840 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
841 61266923 2020-01-14 stsp tog_sigcont_received = 0;
842 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
843 25791caa 2018-10-24 stsp err = view_resize(v);
845 25791caa 2018-10-24 stsp return err;
846 e78dc838 2020-12-04 stsp err = v->input(new, v, KEY_RESIZE);
848 25791caa 2018-10-24 stsp return err;
849 cdfcfb03 2020-12-06 stsp if (v->child) {
850 cdfcfb03 2020-12-06 stsp err = view_resize(v->child);
852 cdfcfb03 2020-12-06 stsp return err;
853 cdfcfb03 2020-12-06 stsp err = v->child->input(new, v->child,
854 cdfcfb03 2020-12-06 stsp KEY_RESIZE);
856 cdfcfb03 2020-12-06 stsp return err;
861 e5a0f69f 2018-08-18 stsp switch (ch) {
865 1e37a5c2 2019-05-12 jcs if (view->child) {
866 e78dc838 2020-12-04 stsp view->focussed = 0;
867 e78dc838 2020-12-04 stsp view->child->focussed = 1;
868 e78dc838 2020-12-04 stsp view->focus_child = 1;
869 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
870 e78dc838 2020-12-04 stsp view->focussed = 0;
871 e78dc838 2020-12-04 stsp view->parent->focussed = 1;
872 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
876 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
877 9970f7fc 2020-12-03 stsp view->dying = 1;
883 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
884 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
886 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
887 e78dc838 2020-12-04 stsp view->focussed = 0;
888 e78dc838 2020-12-04 stsp view->child->focussed = 1;
889 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
891 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
894 e78dc838 2020-12-04 stsp err = view->child->input(new, view->child,
895 9970f7fc 2020-12-03 stsp KEY_RESIZE);
897 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
898 e78dc838 2020-12-04 stsp view->parent->focussed = 0;
899 e78dc838 2020-12-04 stsp view->focussed = 1;
900 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
902 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
906 e78dc838 2020-12-04 stsp err = view->input(new, view, KEY_RESIZE);
909 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
912 60493ae3 2019-06-20 stsp if (view->search_start)
913 2b49a8ae 2019-06-22 stsp view_search_start(view);
915 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
919 8f4ed634 2020-03-26 stsp if (view->search_next) {
920 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
921 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
922 60493ae3 2019-06-20 stsp view->search_next_done = 0;
923 60493ae3 2019-06-20 stsp view->search_next(view);
925 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
928 e78dc838 2020-12-04 stsp err = view->input(new, view, ch);
932 e5a0f69f 2018-08-18 stsp return err;
936 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
938 0cf4efb1 2018-09-29 stsp PANEL *panel;
939 7f64f4d6 2020-12-10 naddy const struct tog_view *view_above;
941 669b5ffa 2018-10-07 stsp if (view->parent)
942 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
944 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
945 0cf4efb1 2018-09-29 stsp if (panel == NULL)
948 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
949 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
950 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
954 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
956 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
957 acdafe9c 2020-12-03 stsp if (view->child == NULL || view->child->focussed)
959 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
961 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
964 669b5ffa 2018-10-07 stsp return view->focussed;
967 bcbd79e2 2018-08-19 stsp static const struct got_error *
968 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
970 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
971 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
972 fb59748f 2020-12-05 stsp struct tog_view *new_view;
973 fd823528 2018-10-22 stsp int fast_refresh = 10;
974 1a76625f 2018-10-22 stsp int done = 0, errcode;
976 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
977 1a76625f 2018-10-22 stsp if (errcode)
978 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
980 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
981 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
983 1004088d 2018-09-29 stsp view->focussed = 1;
984 878940b7 2018-09-29 stsp err = view->show(view);
986 0cf4efb1 2018-09-29 stsp return err;
987 0cf4efb1 2018-09-29 stsp update_panels();
988 0cf4efb1 2018-09-29 stsp doupdate();
989 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
990 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
991 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
992 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
994 e78dc838 2020-12-04 stsp err = view_input(&new_view, &done, view, &views);
997 9970f7fc 2020-12-03 stsp if (view->dying) {
998 e78dc838 2020-12-04 stsp struct tog_view *v, *prev = NULL;
1000 9970f7fc 2020-12-03 stsp if (view_is_parent_view(view))
1001 9970f7fc 2020-12-03 stsp prev = TAILQ_PREV(view, tog_view_list_head,
1003 e78dc838 2020-12-04 stsp else if (view->parent)
1004 669b5ffa 2018-10-07 stsp prev = view->parent;
1006 e78dc838 2020-12-04 stsp if (view->parent) {
1007 9970f7fc 2020-12-03 stsp view->parent->child = NULL;
1008 e78dc838 2020-12-04 stsp view->parent->focus_child = 0;
1010 9970f7fc 2020-12-03 stsp TAILQ_REMOVE(&views, view, entry);
1012 9970f7fc 2020-12-03 stsp err = view_close(view);
1014 e5a0f69f 2018-08-18 stsp goto done;
1016 e78dc838 2020-12-04 stsp view = NULL;
1017 e78dc838 2020-12-04 stsp TAILQ_FOREACH(v, &views, entry) {
1018 e78dc838 2020-12-04 stsp if (v->focussed)
1021 e78dc838 2020-12-04 stsp if (view == NULL && new_view == NULL) {
1022 e78dc838 2020-12-04 stsp /* No view has focus. Try to pick one. */
1024 e78dc838 2020-12-04 stsp view = prev;
1025 e78dc838 2020-12-04 stsp else if (!TAILQ_EMPTY(&views)) {
1026 e78dc838 2020-12-04 stsp view = TAILQ_LAST(&views,
1027 e78dc838 2020-12-04 stsp tog_view_list_head);
1029 e78dc838 2020-12-04 stsp if (view) {
1030 e78dc838 2020-12-04 stsp if (view->focus_child) {
1031 e78dc838 2020-12-04 stsp view->child->focussed = 1;
1032 e78dc838 2020-12-04 stsp view = view->child;
1034 e78dc838 2020-12-04 stsp view->focussed = 1;
1038 bcbd79e2 2018-08-19 stsp if (new_view) {
1039 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
1040 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
1041 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1042 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
1044 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
1045 86c66b02 2018-10-18 stsp err = view_close(v);
1047 86c66b02 2018-10-18 stsp goto done;
1050 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
1051 fed7eaa8 2018-10-24 stsp view = new_view;
1053 669b5ffa 2018-10-07 stsp if (view) {
1054 e78dc838 2020-12-04 stsp if (view_is_parent_view(view)) {
1055 e78dc838 2020-12-04 stsp if (view->child && view->child->focussed)
1056 e78dc838 2020-12-04 stsp view = view->child;
1058 e78dc838 2020-12-04 stsp if (view->parent && view->parent->focussed)
1059 e78dc838 2020-12-04 stsp view = view->parent;
1061 e78dc838 2020-12-04 stsp show_panel(view->panel);
1062 e78dc838 2020-12-04 stsp if (view->child && view_is_splitscreen(view->child))
1063 e78dc838 2020-12-04 stsp show_panel(view->child->panel);
1064 e78dc838 2020-12-04 stsp if (view->parent && view_is_splitscreen(view)) {
1065 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1067 1a76625f 2018-10-22 stsp goto done;
1069 669b5ffa 2018-10-07 stsp err = view->show(view);
1071 1a76625f 2018-10-22 stsp goto done;
1072 669b5ffa 2018-10-07 stsp if (view->child) {
1073 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1075 1a76625f 2018-10-22 stsp goto done;
1077 1a76625f 2018-10-22 stsp update_panels();
1078 1a76625f 2018-10-22 stsp doupdate();
1082 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1083 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1084 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1085 e5a0f69f 2018-08-18 stsp view_close(view);
1088 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1089 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1090 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1092 e5a0f69f 2018-08-18 stsp return err;
1095 4ed7e80c 2018-05-20 stsp __dead static void
1096 9f7d7167 2018-04-29 stsp usage_log(void)
1099 c70c5802 2018-08-01 stsp fprintf(stderr,
1100 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1101 9f7d7167 2018-04-29 stsp getprogname());
1105 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1106 80ddbec8 2018-04-29 stsp static const struct got_error *
1107 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1109 00dfcb92 2018-06-11 stsp char *vis = NULL;
1110 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1112 963b370f 2018-05-20 stsp *ws = NULL;
1113 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1114 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1115 00dfcb92 2018-06-11 stsp int vislen;
1116 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1117 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1119 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1120 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1122 00dfcb92 2018-06-11 stsp return err;
1123 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1124 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1125 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1126 a7f50699 2018-06-11 stsp goto done;
1130 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1131 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1132 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1133 a7f50699 2018-06-11 stsp goto done;
1136 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1137 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1139 00dfcb92 2018-06-11 stsp free(vis);
1140 963b370f 2018-05-20 stsp if (err) {
1141 963b370f 2018-05-20 stsp free(*ws);
1142 963b370f 2018-05-20 stsp *ws = NULL;
1143 963b370f 2018-05-20 stsp *wlen = 0;
1145 963b370f 2018-05-20 stsp return err;
1148 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1149 963b370f 2018-05-20 stsp static const struct got_error *
1150 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1151 27a741e5 2019-09-11 stsp int col_tab_align)
1153 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1154 963b370f 2018-05-20 stsp int cols = 0;
1155 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1156 963b370f 2018-05-20 stsp size_t wlen;
1159 963b370f 2018-05-20 stsp *wlinep = NULL;
1160 b700b5d6 2018-07-10 stsp *widthp = 0;
1162 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1164 963b370f 2018-05-20 stsp return err;
1166 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\n') {
1167 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1170 3f670bfb 2020-12-10 stsp if (wlen > 0 && wline[wlen - 1] == L'\r') {
1171 3f670bfb 2020-12-10 stsp wline[wlen - 1] = L'\0';
1176 27a741e5 2019-09-11 stsp while (i < wlen) {
1177 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1179 27a741e5 2019-09-11 stsp if (width == 0) {
1184 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1185 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1187 27a741e5 2019-09-11 stsp cols += width;
1189 27a741e5 2019-09-11 stsp } else if (width == -1) {
1190 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1191 27a741e5 2019-09-11 stsp width = TABSIZE -
1192 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1194 748d5cab 2020-12-14 naddy width = 1;
1195 748d5cab 2020-12-14 naddy wline[i] = L'.';
1197 748d5cab 2020-12-14 naddy if (cols + width > wlimit)
1199 748d5cab 2020-12-14 naddy cols += width;
1202 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1203 963b370f 2018-05-20 stsp goto done;
1206 963b370f 2018-05-20 stsp wline[i] = L'\0';
1207 b700b5d6 2018-07-10 stsp if (widthp)
1208 b700b5d6 2018-07-10 stsp *widthp = cols;
1211 963b370f 2018-05-20 stsp free(wline);
1213 963b370f 2018-05-20 stsp *wlinep = wline;
1214 963b370f 2018-05-20 stsp return err;
1217 8b473291 2019-02-21 stsp static const struct got_error*
1218 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1219 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1221 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1222 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1224 8b473291 2019-02-21 stsp const char *name;
1226 8b473291 2019-02-21 stsp *refs_str = NULL;
1228 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1229 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1230 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
1233 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1234 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1236 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1237 8b473291 2019-02-21 stsp name += 5;
1238 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1240 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1241 8b473291 2019-02-21 stsp name += 6;
1242 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1243 8b473291 2019-02-21 stsp name += 8;
1244 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1245 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1248 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
1251 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1252 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
1253 5d844a1e 2019-08-13 stsp if (err) {
1254 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
1255 48cae60d 2020-09-22 stsp free(ref_id);
1258 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1259 5d844a1e 2019-08-13 stsp err = NULL;
1260 5d844a1e 2019-08-13 stsp tag = NULL;
1263 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1264 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
1265 48cae60d 2020-09-22 stsp free(ref_id);
1267 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1268 52b5abe1 2019-08-13 stsp if (cmp != 0)
1270 8b473291 2019-02-21 stsp s = *refs_str;
1271 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1272 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1273 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1275 8b473291 2019-02-21 stsp *refs_str = NULL;
1281 8b473291 2019-02-21 stsp return err;
1284 963b370f 2018-05-20 stsp static const struct got_error *
1285 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1286 27a741e5 2019-09-11 stsp int col_tab_align)
1288 5813d178 2019-03-09 stsp char *smallerthan, *at;
1290 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1291 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1292 5813d178 2019-03-09 stsp author = smallerthan + 1;
1293 5813d178 2019-03-09 stsp at = strchr(author, '@');
1295 5813d178 2019-03-09 stsp *at = '\0';
1296 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1299 5813d178 2019-03-09 stsp static const struct got_error *
1300 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1301 8fdc79fe 2020-12-01 naddy struct got_object_id *id, const size_t date_display_cols,
1302 8fdc79fe 2020-12-01 naddy int author_display_cols)
1304 8fdc79fe 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1305 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1306 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1307 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1308 5813d178 2019-03-09 stsp char *author = NULL;
1309 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1310 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1311 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1312 bb737323 2018-05-20 stsp int col, limit;
1313 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1314 ccb26ccd 2018-11-05 stsp struct tm tm;
1315 45d799e2 2018-12-23 stsp time_t committer_time;
1316 11b20872 2019-11-08 stsp struct tog_color *tc;
1318 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1319 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1320 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1321 6db9f7f6 2019-12-10 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1322 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1323 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1325 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1326 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1328 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1329 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_DATE);
1331 11b20872 2019-11-08 stsp wattr_on(view->window,
1332 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1333 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1335 11b20872 2019-11-08 stsp wattr_off(view->window,
1336 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1337 27a741e5 2019-09-11 stsp col = limit;
1338 b39d25c7 2018-07-10 stsp if (col > avail)
1339 b39d25c7 2018-07-10 stsp goto done;
1341 6570a66d 2019-11-08 stsp if (avail >= 120) {
1342 6570a66d 2019-11-08 stsp char *id_str;
1343 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1345 6570a66d 2019-11-08 stsp goto done;
1346 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1348 11b20872 2019-11-08 stsp wattr_on(view->window,
1349 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1350 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1352 11b20872 2019-11-08 stsp wattr_off(view->window,
1353 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1354 6570a66d 2019-11-08 stsp free(id_str);
1356 6570a66d 2019-11-08 stsp if (col > avail)
1357 6570a66d 2019-11-08 stsp goto done;
1360 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1361 5813d178 2019-03-09 stsp if (author == NULL) {
1362 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1363 80ddbec8 2018-04-29 stsp goto done;
1365 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1367 bb737323 2018-05-20 stsp goto done;
1368 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1370 11b20872 2019-11-08 stsp wattr_on(view->window,
1371 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1372 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1374 11b20872 2019-11-08 stsp wattr_off(view->window,
1375 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1376 bb737323 2018-05-20 stsp col += author_width;
1377 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1378 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1380 bb737323 2018-05-20 stsp author_width++;
1382 9c2eaf34 2018-05-20 stsp if (col > avail)
1383 9c2eaf34 2018-05-20 stsp goto done;
1385 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1387 6d9fbc00 2018-04-29 stsp goto done;
1388 bb737323 2018-05-20 stsp logmsg = logmsg0;
1389 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1391 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1392 bb737323 2018-05-20 stsp if (newline)
1393 bb737323 2018-05-20 stsp *newline = '\0';
1394 bb737323 2018-05-20 stsp limit = avail - col;
1395 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1397 bb737323 2018-05-20 stsp goto done;
1398 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1399 bb737323 2018-05-20 stsp col += logmsg_width;
1400 27a741e5 2019-09-11 stsp while (col < avail) {
1401 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1405 80ddbec8 2018-04-29 stsp free(logmsg0);
1406 bb737323 2018-05-20 stsp free(wlogmsg);
1407 5813d178 2019-03-09 stsp free(author);
1408 bb737323 2018-05-20 stsp free(wauthor);
1409 80ddbec8 2018-04-29 stsp free(line);
1410 80ddbec8 2018-04-29 stsp return err;
1413 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1414 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1415 899d86c2 2018-05-10 stsp struct got_object_id *id)
1417 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1419 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1420 80ddbec8 2018-04-29 stsp if (entry == NULL)
1421 899d86c2 2018-05-10 stsp return NULL;
1423 899d86c2 2018-05-10 stsp entry->id = id;
1424 99db9666 2018-05-07 stsp entry->commit = commit;
1425 899d86c2 2018-05-10 stsp return entry;
1428 99db9666 2018-05-07 stsp static void
1429 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1431 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1433 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1434 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1435 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1436 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1437 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1438 99db9666 2018-05-07 stsp free(entry);
1441 99db9666 2018-05-07 stsp static void
1442 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1444 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1445 99db9666 2018-05-07 stsp pop_commit(commits);
1448 c4972b91 2018-05-07 stsp static const struct got_error *
1449 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1450 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1452 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1453 13add988 2019-10-15 stsp regmatch_t regmatch;
1454 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1456 13add988 2019-10-15 stsp *have_match = 0;
1458 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1460 13add988 2019-10-15 stsp return err;
1462 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1464 13add988 2019-10-15 stsp goto done;
1466 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1467 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1468 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1469 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1470 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1471 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1472 13add988 2019-10-15 stsp *have_match = 1;
1474 13add988 2019-10-15 stsp free(id_str);
1475 13add988 2019-10-15 stsp free(logmsg);
1476 13add988 2019-10-15 stsp return err;
1479 13add988 2019-10-15 stsp static const struct got_error *
1480 4e0d2870 2020-12-07 naddy queue_commits(struct tog_log_thread_args *a)
1482 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1485 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1486 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1487 1a76625f 2018-10-22 stsp * while updating the display.
1490 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1491 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1492 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1493 1a76625f 2018-10-22 stsp int errcode;
1495 4e0d2870 2020-12-07 naddy err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1496 4e0d2870 2020-12-07 naddy NULL, NULL);
1497 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1500 4e0d2870 2020-12-07 naddy err = got_object_open_as_commit(&commit, a->repo, id);
1503 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1504 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1505 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1509 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1510 1a76625f 2018-10-22 stsp if (errcode) {
1511 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1512 13add988 2019-10-15 stsp "pthread_mutex_lock");
1516 4e0d2870 2020-12-07 naddy entry->idx = a->commits->ncommits;
1517 4e0d2870 2020-12-07 naddy TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1518 4e0d2870 2020-12-07 naddy a->commits->ncommits++;
1520 4e0d2870 2020-12-07 naddy if (*a->searching == TOG_SEARCH_FORWARD &&
1521 4e0d2870 2020-12-07 naddy !*a->search_next_done) {
1522 7c1452c1 2020-03-26 stsp int have_match;
1523 4e0d2870 2020-12-07 naddy err = match_commit(&have_match, id, commit, a->regex);
1526 7c1452c1 2020-03-26 stsp if (have_match)
1527 4e0d2870 2020-12-07 naddy *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1530 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1531 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1532 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1533 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1536 4e0d2870 2020-12-07 naddy } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1538 9ba79e04 2018-06-11 stsp return err;
1541 2b779855 2020-12-05 naddy static void
1542 2b779855 2020-12-05 naddy select_commit(struct tog_log_view_state *s)
1544 2b779855 2020-12-05 naddy struct commit_queue_entry *entry;
1545 2b779855 2020-12-05 naddy int ncommits = 0;
1547 2b779855 2020-12-05 naddy entry = s->first_displayed_entry;
1548 2b779855 2020-12-05 naddy while (entry) {
1549 2b779855 2020-12-05 naddy if (ncommits == s->selected) {
1550 2b779855 2020-12-05 naddy s->selected_entry = entry;
1553 2b779855 2020-12-05 naddy entry = TAILQ_NEXT(entry, entry);
1554 2b779855 2020-12-05 naddy ncommits++;
1558 0553a4e3 2018-04-30 stsp static const struct got_error *
1559 8fdc79fe 2020-12-01 naddy draw_commits(struct tog_view *view)
1561 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1562 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1563 2b779855 2020-12-05 naddy struct commit_queue_entry *entry = s->selected_entry;
1564 8fdc79fe 2020-12-01 naddy const int limit = view->nlines;
1565 60493ae3 2019-06-20 stsp int width;
1566 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1567 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1568 8b473291 2019-02-21 stsp char *refs_str = NULL;
1569 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1570 11b20872 2019-11-08 stsp struct tog_color *tc;
1571 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1573 8fdc79fe 2020-12-01 naddy if (s->selected_entry &&
1574 8fdc79fe 2020-12-01 naddy !(view->searching && view->search_next_done == 0)) {
1575 8fdc79fe 2020-12-01 naddy err = got_object_id_str(&id_str, s->selected_entry->id);
1577 ecb28ae0 2018-07-16 stsp return err;
1578 8fdc79fe 2020-12-01 naddy err = build_refs_str(&refs_str, &s->refs,
1579 8fdc79fe 2020-12-01 naddy s->selected_entry->id, s->repo);
1581 8fdc79fe 2020-12-01 naddy goto done;
1584 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed == 0)
1585 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1587 8fdc79fe 2020-12-01 naddy if (s->thread_args.commits_needed > 0) {
1588 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1589 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1590 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1591 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1592 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1593 8f4ed634 2020-03-26 stsp goto done;
1596 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1598 f9686aa5 2020-03-27 stsp if (view->searching) {
1599 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1600 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1601 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1602 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1603 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1604 f9686aa5 2020-03-27 stsp search_str = "searching...";
1607 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1608 8fdc79fe 2020-12-01 naddy entry ? entry->idx + 1 : 0, s->commits.ncommits,
1609 f9686aa5 2020-03-27 stsp search_str ? search_str :
1610 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1611 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1612 8f4ed634 2020-03-26 stsp goto done;
1616 8fdc79fe 2020-12-01 naddy if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1617 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1618 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1619 8fdc79fe 2020-12-01 naddy s->in_repo_path, ncommits_str) == -1) {
1620 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1621 1a76625f 2018-10-22 stsp header = NULL;
1622 1a76625f 2018-10-22 stsp goto done;
1624 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1625 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1626 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1627 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1628 1a76625f 2018-10-22 stsp header = NULL;
1629 1a76625f 2018-10-22 stsp goto done;
1631 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1633 1a76625f 2018-10-22 stsp goto done;
1635 2814baeb 2018-08-01 stsp werase(view->window);
1637 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1638 a3404814 2018-09-02 stsp wstandout(view->window);
1639 8fdc79fe 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1641 11b20872 2019-11-08 stsp wattr_on(view->window,
1642 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1643 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1645 11b20872 2019-11-08 stsp wattr_off(view->window,
1646 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1647 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1648 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1651 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1652 a3404814 2018-09-02 stsp wstandend(view->window);
1653 ecb28ae0 2018-07-16 stsp free(wline);
1654 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1655 1a76625f 2018-10-22 stsp goto done;
1657 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1658 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1659 5813d178 2019-03-09 stsp ncommits = 0;
1660 5813d178 2019-03-09 stsp while (entry) {
1661 5813d178 2019-03-09 stsp char *author;
1662 5813d178 2019-03-09 stsp wchar_t *wauthor;
1663 5813d178 2019-03-09 stsp int width;
1664 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1666 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1667 5813d178 2019-03-09 stsp if (author == NULL) {
1668 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1669 5813d178 2019-03-09 stsp goto done;
1671 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1672 27a741e5 2019-09-11 stsp date_display_cols);
1673 5813d178 2019-03-09 stsp if (author_cols < width)
1674 5813d178 2019-03-09 stsp author_cols = width;
1675 5813d178 2019-03-09 stsp free(wauthor);
1676 5813d178 2019-03-09 stsp free(author);
1677 7ca04879 2019-10-19 stsp ncommits++;
1678 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1681 8fdc79fe 2020-12-01 naddy entry = s->first_displayed_entry;
1682 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = s->first_displayed_entry;
1683 867c6645 2018-07-10 stsp ncommits = 0;
1684 899d86c2 2018-05-10 stsp while (entry) {
1685 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1687 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1688 2814baeb 2018-08-01 stsp wstandout(view->window);
1689 8fdc79fe 2020-12-01 naddy err = draw_commit(view, entry->commit, entry->id,
1690 8fdc79fe 2020-12-01 naddy date_display_cols, author_cols);
1691 8fdc79fe 2020-12-01 naddy if (ncommits == s->selected)
1692 2814baeb 2018-08-01 stsp wstandend(view->window);
1694 60493ae3 2019-06-20 stsp goto done;
1695 0553a4e3 2018-04-30 stsp ncommits++;
1696 8fdc79fe 2020-12-01 naddy s->last_displayed_entry = entry;
1697 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1700 1a57306a 2018-09-02 stsp view_vborder(view);
1702 1a76625f 2018-10-22 stsp free(id_str);
1703 8b473291 2019-02-21 stsp free(refs_str);
1704 1a76625f 2018-10-22 stsp free(ncommits_str);
1705 1a76625f 2018-10-22 stsp free(header);
1706 80ddbec8 2018-04-29 stsp return err;
1709 07b55e75 2018-05-10 stsp static void
1710 3e135950 2020-12-01 naddy log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1712 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1713 07b55e75 2018-05-10 stsp int nscrolled = 0;
1715 ffe38506 2020-12-01 naddy entry = TAILQ_FIRST(&s->commits.head);
1716 ffe38506 2020-12-01 naddy if (s->first_displayed_entry == entry)
1719 ffe38506 2020-12-01 naddy entry = s->first_displayed_entry;
1720 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1721 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1722 07b55e75 2018-05-10 stsp if (entry) {
1723 ffe38506 2020-12-01 naddy s->first_displayed_entry = entry;
1724 07b55e75 2018-05-10 stsp nscrolled++;
1729 aa075928 2018-05-10 stsp static const struct got_error *
1730 ffe38506 2020-12-01 naddy trigger_log_thread(struct tog_view *view, int wait)
1732 ffe38506 2020-12-01 naddy struct tog_log_thread_args *ta = &view->state.log.thread_args;
1733 5e224a3e 2019-02-22 stsp int errcode;
1735 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1737 ffe38506 2020-12-01 naddy while (ta->commits_needed > 0) {
1738 ffe38506 2020-12-01 naddy if (ta->log_complete)
1741 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1742 ffe38506 2020-12-01 naddy errcode = pthread_cond_signal(&ta->need_commits);
1743 7aafa0d1 2019-02-22 stsp if (errcode)
1744 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1745 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1748 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1749 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1751 7c1452c1 2020-03-26 stsp if (!wait)
1754 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1755 ffe38506 2020-12-01 naddy show_log_view(view);
1756 7c1452c1 2020-03-26 stsp update_panels();
1757 7c1452c1 2020-03-26 stsp doupdate();
1759 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1760 ffe38506 2020-12-01 naddy errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1761 82954512 2020-02-03 stsp if (errcode)
1762 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1763 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1765 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1766 ffe38506 2020-12-01 naddy show_log_view(view);
1767 7c1452c1 2020-03-26 stsp update_panels();
1768 7c1452c1 2020-03-26 stsp doupdate();
1771 5e224a3e 2019-02-22 stsp return NULL;
1774 5e224a3e 2019-02-22 stsp static const struct got_error *
1775 ffe38506 2020-12-01 naddy log_scroll_down(struct tog_view *view, int maxscroll)
1777 ffe38506 2020-12-01 naddy struct tog_log_view_state *s = &view->state.log;
1778 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1779 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1780 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1782 ffe38506 2020-12-01 naddy if (s->last_displayed_entry == NULL)
1783 5e224a3e 2019-02-22 stsp return NULL;
1785 bf30f154 2020-12-07 naddy ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1786 ffe38506 2020-12-01 naddy if (s->commits.ncommits < ncommits_needed &&
1787 ffe38506 2020-12-01 naddy !s->thread_args.log_complete) {
1789 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1791 ffe38506 2020-12-01 naddy s->thread_args.commits_needed += maxscroll;
1792 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
1794 5e224a3e 2019-02-22 stsp return err;
1798 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1799 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1802 ffe38506 2020-12-01 naddy s->last_displayed_entry = pentry;
1804 ffe38506 2020-12-01 naddy pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1805 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1807 ffe38506 2020-12-01 naddy s->first_displayed_entry = pentry;
1808 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1810 dd0a52c1 2018-05-20 stsp return err;
1813 cd0acaa7 2018-05-20 stsp static const struct got_error *
1814 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1815 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1816 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
1818 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1819 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1820 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1822 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1823 15a94983 2018-12-23 stsp if (diff_view == NULL)
1824 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1826 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1827 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1828 78756c87 2020-11-24 stsp commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
1829 e5a0f69f 2018-08-18 stsp if (err == NULL)
1830 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1831 cd0acaa7 2018-05-20 stsp return err;
1834 80ddbec8 2018-04-29 stsp static const struct got_error *
1835 42a2230c 2020-12-01 naddy tree_view_visit_subtree(struct tog_tree_view_state *s,
1836 42a2230c 2020-12-01 naddy struct got_tree_object *subtree)
1838 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1840 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1841 941e9f74 2019-05-21 stsp if (parent == NULL)
1842 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1844 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1845 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1846 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1847 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1848 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1849 941e9f74 2019-05-21 stsp s->tree = subtree;
1850 941e9f74 2019-05-21 stsp s->selected = 0;
1851 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1852 941e9f74 2019-05-21 stsp return NULL;
1855 941e9f74 2019-05-21 stsp static const struct got_error *
1856 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1857 d91faf3b 2020-12-01 naddy struct got_object_id *commit_id, const char *path)
1859 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1860 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1861 941e9f74 2019-05-21 stsp const char *p;
1862 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1864 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1866 941e9f74 2019-05-21 stsp while (*p) {
1867 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1868 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1869 56e0773d 2019-11-28 stsp char *te_name;
1871 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1874 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1875 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1876 941e9f74 2019-05-21 stsp if (slash == NULL)
1877 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1879 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1880 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1881 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1884 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1885 56e0773d 2019-11-28 stsp if (te == NULL) {
1886 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1887 56e0773d 2019-11-28 stsp free(te_name);
1890 56e0773d 2019-11-28 stsp free(te_name);
1891 9a1d5146 2020-11-27 naddy s->first_displayed_entry = s->selected_entry = te;
1893 9a1d5146 2020-11-27 naddy if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
1894 9a1d5146 2020-11-27 naddy break; /* jump to this file's entry */
1896 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1897 941e9f74 2019-05-21 stsp if (slash)
1898 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1900 941e9f74 2019-05-21 stsp subpath = strdup(path);
1901 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1902 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1906 d91faf3b 2020-12-01 naddy err = got_object_id_by_path(&tree_id, s->repo, commit_id,
1911 d91faf3b 2020-12-01 naddy err = got_object_open_as_tree(&tree, s->repo, tree_id);
1912 941e9f74 2019-05-21 stsp free(tree_id);
1916 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, tree);
1917 941e9f74 2019-05-21 stsp if (err) {
1918 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1921 941e9f74 2019-05-21 stsp if (slash == NULL)
1923 941e9f74 2019-05-21 stsp free(subpath);
1924 941e9f74 2019-05-21 stsp subpath = NULL;
1925 941e9f74 2019-05-21 stsp p = slash;
1928 941e9f74 2019-05-21 stsp free(subpath);
1929 1a76625f 2018-10-22 stsp return err;
1932 61266923 2020-01-14 stsp static const struct got_error *
1933 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1934 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
1935 4e97c21c 2020-12-06 stsp const char *head_ref_name, struct got_repository *repo)
1937 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
1938 55cccc34 2020-02-20 stsp struct got_tree_object *tree;
1939 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
1940 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
1942 55cccc34 2020-02-20 stsp err = got_object_open_as_tree(&tree, repo,
1943 55cccc34 2020-02-20 stsp got_object_commit_get_tree_id(entry->commit));
1945 55cccc34 2020-02-20 stsp return err;
1947 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1948 55cccc34 2020-02-20 stsp if (tree_view == NULL)
1949 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
1951 4e97c21c 2020-12-06 stsp err = open_tree_view(tree_view, tree, entry->id, head_ref_name, repo);
1952 55cccc34 2020-02-20 stsp if (err) {
1953 55cccc34 2020-02-20 stsp got_object_tree_close(tree);
1954 55cccc34 2020-02-20 stsp return err;
1956 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
1958 55cccc34 2020-02-20 stsp *new_view = tree_view;
1960 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
1961 55cccc34 2020-02-20 stsp return NULL;
1963 d91faf3b 2020-12-01 naddy return tree_view_walk_path(s, entry->id, path);
1966 55cccc34 2020-02-20 stsp static const struct got_error *
1967 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1969 61266923 2020-01-14 stsp sigset_t sigset;
1970 61266923 2020-01-14 stsp int errcode;
1972 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1973 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1975 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1976 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1977 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1978 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
1979 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1981 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
1982 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
1983 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1985 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1986 61266923 2020-01-14 stsp if (errcode)
1987 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
1989 61266923 2020-01-14 stsp return NULL;
1992 1a76625f 2018-10-22 stsp static void *
1993 1a76625f 2018-10-22 stsp log_thread(void *arg)
1995 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1996 1a76625f 2018-10-22 stsp int errcode = 0;
1997 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1998 1a76625f 2018-10-22 stsp int done = 0;
2000 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
2002 61266923 2020-01-14 stsp return (void *)err;
2004 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
2005 4e0d2870 2020-12-07 naddy err = queue_commits(a);
2006 1a76625f 2018-10-22 stsp if (err) {
2007 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
2008 1a76625f 2018-10-22 stsp return (void *)err;
2009 1a76625f 2018-10-22 stsp err = NULL;
2011 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
2012 1a76625f 2018-10-22 stsp a->commits_needed--;
2014 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2015 3abe8080 2019-04-10 stsp if (errcode) {
2016 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2017 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2019 3abe8080 2019-04-10 stsp } else if (*a->quit)
2021 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
2022 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
2023 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
2024 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
2027 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
2028 7c1452c1 2020-03-26 stsp if (errcode) {
2029 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2030 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
2031 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
2036 1a76625f 2018-10-22 stsp a->commits_needed = 0;
2038 7c1452c1 2020-03-26 stsp if (a->commits_needed == 0) {
2039 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
2040 7c1452c1 2020-03-26 stsp &tog_mutex);
2041 7c1452c1 2020-03-26 stsp if (errcode)
2042 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
2043 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
2044 21355643 2020-12-06 stsp if (*a->quit)
2049 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2050 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2051 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
2052 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2054 3abe8080 2019-04-10 stsp a->log_complete = 1;
2055 1a76625f 2018-10-22 stsp return (void *)err;
2058 1a76625f 2018-10-22 stsp static const struct got_error *
2059 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2061 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2062 1a76625f 2018-10-22 stsp int errcode;
2064 1a76625f 2018-10-22 stsp if (s->thread) {
2065 1a76625f 2018-10-22 stsp s->quit = 1;
2066 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2067 1a76625f 2018-10-22 stsp if (errcode)
2068 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2069 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2070 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2071 1a76625f 2018-10-22 stsp if (errcode)
2072 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2073 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2074 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2075 1a76625f 2018-10-22 stsp if (errcode)
2076 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2077 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2078 1a76625f 2018-10-22 stsp if (errcode)
2079 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2080 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2081 1a76625f 2018-10-22 stsp s->thread = NULL;
2084 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2085 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
2086 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2089 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2090 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2091 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2094 9343a5fb 2018-06-23 stsp return err;
2097 9343a5fb 2018-06-23 stsp static const struct got_error *
2098 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2100 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2101 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2102 276b94a1 2020-11-13 naddy int errcode;
2104 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2106 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2107 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2108 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2110 276b94a1 2020-11-13 naddy errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2111 276b94a1 2020-11-13 naddy if (errcode && err == NULL)
2112 276b94a1 2020-11-13 naddy err = got_error_set_errno(errcode, "pthread_cond_destroy");
2114 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2115 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2116 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2117 1a76625f 2018-10-22 stsp free(s->start_id);
2118 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2119 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
2120 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
2121 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
2122 1a76625f 2018-10-22 stsp return err;
2125 1a76625f 2018-10-22 stsp static const struct got_error *
2126 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2128 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2130 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2131 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2132 60493ae3 2019-06-20 stsp return NULL;
2135 60493ae3 2019-06-20 stsp static const struct got_error *
2136 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2138 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2139 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2140 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2142 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2143 f9686aa5 2020-03-27 stsp show_log_view(view);
2144 f9686aa5 2020-03-27 stsp update_panels();
2145 f9686aa5 2020-03-27 stsp doupdate();
2147 96e2b566 2019-07-08 stsp if (s->search_entry) {
2148 13add988 2019-10-15 stsp int errcode, ch;
2149 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2150 13add988 2019-10-15 stsp if (errcode)
2151 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2152 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2153 13add988 2019-10-15 stsp ch = wgetch(view->window);
2154 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2155 13add988 2019-10-15 stsp if (errcode)
2156 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2157 13add988 2019-10-15 stsp "pthread_mutex_lock");
2158 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2159 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2160 678cbce5 2019-07-28 stsp return NULL;
2162 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2163 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2165 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2166 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2167 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2168 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2169 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2171 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2172 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2174 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2175 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2177 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2180 60493ae3 2019-06-20 stsp while (1) {
2181 13add988 2019-10-15 stsp int have_match = 0;
2183 60493ae3 2019-06-20 stsp if (entry == NULL) {
2184 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2185 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2186 f9967bca 2020-03-27 stsp view->search_next_done =
2187 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2188 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2189 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2190 f801134a 2019-06-25 stsp return NULL;
2193 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2194 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2195 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2197 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2198 ffe38506 2020-12-01 naddy return trigger_log_thread(view, 0);
2201 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2202 13add988 2019-10-15 stsp &view->regex);
2205 13add988 2019-10-15 stsp if (have_match) {
2206 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2207 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2211 96e2b566 2019-07-08 stsp s->search_entry = entry;
2212 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2213 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2215 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2218 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2219 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2220 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2221 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_DOWN);
2223 60493ae3 2019-06-20 stsp return err;
2226 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2227 e78dc838 2020-12-04 stsp err = input_log_view(NULL, view, KEY_UP);
2229 60493ae3 2019-06-20 stsp return err;
2234 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2236 60493ae3 2019-06-20 stsp return NULL;
2239 60493ae3 2019-06-20 stsp static const struct got_error *
2240 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2241 78756c87 2020-11-24 stsp struct got_repository *repo, const char *head_ref_name,
2242 78756c87 2020-11-24 stsp const char *in_repo_path, int log_branches)
2244 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2245 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2246 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2247 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2248 1a76625f 2018-10-22 stsp int errcode;
2250 78756c87 2020-11-24 stsp SIMPLEQ_INIT(&s->refs);
2252 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2253 f135c941 2020-02-20 stsp free(s->in_repo_path);
2254 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2255 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2256 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2259 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2260 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2261 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2263 78756c87 2020-11-24 stsp err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
2265 78756c87 2020-11-24 stsp goto done;
2267 fb2756b9 2018-08-04 stsp s->repo = repo;
2268 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
2269 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
2270 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
2271 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
2272 9cd7cbd1 2020-12-07 stsp goto done;
2275 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2276 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2277 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2278 5036bf37 2018-09-24 stsp goto done;
2280 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2282 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2283 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2284 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2285 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2287 11b20872 2019-11-08 stsp goto done;
2288 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2289 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2290 11b20872 2019-11-08 stsp if (err) {
2291 11b20872 2019-11-08 stsp free_colors(&s->colors);
2292 11b20872 2019-11-08 stsp goto done;
2294 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2295 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2296 11b20872 2019-11-08 stsp if (err) {
2297 11b20872 2019-11-08 stsp free_colors(&s->colors);
2298 11b20872 2019-11-08 stsp goto done;
2302 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2303 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2304 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2305 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2306 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2308 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2310 1a76625f 2018-10-22 stsp goto done;
2311 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2312 b672a97a 2020-01-27 stsp !s->log_branches);
2314 1a76625f 2018-10-22 stsp goto done;
2315 62d463ca 2020-10-20 naddy err = got_commit_graph_iter_start(thread_graph, s->start_id,
2316 62d463ca 2020-10-20 naddy s->repo, NULL, NULL);
2318 c5b78334 2020-01-12 stsp goto done;
2320 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2321 1a76625f 2018-10-22 stsp if (errcode) {
2322 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2323 1a76625f 2018-10-22 stsp goto done;
2325 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2326 7c1452c1 2020-03-26 stsp if (errcode) {
2327 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2328 7c1452c1 2020-03-26 stsp goto done;
2331 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2332 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2333 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2334 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2335 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2336 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2337 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2338 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2339 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2340 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2341 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2342 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2343 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2346 1a76625f 2018-10-22 stsp close_log_view(view);
2347 ba4f502b 2018-08-04 stsp return err;
2350 e5a0f69f 2018-08-18 stsp static const struct got_error *
2351 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2353 f2f6d207 2020-11-24 stsp const struct got_error *err;
2354 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2356 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2357 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2358 2b380cc8 2018-10-24 stsp &s->thread_args);
2359 2b380cc8 2018-10-24 stsp if (errcode)
2360 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2361 f2f6d207 2020-11-24 stsp if (s->thread_args.commits_needed > 0) {
2362 ffe38506 2020-12-01 naddy err = trigger_log_thread(view, 1);
2364 f2f6d207 2020-11-24 stsp return err;
2368 8fdc79fe 2020-12-01 naddy return draw_commits(view);
2371 e5a0f69f 2018-08-18 stsp static const struct got_error *
2372 e78dc838 2020-12-04 stsp input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2374 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2375 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2376 21355643 2020-12-06 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
2377 6458efa5 2020-11-24 stsp struct tog_view *ref_view = NULL;
2378 669b5ffa 2018-10-07 stsp int begin_x = 0;
2380 e5a0f69f 2018-08-18 stsp switch (ch) {
2382 1e37a5c2 2019-05-12 jcs s->quit = 1;
2385 1e37a5c2 2019-05-12 jcs case KEY_UP:
2388 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2390 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2391 1e37a5c2 2019-05-12 jcs s->selected--;
2393 3e135950 2020-12-01 naddy log_scroll_up(s, 1);
2394 2b779855 2020-12-05 naddy select_commit(s);
2396 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2397 a4292ac5 2019-05-12 jcs case CTRL('b'):
2398 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2400 2b779855 2020-12-05 naddy if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2401 1e37a5c2 2019-05-12 jcs s->selected = 0;
2403 2b779855 2020-12-05 naddy log_scroll_up(s, view->nlines - 1);
2404 2b779855 2020-12-05 naddy select_commit(s);
2407 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2410 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2412 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2413 2b779855 2020-12-05 naddy s->commits.ncommits - 1))
2414 1e37a5c2 2019-05-12 jcs s->selected++;
2416 2b779855 2020-12-05 naddy err = log_scroll_down(view, 1);
2420 2b779855 2020-12-05 naddy select_commit(s);
2422 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2423 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2424 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2425 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2426 1e37a5c2 2019-05-12 jcs if (first == NULL)
2428 ffe38506 2020-12-01 naddy err = log_scroll_down(view, view->nlines - 1);
2431 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2432 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2433 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2434 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2435 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2436 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2438 2b779855 2020-12-05 naddy select_commit(s);
2441 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2442 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2443 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2444 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2445 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2446 2b779855 2020-12-05 naddy select_commit(s);
2447 0bf7f153 2020-12-02 naddy if (s->commits.ncommits < view->nlines - 1 &&
2448 0bf7f153 2020-12-02 naddy !s->thread_args.log_complete) {
2449 0bf7f153 2020-12-02 naddy s->thread_args.commits_needed += (view->nlines - 1) -
2450 0bf7f153 2020-12-02 naddy s->commits.ncommits;
2451 0bf7f153 2020-12-02 naddy err = trigger_log_thread(view, 1);
2454 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2457 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2459 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2460 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2461 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2462 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2463 78756c87 2020-11-24 stsp view, s->repo);
2466 e78dc838 2020-12-04 stsp view->focussed = 0;
2467 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
2468 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2469 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2471 1e37a5c2 2019-05-12 jcs return err;
2472 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
2473 e78dc838 2020-12-04 stsp view->focus_child = 1;
2475 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2478 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2480 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2481 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2482 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2483 4e97c21c 2020-12-06 stsp s->selected_entry, s->in_repo_path, s->head_ref_name,
2487 e78dc838 2020-12-04 stsp view->focussed = 0;
2488 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
2489 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2490 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2492 1e37a5c2 2019-05-12 jcs return err;
2493 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
2494 e78dc838 2020-12-04 stsp view->focus_child = 1;
2496 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2498 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2499 21355643 2020-12-06 stsp case CTRL('l'):
2501 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE &&
2502 21355643 2020-12-06 stsp got_path_is_root_dir(s->in_repo_path))
2504 21355643 2020-12-06 stsp err = stop_log_thread(s);
2506 74cfe85e 2020-10-20 stsp return err;
2507 21355643 2020-12-06 stsp if (ch == KEY_BACKSPACE) {
2508 21355643 2020-12-06 stsp char *parent_path;
2509 21355643 2020-12-06 stsp err = got_path_dirname(&parent_path, s->in_repo_path);
2511 21355643 2020-12-06 stsp return err;
2512 21355643 2020-12-06 stsp free(s->in_repo_path);
2513 21355643 2020-12-06 stsp s->in_repo_path = parent_path;
2514 21355643 2020-12-06 stsp s->thread_args.in_repo_path = s->in_repo_path;
2515 21355643 2020-12-06 stsp } else if (ch == CTRL('l')) {
2516 21355643 2020-12-06 stsp struct got_object_id *start_id;
2517 21355643 2020-12-06 stsp err = got_repo_match_object_id(&start_id, NULL,
2518 21355643 2020-12-06 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2519 21355643 2020-12-06 stsp GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2521 21355643 2020-12-06 stsp return err;
2522 21355643 2020-12-06 stsp free(s->start_id);
2523 21355643 2020-12-06 stsp s->start_id = start_id;
2524 21355643 2020-12-06 stsp s->thread_args.start_id = s->start_id;
2525 21355643 2020-12-06 stsp } else /* 'B' */
2526 21355643 2020-12-06 stsp s->log_branches = !s->log_branches;
2528 21355643 2020-12-06 stsp err = got_repo_open(&s->thread_args.repo,
2529 21355643 2020-12-06 stsp got_repo_get_path(s->repo), NULL);
2531 21355643 2020-12-06 stsp return err;
2532 ca51c541 2020-12-07 stsp got_ref_list_free(&s->refs);
2533 ca51c541 2020-12-07 stsp err = got_ref_list(&s->refs, s->repo, NULL,
2534 ca51c541 2020-12-07 stsp got_ref_cmp_by_name, NULL);
2536 ca51c541 2020-12-07 stsp return err;
2537 21355643 2020-12-06 stsp err = got_commit_graph_open(&s->thread_args.graph,
2538 21355643 2020-12-06 stsp s->in_repo_path, !s->log_branches);
2540 d01904d4 2019-06-25 stsp return err;
2541 21355643 2020-12-06 stsp err = got_commit_graph_iter_start(s->thread_args.graph,
2542 21355643 2020-12-06 stsp s->start_id, s->repo, NULL, NULL);
2544 b672a97a 2020-01-27 stsp return err;
2545 21355643 2020-12-06 stsp free_commits(&s->commits);
2546 21355643 2020-12-06 stsp s->first_displayed_entry = NULL;
2547 21355643 2020-12-06 stsp s->last_displayed_entry = NULL;
2548 21355643 2020-12-06 stsp s->selected_entry = NULL;
2549 21355643 2020-12-06 stsp s->selected = 0;
2550 21355643 2020-12-06 stsp s->thread_args.log_complete = 0;
2551 21355643 2020-12-06 stsp s->quit = 0;
2552 21355643 2020-12-06 stsp s->thread_args.commits_needed = view->nlines;
2555 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
2556 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
2557 6458efa5 2020-11-24 stsp ref_view = view_open(view->nlines, view->ncols,
2558 6458efa5 2020-11-24 stsp view->begin_y, begin_x, TOG_VIEW_REF);
2559 6458efa5 2020-11-24 stsp if (ref_view == NULL)
2560 6458efa5 2020-11-24 stsp return got_error_from_errno("view_open");
2561 6458efa5 2020-11-24 stsp err = open_ref_view(ref_view, s->repo);
2562 6458efa5 2020-11-24 stsp if (err) {
2563 6458efa5 2020-11-24 stsp view_close(ref_view);
2564 6458efa5 2020-11-24 stsp return err;
2566 e78dc838 2020-12-04 stsp view->focussed = 0;
2567 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
2568 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
2569 6458efa5 2020-11-24 stsp err = view_close_child(view);
2571 6458efa5 2020-11-24 stsp return err;
2572 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
2573 e78dc838 2020-12-04 stsp view->focus_child = 1;
2575 6458efa5 2020-11-24 stsp *new_view = ref_view;
2581 80ddbec8 2018-04-29 stsp return err;
2584 4ed7e80c 2018-05-20 stsp static const struct got_error *
2585 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2587 c2db6724 2019-01-04 stsp const struct got_error *error;
2589 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2590 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2591 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2593 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2594 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2596 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2597 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2599 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2600 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2602 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2603 c2db6724 2019-01-04 stsp if (error != NULL)
2604 c2db6724 2019-01-04 stsp return error;
2606 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2607 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2609 c2db6724 2019-01-04 stsp return NULL;
2612 a915003a 2019-02-05 stsp static void
2613 a915003a 2019-02-05 stsp init_curses(void)
2615 a915003a 2019-02-05 stsp initscr();
2617 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2620 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2621 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2622 a915003a 2019-02-05 stsp curs_set(0);
2623 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2624 6d17833f 2019-11-08 stsp start_color();
2625 6d17833f 2019-11-08 stsp use_default_colors();
2627 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2628 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2629 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2632 c2db6724 2019-01-04 stsp static const struct got_error *
2633 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2634 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2636 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2638 f135c941 2020-02-20 stsp if (argc == 0) {
2639 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2640 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2641 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2642 f135c941 2020-02-20 stsp return NULL;
2645 f135c941 2020-02-20 stsp if (worktree) {
2646 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2649 bfd61697 2020-11-14 stsp err = got_worktree_resolve_path(&p, worktree, argv[0]);
2651 f135c941 2020-02-20 stsp return err;
2652 bfd61697 2020-11-14 stsp if (asprintf(in_repo_path, "%s%s%s", prefix,
2653 bfd61697 2020-11-14 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2654 bfd61697 2020-11-14 stsp p) == -1) {
2655 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2656 bfd61697 2020-11-14 stsp *in_repo_path = NULL;
2660 8fa913ec 2020-11-14 stsp err = got_repo_map_path(in_repo_path, repo, argv[0]);
2662 f135c941 2020-02-20 stsp return err;
2665 f135c941 2020-02-20 stsp static const struct got_error *
2666 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2668 80ddbec8 2018-04-29 stsp const struct got_error *error;
2669 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2670 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2671 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2672 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2673 d8f38dc4 2020-12-05 stsp char *start_commit = NULL, *label = NULL;
2674 d8f38dc4 2020-12-05 stsp struct got_reference *ref = NULL;
2675 d8f38dc4 2020-12-05 stsp const char *head_ref_name = NULL;
2676 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2677 04cc582a 2018-08-01 stsp struct tog_view *view;
2679 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2680 80ddbec8 2018-04-29 stsp switch (ch) {
2682 b672a97a 2020-01-27 stsp log_branches = 1;
2685 80ddbec8 2018-04-29 stsp start_commit = optarg;
2688 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2689 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2690 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2694 17020d27 2019-03-07 stsp usage_log();
2695 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2699 80ddbec8 2018-04-29 stsp argc -= optind;
2700 80ddbec8 2018-04-29 stsp argv += optind;
2702 f135c941 2020-02-20 stsp if (argc > 1)
2703 f135c941 2020-02-20 stsp usage_log();
2705 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2706 6962eb72 2020-02-20 stsp if (cwd == NULL)
2707 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
2709 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2710 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2711 963f97a1 2019-03-18 stsp goto done;
2713 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2714 a1fbf39a 2019-08-11 stsp if (worktree)
2715 6962eb72 2020-02-20 stsp repo_path =
2716 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2718 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2720 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2721 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2722 963f97a1 2019-03-18 stsp goto done;
2725 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2726 80ddbec8 2018-04-29 stsp if (error != NULL)
2727 ecb28ae0 2018-07-16 stsp goto done;
2729 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2730 f135c941 2020-02-20 stsp repo, worktree);
2731 f135c941 2020-02-20 stsp if (error)
2732 f135c941 2020-02-20 stsp goto done;
2734 f135c941 2020-02-20 stsp init_curses();
2736 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2737 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2738 c02c541e 2019-03-29 stsp if (error)
2739 c02c541e 2019-03-29 stsp goto done;
2741 d8f38dc4 2020-12-05 stsp if (start_commit == NULL) {
2742 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, &label,
2743 d8f38dc4 2020-12-05 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
2744 d8f38dc4 2020-12-05 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
2745 d8f38dc4 2020-12-05 stsp if (error)
2746 d8f38dc4 2020-12-05 stsp goto done;
2747 d8f38dc4 2020-12-05 stsp head_ref_name = label;
2749 d8f38dc4 2020-12-05 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2750 d8f38dc4 2020-12-05 stsp if (error == NULL)
2751 d8f38dc4 2020-12-05 stsp head_ref_name = got_ref_get_name(ref);
2752 d8f38dc4 2020-12-05 stsp else if (error->code != GOT_ERR_NOT_REF)
2753 d8f38dc4 2020-12-05 stsp goto done;
2754 d8f38dc4 2020-12-05 stsp error = got_repo_match_object_id(&start_id, NULL,
2755 d8f38dc4 2020-12-05 stsp start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2756 d8f38dc4 2020-12-05 stsp if (error)
2757 d8f38dc4 2020-12-05 stsp goto done;
2760 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2761 04cc582a 2018-08-01 stsp if (view == NULL) {
2762 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2763 04cc582a 2018-08-01 stsp goto done;
2765 78756c87 2020-11-24 stsp error = open_log_view(view, start_id, repo, head_ref_name,
2766 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2767 ba4f502b 2018-08-04 stsp if (error)
2768 ba4f502b 2018-08-04 stsp goto done;
2769 2fc00ff4 2019-08-31 stsp if (worktree) {
2770 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2771 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2772 2fc00ff4 2019-08-31 stsp worktree = NULL;
2774 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2776 f135c941 2020-02-20 stsp free(in_repo_path);
2777 ecb28ae0 2018-07-16 stsp free(repo_path);
2778 ecb28ae0 2018-07-16 stsp free(cwd);
2779 899d86c2 2018-05-10 stsp free(start_id);
2780 d8f38dc4 2020-12-05 stsp free(label);
2782 d8f38dc4 2020-12-05 stsp got_ref_close(ref);
2784 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2785 ec142235 2019-03-07 stsp if (worktree)
2786 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2787 80ddbec8 2018-04-29 stsp return error;
2790 4ed7e80c 2018-05-20 stsp __dead static void
2791 9f7d7167 2018-04-29 stsp usage_diff(void)
2794 3dbaef42 2020-11-24 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
2795 3dbaef42 2020-11-24 stsp "[-w] object1 object2\n", getprogname());
2799 6d17833f 2019-11-08 stsp static int
2800 41605754 2020-11-12 stsp match_line(const char *line, regex_t *regex, size_t nmatch,
2801 41605754 2020-11-12 stsp regmatch_t *regmatch)
2803 41605754 2020-11-12 stsp return regexec(regex, line, nmatch, regmatch, 0) == 0;
2806 f26dddb7 2019-11-08 stsp struct tog_color *
2807 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2809 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2811 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2812 41605754 2020-11-12 stsp if (match_line(line, &tc->regex, 0, NULL))
2813 6d17833f 2019-11-08 stsp return tc;
2816 6d17833f 2019-11-08 stsp return NULL;
2819 4ed7e80c 2018-05-20 stsp static const struct got_error *
2820 41605754 2020-11-12 stsp add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
2821 41605754 2020-11-12 stsp WINDOW *window, regmatch_t *regmatch)
2823 41605754 2020-11-12 stsp const struct got_error *err = NULL;
2824 41605754 2020-11-12 stsp wchar_t *wline;
2825 41605754 2020-11-12 stsp int width;
2828 41605754 2020-11-12 stsp *wtotal = 0;
2830 41605754 2020-11-12 stsp s = strndup(line, regmatch->rm_so);
2831 41605754 2020-11-12 stsp if (s == NULL)
2832 41605754 2020-11-12 stsp return got_error_from_errno("strndup");
2834 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2835 41605754 2020-11-12 stsp if (err) {
2837 41605754 2020-11-12 stsp return err;
2839 41605754 2020-11-12 stsp waddwstr(window, wline);
2840 41605754 2020-11-12 stsp free(wline);
2842 41605754 2020-11-12 stsp wlimit -= width;
2843 41605754 2020-11-12 stsp *wtotal += width;
2845 41605754 2020-11-12 stsp if (wlimit > 0) {
2846 41605754 2020-11-12 stsp s = strndup(line + regmatch->rm_so,
2847 41605754 2020-11-12 stsp regmatch->rm_eo - regmatch->rm_so);
2848 41605754 2020-11-12 stsp if (s == NULL) {
2849 41605754 2020-11-12 stsp err = got_error_from_errno("strndup");
2851 41605754 2020-11-12 stsp return err;
2853 41605754 2020-11-12 stsp err = format_line(&wline, &width, s, wlimit, col_tab_align);
2854 41605754 2020-11-12 stsp if (err) {
2856 41605754 2020-11-12 stsp return err;
2858 41605754 2020-11-12 stsp wattr_on(window, A_STANDOUT, NULL);
2859 41605754 2020-11-12 stsp waddwstr(window, wline);
2860 41605754 2020-11-12 stsp wattr_off(window, A_STANDOUT, NULL);
2861 41605754 2020-11-12 stsp free(wline);
2863 41605754 2020-11-12 stsp wlimit -= width;
2864 41605754 2020-11-12 stsp *wtotal += width;
2867 41605754 2020-11-12 stsp if (wlimit > 0 && strlen(line) > regmatch->rm_eo) {
2868 41605754 2020-11-12 stsp err = format_line(&wline, &width,
2869 bf30f154 2020-12-07 naddy line + regmatch->rm_eo, wlimit, col_tab_align);
2871 41605754 2020-11-12 stsp return err;
2872 41605754 2020-11-12 stsp waddwstr(window, wline);
2873 41605754 2020-11-12 stsp free(wline);
2874 41605754 2020-11-12 stsp *wtotal += width;
2877 41605754 2020-11-12 stsp return NULL;
2880 41605754 2020-11-12 stsp static const struct got_error *
2881 89f1a395 2020-12-01 naddy draw_file(struct tog_view *view, const char *header)
2883 89f1a395 2020-12-01 naddy struct tog_diff_view_state *s = &view->state.diff;
2884 89f1a395 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
2885 61e69b96 2018-05-20 stsp const struct got_error *err;
2886 fe621944 2020-11-10 stsp int nprinted = 0;
2887 b304db33 2018-05-20 stsp char *line;
2888 826082fe 2020-12-10 stsp size_t linesize = 0;
2889 826082fe 2020-12-10 stsp ssize_t linelen;
2890 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2891 61e69b96 2018-05-20 stsp wchar_t *wline;
2892 e0b650dd 2018-05-20 stsp int width;
2893 89f1a395 2020-12-01 naddy int max_lines = view->nlines;
2894 89f1a395 2020-12-01 naddy int nlines = s->nlines;
2895 fe621944 2020-11-10 stsp off_t line_offset;
2897 89f1a395 2020-12-01 naddy line_offset = s->line_offsets[s->first_displayed_line - 1];
2898 89f1a395 2020-12-01 naddy if (fseeko(s->f, line_offset, SEEK_SET) == -1)
2899 fe621944 2020-11-10 stsp return got_error_from_errno("fseek");
2901 f7d12f7e 2018-08-01 stsp werase(view->window);
2903 a3404814 2018-09-02 stsp if (header) {
2904 135a2da0 2020-11-11 stsp if (asprintf(&line, "[%d/%d] %s",
2905 89f1a395 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, nlines,
2906 135a2da0 2020-11-11 stsp header) == -1)
2907 135a2da0 2020-11-11 stsp return got_error_from_errno("asprintf");
2908 135a2da0 2020-11-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2909 135a2da0 2020-11-11 stsp free(line);
2911 a3404814 2018-09-02 stsp return err;
2913 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2914 a3404814 2018-09-02 stsp wstandout(view->window);
2915 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2916 e54cc94a 2020-11-11 stsp free(wline);
2917 e54cc94a 2020-11-11 stsp wline = NULL;
2918 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2919 a3404814 2018-09-02 stsp wstandend(view->window);
2920 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2921 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2923 a3404814 2018-09-02 stsp if (max_lines <= 1)
2924 a3404814 2018-09-02 stsp return NULL;
2925 a3404814 2018-09-02 stsp max_lines--;
2928 89f1a395 2020-12-01 naddy s->eof = 0;
2929 826082fe 2020-12-10 stsp line = NULL;
2930 fe621944 2020-11-10 stsp while (max_lines > 0 && nprinted < max_lines) {
2931 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
2932 826082fe 2020-12-10 stsp if (linelen == -1) {
2933 826082fe 2020-12-10 stsp if (feof(s->f)) {
2934 826082fe 2020-12-10 stsp s->eof = 1;
2937 826082fe 2020-12-10 stsp free(line);
2938 826082fe 2020-12-10 stsp return got_ferror(s->f, GOT_ERR_IO);
2941 89f1a395 2020-12-01 naddy tc = match_color(&s->colors, line);
2943 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2944 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2945 89f1a395 2020-12-01 naddy if (s->first_displayed_line + nprinted == s->matched_line &&
2946 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
2947 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols, 0,
2948 41605754 2020-11-12 stsp view->window, regmatch);
2949 41605754 2020-11-12 stsp if (err) {
2950 41605754 2020-11-12 stsp free(line);
2951 41605754 2020-11-12 stsp return err;
2954 41605754 2020-11-12 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2955 41605754 2020-11-12 stsp if (err) {
2956 41605754 2020-11-12 stsp free(line);
2957 41605754 2020-11-12 stsp return err;
2959 41605754 2020-11-12 stsp waddwstr(view->window, wline);
2960 41605754 2020-11-12 stsp free(wline);
2961 41605754 2020-11-12 stsp wline = NULL;
2964 6d17833f 2019-11-08 stsp wattr_off(view->window,
2965 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2966 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2967 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2968 fe621944 2020-11-10 stsp nprinted++;
2970 826082fe 2020-12-10 stsp free(line);
2971 fe621944 2020-11-10 stsp if (nprinted >= 1)
2972 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line +
2973 89f1a395 2020-12-01 naddy (nprinted - 1);
2975 89f1a395 2020-12-01 naddy s->last_displayed_line = s->first_displayed_line;
2977 1a57306a 2018-09-02 stsp view_vborder(view);
2979 89f1a395 2020-12-01 naddy if (s->eof) {
2980 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2981 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2982 c3e9aa98 2019-05-13 jcs nprinted++;
2985 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2987 c3e9aa98 2019-05-13 jcs return err;
2990 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2991 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2992 e54cc94a 2020-11-11 stsp free(wline);
2993 e54cc94a 2020-11-11 stsp wline = NULL;
2994 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2997 26ed57b2 2018-05-19 stsp return NULL;
3000 abd2672a 2018-12-23 stsp static char *
3001 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
3003 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3004 09867e48 2019-08-13 stsp char *p, *s;
3006 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3007 09867e48 2019-08-13 stsp if (tm == NULL)
3008 09867e48 2019-08-13 stsp return NULL;
3009 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3010 09867e48 2019-08-13 stsp if (s == NULL)
3011 09867e48 2019-08-13 stsp return NULL;
3012 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
3014 abd2672a 2018-12-23 stsp *p = '\0';
3018 4ed7e80c 2018-05-20 stsp static const struct got_error *
3019 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3020 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3022 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3023 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3024 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3025 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3027 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3028 0208f208 2020-05-05 stsp if (qid != NULL) {
3029 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3030 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3033 0208f208 2020-05-05 stsp return err;
3035 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3036 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3040 0208f208 2020-05-05 stsp if (tree_id1) {
3041 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3043 0208f208 2020-05-05 stsp goto done;
3046 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3047 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3049 0208f208 2020-05-05 stsp goto done;
3051 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3052 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3054 0208f208 2020-05-05 stsp if (tree1)
3055 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3056 0208f208 2020-05-05 stsp if (tree2)
3057 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3058 0208f208 2020-05-05 stsp return err;
3061 0208f208 2020-05-05 stsp static const struct got_error *
3062 fe621944 2020-11-10 stsp add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3066 fe621944 2020-11-10 stsp p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3067 fe621944 2020-11-10 stsp if (p == NULL)
3068 fe621944 2020-11-10 stsp return got_error_from_errno("reallocarray");
3069 fe621944 2020-11-10 stsp *line_offsets = p;
3070 fe621944 2020-11-10 stsp (*line_offsets)[*nlines] = off;
3071 fe621944 2020-11-10 stsp (*nlines)++;
3072 fe621944 2020-11-10 stsp return NULL;
3075 fe621944 2020-11-10 stsp static const struct got_error *
3076 fe621944 2020-11-10 stsp write_commit_info(off_t **line_offsets, size_t *nlines,
3077 fe621944 2020-11-10 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3078 fe621944 2020-11-10 stsp struct got_repository *repo, FILE *outfile)
3080 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
3081 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
3082 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3083 fe621944 2020-11-10 stsp char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3084 45d799e2 2018-12-23 stsp time_t committer_time;
3085 45d799e2 2018-12-23 stsp const char *author, *committer;
3086 8b473291 2019-02-21 stsp char *refs_str = NULL;
3087 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3088 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3089 fe621944 2020-11-10 stsp off_t outoff = 0;
3092 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3094 8b473291 2019-02-21 stsp if (refs) {
3095 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
3097 8b473291 2019-02-21 stsp return err;
3100 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3102 abd2672a 2018-12-23 stsp return err;
3104 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
3105 15a94983 2018-12-23 stsp if (err) {
3106 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
3107 15a94983 2018-12-23 stsp goto done;
3110 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, 0);
3112 fe621944 2020-11-10 stsp goto done;
3114 fe621944 2020-11-10 stsp n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3115 fe621944 2020-11-10 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3116 fe621944 2020-11-10 stsp if (n < 0) {
3117 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3118 abd2672a 2018-12-23 stsp goto done;
3120 fe621944 2020-11-10 stsp outoff += n;
3121 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3123 fe621944 2020-11-10 stsp goto done;
3125 fe621944 2020-11-10 stsp n = fprintf(outfile, "from: %s\n",
3126 fe621944 2020-11-10 stsp got_object_commit_get_author(commit));
3127 fe621944 2020-11-10 stsp if (n < 0) {
3128 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
3129 abd2672a 2018-12-23 stsp goto done;
3131 fe621944 2020-11-10 stsp outoff += n;
3132 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3134 fe621944 2020-11-10 stsp goto done;
3136 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3137 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
3138 fe621944 2020-11-10 stsp if (datestr) {
3139 fe621944 2020-11-10 stsp n = fprintf(outfile, "date: %s UTC\n", datestr);
3140 fe621944 2020-11-10 stsp if (n < 0) {
3141 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3142 fe621944 2020-11-10 stsp goto done;
3144 fe621944 2020-11-10 stsp outoff += n;
3145 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3147 fe621944 2020-11-10 stsp goto done;
3149 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3150 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3151 fe621944 2020-11-10 stsp if (strcmp(author, committer) != 0) {
3152 fe621944 2020-11-10 stsp n = fprintf(outfile, "via: %s\n", committer);
3153 fe621944 2020-11-10 stsp if (n < 0) {
3154 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3155 fe621944 2020-11-10 stsp goto done;
3157 fe621944 2020-11-10 stsp outoff += n;
3158 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3160 fe621944 2020-11-10 stsp goto done;
3162 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3164 5943eee2 2019-08-13 stsp goto done;
3165 fe621944 2020-11-10 stsp s = logmsg;
3166 fe621944 2020-11-10 stsp while ((line = strsep(&s, "\n")) != NULL) {
3167 fe621944 2020-11-10 stsp n = fprintf(outfile, "%s\n", line);
3168 fe621944 2020-11-10 stsp if (n < 0) {
3169 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3170 fe621944 2020-11-10 stsp goto done;
3172 fe621944 2020-11-10 stsp outoff += n;
3173 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3175 fe621944 2020-11-10 stsp goto done;
3178 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3180 0208f208 2020-05-05 stsp goto done;
3181 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3182 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3183 fe621944 2020-11-10 stsp n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3184 fe621944 2020-11-10 stsp if (n < 0) {
3185 fe621944 2020-11-10 stsp err = got_error_from_errno("fprintf");
3186 fe621944 2020-11-10 stsp goto done;
3188 fe621944 2020-11-10 stsp outoff += n;
3189 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3191 fe621944 2020-11-10 stsp goto done;
3192 0208f208 2020-05-05 stsp free((char *)pe->path);
3193 0208f208 2020-05-05 stsp free(pe->data);
3196 0208f208 2020-05-05 stsp fputc('\n', outfile);
3198 fe621944 2020-11-10 stsp err = add_line_offset(line_offsets, nlines, outoff);
3200 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3201 abd2672a 2018-12-23 stsp free(id_str);
3202 5943eee2 2019-08-13 stsp free(logmsg);
3203 8b473291 2019-02-21 stsp free(refs_str);
3204 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3205 7510f233 2020-08-09 stsp if (err) {
3206 ae6a6978 2020-08-09 stsp free(*line_offsets);
3207 ae6a6978 2020-08-09 stsp *line_offsets = NULL;
3208 ae6a6978 2020-08-09 stsp *nlines = 0;
3210 fe621944 2020-11-10 stsp return err;
3213 abd2672a 2018-12-23 stsp static const struct got_error *
3214 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3216 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3217 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3218 15a94983 2018-12-23 stsp int obj_type;
3220 fe621944 2020-11-10 stsp free(s->line_offsets);
3221 fe621944 2020-11-10 stsp s->line_offsets = malloc(sizeof(off_t));
3222 fe621944 2020-11-10 stsp if (s->line_offsets == NULL)
3223 fe621944 2020-11-10 stsp return got_error_from_errno("malloc");
3224 fe621944 2020-11-10 stsp s->nlines = 0;
3226 511a516b 2018-05-19 stsp f = got_opentemp();
3227 48ae06ee 2018-10-18 stsp if (f == NULL) {
3228 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3229 48ae06ee 2018-10-18 stsp goto done;
3231 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
3232 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3233 fb43ecf1 2019-02-11 stsp goto done;
3237 15a94983 2018-12-23 stsp if (s->id1)
3238 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3240 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3242 15a94983 2018-12-23 stsp goto done;
3244 15a94983 2018-12-23 stsp switch (obj_type) {
3245 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3246 fe621944 2020-11-10 stsp err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3247 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->label1, s->label2, s->diff_context,
3248 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3250 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3251 fe621944 2020-11-10 stsp err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3252 3dbaef42 2020-11-24 stsp s->id1, s->id2, "", "", s->diff_context,
3253 3dbaef42 2020-11-24 stsp s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3255 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3256 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3257 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3258 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3260 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3262 3ffacbe1 2020-02-02 tracey goto done;
3263 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3264 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3265 fe621944 2020-11-10 stsp err = write_commit_info(&s->line_offsets, &s->nlines,
3266 78756c87 2020-11-24 stsp s->id2, &s->refs, s->repo, s->f);
3267 f44b1f58 2020-02-02 tracey if (err)
3268 f44b1f58 2020-02-02 tracey goto done;
3269 f44b1f58 2020-02-02 tracey } else {
3270 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3271 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3272 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3273 fe621944 2020-11-10 stsp err = write_commit_info(
3274 fe621944 2020-11-10 stsp &s->line_offsets, &s->nlines,
3275 78756c87 2020-11-24 stsp s->id2, &s->refs, s->repo, s->f);
3276 f44b1f58 2020-02-02 tracey if (err)
3277 f44b1f58 2020-02-02 tracey goto done;
3282 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3284 fe621944 2020-11-10 stsp err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3285 3dbaef42 2020-11-24 stsp s->id1, s->id2, s->diff_context, s->ignore_whitespace,
3286 3dbaef42 2020-11-24 stsp s->force_text_diff, s->repo, s->f);
3290 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3293 f44b1f58 2020-02-02 tracey if (err)
3294 f44b1f58 2020-02-02 tracey goto done;
3296 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3297 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3298 48ae06ee 2018-10-18 stsp return err;
3301 f5215bb9 2019-02-22 stsp static void
3302 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3304 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3305 f5215bb9 2019-02-22 stsp update_panels();
3306 f5215bb9 2019-02-22 stsp doupdate();
3309 f44b1f58 2020-02-02 tracey static const struct got_error *
3310 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3312 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3314 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3315 f44b1f58 2020-02-02 tracey return NULL;
3318 f44b1f58 2020-02-02 tracey static const struct got_error *
3319 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3321 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3322 f44b1f58 2020-02-02 tracey int lineno;
3323 826082fe 2020-12-10 stsp char *line = NULL;
3324 826082fe 2020-12-10 stsp size_t linesize = 0;
3325 826082fe 2020-12-10 stsp ssize_t linelen;
3327 f44b1f58 2020-02-02 tracey if (!view->searching) {
3328 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3329 f44b1f58 2020-02-02 tracey return NULL;
3332 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3333 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3334 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3336 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3337 f44b1f58 2020-02-02 tracey } else {
3338 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3339 f44b1f58 2020-02-02 tracey lineno = 1;
3341 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3344 f44b1f58 2020-02-02 tracey while (1) {
3345 f44b1f58 2020-02-02 tracey off_t offset;
3347 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3348 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3349 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3353 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3354 f44b1f58 2020-02-02 tracey lineno = 1;
3356 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3359 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3360 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3361 f44b1f58 2020-02-02 tracey free(line);
3362 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3364 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3365 826082fe 2020-12-10 stsp if (linelen != -1 &&
3366 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
3367 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3368 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3371 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3372 f44b1f58 2020-02-02 tracey lineno++;
3374 f44b1f58 2020-02-02 tracey lineno--;
3376 826082fe 2020-12-10 stsp free(line);
3378 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3379 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3380 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3383 f44b1f58 2020-02-02 tracey return NULL;
3386 48ae06ee 2018-10-18 stsp static const struct got_error *
3387 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3388 3dbaef42 2020-11-24 stsp struct got_object_id *id2, const char *label1, const char *label2,
3389 3dbaef42 2020-11-24 stsp int diff_context, int ignore_whitespace, int force_text_diff,
3390 78756c87 2020-11-24 stsp struct tog_view *log_view, struct got_repository *repo)
3392 48ae06ee 2018-10-18 stsp const struct got_error *err;
3393 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3395 78756c87 2020-11-24 stsp SIMPLEQ_INIT(&s->refs);
3397 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3398 15a94983 2018-12-23 stsp int type1, type2;
3399 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3401 15a94983 2018-12-23 stsp return err;
3402 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3404 15a94983 2018-12-23 stsp return err;
3406 15a94983 2018-12-23 stsp if (type1 != type2)
3407 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3409 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3410 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3411 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3412 f44b1f58 2020-02-02 tracey s->repo = repo;
3413 f44b1f58 2020-02-02 tracey s->id1 = id1;
3414 f44b1f58 2020-02-02 tracey s->id2 = id2;
3415 3dbaef42 2020-11-24 stsp s->label1 = label1;
3416 3dbaef42 2020-11-24 stsp s->label2 = label2;
3418 15a94983 2018-12-23 stsp if (id1) {
3419 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3420 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3421 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3423 5465d566 2020-02-01 tracey s->id1 = NULL;
3425 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3426 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3427 5465d566 2020-02-01 tracey free(s->id1);
3428 5465d566 2020-02-01 tracey s->id1 = NULL;
3429 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3431 5465d566 2020-02-01 tracey s->f = NULL;
3432 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3433 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3434 3dbaef42 2020-11-24 stsp s->diff_context = diff_context;
3435 3dbaef42 2020-11-24 stsp s->ignore_whitespace = ignore_whitespace;
3436 64453f7e 2020-11-21 stsp s->force_text_diff = force_text_diff;
3437 5465d566 2020-02-01 tracey s->log_view = log_view;
3438 5465d566 2020-02-01 tracey s->repo = repo;
3440 f44b1f58 2020-02-02 tracey SIMPLEQ_INIT(&s->colors);
3441 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3442 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3443 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3444 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3446 6d17833f 2019-11-08 stsp return err;
3447 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3448 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3449 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3450 6d17833f 2019-11-08 stsp if (err) {
3451 5465d566 2020-02-01 tracey free_colors(&s->colors);
3452 6d17833f 2019-11-08 stsp return err;
3454 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3455 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3456 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3457 6d17833f 2019-11-08 stsp if (err) {
3458 5465d566 2020-02-01 tracey free_colors(&s->colors);
3459 6d17833f 2019-11-08 stsp return err;
3462 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3463 528c17dd 2020-07-31 stsp "^(commit [0-9a-f]|(blob|file) [-+] |[MDmA] [^ ])",
3464 0208f208 2020-05-05 stsp TOG_COLOR_DIFF_META,
3465 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3466 6d17833f 2019-11-08 stsp if (err) {
3467 5465d566 2020-02-01 tracey free_colors(&s->colors);
3468 6d17833f 2019-11-08 stsp return err;
3471 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3472 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3473 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3474 11b20872 2019-11-08 stsp if (err) {
3475 5465d566 2020-02-01 tracey free_colors(&s->colors);
3476 11b20872 2019-11-08 stsp return err;
3479 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3480 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3481 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3482 11b20872 2019-11-08 stsp if (err) {
3483 5465d566 2020-02-01 tracey free_colors(&s->colors);
3484 11b20872 2019-11-08 stsp return err;
3488 78756c87 2020-11-24 stsp err = got_ref_list(&s->refs, repo, NULL, got_ref_cmp_by_name, NULL);
3489 78756c87 2020-11-24 stsp if (err) {
3490 78756c87 2020-11-24 stsp free(s->id1);
3491 78756c87 2020-11-24 stsp s->id1 = NULL;
3492 78756c87 2020-11-24 stsp free(s->id2);
3493 78756c87 2020-11-24 stsp s->id2 = NULL;
3494 78756c87 2020-11-24 stsp free_colors(&s->colors);
3495 78756c87 2020-11-24 stsp return err;
3498 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3499 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3500 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3502 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3503 fe621944 2020-11-10 stsp s->nlines = 0;
3504 5465d566 2020-02-01 tracey err = create_diff(s);
3505 48ae06ee 2018-10-18 stsp if (err) {
3506 5465d566 2020-02-01 tracey free(s->id1);
3507 5465d566 2020-02-01 tracey s->id1 = NULL;
3508 5465d566 2020-02-01 tracey free(s->id2);
3509 5465d566 2020-02-01 tracey s->id2 = NULL;
3510 78756c87 2020-11-24 stsp free_colors(&s->colors);
3511 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
3512 48ae06ee 2018-10-18 stsp return err;
3515 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3516 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3517 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3518 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3519 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3521 5dc9f4bc 2018-08-04 stsp return NULL;
3524 e5a0f69f 2018-08-18 stsp static const struct got_error *
3525 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3527 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3528 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3530 5465d566 2020-02-01 tracey free(s->id1);
3531 5465d566 2020-02-01 tracey s->id1 = NULL;
3532 5465d566 2020-02-01 tracey free(s->id2);
3533 5465d566 2020-02-01 tracey s->id2 = NULL;
3534 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3535 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3536 5465d566 2020-02-01 tracey free_colors(&s->colors);
3537 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3538 fe621944 2020-11-10 stsp s->line_offsets = NULL;
3539 fe621944 2020-11-10 stsp s->nlines = 0;
3540 78756c87 2020-11-24 stsp got_ref_list_free(&s->refs);
3541 e5a0f69f 2018-08-18 stsp return err;
3544 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3545 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3547 a3404814 2018-09-02 stsp const struct got_error *err;
3548 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3549 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3550 3dbaef42 2020-11-24 stsp const char *label1, *label2;
3552 a3404814 2018-09-02 stsp if (s->id1) {
3553 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3555 a3404814 2018-09-02 stsp return err;
3556 3dbaef42 2020-11-24 stsp label1 = s->label1 ? : id_str1;
3558 3dbaef42 2020-11-24 stsp label1 = "/dev/null";
3560 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3562 a3404814 2018-09-02 stsp return err;
3563 3dbaef42 2020-11-24 stsp label2 = s->label2 ? : id_str2;
3565 3dbaef42 2020-11-24 stsp if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3566 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3567 a3404814 2018-09-02 stsp free(id_str1);
3568 a3404814 2018-09-02 stsp free(id_str2);
3569 a3404814 2018-09-02 stsp return err;
3571 a3404814 2018-09-02 stsp free(id_str1);
3572 a3404814 2018-09-02 stsp free(id_str2);
3574 89f1a395 2020-12-01 naddy return draw_file(view, header);
3577 15a087fe 2019-02-21 stsp static const struct got_error *
3578 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3579 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3581 d7a04538 2019-02-21 stsp const struct got_error *err;
3582 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3583 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3584 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3586 15a087fe 2019-02-21 stsp free(s->id2);
3587 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3588 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3589 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3591 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3593 d7a04538 2019-02-21 stsp return err;
3594 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3595 15a087fe 2019-02-21 stsp free(s->id1);
3596 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3597 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3598 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3599 15a087fe 2019-02-21 stsp return NULL;
3602 0cf4efb1 2018-09-29 stsp static const struct got_error *
3603 e78dc838 2020-12-04 stsp input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3605 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3606 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3607 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3608 5a8b5076 2020-12-05 stsp struct commit_queue_entry *old_selected_entry;
3609 826082fe 2020-12-10 stsp char *line = NULL;
3610 826082fe 2020-12-10 stsp size_t linesize = 0;
3611 826082fe 2020-12-10 stsp ssize_t linelen;
3614 e5a0f69f 2018-08-18 stsp switch (ch) {
3617 3dbaef42 2020-11-24 stsp if (ch == 'a')
3618 3dbaef42 2020-11-24 stsp s->force_text_diff = !s->force_text_diff;
3619 3dbaef42 2020-11-24 stsp if (ch == 'w')
3620 3dbaef42 2020-11-24 stsp s->ignore_whitespace = !s->ignore_whitespace;
3621 64453f7e 2020-11-21 stsp wclear(view->window);
3622 64453f7e 2020-11-21 stsp s->first_displayed_line = 1;
3623 64453f7e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3624 64453f7e 2020-11-21 stsp diff_view_indicate_progress(view);
3625 64453f7e 2020-11-21 stsp err = create_diff(s);
3628 1e37a5c2 2019-05-12 jcs case KEY_UP:
3629 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3630 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3632 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3633 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3634 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3637 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3638 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3639 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3642 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3643 1e37a5c2 2019-05-12 jcs if (!s->eof)
3644 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3646 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3647 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3649 00ba99a7 2019-05-12 jcs if (s->eof)
3652 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3653 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->f);
3654 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3655 826082fe 2020-12-10 stsp if (linelen == -1) {
3656 826082fe 2020-12-10 stsp if (feof(s->f)) {
3657 826082fe 2020-12-10 stsp s->eof = 1;
3659 826082fe 2020-12-10 stsp err = got_ferror(s->f, GOT_ERR_IO);
3663 826082fe 2020-12-10 stsp free(line);
3666 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3667 1e37a5c2 2019-05-12 jcs s->diff_context--;
3668 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3669 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3670 27829c9e 2020-11-21 stsp if (s->first_displayed_line + view->nlines - 1 >
3671 27829c9e 2020-11-21 stsp s->nlines) {
3672 27829c9e 2020-11-21 stsp s->first_displayed_line = 1;
3673 27829c9e 2020-11-21 stsp s->last_displayed_line = view->nlines;
3678 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3679 1e37a5c2 2019-05-12 jcs s->diff_context++;
3680 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3681 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3686 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3688 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3689 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3691 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_UP);
3695 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3698 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3702 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3703 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3705 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3706 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3710 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3712 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3713 5a8b5076 2020-12-05 stsp old_selected_entry = ls->selected_entry;
3715 e78dc838 2020-12-04 stsp err = input_log_view(NULL, s->log_view, KEY_DOWN);
3719 5a8b5076 2020-12-05 stsp if (old_selected_entry == ls->selected_entry)
3722 2b779855 2020-12-05 naddy err = set_selected_commit(s, ls->selected_entry);
3726 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3727 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3729 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3730 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3736 bcbd79e2 2018-08-19 stsp return err;
3739 4ed7e80c 2018-05-20 stsp static const struct got_error *
3740 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3742 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3743 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3744 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3745 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3746 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3747 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3748 3dbaef42 2020-11-24 stsp char *label1 = NULL, *label2 = NULL;
3749 3dbaef42 2020-11-24 stsp int diff_context = 3, ignore_whitespace = 0;
3750 64453f7e 2020-11-21 stsp int ch, force_text_diff = 0;
3751 3dbaef42 2020-11-24 stsp const char *errstr;
3752 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3754 3dbaef42 2020-11-24 stsp while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
3755 26ed57b2 2018-05-19 stsp switch (ch) {
3757 64453f7e 2020-11-21 stsp force_text_diff = 1;
3760 3dbaef42 2020-11-24 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3762 3dbaef42 2020-11-24 stsp if (errstr != NULL)
3763 3dbaef42 2020-11-24 stsp err(1, "-C option %s", errstr);
3765 09b5bff8 2020-02-23 naddy case 'r':
3766 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3767 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3768 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3770 3dbaef42 2020-11-24 stsp got_path_strip_trailing_slashes(repo_path);
3773 3dbaef42 2020-11-24 stsp ignore_whitespace = 1;
3776 17020d27 2019-03-07 stsp usage_diff();
3777 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3781 26ed57b2 2018-05-19 stsp argc -= optind;
3782 26ed57b2 2018-05-19 stsp argv += optind;
3784 26ed57b2 2018-05-19 stsp if (argc == 0) {
3785 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3786 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3787 15a94983 2018-12-23 stsp id_str1 = argv[0];
3788 15a94983 2018-12-23 stsp id_str2 = argv[1];
3790 26ed57b2 2018-05-19 stsp usage_diff();
3792 a273ac94 2020-02-23 naddy cwd = getcwd(NULL, 0);
3793 a273ac94 2020-02-23 naddy if (cwd == NULL)
3794 a273ac94 2020-02-23 naddy return got_error_from_errno("getcwd");
3796 a273ac94 2020-02-23 naddy error = got_worktree_open(&worktree, cwd);
3797 a273ac94 2020-02-23 naddy if (error && error->code != GOT_ERR_NOT_WORKTREE)
3798 a273ac94 2020-02-23 naddy goto done;
3800 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3801 a273ac94 2020-02-23 naddy if (worktree)
3802 a273ac94 2020-02-23 naddy repo_path =
3803 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3805 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3807 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3808 a273ac94 2020-02-23 naddy error = got_error_from_errno("strdup");
3809 a273ac94 2020-02-23 naddy goto done;
3812 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3813 eb6600df 2019-01-04 stsp if (error)
3814 eb6600df 2019-01-04 stsp goto done;
3816 a273ac94 2020-02-23 naddy init_curses();
3818 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3819 26ed57b2 2018-05-19 stsp if (error)
3820 26ed57b2 2018-05-19 stsp goto done;
3822 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3823 3dbaef42 2020-11-24 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3824 26ed57b2 2018-05-19 stsp if (error)
3825 26ed57b2 2018-05-19 stsp goto done;
3827 3dbaef42 2020-11-24 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3828 3dbaef42 2020-11-24 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3829 26ed57b2 2018-05-19 stsp if (error)
3830 26ed57b2 2018-05-19 stsp goto done;
3832 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3833 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3834 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3835 ea5e7bb5 2018-08-01 stsp goto done;
3837 3dbaef42 2020-11-24 stsp error = open_diff_view(view, id1, id2, label1, label2, diff_context,
3838 78756c87 2020-11-24 stsp ignore_whitespace, force_text_diff, NULL, repo);
3839 5dc9f4bc 2018-08-04 stsp if (error)
3840 5dc9f4bc 2018-08-04 stsp goto done;
3841 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3843 3dbaef42 2020-11-24 stsp free(label1);
3844 3dbaef42 2020-11-24 stsp free(label2);
3845 c02c541e 2019-03-29 stsp free(repo_path);
3846 a273ac94 2020-02-23 naddy free(cwd);
3848 921be706 2019-06-28 stsp got_repo_close(repo);
3849 a273ac94 2020-02-23 naddy if (worktree)
3850 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3851 26ed57b2 2018-05-19 stsp return error;
3854 4ed7e80c 2018-05-20 stsp __dead static void
3855 9f7d7167 2018-04-29 stsp usage_blame(void)
3858 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3859 9f7d7167 2018-04-29 stsp getprogname());
3863 84451b3e 2018-07-10 stsp struct tog_blame_line {
3864 84451b3e 2018-07-10 stsp int annotated;
3865 84451b3e 2018-07-10 stsp struct got_object_id *id;
3868 4ed7e80c 2018-05-20 stsp static const struct got_error *
3869 4f7c3e5e 2020-12-01 naddy draw_blame(struct tog_view *view)
3871 4f7c3e5e 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
3872 4f7c3e5e 2020-12-01 naddy struct tog_blame *blame = &s->blame;
3873 4f7c3e5e 2020-12-01 naddy regmatch_t *regmatch = &view->regmatch;
3874 84451b3e 2018-07-10 stsp const struct got_error *err;
3875 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3876 826082fe 2020-12-10 stsp char *line = NULL;
3877 826082fe 2020-12-10 stsp size_t linesize = 0;
3878 826082fe 2020-12-10 stsp ssize_t linelen;
3879 84451b3e 2018-07-10 stsp wchar_t *wline;
3880 27a741e5 2019-09-11 stsp int width;
3881 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3882 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3883 ab089a2a 2018-07-12 stsp char *id_str;
3884 11b20872 2019-11-08 stsp struct tog_color *tc;
3886 4f7c3e5e 2020-12-01 naddy err = got_object_id_str(&id_str, s->blamed_commit->id);
3888 ab089a2a 2018-07-12 stsp return err;
3890 4f7c3e5e 2020-12-01 naddy rewind(blame->f);
3891 f7d12f7e 2018-08-01 stsp werase(view->window);
3893 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3894 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3895 ab089a2a 2018-07-12 stsp free(id_str);
3896 ab089a2a 2018-07-12 stsp return err;
3899 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3900 ab089a2a 2018-07-12 stsp free(line);
3901 2550e4c3 2018-07-13 stsp line = NULL;
3903 1cae65b4 2019-09-22 stsp return err;
3904 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3905 a3404814 2018-09-02 stsp wstandout(view->window);
3906 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3908 11b20872 2019-11-08 stsp wattr_on(view->window,
3909 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3910 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3912 11b20872 2019-11-08 stsp wattr_off(view->window,
3913 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3914 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3915 a3404814 2018-09-02 stsp wstandend(view->window);
3916 2550e4c3 2018-07-13 stsp free(wline);
3917 2550e4c3 2018-07-13 stsp wline = NULL;
3918 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3919 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3921 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3922 4f7c3e5e 2020-12-01 naddy s->first_displayed_line - 1 + s->selected_line, blame->nlines,
3923 4f7c3e5e 2020-12-01 naddy s->blame_complete ? "" : "annotating... ", s->path) == -1) {
3924 ab089a2a 2018-07-12 stsp free(id_str);
3925 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3927 ab089a2a 2018-07-12 stsp free(id_str);
3928 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3929 3f60a8ef 2018-07-10 stsp free(line);
3930 2550e4c3 2018-07-13 stsp line = NULL;
3932 3f60a8ef 2018-07-10 stsp return err;
3933 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3934 2550e4c3 2018-07-13 stsp free(wline);
3935 2550e4c3 2018-07-13 stsp wline = NULL;
3936 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3937 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3939 4f7c3e5e 2020-12-01 naddy s->eof = 0;
3940 4f7c3e5e 2020-12-01 naddy while (nprinted < view->nlines - 2) {
3941 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, blame->f);
3942 826082fe 2020-12-10 stsp if (linelen == -1) {
3943 826082fe 2020-12-10 stsp if (feof(blame->f)) {
3944 826082fe 2020-12-10 stsp s->eof = 1;
3947 84451b3e 2018-07-10 stsp free(line);
3948 826082fe 2020-12-10 stsp return got_ferror(blame->f, GOT_ERR_IO);
3950 826082fe 2020-12-10 stsp if (++lineno < s->first_displayed_line)
3953 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
3954 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3956 4f7c3e5e 2020-12-01 naddy if (blame->nlines > 0) {
3957 4f7c3e5e 2020-12-01 naddy blame_line = &blame->lines[lineno - 1];
3958 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3959 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3960 27a741e5 2019-09-11 stsp !(view->focussed &&
3961 4f7c3e5e 2020-12-01 naddy nprinted == s->selected_line - 1)) {
3962 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3963 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3964 8d0fe45a 2019-08-12 stsp char *id_str;
3965 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3966 8d0fe45a 2019-08-12 stsp if (err) {
3967 8d0fe45a 2019-08-12 stsp free(line);
3968 8d0fe45a 2019-08-12 stsp return err;
3970 4f7c3e5e 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
3972 11b20872 2019-11-08 stsp wattr_on(view->window,
3973 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3974 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3976 11b20872 2019-11-08 stsp wattr_off(view->window,
3977 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3978 8d0fe45a 2019-08-12 stsp free(id_str);
3979 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3981 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3982 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3985 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3986 ee41ec32 2018-07-10 stsp prev_id = NULL;
3989 4f7c3e5e 2020-12-01 naddy if (view->focussed && nprinted == s->selected_line - 1)
3990 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3991 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3993 41605754 2020-11-12 stsp if (view->ncols <= 9) {
3994 41605754 2020-11-12 stsp width = 9;
3995 41605754 2020-11-12 stsp wline = wcsdup(L"");
3996 41605754 2020-11-12 stsp if (wline == NULL) {
3997 41605754 2020-11-12 stsp err = got_error_from_errno("wcsdup");
3998 41605754 2020-11-12 stsp free(line);
3999 41605754 2020-11-12 stsp return err;
4001 4f7c3e5e 2020-12-01 naddy } else if (s->first_displayed_line + nprinted ==
4002 4f7c3e5e 2020-12-01 naddy s->matched_line &&
4003 41605754 2020-11-12 stsp regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4004 41605754 2020-11-12 stsp err = add_matched_line(&width, line, view->ncols - 9, 9,
4005 41605754 2020-11-12 stsp view->window, regmatch);
4006 41605754 2020-11-12 stsp if (err) {
4007 41605754 2020-11-12 stsp free(line);
4008 41605754 2020-11-12 stsp return err;
4010 41605754 2020-11-12 stsp width += 9;
4012 41605754 2020-11-12 stsp err = format_line(&wline, &width, line,
4013 41605754 2020-11-12 stsp view->ncols - 9, 9);
4014 41605754 2020-11-12 stsp waddwstr(view->window, wline);
4015 41605754 2020-11-12 stsp free(wline);
4016 41605754 2020-11-12 stsp wline = NULL;
4017 41605754 2020-11-12 stsp width += 9;
4020 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
4021 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
4022 84451b3e 2018-07-10 stsp if (++nprinted == 1)
4023 4f7c3e5e 2020-12-01 naddy s->first_displayed_line = lineno;
4025 826082fe 2020-12-10 stsp free(line);
4026 4f7c3e5e 2020-12-01 naddy s->last_displayed_line = lineno;
4028 1a57306a 2018-09-02 stsp view_vborder(view);
4030 84451b3e 2018-07-10 stsp return NULL;
4033 84451b3e 2018-07-10 stsp static const struct got_error *
4034 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4036 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
4037 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
4038 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
4039 1a76625f 2018-10-22 stsp int errcode;
4041 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
4042 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4043 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
4045 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4046 1a76625f 2018-10-22 stsp if (errcode)
4047 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
4049 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
4050 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
4051 d68a0a7d 2018-07-10 stsp goto done;
4054 d68a0a7d 2018-07-10 stsp if (lineno == -1)
4055 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
4057 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
4058 d68a0a7d 2018-07-10 stsp if (line->annotated)
4059 d68a0a7d 2018-07-10 stsp goto done;
4061 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
4062 84451b3e 2018-07-10 stsp if (line->id == NULL) {
4063 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4064 84451b3e 2018-07-10 stsp goto done;
4066 84451b3e 2018-07-10 stsp line->annotated = 1;
4068 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4069 1a76625f 2018-10-22 stsp if (errcode)
4070 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4071 84451b3e 2018-07-10 stsp return err;
4074 84451b3e 2018-07-10 stsp static void *
4075 84451b3e 2018-07-10 stsp blame_thread(void *arg)
4077 18430de3 2018-07-10 stsp const struct got_error *err;
4078 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
4079 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
4080 1a76625f 2018-10-22 stsp int errcode;
4082 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
4084 61266923 2020-01-14 stsp return (void *)err;
4086 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
4087 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4088 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
4089 fc06ba56 2019-08-22 stsp err = NULL;
4091 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4092 1a76625f 2018-10-22 stsp if (errcode)
4093 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
4094 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4096 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
4097 c9beca56 2018-07-22 stsp ta->repo = NULL;
4098 c9beca56 2018-07-22 stsp *ta->complete = 1;
4100 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4101 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
4102 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4104 18430de3 2018-07-10 stsp return (void *)err;
4107 245d91c1 2018-07-12 stsp static struct got_object_id *
4108 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4109 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
4111 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
4113 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
4114 8d0fe45a 2019-08-12 stsp return NULL;
4116 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
4117 245d91c1 2018-07-12 stsp if (!line->annotated)
4118 245d91c1 2018-07-12 stsp return NULL;
4120 245d91c1 2018-07-12 stsp return line->id;
4123 b880a918 2018-07-10 stsp static const struct got_error *
4124 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
4126 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
4129 245d91c1 2018-07-12 stsp if (blame->thread) {
4130 1a76625f 2018-10-22 stsp int errcode;
4131 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4132 1a76625f 2018-10-22 stsp if (errcode)
4133 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4134 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
4135 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
4136 1a76625f 2018-10-22 stsp if (errcode)
4137 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
4138 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4139 1a76625f 2018-10-22 stsp if (errcode)
4140 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
4141 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
4142 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
4143 245d91c1 2018-07-12 stsp err = NULL;
4144 245d91c1 2018-07-12 stsp blame->thread = NULL;
4146 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
4147 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
4148 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
4150 245d91c1 2018-07-12 stsp if (blame->f) {
4151 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
4152 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4153 245d91c1 2018-07-12 stsp blame->f = NULL;
4155 57670559 2018-12-23 stsp if (blame->lines) {
4156 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
4157 57670559 2018-12-23 stsp free(blame->lines[i].id);
4158 57670559 2018-12-23 stsp free(blame->lines);
4159 57670559 2018-12-23 stsp blame->lines = NULL;
4161 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
4162 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
4164 245d91c1 2018-07-12 stsp return err;
4167 245d91c1 2018-07-12 stsp static const struct got_error *
4168 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4170 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4171 fc06ba56 2019-08-22 stsp int *done = arg;
4172 fc06ba56 2019-08-22 stsp int errcode;
4174 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4175 fc06ba56 2019-08-22 stsp if (errcode)
4176 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4177 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4179 fc06ba56 2019-08-22 stsp if (*done)
4180 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4182 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4183 fc06ba56 2019-08-22 stsp if (errcode)
4184 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4185 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4187 fc06ba56 2019-08-22 stsp return err;
4190 fc06ba56 2019-08-22 stsp static const struct got_error *
4191 a5388363 2020-12-01 naddy run_blame(struct tog_view *view)
4193 a5388363 2020-12-01 naddy struct tog_blame_view_state *s = &view->state.blame;
4194 a5388363 2020-12-01 naddy struct tog_blame *blame = &s->blame;
4195 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4196 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4197 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4198 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4199 15a94983 2018-12-23 stsp int obj_type;
4201 a5388363 2020-12-01 naddy err = got_object_id_by_path(&obj_id, s->repo, s->blamed_commit->id,
4202 a5388363 2020-12-01 naddy s->path);
4204 15a94983 2018-12-23 stsp return err;
4206 a5388363 2020-12-01 naddy err = got_object_get_type(&obj_type, s->repo, obj_id);
4208 84451b3e 2018-07-10 stsp goto done;
4210 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4211 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4212 84451b3e 2018-07-10 stsp goto done;
4215 a5388363 2020-12-01 naddy err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4217 a70480e0 2018-06-23 stsp goto done;
4218 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4219 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4220 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4221 84451b3e 2018-07-10 stsp goto done;
4223 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4224 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4225 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
4226 84451b3e 2018-07-10 stsp goto done;
4228 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4229 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4230 b02560ec 2019-08-19 stsp blame->nlines--;
4232 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4233 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4234 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4235 84451b3e 2018-07-10 stsp goto done;
4238 a5388363 2020-12-01 naddy err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL);
4240 bd24772e 2018-07-11 stsp goto done;
4242 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4243 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4244 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4245 a5388363 2020-12-01 naddy blame->cb_args.commit_id = got_object_id_dup(s->blamed_commit->id);
4246 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4247 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4248 245d91c1 2018-07-12 stsp goto done;
4250 a5388363 2020-12-01 naddy blame->cb_args.quit = &s->done;
4252 a5388363 2020-12-01 naddy blame->thread_args.path = s->path;
4253 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4254 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4255 a5388363 2020-12-01 naddy blame->thread_args.complete = &s->blame_complete;
4256 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4257 a5388363 2020-12-01 naddy blame->thread_args.cancel_arg = &s->done;
4258 a5388363 2020-12-01 naddy s->blame_complete = 0;
4260 f5a09613 2020-12-13 naddy if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4261 f5a09613 2020-12-13 naddy s->first_displayed_line = 1;
4262 f5a09613 2020-12-13 naddy s->last_displayed_line = view->nlines;
4263 f5a09613 2020-12-13 naddy s->selected_line = 1;
4268 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4269 27d434c2 2018-09-15 stsp free(obj_id);
4271 245d91c1 2018-07-12 stsp stop_blame(blame);
4272 245d91c1 2018-07-12 stsp return err;
4275 245d91c1 2018-07-12 stsp static const struct got_error *
4276 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4277 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4279 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4280 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4282 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
4284 c4843652 2019-08-12 stsp s->path = strdup(path);
4285 c4843652 2019-08-12 stsp if (s->path == NULL)
4286 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4288 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4289 c4843652 2019-08-12 stsp if (err) {
4290 c4843652 2019-08-12 stsp free(s->path);
4291 7cbe629d 2018-08-04 stsp return err;
4294 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4295 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4296 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4297 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4298 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4299 fb2756b9 2018-08-04 stsp s->repo = repo;
4300 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4301 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4303 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4304 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4305 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4306 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4308 11b20872 2019-11-08 stsp return err;
4311 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4312 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4313 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4314 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4315 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4317 a5388363 2020-12-01 naddy return run_blame(view);
4320 e5a0f69f 2018-08-18 stsp static const struct got_error *
4321 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4323 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4324 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4326 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4327 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4329 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4330 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4331 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4332 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4333 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4336 e5a0f69f 2018-08-18 stsp free(s->path);
4337 11b20872 2019-11-08 stsp free_colors(&s->colors);
4339 e5a0f69f 2018-08-18 stsp return err;
4342 7cbe629d 2018-08-04 stsp static const struct got_error *
4343 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4345 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4347 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4348 6c4c42e0 2019-06-24 stsp return NULL;
4351 6c4c42e0 2019-06-24 stsp static const struct got_error *
4352 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4354 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4355 6c4c42e0 2019-06-24 stsp int lineno;
4356 826082fe 2020-12-10 stsp char *line = NULL;
4357 826082fe 2020-12-10 stsp size_t linesize = 0;
4358 826082fe 2020-12-10 stsp ssize_t linelen;
4360 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4361 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4362 6c4c42e0 2019-06-24 stsp return NULL;
4365 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4366 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4367 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4369 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4371 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4372 6c4c42e0 2019-06-24 stsp lineno = 1;
4374 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4377 6c4c42e0 2019-06-24 stsp while (1) {
4378 6c4c42e0 2019-06-24 stsp off_t offset;
4380 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4381 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4382 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4386 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4387 6c4c42e0 2019-06-24 stsp lineno = 1;
4389 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4392 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4393 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4394 6c4c42e0 2019-06-24 stsp free(line);
4395 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4397 826082fe 2020-12-10 stsp linelen = getline(&line, &linesize, s->blame.f);
4398 826082fe 2020-12-10 stsp if (linelen != -1 &&
4399 41605754 2020-11-12 stsp match_line(line, &view->regex, 1, &view->regmatch)) {
4400 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4401 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4404 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4409 826082fe 2020-12-10 stsp free(line);
4411 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4412 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4413 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4416 6c4c42e0 2019-06-24 stsp return NULL;
4419 6c4c42e0 2019-06-24 stsp static const struct got_error *
4420 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4422 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4423 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4424 2b380cc8 2018-10-24 stsp int errcode;
4426 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
4427 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4428 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4429 2b380cc8 2018-10-24 stsp if (errcode)
4430 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4432 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4435 51fe7530 2019-08-19 stsp if (s->blame_complete)
4436 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4438 4f7c3e5e 2020-12-01 naddy err = draw_blame(view);
4440 669b5ffa 2018-10-07 stsp view_vborder(view);
4441 e5a0f69f 2018-08-18 stsp return err;
4444 e5a0f69f 2018-08-18 stsp static const struct got_error *
4445 e78dc838 2020-12-04 stsp input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4447 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4448 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4449 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4450 669b5ffa 2018-10-07 stsp int begin_x = 0;
4452 e5a0f69f 2018-08-18 stsp switch (ch) {
4454 1e37a5c2 2019-05-12 jcs s->done = 1;
4457 1e37a5c2 2019-05-12 jcs case KEY_UP:
4458 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4459 1e37a5c2 2019-05-12 jcs s->selected_line--;
4460 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4461 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4462 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4464 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4465 ea025d1d 2020-02-22 naddy case CTRL('b'):
4466 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4467 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4470 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4471 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4472 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4474 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4477 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4478 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4479 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4480 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4481 1e37a5c2 2019-05-12 jcs s->selected_line++;
4482 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4483 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4484 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4487 1e37a5c2 2019-05-12 jcs case 'p': {
4488 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4489 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4490 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4491 1e37a5c2 2019-05-12 jcs if (id == NULL)
4493 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4494 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4495 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4496 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4497 1e37a5c2 2019-05-12 jcs int obj_type;
4498 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4499 1e37a5c2 2019-05-12 jcs s->repo, id);
4502 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
4503 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4504 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4505 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4508 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4509 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4510 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4511 e5a0f69f 2018-08-18 stsp if (err) {
4512 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4513 1e37a5c2 2019-05-12 jcs err = NULL;
4514 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4517 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4519 1e37a5c2 2019-05-12 jcs free(blob_id);
4520 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4521 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4522 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4525 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4527 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4529 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4530 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4532 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4537 1e37a5c2 2019-05-12 jcs s->done = 1;
4538 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4539 1e37a5c2 2019-05-12 jcs s->done = 0;
4540 1e37a5c2 2019-05-12 jcs if (thread_err)
4542 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4543 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4544 a5388363 2020-12-01 naddy err = run_blame(view);
4549 1e37a5c2 2019-05-12 jcs case 'B': {
4550 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4551 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
4552 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4554 1e37a5c2 2019-05-12 jcs s->done = 1;
4555 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4556 1e37a5c2 2019-05-12 jcs s->done = 0;
4557 1e37a5c2 2019-05-12 jcs if (thread_err)
4559 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4560 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4561 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4562 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4563 a5388363 2020-12-01 naddy err = run_blame(view);
4568 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4569 1e37a5c2 2019-05-12 jcs case '\r': {
4570 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4571 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4572 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4573 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4574 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4575 1e37a5c2 2019-05-12 jcs if (id == NULL)
4577 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4580 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4581 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4582 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4583 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4584 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4585 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4586 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4587 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4590 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4591 78756c87 2020-11-24 stsp id, NULL, NULL, 3, 0, 0, NULL, s->repo);
4592 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4594 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4597 e78dc838 2020-12-04 stsp view->focussed = 0;
4598 e78dc838 2020-12-04 stsp diff_view->focussed = 1;
4599 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4600 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4603 72a9cb46 2020-12-03 stsp view_set_child(view, diff_view);
4604 e78dc838 2020-12-04 stsp view->focus_child = 1;
4606 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4611 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4612 ea025d1d 2020-02-22 naddy case CTRL('f'):
4614 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4615 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4616 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4619 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4620 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4621 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4622 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4625 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4626 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4627 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4628 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4630 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4631 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4632 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4634 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4635 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4636 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4637 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4643 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4646 a70480e0 2018-06-23 stsp static const struct got_error *
4647 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4649 a70480e0 2018-06-23 stsp const struct got_error *error;
4650 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4651 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4652 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4653 0587e10c 2020-07-23 stsp char *link_target = NULL;
4654 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4655 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4657 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4659 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4660 a70480e0 2018-06-23 stsp switch (ch) {
4662 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4665 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4666 69069811 2018-08-02 stsp if (repo_path == NULL)
4667 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4671 17020d27 2019-03-07 stsp usage_blame();
4672 a70480e0 2018-06-23 stsp /* NOTREACHED */
4676 a70480e0 2018-06-23 stsp argc -= optind;
4677 a70480e0 2018-06-23 stsp argv += optind;
4679 f135c941 2020-02-20 stsp if (argc != 1)
4680 a70480e0 2018-06-23 stsp usage_blame();
4682 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
4683 6962eb72 2020-02-20 stsp if (cwd == NULL)
4684 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
4686 f135c941 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
4687 f135c941 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4688 f135c941 2020-02-20 stsp goto done;
4690 69069811 2018-08-02 stsp if (repo_path == NULL) {
4691 f135c941 2020-02-20 stsp if (worktree)
4692 eb41ed75 2019-02-05 stsp repo_path =
4693 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4695 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4697 f135c941 2020-02-20 stsp if (repo_path == NULL) {
4698 f135c941 2020-02-20 stsp error = got_error_from_errno("strdup");
4699 f135c941 2020-02-20 stsp goto done;
4702 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4703 c02c541e 2019-03-29 stsp if (error != NULL)
4704 8e94dd5b 2019-01-04 stsp goto done;
4706 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4707 f135c941 2020-02-20 stsp worktree);
4708 c02c541e 2019-03-29 stsp if (error)
4709 92205607 2019-01-04 stsp goto done;
4711 f135c941 2020-02-20 stsp init_curses();
4713 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4714 eb41ed75 2019-02-05 stsp if (error)
4715 69069811 2018-08-02 stsp goto done;
4717 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4718 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4719 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4720 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4721 a70480e0 2018-06-23 stsp if (error != NULL)
4722 66b4983c 2018-06-23 stsp goto done;
4723 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4724 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4726 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4727 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4729 a19e88aa 2018-06-23 stsp if (error != NULL)
4730 8b473291 2019-02-21 stsp goto done;
4732 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4733 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4734 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4735 e1cd8fed 2018-08-01 stsp goto done;
4738 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4739 0587e10c 2020-07-23 stsp commit_id, repo);
4740 7cbe629d 2018-08-04 stsp if (error)
4741 7cbe629d 2018-08-04 stsp goto done;
4743 0587e10c 2020-07-23 stsp error = open_blame_view(view, link_target ? link_target : in_repo_path,
4744 78756c87 2020-11-24 stsp commit_id, repo);
4745 0587e10c 2020-07-23 stsp if (error)
4746 0587e10c 2020-07-23 stsp goto done;
4747 12314ad4 2019-08-31 stsp if (worktree) {
4748 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4749 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4750 12314ad4 2019-08-31 stsp worktree = NULL;
4752 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4754 69069811 2018-08-02 stsp free(repo_path);
4755 f135c941 2020-02-20 stsp free(in_repo_path);
4756 0587e10c 2020-07-23 stsp free(link_target);
4757 69069811 2018-08-02 stsp free(cwd);
4758 a70480e0 2018-06-23 stsp free(commit_id);
4759 eb41ed75 2019-02-05 stsp if (worktree)
4760 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4762 a70480e0 2018-06-23 stsp got_repo_close(repo);
4763 a70480e0 2018-06-23 stsp return error;
4766 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4767 d86d3b18 2020-12-01 naddy draw_tree_entries(struct tog_view *view, const char *parent_path)
4769 d86d3b18 2020-12-01 naddy struct tog_tree_view_state *s = &view->state.tree;
4770 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4771 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4772 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4773 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4774 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4775 d86d3b18 2020-12-01 naddy int limit = view->nlines;
4777 d86d3b18 2020-12-01 naddy s->ndisplayed = 0;
4779 f7d12f7e 2018-08-01 stsp werase(view->window);
4781 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4782 ffd1d5e5 2018-06-23 stsp return NULL;
4784 d86d3b18 2020-12-01 naddy err = format_line(&wline, &width, s->tree_label, view->ncols, 0);
4786 ffd1d5e5 2018-06-23 stsp return err;
4787 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4788 a3404814 2018-09-02 stsp wstandout(view->window);
4789 d86d3b18 2020-12-01 naddy tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4791 11b20872 2019-11-08 stsp wattr_on(view->window,
4792 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4793 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4795 11b20872 2019-11-08 stsp wattr_off(view->window,
4796 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4797 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4798 a3404814 2018-09-02 stsp wstandend(view->window);
4799 2550e4c3 2018-07-13 stsp free(wline);
4800 2550e4c3 2018-07-13 stsp wline = NULL;
4801 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4802 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4803 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4804 ffd1d5e5 2018-06-23 stsp return NULL;
4805 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4807 ce52c690 2018-06-23 stsp return err;
4808 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4809 2550e4c3 2018-07-13 stsp free(wline);
4810 2550e4c3 2018-07-13 stsp wline = NULL;
4811 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4812 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4813 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4814 ffd1d5e5 2018-06-23 stsp return NULL;
4815 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4816 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4817 a1eca9bb 2018-06-23 stsp return NULL;
4819 d86d3b18 2020-12-01 naddy if (s->first_displayed_entry == NULL) {
4820 d86d3b18 2020-12-01 naddy te = got_object_tree_get_first_entry(s->tree);
4821 d86d3b18 2020-12-01 naddy if (s->selected == 0) {
4822 0cf4efb1 2018-09-29 stsp if (view->focussed)
4823 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4824 d86d3b18 2020-12-01 naddy s->selected_entry = NULL;
4826 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4827 d86d3b18 2020-12-01 naddy if (s->selected == 0 && view->focussed)
4828 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4829 d86d3b18 2020-12-01 naddy s->ndisplayed++;
4830 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4831 ffd1d5e5 2018-06-23 stsp return NULL;
4835 d86d3b18 2020-12-01 naddy te = s->first_displayed_entry;
4838 d86d3b18 2020-12-01 naddy nentries = got_object_tree_get_nentries(s->tree);
4839 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4840 0d6c6ee3 2020-05-20 stsp char *line = NULL, *id_str = NULL, *link_target = NULL;
4841 848d6979 2019-08-12 stsp const char *modestr = "";
4842 56e0773d 2019-11-28 stsp mode_t mode;
4844 d86d3b18 2020-12-01 naddy te = got_object_tree_get_entry(s->tree, i);
4845 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4847 d86d3b18 2020-12-01 naddy if (s->show_ids) {
4848 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4849 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4851 638f9024 2019-05-13 stsp return got_error_from_errno(
4852 230a42bd 2019-05-11 jcs "got_object_id_str");
4854 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4855 63c5ca5d 2019-08-24 stsp modestr = "$";
4856 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4859 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target,
4860 d86d3b18 2020-12-01 naddy te, s->repo);
4861 0d6c6ee3 2020-05-20 stsp if (err) {
4862 0d6c6ee3 2020-05-20 stsp free(id_str);
4863 0d6c6ee3 2020-05-20 stsp return err;
4865 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4866 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4867 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4869 848d6979 2019-08-12 stsp modestr = "@";
4871 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4872 848d6979 2019-08-12 stsp modestr = "/";
4873 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4874 848d6979 2019-08-12 stsp modestr = "*";
4875 0d6c6ee3 2020-05-20 stsp if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
4876 0d6c6ee3 2020-05-20 stsp got_tree_entry_get_name(te), modestr,
4877 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "",
4878 0d6c6ee3 2020-05-20 stsp link_target ? link_target : "") == -1) {
4879 1d13200f 2018-07-12 stsp free(id_str);
4880 0d6c6ee3 2020-05-20 stsp free(link_target);
4881 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4883 1d13200f 2018-07-12 stsp free(id_str);
4884 0d6c6ee3 2020-05-20 stsp free(link_target);
4885 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4886 ffd1d5e5 2018-06-23 stsp if (err) {
4887 ffd1d5e5 2018-06-23 stsp free(line);
4890 d86d3b18 2020-12-01 naddy if (n == s->selected) {
4891 0cf4efb1 2018-09-29 stsp if (view->focussed)
4892 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4893 d86d3b18 2020-12-01 naddy s->selected_entry = te;
4895 d86d3b18 2020-12-01 naddy tc = match_color(&s->colors, line);
4897 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4898 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4899 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4901 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4902 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4903 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4904 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4905 d86d3b18 2020-12-01 naddy if (n == s->selected && view->focussed)
4906 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4907 ffd1d5e5 2018-06-23 stsp free(line);
4908 2550e4c3 2018-07-13 stsp free(wline);
4909 2550e4c3 2018-07-13 stsp wline = NULL;
4911 d86d3b18 2020-12-01 naddy s->ndisplayed++;
4912 d86d3b18 2020-12-01 naddy s->last_displayed_entry = te;
4913 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4917 ffd1d5e5 2018-06-23 stsp return err;
4920 ffd1d5e5 2018-06-23 stsp static void
4921 3e135950 2020-12-01 naddy tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
4923 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4924 694d3271 2020-12-01 naddy int isroot = s->tree == s->root;
4925 fa86c4bf 2020-11-29 stsp int i = 0;
4927 694d3271 2020-12-01 naddy if (s->first_displayed_entry == NULL)
4930 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
4931 fa86c4bf 2020-11-29 stsp while (i++ < maxscroll) {
4932 fa86c4bf 2020-11-29 stsp if (te == NULL) {
4933 fa86c4bf 2020-11-29 stsp if (!isroot)
4934 694d3271 2020-12-01 naddy s->first_displayed_entry = NULL;
4937 694d3271 2020-12-01 naddy s->first_displayed_entry = te;
4938 694d3271 2020-12-01 naddy te = got_tree_entry_get_prev(s->tree, te);
4942 fa86c4bf 2020-11-29 stsp static void
4943 3e135950 2020-12-01 naddy tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
4945 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4946 ffd1d5e5 2018-06-23 stsp int n = 0;
4948 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
4949 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree,
4950 694d3271 2020-12-01 naddy s->first_displayed_entry);
4952 694d3271 2020-12-01 naddy next = got_object_tree_get_first_entry(s->tree);
4954 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
4955 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4956 694d3271 2020-12-01 naddy last = got_tree_entry_get_next(s->tree, last);
4957 768394f3 2019-01-24 stsp if (last) {
4958 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
4959 694d3271 2020-12-01 naddy next = got_tree_entry_get_next(s->tree, next);
4964 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4965 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4966 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4968 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4969 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4970 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4972 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4973 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
4974 56e0773d 2019-11-28 stsp + 1 /* slash */;
4976 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
4978 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4979 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4980 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4982 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4983 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4984 d9765a41 2018-06-23 stsp while (pt) {
4985 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
4986 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
4987 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4988 cb2ebc8a 2018-06-23 stsp goto done;
4990 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
4991 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4992 cb2ebc8a 2018-06-23 stsp goto done;
4994 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4997 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4998 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4999 ce52c690 2018-06-23 stsp goto done;
5003 ce52c690 2018-06-23 stsp if (err) {
5004 ce52c690 2018-06-23 stsp free(*path);
5005 ce52c690 2018-06-23 stsp *path = NULL;
5007 ce52c690 2018-06-23 stsp return err;
5010 ce52c690 2018-06-23 stsp static const struct got_error *
5011 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
5012 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
5013 78756c87 2020-11-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5015 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
5016 ce52c690 2018-06-23 stsp char *path;
5017 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
5019 a0de39f3 2019-08-09 stsp *new_view = NULL;
5021 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
5023 ce52c690 2018-06-23 stsp return err;
5025 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5026 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
5027 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
5028 83ce39e3 2019-08-12 stsp goto done;
5031 78756c87 2020-11-24 stsp err = open_blame_view(blame_view, path, commit_id, repo);
5032 e5a0f69f 2018-08-18 stsp if (err) {
5033 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
5034 fc06ba56 2019-08-22 stsp err = NULL;
5035 e5a0f69f 2018-08-18 stsp view_close(blame_view);
5037 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
5039 83ce39e3 2019-08-12 stsp free(path);
5040 69efd4c4 2018-07-18 stsp return err;
5043 69efd4c4 2018-07-18 stsp static const struct got_error *
5044 4e97c21c 2020-12-06 stsp log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5045 4e97c21c 2020-12-06 stsp struct tog_tree_view_state *s)
5047 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
5048 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
5049 69efd4c4 2018-07-18 stsp char *path;
5051 a0de39f3 2019-08-09 stsp *new_view = NULL;
5053 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5054 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
5055 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
5057 4e97c21c 2020-12-06 stsp err = tree_entry_path(&path, &s->parents, s->selected_entry);
5059 69efd4c4 2018-07-18 stsp return err;
5061 4e97c21c 2020-12-06 stsp err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5064 e5a0f69f 2018-08-18 stsp view_close(log_view);
5066 e5a0f69f 2018-08-18 stsp *new_view = log_view;
5067 cb2ebc8a 2018-06-23 stsp free(path);
5068 cb2ebc8a 2018-06-23 stsp return err;
5071 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5072 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
5073 4e97c21c 2020-12-06 stsp struct got_object_id *commit_id, const char *head_ref_name,
5074 4e97c21c 2020-12-06 stsp struct got_repository *repo)
5076 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
5077 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
5078 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5080 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
5082 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
5083 ffd1d5e5 2018-06-23 stsp if (err != NULL)
5084 ffd1d5e5 2018-06-23 stsp goto done;
5086 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5087 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5088 ffd1d5e5 2018-06-23 stsp goto done;
5091 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
5092 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5093 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5094 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
5095 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
5096 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
5097 6484ec90 2018-09-29 stsp goto done;
5099 9cd7cbd1 2020-12-07 stsp if (head_ref_name) {
5100 9cd7cbd1 2020-12-07 stsp s->head_ref_name = strdup(head_ref_name);
5101 9cd7cbd1 2020-12-07 stsp if (s->head_ref_name == NULL) {
5102 9cd7cbd1 2020-12-07 stsp err = got_error_from_errno("strdup");
5103 9cd7cbd1 2020-12-07 stsp goto done;
5106 fb2756b9 2018-08-04 stsp s->repo = repo;
5108 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
5110 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5111 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
5112 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
5113 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5115 c0b01bdb 2019-11-08 stsp goto done;
5116 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5117 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
5118 c0b01bdb 2019-11-08 stsp if (err) {
5119 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5120 c0b01bdb 2019-11-08 stsp goto done;
5122 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
5123 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
5124 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5125 c0b01bdb 2019-11-08 stsp if (err) {
5126 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5127 c0b01bdb 2019-11-08 stsp goto done;
5130 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
5131 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
5132 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5133 11b20872 2019-11-08 stsp if (err) {
5134 11b20872 2019-11-08 stsp free_colors(&s->colors);
5135 11b20872 2019-11-08 stsp goto done;
5138 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5139 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
5140 c0b01bdb 2019-11-08 stsp if (err) {
5141 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5142 c0b01bdb 2019-11-08 stsp goto done;
5146 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
5147 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
5148 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
5149 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
5150 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
5152 ad80ab7b 2018-08-04 stsp free(commit_id_str);
5153 6484ec90 2018-09-29 stsp if (err) {
5154 fb2756b9 2018-08-04 stsp free(s->tree_label);
5155 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5157 ad80ab7b 2018-08-04 stsp return err;
5160 e5a0f69f 2018-08-18 stsp static const struct got_error *
5161 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5163 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5165 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5166 fb2756b9 2018-08-04 stsp free(s->tree_label);
5167 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5168 6484ec90 2018-09-29 stsp free(s->commit_id);
5169 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5170 9cd7cbd1 2020-12-07 stsp free(s->head_ref_name);
5171 9cd7cbd1 2020-12-07 stsp s->head_ref_name = NULL;
5172 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5173 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5174 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5175 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5176 ad80ab7b 2018-08-04 stsp free(parent);
5179 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
5180 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5181 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
5182 7c32bd05 2019-06-22 stsp return NULL;
5185 7c32bd05 2019-06-22 stsp static const struct got_error *
5186 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5188 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5190 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5191 7c32bd05 2019-06-22 stsp return NULL;
5194 7c32bd05 2019-06-22 stsp static int
5195 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5197 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5199 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5203 7c32bd05 2019-06-22 stsp static const struct got_error *
5204 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5206 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5207 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5209 7c32bd05 2019-06-22 stsp if (!view->searching) {
5210 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5211 7c32bd05 2019-06-22 stsp return NULL;
5214 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5215 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5216 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5217 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5218 56e0773d 2019-11-28 stsp s->selected_entry);
5220 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5222 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5223 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5225 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5226 56e0773d 2019-11-28 stsp s->selected_entry);
5229 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5230 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5232 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5235 7c32bd05 2019-06-22 stsp while (1) {
5236 56e0773d 2019-11-28 stsp if (te == NULL) {
5237 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5238 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5239 ac66afb8 2019-06-24 stsp return NULL;
5241 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5242 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5244 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5247 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5248 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5249 56e0773d 2019-11-28 stsp s->matched_entry = te;
5253 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5254 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5256 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5259 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5260 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5261 7c32bd05 2019-06-22 stsp s->selected = 0;
5264 e5a0f69f 2018-08-18 stsp return NULL;
5267 ad80ab7b 2018-08-04 stsp static const struct got_error *
5268 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5270 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5271 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5272 e5a0f69f 2018-08-18 stsp char *parent_path;
5274 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5276 e5a0f69f 2018-08-18 stsp return err;
5278 d86d3b18 2020-12-01 naddy err = draw_tree_entries(view, parent_path);
5279 e5a0f69f 2018-08-18 stsp free(parent_path);
5281 669b5ffa 2018-10-07 stsp view_vborder(view);
5282 e5a0f69f 2018-08-18 stsp return err;
5285 e5a0f69f 2018-08-18 stsp static const struct got_error *
5286 e78dc838 2020-12-04 stsp input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5288 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5289 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5290 152c1c93 2020-11-29 stsp struct tog_view *log_view, *ref_view;
5291 fa86c4bf 2020-11-29 stsp int begin_x = 0;
5293 e5a0f69f 2018-08-18 stsp switch (ch) {
5295 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5298 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5300 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5301 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5302 4e97c21c 2020-12-06 stsp err = log_selected_tree_entry(&log_view, begin_x, s);
5303 e78dc838 2020-12-04 stsp view->focussed = 0;
5304 e78dc838 2020-12-04 stsp log_view->focussed = 1;
5305 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5306 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5308 1e37a5c2 2019-05-12 jcs return err;
5309 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
5310 e78dc838 2020-12-04 stsp view->focus_child = 1;
5312 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5315 152c1c93 2020-11-29 stsp if (view_is_parent_view(view))
5316 152c1c93 2020-11-29 stsp begin_x = view_split_begin_x(view->begin_x);
5317 152c1c93 2020-11-29 stsp ref_view = view_open(view->nlines, view->ncols,
5318 152c1c93 2020-11-29 stsp view->begin_y, begin_x, TOG_VIEW_REF);
5319 152c1c93 2020-11-29 stsp if (ref_view == NULL)
5320 152c1c93 2020-11-29 stsp return got_error_from_errno("view_open");
5321 152c1c93 2020-11-29 stsp err = open_ref_view(ref_view, s->repo);
5322 152c1c93 2020-11-29 stsp if (err) {
5323 152c1c93 2020-11-29 stsp view_close(ref_view);
5324 152c1c93 2020-11-29 stsp return err;
5326 e78dc838 2020-12-04 stsp view->focussed = 0;
5327 e78dc838 2020-12-04 stsp ref_view->focussed = 1;
5328 152c1c93 2020-11-29 stsp if (view_is_parent_view(view)) {
5329 152c1c93 2020-11-29 stsp err = view_close_child(view);
5331 152c1c93 2020-11-29 stsp return err;
5332 72a9cb46 2020-12-03 stsp view_set_child(view, ref_view);
5333 e78dc838 2020-12-04 stsp view->focus_child = 1;
5335 152c1c93 2020-11-29 stsp *new_view = ref_view;
5338 1e37a5c2 2019-05-12 jcs case KEY_UP:
5339 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5340 1e37a5c2 2019-05-12 jcs s->selected--;
5343 3e135950 2020-12-01 naddy tree_scroll_up(s, 1);
5345 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5346 ea025d1d 2020-02-22 naddy case CTRL('b'):
5347 fa86c4bf 2020-11-29 stsp if (s->tree == s->root) {
5348 fa86c4bf 2020-11-29 stsp if (got_object_tree_get_first_entry(s->tree) ==
5349 fa86c4bf 2020-11-29 stsp s->first_displayed_entry)
5350 fa86c4bf 2020-11-29 stsp s->selected = 0;
5352 fa86c4bf 2020-11-29 stsp if (s->first_displayed_entry == NULL)
5353 fa86c4bf 2020-11-29 stsp s->selected = 0;
5355 3e135950 2020-12-01 naddy tree_scroll_up(s, MAX(0, view->nlines - 3));
5358 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5359 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5360 1e37a5c2 2019-05-12 jcs s->selected++;
5363 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5365 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5367 3e135950 2020-12-01 naddy tree_scroll_down(s, 1);
5369 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5370 ea025d1d 2020-02-22 naddy case CTRL('f'):
5371 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5373 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5374 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5375 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5378 3e135950 2020-12-01 naddy tree_scroll_down(s, view->nlines - 3);
5380 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5382 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5383 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5384 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5385 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5386 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5388 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5389 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5391 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5392 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5393 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5394 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5395 1e37a5c2 2019-05-12 jcs s->selected_entry =
5396 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5397 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5398 1e37a5c2 2019-05-12 jcs free(parent);
5399 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5400 56e0773d 2019-11-28 stsp s->selected_entry))) {
5401 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5402 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5403 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5406 42a2230c 2020-12-01 naddy err = tree_view_visit_subtree(s, subtree);
5407 941e9f74 2019-05-21 stsp if (err) {
5408 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5411 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5412 56e0773d 2019-11-28 stsp s->selected_entry))) {
5413 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5414 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5415 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5417 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5418 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5419 78756c87 2020-11-24 stsp s->commit_id, s->repo);
5422 e78dc838 2020-12-04 stsp view->focussed = 0;
5423 e78dc838 2020-12-04 stsp blame_view->focussed = 1;
5424 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5425 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5427 669b5ffa 2018-10-07 stsp return err;
5428 72a9cb46 2020-12-03 stsp view_set_child(view, blame_view);
5429 e78dc838 2020-12-04 stsp view->focus_child = 1;
5431 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5434 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5435 1e1ff4ed 2020-12-07 stsp if (view->nlines >= 4 && s->selected >= view->nlines - 3)
5436 1e1ff4ed 2020-12-07 stsp s->selected = view->nlines - 4;
5442 ffd1d5e5 2018-06-23 stsp return err;
5445 ffd1d5e5 2018-06-23 stsp __dead static void
5446 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5449 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5450 ffd1d5e5 2018-06-23 stsp getprogname());
5454 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5455 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5457 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5458 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5459 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5460 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5461 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5462 4e97c21c 2020-12-06 stsp const char *commit_id_arg = NULL;
5463 4e97c21c 2020-12-06 stsp char *label = NULL;
5464 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
5465 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
5466 4e97c21c 2020-12-06 stsp struct got_reference *ref = NULL;
5467 4e97c21c 2020-12-06 stsp const char *head_ref_name = NULL;
5469 5221c383 2018-08-01 stsp struct tog_view *view;
5471 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5472 ffd1d5e5 2018-06-23 stsp switch (ch) {
5474 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5477 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5478 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5479 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5483 e99e2d15 2020-11-24 naddy usage_tree();
5484 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5488 ffd1d5e5 2018-06-23 stsp argc -= optind;
5489 ffd1d5e5 2018-06-23 stsp argv += optind;
5491 55cccc34 2020-02-20 stsp if (argc > 1)
5492 e99e2d15 2020-11-24 naddy usage_tree();
5494 55cccc34 2020-02-20 stsp cwd = getcwd(NULL, 0);
5495 55cccc34 2020-02-20 stsp if (cwd == NULL)
5496 55cccc34 2020-02-20 stsp return got_error_from_errno("getcwd");
5498 55cccc34 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
5499 55cccc34 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5500 55cccc34 2020-02-20 stsp goto done;
5502 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5503 55cccc34 2020-02-20 stsp if (worktree)
5504 52185f70 2019-02-05 stsp repo_path =
5505 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5507 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5509 55cccc34 2020-02-20 stsp if (repo_path == NULL) {
5510 55cccc34 2020-02-20 stsp error = got_error_from_errno("strdup");
5511 55cccc34 2020-02-20 stsp goto done;
5514 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5515 c02c541e 2019-03-29 stsp if (error != NULL)
5516 52185f70 2019-02-05 stsp goto done;
5518 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5519 55cccc34 2020-02-20 stsp repo, worktree);
5520 55cccc34 2020-02-20 stsp if (error)
5521 55cccc34 2020-02-20 stsp goto done;
5523 55cccc34 2020-02-20 stsp init_curses();
5525 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5526 c02c541e 2019-03-29 stsp if (error)
5527 52185f70 2019-02-05 stsp goto done;
5529 4e97c21c 2020-12-06 stsp if (commit_id_arg == NULL) {
5530 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, &label,
5531 4e97c21c 2020-12-06 stsp worktree ? got_worktree_get_head_ref_name(worktree) :
5532 4e97c21c 2020-12-06 stsp GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, 1, repo);
5533 4e97c21c 2020-12-06 stsp if (error)
5534 4e97c21c 2020-12-06 stsp goto done;
5535 4e97c21c 2020-12-06 stsp head_ref_name = label;
5537 4e97c21c 2020-12-06 stsp error = got_ref_open(&ref, repo, commit_id_arg, 0);
5538 4e97c21c 2020-12-06 stsp if (error == NULL)
5539 4e97c21c 2020-12-06 stsp head_ref_name = got_ref_get_name(ref);
5540 4e97c21c 2020-12-06 stsp else if (error->code != GOT_ERR_NOT_REF)
5541 4e97c21c 2020-12-06 stsp goto done;
5542 4e97c21c 2020-12-06 stsp error = got_repo_match_object_id(&commit_id, NULL,
5543 4e97c21c 2020-12-06 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5544 4e97c21c 2020-12-06 stsp if (error)
5545 4e97c21c 2020-12-06 stsp goto done;
5548 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5549 55cccc34 2020-02-20 stsp if (error)
5550 ffd1d5e5 2018-06-23 stsp goto done;
5552 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
5553 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5554 55cccc34 2020-02-20 stsp if (error)
5555 8b473291 2019-02-21 stsp goto done;
5557 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5558 5221c383 2018-08-01 stsp if (view == NULL) {
5559 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5560 5221c383 2018-08-01 stsp goto done;
5562 4e97c21c 2020-12-06 stsp error = open_tree_view(view, tree, commit_id, head_ref_name, repo);
5563 ad80ab7b 2018-08-04 stsp if (error)
5564 ad80ab7b 2018-08-04 stsp goto done;
5565 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5566 55cccc34 2020-02-20 stsp error = tree_view_walk_path(&view->state.tree, commit_id,
5567 d91faf3b 2020-12-01 naddy in_repo_path);
5568 55cccc34 2020-02-20 stsp if (error)
5569 55cccc34 2020-02-20 stsp goto done;
5572 55cccc34 2020-02-20 stsp if (worktree) {
5573 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5574 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5575 55cccc34 2020-02-20 stsp worktree = NULL;
5577 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5579 52185f70 2019-02-05 stsp free(repo_path);
5580 e4a0e26d 2020-02-20 stsp free(cwd);
5581 ffd1d5e5 2018-06-23 stsp free(commit_id);
5582 4e97c21c 2020-12-06 stsp free(label);
5584 486cd271 2020-12-06 stsp got_ref_close(ref);
5585 ffd1d5e5 2018-06-23 stsp if (commit)
5586 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5588 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5590 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5591 ffd1d5e5 2018-06-23 stsp return error;
5594 6458efa5 2020-11-24 stsp static const struct got_error *
5595 6458efa5 2020-11-24 stsp ref_view_load_refs(struct tog_ref_view_state *s)
5597 6458efa5 2020-11-24 stsp const struct got_error *err;
5598 6458efa5 2020-11-24 stsp struct got_reflist_entry *sre;
5599 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5601 6458efa5 2020-11-24 stsp err = got_ref_list(&s->simplerefs, s->repo, NULL,
5602 6458efa5 2020-11-24 stsp got_ref_cmp_by_name, NULL);
5604 6458efa5 2020-11-24 stsp return err;
5606 6458efa5 2020-11-24 stsp s->nrefs = 0;
5607 6458efa5 2020-11-24 stsp SIMPLEQ_FOREACH(sre, &s->simplerefs, entry) {
5608 6458efa5 2020-11-24 stsp if (strncmp(got_ref_get_name(sre->ref), "refs/got/", 9) == 0)
5611 6458efa5 2020-11-24 stsp re = malloc(sizeof(*re));
5612 6458efa5 2020-11-24 stsp if (re == NULL)
5613 6458efa5 2020-11-24 stsp return got_error_from_errno("malloc");
5615 6458efa5 2020-11-24 stsp re->ref = sre->ref;
5616 6458efa5 2020-11-24 stsp re->idx = s->nrefs++;
5617 6458efa5 2020-11-24 stsp TAILQ_INSERT_TAIL(&s->refs, re, entry);
5620 6458efa5 2020-11-24 stsp return NULL;
5624 6458efa5 2020-11-24 stsp ref_view_free_refs(struct tog_ref_view_state *s)
5626 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5628 6458efa5 2020-11-24 stsp while (!TAILQ_EMPTY(&s->refs)) {
5629 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5630 6458efa5 2020-11-24 stsp TAILQ_REMOVE(&s->refs, re, entry);
5633 6458efa5 2020-11-24 stsp got_ref_list_free(&s->simplerefs);
5636 6458efa5 2020-11-24 stsp static const struct got_error *
5637 6458efa5 2020-11-24 stsp open_ref_view(struct tog_view *view, struct got_repository *repo)
5639 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5640 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5642 6458efa5 2020-11-24 stsp s->selected_entry = 0;
5643 6458efa5 2020-11-24 stsp s->repo = repo;
5645 6458efa5 2020-11-24 stsp SIMPLEQ_INIT(&s->simplerefs);
5646 6458efa5 2020-11-24 stsp TAILQ_INIT(&s->refs);
5647 6458efa5 2020-11-24 stsp SIMPLEQ_INIT(&s->colors);
5649 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
5651 6458efa5 2020-11-24 stsp return err;
5653 34ba6917 2020-11-30 stsp s->first_displayed_entry = TAILQ_FIRST(&s->refs);
5655 6458efa5 2020-11-24 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
5656 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/heads/",
5657 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_HEADS,
5658 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_HEADS"));
5660 6458efa5 2020-11-24 stsp goto done;
5662 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/tags/",
5663 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_TAGS,
5664 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_TAGS"));
5666 6458efa5 2020-11-24 stsp goto done;
5668 6458efa5 2020-11-24 stsp err = add_color(&s->colors, "^refs/remotes/",
5669 6458efa5 2020-11-24 stsp TOG_COLOR_REFS_REMOTES,
5670 6458efa5 2020-11-24 stsp get_color_value("TOG_COLOR_REFS_REMOTES"));
5672 6458efa5 2020-11-24 stsp goto done;
5675 6458efa5 2020-11-24 stsp view->show = show_ref_view;
5676 6458efa5 2020-11-24 stsp view->input = input_ref_view;
5677 6458efa5 2020-11-24 stsp view->close = close_ref_view;
5678 6458efa5 2020-11-24 stsp view->search_start = search_start_ref_view;
5679 6458efa5 2020-11-24 stsp view->search_next = search_next_ref_view;
5682 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5683 6458efa5 2020-11-24 stsp return err;
5686 6458efa5 2020-11-24 stsp static const struct got_error *
5687 6458efa5 2020-11-24 stsp close_ref_view(struct tog_view *view)
5689 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5691 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
5692 6458efa5 2020-11-24 stsp free_colors(&s->colors);
5694 6458efa5 2020-11-24 stsp return NULL;
5697 6458efa5 2020-11-24 stsp static const struct got_error *
5698 c42c9805 2020-11-24 stsp resolve_reflist_entry(struct got_object_id **commit_id,
5699 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5701 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5702 c42c9805 2020-11-24 stsp struct got_object_id *obj_id;
5703 6458efa5 2020-11-24 stsp struct got_tag_object *tag = NULL;
5704 6458efa5 2020-11-24 stsp int obj_type;
5706 c42c9805 2020-11-24 stsp *commit_id = NULL;
5708 6458efa5 2020-11-24 stsp err = got_ref_resolve(&obj_id, repo, re->ref);
5710 6458efa5 2020-11-24 stsp return err;
5712 6458efa5 2020-11-24 stsp err = got_object_get_type(&obj_type, repo, obj_id);
5714 6458efa5 2020-11-24 stsp goto done;
5716 6458efa5 2020-11-24 stsp switch (obj_type) {
5717 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_COMMIT:
5718 c42c9805 2020-11-24 stsp *commit_id = obj_id;
5720 6458efa5 2020-11-24 stsp case GOT_OBJ_TYPE_TAG:
5721 6458efa5 2020-11-24 stsp err = got_object_open_as_tag(&tag, repo, obj_id);
5723 6458efa5 2020-11-24 stsp goto done;
5724 c42c9805 2020-11-24 stsp free(obj_id);
5725 c42c9805 2020-11-24 stsp err = got_object_get_type(&obj_type, repo,
5726 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5728 6458efa5 2020-11-24 stsp goto done;
5729 c42c9805 2020-11-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT) {
5730 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5731 c42c9805 2020-11-24 stsp goto done;
5733 c42c9805 2020-11-24 stsp *commit_id = got_object_id_dup(
5734 c42c9805 2020-11-24 stsp got_object_tag_get_object_id(tag));
5735 c42c9805 2020-11-24 stsp if (*commit_id == NULL) {
5736 c42c9805 2020-11-24 stsp err = got_error_from_errno("got_object_id_dup");
5737 c42c9805 2020-11-24 stsp goto done;
5741 c42c9805 2020-11-24 stsp err = got_error(GOT_ERR_OBJ_TYPE);
5747 c42c9805 2020-11-24 stsp got_object_tag_close(tag);
5748 c42c9805 2020-11-24 stsp if (err) {
5749 c42c9805 2020-11-24 stsp free(*commit_id);
5750 c42c9805 2020-11-24 stsp *commit_id = NULL;
5752 c42c9805 2020-11-24 stsp return err;
5755 c42c9805 2020-11-24 stsp static const struct got_error *
5756 c42c9805 2020-11-24 stsp log_ref_entry(struct tog_view **new_view, int begin_x,
5757 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
5759 c42c9805 2020-11-24 stsp struct tog_view *log_view;
5760 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
5761 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL;
5763 c42c9805 2020-11-24 stsp *new_view = NULL;
5765 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
5766 c42c9805 2020-11-24 stsp if (err) {
5767 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
5768 c42c9805 2020-11-24 stsp return err;
5770 c42c9805 2020-11-24 stsp return NULL;
5773 6458efa5 2020-11-24 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5774 6458efa5 2020-11-24 stsp if (log_view == NULL) {
5775 6458efa5 2020-11-24 stsp err = got_error_from_errno("view_open");
5776 6458efa5 2020-11-24 stsp goto done;
5779 ee756517 2020-12-05 stsp err = open_log_view(log_view, commit_id, repo,
5780 ee756517 2020-12-05 stsp got_ref_get_name(re->ref), "", 0);
5783 6458efa5 2020-11-24 stsp view_close(log_view);
5785 6458efa5 2020-11-24 stsp *new_view = log_view;
5786 c42c9805 2020-11-24 stsp free(commit_id);
5787 6458efa5 2020-11-24 stsp return err;
5790 ce5b7c56 2019-07-09 stsp static void
5791 3e135950 2020-12-01 naddy ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
5793 34ba6917 2020-11-30 stsp struct tog_reflist_entry *re;
5794 34ba6917 2020-11-30 stsp int i = 0;
5796 694d3271 2020-12-01 naddy if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
5799 694d3271 2020-12-01 naddy re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
5800 34ba6917 2020-11-30 stsp while (i++ < maxscroll) {
5801 34ba6917 2020-11-30 stsp if (re == NULL)
5803 694d3271 2020-12-01 naddy s->first_displayed_entry = re;
5804 34ba6917 2020-11-30 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5808 34ba6917 2020-11-30 stsp static void
5809 3e135950 2020-12-01 naddy ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
5811 6458efa5 2020-11-24 stsp struct tog_reflist_entry *next, *last;
5812 6458efa5 2020-11-24 stsp int n = 0;
5814 694d3271 2020-12-01 naddy if (s->first_displayed_entry)
5815 694d3271 2020-12-01 naddy next = TAILQ_NEXT(s->first_displayed_entry, entry);
5817 694d3271 2020-12-01 naddy next = TAILQ_FIRST(&s->refs);
5819 694d3271 2020-12-01 naddy last = s->last_displayed_entry;
5820 6458efa5 2020-11-24 stsp while (next && last && n++ < maxscroll) {
5821 6458efa5 2020-11-24 stsp last = TAILQ_NEXT(last, entry);
5822 6458efa5 2020-11-24 stsp if (last) {
5823 694d3271 2020-12-01 naddy s->first_displayed_entry = next;
5824 6458efa5 2020-11-24 stsp next = TAILQ_NEXT(next, entry);
5829 6458efa5 2020-11-24 stsp static const struct got_error *
5830 6458efa5 2020-11-24 stsp search_start_ref_view(struct tog_view *view)
5832 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5834 6458efa5 2020-11-24 stsp s->matched_entry = NULL;
5835 6458efa5 2020-11-24 stsp return NULL;
5838 6458efa5 2020-11-24 stsp static int
5839 6458efa5 2020-11-24 stsp match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
5841 6458efa5 2020-11-24 stsp regmatch_t regmatch;
5843 6458efa5 2020-11-24 stsp return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
5847 6458efa5 2020-11-24 stsp static const struct got_error *
5848 6458efa5 2020-11-24 stsp search_next_ref_view(struct tog_view *view)
5850 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5851 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re = NULL;
5853 6458efa5 2020-11-24 stsp if (!view->searching) {
5854 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5855 6458efa5 2020-11-24 stsp return NULL;
5858 6458efa5 2020-11-24 stsp if (s->matched_entry) {
5859 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5860 6458efa5 2020-11-24 stsp if (s->selected_entry)
5861 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(s->selected_entry, entry);
5863 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
5864 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
5866 6458efa5 2020-11-24 stsp if (s->selected_entry == NULL)
5867 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5869 6458efa5 2020-11-24 stsp re = TAILQ_PREV(s->selected_entry,
5870 6458efa5 2020-11-24 stsp tog_reflist_head, entry);
5873 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5874 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5876 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5879 6458efa5 2020-11-24 stsp while (1) {
5880 6458efa5 2020-11-24 stsp if (re == NULL) {
5881 6458efa5 2020-11-24 stsp if (s->matched_entry == NULL) {
5882 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5883 6458efa5 2020-11-24 stsp return NULL;
5885 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5886 6458efa5 2020-11-24 stsp re = TAILQ_FIRST(&s->refs);
5888 6458efa5 2020-11-24 stsp re = TAILQ_LAST(&s->refs, tog_reflist_head);
5891 6458efa5 2020-11-24 stsp if (match_reflist_entry(re, &view->regex)) {
5892 6458efa5 2020-11-24 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5893 6458efa5 2020-11-24 stsp s->matched_entry = re;
5897 6458efa5 2020-11-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
5898 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
5900 6458efa5 2020-11-24 stsp re = TAILQ_PREV(re, tog_reflist_head, entry);
5903 6458efa5 2020-11-24 stsp if (s->matched_entry) {
5904 6458efa5 2020-11-24 stsp s->first_displayed_entry = s->matched_entry;
5905 6458efa5 2020-11-24 stsp s->selected = 0;
5908 6458efa5 2020-11-24 stsp return NULL;
5911 6458efa5 2020-11-24 stsp static const struct got_error *
5912 6458efa5 2020-11-24 stsp show_ref_view(struct tog_view *view)
5914 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
5915 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
5916 6458efa5 2020-11-24 stsp struct tog_reflist_entry *re;
5917 6458efa5 2020-11-24 stsp char *line = NULL;
5918 6458efa5 2020-11-24 stsp wchar_t *wline;
5919 6458efa5 2020-11-24 stsp struct tog_color *tc;
5920 6458efa5 2020-11-24 stsp int width, n;
5921 6458efa5 2020-11-24 stsp int limit = view->nlines;
5923 6458efa5 2020-11-24 stsp werase(view->window);
5925 6458efa5 2020-11-24 stsp s->ndisplayed = 0;
5927 6458efa5 2020-11-24 stsp if (limit == 0)
5928 6458efa5 2020-11-24 stsp return NULL;
5930 34ba6917 2020-11-30 stsp re = s->first_displayed_entry;
5932 6458efa5 2020-11-24 stsp if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
5933 6458efa5 2020-11-24 stsp s->nrefs) == -1)
5934 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
5936 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5937 6458efa5 2020-11-24 stsp if (err) {
5938 6458efa5 2020-11-24 stsp free(line);
5939 6458efa5 2020-11-24 stsp return err;
5941 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
5942 6458efa5 2020-11-24 stsp wstandout(view->window);
5943 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
5944 6458efa5 2020-11-24 stsp if (view_needs_focus_indication(view))
5945 6458efa5 2020-11-24 stsp wstandend(view->window);
5946 6458efa5 2020-11-24 stsp free(wline);
5947 6458efa5 2020-11-24 stsp wline = NULL;
5948 6458efa5 2020-11-24 stsp free(line);
5949 6458efa5 2020-11-24 stsp line = NULL;
5950 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
5951 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
5952 6458efa5 2020-11-24 stsp if (--limit <= 0)
5953 6458efa5 2020-11-24 stsp return NULL;
5956 6458efa5 2020-11-24 stsp while (re && limit > 0) {
5957 6458efa5 2020-11-24 stsp char *line = NULL;
5959 6458efa5 2020-11-24 stsp if (got_ref_is_symbolic(re->ref)) {
5960 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s -> %s",
5961 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref),
5962 6458efa5 2020-11-24 stsp got_ref_get_symref_target(re->ref)) == -1)
5963 6458efa5 2020-11-24 stsp return got_error_from_errno("asprintf");
5964 6458efa5 2020-11-24 stsp } else if (s->show_ids) {
5965 6458efa5 2020-11-24 stsp struct got_object_id *id;
5966 6458efa5 2020-11-24 stsp char *id_str;
5967 6458efa5 2020-11-24 stsp err = got_ref_resolve(&id, s->repo, re->ref);
5969 6458efa5 2020-11-24 stsp return err;
5970 6458efa5 2020-11-24 stsp err = got_object_id_str(&id_str, id);
5971 6458efa5 2020-11-24 stsp if (err) {
5973 6458efa5 2020-11-24 stsp return err;
5975 6458efa5 2020-11-24 stsp if (asprintf(&line, "%s: %s",
5976 6458efa5 2020-11-24 stsp got_ref_get_name(re->ref), id_str) == -1) {
5977 6458efa5 2020-11-24 stsp err = got_error_from_errno("asprintf");
5979 6458efa5 2020-11-24 stsp free(id_str);
5980 6458efa5 2020-11-24 stsp return err;
5983 6458efa5 2020-11-24 stsp free(id_str);
5985 6458efa5 2020-11-24 stsp line = strdup(got_ref_get_name(re->ref));
5986 6458efa5 2020-11-24 stsp if (line == NULL)
5987 6458efa5 2020-11-24 stsp return got_error_from_errno("strdup");
5990 6458efa5 2020-11-24 stsp err = format_line(&wline, &width, line, view->ncols, 0);
5991 6458efa5 2020-11-24 stsp if (err) {
5992 6458efa5 2020-11-24 stsp free(line);
5993 6458efa5 2020-11-24 stsp return err;
5995 6458efa5 2020-11-24 stsp if (n == s->selected) {
5996 6458efa5 2020-11-24 stsp if (view->focussed)
5997 6458efa5 2020-11-24 stsp wstandout(view->window);
5998 6458efa5 2020-11-24 stsp s->selected_entry = re;
6000 6458efa5 2020-11-24 stsp tc = match_color(&s->colors, got_ref_get_name(re->ref));
6002 6458efa5 2020-11-24 stsp wattr_on(view->window,
6003 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6004 6458efa5 2020-11-24 stsp waddwstr(view->window, wline);
6006 6458efa5 2020-11-24 stsp wattr_off(view->window,
6007 6458efa5 2020-11-24 stsp COLOR_PAIR(tc->colorpair), NULL);
6008 6458efa5 2020-11-24 stsp if (width < view->ncols - 1)
6009 6458efa5 2020-11-24 stsp waddch(view->window, '\n');
6010 6458efa5 2020-11-24 stsp if (n == s->selected && view->focussed)
6011 6458efa5 2020-11-24 stsp wstandend(view->window);
6012 6458efa5 2020-11-24 stsp free(line);
6013 6458efa5 2020-11-24 stsp free(wline);
6014 6458efa5 2020-11-24 stsp wline = NULL;
6016 6458efa5 2020-11-24 stsp s->ndisplayed++;
6017 6458efa5 2020-11-24 stsp s->last_displayed_entry = re;
6020 6458efa5 2020-11-24 stsp re = TAILQ_NEXT(re, entry);
6023 6458efa5 2020-11-24 stsp view_vborder(view);
6024 6458efa5 2020-11-24 stsp return err;
6027 6458efa5 2020-11-24 stsp static const struct got_error *
6028 c42c9805 2020-11-24 stsp browse_ref_tree(struct tog_view **new_view, int begin_x,
6029 c42c9805 2020-11-24 stsp struct tog_reflist_entry *re, struct got_repository *repo)
6031 c42c9805 2020-11-24 stsp const struct got_error *err = NULL;
6032 c42c9805 2020-11-24 stsp struct got_object_id *commit_id = NULL, *tree_id = NULL;
6033 c42c9805 2020-11-24 stsp struct got_tree_object *tree = NULL;
6034 c42c9805 2020-11-24 stsp struct tog_view *tree_view;
6036 c42c9805 2020-11-24 stsp *new_view = NULL;
6038 c42c9805 2020-11-24 stsp err = resolve_reflist_entry(&commit_id, re, repo);
6039 c42c9805 2020-11-24 stsp if (err) {
6040 c42c9805 2020-11-24 stsp if (err->code != GOT_ERR_OBJ_TYPE)
6041 c42c9805 2020-11-24 stsp return err;
6043 c42c9805 2020-11-24 stsp return NULL;
6046 c42c9805 2020-11-24 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, "/");
6048 c42c9805 2020-11-24 stsp goto done;
6050 c42c9805 2020-11-24 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
6052 c42c9805 2020-11-24 stsp goto done;
6054 c42c9805 2020-11-24 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6055 c42c9805 2020-11-24 stsp if (tree_view == NULL) {
6056 c42c9805 2020-11-24 stsp err = got_error_from_errno("view_open");
6057 c42c9805 2020-11-24 stsp goto done;
6060 4e97c21c 2020-12-06 stsp err = open_tree_view(tree_view, tree, commit_id,
6061 4e97c21c 2020-12-06 stsp got_ref_get_name(re->ref), repo);
6063 c42c9805 2020-11-24 stsp goto done;
6065 c42c9805 2020-11-24 stsp *new_view = tree_view;
6067 c42c9805 2020-11-24 stsp free(commit_id);
6068 c42c9805 2020-11-24 stsp free(tree_id);
6069 c42c9805 2020-11-24 stsp if (err) {
6071 c42c9805 2020-11-24 stsp got_object_tree_close(tree);
6073 c42c9805 2020-11-24 stsp return err;
6075 c42c9805 2020-11-24 stsp static const struct got_error *
6076 e78dc838 2020-12-04 stsp input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6078 6458efa5 2020-11-24 stsp const struct got_error *err = NULL;
6079 6458efa5 2020-11-24 stsp struct tog_ref_view_state *s = &view->state.ref;
6080 c42c9805 2020-11-24 stsp struct tog_view *log_view, *tree_view;
6081 34ba6917 2020-11-30 stsp int begin_x = 0;
6083 6458efa5 2020-11-24 stsp switch (ch) {
6085 6458efa5 2020-11-24 stsp s->show_ids = !s->show_ids;
6087 6458efa5 2020-11-24 stsp case KEY_ENTER:
6088 6458efa5 2020-11-24 stsp case '\r':
6089 6458efa5 2020-11-24 stsp if (!s->selected_entry)
6091 6458efa5 2020-11-24 stsp if (view_is_parent_view(view))
6092 6458efa5 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6093 6458efa5 2020-11-24 stsp err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6095 e78dc838 2020-12-04 stsp view->focussed = 0;
6096 e78dc838 2020-12-04 stsp log_view->focussed = 1;
6097 6458efa5 2020-11-24 stsp if (view_is_parent_view(view)) {
6098 6458efa5 2020-11-24 stsp err = view_close_child(view);
6100 6458efa5 2020-11-24 stsp return err;
6101 72a9cb46 2020-12-03 stsp view_set_child(view, log_view);
6102 e78dc838 2020-12-04 stsp view->focus_child = 1;
6104 6458efa5 2020-11-24 stsp *new_view = log_view;
6107 c42c9805 2020-11-24 stsp if (!s->selected_entry)
6109 c42c9805 2020-11-24 stsp if (view_is_parent_view(view))
6110 c42c9805 2020-11-24 stsp begin_x = view_split_begin_x(view->begin_x);
6111 c42c9805 2020-11-24 stsp err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6113 c42c9805 2020-11-24 stsp if (err || tree_view == NULL)
6115 e78dc838 2020-12-04 stsp view->focussed = 0;
6116 e78dc838 2020-12-04 stsp tree_view->focussed = 1;
6117 c42c9805 2020-11-24 stsp if (view_is_parent_view(view)) {
6118 c42c9805 2020-11-24 stsp err = view_close_child(view);
6120 c42c9805 2020-11-24 stsp return err;
6121 72a9cb46 2020-12-03 stsp view_set_child(view, tree_view);
6122 e78dc838 2020-12-04 stsp view->focus_child = 1;
6124 c42c9805 2020-11-24 stsp *new_view = tree_view;
6127 6458efa5 2020-11-24 stsp case KEY_UP:
6128 6458efa5 2020-11-24 stsp if (s->selected > 0) {
6129 6458efa5 2020-11-24 stsp s->selected--;
6132 3e135950 2020-12-01 naddy ref_scroll_up(s, 1);
6134 6458efa5 2020-11-24 stsp case KEY_PPAGE:
6135 6458efa5 2020-11-24 stsp case CTRL('b'):
6136 34ba6917 2020-11-30 stsp if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6137 34ba6917 2020-11-30 stsp s->selected = 0;
6138 3e135950 2020-12-01 naddy ref_scroll_up(s, MAX(0, view->nlines - 1));
6141 6458efa5 2020-11-24 stsp case KEY_DOWN:
6142 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1) {
6143 6458efa5 2020-11-24 stsp s->selected++;
6146 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6147 6458efa5 2020-11-24 stsp /* can't scroll any further */
6149 3e135950 2020-12-01 naddy ref_scroll_down(s, 1);
6151 6458efa5 2020-11-24 stsp case KEY_NPAGE:
6152 6458efa5 2020-11-24 stsp case CTRL('f'):
6153 6458efa5 2020-11-24 stsp if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6154 6458efa5 2020-11-24 stsp /* can't scroll any further; move cursor down */
6155 6458efa5 2020-11-24 stsp if (s->selected < s->ndisplayed - 1)
6156 6458efa5 2020-11-24 stsp s->selected = s->ndisplayed - 1;
6159 3e135950 2020-12-01 naddy ref_scroll_down(s, view->nlines - 1);
6161 6458efa5 2020-11-24 stsp case CTRL('l'):
6162 6458efa5 2020-11-24 stsp ref_view_free_refs(s);
6163 6458efa5 2020-11-24 stsp err = ref_view_load_refs(s);
6165 6458efa5 2020-11-24 stsp case KEY_RESIZE:
6166 8b5b8d0c 2020-12-06 stsp if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6167 8b5b8d0c 2020-12-06 stsp s->selected = view->nlines - 2;
6173 6458efa5 2020-11-24 stsp return err;
6176 6458efa5 2020-11-24 stsp __dead static void
6177 6458efa5 2020-11-24 stsp usage_ref(void)
6180 6458efa5 2020-11-24 stsp fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6181 6458efa5 2020-11-24 stsp getprogname());
6185 6458efa5 2020-11-24 stsp static const struct got_error *
6186 6458efa5 2020-11-24 stsp cmd_ref(int argc, char *argv[])
6188 6458efa5 2020-11-24 stsp const struct got_error *error;
6189 6458efa5 2020-11-24 stsp struct got_repository *repo = NULL;
6190 6458efa5 2020-11-24 stsp struct got_worktree *worktree = NULL;
6191 6458efa5 2020-11-24 stsp char *cwd = NULL, *repo_path = NULL;
6193 6458efa5 2020-11-24 stsp struct tog_view *view;
6195 6458efa5 2020-11-24 stsp while ((ch = getopt(argc, argv, "r:")) != -1) {
6196 6458efa5 2020-11-24 stsp switch (ch) {
6198 6458efa5 2020-11-24 stsp repo_path = realpath(optarg, NULL);
6199 6458efa5 2020-11-24 stsp if (repo_path == NULL)
6200 6458efa5 2020-11-24 stsp return got_error_from_errno2("realpath",
6204 e99e2d15 2020-11-24 naddy usage_ref();
6205 6458efa5 2020-11-24 stsp /* NOTREACHED */
6209 6458efa5 2020-11-24 stsp argc -= optind;
6210 6458efa5 2020-11-24 stsp argv += optind;
6212 6458efa5 2020-11-24 stsp if (argc > 1)
6213 e99e2d15 2020-11-24 naddy usage_ref();
6215 6458efa5 2020-11-24 stsp cwd = getcwd(NULL, 0);
6216 6458efa5 2020-11-24 stsp if (cwd == NULL)
6217 6458efa5 2020-11-24 stsp return got_error_from_errno("getcwd");
6219 6458efa5 2020-11-24 stsp error = got_worktree_open(&worktree, cwd);
6220 6458efa5 2020-11-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6221 6458efa5 2020-11-24 stsp goto done;
6223 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6224 6458efa5 2020-11-24 stsp if (worktree)
6225 6458efa5 2020-11-24 stsp repo_path =
6226 6458efa5 2020-11-24 stsp strdup(got_worktree_get_repo_path(worktree));
6228 6458efa5 2020-11-24 stsp repo_path = strdup(cwd);
6230 6458efa5 2020-11-24 stsp if (repo_path == NULL) {
6231 6458efa5 2020-11-24 stsp error = got_error_from_errno("strdup");
6232 6458efa5 2020-11-24 stsp goto done;
6235 6458efa5 2020-11-24 stsp error = got_repo_open(&repo, repo_path, NULL);
6236 6458efa5 2020-11-24 stsp if (error != NULL)
6237 6458efa5 2020-11-24 stsp goto done;
6239 6458efa5 2020-11-24 stsp init_curses();
6241 6458efa5 2020-11-24 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
6242 6458efa5 2020-11-24 stsp if (error)
6243 6458efa5 2020-11-24 stsp goto done;
6245 6458efa5 2020-11-24 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6246 6458efa5 2020-11-24 stsp if (view == NULL) {
6247 6458efa5 2020-11-24 stsp error = got_error_from_errno("view_open");
6248 6458efa5 2020-11-24 stsp goto done;
6251 6458efa5 2020-11-24 stsp error = open_ref_view(view, repo);
6252 6458efa5 2020-11-24 stsp if (error)
6253 6458efa5 2020-11-24 stsp goto done;
6255 6458efa5 2020-11-24 stsp if (worktree) {
6256 6458efa5 2020-11-24 stsp /* Release work tree lock. */
6257 6458efa5 2020-11-24 stsp got_worktree_close(worktree);
6258 6458efa5 2020-11-24 stsp worktree = NULL;
6260 6458efa5 2020-11-24 stsp error = view_loop(view);
6262 6458efa5 2020-11-24 stsp free(repo_path);
6263 6458efa5 2020-11-24 stsp free(cwd);
6265 6458efa5 2020-11-24 stsp got_repo_close(repo);
6266 6458efa5 2020-11-24 stsp return error;
6269 6458efa5 2020-11-24 stsp static void
6270 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
6274 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
6275 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
6276 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
6277 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->name);
6279 6879ba42 2020-10-01 naddy fputc('\n', fp);
6282 4ed7e80c 2018-05-20 stsp __dead static void
6283 6879ba42 2020-10-01 naddy usage(int hflag, int status)
6285 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
6287 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
6288 6879ba42 2020-10-01 naddy getprogname());
6289 6879ba42 2020-10-01 naddy if (hflag) {
6290 6879ba42 2020-10-01 naddy fprintf(fp, "lazy usage: %s path\n", getprogname());
6291 6879ba42 2020-10-01 naddy list_commands(fp);
6293 6879ba42 2020-10-01 naddy exit(status);
6296 c2301be8 2018-04-30 stsp static char **
6297 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
6299 ee85c5e8 2020-02-29 stsp va_list ap;
6300 c2301be8 2018-04-30 stsp char **argv;
6303 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
6305 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
6306 c2301be8 2018-04-30 stsp if (argv == NULL)
6307 c2301be8 2018-04-30 stsp err(1, "calloc");
6308 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
6309 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
6310 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
6311 e10c916e 2019-09-15 hiltjo err(1, "strdup");
6314 ee85c5e8 2020-02-29 stsp va_end(ap);
6315 c2301be8 2018-04-30 stsp return argv;
6319 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
6320 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
6321 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
6322 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
6323 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
6325 ee85c5e8 2020-02-29 stsp static const struct got_error *
6326 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
6328 ee85c5e8 2020-02-29 stsp const struct got_error *error = NULL;
6329 ee85c5e8 2020-02-29 stsp struct tog_cmd *cmd = NULL;
6330 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
6331 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
6332 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
6333 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6334 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
6336 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
6337 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
6338 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
6340 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
6341 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6342 ee85c5e8 2020-02-29 stsp goto done;
6344 ee85c5e8 2020-02-29 stsp if (worktree)
6345 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
6347 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
6348 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
6349 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
6350 ee85c5e8 2020-02-29 stsp goto done;
6353 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
6354 ee85c5e8 2020-02-29 stsp if (error != NULL)
6355 ee85c5e8 2020-02-29 stsp goto done;
6357 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6358 ee85c5e8 2020-02-29 stsp repo, worktree);
6359 ee85c5e8 2020-02-29 stsp if (error)
6360 ee85c5e8 2020-02-29 stsp goto done;
6362 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
6363 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
6364 ee85c5e8 2020-02-29 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
6365 ee85c5e8 2020-02-29 stsp if (error)
6366 ee85c5e8 2020-02-29 stsp goto done;
6368 ee85c5e8 2020-02-29 stsp if (worktree) {
6369 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6370 ee85c5e8 2020-02-29 stsp worktree = NULL;
6373 ee85c5e8 2020-02-29 stsp error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
6374 ee85c5e8 2020-02-29 stsp if (error) {
6375 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
6376 ee85c5e8 2020-02-29 stsp goto done;
6377 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
6378 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
6379 6879ba42 2020-10-01 naddy usage(1, 1);
6380 ee85c5e8 2020-02-29 stsp /* not reached */
6383 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
6384 ee85c5e8 2020-02-29 stsp repo = NULL;
6386 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
6387 ee85c5e8 2020-02-29 stsp if (error)
6388 ee85c5e8 2020-02-29 stsp goto done;
6390 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
6392 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
6393 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
6396 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
6397 ee85c5e8 2020-02-29 stsp if (worktree)
6398 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
6400 ee85c5e8 2020-02-29 stsp free(commit_id_str);
6401 ee85c5e8 2020-02-29 stsp free(commit_id);
6402 ee85c5e8 2020-02-29 stsp free(cwd);
6403 ee85c5e8 2020-02-29 stsp free(repo_path);
6404 ee85c5e8 2020-02-29 stsp free(in_repo_path);
6405 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
6407 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
6408 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
6409 ee85c5e8 2020-02-29 stsp free(cmd_argv);
6411 ee85c5e8 2020-02-29 stsp return error;
6415 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
6417 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
6418 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
6419 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
6420 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
6421 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
6422 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
6423 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0}
6426 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
6428 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
6429 9f7d7167 2018-04-29 stsp switch (ch) {
6431 9f7d7167 2018-04-29 stsp hflag = 1;
6434 53ccebc2 2019-07-30 stsp Vflag = 1;
6437 6879ba42 2020-10-01 naddy usage(hflag, 1);
6438 9f7d7167 2018-04-29 stsp /* NOTREACHED */
6442 9f7d7167 2018-04-29 stsp argc -= optind;
6443 9f7d7167 2018-04-29 stsp argv += optind;
6444 9814e6a3 2020-09-27 naddy optind = 1;
6445 c2301be8 2018-04-30 stsp optreset = 1;
6447 53ccebc2 2019-07-30 stsp if (Vflag) {
6448 53ccebc2 2019-07-30 stsp got_version_print_str();
6449 6879ba42 2020-10-01 naddy return 0;
6452 4010e238 2020-12-04 stsp #ifndef PROFILE
6453 4010e238 2020-12-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
6454 4010e238 2020-12-04 stsp NULL) == -1)
6455 4010e238 2020-12-04 stsp err(1, "pledge");
6458 c2301be8 2018-04-30 stsp if (argc == 0) {
6459 f29d3e89 2018-06-23 stsp if (hflag)
6460 6879ba42 2020-10-01 naddy usage(hflag, 0);
6461 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
6462 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
6464 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
6468 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
6469 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
6470 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
6471 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
6472 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
6478 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
6479 ee85c5e8 2020-02-29 stsp if (argc != 1)
6480 6879ba42 2020-10-01 naddy usage(0, 1);
6481 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
6482 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
6484 ee85c5e8 2020-02-29 stsp if (hflag)
6485 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
6487 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
6491 b46c1e04 2020-09-20 naddy putchar('\n');
6492 a2f4a359 2020-02-28 stsp if (cmd_argv) {
6494 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
6495 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
6496 a2f4a359 2020-02-28 stsp free(cmd_argv);
6499 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
6500 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);