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 11b20872 2019-11-08 stsp return COLOR_CYAN;
198 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
199 11b20872 2019-11-08 stsp return COLOR_BLUE;
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 errcode = pthread_mutex_unlock(&tog_mutex);
751 60493ae3 2019-06-20 stsp if (errcode)
752 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
753 60493ae3 2019-06-20 stsp "pthread_mutex_unlock");
754 60493ae3 2019-06-20 stsp pthread_yield();
755 60493ae3 2019-06-20 stsp errcode = pthread_mutex_lock(&tog_mutex);
756 60493ae3 2019-06-20 stsp if (errcode)
757 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
758 60493ae3 2019-06-20 stsp "pthread_mutex_lock");
759 60493ae3 2019-06-20 stsp view->search_next(view);
760 60493ae3 2019-06-20 stsp return NULL;
763 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
764 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
765 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
766 1a76625f 2018-10-22 stsp if (errcode)
767 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
768 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
769 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
770 1a76625f 2018-10-22 stsp if (errcode)
771 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
772 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
774 61266923 2020-01-14 stsp if (tog_sigwinch_received || tog_sigcont_received) {
775 25791caa 2018-10-24 stsp tog_resizeterm();
776 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
777 61266923 2020-01-14 stsp tog_sigcont_received = 0;
778 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
779 25791caa 2018-10-24 stsp err = view_resize(v);
781 25791caa 2018-10-24 stsp return err;
782 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
784 25791caa 2018-10-24 stsp return err;
788 e5a0f69f 2018-08-18 stsp switch (ch) {
792 1e37a5c2 2019-05-12 jcs if (view->child) {
793 1e37a5c2 2019-05-12 jcs *focus = view->child;
794 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
795 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
796 1e37a5c2 2019-05-12 jcs *focus = view->parent;
797 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
801 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
802 1e37a5c2 2019-05-12 jcs *dead = view;
808 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
809 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
811 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
812 669b5ffa 2018-10-07 stsp *focus = view->child;
813 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
814 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
816 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
819 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
820 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
822 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
823 1e37a5c2 2019-05-12 jcs *focus = view;
824 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
825 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
827 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
831 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
832 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
835 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
838 60493ae3 2019-06-20 stsp if (view->search_start)
839 2b49a8ae 2019-06-22 stsp view_search_start(view);
841 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
845 60493ae3 2019-06-20 stsp if (view->search_next && view->searching) {
846 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
847 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
848 60493ae3 2019-06-20 stsp view->search_next_done = 0;
849 60493ae3 2019-06-20 stsp view->search_next(view);
851 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
854 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
858 e5a0f69f 2018-08-18 stsp return err;
862 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
864 0cf4efb1 2018-09-29 stsp PANEL *panel;
865 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
867 669b5ffa 2018-10-07 stsp if (view->parent)
868 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
870 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
871 0cf4efb1 2018-09-29 stsp if (panel == NULL)
874 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
875 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
876 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
880 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
882 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
883 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
885 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
887 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
890 669b5ffa 2018-10-07 stsp return view->focussed;
893 bcbd79e2 2018-08-19 stsp static const struct got_error *
894 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
896 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
897 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
898 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
899 fd823528 2018-10-22 stsp int fast_refresh = 10;
900 1a76625f 2018-10-22 stsp int done = 0, errcode;
902 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
903 1a76625f 2018-10-22 stsp if (errcode)
904 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
906 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
907 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
909 a81bf10d 2018-09-29 stsp main_view = view;
910 1004088d 2018-09-29 stsp view->focussed = 1;
911 878940b7 2018-09-29 stsp err = view->show(view);
913 0cf4efb1 2018-09-29 stsp return err;
914 0cf4efb1 2018-09-29 stsp update_panels();
915 0cf4efb1 2018-09-29 stsp doupdate();
916 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
917 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
918 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
919 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
921 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
922 e4197bf9 2018-08-18 stsp view, &views);
925 e5a0f69f 2018-08-18 stsp if (dead_view) {
926 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
928 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
929 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
930 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
931 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
932 669b5ffa 2018-10-07 stsp prev = view->parent;
934 669b5ffa 2018-10-07 stsp if (dead_view->parent)
935 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
937 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
939 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
940 d01904d4 2019-06-25 stsp if (err || (dead_view == main_view && new_view == NULL))
943 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
944 0cf4efb1 2018-09-29 stsp if (focus_view)
945 0cf4efb1 2018-09-29 stsp view = focus_view;
946 669b5ffa 2018-10-07 stsp else if (prev)
947 669b5ffa 2018-10-07 stsp view = prev;
948 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
949 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
950 0cf4efb1 2018-09-29 stsp tog_view_list_head);
952 0cf4efb1 2018-09-29 stsp view = NULL;
953 669b5ffa 2018-10-07 stsp if (view) {
954 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
955 669b5ffa 2018-10-07 stsp focus_view = view->child;
957 669b5ffa 2018-10-07 stsp focus_view = view;
961 bcbd79e2 2018-08-19 stsp if (new_view) {
962 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
963 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
964 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
965 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
967 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
968 86c66b02 2018-10-18 stsp err = view_close(v);
973 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
974 fed7eaa8 2018-10-24 stsp view = new_view;
975 878940b7 2018-09-29 stsp if (focus_view == NULL)
976 878940b7 2018-09-29 stsp focus_view = new_view;
978 0cf4efb1 2018-09-29 stsp if (focus_view) {
979 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
981 0cf4efb1 2018-09-29 stsp view->focussed = 0;
982 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
983 0cf4efb1 2018-09-29 stsp view = focus_view;
984 878940b7 2018-09-29 stsp if (new_view)
985 878940b7 2018-09-29 stsp show_panel(new_view->panel);
986 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
987 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
989 669b5ffa 2018-10-07 stsp if (view) {
990 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
991 758194b5 2018-10-24 stsp view->focussed = 1;
992 758194b5 2018-10-24 stsp show_panel(view->panel);
993 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
994 758194b5 2018-10-24 stsp show_panel(view->child->panel);
995 1a76625f 2018-10-22 stsp focus_view = view;
997 669b5ffa 2018-10-07 stsp if (view->parent) {
998 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
1000 1a76625f 2018-10-22 stsp goto done;
1002 669b5ffa 2018-10-07 stsp err = view->show(view);
1004 1a76625f 2018-10-22 stsp goto done;
1005 669b5ffa 2018-10-07 stsp if (view->child) {
1006 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1008 1a76625f 2018-10-22 stsp goto done;
1010 1a76625f 2018-10-22 stsp update_panels();
1011 1a76625f 2018-10-22 stsp doupdate();
1015 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1016 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1017 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1018 e5a0f69f 2018-08-18 stsp view_close(view);
1021 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1022 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1023 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1025 e5a0f69f 2018-08-18 stsp return err;
1028 4ed7e80c 2018-05-20 stsp __dead static void
1029 9f7d7167 2018-04-29 stsp usage_log(void)
1032 c70c5802 2018-08-01 stsp fprintf(stderr,
1033 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
1034 9f7d7167 2018-04-29 stsp getprogname());
1038 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1039 80ddbec8 2018-04-29 stsp static const struct got_error *
1040 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1042 00dfcb92 2018-06-11 stsp char *vis = NULL;
1043 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1045 963b370f 2018-05-20 stsp *ws = NULL;
1046 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1047 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1048 00dfcb92 2018-06-11 stsp int vislen;
1049 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1050 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1052 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1053 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1055 00dfcb92 2018-06-11 stsp return err;
1056 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1057 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1058 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1059 a7f50699 2018-06-11 stsp goto done;
1063 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1064 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1065 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1066 a7f50699 2018-06-11 stsp goto done;
1069 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1070 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1072 00dfcb92 2018-06-11 stsp free(vis);
1073 963b370f 2018-05-20 stsp if (err) {
1074 963b370f 2018-05-20 stsp free(*ws);
1075 963b370f 2018-05-20 stsp *ws = NULL;
1076 963b370f 2018-05-20 stsp *wlen = 0;
1078 963b370f 2018-05-20 stsp return err;
1081 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1082 963b370f 2018-05-20 stsp static const struct got_error *
1083 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1084 27a741e5 2019-09-11 stsp int col_tab_align)
1086 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1087 963b370f 2018-05-20 stsp int cols = 0;
1088 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1089 963b370f 2018-05-20 stsp size_t wlen;
1092 963b370f 2018-05-20 stsp *wlinep = NULL;
1093 b700b5d6 2018-07-10 stsp *widthp = 0;
1095 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1097 963b370f 2018-05-20 stsp return err;
1100 27a741e5 2019-09-11 stsp while (i < wlen) {
1101 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1103 27a741e5 2019-09-11 stsp if (width == 0) {
1108 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1109 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1111 27a741e5 2019-09-11 stsp cols += width;
1113 27a741e5 2019-09-11 stsp } else if (width == -1) {
1114 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1115 27a741e5 2019-09-11 stsp width = TABSIZE -
1116 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1117 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1119 27a741e5 2019-09-11 stsp cols += width;
1123 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1124 963b370f 2018-05-20 stsp goto done;
1127 963b370f 2018-05-20 stsp wline[i] = L'\0';
1128 b700b5d6 2018-07-10 stsp if (widthp)
1129 b700b5d6 2018-07-10 stsp *widthp = cols;
1132 963b370f 2018-05-20 stsp free(wline);
1134 963b370f 2018-05-20 stsp *wlinep = wline;
1135 963b370f 2018-05-20 stsp return err;
1138 8b473291 2019-02-21 stsp static const struct got_error*
1139 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1140 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1142 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1143 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1145 8b473291 2019-02-21 stsp const char *name;
1147 8b473291 2019-02-21 stsp *refs_str = NULL;
1149 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1150 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1153 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1154 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1156 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1157 8b473291 2019-02-21 stsp name += 5;
1158 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1160 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1161 8b473291 2019-02-21 stsp name += 6;
1162 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
1163 8b473291 2019-02-21 stsp name += 8;
1164 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1165 52b5abe1 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1166 5d844a1e 2019-08-13 stsp if (err) {
1167 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1169 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1170 5d844a1e 2019-08-13 stsp err = NULL;
1171 5d844a1e 2019-08-13 stsp tag = NULL;
1174 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1175 52b5abe1 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1177 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1178 52b5abe1 2019-08-13 stsp if (cmp != 0)
1180 8b473291 2019-02-21 stsp s = *refs_str;
1181 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1182 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1183 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1185 8b473291 2019-02-21 stsp *refs_str = NULL;
1191 8b473291 2019-02-21 stsp return err;
1194 963b370f 2018-05-20 stsp static const struct got_error *
1195 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1196 27a741e5 2019-09-11 stsp int col_tab_align)
1198 5813d178 2019-03-09 stsp char *smallerthan, *at;
1200 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1201 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1202 5813d178 2019-03-09 stsp author = smallerthan + 1;
1203 5813d178 2019-03-09 stsp at = strchr(author, '@');
1205 5813d178 2019-03-09 stsp *at = '\0';
1206 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1209 5813d178 2019-03-09 stsp static const struct got_error *
1210 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1211 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
1212 11b20872 2019-11-08 stsp const size_t date_display_cols, int author_display_cols,
1213 11b20872 2019-11-08 stsp struct tog_colors *colors)
1215 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1216 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1217 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1218 5813d178 2019-03-09 stsp char *author = NULL;
1219 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1220 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1221 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1222 bb737323 2018-05-20 stsp int col, limit;
1223 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1224 ccb26ccd 2018-11-05 stsp struct tm tm;
1225 45d799e2 2018-12-23 stsp time_t committer_time;
1226 11b20872 2019-11-08 stsp struct tog_color *tc;
1228 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1229 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1230 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1231 6db9f7f6 2019-12-10 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1232 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1233 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1235 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1236 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1238 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1239 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_DATE);
1241 11b20872 2019-11-08 stsp wattr_on(view->window,
1242 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1243 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1245 11b20872 2019-11-08 stsp wattr_off(view->window,
1246 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1247 27a741e5 2019-09-11 stsp col = limit;
1248 b39d25c7 2018-07-10 stsp if (col > avail)
1249 b39d25c7 2018-07-10 stsp goto done;
1251 6570a66d 2019-11-08 stsp if (avail >= 120) {
1252 6570a66d 2019-11-08 stsp char *id_str;
1253 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1255 6570a66d 2019-11-08 stsp goto done;
1256 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1258 11b20872 2019-11-08 stsp wattr_on(view->window,
1259 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1260 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1262 11b20872 2019-11-08 stsp wattr_off(view->window,
1263 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1264 6570a66d 2019-11-08 stsp free(id_str);
1266 6570a66d 2019-11-08 stsp if (col > avail)
1267 6570a66d 2019-11-08 stsp goto done;
1270 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1271 5813d178 2019-03-09 stsp if (author == NULL) {
1272 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1273 80ddbec8 2018-04-29 stsp goto done;
1275 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1277 bb737323 2018-05-20 stsp goto done;
1278 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_AUTHOR);
1280 11b20872 2019-11-08 stsp wattr_on(view->window,
1281 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1282 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1284 11b20872 2019-11-08 stsp wattr_off(view->window,
1285 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1286 bb737323 2018-05-20 stsp col += author_width;
1287 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1288 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1290 bb737323 2018-05-20 stsp author_width++;
1292 9c2eaf34 2018-05-20 stsp if (col > avail)
1293 9c2eaf34 2018-05-20 stsp goto done;
1295 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1297 6d9fbc00 2018-04-29 stsp goto done;
1298 bb737323 2018-05-20 stsp logmsg = logmsg0;
1299 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1301 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1302 bb737323 2018-05-20 stsp if (newline)
1303 bb737323 2018-05-20 stsp *newline = '\0';
1304 bb737323 2018-05-20 stsp limit = avail - col;
1305 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1307 bb737323 2018-05-20 stsp goto done;
1308 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1309 bb737323 2018-05-20 stsp col += logmsg_width;
1310 27a741e5 2019-09-11 stsp while (col < avail) {
1311 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1315 80ddbec8 2018-04-29 stsp free(logmsg0);
1316 bb737323 2018-05-20 stsp free(wlogmsg);
1317 5813d178 2019-03-09 stsp free(author);
1318 bb737323 2018-05-20 stsp free(wauthor);
1319 80ddbec8 2018-04-29 stsp free(line);
1320 80ddbec8 2018-04-29 stsp return err;
1323 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1324 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1325 899d86c2 2018-05-10 stsp struct got_object_id *id)
1327 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1329 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1330 80ddbec8 2018-04-29 stsp if (entry == NULL)
1331 899d86c2 2018-05-10 stsp return NULL;
1333 899d86c2 2018-05-10 stsp entry->id = id;
1334 99db9666 2018-05-07 stsp entry->commit = commit;
1335 899d86c2 2018-05-10 stsp return entry;
1338 99db9666 2018-05-07 stsp static void
1339 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1341 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1343 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1344 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1345 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1346 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1347 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1348 99db9666 2018-05-07 stsp free(entry);
1351 99db9666 2018-05-07 stsp static void
1352 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1354 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1355 99db9666 2018-05-07 stsp pop_commit(commits);
1358 c4972b91 2018-05-07 stsp static const struct got_error *
1359 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1360 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1362 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1363 13add988 2019-10-15 stsp regmatch_t regmatch;
1364 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1366 13add988 2019-10-15 stsp *have_match = 0;
1368 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1370 13add988 2019-10-15 stsp return err;
1372 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1374 13add988 2019-10-15 stsp goto done;
1376 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1377 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1378 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1379 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1380 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1381 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1382 13add988 2019-10-15 stsp *have_match = 1;
1384 13add988 2019-10-15 stsp free(id_str);
1385 13add988 2019-10-15 stsp free(logmsg);
1386 13add988 2019-10-15 stsp return err;
1389 13add988 2019-10-15 stsp static const struct got_error *
1390 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1391 13add988 2019-10-15 stsp int minqueue, struct got_repository *repo, const char *path,
1392 13add988 2019-10-15 stsp int *searching, int *search_next_done, regex_t *regex)
1394 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1395 13add988 2019-10-15 stsp int nqueued = 0, have_match = 0;
1398 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1399 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1400 1a76625f 2018-10-22 stsp * while updating the display.
1402 13add988 2019-10-15 stsp while (nqueued < minqueue ||
1403 13add988 2019-10-15 stsp (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1404 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1405 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1406 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1407 1a76625f 2018-10-22 stsp int errcode;
1409 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1410 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1413 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1416 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1417 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1418 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1422 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1423 1a76625f 2018-10-22 stsp if (errcode) {
1424 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1425 13add988 2019-10-15 stsp "pthread_mutex_lock");
1429 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1430 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1431 ecb28ae0 2018-07-16 stsp nqueued++;
1432 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1434 13add988 2019-10-15 stsp if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1435 13add988 2019-10-15 stsp err = match_commit(&have_match, id, commit, regex);
1436 13add988 2019-10-15 stsp if (err) {
1437 13add988 2019-10-15 stsp pthread_mutex_lock(&tog_mutex);
1442 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1443 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1444 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1445 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1447 13add988 2019-10-15 stsp if (have_match)
1451 9ba79e04 2018-06-11 stsp return err;
1454 0553a4e3 2018-04-30 stsp static const struct got_error *
1455 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1456 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1457 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1458 11b20872 2019-11-08 stsp struct got_reflist_head *refs, const char *path, int commits_needed,
1459 11b20872 2019-11-08 stsp struct tog_colors *colors)
1461 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1462 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1463 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1464 60493ae3 2019-06-20 stsp int width;
1465 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1466 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1467 8b473291 2019-02-21 stsp char *refs_str = NULL;
1468 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1469 11b20872 2019-11-08 stsp struct tog_color *tc;
1470 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1472 e0d42f60 2018-07-22 stsp entry = first;
1473 e0d42f60 2018-07-22 stsp ncommits = 0;
1474 e0d42f60 2018-07-22 stsp while (entry) {
1475 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1476 e0d42f60 2018-07-22 stsp *selected = entry;
1479 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1480 e0d42f60 2018-07-22 stsp ncommits++;
1483 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1484 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1486 ecb28ae0 2018-07-16 stsp return err;
1487 8b473291 2019-02-21 stsp if (refs) {
1488 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, (*selected)->id,
1491 8b473291 2019-02-21 stsp goto done;
1495 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1496 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1498 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1499 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1500 60493ae3 2019-06-20 stsp commits_needed > 0 ?
1501 60493ae3 2019-06-20 stsp (view->searching && view->search_next_done == 0
1502 60493ae3 2019-06-20 stsp ? "searching..." : "loading... ") :
1503 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1504 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1505 8b473291 2019-02-21 stsp goto done;
1508 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1509 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1510 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1511 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1512 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1513 1a76625f 2018-10-22 stsp header = NULL;
1514 1a76625f 2018-10-22 stsp goto done;
1516 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1517 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1518 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1519 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1520 1a76625f 2018-10-22 stsp header = NULL;
1521 1a76625f 2018-10-22 stsp goto done;
1523 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1525 1a76625f 2018-10-22 stsp goto done;
1527 2814baeb 2018-08-01 stsp werase(view->window);
1529 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1530 a3404814 2018-09-02 stsp wstandout(view->window);
1531 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1533 11b20872 2019-11-08 stsp wattr_on(view->window,
1534 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1535 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1537 11b20872 2019-11-08 stsp wattr_off(view->window,
1538 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1539 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1540 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1543 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1544 a3404814 2018-09-02 stsp wstandend(view->window);
1545 ecb28ae0 2018-07-16 stsp free(wline);
1546 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1547 1a76625f 2018-10-22 stsp goto done;
1549 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1550 899d86c2 2018-05-10 stsp entry = first;
1551 5813d178 2019-03-09 stsp ncommits = 0;
1552 5813d178 2019-03-09 stsp while (entry) {
1553 5813d178 2019-03-09 stsp char *author;
1554 5813d178 2019-03-09 stsp wchar_t *wauthor;
1555 5813d178 2019-03-09 stsp int width;
1556 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1558 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1559 5813d178 2019-03-09 stsp if (author == NULL) {
1560 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1561 5813d178 2019-03-09 stsp goto done;
1563 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1564 27a741e5 2019-09-11 stsp date_display_cols);
1565 5813d178 2019-03-09 stsp if (author_cols < width)
1566 5813d178 2019-03-09 stsp author_cols = width;
1567 5813d178 2019-03-09 stsp free(wauthor);
1568 5813d178 2019-03-09 stsp free(author);
1569 7ca04879 2019-10-19 stsp ncommits++;
1570 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1573 5813d178 2019-03-09 stsp entry = first;
1574 899d86c2 2018-05-10 stsp *last = first;
1575 867c6645 2018-07-10 stsp ncommits = 0;
1576 899d86c2 2018-05-10 stsp while (entry) {
1577 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1579 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1580 2814baeb 2018-08-01 stsp wstandout(view->window);
1581 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1582 11b20872 2019-11-08 stsp date_display_cols, author_cols, colors);
1583 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1584 2814baeb 2018-08-01 stsp wstandend(view->window);
1586 60493ae3 2019-06-20 stsp goto done;
1587 0553a4e3 2018-04-30 stsp ncommits++;
1588 899d86c2 2018-05-10 stsp *last = entry;
1589 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1592 1a57306a 2018-09-02 stsp view_vborder(view);
1594 1a76625f 2018-10-22 stsp free(id_str);
1595 8b473291 2019-02-21 stsp free(refs_str);
1596 1a76625f 2018-10-22 stsp free(ncommits_str);
1597 1a76625f 2018-10-22 stsp free(header);
1598 80ddbec8 2018-04-29 stsp return err;
1601 07b55e75 2018-05-10 stsp static void
1602 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1603 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1604 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1606 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1607 07b55e75 2018-05-10 stsp int nscrolled = 0;
1609 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1610 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1613 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1614 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1615 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1616 07b55e75 2018-05-10 stsp if (entry) {
1617 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1618 07b55e75 2018-05-10 stsp nscrolled++;
1623 aa075928 2018-05-10 stsp static const struct got_error *
1624 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1625 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1627 5e224a3e 2019-02-22 stsp int errcode;
1628 8a42fca8 2019-02-22 stsp int max_wait = 20;
1630 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1632 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1633 5e224a3e 2019-02-22 stsp if (*log_complete)
1636 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1637 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1638 7aafa0d1 2019-02-22 stsp if (errcode)
1639 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1640 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1641 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1642 7aafa0d1 2019-02-22 stsp if (errcode)
1643 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1644 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1645 7aafa0d1 2019-02-22 stsp pthread_yield();
1646 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1647 7aafa0d1 2019-02-22 stsp if (errcode)
1648 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1649 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1651 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1653 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1654 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1655 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1656 5e224a3e 2019-02-22 stsp * with few commits in history.
1658 7aafa0d1 2019-02-22 stsp return NULL;
1662 5e224a3e 2019-02-22 stsp return NULL;
1665 5e224a3e 2019-02-22 stsp static const struct got_error *
1666 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1667 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1668 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1669 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1670 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1672 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1673 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1674 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1676 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1677 5e224a3e 2019-02-22 stsp return NULL;
1679 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1680 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1682 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1683 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1685 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1686 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1687 5e224a3e 2019-02-22 stsp need_commits);
1689 5e224a3e 2019-02-22 stsp return err;
1693 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1694 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1697 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1699 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1700 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1702 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1703 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1705 dd0a52c1 2018-05-20 stsp return err;
1708 cd0acaa7 2018-05-20 stsp static const struct got_error *
1709 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1710 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1711 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1712 8b473291 2019-02-21 stsp struct got_repository *repo)
1714 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1715 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1716 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1718 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1719 15a94983 2018-12-23 stsp if (diff_view == NULL)
1720 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1722 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1723 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1724 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1725 e5a0f69f 2018-08-18 stsp if (err == NULL)
1726 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1727 cd0acaa7 2018-05-20 stsp return err;
1730 80ddbec8 2018-04-29 stsp static const struct got_error *
1731 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1732 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1734 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1736 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1737 941e9f74 2019-05-21 stsp if (parent == NULL)
1738 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1740 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1741 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1742 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1743 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1744 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1745 941e9f74 2019-05-21 stsp s->tree = subtree;
1746 941e9f74 2019-05-21 stsp s->selected = 0;
1747 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1748 941e9f74 2019-05-21 stsp return NULL;
1752 941e9f74 2019-05-21 stsp static const struct got_error *
1753 941e9f74 2019-05-21 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1754 941e9f74 2019-05-21 stsp struct commit_queue_entry *entry, const char *path,
1755 941e9f74 2019-05-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
1757 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1758 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1759 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s;
1760 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1761 b03c880f 2019-05-21 stsp char *slash, *subpath = NULL;
1762 941e9f74 2019-05-21 stsp const char *p;
1764 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1765 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1767 9343a5fb 2018-06-23 stsp return err;
1769 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1770 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1771 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1773 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1774 941e9f74 2019-05-21 stsp if (err) {
1775 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1776 941e9f74 2019-05-21 stsp return err;
1778 941e9f74 2019-05-21 stsp s = &tree_view->state.tree;
1780 941e9f74 2019-05-21 stsp *new_view = tree_view;
1782 33cbf02b 2020-01-12 stsp if (got_path_is_root_dir(path))
1783 33cbf02b 2020-01-12 stsp return NULL;
1785 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1787 941e9f74 2019-05-21 stsp while (*p) {
1788 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1789 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1790 56e0773d 2019-11-28 stsp char *te_name;
1792 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1795 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1796 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1797 941e9f74 2019-05-21 stsp if (slash == NULL)
1798 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1800 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1801 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1802 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1805 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1806 56e0773d 2019-11-28 stsp if (te == NULL) {
1807 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1808 56e0773d 2019-11-28 stsp free(te_name);
1811 56e0773d 2019-11-28 stsp free(te_name);
1812 56e0773d 2019-11-28 stsp s->selected_entry = te;
1813 56e0773d 2019-11-28 stsp s->selected = got_tree_entry_get_index(te);
1814 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1815 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1817 56e0773d 2019-11-28 stsp if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1818 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1819 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1820 67409a31 2019-05-24 stsp s->selected = 0;
1824 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1825 941e9f74 2019-05-21 stsp if (slash)
1826 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1828 941e9f74 2019-05-21 stsp subpath = strdup(path);
1829 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1830 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1834 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1839 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1840 941e9f74 2019-05-21 stsp free(tree_id);
1844 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1845 941e9f74 2019-05-21 stsp if (err) {
1846 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1849 941e9f74 2019-05-21 stsp if (slash == NULL)
1851 941e9f74 2019-05-21 stsp free(subpath);
1852 941e9f74 2019-05-21 stsp subpath = NULL;
1853 941e9f74 2019-05-21 stsp p = slash;
1856 941e9f74 2019-05-21 stsp free(subpath);
1857 1a76625f 2018-10-22 stsp return err;
1860 61266923 2020-01-14 stsp static const struct got_error *
1861 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1863 61266923 2020-01-14 stsp sigset_t sigset;
1864 61266923 2020-01-14 stsp int errcode;
1866 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1867 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1869 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1870 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1871 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1872 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
1873 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1875 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
1876 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
1877 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1879 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1880 61266923 2020-01-14 stsp if (errcode)
1881 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
1883 61266923 2020-01-14 stsp return NULL;
1886 1a76625f 2018-10-22 stsp static void *
1887 1a76625f 2018-10-22 stsp log_thread(void *arg)
1889 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1890 1a76625f 2018-10-22 stsp int errcode = 0;
1891 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1892 1a76625f 2018-10-22 stsp int done = 0;
1894 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
1896 61266923 2020-01-14 stsp return (void *)err;
1898 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1899 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1900 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1901 13add988 2019-10-15 stsp a->regex);
1902 1a76625f 2018-10-22 stsp if (err) {
1903 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1904 1a76625f 2018-10-22 stsp return (void *)err;
1905 1a76625f 2018-10-22 stsp err = NULL;
1907 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1908 1a76625f 2018-10-22 stsp a->commits_needed--;
1910 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1911 3abe8080 2019-04-10 stsp if (errcode) {
1912 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1913 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1915 3abe8080 2019-04-10 stsp } else if (*a->quit)
1917 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1918 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1919 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1920 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1924 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1925 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1926 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1927 1a76625f 2018-10-22 stsp &tog_mutex);
1928 1a76625f 2018-10-22 stsp if (errcode)
1929 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1930 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1933 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1934 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1935 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1936 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1938 3abe8080 2019-04-10 stsp a->log_complete = 1;
1939 1a76625f 2018-10-22 stsp return (void *)err;
1942 1a76625f 2018-10-22 stsp static const struct got_error *
1943 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1945 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1946 1a76625f 2018-10-22 stsp int errcode;
1948 1a76625f 2018-10-22 stsp if (s->thread) {
1949 1a76625f 2018-10-22 stsp s->quit = 1;
1950 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1951 1a76625f 2018-10-22 stsp if (errcode)
1952 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1953 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1954 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1955 1a76625f 2018-10-22 stsp if (errcode)
1956 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1957 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1958 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1959 1a76625f 2018-10-22 stsp if (errcode)
1960 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1961 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1962 1a76625f 2018-10-22 stsp if (errcode)
1963 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1964 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1965 1a76625f 2018-10-22 stsp s->thread = NULL;
1968 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1969 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1970 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1972 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1973 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1974 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1977 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1978 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1979 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1982 9343a5fb 2018-06-23 stsp return err;
1985 9343a5fb 2018-06-23 stsp static const struct got_error *
1986 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1988 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1989 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1991 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1992 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1993 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1994 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1995 1a76625f 2018-10-22 stsp free(s->start_id);
1996 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1997 1a76625f 2018-10-22 stsp return err;
2000 1a76625f 2018-10-22 stsp static const struct got_error *
2001 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2003 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2005 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2006 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2007 60493ae3 2019-06-20 stsp return NULL;
2010 60493ae3 2019-06-20 stsp static const struct got_error *
2011 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2013 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2014 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2015 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2017 60493ae3 2019-06-20 stsp if (!view->searching) {
2018 60493ae3 2019-06-20 stsp view->search_next_done = 1;
2019 60493ae3 2019-06-20 stsp return NULL;
2022 96e2b566 2019-07-08 stsp if (s->search_entry) {
2023 13add988 2019-10-15 stsp int errcode, ch;
2024 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2025 13add988 2019-10-15 stsp if (errcode)
2026 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2027 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2028 13add988 2019-10-15 stsp ch = wgetch(view->window);
2029 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2030 13add988 2019-10-15 stsp if (errcode)
2031 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2032 13add988 2019-10-15 stsp "pthread_mutex_lock");
2033 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2034 678cbce5 2019-07-28 stsp view->search_next_done = 1;
2035 678cbce5 2019-07-28 stsp return NULL;
2037 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2038 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2040 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2041 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2042 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2043 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2044 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2046 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
2047 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2049 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2050 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2052 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2055 60493ae3 2019-06-20 stsp while (1) {
2056 13add988 2019-10-15 stsp int have_match = 0;
2058 60493ae3 2019-06-20 stsp if (entry == NULL) {
2059 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2060 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2061 f801134a 2019-06-25 stsp view->search_next_done = 1;
2062 f801134a 2019-06-25 stsp return NULL;
2065 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2066 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2067 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2069 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2070 57b33b64 2019-07-08 stsp return trigger_log_thread(1,
2071 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
2072 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
2073 f801134a 2019-06-25 stsp &s->thread_args.need_commits);
2076 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2077 13add988 2019-10-15 stsp &view->regex);
2080 13add988 2019-10-15 stsp if (have_match) {
2081 60493ae3 2019-06-20 stsp view->search_next_done = 1;
2082 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2086 96e2b566 2019-07-08 stsp s->search_entry = entry;
2087 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2088 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2090 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2093 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2094 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2095 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2096 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2098 60493ae3 2019-06-20 stsp return err;
2101 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2102 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2104 60493ae3 2019-06-20 stsp return err;
2109 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2111 60493ae3 2019-06-20 stsp return NULL;
2114 60493ae3 2019-06-20 stsp static const struct got_error *
2115 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2116 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
2117 d01904d4 2019-06-25 stsp const char *head_ref_name, const char *path, int check_disk)
2119 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2120 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2121 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2122 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2123 1a76625f 2018-10-22 stsp int errcode;
2125 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2126 ecb28ae0 2018-07-16 stsp if (err != NULL)
2127 ecb28ae0 2018-07-16 stsp goto done;
2129 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2130 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2131 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2133 8b473291 2019-02-21 stsp s->refs = refs;
2134 fb2756b9 2018-08-04 stsp s->repo = repo;
2135 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
2136 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2137 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2138 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2139 5036bf37 2018-09-24 stsp goto done;
2142 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2143 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2144 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2145 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2147 11b20872 2019-11-08 stsp goto done;
2148 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2149 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2150 11b20872 2019-11-08 stsp if (err) {
2151 11b20872 2019-11-08 stsp free_colors(&s->colors);
2152 11b20872 2019-11-08 stsp goto done;
2154 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2155 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2156 11b20872 2019-11-08 stsp if (err) {
2157 11b20872 2019-11-08 stsp free_colors(&s->colors);
2158 11b20872 2019-11-08 stsp goto done;
2162 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2163 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2164 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2165 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2166 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2168 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2170 1a76625f 2018-10-22 stsp goto done;
2171 3d509237 2020-01-04 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path, 0);
2173 1a76625f 2018-10-22 stsp goto done;
2174 c5b78334 2020-01-12 stsp err = got_commit_graph_iter_start(thread_graph,
2175 c5b78334 2020-01-12 stsp s->start_id, s->repo, NULL, NULL);
2177 c5b78334 2020-01-12 stsp goto done;
2179 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2180 1a76625f 2018-10-22 stsp if (errcode) {
2181 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2182 1a76625f 2018-10-22 stsp goto done;
2185 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2186 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2187 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2188 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2189 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2190 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2191 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2192 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2193 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2194 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2195 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2196 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2197 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2200 1a76625f 2018-10-22 stsp close_log_view(view);
2201 ba4f502b 2018-08-04 stsp return err;
2204 e5a0f69f 2018-08-18 stsp static const struct got_error *
2205 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2207 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2209 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2210 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2211 2b380cc8 2018-10-24 stsp &s->thread_args);
2212 2b380cc8 2018-10-24 stsp if (errcode)
2213 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2216 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
2217 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
2218 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
2219 11b20872 2019-11-08 stsp s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2222 e5a0f69f 2018-08-18 stsp static const struct got_error *
2223 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2224 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2226 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2227 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2228 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2229 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2230 669b5ffa 2018-10-07 stsp int begin_x = 0;
2231 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2233 e5a0f69f 2018-08-18 stsp switch (ch) {
2235 1e37a5c2 2019-05-12 jcs s->quit = 1;
2238 1e37a5c2 2019-05-12 jcs case KEY_UP:
2241 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2243 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2244 1e37a5c2 2019-05-12 jcs s->selected--;
2246 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
2247 1144d21a 2019-06-21 stsp &s->commits);
2249 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2250 a4292ac5 2019-05-12 jcs case CTRL('b'):
2251 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2253 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
2254 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
2255 1e37a5c2 2019-05-12 jcs s->selected = 0;
2258 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
2259 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
2262 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2265 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2267 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2268 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2269 1e37a5c2 2019-05-12 jcs s->selected++;
2272 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
2273 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
2274 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
2275 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2276 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2278 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2279 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2280 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2281 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2282 1e37a5c2 2019-05-12 jcs if (first == NULL)
2284 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
2285 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
2286 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
2287 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2288 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2291 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2292 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2293 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2294 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2295 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2296 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2298 1e37a5c2 2019-05-12 jcs err = NULL;
2301 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2302 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2303 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2304 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2305 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2307 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2310 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2312 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2313 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2314 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2315 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2316 1e37a5c2 2019-05-12 jcs view, 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, diff_view);
2325 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2328 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2329 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2331 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2334 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2336 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2337 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2338 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2339 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2342 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2343 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2345 1e37a5c2 2019-05-12 jcs return err;
2346 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2348 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2351 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2352 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2354 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2356 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2357 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2359 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2360 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2361 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2363 1e37a5c2 2019-05-12 jcs return err;
2364 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2365 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2366 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2367 638f9024 2019-05-13 stsp return got_error_from_errno(
2368 1e37a5c2 2019-05-12 jcs "view_open");
2369 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2370 d01904d4 2019-06-25 stsp s->repo, s->head_ref_name, parent_path, 0);
2372 1e37a5c2 2019-05-12 jcs return err;;
2373 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2374 1e37a5c2 2019-05-12 jcs *new_view = lv;
2376 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2377 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2379 1e37a5c2 2019-05-12 jcs return NULL;
2382 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2383 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2385 d01904d4 2019-06-25 stsp return err;
2386 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2387 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2388 d01904d4 2019-06-25 stsp if (lv == NULL)
2389 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2390 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&start_id, NULL,
2391 71a27632 2020-01-15 stsp s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2392 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2393 ef129c5e 2019-08-03 stsp if (err) {
2394 ef129c5e 2019-08-03 stsp view_close(lv);
2395 d01904d4 2019-06-25 stsp return err;
2397 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2398 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2399 e6b23735 2019-10-04 stsp free(start_id);
2400 e6b23735 2019-10-04 stsp view_close(lv);
2401 e6b23735 2019-10-04 stsp return got_error_from_errno("strdup");
2403 e6b23735 2019-10-04 stsp got_ref_list_free(s->refs);
2404 e6b23735 2019-10-04 stsp err = got_ref_list(s->refs, s->repo, NULL,
2405 e6b23735 2019-10-04 stsp got_ref_cmp_by_name, NULL);
2406 e6b23735 2019-10-04 stsp if (err) {
2407 d01904d4 2019-06-25 stsp free(start_id);
2408 ef129c5e 2019-08-03 stsp view_close(lv);
2409 50284065 2019-10-04 stsp return err;
2411 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2412 d01904d4 2019-06-25 stsp s->head_ref_name, in_repo_path, 0);
2413 ef129c5e 2019-08-03 stsp if (err) {
2414 ef129c5e 2019-08-03 stsp free(start_id);
2415 ef129c5e 2019-08-03 stsp view_close(lv);
2416 d01904d4 2019-06-25 stsp return err;;
2418 d01904d4 2019-06-25 stsp *dead_view = view;
2419 d01904d4 2019-06-25 stsp *new_view = lv;
2425 80ddbec8 2018-04-29 stsp return err;
2428 4ed7e80c 2018-05-20 stsp static const struct got_error *
2429 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2431 c2db6724 2019-01-04 stsp const struct got_error *error;
2433 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2434 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2435 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2437 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2438 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2440 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2441 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2443 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2444 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2446 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2447 c2db6724 2019-01-04 stsp if (error != NULL)
2448 c2db6724 2019-01-04 stsp return error;
2450 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2451 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2453 c2db6724 2019-01-04 stsp return NULL;
2456 a915003a 2019-02-05 stsp static void
2457 a915003a 2019-02-05 stsp init_curses(void)
2459 a915003a 2019-02-05 stsp initscr();
2461 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2464 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2465 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2466 a915003a 2019-02-05 stsp curs_set(0);
2467 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2468 6d17833f 2019-11-08 stsp start_color();
2469 6d17833f 2019-11-08 stsp use_default_colors();
2471 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2472 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2473 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2476 c2db6724 2019-01-04 stsp static const struct got_error *
2477 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2479 80ddbec8 2018-04-29 stsp const struct got_error *error;
2480 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2481 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2482 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2483 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2484 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2485 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2487 04cc582a 2018-08-01 stsp struct tog_view *view;
2489 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2491 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2492 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2493 c2db6724 2019-01-04 stsp NULL) == -1)
2494 80ddbec8 2018-04-29 stsp err(1, "pledge");
2497 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2498 80ddbec8 2018-04-29 stsp switch (ch) {
2500 80ddbec8 2018-04-29 stsp start_commit = optarg;
2503 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2504 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2505 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2509 17020d27 2019-03-07 stsp usage_log();
2510 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2514 80ddbec8 2018-04-29 stsp argc -= optind;
2515 80ddbec8 2018-04-29 stsp argv += optind;
2517 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2518 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2519 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2520 ecb28ae0 2018-07-16 stsp goto done;
2522 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2523 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2524 963f97a1 2019-03-18 stsp goto done;
2525 963f97a1 2019-03-18 stsp error = NULL;
2527 963f97a1 2019-03-18 stsp if (argc == 0) {
2528 963f97a1 2019-03-18 stsp path = strdup("");
2529 963f97a1 2019-03-18 stsp if (path == NULL) {
2530 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2531 ecb28ae0 2018-07-16 stsp goto done;
2533 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2534 963f97a1 2019-03-18 stsp if (worktree) {
2535 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2537 963f97a1 2019-03-18 stsp if (error)
2538 963f97a1 2019-03-18 stsp goto done;
2540 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2541 963f97a1 2019-03-18 stsp if (path == NULL) {
2542 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2543 963f97a1 2019-03-18 stsp goto done;
2547 963f97a1 2019-03-18 stsp usage_log();
2549 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2550 a1fbf39a 2019-08-11 stsp if (worktree)
2551 b9d7675a 2019-08-11 stsp repo_path = strdup(
2552 b9d7675a 2019-08-11 stsp got_worktree_get_repo_path(worktree));
2554 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2556 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2557 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2558 963f97a1 2019-03-18 stsp goto done;
2561 a915003a 2019-02-05 stsp init_curses();
2563 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2564 80ddbec8 2018-04-29 stsp if (error != NULL)
2565 ecb28ae0 2018-07-16 stsp goto done;
2567 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2568 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2569 c02c541e 2019-03-29 stsp if (error)
2570 c02c541e 2019-03-29 stsp goto done;
2572 15a94983 2018-12-23 stsp if (start_commit == NULL)
2573 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, worktree ?
2574 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2575 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2577 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&start_id, NULL, start_commit,
2578 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
2579 80ddbec8 2018-04-29 stsp if (error != NULL)
2580 8b473291 2019-02-21 stsp goto done;
2582 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2583 8b473291 2019-02-21 stsp if (error)
2584 ecb28ae0 2018-07-16 stsp goto done;
2586 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2587 04cc582a 2018-08-01 stsp if (view == NULL) {
2588 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2589 04cc582a 2018-08-01 stsp goto done;
2591 2fc00ff4 2019-08-31 stsp if (worktree) {
2592 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2593 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2594 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2595 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2596 2fc00ff4 2019-08-31 stsp goto done;
2599 2fc00ff4 2019-08-31 stsp error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2601 ba4f502b 2018-08-04 stsp if (error)
2602 ba4f502b 2018-08-04 stsp goto done;
2603 2fc00ff4 2019-08-31 stsp if (worktree) {
2604 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2605 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2606 2fc00ff4 2019-08-31 stsp worktree = NULL;
2608 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2610 ecb28ae0 2018-07-16 stsp free(repo_path);
2611 ecb28ae0 2018-07-16 stsp free(cwd);
2612 ecb28ae0 2018-07-16 stsp free(path);
2613 899d86c2 2018-05-10 stsp free(start_id);
2614 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2616 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2617 ec142235 2019-03-07 stsp if (worktree)
2618 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2619 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2620 80ddbec8 2018-04-29 stsp return error;
2623 4ed7e80c 2018-05-20 stsp __dead static void
2624 9f7d7167 2018-04-29 stsp usage_diff(void)
2627 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2628 9f7d7167 2018-04-29 stsp getprogname());
2632 b304db33 2018-05-20 stsp static char *
2633 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2635 b304db33 2018-05-20 stsp char *line;
2636 b304db33 2018-05-20 stsp size_t linelen;
2637 b304db33 2018-05-20 stsp size_t lineno;
2638 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2640 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2642 b304db33 2018-05-20 stsp *len = linelen;
2643 b304db33 2018-05-20 stsp return line;
2646 6d17833f 2019-11-08 stsp static int
2647 6d17833f 2019-11-08 stsp match_line(const char *line, regex_t *regex)
2649 6d17833f 2019-11-08 stsp regmatch_t regmatch;
2651 6d17833f 2019-11-08 stsp return regexec(regex, line, 1, ®match, 0) == 0;
2654 f26dddb7 2019-11-08 stsp struct tog_color *
2655 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2657 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2659 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2660 6d17833f 2019-11-08 stsp if (match_line(line, &tc->regex))
2661 6d17833f 2019-11-08 stsp return tc;
2664 6d17833f 2019-11-08 stsp return NULL;
2667 4ed7e80c 2018-05-20 stsp static const struct got_error *
2668 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2669 6d17833f 2019-11-08 stsp int *last_displayed_line, int *eof, int max_lines, char *header,
2670 f26dddb7 2019-11-08 stsp struct tog_colors *colors)
2672 61e69b96 2018-05-20 stsp const struct got_error *err;
2673 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2674 b304db33 2018-05-20 stsp char *line;
2675 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2676 b304db33 2018-05-20 stsp size_t len;
2677 61e69b96 2018-05-20 stsp wchar_t *wline;
2678 e0b650dd 2018-05-20 stsp int width;
2680 26ed57b2 2018-05-19 stsp rewind(f);
2681 f7d12f7e 2018-08-01 stsp werase(view->window);
2683 a3404814 2018-09-02 stsp if (header) {
2684 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
2685 a3404814 2018-09-02 stsp if (err) {
2686 a3404814 2018-09-02 stsp return err;
2689 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2690 a3404814 2018-09-02 stsp wstandout(view->window);
2691 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2692 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2693 a3404814 2018-09-02 stsp wstandend(view->window);
2694 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2695 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2697 a3404814 2018-09-02 stsp if (max_lines <= 1)
2698 a3404814 2018-09-02 stsp return NULL;
2699 a3404814 2018-09-02 stsp max_lines--;
2703 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2704 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2705 26ed57b2 2018-05-19 stsp if (line == NULL) {
2709 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2710 26ed57b2 2018-05-19 stsp free(line);
2714 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2715 61e69b96 2018-05-20 stsp if (err) {
2716 61e69b96 2018-05-20 stsp free(line);
2717 61e69b96 2018-05-20 stsp return err;
2720 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
2722 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2723 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2724 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2726 6d17833f 2019-11-08 stsp wattr_off(view->window,
2727 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2728 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2729 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2730 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2731 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2732 26ed57b2 2018-05-19 stsp free(line);
2733 2550e4c3 2018-07-13 stsp free(wline);
2734 2550e4c3 2018-07-13 stsp wline = NULL;
2736 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2738 1a57306a 2018-09-02 stsp view_vborder(view);
2740 c3e9aa98 2019-05-13 jcs if (*eof) {
2741 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2742 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2743 c3e9aa98 2019-05-13 jcs nprinted++;
2746 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2748 c3e9aa98 2019-05-13 jcs return err;
2751 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2752 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2753 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2756 26ed57b2 2018-05-19 stsp return NULL;
2759 abd2672a 2018-12-23 stsp static char *
2760 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2762 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2763 09867e48 2019-08-13 stsp char *p, *s;
2765 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2766 09867e48 2019-08-13 stsp if (tm == NULL)
2767 09867e48 2019-08-13 stsp return NULL;
2768 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2769 09867e48 2019-08-13 stsp if (s == NULL)
2770 09867e48 2019-08-13 stsp return NULL;
2771 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2773 abd2672a 2018-12-23 stsp *p = '\0';
2777 4ed7e80c 2018-05-20 stsp static const struct got_error *
2778 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2779 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2781 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2782 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
2783 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2784 5943eee2 2019-08-13 stsp char *id_str = NULL, *logmsg = NULL;
2785 45d799e2 2018-12-23 stsp time_t committer_time;
2786 45d799e2 2018-12-23 stsp const char *author, *committer;
2787 8b473291 2019-02-21 stsp char *refs_str = NULL;
2789 8b473291 2019-02-21 stsp if (refs) {
2790 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
2792 8b473291 2019-02-21 stsp return err;
2795 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2797 abd2672a 2018-12-23 stsp return err;
2799 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2800 15a94983 2018-12-23 stsp if (err) {
2801 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2802 15a94983 2018-12-23 stsp goto done;
2805 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2806 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2807 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2808 abd2672a 2018-12-23 stsp goto done;
2810 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2811 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2812 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2813 abd2672a 2018-12-23 stsp goto done;
2815 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2816 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
2817 09867e48 2019-08-13 stsp if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2818 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2819 abd2672a 2018-12-23 stsp goto done;
2821 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2822 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2823 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2824 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2825 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2826 abd2672a 2018-12-23 stsp goto done;
2828 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2830 5943eee2 2019-08-13 stsp goto done;
2831 5943eee2 2019-08-13 stsp if (fprintf(outfile, "%s\n", logmsg) < 0) {
2832 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2833 abd2672a 2018-12-23 stsp goto done;
2836 abd2672a 2018-12-23 stsp free(id_str);
2837 5943eee2 2019-08-13 stsp free(logmsg);
2838 8b473291 2019-02-21 stsp free(refs_str);
2839 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2840 abd2672a 2018-12-23 stsp return err;
2843 abd2672a 2018-12-23 stsp static const struct got_error *
2844 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2846 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2847 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2848 15a94983 2018-12-23 stsp int obj_type;
2850 511a516b 2018-05-19 stsp f = got_opentemp();
2851 48ae06ee 2018-10-18 stsp if (f == NULL) {
2852 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2853 48ae06ee 2018-10-18 stsp goto done;
2855 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2856 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2857 fb43ecf1 2019-02-11 stsp goto done;
2861 15a94983 2018-12-23 stsp if (s->id1)
2862 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2864 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2866 15a94983 2018-12-23 stsp goto done;
2868 15a94983 2018-12-23 stsp switch (obj_type) {
2869 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2870 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2871 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2873 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2874 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2875 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2877 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2878 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2879 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2880 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2882 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2885 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2886 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2887 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2889 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2890 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2891 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2892 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2893 8b473291 2019-02-21 stsp s->repo, f);
2898 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2900 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2901 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2905 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2909 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2910 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2911 48ae06ee 2018-10-18 stsp return err;
2914 f5215bb9 2019-02-22 stsp static void
2915 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2917 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2918 f5215bb9 2019-02-22 stsp update_panels();
2919 f5215bb9 2019-02-22 stsp doupdate();
2922 48ae06ee 2018-10-18 stsp static const struct got_error *
2923 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2924 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2925 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2927 48ae06ee 2018-10-18 stsp const struct got_error *err;
2929 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2930 15a94983 2018-12-23 stsp int type1, type2;
2931 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2933 15a94983 2018-12-23 stsp return err;
2934 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2936 15a94983 2018-12-23 stsp return err;
2938 15a94983 2018-12-23 stsp if (type1 != type2)
2939 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2942 15a94983 2018-12-23 stsp if (id1) {
2943 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2944 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2945 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2947 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2949 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2950 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2951 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2952 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2953 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2955 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2956 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2957 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2958 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2959 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2960 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2961 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2962 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&view->state.diff.colors);
2964 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2965 bddb1296 2019-11-08 stsp err = add_color(&view->state.diff.colors,
2966 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
2967 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
2969 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 TOG_COLOR_DIFF_PLUS,
2972 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
2973 6d17833f 2019-11-08 stsp if (err) {
2974 bddb1296 2019-11-08 stsp free_colors(&view->state.diff.colors);
2975 6d17833f 2019-11-08 stsp return err;
2977 bddb1296 2019-11-08 stsp err = add_color(&view->state.diff.colors,
2978 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
2979 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2980 6d17833f 2019-11-08 stsp if (err) {
2981 bddb1296 2019-11-08 stsp free_colors(&view->state.diff.colors);
2982 6d17833f 2019-11-08 stsp return err;
2985 bddb1296 2019-11-08 stsp err = add_color(&view->state.diff.colors,
2986 11b20872 2019-11-08 stsp "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
2987 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
2988 6d17833f 2019-11-08 stsp if (err) {
2989 bddb1296 2019-11-08 stsp free_colors(&view->state.diff.colors);
2990 6d17833f 2019-11-08 stsp return err;
2993 11b20872 2019-11-08 stsp err = add_color(&view->state.diff.colors,
2994 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
2995 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2996 11b20872 2019-11-08 stsp if (err) {
2997 11b20872 2019-11-08 stsp free_colors(&view->state.diff.colors);
2998 11b20872 2019-11-08 stsp return err;
3001 11b20872 2019-11-08 stsp err = add_color(&view->state.diff.colors,
3002 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3003 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3004 11b20872 2019-11-08 stsp if (err) {
3005 11b20872 2019-11-08 stsp free_colors(&view->state.diff.colors);
3006 11b20872 2019-11-08 stsp return err;
3010 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3011 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3012 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3014 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
3015 48ae06ee 2018-10-18 stsp if (err) {
3016 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
3017 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
3018 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
3019 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
3020 48ae06ee 2018-10-18 stsp return err;
3023 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3024 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3025 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3027 5dc9f4bc 2018-08-04 stsp return NULL;
3030 e5a0f69f 2018-08-18 stsp static const struct got_error *
3031 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3033 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3035 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
3036 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
3037 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
3038 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
3039 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
3040 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3041 bddb1296 2019-11-08 stsp free_colors(&view->state.diff.colors);
3042 e5a0f69f 2018-08-18 stsp return err;
3045 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3046 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3048 a3404814 2018-09-02 stsp const struct got_error *err;
3049 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3050 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3052 a3404814 2018-09-02 stsp if (s->id1) {
3053 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3055 a3404814 2018-09-02 stsp return err;
3057 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3059 a3404814 2018-09-02 stsp return err;
3061 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
3062 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3063 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3064 a3404814 2018-09-02 stsp free(id_str1);
3065 a3404814 2018-09-02 stsp free(id_str2);
3066 a3404814 2018-09-02 stsp return err;
3068 a3404814 2018-09-02 stsp free(id_str1);
3069 a3404814 2018-09-02 stsp free(id_str2);
3071 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
3072 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
3073 bddb1296 2019-11-08 stsp header, &s->colors);
3076 15a087fe 2019-02-21 stsp static const struct got_error *
3077 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3078 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3080 d7a04538 2019-02-21 stsp const struct got_error *err;
3081 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3082 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3083 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3085 15a087fe 2019-02-21 stsp free(s->id2);
3086 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3087 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3088 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3090 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3092 d7a04538 2019-02-21 stsp return err;
3093 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3094 15a087fe 2019-02-21 stsp free(s->id1);
3095 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3096 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3097 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3098 15a087fe 2019-02-21 stsp return NULL;
3101 0cf4efb1 2018-09-29 stsp static const struct got_error *
3102 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3103 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3105 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3106 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3107 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3108 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
3111 e5a0f69f 2018-08-18 stsp switch (ch) {
3113 1e37a5c2 2019-05-12 jcs case KEY_UP:
3114 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3115 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3117 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3118 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3119 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3122 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3123 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3124 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3127 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3128 1e37a5c2 2019-05-12 jcs if (!s->eof)
3129 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3131 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3132 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3134 00ba99a7 2019-05-12 jcs if (s->eof)
3137 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3138 1e37a5c2 2019-05-12 jcs char *line;
3139 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
3140 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3141 1e37a5c2 2019-05-12 jcs if (line == NULL)
3146 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3147 1e37a5c2 2019-05-12 jcs s->diff_context--;
3148 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3149 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3153 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3154 1e37a5c2 2019-05-12 jcs s->diff_context++;
3155 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3156 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3161 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3163 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3164 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
3165 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
3166 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3169 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3174 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3178 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3179 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3181 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3182 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3186 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3188 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3190 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3191 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
3193 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
3194 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
3195 1e37a5c2 2019-05-12 jcs update_panels();
3196 1e37a5c2 2019-05-12 jcs doupdate();
3198 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
3199 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
3200 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
3201 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
3205 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3210 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
3211 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3214 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3218 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3219 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3221 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3222 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3228 bcbd79e2 2018-08-19 stsp return err;
3231 4ed7e80c 2018-05-20 stsp static const struct got_error *
3232 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3234 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3235 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3236 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3237 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3238 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
3239 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3241 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3243 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3245 26ed57b2 2018-05-19 stsp #ifndef PROFILE
3246 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3247 eb6600df 2019-01-04 stsp NULL) == -1)
3248 26ed57b2 2018-05-19 stsp err(1, "pledge");
3251 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3252 26ed57b2 2018-05-19 stsp switch (ch) {
3254 17020d27 2019-03-07 stsp usage_diff();
3255 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3259 26ed57b2 2018-05-19 stsp argc -= optind;
3260 26ed57b2 2018-05-19 stsp argv += optind;
3262 26ed57b2 2018-05-19 stsp if (argc == 0) {
3263 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3264 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3265 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
3266 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
3267 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
3268 15a94983 2018-12-23 stsp id_str1 = argv[0];
3269 15a94983 2018-12-23 stsp id_str2 = argv[1];
3270 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
3271 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
3272 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
3273 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
3274 15a94983 2018-12-23 stsp id_str1 = argv[1];
3275 15a94983 2018-12-23 stsp id_str2 = argv[2];
3277 26ed57b2 2018-05-19 stsp usage_diff();
3279 a915003a 2019-02-05 stsp init_curses();
3281 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3282 eb6600df 2019-01-04 stsp if (error)
3283 eb6600df 2019-01-04 stsp goto done;
3285 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3286 26ed57b2 2018-05-19 stsp if (error)
3287 26ed57b2 2018-05-19 stsp goto done;
3289 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
3290 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3291 26ed57b2 2018-05-19 stsp if (error)
3292 26ed57b2 2018-05-19 stsp goto done;
3294 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
3295 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3296 26ed57b2 2018-05-19 stsp if (error)
3297 26ed57b2 2018-05-19 stsp goto done;
3299 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3300 8b473291 2019-02-21 stsp if (error)
3301 8b473291 2019-02-21 stsp goto done;
3303 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3304 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3305 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3306 ea5e7bb5 2018-08-01 stsp goto done;
3308 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3309 5dc9f4bc 2018-08-04 stsp if (error)
3310 5dc9f4bc 2018-08-04 stsp goto done;
3311 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3313 c02c541e 2019-03-29 stsp free(repo_path);
3315 921be706 2019-06-28 stsp got_repo_close(repo);
3316 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3317 26ed57b2 2018-05-19 stsp return error;
3320 4ed7e80c 2018-05-20 stsp __dead static void
3321 9f7d7167 2018-04-29 stsp usage_blame(void)
3324 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3325 9f7d7167 2018-04-29 stsp getprogname());
3329 84451b3e 2018-07-10 stsp struct tog_blame_line {
3330 84451b3e 2018-07-10 stsp int annotated;
3331 84451b3e 2018-07-10 stsp struct got_object_id *id;
3334 4ed7e80c 2018-05-20 stsp static const struct got_error *
3335 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3336 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
3337 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
3338 11b20872 2019-11-08 stsp int *last_displayed_line, int *eof, int max_lines,
3339 11b20872 2019-11-08 stsp struct tog_colors *colors)
3341 84451b3e 2018-07-10 stsp const struct got_error *err;
3342 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3343 84451b3e 2018-07-10 stsp char *line;
3344 84451b3e 2018-07-10 stsp size_t len;
3345 84451b3e 2018-07-10 stsp wchar_t *wline;
3346 27a741e5 2019-09-11 stsp int width;
3347 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3348 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3349 ab089a2a 2018-07-12 stsp char *id_str;
3350 11b20872 2019-11-08 stsp struct tog_color *tc;
3352 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
3354 ab089a2a 2018-07-12 stsp return err;
3356 84451b3e 2018-07-10 stsp rewind(f);
3357 f7d12f7e 2018-08-01 stsp werase(view->window);
3359 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3360 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3361 ab089a2a 2018-07-12 stsp free(id_str);
3362 ab089a2a 2018-07-12 stsp return err;
3365 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3366 ab089a2a 2018-07-12 stsp free(line);
3367 2550e4c3 2018-07-13 stsp line = NULL;
3369 1cae65b4 2019-09-22 stsp return err;
3370 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3371 a3404814 2018-09-02 stsp wstandout(view->window);
3372 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3374 11b20872 2019-11-08 stsp wattr_on(view->window,
3375 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3376 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3378 11b20872 2019-11-08 stsp wattr_off(view->window,
3379 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3380 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3381 a3404814 2018-09-02 stsp wstandend(view->window);
3382 2550e4c3 2018-07-13 stsp free(wline);
3383 2550e4c3 2018-07-13 stsp wline = NULL;
3384 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3385 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3387 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3388 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
3389 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
3390 ab089a2a 2018-07-12 stsp free(id_str);
3391 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3393 ab089a2a 2018-07-12 stsp free(id_str);
3394 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3395 3f60a8ef 2018-07-10 stsp free(line);
3396 2550e4c3 2018-07-13 stsp line = NULL;
3398 3f60a8ef 2018-07-10 stsp return err;
3399 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3400 2550e4c3 2018-07-13 stsp free(wline);
3401 2550e4c3 2018-07-13 stsp wline = NULL;
3402 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3403 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3406 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
3407 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
3408 84451b3e 2018-07-10 stsp if (line == NULL) {
3412 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
3413 84451b3e 2018-07-10 stsp free(line);
3417 27a741e5 2019-09-11 stsp if (view->ncols <= 9) {
3418 27a741e5 2019-09-11 stsp width = 9;
3419 27a741e5 2019-09-11 stsp wline = wcsdup(L"");
3420 27a741e5 2019-09-11 stsp if (wline == NULL)
3421 27a741e5 2019-09-11 stsp err = got_error_from_errno("wcsdup");
3423 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line,
3424 27a741e5 2019-09-11 stsp view->ncols - 9, 9);
3425 27a741e5 2019-09-11 stsp width += 9;
3427 84451b3e 2018-07-10 stsp if (err) {
3428 84451b3e 2018-07-10 stsp free(line);
3429 84451b3e 2018-07-10 stsp return err;
3432 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3433 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3435 8d0fe45a 2019-08-12 stsp if (nlines > 0) {
3436 8d0fe45a 2019-08-12 stsp blame_line = &lines[lineno - 1];
3437 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3438 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3439 27a741e5 2019-09-11 stsp !(view->focussed &&
3440 27a741e5 2019-09-11 stsp nprinted == selected_line - 1)) {
3441 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3442 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3443 8d0fe45a 2019-08-12 stsp char *id_str;
3444 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3445 8d0fe45a 2019-08-12 stsp if (err) {
3446 8d0fe45a 2019-08-12 stsp free(line);
3447 8d0fe45a 2019-08-12 stsp free(wline);
3448 8d0fe45a 2019-08-12 stsp return err;
3450 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3452 11b20872 2019-11-08 stsp wattr_on(view->window,
3453 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3454 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3456 11b20872 2019-11-08 stsp wattr_off(view->window,
3457 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3458 8d0fe45a 2019-08-12 stsp free(id_str);
3459 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3461 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3462 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3465 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3466 ee41ec32 2018-07-10 stsp prev_id = NULL;
3469 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3470 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3471 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3473 27a741e5 2019-09-11 stsp waddwstr(view->window, wline);
3474 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
3475 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
3476 84451b3e 2018-07-10 stsp if (++nprinted == 1)
3477 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
3478 84451b3e 2018-07-10 stsp free(line);
3479 2550e4c3 2018-07-13 stsp free(wline);
3480 2550e4c3 2018-07-13 stsp wline = NULL;
3482 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
3484 1a57306a 2018-09-02 stsp view_vborder(view);
3486 84451b3e 2018-07-10 stsp return NULL;
3489 84451b3e 2018-07-10 stsp static const struct got_error *
3490 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3492 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
3493 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
3494 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
3495 1a76625f 2018-10-22 stsp int errcode;
3497 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
3498 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3499 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3501 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3502 1a76625f 2018-10-22 stsp if (errcode)
3503 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3505 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3506 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3507 d68a0a7d 2018-07-10 stsp goto done;
3510 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3511 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3513 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3514 d68a0a7d 2018-07-10 stsp if (line->annotated)
3515 d68a0a7d 2018-07-10 stsp goto done;
3517 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3518 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3519 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3520 84451b3e 2018-07-10 stsp goto done;
3522 84451b3e 2018-07-10 stsp line->annotated = 1;
3524 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3525 1a76625f 2018-10-22 stsp if (errcode)
3526 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3527 84451b3e 2018-07-10 stsp return err;
3530 84451b3e 2018-07-10 stsp static void *
3531 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3533 18430de3 2018-07-10 stsp const struct got_error *err;
3534 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3535 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3536 1a76625f 2018-10-22 stsp int errcode;
3538 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
3540 61266923 2020-01-14 stsp return (void *)err;
3542 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
3543 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3544 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
3545 fc06ba56 2019-08-22 stsp err = NULL;
3547 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3548 1a76625f 2018-10-22 stsp if (errcode)
3549 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3550 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3552 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3553 c9beca56 2018-07-22 stsp ta->repo = NULL;
3554 c9beca56 2018-07-22 stsp *ta->complete = 1;
3556 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3557 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3558 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3560 18430de3 2018-07-10 stsp return (void *)err;
3563 245d91c1 2018-07-12 stsp static struct got_object_id *
3564 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3565 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
3567 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3569 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
3570 8d0fe45a 2019-08-12 stsp return NULL;
3572 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3573 245d91c1 2018-07-12 stsp if (!line->annotated)
3574 245d91c1 2018-07-12 stsp return NULL;
3576 245d91c1 2018-07-12 stsp return line->id;
3579 b880a918 2018-07-10 stsp static const struct got_error *
3580 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3582 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3585 245d91c1 2018-07-12 stsp if (blame->thread) {
3586 1a76625f 2018-10-22 stsp int errcode;
3587 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3588 1a76625f 2018-10-22 stsp if (errcode)
3589 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3590 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3591 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3592 1a76625f 2018-10-22 stsp if (errcode)
3593 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3594 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3595 1a76625f 2018-10-22 stsp if (errcode)
3596 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3597 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3598 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3599 245d91c1 2018-07-12 stsp err = NULL;
3600 245d91c1 2018-07-12 stsp blame->thread = NULL;
3602 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3603 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3604 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3606 245d91c1 2018-07-12 stsp if (blame->f) {
3607 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3608 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3609 245d91c1 2018-07-12 stsp blame->f = NULL;
3611 57670559 2018-12-23 stsp if (blame->lines) {
3612 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3613 57670559 2018-12-23 stsp free(blame->lines[i].id);
3614 57670559 2018-12-23 stsp free(blame->lines);
3615 57670559 2018-12-23 stsp blame->lines = NULL;
3617 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3618 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3620 245d91c1 2018-07-12 stsp return err;
3623 245d91c1 2018-07-12 stsp static const struct got_error *
3624 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
3626 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
3627 fc06ba56 2019-08-22 stsp int *done = arg;
3628 fc06ba56 2019-08-22 stsp int errcode;
3630 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3631 fc06ba56 2019-08-22 stsp if (errcode)
3632 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3633 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
3635 fc06ba56 2019-08-22 stsp if (*done)
3636 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
3638 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3639 fc06ba56 2019-08-22 stsp if (errcode)
3640 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3641 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
3643 fc06ba56 2019-08-22 stsp return err;
3646 fc06ba56 2019-08-22 stsp static const struct got_error *
3647 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3648 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3649 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3650 245d91c1 2018-07-12 stsp struct got_repository *repo)
3652 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3653 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3654 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3655 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3656 15a94983 2018-12-23 stsp int obj_type;
3658 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3660 15a94983 2018-12-23 stsp return err;
3661 15a94983 2018-12-23 stsp if (obj_id == NULL)
3662 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
3664 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3666 84451b3e 2018-07-10 stsp goto done;
3668 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3669 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3670 84451b3e 2018-07-10 stsp goto done;
3673 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3675 a70480e0 2018-06-23 stsp goto done;
3676 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3677 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3678 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3679 84451b3e 2018-07-10 stsp goto done;
3681 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3682 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
3683 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
3684 84451b3e 2018-07-10 stsp goto done;
3686 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3687 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3688 b02560ec 2019-08-19 stsp blame->nlines--;
3690 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3691 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3692 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3693 84451b3e 2018-07-10 stsp goto done;
3696 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3698 bd24772e 2018-07-11 stsp goto done;
3700 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3701 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3702 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3703 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3704 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3705 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3706 245d91c1 2018-07-12 stsp goto done;
3708 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3710 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3711 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3712 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3713 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3714 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
3715 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_arg = done;
3716 245d91c1 2018-07-12 stsp *blame_complete = 0;
3720 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3721 27d434c2 2018-09-15 stsp free(obj_id);
3723 245d91c1 2018-07-12 stsp stop_blame(blame);
3724 245d91c1 2018-07-12 stsp return err;
3727 245d91c1 2018-07-12 stsp static const struct got_error *
3728 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3729 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3730 8b473291 2019-02-21 stsp struct got_repository *repo)
3732 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3733 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3735 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3737 c4843652 2019-08-12 stsp s->path = strdup(path);
3738 c4843652 2019-08-12 stsp if (s->path == NULL)
3739 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
3741 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3742 c4843652 2019-08-12 stsp if (err) {
3743 c4843652 2019-08-12 stsp free(s->path);
3744 7cbe629d 2018-08-04 stsp return err;
3747 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3748 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3749 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3750 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3751 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3752 fb2756b9 2018-08-04 stsp s->repo = repo;
3753 8b473291 2019-02-21 stsp s->refs = refs;
3754 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3755 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3757 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
3758 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3759 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3760 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3762 11b20872 2019-11-08 stsp return err;
3765 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3766 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3767 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3768 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
3769 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
3771 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3772 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3773 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3774 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3777 e5a0f69f 2018-08-18 stsp static const struct got_error *
3778 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3780 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3781 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3783 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3784 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3786 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3787 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3788 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3789 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3790 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3793 e5a0f69f 2018-08-18 stsp free(s->path);
3794 11b20872 2019-11-08 stsp free_colors(&s->colors);
3796 e5a0f69f 2018-08-18 stsp return err;
3799 7cbe629d 2018-08-04 stsp static const struct got_error *
3800 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
3802 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3804 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
3805 6c4c42e0 2019-06-24 stsp return NULL;
3808 6c4c42e0 2019-06-24 stsp static const struct got_error *
3809 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
3811 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3812 6c4c42e0 2019-06-24 stsp int lineno;
3814 6c4c42e0 2019-06-24 stsp if (!view->searching) {
3815 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3816 6c4c42e0 2019-06-24 stsp return NULL;
3819 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3820 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3821 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
3823 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
3825 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3826 6c4c42e0 2019-06-24 stsp lineno = 1;
3828 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3831 6c4c42e0 2019-06-24 stsp while (1) {
3832 6c4c42e0 2019-06-24 stsp char *line = NULL;
3833 6c4c42e0 2019-06-24 stsp off_t offset;
3834 6c4c42e0 2019-06-24 stsp size_t len;
3836 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
3837 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
3838 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3839 6c4c42e0 2019-06-24 stsp free(line);
3843 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3844 6c4c42e0 2019-06-24 stsp lineno = 1;
3846 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3849 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
3850 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3851 6c4c42e0 2019-06-24 stsp free(line);
3852 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
3854 6c4c42e0 2019-06-24 stsp free(line);
3855 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
3856 2246482e 2019-06-25 stsp if (line && match_line(line, &view->regex)) {
3857 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3858 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
3859 6c4c42e0 2019-06-24 stsp free(line);
3862 6c4c42e0 2019-06-24 stsp free(line);
3863 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3869 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3870 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
3871 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
3874 6c4c42e0 2019-06-24 stsp return NULL;
3877 6c4c42e0 2019-06-24 stsp static const struct got_error *
3878 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
3880 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3881 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
3882 2b380cc8 2018-10-24 stsp int errcode;
3884 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
3885 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3886 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
3887 2b380cc8 2018-10-24 stsp if (errcode)
3888 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3890 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
3893 51fe7530 2019-08-19 stsp if (s->blame_complete)
3894 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
3896 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3897 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3898 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
3899 11b20872 2019-11-08 stsp &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
3901 669b5ffa 2018-10-07 stsp view_vborder(view);
3902 e5a0f69f 2018-08-18 stsp return err;
3905 e5a0f69f 2018-08-18 stsp static const struct got_error *
3906 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3907 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3909 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
3910 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
3911 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3912 669b5ffa 2018-10-07 stsp int begin_x = 0;
3914 e5a0f69f 2018-08-18 stsp switch (ch) {
3916 1e37a5c2 2019-05-12 jcs s->done = 1;
3919 1e37a5c2 2019-05-12 jcs case KEY_UP:
3920 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
3921 1e37a5c2 2019-05-12 jcs s->selected_line--;
3922 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
3923 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3924 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3926 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3927 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
3928 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3931 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3932 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3933 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3935 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3938 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3939 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3940 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3941 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3942 1e37a5c2 2019-05-12 jcs s->selected_line++;
3943 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3944 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3945 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3948 1e37a5c2 2019-05-12 jcs case 'p': {
3949 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3950 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3951 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3952 1e37a5c2 2019-05-12 jcs if (id == NULL)
3954 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3955 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3956 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3957 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3958 1e37a5c2 2019-05-12 jcs int obj_type;
3959 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3960 1e37a5c2 2019-05-12 jcs s->repo, id);
3963 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3964 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3965 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3966 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3969 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3970 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3971 1e37a5c2 2019-05-12 jcs pid->id, s->path);
3972 e5a0f69f 2018-08-18 stsp if (err) {
3973 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
3974 1e37a5c2 2019-05-12 jcs err = NULL;
3975 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3978 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
3980 1e37a5c2 2019-05-12 jcs free(blob_id);
3981 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
3982 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
3983 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3986 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3988 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3990 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
3991 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
3993 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3998 1e37a5c2 2019-05-12 jcs s->done = 1;
3999 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4000 1e37a5c2 2019-05-12 jcs s->done = 0;
4001 1e37a5c2 2019-05-12 jcs if (thread_err)
4003 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4004 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4005 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4006 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4007 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
4008 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
4013 1e37a5c2 2019-05-12 jcs case 'B': {
4014 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4015 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
4016 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4018 1e37a5c2 2019-05-12 jcs s->done = 1;
4019 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4020 1e37a5c2 2019-05-12 jcs s->done = 0;
4021 1e37a5c2 2019-05-12 jcs if (thread_err)
4023 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4024 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4025 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4026 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4027 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4028 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4029 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
4030 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
4035 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4036 1e37a5c2 2019-05-12 jcs case '\r': {
4037 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4038 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4039 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4040 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4041 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4042 1e37a5c2 2019-05-12 jcs if (id == NULL)
4044 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4047 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4048 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4049 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4050 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4051 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4052 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4053 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4054 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4057 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4058 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
4059 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4061 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4064 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4065 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4068 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
4070 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4073 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
4074 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4076 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4081 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4083 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4084 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4085 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4088 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4089 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4090 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4091 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4094 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4095 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4096 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4097 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4099 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4100 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4101 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4103 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4104 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4105 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4106 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4112 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4115 a70480e0 2018-06-23 stsp static const struct got_error *
4116 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4118 a70480e0 2018-06-23 stsp const struct got_error *error;
4119 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4120 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4121 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4122 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4123 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4124 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4126 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4128 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4130 a70480e0 2018-06-23 stsp #ifndef PROFILE
4131 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4132 8e94dd5b 2019-01-04 stsp NULL) == -1)
4133 a70480e0 2018-06-23 stsp err(1, "pledge");
4136 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4137 a70480e0 2018-06-23 stsp switch (ch) {
4139 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4142 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4143 69069811 2018-08-02 stsp if (repo_path == NULL)
4144 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4148 17020d27 2019-03-07 stsp usage_blame();
4149 a70480e0 2018-06-23 stsp /* NOTREACHED */
4153 a70480e0 2018-06-23 stsp argc -= optind;
4154 a70480e0 2018-06-23 stsp argv += optind;
4156 69069811 2018-08-02 stsp if (argc == 1)
4157 69069811 2018-08-02 stsp path = argv[0];
4159 a70480e0 2018-06-23 stsp usage_blame();
4161 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
4162 69069811 2018-08-02 stsp if (cwd == NULL) {
4163 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4164 69069811 2018-08-02 stsp goto done;
4166 69069811 2018-08-02 stsp if (repo_path == NULL) {
4167 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4168 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4169 69069811 2018-08-02 stsp goto done;
4171 eb41ed75 2019-02-05 stsp error = NULL;
4172 eb41ed75 2019-02-05 stsp if (worktree) {
4173 eb41ed75 2019-02-05 stsp repo_path =
4174 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4175 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
4176 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4177 eb41ed75 2019-02-05 stsp if (error)
4178 eb41ed75 2019-02-05 stsp goto done;
4180 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4181 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
4182 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4183 eb41ed75 2019-02-05 stsp goto done;
4188 a915003a 2019-02-05 stsp init_curses();
4190 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4191 c02c541e 2019-03-29 stsp if (error != NULL)
4192 8e94dd5b 2019-01-04 stsp goto done;
4194 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4195 c02c541e 2019-03-29 stsp if (error)
4196 92205607 2019-01-04 stsp goto done;
4198 eb41ed75 2019-02-05 stsp if (worktree) {
4199 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4200 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4201 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4202 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4203 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4204 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4205 eb41ed75 2019-02-05 stsp path) == -1) {
4206 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4207 eb41ed75 2019-02-05 stsp goto done;
4209 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4212 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4214 eb41ed75 2019-02-05 stsp if (error)
4215 69069811 2018-08-02 stsp goto done;
4217 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4218 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4219 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4220 a70480e0 2018-06-23 stsp if (error != NULL)
4221 66b4983c 2018-06-23 stsp goto done;
4222 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4223 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4225 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4226 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4228 a19e88aa 2018-06-23 stsp if (error != NULL)
4229 8b473291 2019-02-21 stsp goto done;
4231 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4232 8b473291 2019-02-21 stsp if (error)
4233 a19e88aa 2018-06-23 stsp goto done;
4235 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4236 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4237 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4238 e1cd8fed 2018-08-01 stsp goto done;
4240 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4241 7cbe629d 2018-08-04 stsp if (error)
4242 7cbe629d 2018-08-04 stsp goto done;
4243 12314ad4 2019-08-31 stsp if (worktree) {
4244 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4245 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4246 12314ad4 2019-08-31 stsp worktree = NULL;
4248 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4250 69069811 2018-08-02 stsp free(repo_path);
4251 69069811 2018-08-02 stsp free(cwd);
4252 a70480e0 2018-06-23 stsp free(commit_id);
4253 eb41ed75 2019-02-05 stsp if (worktree)
4254 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4256 a70480e0 2018-06-23 stsp got_repo_close(repo);
4257 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4258 a70480e0 2018-06-23 stsp return error;
4261 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4262 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
4263 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
4264 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
4265 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
4266 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
4267 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int selected, int limit,
4268 f26dddb7 2019-11-08 stsp int isroot, struct tog_colors *colors)
4270 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4271 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4272 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4273 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4274 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4276 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
4278 f7d12f7e 2018-08-01 stsp werase(view->window);
4280 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4281 ffd1d5e5 2018-06-23 stsp return NULL;
4283 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, label, view->ncols, 0);
4285 ffd1d5e5 2018-06-23 stsp return err;
4286 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4287 a3404814 2018-09-02 stsp wstandout(view->window);
4288 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
4290 11b20872 2019-11-08 stsp wattr_on(view->window,
4291 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4292 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4294 11b20872 2019-11-08 stsp wattr_off(view->window,
4295 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4296 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4297 a3404814 2018-09-02 stsp wstandend(view->window);
4298 2550e4c3 2018-07-13 stsp free(wline);
4299 2550e4c3 2018-07-13 stsp wline = NULL;
4300 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4301 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4302 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4303 ffd1d5e5 2018-06-23 stsp return NULL;
4304 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4306 ce52c690 2018-06-23 stsp return err;
4307 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4308 2550e4c3 2018-07-13 stsp free(wline);
4309 2550e4c3 2018-07-13 stsp wline = NULL;
4310 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4311 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4312 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4313 ffd1d5e5 2018-06-23 stsp return NULL;
4314 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4315 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4316 a1eca9bb 2018-06-23 stsp return NULL;
4318 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
4319 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(tree);
4320 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
4321 0cf4efb1 2018-09-29 stsp if (view->focussed)
4322 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4323 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
4325 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4326 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
4327 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4328 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4329 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4330 ffd1d5e5 2018-06-23 stsp return NULL;
4334 56e0773d 2019-11-28 stsp te = *first_displayed_entry;
4337 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4338 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4339 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
4340 848d6979 2019-08-12 stsp const char *modestr = "";
4341 56e0773d 2019-11-28 stsp mode_t mode;
4343 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4344 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4346 1d13200f 2018-07-12 stsp if (show_ids) {
4347 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4348 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4350 638f9024 2019-05-13 stsp return got_error_from_errno(
4351 230a42bd 2019-05-11 jcs "got_object_id_str");
4353 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4354 63c5ca5d 2019-08-24 stsp modestr = "$";
4355 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
4356 848d6979 2019-08-12 stsp modestr = "@";
4357 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4358 848d6979 2019-08-12 stsp modestr = "/";
4359 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4360 848d6979 2019-08-12 stsp modestr = "*";
4361 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4362 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te), modestr) == -1) {
4363 1d13200f 2018-07-12 stsp free(id_str);
4364 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4366 1d13200f 2018-07-12 stsp free(id_str);
4367 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4368 ffd1d5e5 2018-06-23 stsp if (err) {
4369 ffd1d5e5 2018-06-23 stsp free(line);
4372 ffd1d5e5 2018-06-23 stsp if (n == selected) {
4373 0cf4efb1 2018-09-29 stsp if (view->focussed)
4374 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4375 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
4377 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
4379 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4380 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4381 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4383 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4384 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4385 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4386 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4387 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
4388 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4389 ffd1d5e5 2018-06-23 stsp free(line);
4390 2550e4c3 2018-07-13 stsp free(wline);
4391 2550e4c3 2018-07-13 stsp wline = NULL;
4393 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4394 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
4395 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4399 ffd1d5e5 2018-06-23 stsp return err;
4402 ffd1d5e5 2018-06-23 stsp static void
4403 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
4404 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
4405 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int isroot)
4407 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4410 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
4413 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, 0);
4414 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
4415 ffd1d5e5 2018-06-23 stsp if (!isroot)
4416 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4421 56e0773d 2019-11-28 stsp while (*first_displayed_entry && i < maxscroll) {
4422 56e0773d 2019-11-28 stsp *first_displayed_entry = got_tree_entry_get_prev(tree,
4423 56e0773d 2019-11-28 stsp *first_displayed_entry);
4426 56e0773d 2019-11-28 stsp if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4427 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4430 768394f3 2019-01-24 stsp static int
4431 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4432 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
4433 56e0773d 2019-11-28 stsp struct got_tree_object *tree)
4435 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4436 ffd1d5e5 2018-06-23 stsp int n = 0;
4438 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
4439 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, *first_displayed_entry);
4441 56e0773d 2019-11-28 stsp next = got_object_tree_get_first_entry(tree);
4443 768394f3 2019-01-24 stsp last = last_displayed_entry;
4444 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4445 56e0773d 2019-11-28 stsp last = got_tree_entry_get_next(tree, last);
4446 768394f3 2019-01-24 stsp if (last) {
4447 768394f3 2019-01-24 stsp *first_displayed_entry = next;
4448 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, next);
4454 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4455 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4456 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4458 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4459 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4460 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4462 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4463 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
4464 56e0773d 2019-11-28 stsp + 1 /* slash */;
4466 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
4468 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4469 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4470 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4472 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4473 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4474 d9765a41 2018-06-23 stsp while (pt) {
4475 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
4476 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
4477 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4478 cb2ebc8a 2018-06-23 stsp goto done;
4480 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
4481 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4482 cb2ebc8a 2018-06-23 stsp goto done;
4484 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4487 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4488 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4489 ce52c690 2018-06-23 stsp goto done;
4493 ce52c690 2018-06-23 stsp if (err) {
4494 ce52c690 2018-06-23 stsp free(*path);
4495 ce52c690 2018-06-23 stsp *path = NULL;
4497 ce52c690 2018-06-23 stsp return err;
4500 ce52c690 2018-06-23 stsp static const struct got_error *
4501 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
4502 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4503 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4504 8b473291 2019-02-21 stsp struct got_repository *repo)
4506 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
4507 ce52c690 2018-06-23 stsp char *path;
4508 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
4510 a0de39f3 2019-08-09 stsp *new_view = NULL;
4512 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
4514 ce52c690 2018-06-23 stsp return err;
4516 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4517 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
4518 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
4519 83ce39e3 2019-08-12 stsp goto done;
4522 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
4523 e5a0f69f 2018-08-18 stsp if (err) {
4524 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
4525 fc06ba56 2019-08-22 stsp err = NULL;
4526 e5a0f69f 2018-08-18 stsp view_close(blame_view);
4528 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
4530 83ce39e3 2019-08-12 stsp free(path);
4531 69efd4c4 2018-07-18 stsp return err;
4534 69efd4c4 2018-07-18 stsp static const struct got_error *
4535 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
4536 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4537 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4538 8b473291 2019-02-21 stsp struct got_repository *repo)
4540 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
4541 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
4542 69efd4c4 2018-07-18 stsp char *path;
4544 a0de39f3 2019-08-09 stsp *new_view = NULL;
4546 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4547 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
4548 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
4550 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
4552 69efd4c4 2018-07-18 stsp return err;
4554 d01904d4 2019-06-25 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4556 e5a0f69f 2018-08-18 stsp view_close(log_view);
4558 e5a0f69f 2018-08-18 stsp *new_view = log_view;
4559 cb2ebc8a 2018-06-23 stsp free(path);
4560 cb2ebc8a 2018-06-23 stsp return err;
4563 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4564 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
4565 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4566 8b473291 2019-02-21 stsp struct got_repository *repo)
4568 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4569 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
4570 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4572 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
4574 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
4575 ffd1d5e5 2018-06-23 stsp if (err != NULL)
4576 ffd1d5e5 2018-06-23 stsp goto done;
4578 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4579 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4580 ffd1d5e5 2018-06-23 stsp goto done;
4583 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
4584 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4585 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4586 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
4587 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
4588 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4589 6484ec90 2018-09-29 stsp goto done;
4591 8b473291 2019-02-21 stsp s->refs = refs;
4592 fb2756b9 2018-08-04 stsp s->repo = repo;
4594 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4596 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4597 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
4598 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
4599 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4601 c0b01bdb 2019-11-08 stsp goto done;
4602 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4603 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
4604 c0b01bdb 2019-11-08 stsp if (err) {
4605 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4606 c0b01bdb 2019-11-08 stsp goto done;
4608 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
4609 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
4610 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4611 c0b01bdb 2019-11-08 stsp if (err) {
4612 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4613 c0b01bdb 2019-11-08 stsp goto done;
4616 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
4617 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
4618 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4619 11b20872 2019-11-08 stsp if (err) {
4620 11b20872 2019-11-08 stsp free_colors(&s->colors);
4621 11b20872 2019-11-08 stsp goto done;
4624 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4625 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4626 c0b01bdb 2019-11-08 stsp if (err) {
4627 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4628 c0b01bdb 2019-11-08 stsp goto done;
4632 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
4633 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
4634 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
4635 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
4636 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
4638 ad80ab7b 2018-08-04 stsp free(commit_id_str);
4639 6484ec90 2018-09-29 stsp if (err) {
4640 fb2756b9 2018-08-04 stsp free(s->tree_label);
4641 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4643 ad80ab7b 2018-08-04 stsp return err;
4646 e5a0f69f 2018-08-18 stsp static const struct got_error *
4647 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
4649 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4651 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4652 fb2756b9 2018-08-04 stsp free(s->tree_label);
4653 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4654 6484ec90 2018-09-29 stsp free(s->commit_id);
4655 6484ec90 2018-09-29 stsp s->commit_id = NULL;
4656 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
4657 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
4658 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
4659 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
4660 ad80ab7b 2018-08-04 stsp free(parent);
4663 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
4664 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
4665 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
4667 7c32bd05 2019-06-22 stsp return NULL;
4670 7c32bd05 2019-06-22 stsp static const struct got_error *
4671 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
4673 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4675 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
4676 7c32bd05 2019-06-22 stsp return NULL;
4679 7c32bd05 2019-06-22 stsp static int
4680 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4682 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
4684 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4688 7c32bd05 2019-06-22 stsp static const struct got_error *
4689 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
4691 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4692 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
4694 7c32bd05 2019-06-22 stsp if (!view->searching) {
4695 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4696 7c32bd05 2019-06-22 stsp return NULL;
4699 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4700 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
4701 7c32bd05 2019-06-22 stsp if (s->selected_entry)
4702 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
4703 56e0773d 2019-11-28 stsp s->selected_entry);
4705 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4707 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
4708 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4710 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
4711 56e0773d 2019-11-28 stsp s->selected_entry);
4714 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4715 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4717 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4720 7c32bd05 2019-06-22 stsp while (1) {
4721 56e0773d 2019-11-28 stsp if (te == NULL) {
4722 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
4723 ac66afb8 2019-06-24 stsp view->search_next_done = 1;
4724 ac66afb8 2019-06-24 stsp return NULL;
4726 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4727 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4729 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4732 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
4733 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4734 56e0773d 2019-11-28 stsp s->matched_entry = te;
4738 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4739 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
4741 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
4744 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4745 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
4746 7c32bd05 2019-06-22 stsp s->selected = 0;
4749 e5a0f69f 2018-08-18 stsp return NULL;
4752 ad80ab7b 2018-08-04 stsp static const struct got_error *
4753 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
4755 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
4756 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4757 e5a0f69f 2018-08-18 stsp char *parent_path;
4759 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
4761 e5a0f69f 2018-08-18 stsp return err;
4763 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
4764 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
4765 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4766 56e0773d 2019-11-28 stsp s->tree, s->selected, view->nlines, s->tree == s->root,
4767 bddb1296 2019-11-08 stsp &s->colors);
4768 e5a0f69f 2018-08-18 stsp free(parent_path);
4770 669b5ffa 2018-10-07 stsp view_vborder(view);
4771 e5a0f69f 2018-08-18 stsp return err;
4774 e5a0f69f 2018-08-18 stsp static const struct got_error *
4775 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4776 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4778 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4779 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
4780 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
4781 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
4783 e5a0f69f 2018-08-18 stsp switch (ch) {
4785 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
4788 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
4790 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4791 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4792 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
4793 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
4794 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
4795 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4796 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4798 1e37a5c2 2019-05-12 jcs return err;
4799 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
4801 1e37a5c2 2019-05-12 jcs view_close(log_view);
4804 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
4805 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4807 1e37a5c2 2019-05-12 jcs *new_view = log_view;
4810 1e37a5c2 2019-05-12 jcs case KEY_UP:
4811 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
4812 1e37a5c2 2019-05-12 jcs s->selected--;
4813 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
4816 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
4818 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
4819 56e0773d 2019-11-28 stsp s->tree, s->tree == s->root);
4821 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4822 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
4823 56e0773d 2019-11-28 stsp MAX(0, view->nlines - 4 - s->selected), s->tree,
4824 1e37a5c2 2019-05-12 jcs s->tree == s->root);
4825 1e37a5c2 2019-05-12 jcs s->selected = 0;
4826 56e0773d 2019-11-28 stsp if (got_object_tree_get_first_entry(s->tree) ==
4827 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
4828 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
4831 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4832 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
4833 1e37a5c2 2019-05-12 jcs s->selected++;
4836 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4838 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
4840 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
4841 56e0773d 2019-11-28 stsp s->last_displayed_entry, s->tree);
4843 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4844 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4846 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
4847 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
4848 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4851 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
4852 56e0773d 2019-11-28 stsp view->nlines, s->last_displayed_entry, s->tree);
4853 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
4854 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
4855 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
4856 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
4858 1e37a5c2 2019-05-12 jcs ndisplayed++;
4859 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
4860 1e37a5c2 2019-05-12 jcs } while (te);
4861 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
4864 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4866 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4867 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4868 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
4869 1e37a5c2 2019-05-12 jcs /* user selected '..' */
4870 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
4872 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
4873 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
4875 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
4876 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
4877 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
4878 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
4879 1e37a5c2 2019-05-12 jcs s->selected_entry =
4880 1e37a5c2 2019-05-12 jcs parent->selected_entry;
4881 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
4882 1e37a5c2 2019-05-12 jcs free(parent);
4883 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
4884 56e0773d 2019-11-28 stsp s->selected_entry))) {
4885 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
4886 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
4887 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
4890 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
4891 941e9f74 2019-05-21 stsp if (err) {
4892 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
4895 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
4896 56e0773d 2019-11-28 stsp s->selected_entry))) {
4897 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
4898 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
4899 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
4901 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
4902 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
4903 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
4906 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
4907 669b5ffa 2018-10-07 stsp err = view_close_child(view);
4909 669b5ffa 2018-10-07 stsp return err;
4910 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
4911 669b5ffa 2018-10-07 stsp if (err) {
4912 1e37a5c2 2019-05-12 jcs view_close(blame_view);
4915 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
4916 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
4918 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
4921 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4922 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
4923 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4929 ffd1d5e5 2018-06-23 stsp return err;
4932 ffd1d5e5 2018-06-23 stsp __dead static void
4933 ffd1d5e5 2018-06-23 stsp usage_tree(void)
4936 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4937 ffd1d5e5 2018-06-23 stsp getprogname());
4941 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4942 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
4944 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
4945 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
4946 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4947 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
4948 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4949 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
4950 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
4951 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
4953 5221c383 2018-08-01 stsp struct tog_view *view;
4955 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4957 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
4958 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4959 d188b9a6 2019-01-04 stsp NULL) == -1)
4960 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
4963 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
4964 ffd1d5e5 2018-06-23 stsp switch (ch) {
4966 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
4969 17020d27 2019-03-07 stsp usage_tree();
4970 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
4974 ffd1d5e5 2018-06-23 stsp argc -= optind;
4975 ffd1d5e5 2018-06-23 stsp argv += optind;
4977 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
4978 52185f70 2019-02-05 stsp struct got_worktree *worktree;
4979 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
4980 52185f70 2019-02-05 stsp if (cwd == NULL)
4981 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
4982 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4983 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4984 52185f70 2019-02-05 stsp goto done;
4985 52185f70 2019-02-05 stsp if (worktree) {
4986 52185f70 2019-02-05 stsp free(cwd);
4987 52185f70 2019-02-05 stsp repo_path =
4988 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4989 52185f70 2019-02-05 stsp got_worktree_close(worktree);
4991 52185f70 2019-02-05 stsp repo_path = cwd;
4992 52185f70 2019-02-05 stsp if (repo_path == NULL) {
4993 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4994 52185f70 2019-02-05 stsp goto done;
4996 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
4997 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
4998 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
4999 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
5001 f13374f4 2020-01-14 stsp usage_tree();
5003 a915003a 2019-02-05 stsp init_curses();
5005 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5006 c02c541e 2019-03-29 stsp if (error != NULL)
5007 52185f70 2019-02-05 stsp goto done;
5009 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5010 c02c541e 2019-03-29 stsp if (error)
5011 52185f70 2019-02-05 stsp goto done;
5013 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5014 71a27632 2020-01-15 stsp commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5015 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5016 ffd1d5e5 2018-06-23 stsp if (error != NULL)
5017 ffd1d5e5 2018-06-23 stsp goto done;
5019 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5020 ffd1d5e5 2018-06-23 stsp if (error != NULL)
5021 ffd1d5e5 2018-06-23 stsp goto done;
5023 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
5024 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5025 ffd1d5e5 2018-06-23 stsp if (error != NULL)
5026 8b473291 2019-02-21 stsp goto done;
5028 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5029 8b473291 2019-02-21 stsp if (error)
5030 ffd1d5e5 2018-06-23 stsp goto done;
5032 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5033 5221c383 2018-08-01 stsp if (view == NULL) {
5034 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5035 5221c383 2018-08-01 stsp goto done;
5037 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
5038 ad80ab7b 2018-08-04 stsp if (error)
5039 ad80ab7b 2018-08-04 stsp goto done;
5040 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5042 52185f70 2019-02-05 stsp free(repo_path);
5043 ffd1d5e5 2018-06-23 stsp free(commit_id);
5044 ffd1d5e5 2018-06-23 stsp if (commit)
5045 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5047 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5049 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5050 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5051 ffd1d5e5 2018-06-23 stsp return error;
5054 ce5b7c56 2019-07-09 stsp static void
5055 ce5b7c56 2019-07-09 stsp list_commands(void)
5059 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
5060 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
5061 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
5062 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->name);
5064 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
5067 4ed7e80c 2018-05-20 stsp __dead static void
5068 ce5b7c56 2019-07-09 stsp usage(int hflag)
5070 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5071 53ccebc2 2019-07-30 stsp getprogname());
5072 ce5b7c56 2019-07-09 stsp if (hflag)
5073 ce5b7c56 2019-07-09 stsp list_commands();
5077 c2301be8 2018-04-30 stsp static char **
5078 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
5080 c2301be8 2018-04-30 stsp char **argv;
5081 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
5083 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
5084 c2301be8 2018-04-30 stsp if (argv == NULL)
5085 c2301be8 2018-04-30 stsp err(1, "calloc");
5086 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
5087 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
5088 e10c916e 2019-09-15 hiltjo err(1, "strdup");
5089 c2301be8 2018-04-30 stsp if (arg1) {
5090 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
5091 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
5092 e10c916e 2019-09-15 hiltjo err(1, "strdup");
5095 c2301be8 2018-04-30 stsp return argv;
5099 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
5101 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
5102 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
5103 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
5104 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
5105 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
5106 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
5107 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
5110 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
5112 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5113 9f7d7167 2018-04-29 stsp switch (ch) {
5115 9f7d7167 2018-04-29 stsp hflag = 1;
5118 53ccebc2 2019-07-30 stsp Vflag = 1;
5121 ce5b7c56 2019-07-09 stsp usage(hflag);
5122 9f7d7167 2018-04-29 stsp /* NOTREACHED */
5126 9f7d7167 2018-04-29 stsp argc -= optind;
5127 9f7d7167 2018-04-29 stsp argv += optind;
5128 9f7d7167 2018-04-29 stsp optind = 0;
5129 c2301be8 2018-04-30 stsp optreset = 1;
5131 53ccebc2 2019-07-30 stsp if (Vflag) {
5132 53ccebc2 2019-07-30 stsp got_version_print_str();
5136 c2301be8 2018-04-30 stsp if (argc == 0) {
5137 f29d3e89 2018-06-23 stsp if (hflag)
5138 ce5b7c56 2019-07-09 stsp usage(hflag);
5139 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
5140 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
5141 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
5146 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
5147 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
5148 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
5149 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
5150 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
5155 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
5156 d70c3147 2019-07-09 stsp fprintf(stderr, "%s: unknown command '%s'\n",
5157 3642c4c6 2019-07-09 stsp getprogname(), argv[0]);
5158 ce5b7c56 2019-07-09 stsp list_commands();
5163 3642c4c6 2019-07-09 stsp if (hflag)
5164 3642c4c6 2019-07-09 stsp cmd->cmd_usage();
5166 3642c4c6 2019-07-09 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5169 c2301be8 2018-04-30 stsp free(cmd_argv);
5170 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
5171 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);