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 79cc719f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
1177 8b473291 2019-02-21 stsp name += 8;
1178 79cc719f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
1179 79cc719f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
1182 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1183 52b5abe1 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1184 5d844a1e 2019-08-13 stsp if (err) {
1185 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1187 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1188 5d844a1e 2019-08-13 stsp err = NULL;
1189 5d844a1e 2019-08-13 stsp tag = NULL;
1192 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1193 52b5abe1 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1195 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1196 52b5abe1 2019-08-13 stsp if (cmp != 0)
1198 8b473291 2019-02-21 stsp s = *refs_str;
1199 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1200 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1201 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1203 8b473291 2019-02-21 stsp *refs_str = NULL;
1209 8b473291 2019-02-21 stsp return err;
1212 963b370f 2018-05-20 stsp static const struct got_error *
1213 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1214 27a741e5 2019-09-11 stsp int col_tab_align)
1216 5813d178 2019-03-09 stsp char *smallerthan, *at;
1218 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1219 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1220 5813d178 2019-03-09 stsp author = smallerthan + 1;
1221 5813d178 2019-03-09 stsp at = strchr(author, '@');
1223 5813d178 2019-03-09 stsp *at = '\0';
1224 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1227 5813d178 2019-03-09 stsp static const struct got_error *
1228 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1229 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
1230 11b20872 2019-11-08 stsp const size_t date_display_cols, int author_display_cols,
1231 11b20872 2019-11-08 stsp struct tog_colors *colors)
1233 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1234 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1235 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1236 5813d178 2019-03-09 stsp char *author = NULL;
1237 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1238 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1239 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1240 bb737323 2018-05-20 stsp int col, limit;
1241 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1242 ccb26ccd 2018-11-05 stsp struct tm tm;
1243 45d799e2 2018-12-23 stsp time_t committer_time;
1244 11b20872 2019-11-08 stsp struct tog_color *tc;
1246 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1247 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1248 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1249 6db9f7f6 2019-12-10 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1250 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1251 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1253 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1254 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1256 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1257 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_DATE);
1259 11b20872 2019-11-08 stsp wattr_on(view->window,
1260 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1261 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1263 11b20872 2019-11-08 stsp wattr_off(view->window,
1264 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1265 27a741e5 2019-09-11 stsp col = limit;
1266 b39d25c7 2018-07-10 stsp if (col > avail)
1267 b39d25c7 2018-07-10 stsp goto done;
1269 6570a66d 2019-11-08 stsp if (avail >= 120) {
1270 6570a66d 2019-11-08 stsp char *id_str;
1271 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1273 6570a66d 2019-11-08 stsp goto done;
1274 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1276 11b20872 2019-11-08 stsp wattr_on(view->window,
1277 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1278 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1280 11b20872 2019-11-08 stsp wattr_off(view->window,
1281 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1282 6570a66d 2019-11-08 stsp free(id_str);
1284 6570a66d 2019-11-08 stsp if (col > avail)
1285 6570a66d 2019-11-08 stsp goto done;
1288 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1289 5813d178 2019-03-09 stsp if (author == NULL) {
1290 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1291 80ddbec8 2018-04-29 stsp goto done;
1293 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1295 bb737323 2018-05-20 stsp goto done;
1296 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_AUTHOR);
1298 11b20872 2019-11-08 stsp wattr_on(view->window,
1299 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1300 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1302 11b20872 2019-11-08 stsp wattr_off(view->window,
1303 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1304 bb737323 2018-05-20 stsp col += author_width;
1305 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1306 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1308 bb737323 2018-05-20 stsp author_width++;
1310 9c2eaf34 2018-05-20 stsp if (col > avail)
1311 9c2eaf34 2018-05-20 stsp goto done;
1313 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1315 6d9fbc00 2018-04-29 stsp goto done;
1316 bb737323 2018-05-20 stsp logmsg = logmsg0;
1317 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1319 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1320 bb737323 2018-05-20 stsp if (newline)
1321 bb737323 2018-05-20 stsp *newline = '\0';
1322 bb737323 2018-05-20 stsp limit = avail - col;
1323 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1325 bb737323 2018-05-20 stsp goto done;
1326 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1327 bb737323 2018-05-20 stsp col += logmsg_width;
1328 27a741e5 2019-09-11 stsp while (col < avail) {
1329 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1333 80ddbec8 2018-04-29 stsp free(logmsg0);
1334 bb737323 2018-05-20 stsp free(wlogmsg);
1335 5813d178 2019-03-09 stsp free(author);
1336 bb737323 2018-05-20 stsp free(wauthor);
1337 80ddbec8 2018-04-29 stsp free(line);
1338 80ddbec8 2018-04-29 stsp return err;
1341 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1342 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1343 899d86c2 2018-05-10 stsp struct got_object_id *id)
1345 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1347 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1348 80ddbec8 2018-04-29 stsp if (entry == NULL)
1349 899d86c2 2018-05-10 stsp return NULL;
1351 899d86c2 2018-05-10 stsp entry->id = id;
1352 99db9666 2018-05-07 stsp entry->commit = commit;
1353 899d86c2 2018-05-10 stsp return entry;
1356 99db9666 2018-05-07 stsp static void
1357 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1359 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1361 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1362 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1363 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1364 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1365 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1366 99db9666 2018-05-07 stsp free(entry);
1369 99db9666 2018-05-07 stsp static void
1370 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1372 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1373 99db9666 2018-05-07 stsp pop_commit(commits);
1376 c4972b91 2018-05-07 stsp static const struct got_error *
1377 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1378 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1380 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1381 13add988 2019-10-15 stsp regmatch_t regmatch;
1382 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1384 13add988 2019-10-15 stsp *have_match = 0;
1386 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1388 13add988 2019-10-15 stsp return err;
1390 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1392 13add988 2019-10-15 stsp goto done;
1394 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1395 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1396 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1397 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1398 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1399 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1400 13add988 2019-10-15 stsp *have_match = 1;
1402 13add988 2019-10-15 stsp free(id_str);
1403 13add988 2019-10-15 stsp free(logmsg);
1404 13add988 2019-10-15 stsp return err;
1407 13add988 2019-10-15 stsp static const struct got_error *
1408 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1409 13add988 2019-10-15 stsp int minqueue, struct got_repository *repo, const char *path,
1410 13add988 2019-10-15 stsp int *searching, int *search_next_done, regex_t *regex)
1412 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1413 7c1452c1 2020-03-26 stsp int nqueued = 0;
1416 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1417 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1418 1a76625f 2018-10-22 stsp * while updating the display.
1420 13add988 2019-10-15 stsp while (nqueued < minqueue ||
1421 13add988 2019-10-15 stsp (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1422 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1423 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1424 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1425 1a76625f 2018-10-22 stsp int errcode;
1427 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1428 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1431 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1434 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1435 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1436 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1440 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1441 1a76625f 2018-10-22 stsp if (errcode) {
1442 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1443 13add988 2019-10-15 stsp "pthread_mutex_lock");
1447 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1448 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1449 ecb28ae0 2018-07-16 stsp nqueued++;
1450 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1452 13add988 2019-10-15 stsp if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1453 7c1452c1 2020-03-26 stsp int have_match;
1454 13add988 2019-10-15 stsp err = match_commit(&have_match, id, commit, regex);
1457 7c1452c1 2020-03-26 stsp if (have_match)
1458 8f4ed634 2020-03-26 stsp *search_next_done = TOG_SEARCH_HAVE_MORE;
1461 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1462 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1463 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1464 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1469 9ba79e04 2018-06-11 stsp return err;
1472 0553a4e3 2018-04-30 stsp static const struct got_error *
1473 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1474 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1475 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1476 11b20872 2019-11-08 stsp struct got_reflist_head *refs, const char *path, int commits_needed,
1477 11b20872 2019-11-08 stsp struct tog_colors *colors)
1479 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1480 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1481 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1482 60493ae3 2019-06-20 stsp int width;
1483 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1484 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1485 8b473291 2019-02-21 stsp char *refs_str = NULL;
1486 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1487 11b20872 2019-11-08 stsp struct tog_color *tc;
1488 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1490 e0d42f60 2018-07-22 stsp entry = first;
1491 e0d42f60 2018-07-22 stsp ncommits = 0;
1492 e0d42f60 2018-07-22 stsp while (entry) {
1493 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1494 e0d42f60 2018-07-22 stsp *selected = entry;
1497 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1498 e0d42f60 2018-07-22 stsp ncommits++;
1501 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1502 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1504 ecb28ae0 2018-07-16 stsp return err;
1505 8b473291 2019-02-21 stsp if (refs) {
1506 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, (*selected)->id,
1509 8b473291 2019-02-21 stsp goto done;
1513 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1514 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1516 8f4ed634 2020-03-26 stsp if (commits_needed > 0) {
1517 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1518 8f4ed634 2020-03-26 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1519 d2ad595c 2020-04-09 stsp (view->searching && !view->search_next_done) ?
1520 d2ad595c 2020-04-09 stsp "searching..." : "loading...") == -1) {
1521 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1522 8f4ed634 2020-03-26 stsp goto done;
1525 f9686aa5 2020-03-27 stsp const char *search_str = NULL;
1527 f9686aa5 2020-03-27 stsp if (view->searching) {
1528 f9686aa5 2020-03-27 stsp if (view->search_next_done == TOG_SEARCH_NO_MORE)
1529 f9686aa5 2020-03-27 stsp search_str = "no more matches";
1530 f9686aa5 2020-03-27 stsp else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1531 f9686aa5 2020-03-27 stsp search_str = "no matches found";
1532 f9686aa5 2020-03-27 stsp else if (!view->search_next_done)
1533 f9686aa5 2020-03-27 stsp search_str = "searching...";
1536 8f4ed634 2020-03-26 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1537 8f4ed634 2020-03-26 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1538 f9686aa5 2020-03-27 stsp search_str ? search_str :
1539 f9686aa5 2020-03-27 stsp (refs_str ? refs_str : "")) == -1) {
1540 8f4ed634 2020-03-26 stsp err = got_error_from_errno("asprintf");
1541 8f4ed634 2020-03-26 stsp goto done;
1545 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1546 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1547 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1548 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1549 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1550 1a76625f 2018-10-22 stsp header = NULL;
1551 1a76625f 2018-10-22 stsp goto done;
1553 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1554 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1555 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1556 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1557 1a76625f 2018-10-22 stsp header = NULL;
1558 1a76625f 2018-10-22 stsp goto done;
1560 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1562 1a76625f 2018-10-22 stsp goto done;
1564 2814baeb 2018-08-01 stsp werase(view->window);
1566 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1567 a3404814 2018-09-02 stsp wstandout(view->window);
1568 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1570 11b20872 2019-11-08 stsp wattr_on(view->window,
1571 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1572 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1574 11b20872 2019-11-08 stsp wattr_off(view->window,
1575 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1576 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1577 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1580 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1581 a3404814 2018-09-02 stsp wstandend(view->window);
1582 ecb28ae0 2018-07-16 stsp free(wline);
1583 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1584 1a76625f 2018-10-22 stsp goto done;
1586 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1587 899d86c2 2018-05-10 stsp entry = first;
1588 5813d178 2019-03-09 stsp ncommits = 0;
1589 5813d178 2019-03-09 stsp while (entry) {
1590 5813d178 2019-03-09 stsp char *author;
1591 5813d178 2019-03-09 stsp wchar_t *wauthor;
1592 5813d178 2019-03-09 stsp int width;
1593 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1595 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1596 5813d178 2019-03-09 stsp if (author == NULL) {
1597 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1598 5813d178 2019-03-09 stsp goto done;
1600 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1601 27a741e5 2019-09-11 stsp date_display_cols);
1602 5813d178 2019-03-09 stsp if (author_cols < width)
1603 5813d178 2019-03-09 stsp author_cols = width;
1604 5813d178 2019-03-09 stsp free(wauthor);
1605 5813d178 2019-03-09 stsp free(author);
1606 7ca04879 2019-10-19 stsp ncommits++;
1607 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1610 5813d178 2019-03-09 stsp entry = first;
1611 899d86c2 2018-05-10 stsp *last = first;
1612 867c6645 2018-07-10 stsp ncommits = 0;
1613 899d86c2 2018-05-10 stsp while (entry) {
1614 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1616 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1617 2814baeb 2018-08-01 stsp wstandout(view->window);
1618 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1619 11b20872 2019-11-08 stsp date_display_cols, author_cols, colors);
1620 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1621 2814baeb 2018-08-01 stsp wstandend(view->window);
1623 60493ae3 2019-06-20 stsp goto done;
1624 0553a4e3 2018-04-30 stsp ncommits++;
1625 899d86c2 2018-05-10 stsp *last = entry;
1626 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1629 1a57306a 2018-09-02 stsp view_vborder(view);
1631 1a76625f 2018-10-22 stsp free(id_str);
1632 8b473291 2019-02-21 stsp free(refs_str);
1633 1a76625f 2018-10-22 stsp free(ncommits_str);
1634 1a76625f 2018-10-22 stsp free(header);
1635 80ddbec8 2018-04-29 stsp return err;
1638 07b55e75 2018-05-10 stsp static void
1639 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1640 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1641 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1643 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1644 07b55e75 2018-05-10 stsp int nscrolled = 0;
1646 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1647 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1650 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1651 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1652 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1653 07b55e75 2018-05-10 stsp if (entry) {
1654 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1655 07b55e75 2018-05-10 stsp nscrolled++;
1660 aa075928 2018-05-10 stsp static const struct got_error *
1661 7c1452c1 2020-03-26 stsp trigger_log_thread(struct tog_view *log_view, int wait,
1662 7c1452c1 2020-03-26 stsp int *commits_needed, int *log_complete,
1663 7c1452c1 2020-03-26 stsp pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1665 5e224a3e 2019-02-22 stsp int errcode;
1667 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1669 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1670 5e224a3e 2019-02-22 stsp if (*log_complete)
1673 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1674 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1675 7aafa0d1 2019-02-22 stsp if (errcode)
1676 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1677 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1680 7c1452c1 2020-03-26 stsp * The mutex will be released while the view loop waits
1681 7c1452c1 2020-03-26 stsp * in wgetch(), at which time the log thread will run.
1683 7c1452c1 2020-03-26 stsp if (!wait)
1686 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1687 7c1452c1 2020-03-26 stsp show_log_view(log_view);
1688 7c1452c1 2020-03-26 stsp update_panels();
1689 7c1452c1 2020-03-26 stsp doupdate();
1691 7c1452c1 2020-03-26 stsp /* Wait right here while next commit is being loaded. */
1692 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1693 82954512 2020-02-03 stsp if (errcode)
1694 82954512 2020-02-03 stsp return got_error_set_errno(errcode,
1695 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1697 7c1452c1 2020-03-26 stsp /* Display progress update in log view. */
1698 7c1452c1 2020-03-26 stsp show_log_view(log_view);
1699 7c1452c1 2020-03-26 stsp update_panels();
1700 7c1452c1 2020-03-26 stsp doupdate();
1703 5e224a3e 2019-02-22 stsp return NULL;
1706 5e224a3e 2019-02-22 stsp static const struct got_error *
1707 7c1452c1 2020-03-26 stsp scroll_down(struct tog_view *log_view,
1708 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1709 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1710 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1711 7c1452c1 2020-03-26 stsp pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1713 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1714 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1715 7c1452c1 2020-03-26 stsp int nscrolled = 0, ncommits_needed;
1717 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1718 5e224a3e 2019-02-22 stsp return NULL;
1720 7c1452c1 2020-03-26 stsp ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1721 7c1452c1 2020-03-26 stsp if (commits->ncommits < ncommits_needed && !*log_complete) {
1723 7c1452c1 2020-03-26 stsp * Ask the log thread for required amount of commits.
1725 7c1452c1 2020-03-26 stsp (*commits_needed) += maxscroll;
1726 7c1452c1 2020-03-26 stsp err = trigger_log_thread(log_view, 1, commits_needed,
1727 7c1452c1 2020-03-26 stsp log_complete, need_commits, commit_loaded);
1729 5e224a3e 2019-02-22 stsp return err;
1733 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1734 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1737 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1739 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1740 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1742 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1743 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1745 dd0a52c1 2018-05-20 stsp return err;
1748 cd0acaa7 2018-05-20 stsp static const struct got_error *
1749 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1750 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1751 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1752 8b473291 2019-02-21 stsp struct got_repository *repo)
1754 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1755 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1756 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1758 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1759 15a94983 2018-12-23 stsp if (diff_view == NULL)
1760 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1762 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1763 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1764 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1765 e5a0f69f 2018-08-18 stsp if (err == NULL)
1766 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1767 cd0acaa7 2018-05-20 stsp return err;
1770 80ddbec8 2018-04-29 stsp static const struct got_error *
1771 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1772 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1774 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1776 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1777 941e9f74 2019-05-21 stsp if (parent == NULL)
1778 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1780 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1781 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1782 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1783 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1784 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1785 941e9f74 2019-05-21 stsp s->tree = subtree;
1786 941e9f74 2019-05-21 stsp s->selected = 0;
1787 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1788 941e9f74 2019-05-21 stsp return NULL;
1791 941e9f74 2019-05-21 stsp static const struct got_error *
1792 55cccc34 2020-02-20 stsp tree_view_walk_path(struct tog_tree_view_state *s,
1793 55cccc34 2020-02-20 stsp struct got_object_id *commit_id,
1794 55cccc34 2020-02-20 stsp const char *path, struct got_repository *repo)
1796 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1797 55cccc34 2020-02-20 stsp struct got_tree_object *tree = NULL;
1798 941e9f74 2019-05-21 stsp const char *p;
1799 55cccc34 2020-02-20 stsp char *slash, *subpath = NULL;
1801 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1803 941e9f74 2019-05-21 stsp while (*p) {
1804 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1805 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1806 56e0773d 2019-11-28 stsp char *te_name;
1808 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1811 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1812 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1813 941e9f74 2019-05-21 stsp if (slash == NULL)
1814 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1816 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1817 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1818 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1821 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1822 56e0773d 2019-11-28 stsp if (te == NULL) {
1823 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1824 56e0773d 2019-11-28 stsp free(te_name);
1827 56e0773d 2019-11-28 stsp free(te_name);
1828 56e0773d 2019-11-28 stsp s->selected_entry = te;
1829 56e0773d 2019-11-28 stsp s->selected = got_tree_entry_get_index(te);
1830 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1831 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1833 56e0773d 2019-11-28 stsp if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1834 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1835 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1836 67409a31 2019-05-24 stsp s->selected = 0;
1840 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1841 941e9f74 2019-05-21 stsp if (slash)
1842 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1844 941e9f74 2019-05-21 stsp subpath = strdup(path);
1845 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1846 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1850 55cccc34 2020-02-20 stsp err = got_object_id_by_path(&tree_id, repo, commit_id,
1855 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1856 941e9f74 2019-05-21 stsp free(tree_id);
1860 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1861 941e9f74 2019-05-21 stsp if (err) {
1862 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1865 941e9f74 2019-05-21 stsp if (slash == NULL)
1867 941e9f74 2019-05-21 stsp free(subpath);
1868 941e9f74 2019-05-21 stsp subpath = NULL;
1869 941e9f74 2019-05-21 stsp p = slash;
1872 941e9f74 2019-05-21 stsp free(subpath);
1873 1a76625f 2018-10-22 stsp return err;
1876 61266923 2020-01-14 stsp static const struct got_error *
1877 55cccc34 2020-02-20 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1878 55cccc34 2020-02-20 stsp struct commit_queue_entry *entry, const char *path,
1879 55cccc34 2020-02-20 stsp struct got_reflist_head *refs, struct got_repository *repo)
1881 55cccc34 2020-02-20 stsp const struct got_error *err = NULL;
1882 55cccc34 2020-02-20 stsp struct got_tree_object *tree;
1883 55cccc34 2020-02-20 stsp struct tog_tree_view_state *s;
1884 55cccc34 2020-02-20 stsp struct tog_view *tree_view;
1886 55cccc34 2020-02-20 stsp err = got_object_open_as_tree(&tree, repo,
1887 55cccc34 2020-02-20 stsp got_object_commit_get_tree_id(entry->commit));
1889 55cccc34 2020-02-20 stsp return err;
1891 55cccc34 2020-02-20 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1892 55cccc34 2020-02-20 stsp if (tree_view == NULL)
1893 55cccc34 2020-02-20 stsp return got_error_from_errno("view_open");
1895 55cccc34 2020-02-20 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1896 55cccc34 2020-02-20 stsp if (err) {
1897 55cccc34 2020-02-20 stsp got_object_tree_close(tree);
1898 55cccc34 2020-02-20 stsp return err;
1900 55cccc34 2020-02-20 stsp s = &tree_view->state.tree;
1902 55cccc34 2020-02-20 stsp *new_view = tree_view;
1904 55cccc34 2020-02-20 stsp if (got_path_is_root_dir(path))
1905 55cccc34 2020-02-20 stsp return NULL;
1907 55cccc34 2020-02-20 stsp return tree_view_walk_path(s, entry->id, path, repo);
1910 55cccc34 2020-02-20 stsp static const struct got_error *
1911 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1913 61266923 2020-01-14 stsp sigset_t sigset;
1914 61266923 2020-01-14 stsp int errcode;
1916 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1917 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1919 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1920 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1921 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1922 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
1923 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1925 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
1926 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
1927 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1929 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1930 61266923 2020-01-14 stsp if (errcode)
1931 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
1933 61266923 2020-01-14 stsp return NULL;
1936 1a76625f 2018-10-22 stsp static void *
1937 1a76625f 2018-10-22 stsp log_thread(void *arg)
1939 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1940 1a76625f 2018-10-22 stsp int errcode = 0;
1941 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1942 1a76625f 2018-10-22 stsp int done = 0;
1944 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
1946 61266923 2020-01-14 stsp return (void *)err;
1948 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1949 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1950 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1951 13add988 2019-10-15 stsp a->regex);
1952 1a76625f 2018-10-22 stsp if (err) {
1953 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1954 1a76625f 2018-10-22 stsp return (void *)err;
1955 1a76625f 2018-10-22 stsp err = NULL;
1957 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1958 1a76625f 2018-10-22 stsp a->commits_needed--;
1960 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1961 3abe8080 2019-04-10 stsp if (errcode) {
1962 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1963 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1965 3abe8080 2019-04-10 stsp } else if (*a->quit)
1967 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1968 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1969 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1970 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1973 7c1452c1 2020-03-26 stsp errcode = pthread_cond_signal(&a->commit_loaded);
1974 7c1452c1 2020-03-26 stsp if (errcode) {
1975 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
1976 7c1452c1 2020-03-26 stsp "pthread_cond_signal");
1977 7c1452c1 2020-03-26 stsp pthread_mutex_unlock(&tog_mutex);
1982 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1984 7c1452c1 2020-03-26 stsp if (a->commits_needed == 0) {
1985 7c1452c1 2020-03-26 stsp errcode = pthread_cond_wait(&a->need_commits,
1986 7c1452c1 2020-03-26 stsp &tog_mutex);
1987 7c1452c1 2020-03-26 stsp if (errcode)
1988 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode,
1989 7c1452c1 2020-03-26 stsp "pthread_cond_wait");
1993 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1994 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1995 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1996 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1998 3abe8080 2019-04-10 stsp a->log_complete = 1;
1999 1a76625f 2018-10-22 stsp return (void *)err;
2002 1a76625f 2018-10-22 stsp static const struct got_error *
2003 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
2005 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2006 1a76625f 2018-10-22 stsp int errcode;
2008 1a76625f 2018-10-22 stsp if (s->thread) {
2009 1a76625f 2018-10-22 stsp s->quit = 1;
2010 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
2011 1a76625f 2018-10-22 stsp if (errcode)
2012 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2013 2af4a041 2019-05-11 jcs "pthread_cond_signal");
2014 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2015 1a76625f 2018-10-22 stsp if (errcode)
2016 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2017 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2018 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
2019 1a76625f 2018-10-22 stsp if (errcode)
2020 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2021 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2022 1a76625f 2018-10-22 stsp if (errcode)
2023 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2024 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2025 1a76625f 2018-10-22 stsp s->thread = NULL;
2028 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2029 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2030 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
2032 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
2033 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
2034 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2037 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2038 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2039 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2042 9343a5fb 2018-06-23 stsp return err;
2045 9343a5fb 2018-06-23 stsp static const struct got_error *
2046 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2048 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2049 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2051 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2052 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2053 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2054 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2055 1a76625f 2018-10-22 stsp free(s->start_id);
2056 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2057 1a76625f 2018-10-22 stsp return err;
2060 1a76625f 2018-10-22 stsp static const struct got_error *
2061 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2063 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2065 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2066 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2067 60493ae3 2019-06-20 stsp return NULL;
2070 60493ae3 2019-06-20 stsp static const struct got_error *
2071 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2073 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2074 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2075 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2077 f9686aa5 2020-03-27 stsp /* Display progress update in log view. */
2078 f9686aa5 2020-03-27 stsp show_log_view(view);
2079 f9686aa5 2020-03-27 stsp update_panels();
2080 f9686aa5 2020-03-27 stsp doupdate();
2082 96e2b566 2019-07-08 stsp if (s->search_entry) {
2083 13add988 2019-10-15 stsp int errcode, ch;
2084 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&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_unlock");
2088 13add988 2019-10-15 stsp ch = wgetch(view->window);
2089 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2090 13add988 2019-10-15 stsp if (errcode)
2091 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2092 13add988 2019-10-15 stsp "pthread_mutex_lock");
2093 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2094 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2095 678cbce5 2019-07-28 stsp return NULL;
2097 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2098 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2100 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2101 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2102 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2103 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2104 8f4ed634 2020-03-26 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
2106 8f4ed634 2020-03-26 stsp entry = TAILQ_PREV(s->matched_entry,
2107 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2109 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2110 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2112 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2115 60493ae3 2019-06-20 stsp while (1) {
2116 13add988 2019-10-15 stsp int have_match = 0;
2118 60493ae3 2019-06-20 stsp if (entry == NULL) {
2119 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2120 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2121 f9967bca 2020-03-27 stsp view->search_next_done =
2122 f9967bca 2020-03-27 stsp (s->matched_entry == NULL ?
2123 f9967bca 2020-03-27 stsp TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2124 8f4ed634 2020-03-26 stsp s->search_entry = NULL;
2125 f801134a 2019-06-25 stsp return NULL;
2128 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2129 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2130 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2132 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2133 7c1452c1 2020-03-26 stsp return trigger_log_thread(view, 0,
2134 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
2135 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
2136 7c1452c1 2020-03-26 stsp &s->thread_args.need_commits,
2137 7c1452c1 2020-03-26 stsp &s->thread_args.commit_loaded);
2140 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2141 13add988 2019-10-15 stsp &view->regex);
2144 13add988 2019-10-15 stsp if (have_match) {
2145 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
2146 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2150 96e2b566 2019-07-08 stsp s->search_entry = entry;
2151 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2152 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2154 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2157 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2158 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2159 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2160 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2162 60493ae3 2019-06-20 stsp return err;
2165 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2166 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2168 60493ae3 2019-06-20 stsp return err;
2173 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2175 60493ae3 2019-06-20 stsp return NULL;
2178 60493ae3 2019-06-20 stsp static const struct got_error *
2179 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2180 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
2181 f135c941 2020-02-20 stsp const char *head_ref_name, const char *in_repo_path,
2182 b672a97a 2020-01-27 stsp int log_branches)
2184 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2185 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2186 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2187 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2188 1a76625f 2018-10-22 stsp int errcode;
2190 f135c941 2020-02-20 stsp if (in_repo_path != s->in_repo_path) {
2191 f135c941 2020-02-20 stsp free(s->in_repo_path);
2192 f135c941 2020-02-20 stsp s->in_repo_path = strdup(in_repo_path);
2193 f135c941 2020-02-20 stsp if (s->in_repo_path == NULL)
2194 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2197 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2198 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2199 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2201 8b473291 2019-02-21 stsp s->refs = refs;
2202 fb2756b9 2018-08-04 stsp s->repo = repo;
2203 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
2204 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2205 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2206 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2207 5036bf37 2018-09-24 stsp goto done;
2209 b672a97a 2020-01-27 stsp s->log_branches = log_branches;
2211 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2212 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2213 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2214 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2216 11b20872 2019-11-08 stsp goto done;
2217 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2218 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2219 11b20872 2019-11-08 stsp if (err) {
2220 11b20872 2019-11-08 stsp free_colors(&s->colors);
2221 11b20872 2019-11-08 stsp goto done;
2223 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2224 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2225 11b20872 2019-11-08 stsp if (err) {
2226 11b20872 2019-11-08 stsp free_colors(&s->colors);
2227 11b20872 2019-11-08 stsp goto done;
2231 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2232 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2233 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2234 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2235 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2237 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2239 1a76625f 2018-10-22 stsp goto done;
2240 b672a97a 2020-01-27 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2241 b672a97a 2020-01-27 stsp !s->log_branches);
2243 1a76625f 2018-10-22 stsp goto done;
2244 c5b78334 2020-01-12 stsp err = got_commit_graph_iter_start(thread_graph,
2245 c5b78334 2020-01-12 stsp s->start_id, s->repo, NULL, NULL);
2247 c5b78334 2020-01-12 stsp goto done;
2249 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2250 1a76625f 2018-10-22 stsp if (errcode) {
2251 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2252 1a76625f 2018-10-22 stsp goto done;
2254 7c1452c1 2020-03-26 stsp errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2255 7c1452c1 2020-03-26 stsp if (errcode) {
2256 7c1452c1 2020-03-26 stsp err = got_error_set_errno(errcode, "pthread_cond_init");
2257 7c1452c1 2020-03-26 stsp goto done;
2260 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2261 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2262 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2263 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2264 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2265 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2266 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2267 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2268 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2269 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2270 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2271 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2272 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2275 1a76625f 2018-10-22 stsp close_log_view(view);
2276 ba4f502b 2018-08-04 stsp return err;
2279 e5a0f69f 2018-08-18 stsp static const struct got_error *
2280 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2282 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2284 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2285 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2286 2b380cc8 2018-10-24 stsp &s->thread_args);
2287 2b380cc8 2018-10-24 stsp if (errcode)
2288 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2291 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
2292 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
2293 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
2294 11b20872 2019-11-08 stsp s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2297 e5a0f69f 2018-08-18 stsp static const struct got_error *
2298 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2299 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2301 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2302 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2303 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2304 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2305 669b5ffa 2018-10-07 stsp int begin_x = 0;
2306 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2308 e5a0f69f 2018-08-18 stsp switch (ch) {
2310 1e37a5c2 2019-05-12 jcs s->quit = 1;
2313 1e37a5c2 2019-05-12 jcs case KEY_UP:
2316 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2318 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2319 1e37a5c2 2019-05-12 jcs s->selected--;
2321 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
2322 1144d21a 2019-06-21 stsp &s->commits);
2324 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2325 a4292ac5 2019-05-12 jcs case CTRL('b'):
2326 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2328 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
2329 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
2330 1e37a5c2 2019-05-12 jcs s->selected = 0;
2333 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
2334 1831ac02 2020-03-23 naddy view->nlines - 1, &s->commits);
2337 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2340 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2342 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2343 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2344 1e37a5c2 2019-05-12 jcs s->selected++;
2347 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
2348 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
2349 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
2350 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2351 7c1452c1 2020-03-26 stsp &s->thread_args.need_commits,
2352 7c1452c1 2020-03-26 stsp &s->thread_args.commit_loaded);
2354 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2355 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2356 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2357 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2358 1e37a5c2 2019-05-12 jcs if (first == NULL)
2360 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
2361 1831ac02 2020-03-23 naddy view->nlines - 1, &s->last_displayed_entry,
2362 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
2363 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2364 7c1452c1 2020-03-26 stsp &s->thread_args.need_commits,
2365 7c1452c1 2020-03-26 stsp &s->thread_args.commit_loaded);
2368 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2369 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2370 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2371 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2372 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2373 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2375 1e37a5c2 2019-05-12 jcs err = NULL;
2378 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2379 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2380 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2381 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2382 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2384 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2387 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2389 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2390 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2391 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2392 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2393 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
2396 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2397 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2399 1e37a5c2 2019-05-12 jcs return err;
2400 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2402 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2405 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2406 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2408 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2411 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2413 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2414 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2415 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2416 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2419 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2420 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2422 1e37a5c2 2019-05-12 jcs return err;
2423 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2425 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2428 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2429 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2431 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2433 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2434 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2436 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2437 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2438 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2440 1e37a5c2 2019-05-12 jcs return err;
2441 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2442 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2443 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2444 638f9024 2019-05-13 stsp return got_error_from_errno(
2445 1e37a5c2 2019-05-12 jcs "view_open");
2446 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2447 f135c941 2020-02-20 stsp s->repo, s->head_ref_name, parent_path,
2448 b672a97a 2020-01-27 stsp s->log_branches);
2450 1e37a5c2 2019-05-12 jcs return err;;
2451 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2452 1e37a5c2 2019-05-12 jcs *new_view = lv;
2454 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2455 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2457 1e37a5c2 2019-05-12 jcs return NULL;
2460 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2461 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2463 d01904d4 2019-06-25 stsp return err;
2464 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2465 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2466 d01904d4 2019-06-25 stsp if (lv == NULL)
2467 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2468 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&start_id, NULL,
2469 71a27632 2020-01-15 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2470 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2471 ef129c5e 2019-08-03 stsp if (err) {
2472 ef129c5e 2019-08-03 stsp view_close(lv);
2473 d01904d4 2019-06-25 stsp return err;
2475 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2476 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2477 e6b23735 2019-10-04 stsp free(start_id);
2478 e6b23735 2019-10-04 stsp view_close(lv);
2479 e6b23735 2019-10-04 stsp return got_error_from_errno("strdup");
2481 e6b23735 2019-10-04 stsp got_ref_list_free(s->refs);
2482 e6b23735 2019-10-04 stsp err = got_ref_list(s->refs, s->repo, NULL,
2483 e6b23735 2019-10-04 stsp got_ref_cmp_by_name, NULL);
2484 e6b23735 2019-10-04 stsp if (err) {
2485 d01904d4 2019-06-25 stsp free(start_id);
2486 ef129c5e 2019-08-03 stsp view_close(lv);
2487 50284065 2019-10-04 stsp return err;
2489 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2490 f135c941 2020-02-20 stsp s->head_ref_name, in_repo_path, s->log_branches);
2491 ef129c5e 2019-08-03 stsp if (err) {
2492 ef129c5e 2019-08-03 stsp free(start_id);
2493 b672a97a 2020-01-27 stsp view_close(lv);
2494 b672a97a 2020-01-27 stsp return err;;
2496 b672a97a 2020-01-27 stsp *dead_view = view;
2497 b672a97a 2020-01-27 stsp *new_view = lv;
2500 b672a97a 2020-01-27 stsp s->log_branches = !s->log_branches;
2501 b672a97a 2020-01-27 stsp err = stop_log_thread(s);
2503 b672a97a 2020-01-27 stsp return err;
2504 b672a97a 2020-01-27 stsp lv = view_open(view->nlines, view->ncols,
2505 b672a97a 2020-01-27 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2506 b672a97a 2020-01-27 stsp if (lv == NULL)
2507 b672a97a 2020-01-27 stsp return got_error_from_errno("view_open");
2508 b672a97a 2020-01-27 stsp err = open_log_view(lv, s->start_id, s->refs, s->repo,
2509 f135c941 2020-02-20 stsp s->head_ref_name, s->in_repo_path, s->log_branches);
2510 b672a97a 2020-01-27 stsp if (err) {
2511 ef129c5e 2019-08-03 stsp view_close(lv);
2512 d01904d4 2019-06-25 stsp return err;;
2514 d01904d4 2019-06-25 stsp *dead_view = view;
2515 d01904d4 2019-06-25 stsp *new_view = lv;
2521 80ddbec8 2018-04-29 stsp return err;
2524 4ed7e80c 2018-05-20 stsp static const struct got_error *
2525 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2527 c2db6724 2019-01-04 stsp const struct got_error *error;
2529 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2530 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2531 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2533 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2534 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2536 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2537 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2539 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2540 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2542 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2543 c2db6724 2019-01-04 stsp if (error != NULL)
2544 c2db6724 2019-01-04 stsp return error;
2546 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2547 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2549 c2db6724 2019-01-04 stsp return NULL;
2552 a915003a 2019-02-05 stsp static void
2553 a915003a 2019-02-05 stsp init_curses(void)
2555 a915003a 2019-02-05 stsp initscr();
2557 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2560 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2561 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2562 a915003a 2019-02-05 stsp curs_set(0);
2563 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2564 6d17833f 2019-11-08 stsp start_color();
2565 6d17833f 2019-11-08 stsp use_default_colors();
2567 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2568 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2569 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2572 c2db6724 2019-01-04 stsp static const struct got_error *
2573 f135c941 2020-02-20 stsp get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2574 f135c941 2020-02-20 stsp struct got_repository *repo, struct got_worktree *worktree)
2576 f135c941 2020-02-20 stsp const struct got_error *err = NULL;
2578 f135c941 2020-02-20 stsp if (argc == 0) {
2579 f135c941 2020-02-20 stsp *in_repo_path = strdup("/");
2580 f135c941 2020-02-20 stsp if (*in_repo_path == NULL)
2581 f135c941 2020-02-20 stsp return got_error_from_errno("strdup");
2582 f135c941 2020-02-20 stsp return NULL;
2585 f135c941 2020-02-20 stsp if (worktree) {
2586 f135c941 2020-02-20 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2587 f135c941 2020-02-20 stsp char *wt_path, *p;
2589 f135c941 2020-02-20 stsp err = got_worktree_resolve_path(&wt_path, worktree, argv[0]);
2591 f135c941 2020-02-20 stsp return err;
2593 f135c941 2020-02-20 stsp if (asprintf(&p, "%s%s%s", prefix,
2594 f135c941 2020-02-20 stsp (strcmp(prefix, "/") != 0) ? "/" : "", wt_path) == -1) {
2595 f135c941 2020-02-20 stsp err = got_error_from_errno("asprintf");
2596 f135c941 2020-02-20 stsp free(wt_path);
2597 f135c941 2020-02-20 stsp return err;
2599 f135c941 2020-02-20 stsp err = got_repo_map_path(in_repo_path, repo, p, 0);
2601 f135c941 2020-02-20 stsp free(wt_path);
2603 f135c941 2020-02-20 stsp err = got_repo_map_path(in_repo_path, repo, argv[0], 1);
2605 f135c941 2020-02-20 stsp return err;
2608 f135c941 2020-02-20 stsp static const struct got_error *
2609 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2611 80ddbec8 2018-04-29 stsp const struct got_error *error;
2612 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2613 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2614 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2615 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2616 f135c941 2020-02-20 stsp char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2617 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2618 f135c941 2020-02-20 stsp int ch, log_branches = 0;
2619 04cc582a 2018-08-01 stsp struct tog_view *view;
2621 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2623 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2624 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2625 c2db6724 2019-01-04 stsp NULL) == -1)
2626 80ddbec8 2018-04-29 stsp err(1, "pledge");
2629 b672a97a 2020-01-27 stsp while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2630 80ddbec8 2018-04-29 stsp switch (ch) {
2632 b672a97a 2020-01-27 stsp log_branches = 1;
2635 80ddbec8 2018-04-29 stsp start_commit = optarg;
2638 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2639 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2640 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2644 17020d27 2019-03-07 stsp usage_log();
2645 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2649 80ddbec8 2018-04-29 stsp argc -= optind;
2650 80ddbec8 2018-04-29 stsp argv += optind;
2652 f135c941 2020-02-20 stsp if (argc > 1)
2653 f135c941 2020-02-20 stsp usage_log();
2655 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2656 6962eb72 2020-02-20 stsp if (cwd == NULL)
2657 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
2659 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2660 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2661 963f97a1 2019-03-18 stsp goto done;
2663 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2664 a1fbf39a 2019-08-11 stsp if (worktree)
2665 6962eb72 2020-02-20 stsp repo_path =
2666 6962eb72 2020-02-20 stsp strdup(got_worktree_get_repo_path(worktree));
2668 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2670 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2671 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2672 963f97a1 2019-03-18 stsp goto done;
2675 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2676 80ddbec8 2018-04-29 stsp if (error != NULL)
2677 ecb28ae0 2018-07-16 stsp goto done;
2679 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2680 f135c941 2020-02-20 stsp repo, worktree);
2681 f135c941 2020-02-20 stsp if (error)
2682 f135c941 2020-02-20 stsp goto done;
2684 f135c941 2020-02-20 stsp init_curses();
2686 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2687 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2688 c02c541e 2019-03-29 stsp if (error)
2689 c02c541e 2019-03-29 stsp goto done;
2691 15a94983 2018-12-23 stsp if (start_commit == NULL)
2692 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, worktree ?
2693 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2694 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2696 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, start_commit,
2697 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2698 80ddbec8 2018-04-29 stsp if (error != NULL)
2699 8b473291 2019-02-21 stsp goto done;
2701 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2702 8b473291 2019-02-21 stsp if (error)
2703 ecb28ae0 2018-07-16 stsp goto done;
2705 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2706 04cc582a 2018-08-01 stsp if (view == NULL) {
2707 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2708 04cc582a 2018-08-01 stsp goto done;
2710 2fc00ff4 2019-08-31 stsp if (worktree) {
2711 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2712 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2713 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2714 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2715 2fc00ff4 2019-08-31 stsp goto done;
2718 2fc00ff4 2019-08-31 stsp error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2719 f135c941 2020-02-20 stsp in_repo_path, log_branches);
2720 ba4f502b 2018-08-04 stsp if (error)
2721 ba4f502b 2018-08-04 stsp goto done;
2722 2fc00ff4 2019-08-31 stsp if (worktree) {
2723 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2724 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2725 2fc00ff4 2019-08-31 stsp worktree = NULL;
2727 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2729 f135c941 2020-02-20 stsp free(in_repo_path);
2730 ecb28ae0 2018-07-16 stsp free(repo_path);
2731 ecb28ae0 2018-07-16 stsp free(cwd);
2732 899d86c2 2018-05-10 stsp free(start_id);
2733 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2735 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2736 ec142235 2019-03-07 stsp if (worktree)
2737 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2738 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2739 80ddbec8 2018-04-29 stsp return error;
2742 4ed7e80c 2018-05-20 stsp __dead static void
2743 9f7d7167 2018-04-29 stsp usage_diff(void)
2746 09b5bff8 2020-02-23 naddy fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2747 9f7d7167 2018-04-29 stsp getprogname());
2751 b304db33 2018-05-20 stsp static char *
2752 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2754 b304db33 2018-05-20 stsp char *line;
2755 b304db33 2018-05-20 stsp size_t linelen;
2756 b304db33 2018-05-20 stsp size_t lineno;
2757 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2759 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2761 b304db33 2018-05-20 stsp *len = linelen;
2762 b304db33 2018-05-20 stsp return line;
2765 6d17833f 2019-11-08 stsp static int
2766 6d17833f 2019-11-08 stsp match_line(const char *line, regex_t *regex)
2768 6d17833f 2019-11-08 stsp regmatch_t regmatch;
2770 6d17833f 2019-11-08 stsp return regexec(regex, line, 1, ®match, 0) == 0;
2773 f26dddb7 2019-11-08 stsp struct tog_color *
2774 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2776 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2778 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2779 6d17833f 2019-11-08 stsp if (match_line(line, &tc->regex))
2780 6d17833f 2019-11-08 stsp return tc;
2783 6d17833f 2019-11-08 stsp return NULL;
2786 4ed7e80c 2018-05-20 stsp static const struct got_error *
2787 f44b1f58 2020-02-02 tracey draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2788 f44b1f58 2020-02-02 tracey int selected_line, int max_lines, int *last_displayed_line, int *eof,
2789 f44b1f58 2020-02-02 tracey char *header, struct tog_colors *colors)
2791 61e69b96 2018-05-20 stsp const struct got_error *err;
2792 f44b1f58 2020-02-02 tracey int lineno = 0, nprinted = 0;
2793 b304db33 2018-05-20 stsp char *line;
2794 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2795 b304db33 2018-05-20 stsp size_t len;
2796 61e69b96 2018-05-20 stsp wchar_t *wline;
2797 e0b650dd 2018-05-20 stsp int width;
2799 26ed57b2 2018-05-19 stsp rewind(f);
2800 f7d12f7e 2018-08-01 stsp werase(view->window);
2802 a3404814 2018-09-02 stsp if (header) {
2803 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
2804 a3404814 2018-09-02 stsp if (err) {
2805 a3404814 2018-09-02 stsp return err;
2808 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2809 a3404814 2018-09-02 stsp wstandout(view->window);
2810 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2811 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2812 a3404814 2018-09-02 stsp wstandend(view->window);
2813 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2814 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2816 a3404814 2018-09-02 stsp if (max_lines <= 1)
2817 a3404814 2018-09-02 stsp return NULL;
2818 a3404814 2018-09-02 stsp max_lines--;
2822 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2823 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2824 26ed57b2 2018-05-19 stsp if (line == NULL) {
2828 f44b1f58 2020-02-02 tracey if (++lineno < *first_displayed_line) {
2829 26ed57b2 2018-05-19 stsp free(line);
2833 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2834 61e69b96 2018-05-20 stsp if (err) {
2835 61e69b96 2018-05-20 stsp free(line);
2836 61e69b96 2018-05-20 stsp return err;
2839 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
2841 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2842 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2843 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2845 6d17833f 2019-11-08 stsp wattr_off(view->window,
2846 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2847 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2848 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2849 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2850 f44b1f58 2020-02-02 tracey *first_displayed_line = lineno;
2851 26ed57b2 2018-05-19 stsp free(line);
2852 2550e4c3 2018-07-13 stsp free(wline);
2853 2550e4c3 2018-07-13 stsp wline = NULL;
2855 f44b1f58 2020-02-02 tracey *last_displayed_line = lineno;
2857 1a57306a 2018-09-02 stsp view_vborder(view);
2859 c3e9aa98 2019-05-13 jcs if (*eof) {
2860 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2861 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2862 c3e9aa98 2019-05-13 jcs nprinted++;
2865 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2867 c3e9aa98 2019-05-13 jcs return err;
2870 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2871 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2872 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2875 26ed57b2 2018-05-19 stsp return NULL;
2878 abd2672a 2018-12-23 stsp static char *
2879 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2881 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2882 09867e48 2019-08-13 stsp char *p, *s;
2884 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2885 09867e48 2019-08-13 stsp if (tm == NULL)
2886 09867e48 2019-08-13 stsp return NULL;
2887 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2888 09867e48 2019-08-13 stsp if (s == NULL)
2889 09867e48 2019-08-13 stsp return NULL;
2890 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2892 abd2672a 2018-12-23 stsp *p = '\0';
2896 4ed7e80c 2018-05-20 stsp static const struct got_error *
2897 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
2898 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
2900 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
2901 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2902 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2903 0208f208 2020-05-05 stsp struct got_object_qid *qid;
2905 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2906 0208f208 2020-05-05 stsp if (qid != NULL) {
2907 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
2908 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
2911 0208f208 2020-05-05 stsp return err;
2913 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
2914 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
2918 0208f208 2020-05-05 stsp if (tree_id1) {
2919 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2921 0208f208 2020-05-05 stsp goto done;
2924 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
2925 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2927 0208f208 2020-05-05 stsp goto done;
2929 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
2930 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
2932 0208f208 2020-05-05 stsp if (tree1)
2933 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
2934 0208f208 2020-05-05 stsp if (tree2)
2935 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
2936 0208f208 2020-05-05 stsp return err;
2939 0208f208 2020-05-05 stsp static const struct got_error *
2940 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2941 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2943 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2944 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
2945 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2946 5943eee2 2019-08-13 stsp char *id_str = NULL, *logmsg = NULL;
2947 45d799e2 2018-12-23 stsp time_t committer_time;
2948 45d799e2 2018-12-23 stsp const char *author, *committer;
2949 8b473291 2019-02-21 stsp char *refs_str = NULL;
2950 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
2951 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
2953 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
2955 8b473291 2019-02-21 stsp if (refs) {
2956 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
2958 8b473291 2019-02-21 stsp return err;
2961 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2963 abd2672a 2018-12-23 stsp return err;
2965 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2966 15a94983 2018-12-23 stsp if (err) {
2967 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2968 15a94983 2018-12-23 stsp goto done;
2971 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2972 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2973 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2974 abd2672a 2018-12-23 stsp goto done;
2976 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2977 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2978 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2979 abd2672a 2018-12-23 stsp goto done;
2981 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2982 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
2983 09867e48 2019-08-13 stsp if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2984 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2985 abd2672a 2018-12-23 stsp goto done;
2987 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2988 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2989 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2990 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2991 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2992 abd2672a 2018-12-23 stsp goto done;
2994 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2996 5943eee2 2019-08-13 stsp goto done;
2997 5943eee2 2019-08-13 stsp if (fprintf(outfile, "%s\n", logmsg) < 0) {
2998 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2999 abd2672a 2018-12-23 stsp goto done;
3001 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3003 0208f208 2020-05-05 stsp goto done;
3004 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3005 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3006 0208f208 2020-05-05 stsp fprintf(outfile, "%c %s\n", cp->status, pe->path);
3007 0208f208 2020-05-05 stsp free((char *)pe->path);
3008 0208f208 2020-05-05 stsp free(pe->data);
3010 0208f208 2020-05-05 stsp fputc('\n', outfile);
3012 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3013 abd2672a 2018-12-23 stsp free(id_str);
3014 5943eee2 2019-08-13 stsp free(logmsg);
3015 8b473291 2019-02-21 stsp free(refs_str);
3016 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3017 abd2672a 2018-12-23 stsp return err;
3020 f44b1f58 2020-02-02 tracey const struct got_error *
3021 f44b1f58 2020-02-02 tracey get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
3022 f44b1f58 2020-02-02 tracey FILE *infile)
3024 f44b1f58 2020-02-02 tracey size_t len;
3025 f44b1f58 2020-02-02 tracey char *buf = NULL;
3027 f44b1f58 2020-02-02 tracey size_t noffsets = 0;
3028 f44b1f58 2020-02-02 tracey off_t off = 0;
3030 f44b1f58 2020-02-02 tracey if (line_offsets)
3031 f44b1f58 2020-02-02 tracey *line_offsets = NULL;
3032 f44b1f58 2020-02-02 tracey if (filesize)
3033 f44b1f58 2020-02-02 tracey *filesize = 0;
3034 f44b1f58 2020-02-02 tracey if (nlines)
3035 f44b1f58 2020-02-02 tracey *nlines = 0;
3037 f44b1f58 2020-02-02 tracey if (fseek(infile, 0, SEEK_END) == -1)
3038 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseek");
3039 f44b1f58 2020-02-02 tracey len = ftell(infile) + 1;
3040 f44b1f58 2020-02-02 tracey if (ferror(infile))
3041 f44b1f58 2020-02-02 tracey return got_error_from_errno("ftell");
3042 f44b1f58 2020-02-02 tracey if (fseek(infile, 0, SEEK_SET) == -1)
3043 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseek");
3045 f44b1f58 2020-02-02 tracey if (len == 0)
3046 f44b1f58 2020-02-02 tracey return NULL;
3047 f44b1f58 2020-02-02 tracey if ((buf = calloc(len, sizeof(char *))) == NULL)
3048 f44b1f58 2020-02-02 tracey return got_error_from_errno("calloc");
3050 f44b1f58 2020-02-02 tracey fread(buf, 1, len, infile);
3051 f44b1f58 2020-02-02 tracey if (ferror(infile))
3052 f44b1f58 2020-02-02 tracey return got_error_from_errno("fread");
3055 f44b1f58 2020-02-02 tracey if (line_offsets && nlines) {
3056 f44b1f58 2020-02-02 tracey if (*line_offsets == NULL) {
3057 f44b1f58 2020-02-02 tracey /* Have some data but perhaps no '\n'. */
3058 f44b1f58 2020-02-02 tracey noffsets = 1;
3059 f44b1f58 2020-02-02 tracey *nlines = 1;
3060 f44b1f58 2020-02-02 tracey *line_offsets = calloc(1, sizeof(**line_offsets));
3061 f44b1f58 2020-02-02 tracey if (*line_offsets == NULL)
3062 f44b1f58 2020-02-02 tracey return got_error_from_errno("calloc");
3063 f44b1f58 2020-02-02 tracey /* Skip forward over end of first line. */
3064 f44b1f58 2020-02-02 tracey while (i < len) {
3065 f44b1f58 2020-02-02 tracey if (buf[i] == '\n')
3070 f44b1f58 2020-02-02 tracey /* Scan '\n' offsets in remaining chunk of data. */
3071 f44b1f58 2020-02-02 tracey while (i < len) {
3072 f44b1f58 2020-02-02 tracey if (buf[i] != '\n') {
3074 f44b1f58 2020-02-02 tracey continue;
3076 f44b1f58 2020-02-02 tracey (*nlines)++;
3077 f44b1f58 2020-02-02 tracey if (noffsets < *nlines) {
3078 f44b1f58 2020-02-02 tracey off_t *o = recallocarray(*line_offsets,
3079 f44b1f58 2020-02-02 tracey noffsets, *nlines,
3080 f44b1f58 2020-02-02 tracey sizeof(**line_offsets));
3081 f44b1f58 2020-02-02 tracey if (o == NULL) {
3082 f44b1f58 2020-02-02 tracey free(*line_offsets);
3083 f44b1f58 2020-02-02 tracey *line_offsets = NULL;
3084 f44b1f58 2020-02-02 tracey return got_error_from_errno(
3085 f44b1f58 2020-02-02 tracey "recallocarray");
3087 f44b1f58 2020-02-02 tracey *line_offsets = o;
3088 f44b1f58 2020-02-02 tracey noffsets = *nlines;
3090 f44b1f58 2020-02-02 tracey off = i + 1;
3091 f44b1f58 2020-02-02 tracey (*line_offsets)[*nlines - 1] = off;
3096 f44b1f58 2020-02-02 tracey if (fflush(infile) != 0)
3097 f44b1f58 2020-02-02 tracey return got_error_from_errno("fflush");
3098 f44b1f58 2020-02-02 tracey rewind(infile);
3100 f44b1f58 2020-02-02 tracey if (filesize)
3101 f44b1f58 2020-02-02 tracey *filesize = len;
3103 f44b1f58 2020-02-02 tracey return NULL;
3106 abd2672a 2018-12-23 stsp static const struct got_error *
3107 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
3109 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
3110 48ae06ee 2018-10-18 stsp FILE *f = NULL;
3111 15a94983 2018-12-23 stsp int obj_type;
3113 511a516b 2018-05-19 stsp f = got_opentemp();
3114 48ae06ee 2018-10-18 stsp if (f == NULL) {
3115 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3116 48ae06ee 2018-10-18 stsp goto done;
3118 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
3119 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3120 fb43ecf1 2019-02-11 stsp goto done;
3124 15a94983 2018-12-23 stsp if (s->id1)
3125 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
3127 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
3129 15a94983 2018-12-23 stsp goto done;
3131 15a94983 2018-12-23 stsp switch (obj_type) {
3132 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
3133 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
3134 f44b1f58 2020-02-02 tracey s->diff_context, 0, s->repo, s->f);
3136 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
3137 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
3138 f44b1f58 2020-02-02 tracey s->diff_context, 0, s->repo, s->f);
3140 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
3141 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3142 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
3143 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
3145 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3147 3ffacbe1 2020-02-02 tracey goto done;
3148 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
3149 f44b1f58 2020-02-02 tracey if (s->id1 == NULL) {
3150 0208f208 2020-05-05 stsp err = write_commit_info(s->id2, s->refs, s->repo, s->f);
3151 f44b1f58 2020-02-02 tracey if (err)
3152 f44b1f58 2020-02-02 tracey goto done;
3153 f44b1f58 2020-02-02 tracey } else {
3154 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
3155 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3156 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
3157 f44b1f58 2020-02-02 tracey err = write_commit_info(s->id2, s->refs,
3158 f44b1f58 2020-02-02 tracey s->repo, s->f);
3159 f44b1f58 2020-02-02 tracey if (err)
3160 f44b1f58 2020-02-02 tracey goto done;
3165 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
3167 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
3168 f44b1f58 2020-02-02 tracey s->diff_context, 0, s->repo, s->f);
3172 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3175 f44b1f58 2020-02-02 tracey if (err)
3176 f44b1f58 2020-02-02 tracey goto done;
3177 f44b1f58 2020-02-02 tracey err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3180 f44b1f58 2020-02-02 tracey if (s->f && fflush(s->f) != 0 && err == NULL)
3181 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3182 48ae06ee 2018-10-18 stsp return err;
3185 f5215bb9 2019-02-22 stsp static void
3186 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
3188 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
3189 f5215bb9 2019-02-22 stsp update_panels();
3190 f5215bb9 2019-02-22 stsp doupdate();
3193 f44b1f58 2020-02-02 tracey static const struct got_error *
3194 f44b1f58 2020-02-02 tracey search_start_diff_view(struct tog_view *view)
3196 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3198 f44b1f58 2020-02-02 tracey s->matched_line = 0;
3199 f44b1f58 2020-02-02 tracey return NULL;
3202 f44b1f58 2020-02-02 tracey static const struct got_error *
3203 f44b1f58 2020-02-02 tracey search_next_diff_view(struct tog_view *view)
3205 f44b1f58 2020-02-02 tracey struct tog_diff_view_state *s = &view->state.diff;
3206 f44b1f58 2020-02-02 tracey int lineno;
3208 f44b1f58 2020-02-02 tracey if (!view->searching) {
3209 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3210 f44b1f58 2020-02-02 tracey return NULL;
3213 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3214 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3215 f44b1f58 2020-02-02 tracey lineno = s->matched_line + 1;
3217 f44b1f58 2020-02-02 tracey lineno = s->matched_line - 1;
3218 f44b1f58 2020-02-02 tracey } else {
3219 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3220 f44b1f58 2020-02-02 tracey lineno = 1;
3222 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3225 f44b1f58 2020-02-02 tracey while (1) {
3226 f44b1f58 2020-02-02 tracey char *line = NULL;
3227 f44b1f58 2020-02-02 tracey off_t offset;
3228 f44b1f58 2020-02-02 tracey size_t len;
3230 f44b1f58 2020-02-02 tracey if (lineno <= 0 || lineno > s->nlines) {
3231 f44b1f58 2020-02-02 tracey if (s->matched_line == 0) {
3232 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3233 f44b1f58 2020-02-02 tracey free(line);
3237 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3238 f44b1f58 2020-02-02 tracey lineno = 1;
3240 f44b1f58 2020-02-02 tracey lineno = s->nlines;
3243 f44b1f58 2020-02-02 tracey offset = s->line_offsets[lineno - 1];
3244 f44b1f58 2020-02-02 tracey if (fseeko(s->f, offset, SEEK_SET) != 0) {
3245 f44b1f58 2020-02-02 tracey free(line);
3246 f44b1f58 2020-02-02 tracey return got_error_from_errno("fseeko");
3248 f44b1f58 2020-02-02 tracey free(line);
3249 f44b1f58 2020-02-02 tracey line = parse_next_line(s->f, &len);
3250 f44b1f58 2020-02-02 tracey if (line && match_line(line, &view->regex)) {
3251 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
3252 f44b1f58 2020-02-02 tracey s->matched_line = lineno;
3253 f44b1f58 2020-02-02 tracey free(line);
3256 f44b1f58 2020-02-02 tracey free(line);
3257 f44b1f58 2020-02-02 tracey if (view->searching == TOG_SEARCH_FORWARD)
3258 f44b1f58 2020-02-02 tracey lineno++;
3260 f44b1f58 2020-02-02 tracey lineno--;
3263 f44b1f58 2020-02-02 tracey if (s->matched_line) {
3264 f44b1f58 2020-02-02 tracey s->first_displayed_line = s->matched_line;
3265 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3268 f44b1f58 2020-02-02 tracey return NULL;
3271 48ae06ee 2018-10-18 stsp static const struct got_error *
3272 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
3273 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
3274 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
3276 48ae06ee 2018-10-18 stsp const struct got_error *err;
3277 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3279 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
3280 15a94983 2018-12-23 stsp int type1, type2;
3281 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
3283 15a94983 2018-12-23 stsp return err;
3284 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
3286 15a94983 2018-12-23 stsp return err;
3288 15a94983 2018-12-23 stsp if (type1 != type2)
3289 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3291 f44b1f58 2020-02-02 tracey s->first_displayed_line = 1;
3292 f44b1f58 2020-02-02 tracey s->last_displayed_line = view->nlines;
3293 f44b1f58 2020-02-02 tracey s->selected_line = 1;
3294 f44b1f58 2020-02-02 tracey s->repo = repo;
3295 f44b1f58 2020-02-02 tracey s->refs = refs;
3296 f44b1f58 2020-02-02 tracey s->id1 = id1;
3297 f44b1f58 2020-02-02 tracey s->id2 = id2;
3299 15a94983 2018-12-23 stsp if (id1) {
3300 5465d566 2020-02-01 tracey s->id1 = got_object_id_dup(id1);
3301 5465d566 2020-02-01 tracey if (s->id1 == NULL)
3302 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3304 5465d566 2020-02-01 tracey s->id1 = NULL;
3306 5465d566 2020-02-01 tracey s->id2 = got_object_id_dup(id2);
3307 5465d566 2020-02-01 tracey if (s->id2 == NULL) {
3308 5465d566 2020-02-01 tracey free(s->id1);
3309 5465d566 2020-02-01 tracey s->id1 = NULL;
3310 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3312 5465d566 2020-02-01 tracey s->f = NULL;
3313 5465d566 2020-02-01 tracey s->first_displayed_line = 1;
3314 5465d566 2020-02-01 tracey s->last_displayed_line = view->nlines;
3315 5465d566 2020-02-01 tracey s->diff_context = 3;
3316 5465d566 2020-02-01 tracey s->log_view = log_view;
3317 5465d566 2020-02-01 tracey s->repo = repo;
3318 5465d566 2020-02-01 tracey s->refs = refs;
3320 f44b1f58 2020-02-02 tracey SIMPLEQ_INIT(&s->colors);
3321 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3322 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3323 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
3324 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
3326 6d17833f 2019-11-08 stsp return err;
3327 5465d566 2020-02-01 tracey err = add_color(&s->colors, "^\\+",
3328 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3329 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3330 6d17833f 2019-11-08 stsp if (err) {
3331 5465d566 2020-02-01 tracey free_colors(&s->colors);
3332 6d17833f 2019-11-08 stsp return err;
3334 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3335 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3336 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3337 6d17833f 2019-11-08 stsp if (err) {
3338 5465d566 2020-02-01 tracey free_colors(&s->colors);
3339 6d17833f 2019-11-08 stsp return err;
3342 f44b1f58 2020-02-02 tracey err = add_color(&s->colors,
3343 0208f208 2020-05-05 stsp "^(commit|(blob|file) [-+] |[MDmA] [^ ])",
3344 0208f208 2020-05-05 stsp TOG_COLOR_DIFF_META,
3345 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3346 6d17833f 2019-11-08 stsp if (err) {
3347 5465d566 2020-02-01 tracey free_colors(&s->colors);
3348 6d17833f 2019-11-08 stsp return err;
3351 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3352 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3353 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3354 11b20872 2019-11-08 stsp if (err) {
3355 5465d566 2020-02-01 tracey free_colors(&s->colors);
3356 11b20872 2019-11-08 stsp return err;
3359 5465d566 2020-02-01 tracey err = add_color(&s->colors,
3360 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3361 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3362 11b20872 2019-11-08 stsp if (err) {
3363 5465d566 2020-02-01 tracey free_colors(&s->colors);
3364 11b20872 2019-11-08 stsp return err;
3368 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3369 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3370 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3372 5465d566 2020-02-01 tracey err = create_diff(s);
3373 48ae06ee 2018-10-18 stsp if (err) {
3374 5465d566 2020-02-01 tracey free(s->id1);
3375 5465d566 2020-02-01 tracey s->id1 = NULL;
3376 5465d566 2020-02-01 tracey free(s->id2);
3377 5465d566 2020-02-01 tracey s->id2 = NULL;
3378 48ae06ee 2018-10-18 stsp return err;
3381 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3382 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3383 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3384 f44b1f58 2020-02-02 tracey view->search_start = search_start_diff_view;
3385 f44b1f58 2020-02-02 tracey view->search_next = search_next_diff_view;
3387 5dc9f4bc 2018-08-04 stsp return NULL;
3390 e5a0f69f 2018-08-18 stsp static const struct got_error *
3391 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3393 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3394 5465d566 2020-02-01 tracey struct tog_diff_view_state *s = &view->state.diff;
3396 5465d566 2020-02-01 tracey free(s->id1);
3397 5465d566 2020-02-01 tracey s->id1 = NULL;
3398 5465d566 2020-02-01 tracey free(s->id2);
3399 5465d566 2020-02-01 tracey s->id2 = NULL;
3400 5465d566 2020-02-01 tracey if (s->f && fclose(s->f) == EOF)
3401 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3402 5465d566 2020-02-01 tracey free_colors(&s->colors);
3403 f44b1f58 2020-02-02 tracey free(s->line_offsets);
3404 e5a0f69f 2018-08-18 stsp return err;
3407 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3408 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3410 a3404814 2018-09-02 stsp const struct got_error *err;
3411 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3412 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3414 a3404814 2018-09-02 stsp if (s->id1) {
3415 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3417 a3404814 2018-09-02 stsp return err;
3419 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3421 a3404814 2018-09-02 stsp return err;
3423 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
3424 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3425 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3426 a3404814 2018-09-02 stsp free(id_str1);
3427 a3404814 2018-09-02 stsp free(id_str2);
3428 a3404814 2018-09-02 stsp return err;
3430 a3404814 2018-09-02 stsp free(id_str1);
3431 a3404814 2018-09-02 stsp free(id_str2);
3433 f44b1f58 2020-02-02 tracey return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3434 f44b1f58 2020-02-02 tracey s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3435 bddb1296 2019-11-08 stsp header, &s->colors);
3438 15a087fe 2019-02-21 stsp static const struct got_error *
3439 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3440 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3442 d7a04538 2019-02-21 stsp const struct got_error *err;
3443 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3444 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3445 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3447 15a087fe 2019-02-21 stsp free(s->id2);
3448 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3449 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3450 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3452 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3454 d7a04538 2019-02-21 stsp return err;
3455 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3456 15a087fe 2019-02-21 stsp free(s->id1);
3457 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3458 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3459 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3460 15a087fe 2019-02-21 stsp return NULL;
3463 0cf4efb1 2018-09-29 stsp static const struct got_error *
3464 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3465 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3467 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3468 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3469 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3470 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
3473 e5a0f69f 2018-08-18 stsp switch (ch) {
3475 1e37a5c2 2019-05-12 jcs case KEY_UP:
3476 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3477 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3479 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3480 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3481 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3484 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3485 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3486 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3489 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3490 1e37a5c2 2019-05-12 jcs if (!s->eof)
3491 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3493 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3494 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3496 00ba99a7 2019-05-12 jcs if (s->eof)
3499 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3500 1e37a5c2 2019-05-12 jcs char *line;
3501 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
3502 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3503 1e37a5c2 2019-05-12 jcs if (line == NULL)
3508 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3509 1e37a5c2 2019-05-12 jcs s->diff_context--;
3510 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3511 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3515 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3516 1e37a5c2 2019-05-12 jcs s->diff_context++;
3517 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3518 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3523 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3525 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3526 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
3527 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
3528 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3531 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3536 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3540 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3541 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3543 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3544 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3548 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3550 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3552 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3553 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
3554 7c1452c1 2020-03-26 stsp err = trigger_log_thread(s->log_view, 1,
3555 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
3556 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
3557 7c1452c1 2020-03-26 stsp &ls->thread_args.need_commits,
3558 7c1452c1 2020-03-26 stsp &ls->thread_args.commit_loaded);
3562 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3567 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
3568 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3571 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3575 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3576 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3578 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3579 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3585 bcbd79e2 2018-08-19 stsp return err;
3588 4ed7e80c 2018-05-20 stsp static const struct got_error *
3589 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3591 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3592 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3593 a273ac94 2020-02-23 naddy struct got_worktree *worktree = NULL;
3594 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3595 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3596 a273ac94 2020-02-23 naddy char *repo_path = NULL, *cwd = NULL;
3597 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3599 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3601 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3603 26ed57b2 2018-05-19 stsp #ifndef PROFILE
3604 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3605 eb6600df 2019-01-04 stsp NULL) == -1)
3606 26ed57b2 2018-05-19 stsp err(1, "pledge");
3609 09b5bff8 2020-02-23 naddy while ((ch = getopt(argc, argv, "r:")) != -1) {
3610 26ed57b2 2018-05-19 stsp switch (ch) {
3611 09b5bff8 2020-02-23 naddy case 'r':
3612 09b5bff8 2020-02-23 naddy repo_path = realpath(optarg, NULL);
3613 09b5bff8 2020-02-23 naddy if (repo_path == NULL)
3614 09b5bff8 2020-02-23 naddy return got_error_from_errno2("realpath",
3618 17020d27 2019-03-07 stsp usage_diff();
3619 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3623 26ed57b2 2018-05-19 stsp argc -= optind;
3624 26ed57b2 2018-05-19 stsp argv += optind;
3626 26ed57b2 2018-05-19 stsp if (argc == 0) {
3627 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3628 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3629 15a94983 2018-12-23 stsp id_str1 = argv[0];
3630 15a94983 2018-12-23 stsp id_str2 = argv[1];
3632 26ed57b2 2018-05-19 stsp usage_diff();
3634 a273ac94 2020-02-23 naddy cwd = getcwd(NULL, 0);
3635 a273ac94 2020-02-23 naddy if (cwd == NULL)
3636 a273ac94 2020-02-23 naddy return got_error_from_errno("getcwd");
3638 a273ac94 2020-02-23 naddy error = got_worktree_open(&worktree, cwd);
3639 a273ac94 2020-02-23 naddy if (error && error->code != GOT_ERR_NOT_WORKTREE)
3640 a273ac94 2020-02-23 naddy goto done;
3642 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3643 a273ac94 2020-02-23 naddy if (worktree)
3644 a273ac94 2020-02-23 naddy repo_path =
3645 a273ac94 2020-02-23 naddy strdup(got_worktree_get_repo_path(worktree));
3647 a273ac94 2020-02-23 naddy repo_path = strdup(cwd);
3649 a273ac94 2020-02-23 naddy if (repo_path == NULL) {
3650 a273ac94 2020-02-23 naddy error = got_error_from_errno("strdup");
3651 a273ac94 2020-02-23 naddy goto done;
3654 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3655 eb6600df 2019-01-04 stsp if (error)
3656 eb6600df 2019-01-04 stsp goto done;
3658 a273ac94 2020-02-23 naddy init_curses();
3660 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3661 26ed57b2 2018-05-19 stsp if (error)
3662 26ed57b2 2018-05-19 stsp goto done;
3664 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
3665 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3666 26ed57b2 2018-05-19 stsp if (error)
3667 26ed57b2 2018-05-19 stsp goto done;
3669 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
3670 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3671 26ed57b2 2018-05-19 stsp if (error)
3672 26ed57b2 2018-05-19 stsp goto done;
3674 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3675 8b473291 2019-02-21 stsp if (error)
3676 8b473291 2019-02-21 stsp goto done;
3678 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3679 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3680 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3681 ea5e7bb5 2018-08-01 stsp goto done;
3683 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3684 5dc9f4bc 2018-08-04 stsp if (error)
3685 5dc9f4bc 2018-08-04 stsp goto done;
3686 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3688 c02c541e 2019-03-29 stsp free(repo_path);
3689 a273ac94 2020-02-23 naddy free(cwd);
3691 921be706 2019-06-28 stsp got_repo_close(repo);
3692 a273ac94 2020-02-23 naddy if (worktree)
3693 a273ac94 2020-02-23 naddy got_worktree_close(worktree);
3694 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3695 26ed57b2 2018-05-19 stsp return error;
3698 4ed7e80c 2018-05-20 stsp __dead static void
3699 9f7d7167 2018-04-29 stsp usage_blame(void)
3702 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3703 9f7d7167 2018-04-29 stsp getprogname());
3707 84451b3e 2018-07-10 stsp struct tog_blame_line {
3708 84451b3e 2018-07-10 stsp int annotated;
3709 84451b3e 2018-07-10 stsp struct got_object_id *id;
3712 4ed7e80c 2018-05-20 stsp static const struct got_error *
3713 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3714 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
3715 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
3716 11b20872 2019-11-08 stsp int *last_displayed_line, int *eof, int max_lines,
3717 11b20872 2019-11-08 stsp struct tog_colors *colors)
3719 84451b3e 2018-07-10 stsp const struct got_error *err;
3720 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3721 84451b3e 2018-07-10 stsp char *line;
3722 84451b3e 2018-07-10 stsp size_t len;
3723 84451b3e 2018-07-10 stsp wchar_t *wline;
3724 27a741e5 2019-09-11 stsp int width;
3725 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3726 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3727 ab089a2a 2018-07-12 stsp char *id_str;
3728 11b20872 2019-11-08 stsp struct tog_color *tc;
3730 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
3732 ab089a2a 2018-07-12 stsp return err;
3734 84451b3e 2018-07-10 stsp rewind(f);
3735 f7d12f7e 2018-08-01 stsp werase(view->window);
3737 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3738 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3739 ab089a2a 2018-07-12 stsp free(id_str);
3740 ab089a2a 2018-07-12 stsp return err;
3743 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3744 ab089a2a 2018-07-12 stsp free(line);
3745 2550e4c3 2018-07-13 stsp line = NULL;
3747 1cae65b4 2019-09-22 stsp return err;
3748 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3749 a3404814 2018-09-02 stsp wstandout(view->window);
3750 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3752 11b20872 2019-11-08 stsp wattr_on(view->window,
3753 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3754 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3756 11b20872 2019-11-08 stsp wattr_off(view->window,
3757 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3758 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3759 a3404814 2018-09-02 stsp wstandend(view->window);
3760 2550e4c3 2018-07-13 stsp free(wline);
3761 2550e4c3 2018-07-13 stsp wline = NULL;
3762 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3763 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3765 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3766 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
3767 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
3768 ab089a2a 2018-07-12 stsp free(id_str);
3769 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3771 ab089a2a 2018-07-12 stsp free(id_str);
3772 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3773 3f60a8ef 2018-07-10 stsp free(line);
3774 2550e4c3 2018-07-13 stsp line = NULL;
3776 3f60a8ef 2018-07-10 stsp return err;
3777 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3778 2550e4c3 2018-07-13 stsp free(wline);
3779 2550e4c3 2018-07-13 stsp wline = NULL;
3780 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3781 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3784 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
3785 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
3786 84451b3e 2018-07-10 stsp if (line == NULL) {
3790 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
3791 84451b3e 2018-07-10 stsp free(line);
3795 27a741e5 2019-09-11 stsp if (view->ncols <= 9) {
3796 27a741e5 2019-09-11 stsp width = 9;
3797 27a741e5 2019-09-11 stsp wline = wcsdup(L"");
3798 27a741e5 2019-09-11 stsp if (wline == NULL)
3799 27a741e5 2019-09-11 stsp err = got_error_from_errno("wcsdup");
3801 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line,
3802 27a741e5 2019-09-11 stsp view->ncols - 9, 9);
3803 27a741e5 2019-09-11 stsp width += 9;
3805 84451b3e 2018-07-10 stsp if (err) {
3806 84451b3e 2018-07-10 stsp free(line);
3807 84451b3e 2018-07-10 stsp return err;
3810 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3811 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3813 8d0fe45a 2019-08-12 stsp if (nlines > 0) {
3814 8d0fe45a 2019-08-12 stsp blame_line = &lines[lineno - 1];
3815 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3816 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3817 27a741e5 2019-09-11 stsp !(view->focussed &&
3818 27a741e5 2019-09-11 stsp nprinted == selected_line - 1)) {
3819 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3820 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3821 8d0fe45a 2019-08-12 stsp char *id_str;
3822 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3823 8d0fe45a 2019-08-12 stsp if (err) {
3824 8d0fe45a 2019-08-12 stsp free(line);
3825 8d0fe45a 2019-08-12 stsp free(wline);
3826 8d0fe45a 2019-08-12 stsp return err;
3828 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3830 11b20872 2019-11-08 stsp wattr_on(view->window,
3831 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3832 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3834 11b20872 2019-11-08 stsp wattr_off(view->window,
3835 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3836 8d0fe45a 2019-08-12 stsp free(id_str);
3837 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3839 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3840 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3843 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3844 ee41ec32 2018-07-10 stsp prev_id = NULL;
3847 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3848 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3849 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3851 27a741e5 2019-09-11 stsp waddwstr(view->window, wline);
3852 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
3853 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
3854 84451b3e 2018-07-10 stsp if (++nprinted == 1)
3855 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
3856 84451b3e 2018-07-10 stsp free(line);
3857 2550e4c3 2018-07-13 stsp free(wline);
3858 2550e4c3 2018-07-13 stsp wline = NULL;
3860 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
3862 1a57306a 2018-09-02 stsp view_vborder(view);
3864 84451b3e 2018-07-10 stsp return NULL;
3867 84451b3e 2018-07-10 stsp static const struct got_error *
3868 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3870 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
3871 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
3872 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
3873 1a76625f 2018-10-22 stsp int errcode;
3875 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
3876 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3877 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3879 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3880 1a76625f 2018-10-22 stsp if (errcode)
3881 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3883 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3884 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3885 d68a0a7d 2018-07-10 stsp goto done;
3888 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3889 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3891 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3892 d68a0a7d 2018-07-10 stsp if (line->annotated)
3893 d68a0a7d 2018-07-10 stsp goto done;
3895 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3896 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3897 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3898 84451b3e 2018-07-10 stsp goto done;
3900 84451b3e 2018-07-10 stsp line->annotated = 1;
3902 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3903 1a76625f 2018-10-22 stsp if (errcode)
3904 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3905 84451b3e 2018-07-10 stsp return err;
3908 84451b3e 2018-07-10 stsp static void *
3909 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3911 18430de3 2018-07-10 stsp const struct got_error *err;
3912 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3913 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3914 1a76625f 2018-10-22 stsp int errcode;
3916 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
3918 61266923 2020-01-14 stsp return (void *)err;
3920 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
3921 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3922 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
3923 fc06ba56 2019-08-22 stsp err = NULL;
3925 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3926 1a76625f 2018-10-22 stsp if (errcode)
3927 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3928 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3930 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3931 c9beca56 2018-07-22 stsp ta->repo = NULL;
3932 c9beca56 2018-07-22 stsp *ta->complete = 1;
3934 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3935 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3936 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3938 18430de3 2018-07-10 stsp return (void *)err;
3941 245d91c1 2018-07-12 stsp static struct got_object_id *
3942 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3943 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
3945 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3947 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
3948 8d0fe45a 2019-08-12 stsp return NULL;
3950 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3951 245d91c1 2018-07-12 stsp if (!line->annotated)
3952 245d91c1 2018-07-12 stsp return NULL;
3954 245d91c1 2018-07-12 stsp return line->id;
3957 b880a918 2018-07-10 stsp static const struct got_error *
3958 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3960 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3963 245d91c1 2018-07-12 stsp if (blame->thread) {
3964 1a76625f 2018-10-22 stsp int errcode;
3965 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3966 1a76625f 2018-10-22 stsp if (errcode)
3967 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3968 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3969 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3970 1a76625f 2018-10-22 stsp if (errcode)
3971 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3972 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3973 1a76625f 2018-10-22 stsp if (errcode)
3974 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3975 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3976 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3977 245d91c1 2018-07-12 stsp err = NULL;
3978 245d91c1 2018-07-12 stsp blame->thread = NULL;
3980 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3981 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3982 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3984 245d91c1 2018-07-12 stsp if (blame->f) {
3985 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3986 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3987 245d91c1 2018-07-12 stsp blame->f = NULL;
3989 57670559 2018-12-23 stsp if (blame->lines) {
3990 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3991 57670559 2018-12-23 stsp free(blame->lines[i].id);
3992 57670559 2018-12-23 stsp free(blame->lines);
3993 57670559 2018-12-23 stsp blame->lines = NULL;
3995 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3996 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3998 245d91c1 2018-07-12 stsp return err;
4001 245d91c1 2018-07-12 stsp static const struct got_error *
4002 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
4004 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
4005 fc06ba56 2019-08-22 stsp int *done = arg;
4006 fc06ba56 2019-08-22 stsp int errcode;
4008 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
4009 fc06ba56 2019-08-22 stsp if (errcode)
4010 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4011 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
4013 fc06ba56 2019-08-22 stsp if (*done)
4014 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
4016 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
4017 fc06ba56 2019-08-22 stsp if (errcode)
4018 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
4019 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
4021 fc06ba56 2019-08-22 stsp return err;
4024 fc06ba56 2019-08-22 stsp static const struct got_error *
4025 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
4026 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
4027 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
4028 245d91c1 2018-07-12 stsp struct got_repository *repo)
4030 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
4031 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
4032 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
4033 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
4034 15a94983 2018-12-23 stsp int obj_type;
4036 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
4038 15a94983 2018-12-23 stsp return err;
4040 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
4042 84451b3e 2018-07-10 stsp goto done;
4044 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4045 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
4046 84451b3e 2018-07-10 stsp goto done;
4049 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4051 a70480e0 2018-06-23 stsp goto done;
4052 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
4053 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
4054 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
4055 84451b3e 2018-07-10 stsp goto done;
4057 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4058 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
4059 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
4060 84451b3e 2018-07-10 stsp goto done;
4062 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4063 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4064 b02560ec 2019-08-19 stsp blame->nlines--;
4066 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4067 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
4068 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
4069 84451b3e 2018-07-10 stsp goto done;
4072 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4074 bd24772e 2018-07-11 stsp goto done;
4076 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
4077 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
4078 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
4079 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
4080 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
4081 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4082 245d91c1 2018-07-12 stsp goto done;
4084 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
4086 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
4087 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
4088 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
4089 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
4090 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
4091 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_arg = done;
4092 245d91c1 2018-07-12 stsp *blame_complete = 0;
4096 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
4097 27d434c2 2018-09-15 stsp free(obj_id);
4099 245d91c1 2018-07-12 stsp stop_blame(blame);
4100 245d91c1 2018-07-12 stsp return err;
4103 245d91c1 2018-07-12 stsp static const struct got_error *
4104 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
4105 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4106 8b473291 2019-02-21 stsp struct got_repository *repo)
4108 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
4109 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4111 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
4113 c4843652 2019-08-12 stsp s->path = strdup(path);
4114 c4843652 2019-08-12 stsp if (s->path == NULL)
4115 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
4117 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4118 c4843652 2019-08-12 stsp if (err) {
4119 c4843652 2019-08-12 stsp free(s->path);
4120 7cbe629d 2018-08-04 stsp return err;
4123 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4124 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
4125 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
4126 fb2756b9 2018-08-04 stsp s->selected_line = 1;
4127 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
4128 fb2756b9 2018-08-04 stsp s->repo = repo;
4129 8b473291 2019-02-21 stsp s->refs = refs;
4130 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
4131 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
4133 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4134 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4135 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4136 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4138 11b20872 2019-11-08 stsp return err;
4141 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
4142 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
4143 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
4144 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
4145 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
4147 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
4148 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
4149 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
4150 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
4153 e5a0f69f 2018-08-18 stsp static const struct got_error *
4154 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
4156 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4157 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4159 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
4160 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
4162 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4163 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
4164 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4165 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4166 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
4169 e5a0f69f 2018-08-18 stsp free(s->path);
4170 11b20872 2019-11-08 stsp free_colors(&s->colors);
4172 e5a0f69f 2018-08-18 stsp return err;
4175 7cbe629d 2018-08-04 stsp static const struct got_error *
4176 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
4178 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4180 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
4181 6c4c42e0 2019-06-24 stsp return NULL;
4184 6c4c42e0 2019-06-24 stsp static const struct got_error *
4185 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
4187 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
4188 6c4c42e0 2019-06-24 stsp int lineno;
4190 6c4c42e0 2019-06-24 stsp if (!view->searching) {
4191 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4192 6c4c42e0 2019-06-24 stsp return NULL;
4195 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4196 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4197 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
4199 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
4201 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4202 6c4c42e0 2019-06-24 stsp lineno = 1;
4204 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4207 6c4c42e0 2019-06-24 stsp while (1) {
4208 6c4c42e0 2019-06-24 stsp char *line = NULL;
4209 6c4c42e0 2019-06-24 stsp off_t offset;
4210 6c4c42e0 2019-06-24 stsp size_t len;
4212 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
4213 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
4214 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4215 6c4c42e0 2019-06-24 stsp free(line);
4219 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4220 6c4c42e0 2019-06-24 stsp lineno = 1;
4222 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
4225 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
4226 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4227 6c4c42e0 2019-06-24 stsp free(line);
4228 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
4230 6c4c42e0 2019-06-24 stsp free(line);
4231 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
4232 2246482e 2019-06-25 stsp if (line && match_line(line, &view->regex)) {
4233 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
4234 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
4235 6c4c42e0 2019-06-24 stsp free(line);
4238 6c4c42e0 2019-06-24 stsp free(line);
4239 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
4245 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
4246 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
4247 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
4250 6c4c42e0 2019-06-24 stsp return NULL;
4253 6c4c42e0 2019-06-24 stsp static const struct got_error *
4254 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
4256 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4257 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
4258 2b380cc8 2018-10-24 stsp int errcode;
4260 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
4261 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4262 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
4263 2b380cc8 2018-10-24 stsp if (errcode)
4264 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
4266 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
4269 51fe7530 2019-08-19 stsp if (s->blame_complete)
4270 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
4272 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4273 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4274 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
4275 11b20872 2019-11-08 stsp &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4277 669b5ffa 2018-10-07 stsp view_vborder(view);
4278 e5a0f69f 2018-08-18 stsp return err;
4281 e5a0f69f 2018-08-18 stsp static const struct got_error *
4282 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4283 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4285 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
4286 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
4287 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
4288 669b5ffa 2018-10-07 stsp int begin_x = 0;
4290 e5a0f69f 2018-08-18 stsp switch (ch) {
4292 1e37a5c2 2019-05-12 jcs s->done = 1;
4295 1e37a5c2 2019-05-12 jcs case KEY_UP:
4296 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
4297 1e37a5c2 2019-05-12 jcs s->selected_line--;
4298 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
4299 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
4300 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
4302 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4303 ea025d1d 2020-02-22 naddy case CTRL('b'):
4304 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
4305 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
4308 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
4309 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
4310 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
4312 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
4315 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4316 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
4317 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
4318 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
4319 1e37a5c2 2019-05-12 jcs s->selected_line++;
4320 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
4321 1e37a5c2 2019-05-12 jcs s->blame.nlines)
4322 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
4325 1e37a5c2 2019-05-12 jcs case 'p': {
4326 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4327 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4328 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4329 1e37a5c2 2019-05-12 jcs if (id == NULL)
4331 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
4332 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
4333 15a94983 2018-12-23 stsp struct got_object_qid *pid;
4334 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
4335 1e37a5c2 2019-05-12 jcs int obj_type;
4336 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
4337 1e37a5c2 2019-05-12 jcs s->repo, id);
4340 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
4341 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
4342 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
4343 15a94983 2018-12-23 stsp got_object_commit_close(commit);
4346 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
4347 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
4348 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4349 e5a0f69f 2018-08-18 stsp if (err) {
4350 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4351 1e37a5c2 2019-05-12 jcs err = NULL;
4352 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4355 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4357 1e37a5c2 2019-05-12 jcs free(blob_id);
4358 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4359 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4360 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4363 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4365 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4367 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4368 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4370 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4375 1e37a5c2 2019-05-12 jcs s->done = 1;
4376 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4377 1e37a5c2 2019-05-12 jcs s->done = 0;
4378 1e37a5c2 2019-05-12 jcs if (thread_err)
4380 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4381 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4382 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4383 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4384 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
4385 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
4390 1e37a5c2 2019-05-12 jcs case 'B': {
4391 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4392 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
4393 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4395 1e37a5c2 2019-05-12 jcs s->done = 1;
4396 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4397 1e37a5c2 2019-05-12 jcs s->done = 0;
4398 1e37a5c2 2019-05-12 jcs if (thread_err)
4400 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4401 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4402 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4403 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4404 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4405 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4406 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
4407 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
4412 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4413 1e37a5c2 2019-05-12 jcs case '\r': {
4414 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4415 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4416 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4417 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4418 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4419 1e37a5c2 2019-05-12 jcs if (id == NULL)
4421 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4424 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4425 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4426 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4427 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4428 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4429 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4430 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4431 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4434 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4435 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
4436 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4438 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4441 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4442 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4445 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
4447 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4450 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
4451 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4453 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4458 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4459 ea025d1d 2020-02-22 naddy case CTRL('f'):
4461 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4462 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4463 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4466 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4467 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4468 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4469 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4472 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4473 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4474 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4475 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4477 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4478 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4479 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4481 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4482 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4483 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4484 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4490 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4493 a70480e0 2018-06-23 stsp static const struct got_error *
4494 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4496 a70480e0 2018-06-23 stsp const struct got_error *error;
4497 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4498 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4499 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4500 f135c941 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4501 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4502 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4504 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4506 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4508 a70480e0 2018-06-23 stsp #ifndef PROFILE
4509 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4510 8e94dd5b 2019-01-04 stsp NULL) == -1)
4511 a70480e0 2018-06-23 stsp err(1, "pledge");
4514 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4515 a70480e0 2018-06-23 stsp switch (ch) {
4517 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4520 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4521 69069811 2018-08-02 stsp if (repo_path == NULL)
4522 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4526 17020d27 2019-03-07 stsp usage_blame();
4527 a70480e0 2018-06-23 stsp /* NOTREACHED */
4531 a70480e0 2018-06-23 stsp argc -= optind;
4532 a70480e0 2018-06-23 stsp argv += optind;
4534 f135c941 2020-02-20 stsp if (argc != 1)
4535 a70480e0 2018-06-23 stsp usage_blame();
4537 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
4538 6962eb72 2020-02-20 stsp if (cwd == NULL)
4539 6962eb72 2020-02-20 stsp return got_error_from_errno("getcwd");
4541 f135c941 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
4542 f135c941 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4543 f135c941 2020-02-20 stsp goto done;
4545 69069811 2018-08-02 stsp if (repo_path == NULL) {
4546 f135c941 2020-02-20 stsp if (worktree)
4547 eb41ed75 2019-02-05 stsp repo_path =
4548 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4550 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4552 f135c941 2020-02-20 stsp if (repo_path == NULL) {
4553 f135c941 2020-02-20 stsp error = got_error_from_errno("strdup");
4554 f135c941 2020-02-20 stsp goto done;
4557 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4558 c02c541e 2019-03-29 stsp if (error != NULL)
4559 8e94dd5b 2019-01-04 stsp goto done;
4561 f135c941 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4562 f135c941 2020-02-20 stsp worktree);
4563 c02c541e 2019-03-29 stsp if (error)
4564 92205607 2019-01-04 stsp goto done;
4566 f135c941 2020-02-20 stsp init_curses();
4568 f135c941 2020-02-20 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4569 eb41ed75 2019-02-05 stsp if (error)
4570 69069811 2018-08-02 stsp goto done;
4572 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4573 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4574 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4575 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4576 a70480e0 2018-06-23 stsp if (error != NULL)
4577 66b4983c 2018-06-23 stsp goto done;
4578 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4579 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4581 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4582 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4584 a19e88aa 2018-06-23 stsp if (error != NULL)
4585 8b473291 2019-02-21 stsp goto done;
4587 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4588 8b473291 2019-02-21 stsp if (error)
4589 a19e88aa 2018-06-23 stsp goto done;
4591 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4592 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4593 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4594 e1cd8fed 2018-08-01 stsp goto done;
4596 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4597 7cbe629d 2018-08-04 stsp if (error)
4598 7cbe629d 2018-08-04 stsp goto done;
4599 12314ad4 2019-08-31 stsp if (worktree) {
4600 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4601 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4602 12314ad4 2019-08-31 stsp worktree = NULL;
4604 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4606 69069811 2018-08-02 stsp free(repo_path);
4607 f135c941 2020-02-20 stsp free(in_repo_path);
4608 69069811 2018-08-02 stsp free(cwd);
4609 a70480e0 2018-06-23 stsp free(commit_id);
4610 eb41ed75 2019-02-05 stsp if (worktree)
4611 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4613 a70480e0 2018-06-23 stsp got_repo_close(repo);
4614 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4615 a70480e0 2018-06-23 stsp return error;
4618 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4619 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
4620 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
4621 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
4622 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
4623 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
4624 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int selected, int limit,
4625 f26dddb7 2019-11-08 stsp int isroot, struct tog_colors *colors)
4627 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4628 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4629 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4630 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4631 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4633 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
4635 f7d12f7e 2018-08-01 stsp werase(view->window);
4637 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4638 ffd1d5e5 2018-06-23 stsp return NULL;
4640 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, label, view->ncols, 0);
4642 ffd1d5e5 2018-06-23 stsp return err;
4643 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4644 a3404814 2018-09-02 stsp wstandout(view->window);
4645 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
4647 11b20872 2019-11-08 stsp wattr_on(view->window,
4648 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4649 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4651 11b20872 2019-11-08 stsp wattr_off(view->window,
4652 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4653 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4654 a3404814 2018-09-02 stsp wstandend(view->window);
4655 2550e4c3 2018-07-13 stsp free(wline);
4656 2550e4c3 2018-07-13 stsp wline = NULL;
4657 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4658 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4659 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4660 ffd1d5e5 2018-06-23 stsp return NULL;
4661 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4663 ce52c690 2018-06-23 stsp return err;
4664 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4665 2550e4c3 2018-07-13 stsp free(wline);
4666 2550e4c3 2018-07-13 stsp wline = NULL;
4667 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4668 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4669 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4670 ffd1d5e5 2018-06-23 stsp return NULL;
4671 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4672 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4673 a1eca9bb 2018-06-23 stsp return NULL;
4675 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
4676 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(tree);
4677 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
4678 0cf4efb1 2018-09-29 stsp if (view->focussed)
4679 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4680 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
4682 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4683 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
4684 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4685 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4686 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4687 ffd1d5e5 2018-06-23 stsp return NULL;
4691 56e0773d 2019-11-28 stsp te = *first_displayed_entry;
4694 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4695 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4696 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
4697 848d6979 2019-08-12 stsp const char *modestr = "";
4698 56e0773d 2019-11-28 stsp mode_t mode;
4700 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4701 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4703 1d13200f 2018-07-12 stsp if (show_ids) {
4704 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4705 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4707 638f9024 2019-05-13 stsp return got_error_from_errno(
4708 230a42bd 2019-05-11 jcs "got_object_id_str");
4710 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4711 63c5ca5d 2019-08-24 stsp modestr = "$";
4712 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
4713 848d6979 2019-08-12 stsp modestr = "@";
4714 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4715 848d6979 2019-08-12 stsp modestr = "/";
4716 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4717 848d6979 2019-08-12 stsp modestr = "*";
4718 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4719 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te), modestr) == -1) {
4720 1d13200f 2018-07-12 stsp free(id_str);
4721 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4723 1d13200f 2018-07-12 stsp free(id_str);
4724 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4725 ffd1d5e5 2018-06-23 stsp if (err) {
4726 ffd1d5e5 2018-06-23 stsp free(line);
4729 ffd1d5e5 2018-06-23 stsp if (n == selected) {
4730 0cf4efb1 2018-09-29 stsp if (view->focussed)
4731 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4732 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
4734 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
4736 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4737 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4738 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4740 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4741 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4742 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4743 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4744 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
4745 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4746 ffd1d5e5 2018-06-23 stsp free(line);
4747 2550e4c3 2018-07-13 stsp free(wline);
4748 2550e4c3 2018-07-13 stsp wline = NULL;
4750 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4751 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
4752 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4756 ffd1d5e5 2018-06-23 stsp return err;
4759 ffd1d5e5 2018-06-23 stsp static void
4760 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
4761 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
4762 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int isroot)
4764 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4767 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
4770 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, 0);
4771 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
4772 ffd1d5e5 2018-06-23 stsp if (!isroot)
4773 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4778 56e0773d 2019-11-28 stsp while (*first_displayed_entry && i < maxscroll) {
4779 56e0773d 2019-11-28 stsp *first_displayed_entry = got_tree_entry_get_prev(tree,
4780 56e0773d 2019-11-28 stsp *first_displayed_entry);
4783 56e0773d 2019-11-28 stsp if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4784 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4787 768394f3 2019-01-24 stsp static int
4788 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4789 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
4790 56e0773d 2019-11-28 stsp struct got_tree_object *tree)
4792 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4793 ffd1d5e5 2018-06-23 stsp int n = 0;
4795 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
4796 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, *first_displayed_entry);
4798 56e0773d 2019-11-28 stsp next = got_object_tree_get_first_entry(tree);
4800 768394f3 2019-01-24 stsp last = last_displayed_entry;
4801 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4802 56e0773d 2019-11-28 stsp last = got_tree_entry_get_next(tree, last);
4803 768394f3 2019-01-24 stsp if (last) {
4804 768394f3 2019-01-24 stsp *first_displayed_entry = next;
4805 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, next);
4811 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4812 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4813 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4815 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4816 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4817 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4819 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4820 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
4821 56e0773d 2019-11-28 stsp + 1 /* slash */;
4823 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
4825 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4826 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4827 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4829 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4830 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4831 d9765a41 2018-06-23 stsp while (pt) {
4832 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
4833 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
4834 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4835 cb2ebc8a 2018-06-23 stsp goto done;
4837 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
4838 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4839 cb2ebc8a 2018-06-23 stsp goto done;
4841 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4844 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4845 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4846 ce52c690 2018-06-23 stsp goto done;
4850 ce52c690 2018-06-23 stsp if (err) {
4851 ce52c690 2018-06-23 stsp free(*path);
4852 ce52c690 2018-06-23 stsp *path = NULL;
4854 ce52c690 2018-06-23 stsp return err;
4857 ce52c690 2018-06-23 stsp static const struct got_error *
4858 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
4859 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4860 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4861 8b473291 2019-02-21 stsp struct got_repository *repo)
4863 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
4864 ce52c690 2018-06-23 stsp char *path;
4865 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
4867 a0de39f3 2019-08-09 stsp *new_view = NULL;
4869 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
4871 ce52c690 2018-06-23 stsp return err;
4873 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4874 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
4875 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
4876 83ce39e3 2019-08-12 stsp goto done;
4879 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
4880 e5a0f69f 2018-08-18 stsp if (err) {
4881 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
4882 fc06ba56 2019-08-22 stsp err = NULL;
4883 e5a0f69f 2018-08-18 stsp view_close(blame_view);
4885 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
4887 83ce39e3 2019-08-12 stsp free(path);
4888 69efd4c4 2018-07-18 stsp return err;
4891 69efd4c4 2018-07-18 stsp static const struct got_error *
4892 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
4893 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4894 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4895 8b473291 2019-02-21 stsp struct got_repository *repo)
4897 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
4898 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
4899 69efd4c4 2018-07-18 stsp char *path;
4901 a0de39f3 2019-08-09 stsp *new_view = NULL;
4903 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4904 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
4905 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
4907 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
4909 69efd4c4 2018-07-18 stsp return err;
4911 f135c941 2020-02-20 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4913 e5a0f69f 2018-08-18 stsp view_close(log_view);
4915 e5a0f69f 2018-08-18 stsp *new_view = log_view;
4916 cb2ebc8a 2018-06-23 stsp free(path);
4917 cb2ebc8a 2018-06-23 stsp return err;
4920 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4921 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
4922 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4923 8b473291 2019-02-21 stsp struct got_repository *repo)
4925 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4926 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
4927 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4929 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
4931 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
4932 ffd1d5e5 2018-06-23 stsp if (err != NULL)
4933 ffd1d5e5 2018-06-23 stsp goto done;
4935 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4936 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4937 ffd1d5e5 2018-06-23 stsp goto done;
4940 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
4941 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4942 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4943 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
4944 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
4945 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4946 6484ec90 2018-09-29 stsp goto done;
4948 8b473291 2019-02-21 stsp s->refs = refs;
4949 fb2756b9 2018-08-04 stsp s->repo = repo;
4951 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4953 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4954 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
4955 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
4956 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4958 c0b01bdb 2019-11-08 stsp goto done;
4959 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4960 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
4961 c0b01bdb 2019-11-08 stsp if (err) {
4962 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4963 c0b01bdb 2019-11-08 stsp goto done;
4965 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
4966 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
4967 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4968 c0b01bdb 2019-11-08 stsp if (err) {
4969 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4970 c0b01bdb 2019-11-08 stsp goto done;
4973 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
4974 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
4975 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4976 11b20872 2019-11-08 stsp if (err) {
4977 11b20872 2019-11-08 stsp free_colors(&s->colors);
4978 11b20872 2019-11-08 stsp goto done;
4981 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4982 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4983 c0b01bdb 2019-11-08 stsp if (err) {
4984 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4985 c0b01bdb 2019-11-08 stsp goto done;
4989 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
4990 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
4991 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
4992 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
4993 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
4995 ad80ab7b 2018-08-04 stsp free(commit_id_str);
4996 6484ec90 2018-09-29 stsp if (err) {
4997 fb2756b9 2018-08-04 stsp free(s->tree_label);
4998 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5000 ad80ab7b 2018-08-04 stsp return err;
5003 e5a0f69f 2018-08-18 stsp static const struct got_error *
5004 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
5006 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5008 bddb1296 2019-11-08 stsp free_colors(&s->colors);
5009 fb2756b9 2018-08-04 stsp free(s->tree_label);
5010 6484ec90 2018-09-29 stsp s->tree_label = NULL;
5011 6484ec90 2018-09-29 stsp free(s->commit_id);
5012 6484ec90 2018-09-29 stsp s->commit_id = NULL;
5013 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
5014 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
5015 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
5016 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
5017 ad80ab7b 2018-08-04 stsp free(parent);
5020 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
5021 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
5022 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
5024 7c32bd05 2019-06-22 stsp return NULL;
5027 7c32bd05 2019-06-22 stsp static const struct got_error *
5028 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
5030 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5032 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
5033 7c32bd05 2019-06-22 stsp return NULL;
5036 7c32bd05 2019-06-22 stsp static int
5037 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5039 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
5041 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5045 7c32bd05 2019-06-22 stsp static const struct got_error *
5046 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
5048 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
5049 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
5051 7c32bd05 2019-06-22 stsp if (!view->searching) {
5052 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5053 7c32bd05 2019-06-22 stsp return NULL;
5056 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5057 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
5058 7c32bd05 2019-06-22 stsp if (s->selected_entry)
5059 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
5060 56e0773d 2019-11-28 stsp s->selected_entry);
5062 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5064 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
5065 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5067 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
5068 56e0773d 2019-11-28 stsp s->selected_entry);
5071 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5072 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5074 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5077 7c32bd05 2019-06-22 stsp while (1) {
5078 56e0773d 2019-11-28 stsp if (te == NULL) {
5079 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
5080 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5081 ac66afb8 2019-06-24 stsp return NULL;
5083 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5084 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
5086 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
5089 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
5090 8f4ed634 2020-03-26 stsp view->search_next_done = TOG_SEARCH_HAVE_MORE;
5091 56e0773d 2019-11-28 stsp s->matched_entry = te;
5095 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
5096 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5098 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
5101 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
5102 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
5103 7c32bd05 2019-06-22 stsp s->selected = 0;
5106 e5a0f69f 2018-08-18 stsp return NULL;
5109 ad80ab7b 2018-08-04 stsp static const struct got_error *
5110 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
5112 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
5113 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
5114 e5a0f69f 2018-08-18 stsp char *parent_path;
5116 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
5118 e5a0f69f 2018-08-18 stsp return err;
5120 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
5121 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
5122 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5123 56e0773d 2019-11-28 stsp s->tree, s->selected, view->nlines, s->tree == s->root,
5124 bddb1296 2019-11-08 stsp &s->colors);
5125 e5a0f69f 2018-08-18 stsp free(parent_path);
5127 669b5ffa 2018-10-07 stsp view_vborder(view);
5128 e5a0f69f 2018-08-18 stsp return err;
5131 e5a0f69f 2018-08-18 stsp static const struct got_error *
5132 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5133 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
5135 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
5136 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
5137 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
5138 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
5140 e5a0f69f 2018-08-18 stsp switch (ch) {
5142 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
5145 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
5147 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
5148 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
5149 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
5150 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
5151 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
5152 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
5153 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
5155 1e37a5c2 2019-05-12 jcs return err;
5156 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
5158 1e37a5c2 2019-05-12 jcs view_close(log_view);
5161 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
5162 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
5164 1e37a5c2 2019-05-12 jcs *new_view = log_view;
5167 1e37a5c2 2019-05-12 jcs case KEY_UP:
5168 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
5169 1e37a5c2 2019-05-12 jcs s->selected--;
5170 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
5173 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
5175 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
5176 56e0773d 2019-11-28 stsp s->tree, s->tree == s->root);
5178 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
5179 ea025d1d 2020-02-22 naddy case CTRL('b'):
5180 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
5181 56e0773d 2019-11-28 stsp MAX(0, view->nlines - 4 - s->selected), s->tree,
5182 1e37a5c2 2019-05-12 jcs s->tree == s->root);
5183 1e37a5c2 2019-05-12 jcs s->selected = 0;
5184 56e0773d 2019-11-28 stsp if (got_object_tree_get_first_entry(s->tree) ==
5185 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
5186 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
5189 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
5190 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
5191 1e37a5c2 2019-05-12 jcs s->selected++;
5194 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5196 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
5198 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
5199 56e0773d 2019-11-28 stsp s->last_displayed_entry, s->tree);
5201 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
5202 ea025d1d 2020-02-22 naddy case CTRL('f'):
5203 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5205 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
5206 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
5207 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5210 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
5211 56e0773d 2019-11-28 stsp view->nlines, s->last_displayed_entry, s->tree);
5212 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
5213 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
5214 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
5215 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
5217 1e37a5c2 2019-05-12 jcs ndisplayed++;
5218 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
5219 1e37a5c2 2019-05-12 jcs } while (te);
5220 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
5223 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
5225 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
5226 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5227 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
5228 1e37a5c2 2019-05-12 jcs /* user selected '..' */
5229 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
5231 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
5232 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
5234 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
5235 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
5236 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
5237 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
5238 1e37a5c2 2019-05-12 jcs s->selected_entry =
5239 1e37a5c2 2019-05-12 jcs parent->selected_entry;
5240 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
5241 1e37a5c2 2019-05-12 jcs free(parent);
5242 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
5243 56e0773d 2019-11-28 stsp s->selected_entry))) {
5244 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
5245 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
5246 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
5249 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
5250 941e9f74 2019-05-21 stsp if (err) {
5251 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
5254 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
5255 56e0773d 2019-11-28 stsp s->selected_entry))) {
5256 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
5257 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
5258 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
5260 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
5261 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
5262 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
5265 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
5266 669b5ffa 2018-10-07 stsp err = view_close_child(view);
5268 669b5ffa 2018-10-07 stsp return err;
5269 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
5270 669b5ffa 2018-10-07 stsp if (err) {
5271 1e37a5c2 2019-05-12 jcs view_close(blame_view);
5274 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
5275 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
5277 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
5280 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
5281 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
5282 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
5288 ffd1d5e5 2018-06-23 stsp return err;
5291 ffd1d5e5 2018-06-23 stsp __dead static void
5292 ffd1d5e5 2018-06-23 stsp usage_tree(void)
5295 55cccc34 2020-02-20 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5296 ffd1d5e5 2018-06-23 stsp getprogname());
5300 ffd1d5e5 2018-06-23 stsp static const struct got_error *
5301 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
5303 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
5304 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
5305 55cccc34 2020-02-20 stsp struct got_worktree *worktree = NULL;
5306 8b473291 2019-02-21 stsp struct got_reflist_head refs;
5307 55cccc34 2020-02-20 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5308 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
5309 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
5310 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
5311 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
5313 5221c383 2018-08-01 stsp struct tog_view *view;
5315 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
5317 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
5318 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5319 d188b9a6 2019-01-04 stsp NULL) == -1)
5320 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
5323 74283ab8 2020-02-07 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5324 ffd1d5e5 2018-06-23 stsp switch (ch) {
5326 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5329 74283ab8 2020-02-07 stsp repo_path = realpath(optarg, NULL);
5330 74283ab8 2020-02-07 stsp if (repo_path == NULL)
5331 74283ab8 2020-02-07 stsp return got_error_from_errno2("realpath",
5335 17020d27 2019-03-07 stsp usage_tree();
5336 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5340 ffd1d5e5 2018-06-23 stsp argc -= optind;
5341 ffd1d5e5 2018-06-23 stsp argv += optind;
5343 55cccc34 2020-02-20 stsp if (argc > 1)
5344 74283ab8 2020-02-07 stsp usage_tree();
5346 55cccc34 2020-02-20 stsp cwd = getcwd(NULL, 0);
5347 55cccc34 2020-02-20 stsp if (cwd == NULL)
5348 55cccc34 2020-02-20 stsp return got_error_from_errno("getcwd");
5350 55cccc34 2020-02-20 stsp error = got_worktree_open(&worktree, cwd);
5351 55cccc34 2020-02-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5352 55cccc34 2020-02-20 stsp goto done;
5354 74283ab8 2020-02-07 stsp if (repo_path == NULL) {
5355 55cccc34 2020-02-20 stsp if (worktree)
5356 52185f70 2019-02-05 stsp repo_path =
5357 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5359 e4a0e26d 2020-02-20 stsp repo_path = strdup(cwd);
5361 55cccc34 2020-02-20 stsp if (repo_path == NULL) {
5362 55cccc34 2020-02-20 stsp error = got_error_from_errno("strdup");
5363 55cccc34 2020-02-20 stsp goto done;
5366 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5367 c02c541e 2019-03-29 stsp if (error != NULL)
5368 52185f70 2019-02-05 stsp goto done;
5370 55cccc34 2020-02-20 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5371 55cccc34 2020-02-20 stsp repo, worktree);
5372 55cccc34 2020-02-20 stsp if (error)
5373 55cccc34 2020-02-20 stsp goto done;
5375 55cccc34 2020-02-20 stsp init_curses();
5377 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5378 c02c541e 2019-03-29 stsp if (error)
5379 52185f70 2019-02-05 stsp goto done;
5381 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5382 71a27632 2020-01-15 stsp commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5383 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5384 55cccc34 2020-02-20 stsp if (error)
5385 ffd1d5e5 2018-06-23 stsp goto done;
5387 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5388 55cccc34 2020-02-20 stsp if (error)
5389 ffd1d5e5 2018-06-23 stsp goto done;
5391 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
5392 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5393 55cccc34 2020-02-20 stsp if (error)
5394 8b473291 2019-02-21 stsp goto done;
5396 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5397 8b473291 2019-02-21 stsp if (error)
5398 ffd1d5e5 2018-06-23 stsp goto done;
5400 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5401 5221c383 2018-08-01 stsp if (view == NULL) {
5402 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5403 5221c383 2018-08-01 stsp goto done;
5405 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
5406 ad80ab7b 2018-08-04 stsp if (error)
5407 ad80ab7b 2018-08-04 stsp goto done;
5408 55cccc34 2020-02-20 stsp if (!got_path_is_root_dir(in_repo_path)) {
5409 55cccc34 2020-02-20 stsp error = tree_view_walk_path(&view->state.tree, commit_id,
5410 55cccc34 2020-02-20 stsp in_repo_path, repo);
5411 55cccc34 2020-02-20 stsp if (error)
5412 55cccc34 2020-02-20 stsp goto done;
5415 55cccc34 2020-02-20 stsp if (worktree) {
5416 55cccc34 2020-02-20 stsp /* Release work tree lock. */
5417 55cccc34 2020-02-20 stsp got_worktree_close(worktree);
5418 55cccc34 2020-02-20 stsp worktree = NULL;
5420 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5422 52185f70 2019-02-05 stsp free(repo_path);
5423 e4a0e26d 2020-02-20 stsp free(cwd);
5424 ffd1d5e5 2018-06-23 stsp free(commit_id);
5425 ffd1d5e5 2018-06-23 stsp if (commit)
5426 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5428 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5430 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5431 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5432 ffd1d5e5 2018-06-23 stsp return error;
5435 ce5b7c56 2019-07-09 stsp static void
5436 ce5b7c56 2019-07-09 stsp list_commands(void)
5440 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
5441 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
5442 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
5443 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->name);
5445 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
5448 4ed7e80c 2018-05-20 stsp __dead static void
5449 ce5b7c56 2019-07-09 stsp usage(int hflag)
5451 ee85c5e8 2020-02-29 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] [command] "
5452 ee85c5e8 2020-02-29 stsp "[arg ...]\n", getprogname());
5453 ee85c5e8 2020-02-29 stsp if (hflag) {
5454 ee85c5e8 2020-02-29 stsp fprintf(stderr, "lazy usage: %s path\n", getprogname());
5455 ce5b7c56 2019-07-09 stsp list_commands();
5460 c2301be8 2018-04-30 stsp static char **
5461 ee85c5e8 2020-02-29 stsp make_argv(int argc, ...)
5463 ee85c5e8 2020-02-29 stsp va_list ap;
5464 c2301be8 2018-04-30 stsp char **argv;
5467 ee85c5e8 2020-02-29 stsp va_start(ap, argc);
5469 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
5470 c2301be8 2018-04-30 stsp if (argv == NULL)
5471 c2301be8 2018-04-30 stsp err(1, "calloc");
5472 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++) {
5473 ee85c5e8 2020-02-29 stsp argv[i] = strdup(va_arg(ap, char *));
5474 ee85c5e8 2020-02-29 stsp if (argv[i] == NULL)
5475 e10c916e 2019-09-15 hiltjo err(1, "strdup");
5478 ee85c5e8 2020-02-29 stsp va_end(ap);
5479 c2301be8 2018-04-30 stsp return argv;
5483 ee85c5e8 2020-02-29 stsp * Try to convert 'tog path' into a 'tog log path' command.
5484 ee85c5e8 2020-02-29 stsp * The user could simply have mistyped the command rather than knowingly
5485 ee85c5e8 2020-02-29 stsp * provided a path. So check whether argv[0] can in fact be resolved
5486 ee85c5e8 2020-02-29 stsp * to a path in the HEAD commit and print a special error if not.
5487 ee85c5e8 2020-02-29 stsp * This hack is for mpi@ <3
5489 ee85c5e8 2020-02-29 stsp static const struct got_error *
5490 ee85c5e8 2020-02-29 stsp tog_log_with_path(int argc, char *argv[])
5492 ee85c5e8 2020-02-29 stsp const struct got_error *error = NULL;
5493 ee85c5e8 2020-02-29 stsp struct tog_cmd *cmd = NULL;
5494 ee85c5e8 2020-02-29 stsp struct got_repository *repo = NULL;
5495 ee85c5e8 2020-02-29 stsp struct got_worktree *worktree = NULL;
5496 ee85c5e8 2020-02-29 stsp struct got_object_id *commit_id = NULL, *id = NULL;
5497 ee85c5e8 2020-02-29 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5498 ee85c5e8 2020-02-29 stsp char *commit_id_str = NULL, **cmd_argv = NULL;
5500 ee85c5e8 2020-02-29 stsp cwd = getcwd(NULL, 0);
5501 ee85c5e8 2020-02-29 stsp if (cwd == NULL)
5502 ee85c5e8 2020-02-29 stsp return got_error_from_errno("getcwd");
5504 ee85c5e8 2020-02-29 stsp error = got_worktree_open(&worktree, cwd);
5505 ee85c5e8 2020-02-29 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5506 ee85c5e8 2020-02-29 stsp goto done;
5508 ee85c5e8 2020-02-29 stsp if (worktree)
5509 ee85c5e8 2020-02-29 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
5511 ee85c5e8 2020-02-29 stsp repo_path = strdup(cwd);
5512 ee85c5e8 2020-02-29 stsp if (repo_path == NULL) {
5513 ee85c5e8 2020-02-29 stsp error = got_error_from_errno("strdup");
5514 ee85c5e8 2020-02-29 stsp goto done;
5517 ee85c5e8 2020-02-29 stsp error = got_repo_open(&repo, repo_path, NULL);
5518 ee85c5e8 2020-02-29 stsp if (error != NULL)
5519 ee85c5e8 2020-02-29 stsp goto done;
5521 ee85c5e8 2020-02-29 stsp error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5522 ee85c5e8 2020-02-29 stsp repo, worktree);
5523 ee85c5e8 2020-02-29 stsp if (error)
5524 ee85c5e8 2020-02-29 stsp goto done;
5526 ee85c5e8 2020-02-29 stsp error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5527 ee85c5e8 2020-02-29 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5528 ee85c5e8 2020-02-29 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5529 ee85c5e8 2020-02-29 stsp if (error)
5530 ee85c5e8 2020-02-29 stsp goto done;
5532 ee85c5e8 2020-02-29 stsp if (worktree) {
5533 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
5534 ee85c5e8 2020-02-29 stsp worktree = NULL;
5537 ee85c5e8 2020-02-29 stsp error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5538 ee85c5e8 2020-02-29 stsp if (error) {
5539 ee85c5e8 2020-02-29 stsp if (error->code != GOT_ERR_NO_TREE_ENTRY)
5540 ee85c5e8 2020-02-29 stsp goto done;
5541 ee85c5e8 2020-02-29 stsp fprintf(stderr, "%s: '%s' is no known command or path\n",
5542 ee85c5e8 2020-02-29 stsp getprogname(), argv[0]);
5544 ee85c5e8 2020-02-29 stsp /* not reached */
5547 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
5548 ee85c5e8 2020-02-29 stsp repo = NULL;
5550 ee85c5e8 2020-02-29 stsp error = got_object_id_str(&commit_id_str, commit_id);
5551 ee85c5e8 2020-02-29 stsp if (error)
5552 ee85c5e8 2020-02-29 stsp goto done;
5554 ee85c5e8 2020-02-29 stsp cmd = &tog_commands[0]; /* log */
5556 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5557 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv);
5560 ee85c5e8 2020-02-29 stsp got_repo_close(repo);
5561 ee85c5e8 2020-02-29 stsp if (worktree)
5562 ee85c5e8 2020-02-29 stsp got_worktree_close(worktree);
5564 ee85c5e8 2020-02-29 stsp free(commit_id_str);
5565 ee85c5e8 2020-02-29 stsp free(commit_id);
5566 ee85c5e8 2020-02-29 stsp free(cwd);
5567 ee85c5e8 2020-02-29 stsp free(repo_path);
5568 ee85c5e8 2020-02-29 stsp free(in_repo_path);
5569 ee85c5e8 2020-02-29 stsp if (cmd_argv) {
5571 ee85c5e8 2020-02-29 stsp for (i = 0; i < argc; i++)
5572 ee85c5e8 2020-02-29 stsp free(cmd_argv[i]);
5573 ee85c5e8 2020-02-29 stsp free(cmd_argv);
5575 ee85c5e8 2020-02-29 stsp return error;
5579 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
5581 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
5582 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
5583 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
5584 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
5585 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
5586 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
5587 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
5590 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
5592 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5593 9f7d7167 2018-04-29 stsp switch (ch) {
5595 9f7d7167 2018-04-29 stsp hflag = 1;
5598 53ccebc2 2019-07-30 stsp Vflag = 1;
5601 ce5b7c56 2019-07-09 stsp usage(hflag);
5602 9f7d7167 2018-04-29 stsp /* NOTREACHED */
5606 9f7d7167 2018-04-29 stsp argc -= optind;
5607 9f7d7167 2018-04-29 stsp argv += optind;
5608 9f7d7167 2018-04-29 stsp optind = 0;
5609 c2301be8 2018-04-30 stsp optreset = 1;
5611 53ccebc2 2019-07-30 stsp if (Vflag) {
5612 53ccebc2 2019-07-30 stsp got_version_print_str();
5616 c2301be8 2018-04-30 stsp if (argc == 0) {
5617 f29d3e89 2018-06-23 stsp if (hflag)
5618 ce5b7c56 2019-07-09 stsp usage(hflag);
5619 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
5620 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
5622 ee85c5e8 2020-02-29 stsp cmd_argv = make_argv(argc, cmd->name);
5626 dfd6c250 2020-02-28 stsp /* Did the user specify a command? */
5627 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
5628 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
5629 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
5630 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
5636 ee85c5e8 2020-02-29 stsp if (cmd == NULL) {
5637 ee85c5e8 2020-02-29 stsp if (argc != 1)
5639 ee85c5e8 2020-02-29 stsp /* No command specified; try log with a path */
5640 ee85c5e8 2020-02-29 stsp error = tog_log_with_path(argc, argv);
5642 ee85c5e8 2020-02-29 stsp if (hflag)
5643 ee85c5e8 2020-02-29 stsp cmd->cmd_usage();
5645 ee85c5e8 2020-02-29 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5649 a2f4a359 2020-02-28 stsp if (cmd_argv) {
5651 a2f4a359 2020-02-28 stsp for (i = 0; i < argc; i++)
5652 a2f4a359 2020-02-28 stsp free(cmd_argv[i]);
5653 a2f4a359 2020-02-28 stsp free(cmd_argv);
5656 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
5657 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);