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 26ed57b2 2018-05-19 stsp #include <stdio.h>
30 9f7d7167 2018-04-29 stsp #include <getopt.h>
31 9f7d7167 2018-04-29 stsp #include <string.h>
32 9f7d7167 2018-04-29 stsp #include <err.h>
33 80ddbec8 2018-04-29 stsp #include <unistd.h>
34 26ed57b2 2018-05-19 stsp #include <util.h>
35 26ed57b2 2018-05-19 stsp #include <limits.h>
36 61e69b96 2018-05-20 stsp #include <wchar.h>
37 788c352e 2018-06-16 stsp #include <time.h>
38 84451b3e 2018-07-10 stsp #include <pthread.h>
39 5036bf37 2018-09-24 stsp #include <libgen.h>
40 60493ae3 2019-06-20 stsp #include <regex.h>
42 53ccebc2 2019-07-30 stsp #include "got_version.h"
43 9f7d7167 2018-04-29 stsp #include "got_error.h"
44 80ddbec8 2018-04-29 stsp #include "got_object.h"
45 80ddbec8 2018-04-29 stsp #include "got_reference.h"
46 80ddbec8 2018-04-29 stsp #include "got_repository.h"
47 80ddbec8 2018-04-29 stsp #include "got_diff.h"
48 511a516b 2018-05-19 stsp #include "got_opentemp.h"
49 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
50 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
51 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
52 a70480e0 2018-06-23 stsp #include "got_blame.h"
53 c2db6724 2019-01-04 stsp #include "got_privsep.h"
54 1dd54920 2019-05-11 stsp #include "got_path.h"
55 b7165be3 2019-02-05 stsp #include "got_worktree.h"
58 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
67 9f7d7167 2018-04-29 stsp #ifndef nitems
68 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 9f7d7167 2018-04-29 stsp struct tog_cmd {
72 c2301be8 2018-04-30 stsp const char *name;
73 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
74 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
77 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
78 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
79 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
81 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
83 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
84 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
85 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
86 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
88 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
89 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
90 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
91 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
92 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
95 d6b05b5b 2018-08-04 stsp enum tog_view_type {
96 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
97 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
98 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
99 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
102 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
104 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
105 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
106 ba4f502b 2018-08-04 stsp struct got_object_id *id;
107 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
110 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
111 ba4f502b 2018-08-04 stsp struct commit_queue {
112 ba4f502b 2018-08-04 stsp int ncommits;
113 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
116 f26dddb7 2019-11-08 stsp struct tog_color {
117 f26dddb7 2019-11-08 stsp SIMPLEQ_ENTRY(tog_color) entry;
118 6d17833f 2019-11-08 stsp regex_t regex;
119 6d17833f 2019-11-08 stsp short colorpair;
121 f26dddb7 2019-11-08 stsp SIMPLEQ_HEAD(tog_colors, tog_color);
123 11b20872 2019-11-08 stsp static const struct got_error *
124 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
125 11b20872 2019-11-08 stsp int idx, short color)
127 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
128 11b20872 2019-11-08 stsp struct tog_color *tc;
129 11b20872 2019-11-08 stsp int regerr = 0;
131 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
132 11b20872 2019-11-08 stsp return NULL;
134 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
136 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
137 11b20872 2019-11-08 stsp if (tc == NULL)
138 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
139 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
140 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
141 11b20872 2019-11-08 stsp if (regerr) {
142 11b20872 2019-11-08 stsp static char regerr_msg[512];
143 11b20872 2019-11-08 stsp static char err_msg[512];
144 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
145 11b20872 2019-11-08 stsp sizeof(regerr_msg));
146 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
147 11b20872 2019-11-08 stsp regerr_msg);
148 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
150 11b20872 2019-11-08 stsp return err;
152 11b20872 2019-11-08 stsp tc->colorpair = idx;
153 11b20872 2019-11-08 stsp SIMPLEQ_INSERT_HEAD(colors, tc, entry);
154 11b20872 2019-11-08 stsp return NULL;
157 11b20872 2019-11-08 stsp static void
158 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
160 11b20872 2019-11-08 stsp struct tog_color *tc;
162 11b20872 2019-11-08 stsp while (!SIMPLEQ_EMPTY(colors)) {
163 11b20872 2019-11-08 stsp tc = SIMPLEQ_FIRST(colors);
164 11b20872 2019-11-08 stsp SIMPLEQ_REMOVE_HEAD(colors, entry);
165 11b20872 2019-11-08 stsp regfree(&tc->regex);
170 11b20872 2019-11-08 stsp struct tog_color *
171 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
173 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
175 11b20872 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
176 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
180 11b20872 2019-11-08 stsp return NULL;
184 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
186 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
187 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
188 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
189 11b20872 2019-11-08 stsp return COLOR_CYAN;
190 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
191 11b20872 2019-11-08 stsp return COLOR_YELLOW;
192 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
193 11b20872 2019-11-08 stsp return COLOR_GREEN;
194 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
195 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
196 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
197 91b8c405 2020-01-25 stsp return COLOR_MAGENTA;
198 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
199 91b8c405 2020-01-25 stsp return COLOR_CYAN;
200 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
201 11b20872 2019-11-08 stsp return COLOR_GREEN;
202 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
203 11b20872 2019-11-08 stsp return COLOR_GREEN;
204 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
205 11b20872 2019-11-08 stsp return COLOR_CYAN;
206 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
207 11b20872 2019-11-08 stsp return COLOR_YELLOW;
213 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
215 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
217 11b20872 2019-11-08 stsp if (val == NULL)
218 11b20872 2019-11-08 stsp return default_color_value(envvar);
220 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
221 11b20872 2019-11-08 stsp return COLOR_BLACK;
222 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
223 11b20872 2019-11-08 stsp return COLOR_RED;
224 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
225 11b20872 2019-11-08 stsp return COLOR_GREEN;
226 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
227 11b20872 2019-11-08 stsp return COLOR_YELLOW;
228 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
229 11b20872 2019-11-08 stsp return COLOR_BLUE;
230 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
231 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
232 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
233 11b20872 2019-11-08 stsp return COLOR_CYAN;
234 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
235 11b20872 2019-11-08 stsp return COLOR_WHITE;
236 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
239 11b20872 2019-11-08 stsp return default_color_value(envvar);
243 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
244 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
246 15a087fe 2019-02-21 stsp int first_displayed_line;
247 15a087fe 2019-02-21 stsp int last_displayed_line;
249 15a087fe 2019-02-21 stsp int diff_context;
250 15a087fe 2019-02-21 stsp struct got_repository *repo;
251 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
252 bddb1296 2019-11-08 stsp struct tog_colors colors;
254 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
255 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
258 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
260 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
261 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
262 1a76625f 2018-10-22 stsp int commits_needed;
263 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
264 1a76625f 2018-10-22 stsp struct commit_queue *commits;
265 1a76625f 2018-10-22 stsp const char *in_repo_path;
266 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
267 1a76625f 2018-10-22 stsp struct got_repository *repo;
268 1a76625f 2018-10-22 stsp int log_complete;
269 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
270 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
271 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
272 13add988 2019-10-15 stsp int *searching;
273 13add988 2019-10-15 stsp int *search_next_done;
274 13add988 2019-10-15 stsp regex_t *regex;
277 1a76625f 2018-10-22 stsp struct tog_log_view_state {
278 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
279 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
280 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
281 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
282 b01e7d3b 2018-08-04 stsp int selected;
283 b01e7d3b 2018-08-04 stsp char *in_repo_path;
284 d01904d4 2019-06-25 stsp const char *head_ref_name;
285 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
286 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
287 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
288 1a76625f 2018-10-22 stsp sig_atomic_t quit;
289 1a76625f 2018-10-22 stsp pthread_t thread;
290 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
291 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
292 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
293 11b20872 2019-11-08 stsp struct tog_colors colors;
296 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
297 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
298 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
299 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
300 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
301 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
302 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
303 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
304 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
305 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
306 11b20872 2019-11-08 stsp #define TOG_COLOR_DATE 11
308 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
309 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
310 e9424729 2018-08-04 stsp int nlines;
312 e9424729 2018-08-04 stsp struct tog_view *view;
313 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
317 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
318 e9424729 2018-08-04 stsp const char *path;
319 e9424729 2018-08-04 stsp struct got_repository *repo;
320 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
321 e9424729 2018-08-04 stsp int *complete;
322 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
323 fc06ba56 2019-08-22 stsp void *cancel_arg;
326 e9424729 2018-08-04 stsp struct tog_blame {
328 e9424729 2018-08-04 stsp size_t filesize;
329 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
330 6fcac457 2018-11-19 stsp int nlines;
331 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
332 e9424729 2018-08-04 stsp pthread_t thread;
333 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
334 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
335 e9424729 2018-08-04 stsp const char *path;
338 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
339 7cbe629d 2018-08-04 stsp int first_displayed_line;
340 7cbe629d 2018-08-04 stsp int last_displayed_line;
341 7cbe629d 2018-08-04 stsp int selected_line;
342 7cbe629d 2018-08-04 stsp int blame_complete;
345 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
346 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
347 e5a0f69f 2018-08-18 stsp char *path;
348 7cbe629d 2018-08-04 stsp struct got_repository *repo;
349 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
350 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
351 e9424729 2018-08-04 stsp struct tog_blame blame;
352 6c4c42e0 2019-06-24 stsp int matched_line;
353 11b20872 2019-11-08 stsp struct tog_colors colors;
356 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
357 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
358 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
359 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
360 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
361 ad80ab7b 2018-08-04 stsp int selected;
364 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
366 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
367 ad80ab7b 2018-08-04 stsp char *tree_label;
368 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
369 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
370 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
371 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
372 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
373 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
374 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
375 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
376 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
377 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
378 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
379 bddb1296 2019-11-08 stsp struct tog_colors colors;
383 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
385 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
386 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
387 669b5ffa 2018-10-07 stsp * there is enough screen estate.
389 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
390 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
392 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
393 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
394 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
396 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
397 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
399 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
401 cc3c9aac 2018-08-01 stsp struct tog_view {
402 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
403 26ed57b2 2018-05-19 stsp WINDOW *window;
404 26ed57b2 2018-05-19 stsp PANEL *panel;
405 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
406 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
407 1004088d 2018-09-29 stsp int focussed;
408 669b5ffa 2018-10-07 stsp struct tog_view *parent;
409 669b5ffa 2018-10-07 stsp struct tog_view *child;
410 669b5ffa 2018-10-07 stsp int child_focussed;
412 5dc9f4bc 2018-08-04 stsp /* type-specific state */
413 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
415 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
416 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
417 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
418 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
421 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
422 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
423 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
424 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
426 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
427 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
428 60493ae3 2019-06-20 stsp int searching;
429 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
430 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
431 60493ae3 2019-06-20 stsp int search_next_done;
432 1803e47f 2019-06-22 stsp regex_t regex;
435 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
436 fb872ab2 2019-02-21 stsp struct got_object_id *, struct got_object_id *, struct tog_view *,
437 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
438 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
439 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
440 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
441 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
443 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
444 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *,
445 d01904d4 2019-06-25 stsp struct got_repository *, const char *, const char *, int);
446 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
447 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
448 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
449 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
450 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
451 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
453 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
454 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *, struct got_repository *);
455 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
456 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
457 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
458 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
459 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
460 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
462 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
463 8b473291 2019-02-21 stsp struct got_tree_object *, struct got_object_id *,
464 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
465 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
466 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
467 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
468 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
469 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
470 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
472 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
473 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
474 61266923 2020-01-14 stsp static volatile sig_atomic_t tog_sigcont_received;
476 25791caa 2018-10-24 stsp static void
477 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
479 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
482 83baff54 2019-08-12 stsp static void
483 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
485 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
488 61266923 2020-01-14 stsp static void
489 61266923 2020-01-14 stsp tog_sigcont(int signo)
491 61266923 2020-01-14 stsp tog_sigcont_received = 1;
494 e5a0f69f 2018-08-18 stsp static const struct got_error *
495 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
497 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
499 669b5ffa 2018-10-07 stsp if (view->child) {
500 669b5ffa 2018-10-07 stsp view_close(view->child);
501 669b5ffa 2018-10-07 stsp view->child = NULL;
503 e5a0f69f 2018-08-18 stsp if (view->close)
504 e5a0f69f 2018-08-18 stsp err = view->close(view);
505 ea5e7bb5 2018-08-01 stsp if (view->panel)
506 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
507 ea5e7bb5 2018-08-01 stsp if (view->window)
508 ea5e7bb5 2018-08-01 stsp delwin(view->window);
509 ea5e7bb5 2018-08-01 stsp free(view);
510 e5a0f69f 2018-08-18 stsp return err;
513 ea5e7bb5 2018-08-01 stsp static struct tog_view *
514 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
515 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
517 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
519 ea5e7bb5 2018-08-01 stsp if (view == NULL)
520 ea5e7bb5 2018-08-01 stsp return NULL;
522 d6b05b5b 2018-08-04 stsp view->type = type;
523 f7d12f7e 2018-08-01 stsp view->lines = LINES;
524 f7d12f7e 2018-08-01 stsp view->cols = COLS;
525 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
526 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
527 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
528 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
529 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
530 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
531 96a765a8 2018-08-04 stsp view_close(view);
532 ea5e7bb5 2018-08-01 stsp return NULL;
534 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
535 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
536 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
537 96a765a8 2018-08-04 stsp view_close(view);
538 ea5e7bb5 2018-08-01 stsp return NULL;
541 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
542 ea5e7bb5 2018-08-01 stsp return view;
546 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
548 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
550 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
553 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
555 5c60c32a 2018-10-18 stsp static const struct got_error *
556 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
558 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
560 5c60c32a 2018-10-18 stsp view->begin_y = 0;
561 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
562 5c60c32a 2018-10-18 stsp view->nlines = LINES;
563 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
564 5c60c32a 2018-10-18 stsp view->lines = LINES;
565 5c60c32a 2018-10-18 stsp view->cols = COLS;
566 5c60c32a 2018-10-18 stsp err = view_resize(view);
568 5c60c32a 2018-10-18 stsp return err;
570 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
571 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
573 5c60c32a 2018-10-18 stsp return NULL;
576 5c60c32a 2018-10-18 stsp static const struct got_error *
577 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
579 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
581 5c60c32a 2018-10-18 stsp view->begin_x = 0;
582 5c60c32a 2018-10-18 stsp view->begin_y = 0;
583 5c60c32a 2018-10-18 stsp view->nlines = LINES;
584 5c60c32a 2018-10-18 stsp view->ncols = COLS;
585 5c60c32a 2018-10-18 stsp view->lines = LINES;
586 5c60c32a 2018-10-18 stsp view->cols = COLS;
587 5c60c32a 2018-10-18 stsp err = view_resize(view);
589 5c60c32a 2018-10-18 stsp return err;
591 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
592 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
594 5c60c32a 2018-10-18 stsp return NULL;
598 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
600 5c60c32a 2018-10-18 stsp return view->parent == NULL;
603 4d8c2215 2018-08-19 stsp static const struct got_error *
604 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
606 f7d12f7e 2018-08-01 stsp int nlines, ncols;
608 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
609 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
611 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
613 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
614 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
616 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
618 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
619 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
620 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
621 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
622 25791caa 2018-10-24 stsp wclear(view->window);
624 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
625 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
626 0cf4efb1 2018-09-29 stsp view->lines = LINES;
627 0cf4efb1 2018-09-29 stsp view->cols = COLS;
629 6e3e5d9c 2018-10-18 stsp if (view->child) {
630 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
631 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
632 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
633 5c60c32a 2018-10-18 stsp if (view->child->focussed)
634 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
636 5c60c32a 2018-10-18 stsp show_panel(view->panel);
638 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
639 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
643 5c60c32a 2018-10-18 stsp return NULL;
646 669b5ffa 2018-10-07 stsp static const struct got_error *
647 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
649 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
651 669b5ffa 2018-10-07 stsp if (view->child == NULL)
652 669b5ffa 2018-10-07 stsp return NULL;
654 669b5ffa 2018-10-07 stsp err = view_close(view->child);
655 669b5ffa 2018-10-07 stsp view->child = NULL;
656 669b5ffa 2018-10-07 stsp return err;
659 669b5ffa 2018-10-07 stsp static const struct got_error *
660 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
662 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
664 669b5ffa 2018-10-07 stsp view->child = child;
665 669b5ffa 2018-10-07 stsp child->parent = view;
666 669b5ffa 2018-10-07 stsp return err;
670 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
672 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
675 34bc9ec9 2019-02-22 stsp static void
676 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
678 25791caa 2018-10-24 stsp int cols, lines;
679 25791caa 2018-10-24 stsp struct winsize size;
681 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
682 25791caa 2018-10-24 stsp cols = 80; /* Default */
683 25791caa 2018-10-24 stsp lines = 24;
685 25791caa 2018-10-24 stsp cols = size.ws_col;
686 25791caa 2018-10-24 stsp lines = size.ws_row;
688 25791caa 2018-10-24 stsp resize_term(lines, cols);
691 2b49a8ae 2019-06-22 stsp static const struct got_error *
692 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
694 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
695 2b49a8ae 2019-06-22 stsp char pattern[1024];
697 7d049c18 2019-08-21 stsp int begin_x = 0;
699 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
700 2b49a8ae 2019-06-22 stsp return NULL;
702 7d049c18 2019-08-21 stsp if (!view_is_parent_view(view))
703 7d049c18 2019-08-21 stsp begin_x = view_split_begin_x(view->begin_x);
704 2b49a8ae 2019-06-22 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1,
705 7d049c18 2019-08-21 stsp begin_x, "/");
706 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
708 2b49a8ae 2019-06-22 stsp nocbreak();
710 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
713 2b49a8ae 2019-06-22 stsp if (ret == ERR)
714 2b49a8ae 2019-06-22 stsp return NULL;
716 2b49a8ae 2019-06-22 stsp if (view->searching) {
717 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
718 2b49a8ae 2019-06-22 stsp view->searching = 0;
721 2b49a8ae 2019-06-22 stsp if (regcomp(&view->regex, pattern,
722 f5daf9b1 2019-06-24 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
723 7c32bd05 2019-06-22 stsp err = view->search_start(view);
725 7c32bd05 2019-06-22 stsp regfree(&view->regex);
726 7c32bd05 2019-06-22 stsp return err;
728 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
729 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
730 2b49a8ae 2019-06-22 stsp view->search_next(view);
733 2b49a8ae 2019-06-22 stsp return NULL;
736 0cf4efb1 2018-09-29 stsp static const struct got_error *
737 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
738 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
739 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
741 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
742 669b5ffa 2018-10-07 stsp struct tog_view *v;
743 1a76625f 2018-10-22 stsp int ch, errcode;
745 e5a0f69f 2018-08-18 stsp *new = NULL;
746 e5a0f69f 2018-08-18 stsp *dead = NULL;
747 0cf4efb1 2018-09-29 stsp *focus = NULL;
749 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
750 60493ae3 2019-06-20 stsp view->search_next(view);
751 60493ae3 2019-06-20 stsp return NULL;
754 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
755 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
756 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
757 1a76625f 2018-10-22 stsp if (errcode)
758 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
759 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
760 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
761 1a76625f 2018-10-22 stsp if (errcode)
762 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
763 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
765 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
766 25791caa 2018-10-24 stsp tog_resizeterm();
767 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
768 61266923 2020-01-14 stsp tog_sigcont_received = 0;
769 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
770 25791caa 2018-10-24 stsp err = view_resize(v);
772 25791caa 2018-10-24 stsp return err;
773 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
775 25791caa 2018-10-24 stsp return err;
779 e5a0f69f 2018-08-18 stsp switch (ch) {
783 1e37a5c2 2019-05-12 jcs if (view->child) {
784 1e37a5c2 2019-05-12 jcs *focus = view->child;
785 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
786 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
787 1e37a5c2 2019-05-12 jcs *focus = view->parent;
788 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
792 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
793 1e37a5c2 2019-05-12 jcs *dead = view;
799 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
800 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
802 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
803 669b5ffa 2018-10-07 stsp *focus = view->child;
804 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
805 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
807 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
810 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
811 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
813 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
814 1e37a5c2 2019-05-12 jcs *focus = view;
815 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
816 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
818 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
822 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
823 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
826 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
829 60493ae3 2019-06-20 stsp if (view->search_start)
830 2b49a8ae 2019-06-22 stsp view_search_start(view);
832 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
836 60493ae3 2019-06-20 stsp if (view->search_next && view->searching) {
837 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
838 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
839 60493ae3 2019-06-20 stsp view->search_next_done = 0;
840 60493ae3 2019-06-20 stsp view->search_next(view);
842 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
845 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
849 e5a0f69f 2018-08-18 stsp return err;
853 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
855 0cf4efb1 2018-09-29 stsp PANEL *panel;
856 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
858 669b5ffa 2018-10-07 stsp if (view->parent)
859 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
861 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
862 0cf4efb1 2018-09-29 stsp if (panel == NULL)
865 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
866 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
867 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
871 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
873 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
874 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
876 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
878 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
881 669b5ffa 2018-10-07 stsp return view->focussed;
884 bcbd79e2 2018-08-19 stsp static const struct got_error *
885 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
887 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
888 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
889 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
890 fd823528 2018-10-22 stsp int fast_refresh = 10;
891 1a76625f 2018-10-22 stsp int done = 0, errcode;
893 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
894 1a76625f 2018-10-22 stsp if (errcode)
895 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
897 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
898 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
900 a81bf10d 2018-09-29 stsp main_view = view;
901 1004088d 2018-09-29 stsp view->focussed = 1;
902 878940b7 2018-09-29 stsp err = view->show(view);
904 0cf4efb1 2018-09-29 stsp return err;
905 0cf4efb1 2018-09-29 stsp update_panels();
906 0cf4efb1 2018-09-29 stsp doupdate();
907 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
908 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
909 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
910 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
912 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
913 e4197bf9 2018-08-18 stsp view, &views);
916 e5a0f69f 2018-08-18 stsp if (dead_view) {
917 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
919 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
920 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
921 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
922 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
923 669b5ffa 2018-10-07 stsp prev = view->parent;
925 669b5ffa 2018-10-07 stsp if (dead_view->parent)
926 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
928 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
930 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
931 d01904d4 2019-06-25 stsp if (err || (dead_view == main_view && new_view == NULL))
934 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
935 0cf4efb1 2018-09-29 stsp if (focus_view)
936 0cf4efb1 2018-09-29 stsp view = focus_view;
937 669b5ffa 2018-10-07 stsp else if (prev)
938 669b5ffa 2018-10-07 stsp view = prev;
939 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
940 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
941 0cf4efb1 2018-09-29 stsp tog_view_list_head);
943 0cf4efb1 2018-09-29 stsp view = NULL;
944 669b5ffa 2018-10-07 stsp if (view) {
945 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
946 669b5ffa 2018-10-07 stsp focus_view = view->child;
948 669b5ffa 2018-10-07 stsp focus_view = view;
952 bcbd79e2 2018-08-19 stsp if (new_view) {
953 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
954 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
955 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
956 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
958 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
959 86c66b02 2018-10-18 stsp err = view_close(v);
964 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
965 fed7eaa8 2018-10-24 stsp view = new_view;
966 878940b7 2018-09-29 stsp if (focus_view == NULL)
967 878940b7 2018-09-29 stsp focus_view = new_view;
969 0cf4efb1 2018-09-29 stsp if (focus_view) {
970 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
972 0cf4efb1 2018-09-29 stsp view->focussed = 0;
973 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
974 0cf4efb1 2018-09-29 stsp view = focus_view;
975 878940b7 2018-09-29 stsp if (new_view)
976 878940b7 2018-09-29 stsp show_panel(new_view->panel);
977 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
978 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
980 669b5ffa 2018-10-07 stsp if (view) {
981 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
982 758194b5 2018-10-24 stsp view->focussed = 1;
983 758194b5 2018-10-24 stsp show_panel(view->panel);
984 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
985 758194b5 2018-10-24 stsp show_panel(view->child->panel);
986 1a76625f 2018-10-22 stsp focus_view = view;
988 669b5ffa 2018-10-07 stsp if (view->parent) {
989 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
993 669b5ffa 2018-10-07 stsp err = view->show(view);
996 669b5ffa 2018-10-07 stsp if (view->child) {
997 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1001 1a76625f 2018-10-22 stsp update_panels();
1002 1a76625f 2018-10-22 stsp doupdate();
1006 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1007 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1008 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1009 e5a0f69f 2018-08-18 stsp view_close(view);
1012 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1013 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1014 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1016 e5a0f69f 2018-08-18 stsp return err;
1019 4ed7e80c 2018-05-20 stsp __dead static void
1020 9f7d7167 2018-04-29 stsp usage_log(void)
1023 c70c5802 2018-08-01 stsp fprintf(stderr,
1024 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
1025 9f7d7167 2018-04-29 stsp getprogname());
1029 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1030 80ddbec8 2018-04-29 stsp static const struct got_error *
1031 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1033 00dfcb92 2018-06-11 stsp char *vis = NULL;
1034 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1036 963b370f 2018-05-20 stsp *ws = NULL;
1037 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1038 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1039 00dfcb92 2018-06-11 stsp int vislen;
1040 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1041 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1043 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1044 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1046 00dfcb92 2018-06-11 stsp return err;
1047 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1048 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1049 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1050 a7f50699 2018-06-11 stsp goto done;
1054 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1055 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1056 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1057 a7f50699 2018-06-11 stsp goto done;
1060 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1061 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1063 00dfcb92 2018-06-11 stsp free(vis);
1064 963b370f 2018-05-20 stsp if (err) {
1065 963b370f 2018-05-20 stsp free(*ws);
1066 963b370f 2018-05-20 stsp *ws = NULL;
1067 963b370f 2018-05-20 stsp *wlen = 0;
1069 963b370f 2018-05-20 stsp return err;
1072 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1073 963b370f 2018-05-20 stsp static const struct got_error *
1074 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1075 27a741e5 2019-09-11 stsp int col_tab_align)
1077 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1078 963b370f 2018-05-20 stsp int cols = 0;
1079 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1080 963b370f 2018-05-20 stsp size_t wlen;
1083 963b370f 2018-05-20 stsp *wlinep = NULL;
1084 b700b5d6 2018-07-10 stsp *widthp = 0;
1086 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1088 963b370f 2018-05-20 stsp return err;
1091 27a741e5 2019-09-11 stsp while (i < wlen) {
1092 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1094 27a741e5 2019-09-11 stsp if (width == 0) {
1099 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1100 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1102 27a741e5 2019-09-11 stsp cols += width;
1104 27a741e5 2019-09-11 stsp } else if (width == -1) {
1105 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1106 27a741e5 2019-09-11 stsp width = TABSIZE -
1107 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1108 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1110 27a741e5 2019-09-11 stsp cols += width;
1114 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1115 963b370f 2018-05-20 stsp goto done;
1118 963b370f 2018-05-20 stsp wline[i] = L'\0';
1119 b700b5d6 2018-07-10 stsp if (widthp)
1120 b700b5d6 2018-07-10 stsp *widthp = cols;
1123 963b370f 2018-05-20 stsp free(wline);
1125 963b370f 2018-05-20 stsp *wlinep = wline;
1126 963b370f 2018-05-20 stsp return err;
1129 8b473291 2019-02-21 stsp static const struct got_error*
1130 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1131 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1133 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1134 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1136 8b473291 2019-02-21 stsp const char *name;
1138 8b473291 2019-02-21 stsp *refs_str = NULL;
1140 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1141 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1144 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1145 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1147 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1148 8b473291 2019-02-21 stsp name += 5;
1149 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1151 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1152 8b473291 2019-02-21 stsp name += 6;
1153 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
1154 8b473291 2019-02-21 stsp name += 8;
1155 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1156 52b5abe1 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1157 5d844a1e 2019-08-13 stsp if (err) {
1158 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1160 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1161 5d844a1e 2019-08-13 stsp err = NULL;
1162 5d844a1e 2019-08-13 stsp tag = NULL;
1165 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1166 52b5abe1 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1168 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1169 52b5abe1 2019-08-13 stsp if (cmp != 0)
1171 8b473291 2019-02-21 stsp s = *refs_str;
1172 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1173 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1174 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1176 8b473291 2019-02-21 stsp *refs_str = NULL;
1182 8b473291 2019-02-21 stsp return err;
1185 963b370f 2018-05-20 stsp static const struct got_error *
1186 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1187 27a741e5 2019-09-11 stsp int col_tab_align)
1189 5813d178 2019-03-09 stsp char *smallerthan, *at;
1191 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1192 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1193 5813d178 2019-03-09 stsp author = smallerthan + 1;
1194 5813d178 2019-03-09 stsp at = strchr(author, '@');
1196 5813d178 2019-03-09 stsp *at = '\0';
1197 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1200 5813d178 2019-03-09 stsp static const struct got_error *
1201 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1202 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
1203 11b20872 2019-11-08 stsp const size_t date_display_cols, int author_display_cols,
1204 11b20872 2019-11-08 stsp struct tog_colors *colors)
1206 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1207 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1208 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1209 5813d178 2019-03-09 stsp char *author = NULL;
1210 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1211 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1212 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1213 bb737323 2018-05-20 stsp int col, limit;
1214 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1215 ccb26ccd 2018-11-05 stsp struct tm tm;
1216 45d799e2 2018-12-23 stsp time_t committer_time;
1217 11b20872 2019-11-08 stsp struct tog_color *tc;
1219 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1220 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1221 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1222 6db9f7f6 2019-12-10 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1223 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1224 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1226 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1227 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1229 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1230 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_DATE);
1232 11b20872 2019-11-08 stsp wattr_on(view->window,
1233 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1234 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1236 11b20872 2019-11-08 stsp wattr_off(view->window,
1237 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1238 27a741e5 2019-09-11 stsp col = limit;
1239 b39d25c7 2018-07-10 stsp if (col > avail)
1240 b39d25c7 2018-07-10 stsp goto done;
1242 6570a66d 2019-11-08 stsp if (avail >= 120) {
1243 6570a66d 2019-11-08 stsp char *id_str;
1244 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1246 6570a66d 2019-11-08 stsp goto done;
1247 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1249 11b20872 2019-11-08 stsp wattr_on(view->window,
1250 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1251 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1253 11b20872 2019-11-08 stsp wattr_off(view->window,
1254 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1255 6570a66d 2019-11-08 stsp free(id_str);
1257 6570a66d 2019-11-08 stsp if (col > avail)
1258 6570a66d 2019-11-08 stsp goto done;
1261 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1262 5813d178 2019-03-09 stsp if (author == NULL) {
1263 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1264 80ddbec8 2018-04-29 stsp goto done;
1266 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1268 bb737323 2018-05-20 stsp goto done;
1269 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_AUTHOR);
1271 11b20872 2019-11-08 stsp wattr_on(view->window,
1272 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1273 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1275 11b20872 2019-11-08 stsp wattr_off(view->window,
1276 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1277 bb737323 2018-05-20 stsp col += author_width;
1278 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1279 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1281 bb737323 2018-05-20 stsp author_width++;
1283 9c2eaf34 2018-05-20 stsp if (col > avail)
1284 9c2eaf34 2018-05-20 stsp goto done;
1286 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1288 6d9fbc00 2018-04-29 stsp goto done;
1289 bb737323 2018-05-20 stsp logmsg = logmsg0;
1290 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1292 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1293 bb737323 2018-05-20 stsp if (newline)
1294 bb737323 2018-05-20 stsp *newline = '\0';
1295 bb737323 2018-05-20 stsp limit = avail - col;
1296 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1298 bb737323 2018-05-20 stsp goto done;
1299 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1300 bb737323 2018-05-20 stsp col += logmsg_width;
1301 27a741e5 2019-09-11 stsp while (col < avail) {
1302 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1306 80ddbec8 2018-04-29 stsp free(logmsg0);
1307 bb737323 2018-05-20 stsp free(wlogmsg);
1308 5813d178 2019-03-09 stsp free(author);
1309 bb737323 2018-05-20 stsp free(wauthor);
1310 80ddbec8 2018-04-29 stsp free(line);
1311 80ddbec8 2018-04-29 stsp return err;
1314 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1315 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1316 899d86c2 2018-05-10 stsp struct got_object_id *id)
1318 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1320 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1321 80ddbec8 2018-04-29 stsp if (entry == NULL)
1322 899d86c2 2018-05-10 stsp return NULL;
1324 899d86c2 2018-05-10 stsp entry->id = id;
1325 99db9666 2018-05-07 stsp entry->commit = commit;
1326 899d86c2 2018-05-10 stsp return entry;
1329 99db9666 2018-05-07 stsp static void
1330 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1332 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1334 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1335 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1336 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1337 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1338 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1339 99db9666 2018-05-07 stsp free(entry);
1342 99db9666 2018-05-07 stsp static void
1343 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1345 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1346 99db9666 2018-05-07 stsp pop_commit(commits);
1349 c4972b91 2018-05-07 stsp static const struct got_error *
1350 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1351 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1353 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1354 13add988 2019-10-15 stsp regmatch_t regmatch;
1355 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1357 13add988 2019-10-15 stsp *have_match = 0;
1359 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1361 13add988 2019-10-15 stsp return err;
1363 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1365 13add988 2019-10-15 stsp goto done;
1367 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1368 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1369 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1370 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1371 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1372 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1373 13add988 2019-10-15 stsp *have_match = 1;
1375 13add988 2019-10-15 stsp free(id_str);
1376 13add988 2019-10-15 stsp free(logmsg);
1377 13add988 2019-10-15 stsp return err;
1380 13add988 2019-10-15 stsp static const struct got_error *
1381 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1382 13add988 2019-10-15 stsp int minqueue, struct got_repository *repo, const char *path,
1383 13add988 2019-10-15 stsp int *searching, int *search_next_done, regex_t *regex)
1385 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1386 13add988 2019-10-15 stsp int nqueued = 0, have_match = 0;
1389 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1390 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1391 1a76625f 2018-10-22 stsp * while updating the display.
1393 13add988 2019-10-15 stsp while (nqueued < minqueue ||
1394 13add988 2019-10-15 stsp (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1395 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1396 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1397 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1398 1a76625f 2018-10-22 stsp int errcode;
1400 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1401 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1404 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1407 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1408 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1409 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1413 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1414 1a76625f 2018-10-22 stsp if (errcode) {
1415 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1416 13add988 2019-10-15 stsp "pthread_mutex_lock");
1420 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1421 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1422 ecb28ae0 2018-07-16 stsp nqueued++;
1423 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1425 13add988 2019-10-15 stsp if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1426 13add988 2019-10-15 stsp err = match_commit(&have_match, id, commit, regex);
1429 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1430 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1431 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1432 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1434 a93813fa 2020-01-20 mpi if (err || have_match)
1438 9ba79e04 2018-06-11 stsp return err;
1441 0553a4e3 2018-04-30 stsp static const struct got_error *
1442 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1443 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1444 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1445 11b20872 2019-11-08 stsp struct got_reflist_head *refs, const char *path, int commits_needed,
1446 11b20872 2019-11-08 stsp struct tog_colors *colors)
1448 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1449 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1450 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1451 60493ae3 2019-06-20 stsp int width;
1452 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1453 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1454 8b473291 2019-02-21 stsp char *refs_str = NULL;
1455 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1456 11b20872 2019-11-08 stsp struct tog_color *tc;
1457 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1459 e0d42f60 2018-07-22 stsp entry = first;
1460 e0d42f60 2018-07-22 stsp ncommits = 0;
1461 e0d42f60 2018-07-22 stsp while (entry) {
1462 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1463 e0d42f60 2018-07-22 stsp *selected = entry;
1466 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1467 e0d42f60 2018-07-22 stsp ncommits++;
1470 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1471 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1473 ecb28ae0 2018-07-16 stsp return err;
1474 8b473291 2019-02-21 stsp if (refs) {
1475 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, (*selected)->id,
1478 8b473291 2019-02-21 stsp goto done;
1482 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1483 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1485 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1486 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1487 60493ae3 2019-06-20 stsp commits_needed > 0 ?
1488 60493ae3 2019-06-20 stsp (view->searching && view->search_next_done == 0
1489 60493ae3 2019-06-20 stsp ? "searching..." : "loading... ") :
1490 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1491 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1492 8b473291 2019-02-21 stsp goto done;
1495 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1496 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1497 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1498 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1499 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1500 1a76625f 2018-10-22 stsp header = NULL;
1501 1a76625f 2018-10-22 stsp goto done;
1503 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1504 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1505 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1506 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1507 1a76625f 2018-10-22 stsp header = NULL;
1508 1a76625f 2018-10-22 stsp goto done;
1510 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1512 1a76625f 2018-10-22 stsp goto done;
1514 2814baeb 2018-08-01 stsp werase(view->window);
1516 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1517 a3404814 2018-09-02 stsp wstandout(view->window);
1518 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1520 11b20872 2019-11-08 stsp wattr_on(view->window,
1521 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1522 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1524 11b20872 2019-11-08 stsp wattr_off(view->window,
1525 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1526 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1527 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1530 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1531 a3404814 2018-09-02 stsp wstandend(view->window);
1532 ecb28ae0 2018-07-16 stsp free(wline);
1533 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1534 1a76625f 2018-10-22 stsp goto done;
1536 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1537 899d86c2 2018-05-10 stsp entry = first;
1538 5813d178 2019-03-09 stsp ncommits = 0;
1539 5813d178 2019-03-09 stsp while (entry) {
1540 5813d178 2019-03-09 stsp char *author;
1541 5813d178 2019-03-09 stsp wchar_t *wauthor;
1542 5813d178 2019-03-09 stsp int width;
1543 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1545 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1546 5813d178 2019-03-09 stsp if (author == NULL) {
1547 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1548 5813d178 2019-03-09 stsp goto done;
1550 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1551 27a741e5 2019-09-11 stsp date_display_cols);
1552 5813d178 2019-03-09 stsp if (author_cols < width)
1553 5813d178 2019-03-09 stsp author_cols = width;
1554 5813d178 2019-03-09 stsp free(wauthor);
1555 5813d178 2019-03-09 stsp free(author);
1556 7ca04879 2019-10-19 stsp ncommits++;
1557 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1560 5813d178 2019-03-09 stsp entry = first;
1561 899d86c2 2018-05-10 stsp *last = first;
1562 867c6645 2018-07-10 stsp ncommits = 0;
1563 899d86c2 2018-05-10 stsp while (entry) {
1564 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1566 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1567 2814baeb 2018-08-01 stsp wstandout(view->window);
1568 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1569 11b20872 2019-11-08 stsp date_display_cols, author_cols, colors);
1570 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1571 2814baeb 2018-08-01 stsp wstandend(view->window);
1573 60493ae3 2019-06-20 stsp goto done;
1574 0553a4e3 2018-04-30 stsp ncommits++;
1575 899d86c2 2018-05-10 stsp *last = entry;
1576 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1579 1a57306a 2018-09-02 stsp view_vborder(view);
1581 1a76625f 2018-10-22 stsp free(id_str);
1582 8b473291 2019-02-21 stsp free(refs_str);
1583 1a76625f 2018-10-22 stsp free(ncommits_str);
1584 1a76625f 2018-10-22 stsp free(header);
1585 80ddbec8 2018-04-29 stsp return err;
1588 07b55e75 2018-05-10 stsp static void
1589 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1590 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1591 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1593 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1594 07b55e75 2018-05-10 stsp int nscrolled = 0;
1596 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1597 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1600 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1601 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1602 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1603 07b55e75 2018-05-10 stsp if (entry) {
1604 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1605 07b55e75 2018-05-10 stsp nscrolled++;
1610 aa075928 2018-05-10 stsp static const struct got_error *
1611 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1612 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1614 5e224a3e 2019-02-22 stsp int errcode;
1615 8a42fca8 2019-02-22 stsp int max_wait = 20;
1617 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1619 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1620 5e224a3e 2019-02-22 stsp if (*log_complete)
1623 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1624 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1625 7aafa0d1 2019-02-22 stsp if (errcode)
1626 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1627 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1628 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1630 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1631 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1632 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1633 5e224a3e 2019-02-22 stsp * with few commits in history.
1635 7aafa0d1 2019-02-22 stsp return NULL;
1639 5e224a3e 2019-02-22 stsp return NULL;
1642 5e224a3e 2019-02-22 stsp static const struct got_error *
1643 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1644 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1645 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1646 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1647 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1649 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1650 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1651 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1653 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1654 5e224a3e 2019-02-22 stsp return NULL;
1656 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1657 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1659 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1660 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1662 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1663 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1664 5e224a3e 2019-02-22 stsp need_commits);
1666 5e224a3e 2019-02-22 stsp return err;
1670 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1671 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1674 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1676 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1677 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1679 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1680 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1682 dd0a52c1 2018-05-20 stsp return err;
1685 cd0acaa7 2018-05-20 stsp static const struct got_error *
1686 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1687 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1688 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1689 8b473291 2019-02-21 stsp struct got_repository *repo)
1691 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1692 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1693 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1695 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1696 15a94983 2018-12-23 stsp if (diff_view == NULL)
1697 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1699 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1700 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1701 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1702 e5a0f69f 2018-08-18 stsp if (err == NULL)
1703 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1704 cd0acaa7 2018-05-20 stsp return err;
1707 80ddbec8 2018-04-29 stsp static const struct got_error *
1708 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1709 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1711 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1713 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1714 941e9f74 2019-05-21 stsp if (parent == NULL)
1715 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1717 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1718 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1719 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1720 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1721 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1722 941e9f74 2019-05-21 stsp s->tree = subtree;
1723 941e9f74 2019-05-21 stsp s->selected = 0;
1724 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1725 941e9f74 2019-05-21 stsp return NULL;
1729 941e9f74 2019-05-21 stsp static const struct got_error *
1730 941e9f74 2019-05-21 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1731 941e9f74 2019-05-21 stsp struct commit_queue_entry *entry, const char *path,
1732 941e9f74 2019-05-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
1734 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1735 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1736 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s;
1737 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1738 b03c880f 2019-05-21 stsp char *slash, *subpath = NULL;
1739 941e9f74 2019-05-21 stsp const char *p;
1741 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1742 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1744 9343a5fb 2018-06-23 stsp return err;
1746 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1747 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1748 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1750 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1751 941e9f74 2019-05-21 stsp if (err) {
1752 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1753 941e9f74 2019-05-21 stsp return err;
1755 941e9f74 2019-05-21 stsp s = &tree_view->state.tree;
1757 941e9f74 2019-05-21 stsp *new_view = tree_view;
1759 33cbf02b 2020-01-12 stsp if (got_path_is_root_dir(path))
1760 33cbf02b 2020-01-12 stsp return NULL;
1762 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1764 941e9f74 2019-05-21 stsp while (*p) {
1765 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1766 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1767 56e0773d 2019-11-28 stsp char *te_name;
1769 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1772 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1773 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1774 941e9f74 2019-05-21 stsp if (slash == NULL)
1775 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1777 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1778 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1779 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1782 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1783 56e0773d 2019-11-28 stsp if (te == NULL) {
1784 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1785 56e0773d 2019-11-28 stsp free(te_name);
1788 56e0773d 2019-11-28 stsp free(te_name);
1789 56e0773d 2019-11-28 stsp s->selected_entry = te;
1790 56e0773d 2019-11-28 stsp s->selected = got_tree_entry_get_index(te);
1791 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1792 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1794 56e0773d 2019-11-28 stsp if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1795 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1796 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1797 67409a31 2019-05-24 stsp s->selected = 0;
1801 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1802 941e9f74 2019-05-21 stsp if (slash)
1803 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1805 941e9f74 2019-05-21 stsp subpath = strdup(path);
1806 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1807 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1811 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1816 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1817 941e9f74 2019-05-21 stsp free(tree_id);
1821 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1822 941e9f74 2019-05-21 stsp if (err) {
1823 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1826 941e9f74 2019-05-21 stsp if (slash == NULL)
1828 941e9f74 2019-05-21 stsp free(subpath);
1829 941e9f74 2019-05-21 stsp subpath = NULL;
1830 941e9f74 2019-05-21 stsp p = slash;
1833 941e9f74 2019-05-21 stsp free(subpath);
1834 1a76625f 2018-10-22 stsp return err;
1837 61266923 2020-01-14 stsp static const struct got_error *
1838 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1840 61266923 2020-01-14 stsp sigset_t sigset;
1841 61266923 2020-01-14 stsp int errcode;
1843 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1844 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1846 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1847 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1848 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1849 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
1850 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1852 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
1853 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
1854 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1856 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1857 61266923 2020-01-14 stsp if (errcode)
1858 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
1860 61266923 2020-01-14 stsp return NULL;
1863 1a76625f 2018-10-22 stsp static void *
1864 1a76625f 2018-10-22 stsp log_thread(void *arg)
1866 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1867 1a76625f 2018-10-22 stsp int errcode = 0;
1868 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1869 1a76625f 2018-10-22 stsp int done = 0;
1871 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
1873 61266923 2020-01-14 stsp return (void *)err;
1875 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1876 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1877 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1878 13add988 2019-10-15 stsp a->regex);
1879 1a76625f 2018-10-22 stsp if (err) {
1880 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1881 1a76625f 2018-10-22 stsp return (void *)err;
1882 1a76625f 2018-10-22 stsp err = NULL;
1884 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1885 1a76625f 2018-10-22 stsp a->commits_needed--;
1887 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1888 3abe8080 2019-04-10 stsp if (errcode) {
1889 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1890 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1892 3abe8080 2019-04-10 stsp } else if (*a->quit)
1894 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1895 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1896 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1897 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1901 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1902 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1903 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1904 1a76625f 2018-10-22 stsp &tog_mutex);
1905 1a76625f 2018-10-22 stsp if (errcode)
1906 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1907 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1910 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1911 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1912 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1913 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1915 3abe8080 2019-04-10 stsp a->log_complete = 1;
1916 1a76625f 2018-10-22 stsp return (void *)err;
1919 1a76625f 2018-10-22 stsp static const struct got_error *
1920 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1922 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1923 1a76625f 2018-10-22 stsp int errcode;
1925 1a76625f 2018-10-22 stsp if (s->thread) {
1926 1a76625f 2018-10-22 stsp s->quit = 1;
1927 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1928 1a76625f 2018-10-22 stsp if (errcode)
1929 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1930 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1931 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1932 1a76625f 2018-10-22 stsp if (errcode)
1933 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1934 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1935 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1936 1a76625f 2018-10-22 stsp if (errcode)
1937 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1938 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1939 1a76625f 2018-10-22 stsp if (errcode)
1940 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1941 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1942 1a76625f 2018-10-22 stsp s->thread = NULL;
1945 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1946 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1947 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1949 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1950 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1951 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1954 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1955 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1956 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1959 9343a5fb 2018-06-23 stsp return err;
1962 9343a5fb 2018-06-23 stsp static const struct got_error *
1963 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1965 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1966 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1968 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1969 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1970 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1971 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1972 1a76625f 2018-10-22 stsp free(s->start_id);
1973 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1974 1a76625f 2018-10-22 stsp return err;
1977 1a76625f 2018-10-22 stsp static const struct got_error *
1978 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
1980 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1982 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
1983 96e2b566 2019-07-08 stsp s->search_entry = NULL;
1984 60493ae3 2019-06-20 stsp return NULL;
1987 60493ae3 2019-06-20 stsp static const struct got_error *
1988 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
1990 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
1991 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1992 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
1994 60493ae3 2019-06-20 stsp if (!view->searching) {
1995 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1996 60493ae3 2019-06-20 stsp return NULL;
1999 96e2b566 2019-07-08 stsp if (s->search_entry) {
2000 13add988 2019-10-15 stsp int errcode, ch;
2001 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2002 13add988 2019-10-15 stsp if (errcode)
2003 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2004 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2005 13add988 2019-10-15 stsp ch = wgetch(view->window);
2006 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2007 13add988 2019-10-15 stsp if (errcode)
2008 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2009 13add988 2019-10-15 stsp "pthread_mutex_lock");
2010 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2011 678cbce5 2019-07-28 stsp view->search_next_done = 1;
2012 678cbce5 2019-07-28 stsp return NULL;
2014 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2015 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2017 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2018 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2019 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2020 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2021 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2023 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
2024 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2026 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2027 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2029 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2032 60493ae3 2019-06-20 stsp while (1) {
2033 13add988 2019-10-15 stsp int have_match = 0;
2035 60493ae3 2019-06-20 stsp if (entry == NULL) {
2036 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2037 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2038 f801134a 2019-06-25 stsp view->search_next_done = 1;
2039 f801134a 2019-06-25 stsp return NULL;
2042 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2043 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2044 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2046 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2047 57b33b64 2019-07-08 stsp return trigger_log_thread(1,
2048 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
2049 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
2050 f801134a 2019-06-25 stsp &s->thread_args.need_commits);
2053 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2054 13add988 2019-10-15 stsp &view->regex);
2057 13add988 2019-10-15 stsp if (have_match) {
2058 60493ae3 2019-06-20 stsp view->search_next_done = 1;
2059 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2063 96e2b566 2019-07-08 stsp s->search_entry = entry;
2064 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2065 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2067 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2070 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2071 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2072 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2073 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2075 60493ae3 2019-06-20 stsp return err;
2078 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2079 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2081 60493ae3 2019-06-20 stsp return err;
2086 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2088 60493ae3 2019-06-20 stsp return NULL;
2091 60493ae3 2019-06-20 stsp static const struct got_error *
2092 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2093 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
2094 d01904d4 2019-06-25 stsp const char *head_ref_name, const char *path, int check_disk)
2096 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2097 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2098 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2099 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2100 1a76625f 2018-10-22 stsp int errcode;
2102 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2103 ecb28ae0 2018-07-16 stsp if (err != NULL)
2104 ecb28ae0 2018-07-16 stsp goto done;
2106 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2107 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2108 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2110 8b473291 2019-02-21 stsp s->refs = refs;
2111 fb2756b9 2018-08-04 stsp s->repo = repo;
2112 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
2113 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2114 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2115 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2116 5036bf37 2018-09-24 stsp goto done;
2119 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2120 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2121 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2122 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2124 11b20872 2019-11-08 stsp goto done;
2125 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2126 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2127 11b20872 2019-11-08 stsp if (err) {
2128 11b20872 2019-11-08 stsp free_colors(&s->colors);
2129 11b20872 2019-11-08 stsp goto done;
2131 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2132 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2133 11b20872 2019-11-08 stsp if (err) {
2134 11b20872 2019-11-08 stsp free_colors(&s->colors);
2135 11b20872 2019-11-08 stsp goto done;
2139 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2140 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2141 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2142 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2143 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2145 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2147 1a76625f 2018-10-22 stsp goto done;
2148 3d509237 2020-01-04 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path, 0);
2150 1a76625f 2018-10-22 stsp goto done;
2151 c5b78334 2020-01-12 stsp err = got_commit_graph_iter_start(thread_graph,
2152 c5b78334 2020-01-12 stsp s->start_id, s->repo, NULL, NULL);
2154 c5b78334 2020-01-12 stsp goto done;
2156 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2157 1a76625f 2018-10-22 stsp if (errcode) {
2158 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2159 1a76625f 2018-10-22 stsp goto done;
2162 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2163 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2164 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2165 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2166 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2167 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2168 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2169 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2170 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2171 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2172 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2173 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2174 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2177 1a76625f 2018-10-22 stsp close_log_view(view);
2178 ba4f502b 2018-08-04 stsp return err;
2181 e5a0f69f 2018-08-18 stsp static const struct got_error *
2182 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2184 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2186 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2187 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2188 2b380cc8 2018-10-24 stsp &s->thread_args);
2189 2b380cc8 2018-10-24 stsp if (errcode)
2190 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2193 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
2194 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
2195 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
2196 11b20872 2019-11-08 stsp s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2199 e5a0f69f 2018-08-18 stsp static const struct got_error *
2200 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2201 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2203 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2204 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2205 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2206 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2207 669b5ffa 2018-10-07 stsp int begin_x = 0;
2208 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2210 e5a0f69f 2018-08-18 stsp switch (ch) {
2212 1e37a5c2 2019-05-12 jcs s->quit = 1;
2215 1e37a5c2 2019-05-12 jcs case KEY_UP:
2218 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2220 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2221 1e37a5c2 2019-05-12 jcs s->selected--;
2223 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
2224 1144d21a 2019-06-21 stsp &s->commits);
2226 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2227 a4292ac5 2019-05-12 jcs case CTRL('b'):
2228 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2230 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
2231 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
2232 1e37a5c2 2019-05-12 jcs s->selected = 0;
2235 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
2236 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
2239 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2242 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2244 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2245 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2246 1e37a5c2 2019-05-12 jcs s->selected++;
2249 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
2250 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
2251 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
2252 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2253 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2255 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2256 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2257 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2258 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2259 1e37a5c2 2019-05-12 jcs if (first == NULL)
2261 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
2262 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
2263 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
2264 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2265 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2268 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2269 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2270 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2271 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2272 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2273 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2275 1e37a5c2 2019-05-12 jcs err = NULL;
2278 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2279 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2280 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2281 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2282 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2284 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2287 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2289 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2290 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2291 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2292 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2293 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
2296 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2297 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2299 1e37a5c2 2019-05-12 jcs return err;
2300 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2302 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2305 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2306 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2308 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2311 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2313 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2314 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2315 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2316 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2319 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2320 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2322 1e37a5c2 2019-05-12 jcs return err;
2323 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2325 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2328 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2329 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2331 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2333 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2334 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2336 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2337 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2338 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2340 1e37a5c2 2019-05-12 jcs return err;
2341 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2342 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2343 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2344 638f9024 2019-05-13 stsp return got_error_from_errno(
2345 1e37a5c2 2019-05-12 jcs "view_open");
2346 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2347 d01904d4 2019-06-25 stsp s->repo, s->head_ref_name, parent_path, 0);
2349 1e37a5c2 2019-05-12 jcs return err;;
2350 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2351 1e37a5c2 2019-05-12 jcs *new_view = lv;
2353 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2354 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2356 1e37a5c2 2019-05-12 jcs return NULL;
2359 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2360 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2362 d01904d4 2019-06-25 stsp return err;
2363 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2364 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2365 d01904d4 2019-06-25 stsp if (lv == NULL)
2366 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2367 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&start_id, NULL,
2368 71a27632 2020-01-15 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2369 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2370 ef129c5e 2019-08-03 stsp if (err) {
2371 ef129c5e 2019-08-03 stsp view_close(lv);
2372 d01904d4 2019-06-25 stsp return err;
2374 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2375 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2376 e6b23735 2019-10-04 stsp free(start_id);
2377 e6b23735 2019-10-04 stsp view_close(lv);
2378 e6b23735 2019-10-04 stsp return got_error_from_errno("strdup");
2380 e6b23735 2019-10-04 stsp got_ref_list_free(s->refs);
2381 e6b23735 2019-10-04 stsp err = got_ref_list(s->refs, s->repo, NULL,
2382 e6b23735 2019-10-04 stsp got_ref_cmp_by_name, NULL);
2383 e6b23735 2019-10-04 stsp if (err) {
2384 d01904d4 2019-06-25 stsp free(start_id);
2385 ef129c5e 2019-08-03 stsp view_close(lv);
2386 50284065 2019-10-04 stsp return err;
2388 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2389 d01904d4 2019-06-25 stsp s->head_ref_name, in_repo_path, 0);
2390 ef129c5e 2019-08-03 stsp if (err) {
2391 ef129c5e 2019-08-03 stsp free(start_id);
2392 ef129c5e 2019-08-03 stsp view_close(lv);
2393 d01904d4 2019-06-25 stsp return err;;
2395 d01904d4 2019-06-25 stsp *dead_view = view;
2396 d01904d4 2019-06-25 stsp *new_view = lv;
2402 80ddbec8 2018-04-29 stsp return err;
2405 4ed7e80c 2018-05-20 stsp static const struct got_error *
2406 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2408 c2db6724 2019-01-04 stsp const struct got_error *error;
2410 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2411 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2412 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2414 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2415 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2417 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2418 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2420 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2421 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2423 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2424 c2db6724 2019-01-04 stsp if (error != NULL)
2425 c2db6724 2019-01-04 stsp return error;
2427 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2428 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2430 c2db6724 2019-01-04 stsp return NULL;
2433 a915003a 2019-02-05 stsp static void
2434 a915003a 2019-02-05 stsp init_curses(void)
2436 a915003a 2019-02-05 stsp initscr();
2438 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2441 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2442 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2443 a915003a 2019-02-05 stsp curs_set(0);
2444 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2445 6d17833f 2019-11-08 stsp start_color();
2446 6d17833f 2019-11-08 stsp use_default_colors();
2448 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2449 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2450 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2453 c2db6724 2019-01-04 stsp static const struct got_error *
2454 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2456 80ddbec8 2018-04-29 stsp const struct got_error *error;
2457 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2458 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2459 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2460 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2461 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2462 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2464 04cc582a 2018-08-01 stsp struct tog_view *view;
2466 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2468 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2469 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2470 c2db6724 2019-01-04 stsp NULL) == -1)
2471 80ddbec8 2018-04-29 stsp err(1, "pledge");
2474 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2475 80ddbec8 2018-04-29 stsp switch (ch) {
2477 80ddbec8 2018-04-29 stsp start_commit = optarg;
2480 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2481 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2482 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2486 17020d27 2019-03-07 stsp usage_log();
2487 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2491 80ddbec8 2018-04-29 stsp argc -= optind;
2492 80ddbec8 2018-04-29 stsp argv += optind;
2494 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2495 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2496 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2497 ecb28ae0 2018-07-16 stsp goto done;
2499 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2500 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2501 963f97a1 2019-03-18 stsp goto done;
2502 963f97a1 2019-03-18 stsp error = NULL;
2504 963f97a1 2019-03-18 stsp if (argc == 0) {
2505 963f97a1 2019-03-18 stsp path = strdup("");
2506 963f97a1 2019-03-18 stsp if (path == NULL) {
2507 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2508 ecb28ae0 2018-07-16 stsp goto done;
2510 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2511 963f97a1 2019-03-18 stsp if (worktree) {
2512 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2514 963f97a1 2019-03-18 stsp if (error)
2515 963f97a1 2019-03-18 stsp goto done;
2517 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2518 963f97a1 2019-03-18 stsp if (path == NULL) {
2519 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2520 963f97a1 2019-03-18 stsp goto done;
2524 963f97a1 2019-03-18 stsp usage_log();
2526 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2527 a1fbf39a 2019-08-11 stsp if (worktree)
2528 b9d7675a 2019-08-11 stsp repo_path = strdup(
2529 b9d7675a 2019-08-11 stsp got_worktree_get_repo_path(worktree));
2531 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2533 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2534 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2535 963f97a1 2019-03-18 stsp goto done;
2538 a915003a 2019-02-05 stsp init_curses();
2540 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2541 80ddbec8 2018-04-29 stsp if (error != NULL)
2542 ecb28ae0 2018-07-16 stsp goto done;
2544 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2545 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2546 c02c541e 2019-03-29 stsp if (error)
2547 c02c541e 2019-03-29 stsp goto done;
2549 15a94983 2018-12-23 stsp if (start_commit == NULL)
2550 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, worktree ?
2551 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2552 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2554 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, start_commit,
2555 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2556 80ddbec8 2018-04-29 stsp if (error != NULL)
2557 8b473291 2019-02-21 stsp goto done;
2559 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2560 8b473291 2019-02-21 stsp if (error)
2561 ecb28ae0 2018-07-16 stsp goto done;
2563 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2564 04cc582a 2018-08-01 stsp if (view == NULL) {
2565 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2566 04cc582a 2018-08-01 stsp goto done;
2568 2fc00ff4 2019-08-31 stsp if (worktree) {
2569 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2570 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2571 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2572 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2573 2fc00ff4 2019-08-31 stsp goto done;
2576 2fc00ff4 2019-08-31 stsp error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2578 ba4f502b 2018-08-04 stsp if (error)
2579 ba4f502b 2018-08-04 stsp goto done;
2580 2fc00ff4 2019-08-31 stsp if (worktree) {
2581 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2582 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2583 2fc00ff4 2019-08-31 stsp worktree = NULL;
2585 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2587 ecb28ae0 2018-07-16 stsp free(repo_path);
2588 ecb28ae0 2018-07-16 stsp free(cwd);
2589 ecb28ae0 2018-07-16 stsp free(path);
2590 899d86c2 2018-05-10 stsp free(start_id);
2591 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2593 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2594 ec142235 2019-03-07 stsp if (worktree)
2595 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2596 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2597 80ddbec8 2018-04-29 stsp return error;
2600 4ed7e80c 2018-05-20 stsp __dead static void
2601 9f7d7167 2018-04-29 stsp usage_diff(void)
2604 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2605 9f7d7167 2018-04-29 stsp getprogname());
2609 b304db33 2018-05-20 stsp static char *
2610 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2612 b304db33 2018-05-20 stsp char *line;
2613 b304db33 2018-05-20 stsp size_t linelen;
2614 b304db33 2018-05-20 stsp size_t lineno;
2615 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2617 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2619 b304db33 2018-05-20 stsp *len = linelen;
2620 b304db33 2018-05-20 stsp return line;
2623 6d17833f 2019-11-08 stsp static int
2624 6d17833f 2019-11-08 stsp match_line(const char *line, regex_t *regex)
2626 6d17833f 2019-11-08 stsp regmatch_t regmatch;
2628 6d17833f 2019-11-08 stsp return regexec(regex, line, 1, ®match, 0) == 0;
2631 f26dddb7 2019-11-08 stsp struct tog_color *
2632 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2634 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2636 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2637 6d17833f 2019-11-08 stsp if (match_line(line, &tc->regex))
2638 6d17833f 2019-11-08 stsp return tc;
2641 6d17833f 2019-11-08 stsp return NULL;
2644 4ed7e80c 2018-05-20 stsp static const struct got_error *
2645 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2646 6d17833f 2019-11-08 stsp int *last_displayed_line, int *eof, int max_lines, char *header,
2647 f26dddb7 2019-11-08 stsp struct tog_colors *colors)
2649 61e69b96 2018-05-20 stsp const struct got_error *err;
2650 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2651 b304db33 2018-05-20 stsp char *line;
2652 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2653 b304db33 2018-05-20 stsp size_t len;
2654 61e69b96 2018-05-20 stsp wchar_t *wline;
2655 e0b650dd 2018-05-20 stsp int width;
2657 26ed57b2 2018-05-19 stsp rewind(f);
2658 f7d12f7e 2018-08-01 stsp werase(view->window);
2660 a3404814 2018-09-02 stsp if (header) {
2661 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
2662 a3404814 2018-09-02 stsp if (err) {
2663 a3404814 2018-09-02 stsp return err;
2666 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2667 a3404814 2018-09-02 stsp wstandout(view->window);
2668 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2669 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2670 a3404814 2018-09-02 stsp wstandend(view->window);
2671 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2672 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2674 a3404814 2018-09-02 stsp if (max_lines <= 1)
2675 a3404814 2018-09-02 stsp return NULL;
2676 a3404814 2018-09-02 stsp max_lines--;
2680 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2681 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2682 26ed57b2 2018-05-19 stsp if (line == NULL) {
2686 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2687 26ed57b2 2018-05-19 stsp free(line);
2691 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2692 61e69b96 2018-05-20 stsp if (err) {
2693 61e69b96 2018-05-20 stsp free(line);
2694 61e69b96 2018-05-20 stsp return err;
2697 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
2699 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2700 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2701 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2703 6d17833f 2019-11-08 stsp wattr_off(view->window,
2704 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2705 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2706 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2707 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2708 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2709 26ed57b2 2018-05-19 stsp free(line);
2710 2550e4c3 2018-07-13 stsp free(wline);
2711 2550e4c3 2018-07-13 stsp wline = NULL;
2713 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2715 1a57306a 2018-09-02 stsp view_vborder(view);
2717 c3e9aa98 2019-05-13 jcs if (*eof) {
2718 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2719 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2720 c3e9aa98 2019-05-13 jcs nprinted++;
2723 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2725 c3e9aa98 2019-05-13 jcs return err;
2728 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2729 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2730 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2733 26ed57b2 2018-05-19 stsp return NULL;
2736 abd2672a 2018-12-23 stsp static char *
2737 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2739 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2740 09867e48 2019-08-13 stsp char *p, *s;
2742 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2743 09867e48 2019-08-13 stsp if (tm == NULL)
2744 09867e48 2019-08-13 stsp return NULL;
2745 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2746 09867e48 2019-08-13 stsp if (s == NULL)
2747 09867e48 2019-08-13 stsp return NULL;
2748 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2750 abd2672a 2018-12-23 stsp *p = '\0';
2754 4ed7e80c 2018-05-20 stsp static const struct got_error *
2755 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2756 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2758 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2759 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
2760 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2761 5943eee2 2019-08-13 stsp char *id_str = NULL, *logmsg = NULL;
2762 45d799e2 2018-12-23 stsp time_t committer_time;
2763 45d799e2 2018-12-23 stsp const char *author, *committer;
2764 8b473291 2019-02-21 stsp char *refs_str = NULL;
2766 8b473291 2019-02-21 stsp if (refs) {
2767 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
2769 8b473291 2019-02-21 stsp return err;
2772 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2774 abd2672a 2018-12-23 stsp return err;
2776 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2777 15a94983 2018-12-23 stsp if (err) {
2778 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2779 15a94983 2018-12-23 stsp goto done;
2782 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2783 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2784 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2785 abd2672a 2018-12-23 stsp goto done;
2787 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2788 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2789 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2790 abd2672a 2018-12-23 stsp goto done;
2792 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2793 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
2794 09867e48 2019-08-13 stsp if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2795 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2796 abd2672a 2018-12-23 stsp goto done;
2798 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2799 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2800 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2801 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2802 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2803 abd2672a 2018-12-23 stsp goto done;
2805 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2807 5943eee2 2019-08-13 stsp goto done;
2808 5943eee2 2019-08-13 stsp if (fprintf(outfile, "%s\n", logmsg) < 0) {
2809 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2810 abd2672a 2018-12-23 stsp goto done;
2813 abd2672a 2018-12-23 stsp free(id_str);
2814 5943eee2 2019-08-13 stsp free(logmsg);
2815 8b473291 2019-02-21 stsp free(refs_str);
2816 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2817 abd2672a 2018-12-23 stsp return err;
2820 abd2672a 2018-12-23 stsp static const struct got_error *
2821 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2823 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2824 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2825 15a94983 2018-12-23 stsp int obj_type;
2827 511a516b 2018-05-19 stsp f = got_opentemp();
2828 48ae06ee 2018-10-18 stsp if (f == NULL) {
2829 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2830 48ae06ee 2018-10-18 stsp goto done;
2832 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2833 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2834 fb43ecf1 2019-02-11 stsp goto done;
2838 15a94983 2018-12-23 stsp if (s->id1)
2839 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2841 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2843 15a94983 2018-12-23 stsp goto done;
2845 15a94983 2018-12-23 stsp switch (obj_type) {
2846 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2847 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2848 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2850 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2851 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2852 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2854 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2855 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2856 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2857 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2859 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2862 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2863 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2864 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2866 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2867 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2868 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2869 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2870 8b473291 2019-02-21 stsp s->repo, f);
2875 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2877 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2878 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2882 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2886 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2887 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2888 48ae06ee 2018-10-18 stsp return err;
2891 f5215bb9 2019-02-22 stsp static void
2892 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2894 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2895 f5215bb9 2019-02-22 stsp update_panels();
2896 f5215bb9 2019-02-22 stsp doupdate();
2899 48ae06ee 2018-10-18 stsp static const struct got_error *
2900 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2901 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2902 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2904 48ae06ee 2018-10-18 stsp const struct got_error *err;
2906 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2907 15a94983 2018-12-23 stsp int type1, type2;
2908 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2910 15a94983 2018-12-23 stsp return err;
2911 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2913 15a94983 2018-12-23 stsp return err;
2915 15a94983 2018-12-23 stsp if (type1 != type2)
2916 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2919 15a94983 2018-12-23 stsp if (id1) {
2920 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2921 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2922 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2924 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2926 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2927 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2928 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2929 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2930 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2932 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2933 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2934 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2935 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2936 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2937 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2938 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2939 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&view->state.diff.colors);
2941 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2942 bddb1296 2019-11-08 stsp err = add_color(&view->state.diff.colors,
2943 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
2944 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
2946 6d17833f 2019-11-08 stsp return err;
2947 11b20872 2019-11-08 stsp err = add_color(&view->state.diff.colors, "^\\+",
2948 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
2949 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
2950 6d17833f 2019-11-08 stsp if (err) {
2951 bddb1296 2019-11-08 stsp free_colors(&view->state.diff.colors);
2952 6d17833f 2019-11-08 stsp return err;
2954 bddb1296 2019-11-08 stsp err = add_color(&view->state.diff.colors,
2955 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
2956 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2957 6d17833f 2019-11-08 stsp if (err) {
2958 bddb1296 2019-11-08 stsp free_colors(&view->state.diff.colors);
2959 6d17833f 2019-11-08 stsp return err;
2962 bddb1296 2019-11-08 stsp err = add_color(&view->state.diff.colors,
2963 11b20872 2019-11-08 stsp "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
2964 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
2965 6d17833f 2019-11-08 stsp if (err) {
2966 bddb1296 2019-11-08 stsp free_colors(&view->state.diff.colors);
2967 6d17833f 2019-11-08 stsp return err;
2970 11b20872 2019-11-08 stsp err = add_color(&view->state.diff.colors,
2971 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
2972 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2973 11b20872 2019-11-08 stsp if (err) {
2974 11b20872 2019-11-08 stsp free_colors(&view->state.diff.colors);
2975 11b20872 2019-11-08 stsp return err;
2978 11b20872 2019-11-08 stsp err = add_color(&view->state.diff.colors,
2979 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
2980 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2981 11b20872 2019-11-08 stsp if (err) {
2982 11b20872 2019-11-08 stsp free_colors(&view->state.diff.colors);
2983 11b20872 2019-11-08 stsp return err;
2987 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2988 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2989 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2991 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2992 48ae06ee 2018-10-18 stsp if (err) {
2993 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2994 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2995 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2996 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2997 48ae06ee 2018-10-18 stsp return err;
3000 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3001 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3002 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3004 5dc9f4bc 2018-08-04 stsp return NULL;
3007 e5a0f69f 2018-08-18 stsp static const struct got_error *
3008 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3010 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3012 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
3013 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
3014 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
3015 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
3016 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
3017 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3018 bddb1296 2019-11-08 stsp free_colors(&view->state.diff.colors);
3019 e5a0f69f 2018-08-18 stsp return err;
3022 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3023 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3025 a3404814 2018-09-02 stsp const struct got_error *err;
3026 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3027 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3029 a3404814 2018-09-02 stsp if (s->id1) {
3030 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3032 a3404814 2018-09-02 stsp return err;
3034 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3036 a3404814 2018-09-02 stsp return err;
3038 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
3039 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3040 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3041 a3404814 2018-09-02 stsp free(id_str1);
3042 a3404814 2018-09-02 stsp free(id_str2);
3043 a3404814 2018-09-02 stsp return err;
3045 a3404814 2018-09-02 stsp free(id_str1);
3046 a3404814 2018-09-02 stsp free(id_str2);
3048 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
3049 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
3050 bddb1296 2019-11-08 stsp header, &s->colors);
3053 15a087fe 2019-02-21 stsp static const struct got_error *
3054 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3055 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3057 d7a04538 2019-02-21 stsp const struct got_error *err;
3058 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3059 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3060 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3062 15a087fe 2019-02-21 stsp free(s->id2);
3063 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3064 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3065 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3067 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3069 d7a04538 2019-02-21 stsp return err;
3070 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3071 15a087fe 2019-02-21 stsp free(s->id1);
3072 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3073 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3074 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3075 15a087fe 2019-02-21 stsp return NULL;
3078 0cf4efb1 2018-09-29 stsp static const struct got_error *
3079 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3080 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3082 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3083 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3084 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3085 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
3088 e5a0f69f 2018-08-18 stsp switch (ch) {
3090 1e37a5c2 2019-05-12 jcs case KEY_UP:
3091 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3092 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3094 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3095 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3096 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3099 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3100 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3101 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3104 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3105 1e37a5c2 2019-05-12 jcs if (!s->eof)
3106 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3108 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3109 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3111 00ba99a7 2019-05-12 jcs if (s->eof)
3114 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3115 1e37a5c2 2019-05-12 jcs char *line;
3116 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
3117 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3118 1e37a5c2 2019-05-12 jcs if (line == NULL)
3123 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3124 1e37a5c2 2019-05-12 jcs s->diff_context--;
3125 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3126 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3130 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3131 1e37a5c2 2019-05-12 jcs s->diff_context++;
3132 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3133 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3138 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3140 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3141 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
3142 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
3143 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3146 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3151 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3155 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3156 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3158 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3159 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3163 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3165 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3167 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3168 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
3170 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
3171 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
3172 1e37a5c2 2019-05-12 jcs update_panels();
3173 1e37a5c2 2019-05-12 jcs doupdate();
3175 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
3176 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
3177 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
3178 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
3182 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3187 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
3188 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3191 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3195 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3196 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3198 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3199 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3205 bcbd79e2 2018-08-19 stsp return err;
3208 4ed7e80c 2018-05-20 stsp static const struct got_error *
3209 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3211 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3212 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3213 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3214 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3215 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
3216 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3218 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3220 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3222 26ed57b2 2018-05-19 stsp #ifndef PROFILE
3223 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3224 eb6600df 2019-01-04 stsp NULL) == -1)
3225 26ed57b2 2018-05-19 stsp err(1, "pledge");
3228 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3229 26ed57b2 2018-05-19 stsp switch (ch) {
3231 17020d27 2019-03-07 stsp usage_diff();
3232 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3236 26ed57b2 2018-05-19 stsp argc -= optind;
3237 26ed57b2 2018-05-19 stsp argv += optind;
3239 26ed57b2 2018-05-19 stsp if (argc == 0) {
3240 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3241 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3242 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
3243 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
3244 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
3245 15a94983 2018-12-23 stsp id_str1 = argv[0];
3246 15a94983 2018-12-23 stsp id_str2 = argv[1];
3247 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
3248 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
3249 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
3250 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
3251 15a94983 2018-12-23 stsp id_str1 = argv[1];
3252 15a94983 2018-12-23 stsp id_str2 = argv[2];
3254 26ed57b2 2018-05-19 stsp usage_diff();
3256 a915003a 2019-02-05 stsp init_curses();
3258 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3259 eb6600df 2019-01-04 stsp if (error)
3260 eb6600df 2019-01-04 stsp goto done;
3262 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3263 26ed57b2 2018-05-19 stsp if (error)
3264 26ed57b2 2018-05-19 stsp goto done;
3266 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
3267 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3268 26ed57b2 2018-05-19 stsp if (error)
3269 26ed57b2 2018-05-19 stsp goto done;
3271 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
3272 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3273 26ed57b2 2018-05-19 stsp if (error)
3274 26ed57b2 2018-05-19 stsp goto done;
3276 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3277 8b473291 2019-02-21 stsp if (error)
3278 8b473291 2019-02-21 stsp goto done;
3280 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3281 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3282 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3283 ea5e7bb5 2018-08-01 stsp goto done;
3285 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3286 5dc9f4bc 2018-08-04 stsp if (error)
3287 5dc9f4bc 2018-08-04 stsp goto done;
3288 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3290 c02c541e 2019-03-29 stsp free(repo_path);
3292 921be706 2019-06-28 stsp got_repo_close(repo);
3293 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3294 26ed57b2 2018-05-19 stsp return error;
3297 4ed7e80c 2018-05-20 stsp __dead static void
3298 9f7d7167 2018-04-29 stsp usage_blame(void)
3301 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3302 9f7d7167 2018-04-29 stsp getprogname());
3306 84451b3e 2018-07-10 stsp struct tog_blame_line {
3307 84451b3e 2018-07-10 stsp int annotated;
3308 84451b3e 2018-07-10 stsp struct got_object_id *id;
3311 4ed7e80c 2018-05-20 stsp static const struct got_error *
3312 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3313 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
3314 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
3315 11b20872 2019-11-08 stsp int *last_displayed_line, int *eof, int max_lines,
3316 11b20872 2019-11-08 stsp struct tog_colors *colors)
3318 84451b3e 2018-07-10 stsp const struct got_error *err;
3319 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3320 84451b3e 2018-07-10 stsp char *line;
3321 84451b3e 2018-07-10 stsp size_t len;
3322 84451b3e 2018-07-10 stsp wchar_t *wline;
3323 27a741e5 2019-09-11 stsp int width;
3324 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3325 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3326 ab089a2a 2018-07-12 stsp char *id_str;
3327 11b20872 2019-11-08 stsp struct tog_color *tc;
3329 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
3331 ab089a2a 2018-07-12 stsp return err;
3333 84451b3e 2018-07-10 stsp rewind(f);
3334 f7d12f7e 2018-08-01 stsp werase(view->window);
3336 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3337 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3338 ab089a2a 2018-07-12 stsp free(id_str);
3339 ab089a2a 2018-07-12 stsp return err;
3342 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3343 ab089a2a 2018-07-12 stsp free(line);
3344 2550e4c3 2018-07-13 stsp line = NULL;
3346 1cae65b4 2019-09-22 stsp return err;
3347 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3348 a3404814 2018-09-02 stsp wstandout(view->window);
3349 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3351 11b20872 2019-11-08 stsp wattr_on(view->window,
3352 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3353 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3355 11b20872 2019-11-08 stsp wattr_off(view->window,
3356 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3357 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3358 a3404814 2018-09-02 stsp wstandend(view->window);
3359 2550e4c3 2018-07-13 stsp free(wline);
3360 2550e4c3 2018-07-13 stsp wline = NULL;
3361 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3362 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3364 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3365 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
3366 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
3367 ab089a2a 2018-07-12 stsp free(id_str);
3368 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3370 ab089a2a 2018-07-12 stsp free(id_str);
3371 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3372 3f60a8ef 2018-07-10 stsp free(line);
3373 2550e4c3 2018-07-13 stsp line = NULL;
3375 3f60a8ef 2018-07-10 stsp return err;
3376 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3377 2550e4c3 2018-07-13 stsp free(wline);
3378 2550e4c3 2018-07-13 stsp wline = NULL;
3379 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3380 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3383 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
3384 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
3385 84451b3e 2018-07-10 stsp if (line == NULL) {
3389 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
3390 84451b3e 2018-07-10 stsp free(line);
3394 27a741e5 2019-09-11 stsp if (view->ncols <= 9) {
3395 27a741e5 2019-09-11 stsp width = 9;
3396 27a741e5 2019-09-11 stsp wline = wcsdup(L"");
3397 27a741e5 2019-09-11 stsp if (wline == NULL)
3398 27a741e5 2019-09-11 stsp err = got_error_from_errno("wcsdup");
3400 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line,
3401 27a741e5 2019-09-11 stsp view->ncols - 9, 9);
3402 27a741e5 2019-09-11 stsp width += 9;
3404 84451b3e 2018-07-10 stsp if (err) {
3405 84451b3e 2018-07-10 stsp free(line);
3406 84451b3e 2018-07-10 stsp return err;
3409 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3410 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3412 8d0fe45a 2019-08-12 stsp if (nlines > 0) {
3413 8d0fe45a 2019-08-12 stsp blame_line = &lines[lineno - 1];
3414 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3415 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3416 27a741e5 2019-09-11 stsp !(view->focussed &&
3417 27a741e5 2019-09-11 stsp nprinted == selected_line - 1)) {
3418 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3419 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3420 8d0fe45a 2019-08-12 stsp char *id_str;
3421 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3422 8d0fe45a 2019-08-12 stsp if (err) {
3423 8d0fe45a 2019-08-12 stsp free(line);
3424 8d0fe45a 2019-08-12 stsp free(wline);
3425 8d0fe45a 2019-08-12 stsp return err;
3427 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3429 11b20872 2019-11-08 stsp wattr_on(view->window,
3430 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3431 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3433 11b20872 2019-11-08 stsp wattr_off(view->window,
3434 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3435 8d0fe45a 2019-08-12 stsp free(id_str);
3436 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3438 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3439 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3442 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3443 ee41ec32 2018-07-10 stsp prev_id = NULL;
3446 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3447 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3448 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3450 27a741e5 2019-09-11 stsp waddwstr(view->window, wline);
3451 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
3452 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
3453 84451b3e 2018-07-10 stsp if (++nprinted == 1)
3454 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
3455 84451b3e 2018-07-10 stsp free(line);
3456 2550e4c3 2018-07-13 stsp free(wline);
3457 2550e4c3 2018-07-13 stsp wline = NULL;
3459 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
3461 1a57306a 2018-09-02 stsp view_vborder(view);
3463 84451b3e 2018-07-10 stsp return NULL;
3466 84451b3e 2018-07-10 stsp static const struct got_error *
3467 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3469 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
3470 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
3471 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
3472 1a76625f 2018-10-22 stsp int errcode;
3474 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
3475 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3476 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3478 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3479 1a76625f 2018-10-22 stsp if (errcode)
3480 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3482 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3483 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3484 d68a0a7d 2018-07-10 stsp goto done;
3487 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3488 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3490 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3491 d68a0a7d 2018-07-10 stsp if (line->annotated)
3492 d68a0a7d 2018-07-10 stsp goto done;
3494 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3495 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3496 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3497 84451b3e 2018-07-10 stsp goto done;
3499 84451b3e 2018-07-10 stsp line->annotated = 1;
3501 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3502 1a76625f 2018-10-22 stsp if (errcode)
3503 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3504 84451b3e 2018-07-10 stsp return err;
3507 84451b3e 2018-07-10 stsp static void *
3508 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3510 18430de3 2018-07-10 stsp const struct got_error *err;
3511 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3512 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3513 1a76625f 2018-10-22 stsp int errcode;
3515 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
3517 61266923 2020-01-14 stsp return (void *)err;
3519 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
3520 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3521 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
3522 fc06ba56 2019-08-22 stsp err = NULL;
3524 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3525 1a76625f 2018-10-22 stsp if (errcode)
3526 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3527 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3529 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3530 c9beca56 2018-07-22 stsp ta->repo = NULL;
3531 c9beca56 2018-07-22 stsp *ta->complete = 1;
3533 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3534 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3535 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3537 18430de3 2018-07-10 stsp return (void *)err;
3540 245d91c1 2018-07-12 stsp static struct got_object_id *
3541 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3542 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
3544 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3546 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
3547 8d0fe45a 2019-08-12 stsp return NULL;
3549 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3550 245d91c1 2018-07-12 stsp if (!line->annotated)
3551 245d91c1 2018-07-12 stsp return NULL;
3553 245d91c1 2018-07-12 stsp return line->id;
3556 b880a918 2018-07-10 stsp static const struct got_error *
3557 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3559 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3562 245d91c1 2018-07-12 stsp if (blame->thread) {
3563 1a76625f 2018-10-22 stsp int errcode;
3564 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3565 1a76625f 2018-10-22 stsp if (errcode)
3566 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3567 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3568 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3569 1a76625f 2018-10-22 stsp if (errcode)
3570 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3571 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3572 1a76625f 2018-10-22 stsp if (errcode)
3573 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3574 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3575 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3576 245d91c1 2018-07-12 stsp err = NULL;
3577 245d91c1 2018-07-12 stsp blame->thread = NULL;
3579 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3580 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3581 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3583 245d91c1 2018-07-12 stsp if (blame->f) {
3584 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3585 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3586 245d91c1 2018-07-12 stsp blame->f = NULL;
3588 57670559 2018-12-23 stsp if (blame->lines) {
3589 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3590 57670559 2018-12-23 stsp free(blame->lines[i].id);
3591 57670559 2018-12-23 stsp free(blame->lines);
3592 57670559 2018-12-23 stsp blame->lines = NULL;
3594 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3595 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3597 245d91c1 2018-07-12 stsp return err;
3600 245d91c1 2018-07-12 stsp static const struct got_error *
3601 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
3603 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
3604 fc06ba56 2019-08-22 stsp int *done = arg;
3605 fc06ba56 2019-08-22 stsp int errcode;
3607 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3608 fc06ba56 2019-08-22 stsp if (errcode)
3609 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3610 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
3612 fc06ba56 2019-08-22 stsp if (*done)
3613 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
3615 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3616 fc06ba56 2019-08-22 stsp if (errcode)
3617 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3618 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
3620 fc06ba56 2019-08-22 stsp return err;
3623 fc06ba56 2019-08-22 stsp static const struct got_error *
3624 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3625 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3626 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3627 245d91c1 2018-07-12 stsp struct got_repository *repo)
3629 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3630 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3631 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3632 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3633 15a94983 2018-12-23 stsp int obj_type;
3635 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3637 15a94983 2018-12-23 stsp return err;
3639 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3641 84451b3e 2018-07-10 stsp goto done;
3643 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3644 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3645 84451b3e 2018-07-10 stsp goto done;
3648 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3650 a70480e0 2018-06-23 stsp goto done;
3651 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3652 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3653 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3654 84451b3e 2018-07-10 stsp goto done;
3656 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3657 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
3658 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
3659 84451b3e 2018-07-10 stsp goto done;
3661 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3662 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3663 b02560ec 2019-08-19 stsp blame->nlines--;
3665 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3666 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3667 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3668 84451b3e 2018-07-10 stsp goto done;
3671 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3673 bd24772e 2018-07-11 stsp goto done;
3675 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3676 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3677 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3678 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3679 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3680 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3681 245d91c1 2018-07-12 stsp goto done;
3683 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3685 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3686 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3687 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3688 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3689 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
3690 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_arg = done;
3691 245d91c1 2018-07-12 stsp *blame_complete = 0;
3695 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3696 27d434c2 2018-09-15 stsp free(obj_id);
3698 245d91c1 2018-07-12 stsp stop_blame(blame);
3699 245d91c1 2018-07-12 stsp return err;
3702 245d91c1 2018-07-12 stsp static const struct got_error *
3703 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3704 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3705 8b473291 2019-02-21 stsp struct got_repository *repo)
3707 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3708 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3710 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3712 c4843652 2019-08-12 stsp s->path = strdup(path);
3713 c4843652 2019-08-12 stsp if (s->path == NULL)
3714 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
3716 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3717 c4843652 2019-08-12 stsp if (err) {
3718 c4843652 2019-08-12 stsp free(s->path);
3719 7cbe629d 2018-08-04 stsp return err;
3722 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3723 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3724 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3725 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3726 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3727 fb2756b9 2018-08-04 stsp s->repo = repo;
3728 8b473291 2019-02-21 stsp s->refs = refs;
3729 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3730 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3732 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
3733 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3734 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3735 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3737 11b20872 2019-11-08 stsp return err;
3740 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3741 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3742 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3743 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
3744 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
3746 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3747 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3748 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3749 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3752 e5a0f69f 2018-08-18 stsp static const struct got_error *
3753 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3755 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3756 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3758 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3759 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3761 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3762 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3763 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3764 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3765 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3768 e5a0f69f 2018-08-18 stsp free(s->path);
3769 11b20872 2019-11-08 stsp free_colors(&s->colors);
3771 e5a0f69f 2018-08-18 stsp return err;
3774 7cbe629d 2018-08-04 stsp static const struct got_error *
3775 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
3777 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3779 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
3780 6c4c42e0 2019-06-24 stsp return NULL;
3783 6c4c42e0 2019-06-24 stsp static const struct got_error *
3784 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
3786 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3787 6c4c42e0 2019-06-24 stsp int lineno;
3789 6c4c42e0 2019-06-24 stsp if (!view->searching) {
3790 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3791 6c4c42e0 2019-06-24 stsp return NULL;
3794 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3795 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3796 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
3798 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
3800 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3801 6c4c42e0 2019-06-24 stsp lineno = 1;
3803 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3806 6c4c42e0 2019-06-24 stsp while (1) {
3807 6c4c42e0 2019-06-24 stsp char *line = NULL;
3808 6c4c42e0 2019-06-24 stsp off_t offset;
3809 6c4c42e0 2019-06-24 stsp size_t len;
3811 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
3812 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
3813 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3814 6c4c42e0 2019-06-24 stsp free(line);
3818 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3819 6c4c42e0 2019-06-24 stsp lineno = 1;
3821 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3824 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
3825 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3826 6c4c42e0 2019-06-24 stsp free(line);
3827 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
3829 6c4c42e0 2019-06-24 stsp free(line);
3830 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
3831 2246482e 2019-06-25 stsp if (line && match_line(line, &view->regex)) {
3832 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3833 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
3834 6c4c42e0 2019-06-24 stsp free(line);
3837 6c4c42e0 2019-06-24 stsp free(line);
3838 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3844 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3845 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
3846 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
3849 6c4c42e0 2019-06-24 stsp return NULL;
3852 6c4c42e0 2019-06-24 stsp static const struct got_error *
3853 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
3855 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3856 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
3857 2b380cc8 2018-10-24 stsp int errcode;
3859 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
3860 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3861 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
3862 2b380cc8 2018-10-24 stsp if (errcode)
3863 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3865 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
3868 51fe7530 2019-08-19 stsp if (s->blame_complete)
3869 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
3871 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3872 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3873 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
3874 11b20872 2019-11-08 stsp &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
3876 669b5ffa 2018-10-07 stsp view_vborder(view);
3877 e5a0f69f 2018-08-18 stsp return err;
3880 e5a0f69f 2018-08-18 stsp static const struct got_error *
3881 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3882 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3884 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
3885 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
3886 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3887 669b5ffa 2018-10-07 stsp int begin_x = 0;
3889 e5a0f69f 2018-08-18 stsp switch (ch) {
3891 1e37a5c2 2019-05-12 jcs s->done = 1;
3894 1e37a5c2 2019-05-12 jcs case KEY_UP:
3895 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
3896 1e37a5c2 2019-05-12 jcs s->selected_line--;
3897 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
3898 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3899 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3901 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3902 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
3903 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3906 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3907 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3908 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3910 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3913 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3914 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3915 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3916 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3917 1e37a5c2 2019-05-12 jcs s->selected_line++;
3918 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3919 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3920 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3923 1e37a5c2 2019-05-12 jcs case 'p': {
3924 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3925 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3926 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3927 1e37a5c2 2019-05-12 jcs if (id == NULL)
3929 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3930 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3931 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3932 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3933 1e37a5c2 2019-05-12 jcs int obj_type;
3934 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3935 1e37a5c2 2019-05-12 jcs s->repo, id);
3938 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3939 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3940 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3941 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3944 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3945 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3946 1e37a5c2 2019-05-12 jcs pid->id, s->path);
3947 e5a0f69f 2018-08-18 stsp if (err) {
3948 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
3949 1e37a5c2 2019-05-12 jcs err = NULL;
3950 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3953 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
3955 1e37a5c2 2019-05-12 jcs free(blob_id);
3956 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
3957 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
3958 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3961 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3963 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3965 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
3966 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
3968 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3973 1e37a5c2 2019-05-12 jcs s->done = 1;
3974 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3975 1e37a5c2 2019-05-12 jcs s->done = 0;
3976 1e37a5c2 2019-05-12 jcs if (thread_err)
3978 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3979 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
3980 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3981 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3982 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
3983 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
3988 1e37a5c2 2019-05-12 jcs case 'B': {
3989 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
3990 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
3991 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
3993 1e37a5c2 2019-05-12 jcs s->done = 1;
3994 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3995 1e37a5c2 2019-05-12 jcs s->done = 0;
3996 1e37a5c2 2019-05-12 jcs if (thread_err)
3998 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3999 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4000 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4001 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4002 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4003 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4004 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
4005 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
4010 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4011 1e37a5c2 2019-05-12 jcs case '\r': {
4012 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4013 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4014 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4015 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4016 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4017 1e37a5c2 2019-05-12 jcs if (id == NULL)
4019 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4022 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4023 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4024 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4025 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4026 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4027 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4028 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4029 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4032 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4033 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
4034 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4036 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4039 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4040 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4043 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
4045 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4048 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
4049 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4051 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4056 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4058 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4059 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4060 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4063 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4064 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4065 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4066 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4069 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4070 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4071 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4072 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4074 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4075 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4076 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4078 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4079 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4080 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4081 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4087 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4090 a70480e0 2018-06-23 stsp static const struct got_error *
4091 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4093 a70480e0 2018-06-23 stsp const struct got_error *error;
4094 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4095 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4096 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4097 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4098 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4099 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4101 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4103 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4105 a70480e0 2018-06-23 stsp #ifndef PROFILE
4106 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4107 8e94dd5b 2019-01-04 stsp NULL) == -1)
4108 a70480e0 2018-06-23 stsp err(1, "pledge");
4111 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4112 a70480e0 2018-06-23 stsp switch (ch) {
4114 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4117 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4118 69069811 2018-08-02 stsp if (repo_path == NULL)
4119 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4123 17020d27 2019-03-07 stsp usage_blame();
4124 a70480e0 2018-06-23 stsp /* NOTREACHED */
4128 a70480e0 2018-06-23 stsp argc -= optind;
4129 a70480e0 2018-06-23 stsp argv += optind;
4131 69069811 2018-08-02 stsp if (argc == 1)
4132 69069811 2018-08-02 stsp path = argv[0];
4134 a70480e0 2018-06-23 stsp usage_blame();
4136 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
4137 69069811 2018-08-02 stsp if (cwd == NULL) {
4138 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4139 69069811 2018-08-02 stsp goto done;
4141 69069811 2018-08-02 stsp if (repo_path == NULL) {
4142 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4143 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4144 69069811 2018-08-02 stsp goto done;
4146 eb41ed75 2019-02-05 stsp error = NULL;
4147 eb41ed75 2019-02-05 stsp if (worktree) {
4148 eb41ed75 2019-02-05 stsp repo_path =
4149 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4150 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
4151 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4152 eb41ed75 2019-02-05 stsp if (error)
4153 eb41ed75 2019-02-05 stsp goto done;
4155 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4156 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
4157 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4158 eb41ed75 2019-02-05 stsp goto done;
4163 a915003a 2019-02-05 stsp init_curses();
4165 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4166 c02c541e 2019-03-29 stsp if (error != NULL)
4167 8e94dd5b 2019-01-04 stsp goto done;
4169 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4170 c02c541e 2019-03-29 stsp if (error)
4171 92205607 2019-01-04 stsp goto done;
4173 eb41ed75 2019-02-05 stsp if (worktree) {
4174 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4175 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4176 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4177 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4178 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4179 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4180 eb41ed75 2019-02-05 stsp path) == -1) {
4181 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4182 eb41ed75 2019-02-05 stsp goto done;
4184 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4187 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4189 eb41ed75 2019-02-05 stsp if (error)
4190 69069811 2018-08-02 stsp goto done;
4192 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4193 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4194 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4195 a70480e0 2018-06-23 stsp if (error != NULL)
4196 66b4983c 2018-06-23 stsp goto done;
4197 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4198 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4200 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4201 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4203 a19e88aa 2018-06-23 stsp if (error != NULL)
4204 8b473291 2019-02-21 stsp goto done;
4206 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4207 8b473291 2019-02-21 stsp if (error)
4208 a19e88aa 2018-06-23 stsp goto done;
4210 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4211 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4212 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4213 e1cd8fed 2018-08-01 stsp goto done;
4215 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4216 7cbe629d 2018-08-04 stsp if (error)
4217 7cbe629d 2018-08-04 stsp goto done;
4218 12314ad4 2019-08-31 stsp if (worktree) {
4219 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4220 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4221 12314ad4 2019-08-31 stsp worktree = NULL;
4223 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4225 69069811 2018-08-02 stsp free(repo_path);
4226 69069811 2018-08-02 stsp free(cwd);
4227 a70480e0 2018-06-23 stsp free(commit_id);
4228 eb41ed75 2019-02-05 stsp if (worktree)
4229 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4231 a70480e0 2018-06-23 stsp got_repo_close(repo);
4232 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4233 a70480e0 2018-06-23 stsp return error;
4236 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4237 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
4238 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
4239 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
4240 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
4241 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
4242 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int selected, int limit,
4243 f26dddb7 2019-11-08 stsp int isroot, struct tog_colors *colors)
4245 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4246 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4247 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4248 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4249 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4251 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
4253 f7d12f7e 2018-08-01 stsp werase(view->window);
4255 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4256 ffd1d5e5 2018-06-23 stsp return NULL;
4258 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, label, view->ncols, 0);
4260 ffd1d5e5 2018-06-23 stsp return err;
4261 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4262 a3404814 2018-09-02 stsp wstandout(view->window);
4263 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
4265 11b20872 2019-11-08 stsp wattr_on(view->window,
4266 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4267 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4269 11b20872 2019-11-08 stsp wattr_off(view->window,
4270 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4271 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4272 a3404814 2018-09-02 stsp wstandend(view->window);
4273 2550e4c3 2018-07-13 stsp free(wline);
4274 2550e4c3 2018-07-13 stsp wline = NULL;
4275 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4276 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4277 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4278 ffd1d5e5 2018-06-23 stsp return NULL;
4279 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4281 ce52c690 2018-06-23 stsp return err;
4282 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4283 2550e4c3 2018-07-13 stsp free(wline);
4284 2550e4c3 2018-07-13 stsp wline = NULL;
4285 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4286 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4287 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4288 ffd1d5e5 2018-06-23 stsp return NULL;
4289 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4290 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4291 a1eca9bb 2018-06-23 stsp return NULL;
4293 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
4294 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(tree);
4295 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
4296 0cf4efb1 2018-09-29 stsp if (view->focussed)
4297 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4298 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
4300 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4301 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
4302 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4303 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4304 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4305 ffd1d5e5 2018-06-23 stsp return NULL;
4309 56e0773d 2019-11-28 stsp te = *first_displayed_entry;
4312 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4313 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4314 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
4315 848d6979 2019-08-12 stsp const char *modestr = "";
4316 56e0773d 2019-11-28 stsp mode_t mode;
4318 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4319 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4321 1d13200f 2018-07-12 stsp if (show_ids) {
4322 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4323 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4325 638f9024 2019-05-13 stsp return got_error_from_errno(
4326 230a42bd 2019-05-11 jcs "got_object_id_str");
4328 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4329 63c5ca5d 2019-08-24 stsp modestr = "$";
4330 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
4331 848d6979 2019-08-12 stsp modestr = "@";
4332 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4333 848d6979 2019-08-12 stsp modestr = "/";
4334 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4335 848d6979 2019-08-12 stsp modestr = "*";
4336 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4337 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te), modestr) == -1) {
4338 1d13200f 2018-07-12 stsp free(id_str);
4339 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4341 1d13200f 2018-07-12 stsp free(id_str);
4342 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4343 ffd1d5e5 2018-06-23 stsp if (err) {
4344 ffd1d5e5 2018-06-23 stsp free(line);
4347 ffd1d5e5 2018-06-23 stsp if (n == selected) {
4348 0cf4efb1 2018-09-29 stsp if (view->focussed)
4349 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4350 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
4352 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
4354 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4355 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4356 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4358 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4359 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4360 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4361 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4362 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
4363 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4364 ffd1d5e5 2018-06-23 stsp free(line);
4365 2550e4c3 2018-07-13 stsp free(wline);
4366 2550e4c3 2018-07-13 stsp wline = NULL;
4368 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4369 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
4370 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4374 ffd1d5e5 2018-06-23 stsp return err;
4377 ffd1d5e5 2018-06-23 stsp static void
4378 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
4379 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
4380 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int isroot)
4382 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4385 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
4388 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, 0);
4389 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
4390 ffd1d5e5 2018-06-23 stsp if (!isroot)
4391 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4396 56e0773d 2019-11-28 stsp while (*first_displayed_entry && i < maxscroll) {
4397 56e0773d 2019-11-28 stsp *first_displayed_entry = got_tree_entry_get_prev(tree,
4398 56e0773d 2019-11-28 stsp *first_displayed_entry);
4401 56e0773d 2019-11-28 stsp if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4402 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4405 768394f3 2019-01-24 stsp static int
4406 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4407 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
4408 56e0773d 2019-11-28 stsp struct got_tree_object *tree)
4410 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4411 ffd1d5e5 2018-06-23 stsp int n = 0;
4413 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
4414 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, *first_displayed_entry);
4416 56e0773d 2019-11-28 stsp next = got_object_tree_get_first_entry(tree);
4418 768394f3 2019-01-24 stsp last = last_displayed_entry;
4419 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4420 56e0773d 2019-11-28 stsp last = got_tree_entry_get_next(tree, last);
4421 768394f3 2019-01-24 stsp if (last) {
4422 768394f3 2019-01-24 stsp *first_displayed_entry = next;
4423 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, next);
4429 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4430 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4431 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4433 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4434 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4435 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4437 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4438 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
4439 56e0773d 2019-11-28 stsp + 1 /* slash */;
4441 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
4443 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4444 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4445 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4447 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4448 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4449 d9765a41 2018-06-23 stsp while (pt) {
4450 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
4451 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
4452 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4453 cb2ebc8a 2018-06-23 stsp goto done;
4455 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
4456 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4457 cb2ebc8a 2018-06-23 stsp goto done;
4459 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4462 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4463 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4464 ce52c690 2018-06-23 stsp goto done;
4468 ce52c690 2018-06-23 stsp if (err) {
4469 ce52c690 2018-06-23 stsp free(*path);
4470 ce52c690 2018-06-23 stsp *path = NULL;
4472 ce52c690 2018-06-23 stsp return err;
4475 ce52c690 2018-06-23 stsp static const struct got_error *
4476 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
4477 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4478 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4479 8b473291 2019-02-21 stsp struct got_repository *repo)
4481 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
4482 ce52c690 2018-06-23 stsp char *path;
4483 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
4485 a0de39f3 2019-08-09 stsp *new_view = NULL;
4487 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
4489 ce52c690 2018-06-23 stsp return err;
4491 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4492 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
4493 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
4494 83ce39e3 2019-08-12 stsp goto done;
4497 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
4498 e5a0f69f 2018-08-18 stsp if (err) {
4499 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
4500 fc06ba56 2019-08-22 stsp err = NULL;
4501 e5a0f69f 2018-08-18 stsp view_close(blame_view);
4503 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
4505 83ce39e3 2019-08-12 stsp free(path);
4506 69efd4c4 2018-07-18 stsp return err;
4509 69efd4c4 2018-07-18 stsp static const struct got_error *
4510 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
4511 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4512 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4513 8b473291 2019-02-21 stsp struct got_repository *repo)
4515 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
4516 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
4517 69efd4c4 2018-07-18 stsp char *path;
4519 a0de39f3 2019-08-09 stsp *new_view = NULL;
4521 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4522 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
4523 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
4525 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
4527 69efd4c4 2018-07-18 stsp return err;
4529 d01904d4 2019-06-25 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4531 e5a0f69f 2018-08-18 stsp view_close(log_view);
4533 e5a0f69f 2018-08-18 stsp *new_view = log_view;
4534 cb2ebc8a 2018-06-23 stsp free(path);
4535 cb2ebc8a 2018-06-23 stsp return err;
4538 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4539 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
4540 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4541 8b473291 2019-02-21 stsp struct got_repository *repo)
4543 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4544 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
4545 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4547 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
4549 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
4550 ffd1d5e5 2018-06-23 stsp if (err != NULL)
4551 ffd1d5e5 2018-06-23 stsp goto done;
4553 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4554 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4555 ffd1d5e5 2018-06-23 stsp goto done;
4558 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
4559 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4560 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4561 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
4562 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
4563 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4564 6484ec90 2018-09-29 stsp goto done;
4566 8b473291 2019-02-21 stsp s->refs = refs;
4567 fb2756b9 2018-08-04 stsp s->repo = repo;
4569 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4571 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4572 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
4573 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
4574 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4576 c0b01bdb 2019-11-08 stsp goto done;
4577 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4578 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
4579 c0b01bdb 2019-11-08 stsp if (err) {
4580 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4581 c0b01bdb 2019-11-08 stsp goto done;
4583 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
4584 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
4585 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4586 c0b01bdb 2019-11-08 stsp if (err) {
4587 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4588 c0b01bdb 2019-11-08 stsp goto done;
4591 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
4592 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
4593 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4594 11b20872 2019-11-08 stsp if (err) {
4595 11b20872 2019-11-08 stsp free_colors(&s->colors);
4596 11b20872 2019-11-08 stsp goto done;
4599 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4600 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4601 c0b01bdb 2019-11-08 stsp if (err) {
4602 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4603 c0b01bdb 2019-11-08 stsp goto done;
4607 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
4608 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
4609 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
4610 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
4611 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
4613 ad80ab7b 2018-08-04 stsp free(commit_id_str);
4614 6484ec90 2018-09-29 stsp if (err) {
4615 fb2756b9 2018-08-04 stsp free(s->tree_label);
4616 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4618 ad80ab7b 2018-08-04 stsp return err;
4621 e5a0f69f 2018-08-18 stsp static const struct got_error *
4622 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
4624 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4626 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4627 fb2756b9 2018-08-04 stsp free(s->tree_label);
4628 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4629 6484ec90 2018-09-29 stsp free(s->commit_id);
4630 6484ec90 2018-09-29 stsp s->commit_id = NULL;
4631 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
4632 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
4633 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
4634 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
4635 ad80ab7b 2018-08-04 stsp free(parent);
4638 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
4639 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
4640 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
4642 7c32bd05 2019-06-22 stsp return NULL;
4645 7c32bd05 2019-06-22 stsp static const struct got_error *
4646 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
4648 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4650 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
4651 7c32bd05 2019-06-22 stsp return NULL;
4654 7c32bd05 2019-06-22 stsp static int
4655 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4657 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
4659 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4663 7c32bd05 2019-06-22 stsp static const struct got_error *
4664 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
4666 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4667 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
4669 7c32bd05 2019-06-22 stsp if (!view->searching) {
4670 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4671 7c32bd05 2019-06-22 stsp return NULL;
4674 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4675 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
4676 7c32bd05 2019-06-22 stsp if (s->selected_entry)
4677 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
4678 56e0773d 2019-11-28 stsp s->selected_entry);
4680 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4682 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
4683 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4685 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
4686 56e0773d 2019-11-28 stsp s->selected_entry);
4689 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4690 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4692 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4695 7c32bd05 2019-06-22 stsp while (1) {
4696 56e0773d 2019-11-28 stsp if (te == NULL) {
4697 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
4698 ac66afb8 2019-06-24 stsp view->search_next_done = 1;
4699 ac66afb8 2019-06-24 stsp return NULL;
4701 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4702 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4704 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4707 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
4708 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4709 56e0773d 2019-11-28 stsp s->matched_entry = te;
4713 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4714 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
4716 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
4719 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4720 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
4721 7c32bd05 2019-06-22 stsp s->selected = 0;
4724 e5a0f69f 2018-08-18 stsp return NULL;
4727 ad80ab7b 2018-08-04 stsp static const struct got_error *
4728 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
4730 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
4731 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4732 e5a0f69f 2018-08-18 stsp char *parent_path;
4734 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
4736 e5a0f69f 2018-08-18 stsp return err;
4738 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
4739 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
4740 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4741 56e0773d 2019-11-28 stsp s->tree, s->selected, view->nlines, s->tree == s->root,
4742 bddb1296 2019-11-08 stsp &s->colors);
4743 e5a0f69f 2018-08-18 stsp free(parent_path);
4745 669b5ffa 2018-10-07 stsp view_vborder(view);
4746 e5a0f69f 2018-08-18 stsp return err;
4749 e5a0f69f 2018-08-18 stsp static const struct got_error *
4750 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4751 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4753 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4754 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
4755 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
4756 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
4758 e5a0f69f 2018-08-18 stsp switch (ch) {
4760 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
4763 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
4765 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4766 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4767 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
4768 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
4769 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
4770 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4771 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4773 1e37a5c2 2019-05-12 jcs return err;
4774 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
4776 1e37a5c2 2019-05-12 jcs view_close(log_view);
4779 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
4780 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4782 1e37a5c2 2019-05-12 jcs *new_view = log_view;
4785 1e37a5c2 2019-05-12 jcs case KEY_UP:
4786 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
4787 1e37a5c2 2019-05-12 jcs s->selected--;
4788 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
4791 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
4793 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
4794 56e0773d 2019-11-28 stsp s->tree, s->tree == s->root);
4796 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4797 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
4798 56e0773d 2019-11-28 stsp MAX(0, view->nlines - 4 - s->selected), s->tree,
4799 1e37a5c2 2019-05-12 jcs s->tree == s->root);
4800 1e37a5c2 2019-05-12 jcs s->selected = 0;
4801 56e0773d 2019-11-28 stsp if (got_object_tree_get_first_entry(s->tree) ==
4802 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
4803 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
4806 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4807 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
4808 1e37a5c2 2019-05-12 jcs s->selected++;
4811 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4813 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
4815 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
4816 56e0773d 2019-11-28 stsp s->last_displayed_entry, s->tree);
4818 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4819 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4821 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
4822 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
4823 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4826 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
4827 56e0773d 2019-11-28 stsp view->nlines, s->last_displayed_entry, s->tree);
4828 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
4829 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
4830 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
4831 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
4833 1e37a5c2 2019-05-12 jcs ndisplayed++;
4834 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
4835 1e37a5c2 2019-05-12 jcs } while (te);
4836 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
4839 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4841 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4842 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4843 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
4844 1e37a5c2 2019-05-12 jcs /* user selected '..' */
4845 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
4847 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
4848 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
4850 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
4851 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
4852 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
4853 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
4854 1e37a5c2 2019-05-12 jcs s->selected_entry =
4855 1e37a5c2 2019-05-12 jcs parent->selected_entry;
4856 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
4857 1e37a5c2 2019-05-12 jcs free(parent);
4858 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
4859 56e0773d 2019-11-28 stsp s->selected_entry))) {
4860 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
4861 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
4862 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
4865 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
4866 941e9f74 2019-05-21 stsp if (err) {
4867 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
4870 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
4871 56e0773d 2019-11-28 stsp s->selected_entry))) {
4872 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
4873 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
4874 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
4876 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
4877 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
4878 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
4881 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
4882 669b5ffa 2018-10-07 stsp err = view_close_child(view);
4884 669b5ffa 2018-10-07 stsp return err;
4885 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
4886 669b5ffa 2018-10-07 stsp if (err) {
4887 1e37a5c2 2019-05-12 jcs view_close(blame_view);
4890 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
4891 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
4893 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
4896 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4897 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
4898 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4904 ffd1d5e5 2018-06-23 stsp return err;
4907 ffd1d5e5 2018-06-23 stsp __dead static void
4908 ffd1d5e5 2018-06-23 stsp usage_tree(void)
4911 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4912 ffd1d5e5 2018-06-23 stsp getprogname());
4916 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4917 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
4919 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
4920 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
4921 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4922 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
4923 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4924 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
4925 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
4926 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
4928 5221c383 2018-08-01 stsp struct tog_view *view;
4930 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4932 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
4933 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4934 d188b9a6 2019-01-04 stsp NULL) == -1)
4935 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
4938 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
4939 ffd1d5e5 2018-06-23 stsp switch (ch) {
4941 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
4944 17020d27 2019-03-07 stsp usage_tree();
4945 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
4949 ffd1d5e5 2018-06-23 stsp argc -= optind;
4950 ffd1d5e5 2018-06-23 stsp argv += optind;
4952 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
4953 52185f70 2019-02-05 stsp struct got_worktree *worktree;
4954 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
4955 52185f70 2019-02-05 stsp if (cwd == NULL)
4956 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
4957 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4958 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4959 52185f70 2019-02-05 stsp goto done;
4960 52185f70 2019-02-05 stsp if (worktree) {
4961 52185f70 2019-02-05 stsp free(cwd);
4962 52185f70 2019-02-05 stsp repo_path =
4963 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4964 52185f70 2019-02-05 stsp got_worktree_close(worktree);
4966 52185f70 2019-02-05 stsp repo_path = cwd;
4967 52185f70 2019-02-05 stsp if (repo_path == NULL) {
4968 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4969 52185f70 2019-02-05 stsp goto done;
4971 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
4972 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
4973 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
4974 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
4976 f13374f4 2020-01-14 stsp usage_tree();
4978 a915003a 2019-02-05 stsp init_curses();
4980 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4981 c02c541e 2019-03-29 stsp if (error != NULL)
4982 52185f70 2019-02-05 stsp goto done;
4984 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4985 c02c541e 2019-03-29 stsp if (error)
4986 52185f70 2019-02-05 stsp goto done;
4988 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4989 71a27632 2020-01-15 stsp commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
4990 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
4991 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4992 ffd1d5e5 2018-06-23 stsp goto done;
4994 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4995 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4996 ffd1d5e5 2018-06-23 stsp goto done;
4998 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
4999 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5000 ffd1d5e5 2018-06-23 stsp if (error != NULL)
5001 8b473291 2019-02-21 stsp goto done;
5003 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5004 8b473291 2019-02-21 stsp if (error)
5005 ffd1d5e5 2018-06-23 stsp goto done;
5007 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5008 5221c383 2018-08-01 stsp if (view == NULL) {
5009 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5010 5221c383 2018-08-01 stsp goto done;
5012 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
5013 ad80ab7b 2018-08-04 stsp if (error)
5014 ad80ab7b 2018-08-04 stsp goto done;
5015 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5017 52185f70 2019-02-05 stsp free(repo_path);
5018 ffd1d5e5 2018-06-23 stsp free(commit_id);
5019 ffd1d5e5 2018-06-23 stsp if (commit)
5020 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5022 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5024 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5025 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5026 ffd1d5e5 2018-06-23 stsp return error;
5029 ce5b7c56 2019-07-09 stsp static void
5030 ce5b7c56 2019-07-09 stsp list_commands(void)
5034 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
5035 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
5036 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
5037 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->name);
5039 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
5042 4ed7e80c 2018-05-20 stsp __dead static void
5043 ce5b7c56 2019-07-09 stsp usage(int hflag)
5045 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5046 53ccebc2 2019-07-30 stsp getprogname());
5047 ce5b7c56 2019-07-09 stsp if (hflag)
5048 ce5b7c56 2019-07-09 stsp list_commands();
5052 c2301be8 2018-04-30 stsp static char **
5053 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
5055 c2301be8 2018-04-30 stsp char **argv;
5056 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
5058 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
5059 c2301be8 2018-04-30 stsp if (argv == NULL)
5060 c2301be8 2018-04-30 stsp err(1, "calloc");
5061 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
5062 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
5063 e10c916e 2019-09-15 hiltjo err(1, "strdup");
5064 c2301be8 2018-04-30 stsp if (arg1) {
5065 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
5066 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
5067 e10c916e 2019-09-15 hiltjo err(1, "strdup");
5070 c2301be8 2018-04-30 stsp return argv;
5074 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
5076 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
5077 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
5078 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
5079 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
5080 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
5081 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
5082 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
5085 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
5087 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5088 9f7d7167 2018-04-29 stsp switch (ch) {
5090 9f7d7167 2018-04-29 stsp hflag = 1;
5093 53ccebc2 2019-07-30 stsp Vflag = 1;
5096 ce5b7c56 2019-07-09 stsp usage(hflag);
5097 9f7d7167 2018-04-29 stsp /* NOTREACHED */
5101 9f7d7167 2018-04-29 stsp argc -= optind;
5102 9f7d7167 2018-04-29 stsp argv += optind;
5103 9f7d7167 2018-04-29 stsp optind = 0;
5104 c2301be8 2018-04-30 stsp optreset = 1;
5106 53ccebc2 2019-07-30 stsp if (Vflag) {
5107 53ccebc2 2019-07-30 stsp got_version_print_str();
5111 c2301be8 2018-04-30 stsp if (argc == 0) {
5112 f29d3e89 2018-06-23 stsp if (hflag)
5113 ce5b7c56 2019-07-09 stsp usage(hflag);
5114 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
5115 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
5116 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
5121 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
5122 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
5123 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
5124 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
5125 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
5130 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
5131 d70c3147 2019-07-09 stsp fprintf(stderr, "%s: unknown command '%s'\n",
5132 3642c4c6 2019-07-09 stsp getprogname(), argv[0]);
5133 ce5b7c56 2019-07-09 stsp list_commands();
5138 3642c4c6 2019-07-09 stsp if (hflag)
5139 3642c4c6 2019-07-09 stsp cmd->cmd_usage();
5141 3642c4c6 2019-07-09 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5144 c2301be8 2018-04-30 stsp free(cmd_argv);
5145 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
5146 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);