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 31120ada 2018-04-30 stsp #include <errno.h>
22 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
23 9f7d7167 2018-04-29 stsp #include <curses.h>
24 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
25 9f7d7167 2018-04-29 stsp #include <panel.h>
26 9f7d7167 2018-04-29 stsp #include <locale.h>
27 61266923 2020-01-14 stsp #include <signal.h>
28 9f7d7167 2018-04-29 stsp #include <stdlib.h>
29 ee85c5e8 2020-02-29 stsp #include <stdarg.h>
30 26ed57b2 2018-05-19 stsp #include <stdio.h>
31 9f7d7167 2018-04-29 stsp #include <getopt.h>
32 9f7d7167 2018-04-29 stsp #include <string.h>
33 9f7d7167 2018-04-29 stsp #include <err.h>
34 80ddbec8 2018-04-29 stsp #include <unistd.h>
35 26ed57b2 2018-05-19 stsp #include <util.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 ce5b7c56 2019-07-09 stsp __dead static void usage(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);
84 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
85 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
86 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
87 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
89 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
90 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
91 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
92 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
93 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
96 d6b05b5b 2018-08-04 stsp enum tog_view_type {
97 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
98 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
99 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
100 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
103 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
105 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
106 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
107 ba4f502b 2018-08-04 stsp struct got_object_id *id;
108 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
111 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
112 ba4f502b 2018-08-04 stsp struct commit_queue {
113 ba4f502b 2018-08-04 stsp int ncommits;
114 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
117 f26dddb7 2019-11-08 stsp struct tog_color {
118 f26dddb7 2019-11-08 stsp SIMPLEQ_ENTRY(tog_color) entry;
119 6d17833f 2019-11-08 stsp regex_t regex;
120 6d17833f 2019-11-08 stsp short colorpair;
122 f26dddb7 2019-11-08 stsp SIMPLEQ_HEAD(tog_colors, tog_color);
124 11b20872 2019-11-08 stsp static const struct got_error *
125 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
126 11b20872 2019-11-08 stsp int idx, short color)
128 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
129 11b20872 2019-11-08 stsp struct tog_color *tc;
130 11b20872 2019-11-08 stsp int regerr = 0;
132 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
133 11b20872 2019-11-08 stsp return NULL;
135 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
137 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
138 11b20872 2019-11-08 stsp if (tc == NULL)
139 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
140 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
141 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
142 11b20872 2019-11-08 stsp if (regerr) {
143 11b20872 2019-11-08 stsp static char regerr_msg[512];
144 11b20872 2019-11-08 stsp static char err_msg[512];
145 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
146 11b20872 2019-11-08 stsp sizeof(regerr_msg));
147 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
148 11b20872 2019-11-08 stsp regerr_msg);
149 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
151 11b20872 2019-11-08 stsp return err;
153 11b20872 2019-11-08 stsp tc->colorpair = idx;
154 11b20872 2019-11-08 stsp SIMPLEQ_INSERT_HEAD(colors, tc, entry);
155 11b20872 2019-11-08 stsp return NULL;
158 11b20872 2019-11-08 stsp static void
159 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
161 11b20872 2019-11-08 stsp struct tog_color *tc;
163 11b20872 2019-11-08 stsp while (!SIMPLEQ_EMPTY(colors)) {
164 11b20872 2019-11-08 stsp tc = SIMPLEQ_FIRST(colors);
165 11b20872 2019-11-08 stsp SIMPLEQ_REMOVE_HEAD(colors, entry);
166 11b20872 2019-11-08 stsp regfree(&tc->regex);
171 11b20872 2019-11-08 stsp struct tog_color *
172 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
174 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
176 11b20872 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
177 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
181 11b20872 2019-11-08 stsp return NULL;
185 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
187 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
188 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
189 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
190 11b20872 2019-11-08 stsp return COLOR_CYAN;
191 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
192 11b20872 2019-11-08 stsp return COLOR_YELLOW;
193 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
194 11b20872 2019-11-08 stsp return COLOR_GREEN;
195 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
196 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
197 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
198 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
199 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
200 91b8c405 2020-01-25 stsp return COLOR_CYAN;
201 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
202 11b20872 2019-11-08 stsp return COLOR_GREEN;
203 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
204 11b20872 2019-11-08 stsp return COLOR_GREEN;
205 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
206 11b20872 2019-11-08 stsp return COLOR_CYAN;
207 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
208 11b20872 2019-11-08 stsp return COLOR_YELLOW;
214 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
216 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
218 11b20872 2019-11-08 stsp if (val == NULL)
219 11b20872 2019-11-08 stsp return default_color_value(envvar);
221 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
222 11b20872 2019-11-08 stsp return COLOR_BLACK;
223 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
224 11b20872 2019-11-08 stsp return COLOR_RED;
225 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
226 11b20872 2019-11-08 stsp return COLOR_GREEN;
227 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
228 11b20872 2019-11-08 stsp return COLOR_YELLOW;
229 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
230 11b20872 2019-11-08 stsp return COLOR_BLUE;
231 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
232 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
233 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
234 11b20872 2019-11-08 stsp return COLOR_CYAN;
235 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
236 11b20872 2019-11-08 stsp return COLOR_WHITE;
237 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
240 11b20872 2019-11-08 stsp return default_color_value(envvar);
244 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
245 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
247 15a087fe 2019-02-21 stsp int first_displayed_line;
248 15a087fe 2019-02-21 stsp int last_displayed_line;
250 15a087fe 2019-02-21 stsp int diff_context;
251 15a087fe 2019-02-21 stsp struct got_repository *repo;
252 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
253 bddb1296 2019-11-08 stsp struct tog_colors colors;
254 f44b1f58 2020-02-02 tracey int nlines;
255 f44b1f58 2020-02-02 tracey off_t *line_offsets;
256 f44b1f58 2020-02-02 tracey int matched_line;
257 f44b1f58 2020-02-02 tracey int selected_line;
258 f44b1f58 2020-02-02 tracey size_t filesize;
260 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
261 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
264 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
266 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
267 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
268 7c1452c1 2020-03-26 stsp pthread_cond_t commit_loaded;
269 1a76625f 2018-10-22 stsp int commits_needed;
270 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
271 1a76625f 2018-10-22 stsp struct commit_queue *commits;
272 1a76625f 2018-10-22 stsp const char *in_repo_path;
273 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
274 1a76625f 2018-10-22 stsp struct got_repository *repo;
275 1a76625f 2018-10-22 stsp int log_complete;
276 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
277 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
278 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
279 13add988 2019-10-15 stsp int *searching;
280 13add988 2019-10-15 stsp int *search_next_done;
281 13add988 2019-10-15 stsp regex_t *regex;
284 1a76625f 2018-10-22 stsp struct tog_log_view_state {
285 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
286 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
287 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
288 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
289 b01e7d3b 2018-08-04 stsp int selected;
290 b01e7d3b 2018-08-04 stsp char *in_repo_path;
291 d01904d4 2019-06-25 stsp const char *head_ref_name;
292 b672a97a 2020-01-27 stsp int log_branches;
293 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
294 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
295 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
296 1a76625f 2018-10-22 stsp sig_atomic_t quit;
297 1a76625f 2018-10-22 stsp pthread_t thread;
298 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
299 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
300 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
301 11b20872 2019-11-08 stsp struct tog_colors colors;
304 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
305 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
306 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
307 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
308 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
309 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
310 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
311 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
312 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
313 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
314 11b20872 2019-11-08 stsp #define TOG_COLOR_DATE 11
316 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
317 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
318 e9424729 2018-08-04 stsp int nlines;
320 e9424729 2018-08-04 stsp struct tog_view *view;
321 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
325 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
326 e9424729 2018-08-04 stsp const char *path;
327 e9424729 2018-08-04 stsp struct got_repository *repo;
328 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
329 e9424729 2018-08-04 stsp int *complete;
330 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
331 fc06ba56 2019-08-22 stsp void *cancel_arg;
334 e9424729 2018-08-04 stsp struct tog_blame {
336 e9424729 2018-08-04 stsp size_t filesize;
337 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
338 6fcac457 2018-11-19 stsp int nlines;
339 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
340 e9424729 2018-08-04 stsp pthread_t thread;
341 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
342 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
343 e9424729 2018-08-04 stsp const char *path;
346 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
347 7cbe629d 2018-08-04 stsp int first_displayed_line;
348 7cbe629d 2018-08-04 stsp int last_displayed_line;
349 7cbe629d 2018-08-04 stsp int selected_line;
350 7cbe629d 2018-08-04 stsp int blame_complete;
353 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
354 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
355 e5a0f69f 2018-08-18 stsp char *path;
356 7cbe629d 2018-08-04 stsp struct got_repository *repo;
357 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
358 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
359 e9424729 2018-08-04 stsp struct tog_blame blame;
360 6c4c42e0 2019-06-24 stsp int matched_line;
361 11b20872 2019-11-08 stsp struct tog_colors colors;
364 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
365 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
366 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
367 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
368 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
369 ad80ab7b 2018-08-04 stsp int selected;
372 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
374 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
375 ad80ab7b 2018-08-04 stsp char *tree_label;
376 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
377 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
378 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
379 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
380 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
381 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
382 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
383 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
384 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
385 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
386 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
387 bddb1296 2019-11-08 stsp struct tog_colors colors;
391 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
393 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
394 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
395 669b5ffa 2018-10-07 stsp * there is enough screen estate.
397 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
398 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
400 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
401 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
402 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
404 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
405 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
407 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
409 cc3c9aac 2018-08-01 stsp struct tog_view {
410 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
411 26ed57b2 2018-05-19 stsp WINDOW *window;
412 26ed57b2 2018-05-19 stsp PANEL *panel;
413 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
414 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
415 1004088d 2018-09-29 stsp int focussed;
416 669b5ffa 2018-10-07 stsp struct tog_view *parent;
417 669b5ffa 2018-10-07 stsp struct tog_view *child;
418 669b5ffa 2018-10-07 stsp int child_focussed;
420 5dc9f4bc 2018-08-04 stsp /* type-specific state */
421 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
423 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
424 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
425 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
426 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
429 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
430 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
431 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
432 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
434 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
435 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
436 60493ae3 2019-06-20 stsp int searching;
437 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
438 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
439 60493ae3 2019-06-20 stsp int search_next_done;
440 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_HAVE_MORE 1
441 8f4ed634 2020-03-26 stsp #define TOG_SEARCH_NO_MORE 2
442 f9967bca 2020-03-27 stsp #define TOG_SEARCH_HAVE_NONE 3
443 1803e47f 2019-06-22 stsp regex_t regex;
446 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
447 fb872ab2 2019-02-21 stsp struct got_object_id *, struct got_object_id *, struct tog_view *,
448 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
449 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
450 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
451 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
452 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
453 f44b1f58 2020-02-02 tracey static const struct got_error *search_start_diff_view(struct tog_view *);
454 f44b1f58 2020-02-02 tracey static const struct got_error *search_next_diff_view(struct tog_view *);
456 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
457 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *,
458 f135c941 2020-02-20 stsp struct got_repository *, const char *, const char *, int);
459 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
460 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
461 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
462 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
463 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
464 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
466 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
467 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *, struct got_repository *);
468 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
469 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
470 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
471 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
472 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
473 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
475 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
476 55cccc34 2020-02-20 stsp struct got_tree_object *, struct got_object_id *, struct got_reflist_head *,
477 55cccc34 2020-02-20 stsp struct got_repository *);
478 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
479 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
480 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
481 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
482 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
483 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
485 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
486 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
487 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
489 25791caa 2018-10-24 stsp static void
490 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
492 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
495 83baff54 2019-08-12 stsp static void
496 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
498 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
501 61266923 2020-01-14 stsp static void
502 61266923 2020-01-14 stsp tog_sigcont(int signo)
504 61266923 2020-01-14 stsp tog_sigcont_received = 1;
507 e5a0f69f 2018-08-18 stsp static const struct got_error *
508 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
510 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
512 669b5ffa 2018-10-07 stsp if (view->child) {
513 669b5ffa 2018-10-07 stsp view_close(view->child);
514 669b5ffa 2018-10-07 stsp view->child = NULL;
516 e5a0f69f 2018-08-18 stsp if (view->close)
517 e5a0f69f 2018-08-18 stsp err = view->close(view);
518 ea5e7bb5 2018-08-01 stsp if (view->panel)
519 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
520 ea5e7bb5 2018-08-01 stsp if (view->window)
521 ea5e7bb5 2018-08-01 stsp delwin(view->window);
522 ea5e7bb5 2018-08-01 stsp free(view);
523 e5a0f69f 2018-08-18 stsp return err;
526 ea5e7bb5 2018-08-01 stsp static struct tog_view *
527 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
528 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
530 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
532 ea5e7bb5 2018-08-01 stsp if (view == NULL)
533 ea5e7bb5 2018-08-01 stsp return NULL;
535 d6b05b5b 2018-08-04 stsp view->type = type;
536 f7d12f7e 2018-08-01 stsp view->lines = LINES;
537 f7d12f7e 2018-08-01 stsp view->cols = COLS;
538 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
539 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
540 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
541 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
542 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
543 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
544 96a765a8 2018-08-04 stsp view_close(view);
545 ea5e7bb5 2018-08-01 stsp return NULL;
547 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
548 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
549 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
550 96a765a8 2018-08-04 stsp view_close(view);
551 ea5e7bb5 2018-08-01 stsp return NULL;
554 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
555 ea5e7bb5 2018-08-01 stsp return view;
559 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
561 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
563 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
566 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
568 5c60c32a 2018-10-18 stsp static const struct got_error *
569 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
571 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
573 5c60c32a 2018-10-18 stsp view->begin_y = 0;
574 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
575 5c60c32a 2018-10-18 stsp view->nlines = LINES;
576 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
577 5c60c32a 2018-10-18 stsp view->lines = LINES;
578 5c60c32a 2018-10-18 stsp view->cols = COLS;
579 5c60c32a 2018-10-18 stsp err = view_resize(view);
581 5c60c32a 2018-10-18 stsp return err;
583 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
584 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
586 5c60c32a 2018-10-18 stsp return NULL;
589 5c60c32a 2018-10-18 stsp static const struct got_error *
590 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
592 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
594 5c60c32a 2018-10-18 stsp view->begin_x = 0;
595 5c60c32a 2018-10-18 stsp view->begin_y = 0;
596 5c60c32a 2018-10-18 stsp view->nlines = LINES;
597 5c60c32a 2018-10-18 stsp view->ncols = COLS;
598 5c60c32a 2018-10-18 stsp view->lines = LINES;
599 5c60c32a 2018-10-18 stsp view->cols = COLS;
600 5c60c32a 2018-10-18 stsp err = view_resize(view);
602 5c60c32a 2018-10-18 stsp return err;
604 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
605 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
607 5c60c32a 2018-10-18 stsp return NULL;
611 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
613 5c60c32a 2018-10-18 stsp return view->parent == NULL;
616 4d8c2215 2018-08-19 stsp static const struct got_error *
617 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
619 f7d12f7e 2018-08-01 stsp int nlines, ncols;
621 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
622 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
624 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
626 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
627 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
629 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
631 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
632 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
633 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
634 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
635 25791caa 2018-10-24 stsp wclear(view->window);
637 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
638 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
639 0cf4efb1 2018-09-29 stsp view->lines = LINES;
640 0cf4efb1 2018-09-29 stsp view->cols = COLS;
642 6e3e5d9c 2018-10-18 stsp if (view->child) {
643 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
644 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
645 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
646 5c60c32a 2018-10-18 stsp if (view->child->focussed)
647 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
649 5c60c32a 2018-10-18 stsp show_panel(view->panel);
651 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
652 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
656 5c60c32a 2018-10-18 stsp return NULL;
659 669b5ffa 2018-10-07 stsp static const struct got_error *
660 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
662 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
664 669b5ffa 2018-10-07 stsp if (view->child == NULL)
665 669b5ffa 2018-10-07 stsp return NULL;
667 669b5ffa 2018-10-07 stsp err = view_close(view->child);
668 669b5ffa 2018-10-07 stsp view->child = NULL;
669 669b5ffa 2018-10-07 stsp return err;
672 669b5ffa 2018-10-07 stsp static const struct got_error *
673 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
675 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
677 669b5ffa 2018-10-07 stsp view->child = child;
678 669b5ffa 2018-10-07 stsp child->parent = view;
679 669b5ffa 2018-10-07 stsp return err;
683 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
685 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
688 34bc9ec9 2019-02-22 stsp static void
689 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
691 25791caa 2018-10-24 stsp int cols, lines;
692 25791caa 2018-10-24 stsp struct winsize size;
694 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
695 25791caa 2018-10-24 stsp cols = 80; /* Default */
696 25791caa 2018-10-24 stsp lines = 24;
698 25791caa 2018-10-24 stsp cols = size.ws_col;
699 25791caa 2018-10-24 stsp lines = size.ws_row;
701 25791caa 2018-10-24 stsp resize_term(lines, cols);
704 2b49a8ae 2019-06-22 stsp static const struct got_error *
705 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
707 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
708 2b49a8ae 2019-06-22 stsp char pattern[1024];
711 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
712 2b49a8ae 2019-06-22 stsp return NULL;
714 cb1159f8 2020-02-19 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
715 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
717 2b49a8ae 2019-06-22 stsp nocbreak();
719 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
722 2b49a8ae 2019-06-22 stsp if (ret == ERR)
723 2b49a8ae 2019-06-22 stsp return NULL;
725 2b49a8ae 2019-06-22 stsp if (view->searching) {
726 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
727 2b49a8ae 2019-06-22 stsp view->searching = 0;
730 2b49a8ae 2019-06-22 stsp if (regcomp(&view->regex, pattern,
731 f5daf9b1 2019-06-24 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
732 7c32bd05 2019-06-22 stsp err = view->search_start(view);
734 7c32bd05 2019-06-22 stsp regfree(&view->regex);
735 7c32bd05 2019-06-22 stsp return err;
737 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
738 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
739 2b49a8ae 2019-06-22 stsp view->search_next(view);
742 2b49a8ae 2019-06-22 stsp return NULL;
745 0cf4efb1 2018-09-29 stsp static const struct got_error *
746 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
747 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
748 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
750 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
751 669b5ffa 2018-10-07 stsp struct tog_view *v;
752 1a76625f 2018-10-22 stsp int ch, errcode;
754 e5a0f69f 2018-08-18 stsp *new = NULL;
755 e5a0f69f 2018-08-18 stsp *dead = NULL;
756 0cf4efb1 2018-09-29 stsp *focus = NULL;
758 f9967bca 2020-03-27 stsp /* Clear "no matches" indicator. */
759 f9967bca 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE ||
760 f9967bca 2020-03-27 stsp view->search_next_done == TOG_SEARCH_HAVE_NONE)
761 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
763 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
764 82954512 2020-02-03 stsp errcode = pthread_mutex_unlock(&tog_mutex);
765 82954512 2020-02-03 stsp if (errcode)
766 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
767 82954512 2020-02-03 stsp "pthread_mutex_unlock");
768 82954512 2020-02-03 stsp pthread_yield();
769 82954512 2020-02-03 stsp errcode = pthread_mutex_lock(&tog_mutex);
770 82954512 2020-02-03 stsp if (errcode)
771 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
772 82954512 2020-02-03 stsp "pthread_mutex_lock");
773 60493ae3 2019-06-20 stsp view->search_next(view);
774 60493ae3 2019-06-20 stsp return NULL;
777 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
778 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
779 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
780 1a76625f 2018-10-22 stsp if (errcode)
781 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
782 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
783 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
784 1a76625f 2018-10-22 stsp if (errcode)
785 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
786 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
788 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
789 25791caa 2018-10-24 stsp tog_resizeterm();
790 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
791 61266923 2020-01-14 stsp tog_sigcont_received = 0;
792 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
793 25791caa 2018-10-24 stsp err = view_resize(v);
795 25791caa 2018-10-24 stsp return err;
796 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
798 25791caa 2018-10-24 stsp return err;
802 e5a0f69f 2018-08-18 stsp switch (ch) {
806 1e37a5c2 2019-05-12 jcs if (view->child) {
807 1e37a5c2 2019-05-12 jcs *focus = view->child;
808 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
809 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
810 1e37a5c2 2019-05-12 jcs *focus = view->parent;
811 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
815 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
816 1e37a5c2 2019-05-12 jcs *dead = view;
822 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
823 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
825 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
826 669b5ffa 2018-10-07 stsp *focus = view->child;
827 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
828 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
830 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
833 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
834 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
836 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
837 1e37a5c2 2019-05-12 jcs *focus = view;
838 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
839 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
841 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
845 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
846 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
849 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
852 60493ae3 2019-06-20 stsp if (view->search_start)
853 2b49a8ae 2019-06-22 stsp view_search_start(view);
855 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
859 8f4ed634 2020-03-26 stsp if (view->search_next) {
860 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
861 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
862 60493ae3 2019-06-20 stsp view->search_next_done = 0;
863 60493ae3 2019-06-20 stsp view->search_next(view);
865 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
868 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
872 e5a0f69f 2018-08-18 stsp return err;
876 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
878 0cf4efb1 2018-09-29 stsp PANEL *panel;
879 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
881 669b5ffa 2018-10-07 stsp if (view->parent)
882 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
884 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
885 0cf4efb1 2018-09-29 stsp if (panel == NULL)
888 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
889 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
890 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
894 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
896 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
897 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
899 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
901 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
904 669b5ffa 2018-10-07 stsp return view->focussed;
907 bcbd79e2 2018-08-19 stsp static const struct got_error *
908 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
910 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
911 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
912 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
913 fd823528 2018-10-22 stsp int fast_refresh = 10;
914 1a76625f 2018-10-22 stsp int done = 0, errcode;
916 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
917 1a76625f 2018-10-22 stsp if (errcode)
918 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
920 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
921 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
923 a81bf10d 2018-09-29 stsp main_view = view;
924 1004088d 2018-09-29 stsp view->focussed = 1;
925 878940b7 2018-09-29 stsp err = view->show(view);
927 0cf4efb1 2018-09-29 stsp return err;
928 0cf4efb1 2018-09-29 stsp update_panels();
929 0cf4efb1 2018-09-29 stsp doupdate();
930 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
931 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
932 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
933 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
935 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
936 e4197bf9 2018-08-18 stsp view, &views);
939 e5a0f69f 2018-08-18 stsp if (dead_view) {
940 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
942 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
943 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
944 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
945 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
946 669b5ffa 2018-10-07 stsp prev = view->parent;
948 669b5ffa 2018-10-07 stsp if (dead_view->parent)
949 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
951 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
953 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
954 d01904d4 2019-06-25 stsp if (err || (dead_view == main_view && new_view == NULL))
957 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
958 0cf4efb1 2018-09-29 stsp if (focus_view)
959 0cf4efb1 2018-09-29 stsp view = focus_view;
960 669b5ffa 2018-10-07 stsp else if (prev)
961 669b5ffa 2018-10-07 stsp view = prev;
962 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
963 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
964 0cf4efb1 2018-09-29 stsp tog_view_list_head);
966 0cf4efb1 2018-09-29 stsp view = NULL;
967 669b5ffa 2018-10-07 stsp if (view) {
968 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
969 669b5ffa 2018-10-07 stsp focus_view = view->child;
971 669b5ffa 2018-10-07 stsp focus_view = view;
975 bcbd79e2 2018-08-19 stsp if (new_view) {
976 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
977 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
978 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
979 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
981 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
982 86c66b02 2018-10-18 stsp err = view_close(v);
987 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
988 fed7eaa8 2018-10-24 stsp view = new_view;
989 878940b7 2018-09-29 stsp if (focus_view == NULL)
990 878940b7 2018-09-29 stsp focus_view = new_view;
992 0cf4efb1 2018-09-29 stsp if (focus_view) {
993 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
995 0cf4efb1 2018-09-29 stsp view->focussed = 0;
996 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
997 0cf4efb1 2018-09-29 stsp view = focus_view;
998 878940b7 2018-09-29 stsp if (new_view)
999 878940b7 2018-09-29 stsp show_panel(new_view->panel);
1000 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
1001 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
1003 669b5ffa 2018-10-07 stsp if (view) {
1004 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
1005 758194b5 2018-10-24 stsp view->focussed = 1;
1006 758194b5 2018-10-24 stsp show_panel(view->panel);
1007 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
1008 758194b5 2018-10-24 stsp show_panel(view->child->panel);
1009 1a76625f 2018-10-22 stsp focus_view = view;
1011 669b5ffa 2018-10-07 stsp if (view->parent) {
1012 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1014 1a76625f 2018-10-22 stsp goto done;
1016 669b5ffa 2018-10-07 stsp err = view->show(view);
1018 1a76625f 2018-10-22 stsp goto done;
1019 669b5ffa 2018-10-07 stsp if (view->child) {
1020 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1022 1a76625f 2018-10-22 stsp goto done;
1024 1a76625f 2018-10-22 stsp update_panels();
1025 1a76625f 2018-10-22 stsp doupdate();
1029 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1030 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1031 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1032 e5a0f69f 2018-08-18 stsp view_close(view);
1035 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1036 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1037 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1039 e5a0f69f 2018-08-18 stsp return err;
1042 4ed7e80c 2018-05-20 stsp __dead static void
1043 9f7d7167 2018-04-29 stsp usage_log(void)
1046 c70c5802 2018-08-01 stsp fprintf(stderr,
1047 b672a97a 2020-01-27 stsp "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1048 9f7d7167 2018-04-29 stsp getprogname());
1052 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1053 80ddbec8 2018-04-29 stsp static const struct got_error *
1054 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1056 00dfcb92 2018-06-11 stsp char *vis = NULL;
1057 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1059 963b370f 2018-05-20 stsp *ws = NULL;
1060 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1061 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1062 00dfcb92 2018-06-11 stsp int vislen;
1063 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1064 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1066 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1067 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1069 00dfcb92 2018-06-11 stsp return err;
1070 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1071 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1072 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1073 a7f50699 2018-06-11 stsp goto done;
1077 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1078 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1079 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1080 a7f50699 2018-06-11 stsp goto done;
1083 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1084 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1086 00dfcb92 2018-06-11 stsp free(vis);
1087 963b370f 2018-05-20 stsp if (err) {
1088 963b370f 2018-05-20 stsp free(*ws);
1089 963b370f 2018-05-20 stsp *ws = NULL;
1090 963b370f 2018-05-20 stsp *wlen = 0;
1092 963b370f 2018-05-20 stsp return err;
1095 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1096 963b370f 2018-05-20 stsp static const struct got_error *
1097 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1098 27a741e5 2019-09-11 stsp int col_tab_align)
1100 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1101 963b370f 2018-05-20 stsp int cols = 0;
1102 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1103 963b370f 2018-05-20 stsp size_t wlen;
1106 963b370f 2018-05-20 stsp *wlinep = NULL;
1107 b700b5d6 2018-07-10 stsp *widthp = 0;
1109 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1111 963b370f 2018-05-20 stsp return err;
1114 27a741e5 2019-09-11 stsp while (i < wlen) {
1115 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1117 27a741e5 2019-09-11 stsp if (width == 0) {
1122 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1123 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1125 27a741e5 2019-09-11 stsp cols += width;
1127 27a741e5 2019-09-11 stsp } else if (width == -1) {
1128 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1129 27a741e5 2019-09-11 stsp width = TABSIZE -
1130 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1131 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1133 27a741e5 2019-09-11 stsp cols += width;
1137 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1138 963b370f 2018-05-20 stsp goto done;
1141 963b370f 2018-05-20 stsp wline[i] = L'\0';
1142 b700b5d6 2018-07-10 stsp if (widthp)
1143 b700b5d6 2018-07-10 stsp *widthp = cols;
1146 963b370f 2018-05-20 stsp free(wline);
1148 963b370f 2018-05-20 stsp *wlinep = wline;
1149 963b370f 2018-05-20 stsp return err;
1152 8b473291 2019-02-21 stsp static const struct got_error*
1153 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1154 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1156 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1157 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1159 8b473291 2019-02-21 stsp const char *name;
1161 8b473291 2019-02-21 stsp *refs_str = NULL;
1163 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1164 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1167 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1168 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1170 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1171 8b473291 2019-02-21 stsp name += 5;
1172 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1174 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1175 8b473291 2019-02-21 stsp name += 6;
1176 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
1177 8b473291 2019-02-21 stsp name += 8;
1178 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1179 52b5abe1 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1180 5d844a1e 2019-08-13 stsp if (err) {
1181 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1183 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1184 5d844a1e 2019-08-13 stsp err = NULL;
1185 5d844a1e 2019-08-13 stsp tag = NULL;
1188 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1189 52b5abe1 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1191 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1192 52b5abe1 2019-08-13 stsp if (cmp != 0)
1194 8b473291 2019-02-21 stsp s = *refs_str;
1195 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1196 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1197 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1199 8b473291 2019-02-21 stsp *refs_str = NULL;
1205 8b473291 2019-02-21 stsp return err;
1208 963b370f 2018-05-20 stsp static const struct got_error *
1209 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1210 27a741e5 2019-09-11 stsp int col_tab_align)
1212 5813d178 2019-03-09 stsp char *smallerthan, *at;
1214 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1215 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1216 5813d178 2019-03-09 stsp author = smallerthan + 1;
1217 5813d178 2019-03-09 stsp at = strchr(author, '@');
1219 5813d178 2019-03-09 stsp *at = '\0';
1220 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1223 5813d178 2019-03-09 stsp static const struct got_error *
1224 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1225 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
1226 11b20872 2019-11-08 stsp const size_t date_display_cols, int author_display_cols,
1227 11b20872 2019-11-08 stsp struct tog_colors *colors)
1229 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1230 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1231 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1232 5813d178 2019-03-09 stsp char *author = NULL;
1233 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1234 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1235 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1236 bb737323 2018-05-20 stsp int col, limit;
1237 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1238 ccb26ccd 2018-11-05 stsp struct tm tm;
1239 45d799e2 2018-12-23 stsp time_t committer_time;
1240 11b20872 2019-11-08 stsp struct tog_color *tc;
1242 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1243 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1244 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1245 6db9f7f6 2019-12-10 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1246 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1247 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1249 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1250 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1252 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1253 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_DATE);
1255 11b20872 2019-11-08 stsp wattr_on(view->window,
1256 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1257 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1259 11b20872 2019-11-08 stsp wattr_off(view->window,
1260 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1261 27a741e5 2019-09-11 stsp col = limit;
1262 b39d25c7 2018-07-10 stsp if (col > avail)
1263 b39d25c7 2018-07-10 stsp goto done;
1265 6570a66d 2019-11-08 stsp if (avail >= 120) {
1266 6570a66d 2019-11-08 stsp char *id_str;
1267 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1269 6570a66d 2019-11-08 stsp goto done;
1270 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1272 11b20872 2019-11-08 stsp wattr_on(view->window,
1273 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1274 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1276 11b20872 2019-11-08 stsp wattr_off(view->window,
1277 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1278 6570a66d 2019-11-08 stsp free(id_str);
1280 6570a66d 2019-11-08 stsp if (col > avail)
1281 6570a66d 2019-11-08 stsp goto done;
1284 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1285 5813d178 2019-03-09 stsp if (author == NULL) {
1286 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1287 80ddbec8 2018-04-29 stsp goto done;
1289 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1291 bb737323 2018-05-20 stsp goto done;
1292 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_AUTHOR);
1294 11b20872 2019-11-08 stsp wattr_on(view->window,
1295 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1296 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1298 11b20872 2019-11-08 stsp wattr_off(view->window,
1299 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1300 bb737323 2018-05-20 stsp col += author_width;
1301 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1302 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1304 bb737323 2018-05-20 stsp author_width++;
1306 9c2eaf34 2018-05-20 stsp if (col > avail)
1307 9c2eaf34 2018-05-20 stsp goto done;
1309 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1311 6d9fbc00 2018-04-29 stsp goto done;
1312 bb737323 2018-05-20 stsp logmsg = logmsg0;
1313 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1315 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1316 bb737323 2018-05-20 stsp if (newline)
1317 bb737323 2018-05-20 stsp *newline = '\0';
1318 bb737323 2018-05-20 stsp limit = avail - col;
1319 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1321 bb737323 2018-05-20 stsp goto done;
1322 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1323 bb737323 2018-05-20 stsp col += logmsg_width;
1324 27a741e5 2019-09-11 stsp while (col < avail) {
1325 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1329 80ddbec8 2018-04-29 stsp free(logmsg0);
1330 bb737323 2018-05-20 stsp free(wlogmsg);
1331 5813d178 2019-03-09 stsp free(author);
1332 bb737323 2018-05-20 stsp free(wauthor);
1333 80ddbec8 2018-04-29 stsp free(line);
1334 80ddbec8 2018-04-29 stsp return err;
1337 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1338 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1339 899d86c2 2018-05-10 stsp struct got_object_id *id)
1341 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1343 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1344 80ddbec8 2018-04-29 stsp if (entry == NULL)
1345 899d86c2 2018-05-10 stsp return NULL;
1347 899d86c2 2018-05-10 stsp entry->id = id;
1348 99db9666 2018-05-07 stsp entry->commit = commit;
1349 899d86c2 2018-05-10 stsp return entry;
1352 99db9666 2018-05-07 stsp static void
1353 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1355 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1357 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1358 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1359 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1360 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1361 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1362 99db9666 2018-05-07 stsp free(entry);
1365 99db9666 2018-05-07 stsp static void
1366 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1368 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1369 99db9666 2018-05-07 stsp pop_commit(commits);
1372 c4972b91 2018-05-07 stsp static const struct got_error *
1373 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1374 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1376 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1377 13add988 2019-10-15 stsp regmatch_t regmatch;
1378 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1380 13add988 2019-10-15 stsp *have_match = 0;
1382 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1384 13add988 2019-10-15 stsp return err;
1386 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1388 13add988 2019-10-15 stsp goto done;
1390 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1391 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1392 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1393 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1394 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1395 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1396 13add988 2019-10-15 stsp *have_match = 1;
1398 13add988 2019-10-15 stsp free(id_str);
1399 13add988 2019-10-15 stsp free(logmsg);
1400 13add988 2019-10-15 stsp return err;
1403 13add988 2019-10-15 stsp static const struct got_error *
1404 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1405 13add988 2019-10-15 stsp int minqueue, struct got_repository *repo, const char *path,
1406 13add988 2019-10-15 stsp int *searching, int *search_next_done, regex_t *regex)
1408 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1409 7c1452c1 2020-03-26 stsp int nqueued = 0;
1412 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1413 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1414 1a76625f 2018-10-22 stsp * while updating the display.
1416 13add988 2019-10-15 stsp while (nqueued < minqueue ||
1417 13add988 2019-10-15 stsp (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1418 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1419 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1420 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1421 1a76625f 2018-10-22 stsp int errcode;
1423 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1424 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1427 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1430 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1431 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1432 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1436 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1437 1a76625f 2018-10-22 stsp if (errcode) {
1438 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1439 13add988 2019-10-15 stsp "pthread_mutex_lock");
1443 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1444 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1445 ecb28ae0 2018-07-16 stsp nqueued++;
1446 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1448 13add988 2019-10-15 stsp if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1449 7c1452c1 2020-03-26 stsp int have_match;
1450 13add988 2019-10-15 stsp err = match_commit(&have_match, id, commit, regex);
1453 7c1452c1 2020-03-26 stsp if (have_match)
1454 8f4ed634 2020-03-26 stsp *search_next_done = TOG_SEARCH_HAVE_MORE;
1457 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1458 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1459 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1460 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1465 9ba79e04 2018-06-11 stsp return err;
1468 0553a4e3 2018-04-30 stsp static const struct got_error *
1469 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1470 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1471 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1472 11b20872 2019-11-08 stsp struct got_reflist_head *refs, const char *path, int commits_needed,
1473 11b20872 2019-11-08 stsp struct tog_colors *colors)
1475 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1476 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1477 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1478 60493ae3 2019-06-20 stsp int width;
1479 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1480 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1481 8b473291 2019-02-21 stsp char *refs_str = NULL;
1482 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1483 11b20872 2019-11-08 stsp struct tog_color *tc;
1484 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1486 e0d42f60 2018-07-22 stsp entry = first;
1487 e0d42f60 2018-07-22 stsp ncommits = 0;
1488 e0d42f60 2018-07-22 stsp while (entry) {
1489 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1490 e0d42f60 2018-07-22 stsp *selected = entry;
1493 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1494 e0d42f60 2018-07-22 stsp ncommits++;
1497 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1498 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1500 ecb28ae0 2018-07-16 stsp return err;
1501 8b473291 2019-02-21 stsp if (refs) {
1502 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, (*selected)->id,
1505 8b473291 2019-02-21 stsp goto done;
1509 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1510 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1512 8f4ed634 2020-03-26 stsp if (commits_needed > 0) {
1513 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1514 8f4ed634 2020-03-26 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1515 8f4ed634 2020-03-26 stsp view->searching ? "searching..." : "loading...") == -1) {
1516 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1517 8f4ed634 2020-03-26 stsp goto done;
1520 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1522 f9686aa5 2020-03-27 stsp if (view->searching) {
1523 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1524 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1525 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1526 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1527 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1528 f9686aa5 2020-03-27 stsp search_str = "searching...";
1531 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1532 8f4ed634 2020-03-26 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1533 f9686aa5 2020-03-27 stsp search_str ? search_str :
1534 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1535 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1536 8f4ed634 2020-03-26 stsp goto done;
1540 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1541 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1542 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1543 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1544 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1545 1a76625f 2018-10-22 stsp header = NULL;
1546 1a76625f 2018-10-22 stsp goto done;
1548 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1549 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1550 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1551 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1552 1a76625f 2018-10-22 stsp header = NULL;
1553 1a76625f 2018-10-22 stsp goto done;
1555 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1557 1a76625f 2018-10-22 stsp goto done;
1559 2814baeb 2018-08-01 stsp werase(view->window);
1561 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1562 a3404814 2018-09-02 stsp wstandout(view->window);
1563 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1565 11b20872 2019-11-08 stsp wattr_on(view->window,
1566 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1567 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1569 11b20872 2019-11-08 stsp wattr_off(view->window,
1570 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1571 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1572 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1575 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1576 a3404814 2018-09-02 stsp wstandend(view->window);
1577 ecb28ae0 2018-07-16 stsp free(wline);
1578 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1579 1a76625f 2018-10-22 stsp goto done;
1581 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1582 899d86c2 2018-05-10 stsp entry = first;
1583 5813d178 2019-03-09 stsp ncommits = 0;
1584 5813d178 2019-03-09 stsp while (entry) {
1585 5813d178 2019-03-09 stsp char *author;
1586 5813d178 2019-03-09 stsp wchar_t *wauthor;
1587 5813d178 2019-03-09 stsp int width;
1588 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1590 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1591 5813d178 2019-03-09 stsp if (author == NULL) {
1592 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1593 5813d178 2019-03-09 stsp goto done;
1595 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1596 27a741e5 2019-09-11 stsp date_display_cols);
1597 5813d178 2019-03-09 stsp if (author_cols < width)
1598 5813d178 2019-03-09 stsp author_cols = width;
1599 5813d178 2019-03-09 stsp free(wauthor);
1600 5813d178 2019-03-09 stsp free(author);
1601 7ca04879 2019-10-19 stsp ncommits++;
1602 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1605 5813d178 2019-03-09 stsp entry = first;
1606 899d86c2 2018-05-10 stsp *last = first;
1607 867c6645 2018-07-10 stsp ncommits = 0;
1608 899d86c2 2018-05-10 stsp while (entry) {
1609 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1611 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1612 2814baeb 2018-08-01 stsp wstandout(view->window);
1613 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1614 11b20872 2019-11-08 stsp date_display_cols, author_cols, colors);
1615 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1616 2814baeb 2018-08-01 stsp wstandend(view->window);
1618 60493ae3 2019-06-20 stsp goto done;
1619 0553a4e3 2018-04-30 stsp ncommits++;
1620 899d86c2 2018-05-10 stsp *last = entry;
1621 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1624 1a57306a 2018-09-02 stsp view_vborder(view);
1626 1a76625f 2018-10-22 stsp free(id_str);
1627 8b473291 2019-02-21 stsp free(refs_str);
1628 1a76625f 2018-10-22 stsp free(ncommits_str);
1629 1a76625f 2018-10-22 stsp free(header);
1630 80ddbec8 2018-04-29 stsp return err;
1633 07b55e75 2018-05-10 stsp static void
1634 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1635 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1636 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1638 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1639 07b55e75 2018-05-10 stsp int nscrolled = 0;
1641 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1642 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1645 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1646 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1647 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1648 07b55e75 2018-05-10 stsp if (entry) {
1649 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1650 07b55e75 2018-05-10 stsp nscrolled++;
1655 aa075928 2018-05-10 stsp static const struct got_error *
1656 7c1452c1 2020-03-26 stsp trigger_log_thread(struct tog_view *log_view, int wait,
1657 7c1452c1 2020-03-26 stsp int *commits_needed, int *log_complete,
1658 7c1452c1 2020-03-26 stsp pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1660 5e224a3e 2019-02-22 stsp int errcode;
1662 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1664 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1665 5e224a3e 2019-02-22 stsp if (*log_complete)
1668 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1669 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1670 7aafa0d1 2019-02-22 stsp if (errcode)
1671 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1672 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1675 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1676 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1678 7c1452c1 2020-03-26 stsp if (!wait)
1681 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1682 7c1452c1 2020-03-26 stsp show_log_view(log_view);
1683 7c1452c1 2020-03-26 stsp update_panels();
1684 7c1452c1 2020-03-26 stsp doupdate();
1686 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1687 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1688 82954512 2020-02-03 stsp if (errcode)
1689 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1690 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1692 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1693 7c1452c1 2020-03-26 stsp show_log_view(log_view);
1694 7c1452c1 2020-03-26 stsp update_panels();
1695 7c1452c1 2020-03-26 stsp doupdate();
1698 5e224a3e 2019-02-22 stsp return NULL;
1701 5e224a3e 2019-02-22 stsp static const struct got_error *
1702 7c1452c1 2020-03-26 stsp scroll_down(struct tog_view *log_view,
1703 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1704 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1705 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1706 7c1452c1 2020-03-26 stsp pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1708 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1709 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1710 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1712 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1713 5e224a3e 2019-02-22 stsp return NULL;
1715 7c1452c1 2020-03-26 stsp ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1716 7c1452c1 2020-03-26 stsp if (commits->ncommits < ncommits_needed && !*log_complete) {
1718 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1720 7c1452c1 2020-03-26 stsp (*commits_needed) += maxscroll;
1721 7c1452c1 2020-03-26 stsp err = trigger_log_thread(log_view, 1, commits_needed,
1722 7c1452c1 2020-03-26 stsp log_complete, need_commits, commit_loaded);
1724 5e224a3e 2019-02-22 stsp return err;
1728 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1729 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1732 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1734 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1735 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1737 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1738 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1740 dd0a52c1 2018-05-20 stsp return err;
1743 cd0acaa7 2018-05-20 stsp static const struct got_error *
1744 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1745 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1746 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1747 8b473291 2019-02-21 stsp struct got_repository *repo)
1749 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1750 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1751 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1753 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1754 15a94983 2018-12-23 stsp if (diff_view == NULL)
1755 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1757 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1758 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1759 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1760 e5a0f69f 2018-08-18 stsp if (err == NULL)
1761 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1762 cd0acaa7 2018-05-20 stsp return err;
1765 80ddbec8 2018-04-29 stsp static const struct got_error *
1766 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1767 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1769 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1771 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1772 941e9f74 2019-05-21 stsp if (parent == NULL)
1773 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1775 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1776 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1777 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1778 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1779 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1780 941e9f74 2019-05-21 stsp s->tree = subtree;
1781 941e9f74 2019-05-21 stsp s->selected = 0;
1782 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1783 941e9f74 2019-05-21 stsp return NULL;
1786 941e9f74 2019-05-21 stsp static const struct got_error *
1787 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1788 55cccc34 2020-02-20 stsp struct got_object_id *commit_id,
1789 55cccc34 2020-02-20 stsp const char *path, struct got_repository *repo)
1791 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1792 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1793 941e9f74 2019-05-21 stsp const char *p;
1794 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1796 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1798 941e9f74 2019-05-21 stsp while (*p) {
1799 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1800 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1801 56e0773d 2019-11-28 stsp char *te_name;
1803 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1806 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1807 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1808 941e9f74 2019-05-21 stsp if (slash == NULL)
1809 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1811 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1812 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1813 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1816 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1817 56e0773d 2019-11-28 stsp if (te == NULL) {
1818 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1819 56e0773d 2019-11-28 stsp free(te_name);
1822 56e0773d 2019-11-28 stsp free(te_name);
1823 56e0773d 2019-11-28 stsp s->selected_entry = te;
1824 56e0773d 2019-11-28 stsp s->selected = got_tree_entry_get_index(te);
1825 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1826 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1828 56e0773d 2019-11-28 stsp if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1829 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1830 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1831 67409a31 2019-05-24 stsp s->selected = 0;
1835 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1836 941e9f74 2019-05-21 stsp if (slash)
1837 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1839 941e9f74 2019-05-21 stsp subpath = strdup(path);
1840 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1841 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1845 55cccc34 2020-02-20 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
1850 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1851 941e9f74 2019-05-21 stsp free(tree_id);
1855 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1856 941e9f74 2019-05-21 stsp if (err) {
1857 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1860 941e9f74 2019-05-21 stsp if (slash == NULL)
1862 941e9f74 2019-05-21 stsp free(subpath);
1863 941e9f74 2019-05-21 stsp subpath = NULL;
1864 941e9f74 2019-05-21 stsp p = slash;
1867 941e9f74 2019-05-21 stsp free(subpath);
1868 1a76625f 2018-10-22 stsp return err;
1871 61266923 2020-01-14 stsp static const struct got_error *
1872 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1873 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
1874 55cccc34 2020-02-20 stsp struct got_reflist_head *refs, struct got_repository *repo)
1876 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
1877 55cccc34 2020-02-20 stsp struct got_tree_object *tree;
1878 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
1879 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
1881 55cccc34 2020-02-20 stsp err = got_object_open_as_tree(&tree, repo,
1882 55cccc34 2020-02-20 stsp got_object_commit_get_tree_id(entry->commit));
1884 55cccc34 2020-02-20 stsp return err;
1886 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1887 55cccc34 2020-02-20 stsp if (tree_view == NULL)
1888 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
1890 55cccc34 2020-02-20 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1891 55cccc34 2020-02-20 stsp if (err) {
1892 55cccc34 2020-02-20 stsp got_object_tree_close(tree);
1893 55cccc34 2020-02-20 stsp return err;
1895 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
1897 55cccc34 2020-02-20 stsp *new_view = tree_view;
1899 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
1900 55cccc34 2020-02-20 stsp return NULL;
1902 55cccc34 2020-02-20 stsp return tree_view_walk_path(s, entry->id, path, repo);
1905 55cccc34 2020-02-20 stsp static const struct got_error *
1906 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1908 61266923 2020-01-14 stsp sigset_t sigset;
1909 61266923 2020-01-14 stsp int errcode;
1911 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1912 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1914 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1915 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1916 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1917 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
1918 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1920 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
1921 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
1922 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1924 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1925 61266923 2020-01-14 stsp if (errcode)
1926 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
1928 61266923 2020-01-14 stsp return NULL;
1931 1a76625f 2018-10-22 stsp static void *
1932 1a76625f 2018-10-22 stsp log_thread(void *arg)
1934 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1935 1a76625f 2018-10-22 stsp int errcode = 0;
1936 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1937 1a76625f 2018-10-22 stsp int done = 0;
1939 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
1941 61266923 2020-01-14 stsp return (void *)err;
1943 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1944 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1945 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1946 13add988 2019-10-15 stsp a->regex);
1947 1a76625f 2018-10-22 stsp if (err) {
1948 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1949 1a76625f 2018-10-22 stsp return (void *)err;
1950 1a76625f 2018-10-22 stsp err = NULL;
1952 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1953 1a76625f 2018-10-22 stsp a->commits_needed--;
1955 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1956 3abe8080 2019-04-10 stsp if (errcode) {
1957 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1958 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1960 3abe8080 2019-04-10 stsp } else if (*a->quit)
1962 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1963 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1964 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1965 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1968 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
1969 7c1452c1 2020-03-26 stsp if (errcode) {
1970 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
1971 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
1972 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
1977 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1979 7c1452c1 2020-03-26 stsp if (a->commits_needed == 0) {
1980 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
1981 7c1452c1 2020-03-26 stsp &tog_mutex);
1982 7c1452c1 2020-03-26 stsp if (errcode)
1983 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
1984 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1988 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1989 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1990 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1991 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1993 3abe8080 2019-04-10 stsp a->log_complete = 1;
1994 1a76625f 2018-10-22 stsp return (void *)err;
1997 1a76625f 2018-10-22 stsp static const struct got_error *
1998 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2000 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2001 1a76625f 2018-10-22 stsp int errcode;
2003 1a76625f 2018-10-22 stsp if (s->thread) {
2004 1a76625f 2018-10-22 stsp s->quit = 1;
2005 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2006 1a76625f 2018-10-22 stsp if (errcode)
2007 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2008 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2009 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2010 1a76625f 2018-10-22 stsp if (errcode)
2011 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2012 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2013 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2014 1a76625f 2018-10-22 stsp if (errcode)
2015 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2016 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2017 1a76625f 2018-10-22 stsp if (errcode)
2018 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2019 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2020 1a76625f 2018-10-22 stsp s->thread = NULL;
2023 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2024 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2025 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
2027 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2028 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
2029 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2032 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2033 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2034 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2037 9343a5fb 2018-06-23 stsp return err;
2040 9343a5fb 2018-06-23 stsp static const struct got_error *
2041 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2043 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2044 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2046 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2047 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2048 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2049 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2050 1a76625f 2018-10-22 stsp free(s->start_id);
2051 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2052 1a76625f 2018-10-22 stsp return err;
2055 1a76625f 2018-10-22 stsp static const struct got_error *
2056 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2058 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2060 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2061 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2062 60493ae3 2019-06-20 stsp return NULL;
2065 60493ae3 2019-06-20 stsp static const struct got_error *
2066 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2068 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2069 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2070 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2072 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2073 f9686aa5 2020-03-27 stsp show_log_view(view);
2074 f9686aa5 2020-03-27 stsp update_panels();
2075 f9686aa5 2020-03-27 stsp doupdate();
2077 96e2b566 2019-07-08 stsp if (s->search_entry) {
2078 13add988 2019-10-15 stsp int errcode, ch;
2079 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2080 13add988 2019-10-15 stsp if (errcode)
2081 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2082 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2083 13add988 2019-10-15 stsp ch = wgetch(view->window);
2084 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2085 13add988 2019-10-15 stsp if (errcode)
2086 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2087 13add988 2019-10-15 stsp "pthread_mutex_lock");
2088 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2089 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2090 678cbce5 2019-07-28 stsp return NULL;
2092 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2093 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2095 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2096 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2097 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2098 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2099 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2101 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2102 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2104 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2105 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2107 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2110 60493ae3 2019-06-20 stsp while (1) {
2111 13add988 2019-10-15 stsp int have_match = 0;
2113 60493ae3 2019-06-20 stsp if (entry == NULL) {
2114 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2115 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2116 f9967bca 2020-03-27 stsp view->search_next_done =
2117 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2118 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2119 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2120 f801134a 2019-06-25 stsp return NULL;
2123 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2124 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2125 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2127 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2128 7c1452c1 2020-03-26 stsp return trigger_log_thread(view, 0,
2129 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
2130 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
2131 7c1452c1 2020-03-26 stsp &s->thread_args.need_commits,
2132 7c1452c1 2020-03-26 stsp &s->thread_args.commit_loaded);
2135 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2136 13add988 2019-10-15 stsp &view->regex);
2139 13add988 2019-10-15 stsp if (have_match) {
2140 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2141 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2145 96e2b566 2019-07-08 stsp s->search_entry = entry;
2146 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2147 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2149 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2152 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2153 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2154 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2155 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2157 60493ae3 2019-06-20 stsp return err;
2160 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2161 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2163 60493ae3 2019-06-20 stsp return err;
2168 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2170 60493ae3 2019-06-20 stsp return NULL;
2173 60493ae3 2019-06-20 stsp static const struct got_error *
2174 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2175 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
2176 f135c941 2020-02-20 stsp const char *head_ref_name, const char *in_repo_path,
2177 b672a97a 2020-01-27 stsp int log_branches)
2179 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2180 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2181 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2182 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2183 1a76625f 2018-10-22 stsp int errcode;
2185 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2186 f135c941 2020-02-20 stsp free(s->in_repo_path);
2187 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2188 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2189 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2192 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2193 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2194 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2196 8b473291 2019-02-21 stsp s->refs = refs;
2197 fb2756b9 2018-08-04 stsp s->repo = repo;
2198 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
2199 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2200 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2201 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2202 5036bf37 2018-09-24 stsp goto done;
2204 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2206 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2207 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2208 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2209 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2211 11b20872 2019-11-08 stsp goto done;
2212 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2213 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2214 11b20872 2019-11-08 stsp if (err) {
2215 11b20872 2019-11-08 stsp free_colors(&s->colors);
2216 11b20872 2019-11-08 stsp goto done;
2218 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2219 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2220 11b20872 2019-11-08 stsp if (err) {
2221 11b20872 2019-11-08 stsp free_colors(&s->colors);
2222 11b20872 2019-11-08 stsp goto done;
2226 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2227 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2228 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2229 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2230 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2232 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2234 1a76625f 2018-10-22 stsp goto done;
2235 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2236 b672a97a 2020-01-27 stsp !s->log_branches);
2238 1a76625f 2018-10-22 stsp goto done;
2239 c5b78334 2020-01-12 stsp err = got_commit_graph_iter_start(thread_graph,
2240 c5b78334 2020-01-12 stsp s->start_id, s->repo, NULL, NULL);
2242 c5b78334 2020-01-12 stsp goto done;
2244 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2245 1a76625f 2018-10-22 stsp if (errcode) {
2246 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2247 1a76625f 2018-10-22 stsp goto done;
2249 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2250 7c1452c1 2020-03-26 stsp if (errcode) {
2251 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2252 7c1452c1 2020-03-26 stsp goto done;
2255 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2256 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2257 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2258 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2259 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2260 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2261 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2262 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2263 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2264 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2265 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2266 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2267 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2270 1a76625f 2018-10-22 stsp close_log_view(view);
2271 ba4f502b 2018-08-04 stsp return err;
2274 e5a0f69f 2018-08-18 stsp static const struct got_error *
2275 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2277 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2279 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2280 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2281 2b380cc8 2018-10-24 stsp &s->thread_args);
2282 2b380cc8 2018-10-24 stsp if (errcode)
2283 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2286 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
2287 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
2288 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
2289 11b20872 2019-11-08 stsp s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2292 e5a0f69f 2018-08-18 stsp static const struct got_error *
2293 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2294 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2296 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2297 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2298 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2299 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2300 669b5ffa 2018-10-07 stsp int begin_x = 0;
2301 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2303 e5a0f69f 2018-08-18 stsp switch (ch) {
2305 1e37a5c2 2019-05-12 jcs s->quit = 1;
2308 1e37a5c2 2019-05-12 jcs case KEY_UP:
2311 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2313 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2314 1e37a5c2 2019-05-12 jcs s->selected--;
2316 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
2317 1144d21a 2019-06-21 stsp &s->commits);
2319 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2320 a4292ac5 2019-05-12 jcs case CTRL('b'):
2321 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2323 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
2324 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
2325 1e37a5c2 2019-05-12 jcs s->selected = 0;
2328 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
2329 1831ac02 2020-03-23 naddy view->nlines - 1, &s->commits);
2332 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2335 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2337 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2338 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2339 1e37a5c2 2019-05-12 jcs s->selected++;
2342 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
2343 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
2344 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
2345 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2346 7c1452c1 2020-03-26 stsp &s->thread_args.need_commits,
2347 7c1452c1 2020-03-26 stsp &s->thread_args.commit_loaded);
2349 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2350 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2351 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2352 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2353 1e37a5c2 2019-05-12 jcs if (first == NULL)
2355 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
2356 1831ac02 2020-03-23 naddy view->nlines - 1, &s->last_displayed_entry,
2357 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
2358 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2359 7c1452c1 2020-03-26 stsp &s->thread_args.need_commits,
2360 7c1452c1 2020-03-26 stsp &s->thread_args.commit_loaded);
2363 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2364 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2365 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2366 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2367 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2368 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2370 1e37a5c2 2019-05-12 jcs err = NULL;
2373 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2374 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2375 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2376 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2377 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2379 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2382 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2384 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2385 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2386 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2387 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2388 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
2391 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2392 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2394 1e37a5c2 2019-05-12 jcs return err;
2395 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2397 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2400 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2401 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2403 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2406 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2408 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2409 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2410 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2411 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2414 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2415 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2417 1e37a5c2 2019-05-12 jcs return err;
2418 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2420 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2423 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2424 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2426 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2428 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2429 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2431 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2432 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2433 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2435 1e37a5c2 2019-05-12 jcs return err;
2436 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2437 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2438 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2439 638f9024 2019-05-13 stsp return got_error_from_errno(
2440 1e37a5c2 2019-05-12 jcs "view_open");
2441 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2442 f135c941 2020-02-20 stsp s->repo, s->head_ref_name, parent_path,
2443 b672a97a 2020-01-27 stsp s->log_branches);
2445 1e37a5c2 2019-05-12 jcs return err;;
2446 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2447 1e37a5c2 2019-05-12 jcs *new_view = lv;
2449 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2450 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2452 1e37a5c2 2019-05-12 jcs return NULL;
2455 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2456 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2458 d01904d4 2019-06-25 stsp return err;
2459 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2460 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2461 d01904d4 2019-06-25 stsp if (lv == NULL)
2462 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2463 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&start_id, NULL,
2464 71a27632 2020-01-15 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2465 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2466 ef129c5e 2019-08-03 stsp if (err) {
2467 ef129c5e 2019-08-03 stsp view_close(lv);
2468 d01904d4 2019-06-25 stsp return err;
2470 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2471 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2472 e6b23735 2019-10-04 stsp free(start_id);
2473 e6b23735 2019-10-04 stsp view_close(lv);
2474 e6b23735 2019-10-04 stsp return got_error_from_errno("strdup");
2476 e6b23735 2019-10-04 stsp got_ref_list_free(s->refs);
2477 e6b23735 2019-10-04 stsp err = got_ref_list(s->refs, s->repo, NULL,
2478 e6b23735 2019-10-04 stsp got_ref_cmp_by_name, NULL);
2479 e6b23735 2019-10-04 stsp if (err) {
2480 d01904d4 2019-06-25 stsp free(start_id);
2481 ef129c5e 2019-08-03 stsp view_close(lv);
2482 50284065 2019-10-04 stsp return err;
2484 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2485 f135c941 2020-02-20 stsp s->head_ref_name, in_repo_path, s->log_branches);
2486 ef129c5e 2019-08-03 stsp if (err) {
2487 ef129c5e 2019-08-03 stsp free(start_id);
2488 b672a97a 2020-01-27 stsp view_close(lv);
2489 b672a97a 2020-01-27 stsp return err;;
2491 b672a97a 2020-01-27 stsp *dead_view = view;
2492 b672a97a 2020-01-27 stsp *new_view = lv;
2495 b672a97a 2020-01-27 stsp s->log_branches = !s->log_branches;
2496 b672a97a 2020-01-27 stsp err = stop_log_thread(s);
2498 b672a97a 2020-01-27 stsp return err;
2499 b672a97a 2020-01-27 stsp lv = view_open(view->nlines, view->ncols,
2500 b672a97a 2020-01-27 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2501 b672a97a 2020-01-27 stsp if (lv == NULL)
2502 b672a97a 2020-01-27 stsp return got_error_from_errno("view_open");
2503 b672a97a 2020-01-27 stsp err = open_log_view(lv, s->start_id, s->refs, s->repo,
2504 f135c941 2020-02-20 stsp s->head_ref_name, s->in_repo_path, s->log_branches);
2505 b672a97a 2020-01-27 stsp if (err) {
2506 ef129c5e 2019-08-03 stsp view_close(lv);
2507 d01904d4 2019-06-25 stsp return err;;
2509 d01904d4 2019-06-25 stsp *dead_view = view;
2510 d01904d4 2019-06-25 stsp *new_view = lv;
2516 80ddbec8 2018-04-29 stsp return err;
2519 4ed7e80c 2018-05-20 stsp static const struct got_error *
2520 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2522 c2db6724 2019-01-04 stsp const struct got_error *error;
2524 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2525 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2526 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2528 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2529 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2531 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2532 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2534 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2535 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2537 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2538 c2db6724 2019-01-04 stsp if (error != NULL)
2539 c2db6724 2019-01-04 stsp return error;
2541 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2542 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2544 c2db6724 2019-01-04 stsp return NULL;
2547 a915003a 2019-02-05 stsp static void
2548 a915003a 2019-02-05 stsp init_curses(void)
2550 a915003a 2019-02-05 stsp initscr();
2552 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2555 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2556 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2557 a915003a 2019-02-05 stsp curs_set(0);
2558 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2559 6d17833f 2019-11-08 stsp start_color();
2560 6d17833f 2019-11-08 stsp use_default_colors();
2562 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2563 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2564 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2567 c2db6724 2019-01-04 stsp static const struct got_error *
2568 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2569 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2571 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2573 f135c941 2020-02-20 stsp if (argc == 0) {
2574 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2575 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2576 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2577 f135c941 2020-02-20 stsp return NULL;
2580 f135c941 2020-02-20 stsp if (worktree) {
2581 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2582 f135c941 2020-02-20 stsp char *wt_path, *p;
2584 f135c941 2020-02-20 stsp err = got_worktree_resolve_path(&wt_path, worktree, argv[0]);
2586 f135c941 2020-02-20 stsp return err;
2588 f135c941 2020-02-20 stsp if (asprintf(&p, "%s%s%s", prefix,
2589 f135c941 2020-02-20 stsp (strcmp(prefix, "/") != 0) ? "/" : "", wt_path) == -1) {
2590 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2591 f135c941 2020-02-20 stsp free(wt_path);
2592 f135c941 2020-02-20 stsp return err;
2594 f135c941 2020-02-20 stsp err = got_repo_map_path(in_repo_path, repo, p, 0);
2596 f135c941 2020-02-20 stsp free(wt_path);
2598 f135c941 2020-02-20 stsp err = got_repo_map_path(in_repo_path, repo, argv[0], 1);
2600 f135c941 2020-02-20 stsp return err;
2603 f135c941 2020-02-20 stsp static const struct got_error *
2604 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2606 80ddbec8 2018-04-29 stsp const struct got_error *error;
2607 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2608 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2609 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2610 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2611 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2612 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2613 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2614 04cc582a 2018-08-01 stsp struct tog_view *view;
2616 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2618 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2619 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2620 c2db6724 2019-01-04 stsp NULL) == -1)
2621 80ddbec8 2018-04-29 stsp err(1, "pledge");
2624 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2625 80ddbec8 2018-04-29 stsp switch (ch) {
2627 b672a97a 2020-01-27 stsp log_branches = 1;
2630 80ddbec8 2018-04-29 stsp start_commit = optarg;
2633 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2634 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2635 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2639 17020d27 2019-03-07 stsp usage_log();
2640 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2644 80ddbec8 2018-04-29 stsp argc -= optind;
2645 80ddbec8 2018-04-29 stsp argv += optind;
2647 f135c941 2020-02-20 stsp if (argc > 1)
2648 f135c941 2020-02-20 stsp usage_log();
2650 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2651 6962eb72 2020-02-20 stsp if (cwd == NULL)
2652 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
2654 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2655 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2656 963f97a1 2019-03-18 stsp goto done;
2658 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2659 a1fbf39a 2019-08-11 stsp if (worktree)
2660 6962eb72 2020-02-20 stsp repo_path =
2661 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2663 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2665 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2666 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2667 963f97a1 2019-03-18 stsp goto done;
2670 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2671 80ddbec8 2018-04-29 stsp if (error != NULL)
2672 ecb28ae0 2018-07-16 stsp goto done;
2674 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2675 f135c941 2020-02-20 stsp repo, worktree);
2676 f135c941 2020-02-20 stsp if (error)
2677 f135c941 2020-02-20 stsp goto done;
2679 f135c941 2020-02-20 stsp init_curses();
2681 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2682 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2683 c02c541e 2019-03-29 stsp if (error)
2684 c02c541e 2019-03-29 stsp goto done;
2686 15a94983 2018-12-23 stsp if (start_commit == NULL)
2687 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, worktree ?
2688 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2689 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2691 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, start_commit,
2692 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2693 80ddbec8 2018-04-29 stsp if (error != NULL)
2694 8b473291 2019-02-21 stsp goto done;
2696 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2697 8b473291 2019-02-21 stsp if (error)
2698 ecb28ae0 2018-07-16 stsp goto done;
2700 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2701 04cc582a 2018-08-01 stsp if (view == NULL) {
2702 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2703 04cc582a 2018-08-01 stsp goto done;
2705 2fc00ff4 2019-08-31 stsp if (worktree) {
2706 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2707 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2708 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2709 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2710 2fc00ff4 2019-08-31 stsp goto done;
2713 2fc00ff4 2019-08-31 stsp error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2714 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2715 ba4f502b 2018-08-04 stsp if (error)
2716 ba4f502b 2018-08-04 stsp goto done;
2717 2fc00ff4 2019-08-31 stsp if (worktree) {
2718 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2719 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2720 2fc00ff4 2019-08-31 stsp worktree = NULL;
2722 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2724 f135c941 2020-02-20 stsp free(in_repo_path);
2725 ecb28ae0 2018-07-16 stsp free(repo_path);
2726 ecb28ae0 2018-07-16 stsp free(cwd);
2727 899d86c2 2018-05-10 stsp free(start_id);
2728 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2730 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2731 ec142235 2019-03-07 stsp if (worktree)
2732 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2733 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2734 80ddbec8 2018-04-29 stsp return error;
2737 4ed7e80c 2018-05-20 stsp __dead static void
2738 9f7d7167 2018-04-29 stsp usage_diff(void)
2741 09b5bff8 2020-02-23 naddy fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2742 9f7d7167 2018-04-29 stsp getprogname());
2746 b304db33 2018-05-20 stsp static char *
2747 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2749 b304db33 2018-05-20 stsp char *line;
2750 b304db33 2018-05-20 stsp size_t linelen;
2751 b304db33 2018-05-20 stsp size_t lineno;
2752 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2754 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2756 b304db33 2018-05-20 stsp *len = linelen;
2757 b304db33 2018-05-20 stsp return line;
2760 6d17833f 2019-11-08 stsp static int
2761 6d17833f 2019-11-08 stsp match_line(const char *line, regex_t *regex)
2763 6d17833f 2019-11-08 stsp regmatch_t regmatch;
2765 6d17833f 2019-11-08 stsp return regexec(regex, line, 1, ®match, 0) == 0;
2768 f26dddb7 2019-11-08 stsp struct tog_color *
2769 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2771 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2773 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2774 6d17833f 2019-11-08 stsp if (match_line(line, &tc->regex))
2775 6d17833f 2019-11-08 stsp return tc;
2778 6d17833f 2019-11-08 stsp return NULL;
2781 4ed7e80c 2018-05-20 stsp static const struct got_error *
2782 f44b1f58 2020-02-02 tracey draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2783 f44b1f58 2020-02-02 tracey int selected_line, int max_lines, int *last_displayed_line, int *eof,
2784 f44b1f58 2020-02-02 tracey char *header, struct tog_colors *colors)
2786 61e69b96 2018-05-20 stsp const struct got_error *err;
2787 f44b1f58 2020-02-02 tracey int lineno = 0, nprinted = 0;
2788 b304db33 2018-05-20 stsp char *line;
2789 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2790 b304db33 2018-05-20 stsp size_t len;
2791 61e69b96 2018-05-20 stsp wchar_t *wline;
2792 e0b650dd 2018-05-20 stsp int width;
2794 26ed57b2 2018-05-19 stsp rewind(f);
2795 f7d12f7e 2018-08-01 stsp werase(view->window);
2797 a3404814 2018-09-02 stsp if (header) {
2798 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
2799 a3404814 2018-09-02 stsp if (err) {
2800 a3404814 2018-09-02 stsp return err;
2803 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2804 a3404814 2018-09-02 stsp wstandout(view->window);
2805 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2806 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2807 a3404814 2018-09-02 stsp wstandend(view->window);
2808 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2809 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2811 a3404814 2018-09-02 stsp if (max_lines <= 1)
2812 a3404814 2018-09-02 stsp return NULL;
2813 a3404814 2018-09-02 stsp max_lines--;
2817 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2818 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2819 26ed57b2 2018-05-19 stsp if (line == NULL) {
2823 f44b1f58 2020-02-02 tracey if (++lineno < *first_displayed_line) {
2824 26ed57b2 2018-05-19 stsp free(line);
2828 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2829 61e69b96 2018-05-20 stsp if (err) {
2830 61e69b96 2018-05-20 stsp free(line);
2831 61e69b96 2018-05-20 stsp return err;
2834 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
2836 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2837 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2838 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2840 6d17833f 2019-11-08 stsp wattr_off(view->window,
2841 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2842 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2843 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2844 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2845 f44b1f58 2020-02-02 tracey *first_displayed_line = lineno;
2846 26ed57b2 2018-05-19 stsp free(line);
2847 2550e4c3 2018-07-13 stsp free(wline);
2848 2550e4c3 2018-07-13 stsp wline = NULL;
2850 f44b1f58 2020-02-02 tracey *last_displayed_line = lineno;
2852 1a57306a 2018-09-02 stsp view_vborder(view);
2854 c3e9aa98 2019-05-13 jcs if (*eof) {
2855 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2856 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2857 c3e9aa98 2019-05-13 jcs nprinted++;
2860 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2862 c3e9aa98 2019-05-13 jcs return err;
2865 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2866 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2867 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2870 26ed57b2 2018-05-19 stsp return NULL;
2873 abd2672a 2018-12-23 stsp static char *
2874 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2876 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2877 09867e48 2019-08-13 stsp char *p, *s;
2879 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2880 09867e48 2019-08-13 stsp if (tm == NULL)
2881 09867e48 2019-08-13 stsp return NULL;
2882 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2883 09867e48 2019-08-13 stsp if (s == NULL)
2884 09867e48 2019-08-13 stsp return NULL;
2885 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2887 abd2672a 2018-12-23 stsp *p = '\0';
2891 4ed7e80c 2018-05-20 stsp static const struct got_error *
2892 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2893 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2895 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2896 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
2897 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2898 5943eee2 2019-08-13 stsp char *id_str = NULL, *logmsg = NULL;
2899 45d799e2 2018-12-23 stsp time_t committer_time;
2900 45d799e2 2018-12-23 stsp const char *author, *committer;
2901 8b473291 2019-02-21 stsp char *refs_str = NULL;
2903 8b473291 2019-02-21 stsp if (refs) {
2904 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
2906 8b473291 2019-02-21 stsp return err;
2909 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2911 abd2672a 2018-12-23 stsp return err;
2913 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2914 15a94983 2018-12-23 stsp if (err) {
2915 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2916 15a94983 2018-12-23 stsp goto done;
2919 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2920 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2921 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2922 abd2672a 2018-12-23 stsp goto done;
2924 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2925 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2926 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2927 abd2672a 2018-12-23 stsp goto done;
2929 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2930 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
2931 09867e48 2019-08-13 stsp if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2932 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2933 abd2672a 2018-12-23 stsp goto done;
2935 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2936 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2937 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2938 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2939 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2940 abd2672a 2018-12-23 stsp goto done;
2942 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2944 5943eee2 2019-08-13 stsp goto done;
2945 5943eee2 2019-08-13 stsp if (fprintf(outfile, "%s\n", logmsg) < 0) {
2946 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2947 abd2672a 2018-12-23 stsp goto done;
2950 abd2672a 2018-12-23 stsp free(id_str);
2951 5943eee2 2019-08-13 stsp free(logmsg);
2952 8b473291 2019-02-21 stsp free(refs_str);
2953 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2954 abd2672a 2018-12-23 stsp return err;
2957 f44b1f58 2020-02-02 tracey const struct got_error *
2958 f44b1f58 2020-02-02 tracey get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2959 f44b1f58 2020-02-02 tracey FILE *infile)
2961 f44b1f58 2020-02-02 tracey size_t len;
2962 f44b1f58 2020-02-02 tracey char *buf = NULL;
2964 f44b1f58 2020-02-02 tracey size_t noffsets = 0;
2965 f44b1f58 2020-02-02 tracey off_t off = 0;
2967 f44b1f58 2020-02-02 tracey if (line_offsets)
2968 f44b1f58 2020-02-02 tracey *line_offsets = NULL;
2969 f44b1f58 2020-02-02 tracey if (filesize)
2970 f44b1f58 2020-02-02 tracey *filesize = 0;
2971 f44b1f58 2020-02-02 tracey if (nlines)
2972 f44b1f58 2020-02-02 tracey *nlines = 0;
2974 f44b1f58 2020-02-02 tracey if (fseek(infile, 0, SEEK_END) == -1)
2975 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseek");
2976 f44b1f58 2020-02-02 tracey len = ftell(infile) + 1;
2977 f44b1f58 2020-02-02 tracey if (ferror(infile))
2978 f44b1f58 2020-02-02 tracey return got_error_from_errno("ftell");
2979 f44b1f58 2020-02-02 tracey if (fseek(infile, 0, SEEK_SET) == -1)
2980 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseek");
2982 f44b1f58 2020-02-02 tracey if (len == 0)
2983 f44b1f58 2020-02-02 tracey return NULL;
2984 f44b1f58 2020-02-02 tracey if ((buf = calloc(len, sizeof(char *))) == NULL)
2985 f44b1f58 2020-02-02 tracey return got_error_from_errno("calloc");
2987 f44b1f58 2020-02-02 tracey fread(buf, 1, len, infile);
2988 f44b1f58 2020-02-02 tracey if (ferror(infile))
2989 f44b1f58 2020-02-02 tracey return got_error_from_errno("fread");
2992 f44b1f58 2020-02-02 tracey if (line_offsets && nlines) {
2993 f44b1f58 2020-02-02 tracey if (*line_offsets == NULL) {
2994 f44b1f58 2020-02-02 tracey /* Have some data but perhaps no '\n'. */
2995 f44b1f58 2020-02-02 tracey noffsets = 1;
2996 f44b1f58 2020-02-02 tracey *nlines = 1;
2997 f44b1f58 2020-02-02 tracey *line_offsets = calloc(1, sizeof(**line_offsets));
2998 f44b1f58 2020-02-02 tracey if (*line_offsets == NULL)
2999 f44b1f58 2020-02-02 tracey return got_error_from_errno("calloc");
3000 f44b1f58 2020-02-02 tracey /* Skip forward over end of first line. */
3001 f44b1f58 2020-02-02 tracey while (i < len) {
3002 f44b1f58 2020-02-02 tracey if (buf[i] == '\n')
3007 f44b1f58 2020-02-02 tracey /* Scan '\n' offsets in remaining chunk of data. */
3008 f44b1f58 2020-02-02 tracey while (i < len) {
3009 f44b1f58 2020-02-02 tracey if (buf[i] != '\n') {
3011 f44b1f58 2020-02-02 tracey continue;
3013 f44b1f58 2020-02-02 tracey (*nlines)++;
3014 f44b1f58 2020-02-02 tracey if (noffsets < *nlines) {
3015 f44b1f58 2020-02-02 tracey off_t *o = recallocarray(*line_offsets,
3016 f44b1f58 2020-02-02 tracey noffsets, *nlines,
3017 f44b1f58 2020-02-02 tracey sizeof(**line_offsets));
3018 f44b1f58 2020-02-02 tracey if (o == NULL) {
3019 f44b1f58 2020-02-02 tracey free(*line_offsets);
3020 f44b1f58 2020-02-02 tracey *line_offsets = NULL;
3021 f44b1f58 2020-02-02 tracey return got_error_from_errno(
3022 f44b1f58 2020-02-02 tracey "recallocarray");
3024 f44b1f58 2020-02-02 tracey *line_offsets = o;
3025 f44b1f58 2020-02-02 tracey noffsets = *nlines;
3027 f44b1f58 2020-02-02 tracey off = i + 1;
3028 f44b1f58 2020-02-02 tracey (*line_offsets)[*nlines - 1] = off;
3033 f44b1f58 2020-02-02 tracey if (fflush(infile) != 0)
3034 f44b1f58 2020-02-02 tracey return got_error_from_errno("fflush");
3035 f44b1f58 2020-02-02 tracey rewind(infile);
3037 f44b1f58 2020-02-02 tracey if (filesize)
3038 f44b1f58 2020-02-02 tracey *filesize = len;
3040 f44b1f58 2020-02-02 tracey return NULL;
3043 abd2672a 2018-12-23 stsp static const struct got_error *
3044 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3046 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3047 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3048 15a94983 2018-12-23 stsp int obj_type;
3050 511a516b 2018-05-19 stsp f = got_opentemp();
3051 48ae06ee 2018-10-18 stsp if (f == NULL) {
3052 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3053 48ae06ee 2018-10-18 stsp goto done;
3055 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
3056 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3057 fb43ecf1 2019-02-11 stsp goto done;
3061 15a94983 2018-12-23 stsp if (s->id1)
3062 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3064 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3066 15a94983 2018-12-23 stsp goto done;
3068 15a94983 2018-12-23 stsp switch (obj_type) {
3069 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3070 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
3071 f44b1f58 2020-02-02 tracey s->diff_context, 0, s->repo, s->f);
3073 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3074 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
3075 f44b1f58 2020-02-02 tracey s->diff_context, 0, s->repo, s->f);
3077 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3078 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3079 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3080 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3082 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3084 3ffacbe1 2020-02-02 tracey goto done;
3085 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3086 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3087 f44b1f58 2020-02-02 tracey err =write_commit_info(s->id2, s->refs, s->repo, s->f);
3088 f44b1f58 2020-02-02 tracey if (err)
3089 f44b1f58 2020-02-02 tracey goto done;
3090 f44b1f58 2020-02-02 tracey } else {
3091 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3092 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3093 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3094 f44b1f58 2020-02-02 tracey err = write_commit_info(s->id2, s->refs,
3095 f44b1f58 2020-02-02 tracey s->repo, s->f);
3096 f44b1f58 2020-02-02 tracey if (err)
3097 f44b1f58 2020-02-02 tracey goto done;
3102 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3104 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
3105 f44b1f58 2020-02-02 tracey s->diff_context, 0, s->repo, s->f);
3109 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3112 f44b1f58 2020-02-02 tracey if (err)
3113 f44b1f58 2020-02-02 tracey goto done;
3114 f44b1f58 2020-02-02 tracey err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3117 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3118 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3119 48ae06ee 2018-10-18 stsp return err;
3122 f5215bb9 2019-02-22 stsp static void
3123 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3125 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3126 f5215bb9 2019-02-22 stsp update_panels();
3127 f5215bb9 2019-02-22 stsp doupdate();
3130 f44b1f58 2020-02-02 tracey static const struct got_error *
3131 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3133 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3135 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3136 f44b1f58 2020-02-02 tracey return NULL;
3139 f44b1f58 2020-02-02 tracey static const struct got_error *
3140 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3142 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3143 f44b1f58 2020-02-02 tracey int lineno;
3145 f44b1f58 2020-02-02 tracey if (!view->searching) {
3146 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3147 f44b1f58 2020-02-02 tracey return NULL;
3150 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3151 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3152 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3154 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3155 f44b1f58 2020-02-02 tracey } else {
3156 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3157 f44b1f58 2020-02-02 tracey lineno = 1;
3159 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3162 f44b1f58 2020-02-02 tracey while (1) {
3163 f44b1f58 2020-02-02 tracey char *line = NULL;
3164 f44b1f58 2020-02-02 tracey off_t offset;
3165 f44b1f58 2020-02-02 tracey size_t len;
3167 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3168 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3169 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3170 f44b1f58 2020-02-02 tracey free(line);
3174 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3175 f44b1f58 2020-02-02 tracey lineno = 1;
3177 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3180 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3181 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3182 f44b1f58 2020-02-02 tracey free(line);
3183 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3185 f44b1f58 2020-02-02 tracey free(line);
3186 f44b1f58 2020-02-02 tracey line = parse_next_line(s->f, &len);
3187 f44b1f58 2020-02-02 tracey if (line && match_line(line, &view->regex)) {
3188 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3189 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3190 f44b1f58 2020-02-02 tracey free(line);
3193 f44b1f58 2020-02-02 tracey free(line);
3194 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3195 f44b1f58 2020-02-02 tracey lineno++;
3197 f44b1f58 2020-02-02 tracey lineno--;
3200 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3201 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3202 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3205 f44b1f58 2020-02-02 tracey return NULL;
3208 48ae06ee 2018-10-18 stsp static const struct got_error *
3209 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3210 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
3211 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
3213 48ae06ee 2018-10-18 stsp const struct got_error *err;
3214 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3216 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3217 15a94983 2018-12-23 stsp int type1, type2;
3218 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3220 15a94983 2018-12-23 stsp return err;
3221 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3223 15a94983 2018-12-23 stsp return err;
3225 15a94983 2018-12-23 stsp if (type1 != type2)
3226 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3228 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3229 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3230 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3231 f44b1f58 2020-02-02 tracey s->repo = repo;
3232 f44b1f58 2020-02-02 tracey s->refs = refs;
3233 f44b1f58 2020-02-02 tracey s->id1 = id1;
3234 f44b1f58 2020-02-02 tracey s->id2 = id2;
3236 15a94983 2018-12-23 stsp if (id1) {
3237 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3238 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3239 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3241 5465d566 2020-02-01 tracey s->id1 = NULL;
3243 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3244 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3245 5465d566 2020-02-01 tracey free(s->id1);
3246 5465d566 2020-02-01 tracey s->id1 = NULL;
3247 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3249 5465d566 2020-02-01 tracey s->f = NULL;
3250 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3251 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3252 5465d566 2020-02-01 tracey s->diff_context = 3;
3253 5465d566 2020-02-01 tracey s->log_view = log_view;
3254 5465d566 2020-02-01 tracey s->repo = repo;
3255 5465d566 2020-02-01 tracey s->refs = refs;
3257 f44b1f58 2020-02-02 tracey SIMPLEQ_INIT(&s->colors);
3258 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3259 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3260 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3261 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3263 6d17833f 2019-11-08 stsp return err;
3264 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3265 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3266 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3267 6d17833f 2019-11-08 stsp if (err) {
3268 5465d566 2020-02-01 tracey free_colors(&s->colors);
3269 6d17833f 2019-11-08 stsp return err;
3271 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3272 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3273 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3274 6d17833f 2019-11-08 stsp if (err) {
3275 5465d566 2020-02-01 tracey free_colors(&s->colors);
3276 6d17833f 2019-11-08 stsp return err;
3279 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3280 11b20872 2019-11-08 stsp "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3281 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3282 6d17833f 2019-11-08 stsp if (err) {
3283 5465d566 2020-02-01 tracey free_colors(&s->colors);
3284 6d17833f 2019-11-08 stsp return err;
3287 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3288 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3289 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3290 11b20872 2019-11-08 stsp if (err) {
3291 5465d566 2020-02-01 tracey free_colors(&s->colors);
3292 11b20872 2019-11-08 stsp return err;
3295 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3296 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3297 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3298 11b20872 2019-11-08 stsp if (err) {
3299 5465d566 2020-02-01 tracey free_colors(&s->colors);
3300 11b20872 2019-11-08 stsp return err;
3304 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3305 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3306 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3308 5465d566 2020-02-01 tracey err = create_diff(s);
3309 48ae06ee 2018-10-18 stsp if (err) {
3310 5465d566 2020-02-01 tracey free(s->id1);
3311 5465d566 2020-02-01 tracey s->id1 = NULL;
3312 5465d566 2020-02-01 tracey free(s->id2);
3313 5465d566 2020-02-01 tracey s->id2 = NULL;
3314 48ae06ee 2018-10-18 stsp return err;
3317 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3318 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3319 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3320 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3321 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3323 5dc9f4bc 2018-08-04 stsp return NULL;
3326 e5a0f69f 2018-08-18 stsp static const struct got_error *
3327 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3329 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3330 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3332 5465d566 2020-02-01 tracey free(s->id1);
3333 5465d566 2020-02-01 tracey s->id1 = NULL;
3334 5465d566 2020-02-01 tracey free(s->id2);
3335 5465d566 2020-02-01 tracey s->id2 = NULL;
3336 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3337 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3338 5465d566 2020-02-01 tracey free_colors(&s->colors);
3339 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3340 e5a0f69f 2018-08-18 stsp return err;
3343 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3344 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3346 a3404814 2018-09-02 stsp const struct got_error *err;
3347 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3348 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3350 a3404814 2018-09-02 stsp if (s->id1) {
3351 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3353 a3404814 2018-09-02 stsp return err;
3355 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3357 a3404814 2018-09-02 stsp return err;
3359 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
3360 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3361 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3362 a3404814 2018-09-02 stsp free(id_str1);
3363 a3404814 2018-09-02 stsp free(id_str2);
3364 a3404814 2018-09-02 stsp return err;
3366 a3404814 2018-09-02 stsp free(id_str1);
3367 a3404814 2018-09-02 stsp free(id_str2);
3369 f44b1f58 2020-02-02 tracey return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3370 f44b1f58 2020-02-02 tracey s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3371 bddb1296 2019-11-08 stsp header, &s->colors);
3374 15a087fe 2019-02-21 stsp static const struct got_error *
3375 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3376 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3378 d7a04538 2019-02-21 stsp const struct got_error *err;
3379 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3380 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3381 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3383 15a087fe 2019-02-21 stsp free(s->id2);
3384 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3385 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3386 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3388 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3390 d7a04538 2019-02-21 stsp return err;
3391 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3392 15a087fe 2019-02-21 stsp free(s->id1);
3393 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3394 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3395 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3396 15a087fe 2019-02-21 stsp return NULL;
3399 0cf4efb1 2018-09-29 stsp static const struct got_error *
3400 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3401 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3403 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3404 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3405 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3406 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
3409 e5a0f69f 2018-08-18 stsp switch (ch) {
3411 1e37a5c2 2019-05-12 jcs case KEY_UP:
3412 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3413 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3415 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3416 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3417 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3420 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3421 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3422 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3425 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3426 1e37a5c2 2019-05-12 jcs if (!s->eof)
3427 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3429 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3430 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3432 00ba99a7 2019-05-12 jcs if (s->eof)
3435 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3436 1e37a5c2 2019-05-12 jcs char *line;
3437 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
3438 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3439 1e37a5c2 2019-05-12 jcs if (line == NULL)
3444 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3445 1e37a5c2 2019-05-12 jcs s->diff_context--;
3446 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3447 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3451 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3452 1e37a5c2 2019-05-12 jcs s->diff_context++;
3453 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3454 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3459 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3461 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3462 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
3463 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
3464 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3467 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3472 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3476 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3477 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3479 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3480 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3484 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3486 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3488 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3489 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
3490 7c1452c1 2020-03-26 stsp err = trigger_log_thread(s->log_view, 1,
3491 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
3492 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
3493 7c1452c1 2020-03-26 stsp &ls->thread_args.need_commits,
3494 7c1452c1 2020-03-26 stsp &ls->thread_args.commit_loaded);
3498 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3503 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
3504 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3507 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3511 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3512 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3514 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3515 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3521 bcbd79e2 2018-08-19 stsp return err;
3524 4ed7e80c 2018-05-20 stsp static const struct got_error *
3525 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3527 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3528 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3529 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3530 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3531 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3532 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3533 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3535 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3537 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3539 26ed57b2 2018-05-19 stsp #ifndef PROFILE
3540 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3541 eb6600df 2019-01-04 stsp NULL) == -1)
3542 26ed57b2 2018-05-19 stsp err(1, "pledge");
3545 09b5bff8 2020-02-23 naddy while ((ch = getopt(argc, argv, "r:")) != -1) {
3546 26ed57b2 2018-05-19 stsp switch (ch) {
3547 09b5bff8 2020-02-23 naddy case 'r':
3548 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3549 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3550 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3554 17020d27 2019-03-07 stsp usage_diff();
3555 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3559 26ed57b2 2018-05-19 stsp argc -= optind;
3560 26ed57b2 2018-05-19 stsp argv += optind;
3562 26ed57b2 2018-05-19 stsp if (argc == 0) {
3563 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3564 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3565 15a94983 2018-12-23 stsp id_str1 = argv[0];
3566 15a94983 2018-12-23 stsp id_str2 = argv[1];
3568 26ed57b2 2018-05-19 stsp usage_diff();
3570 a273ac94 2020-02-23 naddy cwd = getcwd(NULL, 0);
3571 a273ac94 2020-02-23 naddy if (cwd == NULL)
3572 a273ac94 2020-02-23 naddy return got_error_from_errno("getcwd");
3574 a273ac94 2020-02-23 naddy error = got_worktree_open(&worktree, cwd);
3575 a273ac94 2020-02-23 naddy if (error && error->code != GOT_ERR_NOT_WORKTREE)
3576 a273ac94 2020-02-23 naddy goto done;
3578 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3579 a273ac94 2020-02-23 naddy if (worktree)
3580 a273ac94 2020-02-23 naddy repo_path =
3581 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3583 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3585 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3586 a273ac94 2020-02-23 naddy error = got_error_from_errno("strdup");
3587 a273ac94 2020-02-23 naddy goto done;
3590 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3591 eb6600df 2019-01-04 stsp if (error)
3592 eb6600df 2019-01-04 stsp goto done;
3594 a273ac94 2020-02-23 naddy init_curses();
3596 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3597 26ed57b2 2018-05-19 stsp if (error)
3598 26ed57b2 2018-05-19 stsp goto done;
3600 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
3601 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3602 26ed57b2 2018-05-19 stsp if (error)
3603 26ed57b2 2018-05-19 stsp goto done;
3605 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
3606 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3607 26ed57b2 2018-05-19 stsp if (error)
3608 26ed57b2 2018-05-19 stsp goto done;
3610 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3611 8b473291 2019-02-21 stsp if (error)
3612 8b473291 2019-02-21 stsp goto done;
3614 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3615 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3616 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3617 ea5e7bb5 2018-08-01 stsp goto done;
3619 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3620 5dc9f4bc 2018-08-04 stsp if (error)
3621 5dc9f4bc 2018-08-04 stsp goto done;
3622 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3624 c02c541e 2019-03-29 stsp free(repo_path);
3625 a273ac94 2020-02-23 naddy free(cwd);
3627 921be706 2019-06-28 stsp got_repo_close(repo);
3628 a273ac94 2020-02-23 naddy if (worktree)
3629 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3630 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3631 26ed57b2 2018-05-19 stsp return error;
3634 4ed7e80c 2018-05-20 stsp __dead static void
3635 9f7d7167 2018-04-29 stsp usage_blame(void)
3638 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3639 9f7d7167 2018-04-29 stsp getprogname());
3643 84451b3e 2018-07-10 stsp struct tog_blame_line {
3644 84451b3e 2018-07-10 stsp int annotated;
3645 84451b3e 2018-07-10 stsp struct got_object_id *id;
3648 4ed7e80c 2018-05-20 stsp static const struct got_error *
3649 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3650 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
3651 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
3652 11b20872 2019-11-08 stsp int *last_displayed_line, int *eof, int max_lines,
3653 11b20872 2019-11-08 stsp struct tog_colors *colors)
3655 84451b3e 2018-07-10 stsp const struct got_error *err;
3656 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3657 84451b3e 2018-07-10 stsp char *line;
3658 84451b3e 2018-07-10 stsp size_t len;
3659 84451b3e 2018-07-10 stsp wchar_t *wline;
3660 27a741e5 2019-09-11 stsp int width;
3661 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3662 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3663 ab089a2a 2018-07-12 stsp char *id_str;
3664 11b20872 2019-11-08 stsp struct tog_color *tc;
3666 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
3668 ab089a2a 2018-07-12 stsp return err;
3670 84451b3e 2018-07-10 stsp rewind(f);
3671 f7d12f7e 2018-08-01 stsp werase(view->window);
3673 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3674 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3675 ab089a2a 2018-07-12 stsp free(id_str);
3676 ab089a2a 2018-07-12 stsp return err;
3679 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3680 ab089a2a 2018-07-12 stsp free(line);
3681 2550e4c3 2018-07-13 stsp line = NULL;
3683 1cae65b4 2019-09-22 stsp return err;
3684 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3685 a3404814 2018-09-02 stsp wstandout(view->window);
3686 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3688 11b20872 2019-11-08 stsp wattr_on(view->window,
3689 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3690 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3692 11b20872 2019-11-08 stsp wattr_off(view->window,
3693 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3694 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3695 a3404814 2018-09-02 stsp wstandend(view->window);
3696 2550e4c3 2018-07-13 stsp free(wline);
3697 2550e4c3 2018-07-13 stsp wline = NULL;
3698 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3699 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3701 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3702 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
3703 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
3704 ab089a2a 2018-07-12 stsp free(id_str);
3705 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3707 ab089a2a 2018-07-12 stsp free(id_str);
3708 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3709 3f60a8ef 2018-07-10 stsp free(line);
3710 2550e4c3 2018-07-13 stsp line = NULL;
3712 3f60a8ef 2018-07-10 stsp return err;
3713 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3714 2550e4c3 2018-07-13 stsp free(wline);
3715 2550e4c3 2018-07-13 stsp wline = NULL;
3716 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3717 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3720 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
3721 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
3722 84451b3e 2018-07-10 stsp if (line == NULL) {
3726 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
3727 84451b3e 2018-07-10 stsp free(line);
3731 27a741e5 2019-09-11 stsp if (view->ncols <= 9) {
3732 27a741e5 2019-09-11 stsp width = 9;
3733 27a741e5 2019-09-11 stsp wline = wcsdup(L"");
3734 27a741e5 2019-09-11 stsp if (wline == NULL)
3735 27a741e5 2019-09-11 stsp err = got_error_from_errno("wcsdup");
3737 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line,
3738 27a741e5 2019-09-11 stsp view->ncols - 9, 9);
3739 27a741e5 2019-09-11 stsp width += 9;
3741 84451b3e 2018-07-10 stsp if (err) {
3742 84451b3e 2018-07-10 stsp free(line);
3743 84451b3e 2018-07-10 stsp return err;
3746 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3747 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3749 8d0fe45a 2019-08-12 stsp if (nlines > 0) {
3750 8d0fe45a 2019-08-12 stsp blame_line = &lines[lineno - 1];
3751 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3752 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3753 27a741e5 2019-09-11 stsp !(view->focussed &&
3754 27a741e5 2019-09-11 stsp nprinted == selected_line - 1)) {
3755 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3756 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3757 8d0fe45a 2019-08-12 stsp char *id_str;
3758 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3759 8d0fe45a 2019-08-12 stsp if (err) {
3760 8d0fe45a 2019-08-12 stsp free(line);
3761 8d0fe45a 2019-08-12 stsp free(wline);
3762 8d0fe45a 2019-08-12 stsp return err;
3764 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3766 11b20872 2019-11-08 stsp wattr_on(view->window,
3767 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3768 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3770 11b20872 2019-11-08 stsp wattr_off(view->window,
3771 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3772 8d0fe45a 2019-08-12 stsp free(id_str);
3773 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3775 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3776 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3779 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3780 ee41ec32 2018-07-10 stsp prev_id = NULL;
3783 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3784 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3785 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3787 27a741e5 2019-09-11 stsp waddwstr(view->window, wline);
3788 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
3789 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
3790 84451b3e 2018-07-10 stsp if (++nprinted == 1)
3791 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
3792 84451b3e 2018-07-10 stsp free(line);
3793 2550e4c3 2018-07-13 stsp free(wline);
3794 2550e4c3 2018-07-13 stsp wline = NULL;
3796 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
3798 1a57306a 2018-09-02 stsp view_vborder(view);
3800 84451b3e 2018-07-10 stsp return NULL;
3803 84451b3e 2018-07-10 stsp static const struct got_error *
3804 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3806 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
3807 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
3808 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
3809 1a76625f 2018-10-22 stsp int errcode;
3811 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
3812 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3813 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3815 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3816 1a76625f 2018-10-22 stsp if (errcode)
3817 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3819 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3820 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3821 d68a0a7d 2018-07-10 stsp goto done;
3824 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3825 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3827 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3828 d68a0a7d 2018-07-10 stsp if (line->annotated)
3829 d68a0a7d 2018-07-10 stsp goto done;
3831 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3832 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3833 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3834 84451b3e 2018-07-10 stsp goto done;
3836 84451b3e 2018-07-10 stsp line->annotated = 1;
3838 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3839 1a76625f 2018-10-22 stsp if (errcode)
3840 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3841 84451b3e 2018-07-10 stsp return err;
3844 84451b3e 2018-07-10 stsp static void *
3845 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3847 18430de3 2018-07-10 stsp const struct got_error *err;
3848 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3849 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3850 1a76625f 2018-10-22 stsp int errcode;
3852 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
3854 61266923 2020-01-14 stsp return (void *)err;
3856 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
3857 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3858 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
3859 fc06ba56 2019-08-22 stsp err = NULL;
3861 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3862 1a76625f 2018-10-22 stsp if (errcode)
3863 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3864 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3866 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3867 c9beca56 2018-07-22 stsp ta->repo = NULL;
3868 c9beca56 2018-07-22 stsp *ta->complete = 1;
3870 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3871 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3872 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3874 18430de3 2018-07-10 stsp return (void *)err;
3877 245d91c1 2018-07-12 stsp static struct got_object_id *
3878 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3879 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
3881 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3883 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
3884 8d0fe45a 2019-08-12 stsp return NULL;
3886 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3887 245d91c1 2018-07-12 stsp if (!line->annotated)
3888 245d91c1 2018-07-12 stsp return NULL;
3890 245d91c1 2018-07-12 stsp return line->id;
3893 b880a918 2018-07-10 stsp static const struct got_error *
3894 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3896 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3899 245d91c1 2018-07-12 stsp if (blame->thread) {
3900 1a76625f 2018-10-22 stsp int errcode;
3901 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3902 1a76625f 2018-10-22 stsp if (errcode)
3903 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3904 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3905 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3906 1a76625f 2018-10-22 stsp if (errcode)
3907 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3908 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3909 1a76625f 2018-10-22 stsp if (errcode)
3910 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3911 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3912 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3913 245d91c1 2018-07-12 stsp err = NULL;
3914 245d91c1 2018-07-12 stsp blame->thread = NULL;
3916 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3917 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3918 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3920 245d91c1 2018-07-12 stsp if (blame->f) {
3921 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3922 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3923 245d91c1 2018-07-12 stsp blame->f = NULL;
3925 57670559 2018-12-23 stsp if (blame->lines) {
3926 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3927 57670559 2018-12-23 stsp free(blame->lines[i].id);
3928 57670559 2018-12-23 stsp free(blame->lines);
3929 57670559 2018-12-23 stsp blame->lines = NULL;
3931 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3932 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3934 245d91c1 2018-07-12 stsp return err;
3937 245d91c1 2018-07-12 stsp static const struct got_error *
3938 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
3940 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
3941 fc06ba56 2019-08-22 stsp int *done = arg;
3942 fc06ba56 2019-08-22 stsp int errcode;
3944 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3945 fc06ba56 2019-08-22 stsp if (errcode)
3946 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3947 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
3949 fc06ba56 2019-08-22 stsp if (*done)
3950 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
3952 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3953 fc06ba56 2019-08-22 stsp if (errcode)
3954 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3955 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
3957 fc06ba56 2019-08-22 stsp return err;
3960 fc06ba56 2019-08-22 stsp static const struct got_error *
3961 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3962 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3963 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3964 245d91c1 2018-07-12 stsp struct got_repository *repo)
3966 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3967 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3968 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3969 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3970 15a94983 2018-12-23 stsp int obj_type;
3972 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3974 15a94983 2018-12-23 stsp return err;
3976 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3978 84451b3e 2018-07-10 stsp goto done;
3980 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3981 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3982 84451b3e 2018-07-10 stsp goto done;
3985 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3987 a70480e0 2018-06-23 stsp goto done;
3988 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3989 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3990 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3991 84451b3e 2018-07-10 stsp goto done;
3993 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3994 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
3995 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
3996 84451b3e 2018-07-10 stsp goto done;
3998 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3999 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4000 b02560ec 2019-08-19 stsp blame->nlines--;
4002 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4003 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4004 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4005 84451b3e 2018-07-10 stsp goto done;
4008 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4010 bd24772e 2018-07-11 stsp goto done;
4012 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4013 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4014 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4015 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
4016 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4017 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4018 245d91c1 2018-07-12 stsp goto done;
4020 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
4022 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
4023 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4024 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4025 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
4026 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4027 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_arg = done;
4028 245d91c1 2018-07-12 stsp *blame_complete = 0;
4032 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4033 27d434c2 2018-09-15 stsp free(obj_id);
4035 245d91c1 2018-07-12 stsp stop_blame(blame);
4036 245d91c1 2018-07-12 stsp return err;
4039 245d91c1 2018-07-12 stsp static const struct got_error *
4040 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4041 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4042 8b473291 2019-02-21 stsp struct got_repository *repo)
4044 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4045 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4047 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
4049 c4843652 2019-08-12 stsp s->path = strdup(path);
4050 c4843652 2019-08-12 stsp if (s->path == NULL)
4051 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4053 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4054 c4843652 2019-08-12 stsp if (err) {
4055 c4843652 2019-08-12 stsp free(s->path);
4056 7cbe629d 2018-08-04 stsp return err;
4059 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4060 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4061 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4062 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4063 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4064 fb2756b9 2018-08-04 stsp s->repo = repo;
4065 8b473291 2019-02-21 stsp s->refs = refs;
4066 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4067 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4069 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4070 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4071 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4072 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4074 11b20872 2019-11-08 stsp return err;
4077 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4078 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4079 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4080 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4081 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4083 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
4084 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
4085 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
4086 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
4089 e5a0f69f 2018-08-18 stsp static const struct got_error *
4090 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4092 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4093 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4095 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4096 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4098 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4099 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4100 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4101 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4102 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4105 e5a0f69f 2018-08-18 stsp free(s->path);
4106 11b20872 2019-11-08 stsp free_colors(&s->colors);
4108 e5a0f69f 2018-08-18 stsp return err;
4111 7cbe629d 2018-08-04 stsp static const struct got_error *
4112 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4114 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4116 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4117 6c4c42e0 2019-06-24 stsp return NULL;
4120 6c4c42e0 2019-06-24 stsp static const struct got_error *
4121 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4123 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4124 6c4c42e0 2019-06-24 stsp int lineno;
4126 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4127 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4128 6c4c42e0 2019-06-24 stsp return NULL;
4131 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4132 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4133 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4135 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4137 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4138 6c4c42e0 2019-06-24 stsp lineno = 1;
4140 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4143 6c4c42e0 2019-06-24 stsp while (1) {
4144 6c4c42e0 2019-06-24 stsp char *line = NULL;
4145 6c4c42e0 2019-06-24 stsp off_t offset;
4146 6c4c42e0 2019-06-24 stsp size_t len;
4148 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4149 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4150 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4151 6c4c42e0 2019-06-24 stsp free(line);
4155 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4156 6c4c42e0 2019-06-24 stsp lineno = 1;
4158 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4161 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4162 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4163 6c4c42e0 2019-06-24 stsp free(line);
4164 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4166 6c4c42e0 2019-06-24 stsp free(line);
4167 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
4168 2246482e 2019-06-25 stsp if (line && match_line(line, &view->regex)) {
4169 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4170 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4171 6c4c42e0 2019-06-24 stsp free(line);
4174 6c4c42e0 2019-06-24 stsp free(line);
4175 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4181 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4182 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4183 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4186 6c4c42e0 2019-06-24 stsp return NULL;
4189 6c4c42e0 2019-06-24 stsp static const struct got_error *
4190 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4192 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4193 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4194 2b380cc8 2018-10-24 stsp int errcode;
4196 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
4197 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4198 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4199 2b380cc8 2018-10-24 stsp if (errcode)
4200 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4202 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4205 51fe7530 2019-08-19 stsp if (s->blame_complete)
4206 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4208 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4209 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4210 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
4211 11b20872 2019-11-08 stsp &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4213 669b5ffa 2018-10-07 stsp view_vborder(view);
4214 e5a0f69f 2018-08-18 stsp return err;
4217 e5a0f69f 2018-08-18 stsp static const struct got_error *
4218 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4219 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4221 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4222 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4223 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4224 669b5ffa 2018-10-07 stsp int begin_x = 0;
4226 e5a0f69f 2018-08-18 stsp switch (ch) {
4228 1e37a5c2 2019-05-12 jcs s->done = 1;
4231 1e37a5c2 2019-05-12 jcs case KEY_UP:
4232 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4233 1e37a5c2 2019-05-12 jcs s->selected_line--;
4234 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4235 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4236 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4238 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4239 ea025d1d 2020-02-22 naddy case CTRL('b'):
4240 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4241 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4244 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4245 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4246 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4248 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4251 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4252 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4253 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4254 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4255 1e37a5c2 2019-05-12 jcs s->selected_line++;
4256 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4257 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4258 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4261 1e37a5c2 2019-05-12 jcs case 'p': {
4262 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4263 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4264 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4265 1e37a5c2 2019-05-12 jcs if (id == NULL)
4267 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4268 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4269 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4270 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4271 1e37a5c2 2019-05-12 jcs int obj_type;
4272 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4273 1e37a5c2 2019-05-12 jcs s->repo, id);
4276 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
4277 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4278 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4279 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4282 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4283 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4284 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4285 e5a0f69f 2018-08-18 stsp if (err) {
4286 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4287 1e37a5c2 2019-05-12 jcs err = NULL;
4288 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4291 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4293 1e37a5c2 2019-05-12 jcs free(blob_id);
4294 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4295 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4296 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4299 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4301 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4303 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4304 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4306 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4311 1e37a5c2 2019-05-12 jcs s->done = 1;
4312 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4313 1e37a5c2 2019-05-12 jcs s->done = 0;
4314 1e37a5c2 2019-05-12 jcs if (thread_err)
4316 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4317 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4318 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4319 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4320 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
4321 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
4326 1e37a5c2 2019-05-12 jcs case 'B': {
4327 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4328 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
4329 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4331 1e37a5c2 2019-05-12 jcs s->done = 1;
4332 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4333 1e37a5c2 2019-05-12 jcs s->done = 0;
4334 1e37a5c2 2019-05-12 jcs if (thread_err)
4336 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4337 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4338 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4339 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4340 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4341 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4342 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
4343 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
4348 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4349 1e37a5c2 2019-05-12 jcs case '\r': {
4350 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4351 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4352 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4353 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4354 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4355 1e37a5c2 2019-05-12 jcs if (id == NULL)
4357 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4360 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4361 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4362 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4363 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4364 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4365 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4366 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4367 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4370 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4371 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
4372 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4374 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4377 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4378 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4381 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
4383 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4386 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
4387 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4389 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4394 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4395 ea025d1d 2020-02-22 naddy case CTRL('f'):
4397 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4398 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4399 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4402 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4403 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4404 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4405 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4408 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4409 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4410 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4411 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4413 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4414 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4415 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4417 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4418 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4419 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4420 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4426 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4429 a70480e0 2018-06-23 stsp static const struct got_error *
4430 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4432 a70480e0 2018-06-23 stsp const struct got_error *error;
4433 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4434 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4435 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4436 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4437 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4438 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4440 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4442 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4444 a70480e0 2018-06-23 stsp #ifndef PROFILE
4445 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4446 8e94dd5b 2019-01-04 stsp NULL) == -1)
4447 a70480e0 2018-06-23 stsp err(1, "pledge");
4450 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4451 a70480e0 2018-06-23 stsp switch (ch) {
4453 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4456 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4457 69069811 2018-08-02 stsp if (repo_path == NULL)
4458 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4462 17020d27 2019-03-07 stsp usage_blame();
4463 a70480e0 2018-06-23 stsp /* NOTREACHED */
4467 a70480e0 2018-06-23 stsp argc -= optind;
4468 a70480e0 2018-06-23 stsp argv += optind;
4470 f135c941 2020-02-20 stsp if (argc != 1)
4471 a70480e0 2018-06-23 stsp usage_blame();
4473 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
4474 6962eb72 2020-02-20 stsp if (cwd == NULL)
4475 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
4477 f135c941 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
4478 f135c941 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4479 f135c941 2020-02-20 stsp goto done;
4481 69069811 2018-08-02 stsp if (repo_path == NULL) {
4482 f135c941 2020-02-20 stsp if (worktree)
4483 eb41ed75 2019-02-05 stsp repo_path =
4484 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4486 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4488 f135c941 2020-02-20 stsp if (repo_path == NULL) {
4489 f135c941 2020-02-20 stsp error = got_error_from_errno("strdup");
4490 f135c941 2020-02-20 stsp goto done;
4493 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4494 c02c541e 2019-03-29 stsp if (error != NULL)
4495 8e94dd5b 2019-01-04 stsp goto done;
4497 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4498 f135c941 2020-02-20 stsp worktree);
4499 c02c541e 2019-03-29 stsp if (error)
4500 92205607 2019-01-04 stsp goto done;
4502 f135c941 2020-02-20 stsp init_curses();
4504 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4505 eb41ed75 2019-02-05 stsp if (error)
4506 69069811 2018-08-02 stsp goto done;
4508 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4509 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4510 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4511 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4512 a70480e0 2018-06-23 stsp if (error != NULL)
4513 66b4983c 2018-06-23 stsp goto done;
4514 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4515 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4517 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4518 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4520 a19e88aa 2018-06-23 stsp if (error != NULL)
4521 8b473291 2019-02-21 stsp goto done;
4523 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4524 8b473291 2019-02-21 stsp if (error)
4525 a19e88aa 2018-06-23 stsp goto done;
4527 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4528 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4529 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4530 e1cd8fed 2018-08-01 stsp goto done;
4532 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4533 7cbe629d 2018-08-04 stsp if (error)
4534 7cbe629d 2018-08-04 stsp goto done;
4535 12314ad4 2019-08-31 stsp if (worktree) {
4536 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4537 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4538 12314ad4 2019-08-31 stsp worktree = NULL;
4540 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4542 69069811 2018-08-02 stsp free(repo_path);
4543 f135c941 2020-02-20 stsp free(in_repo_path);
4544 69069811 2018-08-02 stsp free(cwd);
4545 a70480e0 2018-06-23 stsp free(commit_id);
4546 eb41ed75 2019-02-05 stsp if (worktree)
4547 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4549 a70480e0 2018-06-23 stsp got_repo_close(repo);
4550 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4551 a70480e0 2018-06-23 stsp return error;
4554 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4555 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
4556 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
4557 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
4558 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
4559 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
4560 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int selected, int limit,
4561 f26dddb7 2019-11-08 stsp int isroot, struct tog_colors *colors)
4563 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4564 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4565 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4566 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4567 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4569 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
4571 f7d12f7e 2018-08-01 stsp werase(view->window);
4573 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4574 ffd1d5e5 2018-06-23 stsp return NULL;
4576 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, label, view->ncols, 0);
4578 ffd1d5e5 2018-06-23 stsp return err;
4579 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4580 a3404814 2018-09-02 stsp wstandout(view->window);
4581 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
4583 11b20872 2019-11-08 stsp wattr_on(view->window,
4584 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4585 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4587 11b20872 2019-11-08 stsp wattr_off(view->window,
4588 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4589 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4590 a3404814 2018-09-02 stsp wstandend(view->window);
4591 2550e4c3 2018-07-13 stsp free(wline);
4592 2550e4c3 2018-07-13 stsp wline = NULL;
4593 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4594 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4595 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4596 ffd1d5e5 2018-06-23 stsp return NULL;
4597 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4599 ce52c690 2018-06-23 stsp return err;
4600 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4601 2550e4c3 2018-07-13 stsp free(wline);
4602 2550e4c3 2018-07-13 stsp wline = NULL;
4603 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4604 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4605 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4606 ffd1d5e5 2018-06-23 stsp return NULL;
4607 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4608 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4609 a1eca9bb 2018-06-23 stsp return NULL;
4611 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
4612 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(tree);
4613 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
4614 0cf4efb1 2018-09-29 stsp if (view->focussed)
4615 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4616 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
4618 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4619 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
4620 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4621 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4622 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4623 ffd1d5e5 2018-06-23 stsp return NULL;
4627 56e0773d 2019-11-28 stsp te = *first_displayed_entry;
4630 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4631 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4632 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
4633 848d6979 2019-08-12 stsp const char *modestr = "";
4634 56e0773d 2019-11-28 stsp mode_t mode;
4636 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4637 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4639 1d13200f 2018-07-12 stsp if (show_ids) {
4640 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4641 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4643 638f9024 2019-05-13 stsp return got_error_from_errno(
4644 230a42bd 2019-05-11 jcs "got_object_id_str");
4646 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4647 63c5ca5d 2019-08-24 stsp modestr = "$";
4648 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
4649 848d6979 2019-08-12 stsp modestr = "@";
4650 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4651 848d6979 2019-08-12 stsp modestr = "/";
4652 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4653 848d6979 2019-08-12 stsp modestr = "*";
4654 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4655 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te), modestr) == -1) {
4656 1d13200f 2018-07-12 stsp free(id_str);
4657 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4659 1d13200f 2018-07-12 stsp free(id_str);
4660 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4661 ffd1d5e5 2018-06-23 stsp if (err) {
4662 ffd1d5e5 2018-06-23 stsp free(line);
4665 ffd1d5e5 2018-06-23 stsp if (n == selected) {
4666 0cf4efb1 2018-09-29 stsp if (view->focussed)
4667 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4668 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
4670 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
4672 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4673 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4674 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4676 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4677 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4678 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4679 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4680 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
4681 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4682 ffd1d5e5 2018-06-23 stsp free(line);
4683 2550e4c3 2018-07-13 stsp free(wline);
4684 2550e4c3 2018-07-13 stsp wline = NULL;
4686 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4687 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
4688 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4692 ffd1d5e5 2018-06-23 stsp return err;
4695 ffd1d5e5 2018-06-23 stsp static void
4696 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
4697 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
4698 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int isroot)
4700 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4703 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
4706 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, 0);
4707 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
4708 ffd1d5e5 2018-06-23 stsp if (!isroot)
4709 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4714 56e0773d 2019-11-28 stsp while (*first_displayed_entry && i < maxscroll) {
4715 56e0773d 2019-11-28 stsp *first_displayed_entry = got_tree_entry_get_prev(tree,
4716 56e0773d 2019-11-28 stsp *first_displayed_entry);
4719 56e0773d 2019-11-28 stsp if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4720 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4723 768394f3 2019-01-24 stsp static int
4724 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4725 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
4726 56e0773d 2019-11-28 stsp struct got_tree_object *tree)
4728 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4729 ffd1d5e5 2018-06-23 stsp int n = 0;
4731 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
4732 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, *first_displayed_entry);
4734 56e0773d 2019-11-28 stsp next = got_object_tree_get_first_entry(tree);
4736 768394f3 2019-01-24 stsp last = last_displayed_entry;
4737 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4738 56e0773d 2019-11-28 stsp last = got_tree_entry_get_next(tree, last);
4739 768394f3 2019-01-24 stsp if (last) {
4740 768394f3 2019-01-24 stsp *first_displayed_entry = next;
4741 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, next);
4747 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4748 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4749 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4751 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4752 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4753 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4755 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4756 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
4757 56e0773d 2019-11-28 stsp + 1 /* slash */;
4759 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
4761 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4762 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4763 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4765 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4766 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4767 d9765a41 2018-06-23 stsp while (pt) {
4768 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
4769 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
4770 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4771 cb2ebc8a 2018-06-23 stsp goto done;
4773 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
4774 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4775 cb2ebc8a 2018-06-23 stsp goto done;
4777 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4780 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4781 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4782 ce52c690 2018-06-23 stsp goto done;
4786 ce52c690 2018-06-23 stsp if (err) {
4787 ce52c690 2018-06-23 stsp free(*path);
4788 ce52c690 2018-06-23 stsp *path = NULL;
4790 ce52c690 2018-06-23 stsp return err;
4793 ce52c690 2018-06-23 stsp static const struct got_error *
4794 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
4795 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4796 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4797 8b473291 2019-02-21 stsp struct got_repository *repo)
4799 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
4800 ce52c690 2018-06-23 stsp char *path;
4801 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
4803 a0de39f3 2019-08-09 stsp *new_view = NULL;
4805 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
4807 ce52c690 2018-06-23 stsp return err;
4809 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4810 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
4811 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
4812 83ce39e3 2019-08-12 stsp goto done;
4815 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
4816 e5a0f69f 2018-08-18 stsp if (err) {
4817 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
4818 fc06ba56 2019-08-22 stsp err = NULL;
4819 e5a0f69f 2018-08-18 stsp view_close(blame_view);
4821 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
4823 83ce39e3 2019-08-12 stsp free(path);
4824 69efd4c4 2018-07-18 stsp return err;
4827 69efd4c4 2018-07-18 stsp static const struct got_error *
4828 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
4829 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4830 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4831 8b473291 2019-02-21 stsp struct got_repository *repo)
4833 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
4834 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
4835 69efd4c4 2018-07-18 stsp char *path;
4837 a0de39f3 2019-08-09 stsp *new_view = NULL;
4839 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4840 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
4841 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
4843 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
4845 69efd4c4 2018-07-18 stsp return err;
4847 f135c941 2020-02-20 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4849 e5a0f69f 2018-08-18 stsp view_close(log_view);
4851 e5a0f69f 2018-08-18 stsp *new_view = log_view;
4852 cb2ebc8a 2018-06-23 stsp free(path);
4853 cb2ebc8a 2018-06-23 stsp return err;
4856 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4857 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
4858 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4859 8b473291 2019-02-21 stsp struct got_repository *repo)
4861 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4862 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
4863 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4865 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
4867 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
4868 ffd1d5e5 2018-06-23 stsp if (err != NULL)
4869 ffd1d5e5 2018-06-23 stsp goto done;
4871 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4872 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4873 ffd1d5e5 2018-06-23 stsp goto done;
4876 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
4877 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4878 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4879 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
4880 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
4881 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4882 6484ec90 2018-09-29 stsp goto done;
4884 8b473291 2019-02-21 stsp s->refs = refs;
4885 fb2756b9 2018-08-04 stsp s->repo = repo;
4887 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4889 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4890 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
4891 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
4892 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4894 c0b01bdb 2019-11-08 stsp goto done;
4895 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4896 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
4897 c0b01bdb 2019-11-08 stsp if (err) {
4898 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4899 c0b01bdb 2019-11-08 stsp goto done;
4901 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
4902 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
4903 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4904 c0b01bdb 2019-11-08 stsp if (err) {
4905 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4906 c0b01bdb 2019-11-08 stsp goto done;
4909 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
4910 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
4911 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4912 11b20872 2019-11-08 stsp if (err) {
4913 11b20872 2019-11-08 stsp free_colors(&s->colors);
4914 11b20872 2019-11-08 stsp goto done;
4917 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4918 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4919 c0b01bdb 2019-11-08 stsp if (err) {
4920 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4921 c0b01bdb 2019-11-08 stsp goto done;
4925 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
4926 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
4927 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
4928 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
4929 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
4931 ad80ab7b 2018-08-04 stsp free(commit_id_str);
4932 6484ec90 2018-09-29 stsp if (err) {
4933 fb2756b9 2018-08-04 stsp free(s->tree_label);
4934 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4936 ad80ab7b 2018-08-04 stsp return err;
4939 e5a0f69f 2018-08-18 stsp static const struct got_error *
4940 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
4942 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4944 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4945 fb2756b9 2018-08-04 stsp free(s->tree_label);
4946 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4947 6484ec90 2018-09-29 stsp free(s->commit_id);
4948 6484ec90 2018-09-29 stsp s->commit_id = NULL;
4949 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
4950 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
4951 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
4952 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
4953 ad80ab7b 2018-08-04 stsp free(parent);
4956 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
4957 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
4958 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
4960 7c32bd05 2019-06-22 stsp return NULL;
4963 7c32bd05 2019-06-22 stsp static const struct got_error *
4964 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
4966 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4968 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
4969 7c32bd05 2019-06-22 stsp return NULL;
4972 7c32bd05 2019-06-22 stsp static int
4973 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4975 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
4977 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4981 7c32bd05 2019-06-22 stsp static const struct got_error *
4982 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
4984 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4985 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
4987 7c32bd05 2019-06-22 stsp if (!view->searching) {
4988 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4989 7c32bd05 2019-06-22 stsp return NULL;
4992 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4993 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
4994 7c32bd05 2019-06-22 stsp if (s->selected_entry)
4995 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
4996 56e0773d 2019-11-28 stsp s->selected_entry);
4998 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5000 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5001 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5003 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5004 56e0773d 2019-11-28 stsp s->selected_entry);
5007 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5008 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5010 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5013 7c32bd05 2019-06-22 stsp while (1) {
5014 56e0773d 2019-11-28 stsp if (te == NULL) {
5015 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5016 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5017 ac66afb8 2019-06-24 stsp return NULL;
5019 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5020 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5022 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5025 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5026 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5027 56e0773d 2019-11-28 stsp s->matched_entry = te;
5031 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5032 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5034 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5037 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5038 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5039 7c32bd05 2019-06-22 stsp s->selected = 0;
5042 e5a0f69f 2018-08-18 stsp return NULL;
5045 ad80ab7b 2018-08-04 stsp static const struct got_error *
5046 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5048 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5049 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5050 e5a0f69f 2018-08-18 stsp char *parent_path;
5052 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5054 e5a0f69f 2018-08-18 stsp return err;
5056 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
5057 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
5058 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5059 56e0773d 2019-11-28 stsp s->tree, s->selected, view->nlines, s->tree == s->root,
5060 bddb1296 2019-11-08 stsp &s->colors);
5061 e5a0f69f 2018-08-18 stsp free(parent_path);
5063 669b5ffa 2018-10-07 stsp view_vborder(view);
5064 e5a0f69f 2018-08-18 stsp return err;
5067 e5a0f69f 2018-08-18 stsp static const struct got_error *
5068 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5069 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
5071 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5072 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5073 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
5074 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
5076 e5a0f69f 2018-08-18 stsp switch (ch) {
5078 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5081 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5083 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5084 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5085 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
5086 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
5087 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
5088 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5089 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5091 1e37a5c2 2019-05-12 jcs return err;
5092 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
5094 1e37a5c2 2019-05-12 jcs view_close(log_view);
5097 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
5098 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
5100 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5103 1e37a5c2 2019-05-12 jcs case KEY_UP:
5104 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5105 1e37a5c2 2019-05-12 jcs s->selected--;
5106 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
5109 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
5111 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
5112 56e0773d 2019-11-28 stsp s->tree, s->tree == s->root);
5114 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5115 ea025d1d 2020-02-22 naddy case CTRL('b'):
5116 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
5117 56e0773d 2019-11-28 stsp MAX(0, view->nlines - 4 - s->selected), s->tree,
5118 1e37a5c2 2019-05-12 jcs s->tree == s->root);
5119 1e37a5c2 2019-05-12 jcs s->selected = 0;
5120 56e0773d 2019-11-28 stsp if (got_object_tree_get_first_entry(s->tree) ==
5121 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
5122 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
5125 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5126 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5127 1e37a5c2 2019-05-12 jcs s->selected++;
5130 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5132 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5134 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
5135 56e0773d 2019-11-28 stsp s->last_displayed_entry, s->tree);
5137 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5138 ea025d1d 2020-02-22 naddy case CTRL('f'):
5139 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5141 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5142 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5143 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5146 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
5147 56e0773d 2019-11-28 stsp view->nlines, s->last_displayed_entry, s->tree);
5148 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
5149 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
5150 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
5151 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
5153 1e37a5c2 2019-05-12 jcs ndisplayed++;
5154 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5155 1e37a5c2 2019-05-12 jcs } while (te);
5156 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
5159 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5161 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5162 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5163 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5164 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5165 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5167 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5168 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5170 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5171 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5172 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5173 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5174 1e37a5c2 2019-05-12 jcs s->selected_entry =
5175 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5176 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5177 1e37a5c2 2019-05-12 jcs free(parent);
5178 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5179 56e0773d 2019-11-28 stsp s->selected_entry))) {
5180 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5181 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5182 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5185 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
5186 941e9f74 2019-05-21 stsp if (err) {
5187 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5190 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5191 56e0773d 2019-11-28 stsp s->selected_entry))) {
5192 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5193 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5194 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5196 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5197 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5198 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
5201 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5202 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5204 669b5ffa 2018-10-07 stsp return err;
5205 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
5206 669b5ffa 2018-10-07 stsp if (err) {
5207 1e37a5c2 2019-05-12 jcs view_close(blame_view);
5210 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
5211 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
5213 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5216 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5217 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
5218 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5224 ffd1d5e5 2018-06-23 stsp return err;
5227 ffd1d5e5 2018-06-23 stsp __dead static void
5228 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5231 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5232 ffd1d5e5 2018-06-23 stsp getprogname());
5236 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5237 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5239 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5240 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5241 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5242 8b473291 2019-02-21 stsp struct got_reflist_head refs;
5243 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5244 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5245 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
5246 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
5247 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
5249 5221c383 2018-08-01 stsp struct tog_view *view;
5251 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
5253 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
5254 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5255 d188b9a6 2019-01-04 stsp NULL) == -1)
5256 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
5259 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5260 ffd1d5e5 2018-06-23 stsp switch (ch) {
5262 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5265 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5266 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5267 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5271 17020d27 2019-03-07 stsp usage_tree();
5272 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5276 ffd1d5e5 2018-06-23 stsp argc -= optind;
5277 ffd1d5e5 2018-06-23 stsp argv += optind;
5279 55cccc34 2020-02-20 stsp if (argc > 1)
5280 74283ab8 2020-02-07 stsp usage_tree();
5282 55cccc34 2020-02-20 stsp cwd = getcwd(NULL, 0);
5283 55cccc34 2020-02-20 stsp if (cwd == NULL)
5284 55cccc34 2020-02-20 stsp return got_error_from_errno("getcwd");
5286 55cccc34 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
5287 55cccc34 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5288 55cccc34 2020-02-20 stsp goto done;
5290 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5291 55cccc34 2020-02-20 stsp if (worktree)
5292 52185f70 2019-02-05 stsp repo_path =
5293 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5295 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5297 55cccc34 2020-02-20 stsp if (repo_path == NULL) {
5298 55cccc34 2020-02-20 stsp error = got_error_from_errno("strdup");
5299 55cccc34 2020-02-20 stsp goto done;
5302 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5303 c02c541e 2019-03-29 stsp if (error != NULL)
5304 52185f70 2019-02-05 stsp goto done;
5306 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5307 55cccc34 2020-02-20 stsp repo, worktree);
5308 55cccc34 2020-02-20 stsp if (error)
5309 55cccc34 2020-02-20 stsp goto done;
5311 55cccc34 2020-02-20 stsp init_curses();
5313 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5314 c02c541e 2019-03-29 stsp if (error)
5315 52185f70 2019-02-05 stsp goto done;
5317 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5318 71a27632 2020-01-15 stsp commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5319 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5320 55cccc34 2020-02-20 stsp if (error)
5321 ffd1d5e5 2018-06-23 stsp goto done;
5323 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5324 55cccc34 2020-02-20 stsp if (error)
5325 ffd1d5e5 2018-06-23 stsp goto done;
5327 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
5328 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5329 55cccc34 2020-02-20 stsp if (error)
5330 8b473291 2019-02-21 stsp goto done;
5332 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5333 8b473291 2019-02-21 stsp if (error)
5334 ffd1d5e5 2018-06-23 stsp goto done;
5336 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5337 5221c383 2018-08-01 stsp if (view == NULL) {
5338 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5339 5221c383 2018-08-01 stsp goto done;
5341 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
5342 ad80ab7b 2018-08-04 stsp if (error)
5343 ad80ab7b 2018-08-04 stsp goto done;
5344 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5345 55cccc34 2020-02-20 stsp error = tree_view_walk_path(&view->state.tree, commit_id,
5346 55cccc34 2020-02-20 stsp in_repo_path, repo);
5347 55cccc34 2020-02-20 stsp if (error)
5348 55cccc34 2020-02-20 stsp goto done;
5351 55cccc34 2020-02-20 stsp if (worktree) {
5352 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5353 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5354 55cccc34 2020-02-20 stsp worktree = NULL;
5356 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5358 52185f70 2019-02-05 stsp free(repo_path);
5359 e4a0e26d 2020-02-20 stsp free(cwd);
5360 ffd1d5e5 2018-06-23 stsp free(commit_id);
5361 ffd1d5e5 2018-06-23 stsp if (commit)
5362 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5364 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5366 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5367 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5368 ffd1d5e5 2018-06-23 stsp return error;
5371 ce5b7c56 2019-07-09 stsp static void
5372 ce5b7c56 2019-07-09 stsp list_commands(void)
5376 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
5377 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
5378 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
5379 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->name);
5381 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
5384 4ed7e80c 2018-05-20 stsp __dead static void
5385 ce5b7c56 2019-07-09 stsp usage(int hflag)
5387 ee85c5e8 2020-02-29 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] [command] "
5388 ee85c5e8 2020-02-29 stsp "[arg ...]\n", getprogname());
5389 ee85c5e8 2020-02-29 stsp if (hflag) {
5390 ee85c5e8 2020-02-29 stsp fprintf(stderr, "lazy usage: %s path\n", getprogname());
5391 ce5b7c56 2019-07-09 stsp list_commands();
5396 c2301be8 2018-04-30 stsp static char **
5397 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
5399 ee85c5e8 2020-02-29 stsp va_list ap;
5400 c2301be8 2018-04-30 stsp char **argv;
5403 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
5405 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
5406 c2301be8 2018-04-30 stsp if (argv == NULL)
5407 c2301be8 2018-04-30 stsp err(1, "calloc");
5408 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
5409 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
5410 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
5411 e10c916e 2019-09-15 hiltjo err(1, "strdup");
5414 ee85c5e8 2020-02-29 stsp va_end(ap);
5415 c2301be8 2018-04-30 stsp return argv;
5419 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
5420 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
5421 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
5422 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
5423 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
5425 ee85c5e8 2020-02-29 stsp static const struct got_error *
5426 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
5428 ee85c5e8 2020-02-29 stsp const struct got_error *error = NULL;
5429 ee85c5e8 2020-02-29 stsp struct tog_cmd *cmd = NULL;
5430 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
5431 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
5432 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
5433 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5434 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
5436 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
5437 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
5438 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
5440 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
5441 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5442 ee85c5e8 2020-02-29 stsp goto done;
5444 ee85c5e8 2020-02-29 stsp if (worktree)
5445 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
5447 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
5448 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
5449 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
5450 ee85c5e8 2020-02-29 stsp goto done;
5453 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
5454 ee85c5e8 2020-02-29 stsp if (error != NULL)
5455 ee85c5e8 2020-02-29 stsp goto done;
5457 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5458 ee85c5e8 2020-02-29 stsp repo, worktree);
5459 ee85c5e8 2020-02-29 stsp if (error)
5460 ee85c5e8 2020-02-29 stsp goto done;
5462 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5463 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5464 ee85c5e8 2020-02-29 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5465 ee85c5e8 2020-02-29 stsp if (error)
5466 ee85c5e8 2020-02-29 stsp goto done;
5468 ee85c5e8 2020-02-29 stsp if (worktree) {
5469 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
5470 ee85c5e8 2020-02-29 stsp worktree = NULL;
5473 ee85c5e8 2020-02-29 stsp error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5474 ee85c5e8 2020-02-29 stsp if (error) {
5475 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
5476 ee85c5e8 2020-02-29 stsp goto done;
5477 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
5478 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
5480 ee85c5e8 2020-02-29 stsp /* not reached */
5483 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
5484 ee85c5e8 2020-02-29 stsp repo = NULL;
5486 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
5487 ee85c5e8 2020-02-29 stsp if (error)
5488 ee85c5e8 2020-02-29 stsp goto done;
5490 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
5492 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5493 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
5496 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
5497 ee85c5e8 2020-02-29 stsp if (worktree)
5498 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
5500 ee85c5e8 2020-02-29 stsp free(commit_id_str);
5501 ee85c5e8 2020-02-29 stsp free(commit_id);
5502 ee85c5e8 2020-02-29 stsp free(cwd);
5503 ee85c5e8 2020-02-29 stsp free(repo_path);
5504 ee85c5e8 2020-02-29 stsp free(in_repo_path);
5505 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
5507 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
5508 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
5509 ee85c5e8 2020-02-29 stsp free(cmd_argv);
5511 ee85c5e8 2020-02-29 stsp return error;
5515 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
5517 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
5518 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
5519 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
5520 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
5521 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
5522 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
5523 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
5526 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
5528 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5529 9f7d7167 2018-04-29 stsp switch (ch) {
5531 9f7d7167 2018-04-29 stsp hflag = 1;
5534 53ccebc2 2019-07-30 stsp Vflag = 1;
5537 ce5b7c56 2019-07-09 stsp usage(hflag);
5538 9f7d7167 2018-04-29 stsp /* NOTREACHED */
5542 9f7d7167 2018-04-29 stsp argc -= optind;
5543 9f7d7167 2018-04-29 stsp argv += optind;
5544 9f7d7167 2018-04-29 stsp optind = 0;
5545 c2301be8 2018-04-30 stsp optreset = 1;
5547 53ccebc2 2019-07-30 stsp if (Vflag) {
5548 53ccebc2 2019-07-30 stsp got_version_print_str();
5552 c2301be8 2018-04-30 stsp if (argc == 0) {
5553 f29d3e89 2018-06-23 stsp if (hflag)
5554 ce5b7c56 2019-07-09 stsp usage(hflag);
5555 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
5556 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
5558 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
5562 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
5563 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
5564 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
5565 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
5566 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
5572 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
5573 ee85c5e8 2020-02-29 stsp if (argc != 1)
5575 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
5576 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
5578 ee85c5e8 2020-02-29 stsp if (hflag)
5579 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
5581 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5585 a2f4a359 2020-02-28 stsp if (cmd_argv) {
5587 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
5588 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
5589 a2f4a359 2020-02-28 stsp free(cmd_argv);
5592 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
5593 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);