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 99db9666 2018-05-07 stsp static const struct got_error *
1455 19e70ad6 2019-05-14 stsp get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1456 19e70ad6 2019-05-14 stsp struct got_repository *repo)
1458 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1459 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1461 9ba79e04 2018-06-11 stsp *head_id = NULL;
1463 19e70ad6 2019-05-14 stsp err = got_ref_open(&head_ref, repo, branch_name, 0);
1465 99db9666 2018-05-07 stsp return err;
1467 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1468 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1469 9ba79e04 2018-06-11 stsp if (err) {
1470 9ba79e04 2018-06-11 stsp *head_id = NULL;
1471 99db9666 2018-05-07 stsp return err;
1474 9ba79e04 2018-06-11 stsp return NULL;
1477 0553a4e3 2018-04-30 stsp static const struct got_error *
1478 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1479 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1480 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1481 11b20872 2019-11-08 stsp struct got_reflist_head *refs, const char *path, int commits_needed,
1482 11b20872 2019-11-08 stsp struct tog_colors *colors)
1484 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1485 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1486 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1487 60493ae3 2019-06-20 stsp int width;
1488 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1489 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1490 8b473291 2019-02-21 stsp char *refs_str = NULL;
1491 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1492 11b20872 2019-11-08 stsp struct tog_color *tc;
1493 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1495 e0d42f60 2018-07-22 stsp entry = first;
1496 e0d42f60 2018-07-22 stsp ncommits = 0;
1497 e0d42f60 2018-07-22 stsp while (entry) {
1498 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1499 e0d42f60 2018-07-22 stsp *selected = entry;
1502 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1503 e0d42f60 2018-07-22 stsp ncommits++;
1506 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1507 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1509 ecb28ae0 2018-07-16 stsp return err;
1510 8b473291 2019-02-21 stsp if (refs) {
1511 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, (*selected)->id,
1514 8b473291 2019-02-21 stsp goto done;
1518 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1519 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1521 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1522 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1523 60493ae3 2019-06-20 stsp commits_needed > 0 ?
1524 60493ae3 2019-06-20 stsp (view->searching && view->search_next_done == 0
1525 60493ae3 2019-06-20 stsp ? "searching..." : "loading... ") :
1526 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1527 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1528 8b473291 2019-02-21 stsp goto done;
1531 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1532 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1533 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1534 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1535 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1536 1a76625f 2018-10-22 stsp header = NULL;
1537 1a76625f 2018-10-22 stsp goto done;
1539 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1540 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1541 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1542 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1543 1a76625f 2018-10-22 stsp header = NULL;
1544 1a76625f 2018-10-22 stsp goto done;
1546 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1548 1a76625f 2018-10-22 stsp goto done;
1550 2814baeb 2018-08-01 stsp werase(view->window);
1552 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1553 a3404814 2018-09-02 stsp wstandout(view->window);
1554 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1556 11b20872 2019-11-08 stsp wattr_on(view->window,
1557 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1558 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1560 11b20872 2019-11-08 stsp wattr_off(view->window,
1561 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1562 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1563 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1566 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1567 a3404814 2018-09-02 stsp wstandend(view->window);
1568 ecb28ae0 2018-07-16 stsp free(wline);
1569 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1570 1a76625f 2018-10-22 stsp goto done;
1572 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1573 899d86c2 2018-05-10 stsp entry = first;
1574 5813d178 2019-03-09 stsp ncommits = 0;
1575 5813d178 2019-03-09 stsp while (entry) {
1576 5813d178 2019-03-09 stsp char *author;
1577 5813d178 2019-03-09 stsp wchar_t *wauthor;
1578 5813d178 2019-03-09 stsp int width;
1579 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1581 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1582 5813d178 2019-03-09 stsp if (author == NULL) {
1583 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1584 5813d178 2019-03-09 stsp goto done;
1586 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1587 27a741e5 2019-09-11 stsp date_display_cols);
1588 5813d178 2019-03-09 stsp if (author_cols < width)
1589 5813d178 2019-03-09 stsp author_cols = width;
1590 5813d178 2019-03-09 stsp free(wauthor);
1591 5813d178 2019-03-09 stsp free(author);
1592 7ca04879 2019-10-19 stsp ncommits++;
1593 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1596 5813d178 2019-03-09 stsp entry = first;
1597 899d86c2 2018-05-10 stsp *last = first;
1598 867c6645 2018-07-10 stsp ncommits = 0;
1599 899d86c2 2018-05-10 stsp while (entry) {
1600 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1602 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1603 2814baeb 2018-08-01 stsp wstandout(view->window);
1604 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1605 11b20872 2019-11-08 stsp date_display_cols, author_cols, colors);
1606 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1607 2814baeb 2018-08-01 stsp wstandend(view->window);
1609 60493ae3 2019-06-20 stsp goto done;
1610 0553a4e3 2018-04-30 stsp ncommits++;
1611 899d86c2 2018-05-10 stsp *last = entry;
1612 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1615 1a57306a 2018-09-02 stsp view_vborder(view);
1617 1a76625f 2018-10-22 stsp free(id_str);
1618 8b473291 2019-02-21 stsp free(refs_str);
1619 1a76625f 2018-10-22 stsp free(ncommits_str);
1620 1a76625f 2018-10-22 stsp free(header);
1621 80ddbec8 2018-04-29 stsp return err;
1624 07b55e75 2018-05-10 stsp static void
1625 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1626 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1627 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1629 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1630 07b55e75 2018-05-10 stsp int nscrolled = 0;
1632 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1633 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1636 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1637 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1638 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1639 07b55e75 2018-05-10 stsp if (entry) {
1640 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1641 07b55e75 2018-05-10 stsp nscrolled++;
1646 aa075928 2018-05-10 stsp static const struct got_error *
1647 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1648 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1650 5e224a3e 2019-02-22 stsp int errcode;
1651 8a42fca8 2019-02-22 stsp int max_wait = 20;
1653 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1655 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1656 5e224a3e 2019-02-22 stsp if (*log_complete)
1659 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1660 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1661 7aafa0d1 2019-02-22 stsp if (errcode)
1662 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1663 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1664 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1665 7aafa0d1 2019-02-22 stsp if (errcode)
1666 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1667 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1668 7aafa0d1 2019-02-22 stsp pthread_yield();
1669 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1670 7aafa0d1 2019-02-22 stsp if (errcode)
1671 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1672 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1674 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1676 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1677 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1678 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1679 5e224a3e 2019-02-22 stsp * with few commits in history.
1681 7aafa0d1 2019-02-22 stsp return NULL;
1685 5e224a3e 2019-02-22 stsp return NULL;
1688 5e224a3e 2019-02-22 stsp static const struct got_error *
1689 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1690 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1691 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1692 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1693 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1695 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1696 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1697 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1699 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1700 5e224a3e 2019-02-22 stsp return NULL;
1702 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1703 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1705 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1706 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1708 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1709 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1710 5e224a3e 2019-02-22 stsp need_commits);
1712 5e224a3e 2019-02-22 stsp return err;
1716 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1717 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1720 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1722 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1723 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1725 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1726 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1728 dd0a52c1 2018-05-20 stsp return err;
1731 cd0acaa7 2018-05-20 stsp static const struct got_error *
1732 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1733 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1734 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1735 8b473291 2019-02-21 stsp struct got_repository *repo)
1737 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1738 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1739 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1741 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1742 15a94983 2018-12-23 stsp if (diff_view == NULL)
1743 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1745 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1746 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1747 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1748 e5a0f69f 2018-08-18 stsp if (err == NULL)
1749 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1750 cd0acaa7 2018-05-20 stsp return err;
1753 80ddbec8 2018-04-29 stsp static const struct got_error *
1754 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1755 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1757 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1759 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1760 941e9f74 2019-05-21 stsp if (parent == NULL)
1761 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1763 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1764 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1765 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1766 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1767 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1768 941e9f74 2019-05-21 stsp s->tree = subtree;
1769 941e9f74 2019-05-21 stsp s->selected = 0;
1770 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1771 941e9f74 2019-05-21 stsp return NULL;
1775 941e9f74 2019-05-21 stsp static const struct got_error *
1776 941e9f74 2019-05-21 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1777 941e9f74 2019-05-21 stsp struct commit_queue_entry *entry, const char *path,
1778 941e9f74 2019-05-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
1780 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1781 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1782 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s;
1783 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1784 b03c880f 2019-05-21 stsp char *slash, *subpath = NULL;
1785 941e9f74 2019-05-21 stsp const char *p;
1787 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1788 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1790 9343a5fb 2018-06-23 stsp return err;
1792 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1793 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1794 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1796 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1797 941e9f74 2019-05-21 stsp if (err) {
1798 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1799 941e9f74 2019-05-21 stsp return err;
1801 941e9f74 2019-05-21 stsp s = &tree_view->state.tree;
1803 941e9f74 2019-05-21 stsp *new_view = tree_view;
1805 33cbf02b 2020-01-12 stsp if (got_path_is_root_dir(path))
1806 33cbf02b 2020-01-12 stsp return NULL;
1808 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1810 941e9f74 2019-05-21 stsp while (*p) {
1811 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1812 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1813 56e0773d 2019-11-28 stsp char *te_name;
1815 33cbf02b 2020-01-12 stsp while (p[0] == '/')
1818 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1819 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1820 941e9f74 2019-05-21 stsp if (slash == NULL)
1821 33cbf02b 2020-01-12 stsp te_name = strdup(p);
1823 33cbf02b 2020-01-12 stsp te_name = strndup(p, slash - p);
1824 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1825 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1828 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1829 56e0773d 2019-11-28 stsp if (te == NULL) {
1830 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1831 56e0773d 2019-11-28 stsp free(te_name);
1834 56e0773d 2019-11-28 stsp free(te_name);
1835 56e0773d 2019-11-28 stsp s->selected_entry = te;
1836 56e0773d 2019-11-28 stsp s->selected = got_tree_entry_get_index(te);
1837 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1838 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1840 56e0773d 2019-11-28 stsp if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1841 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1842 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1843 67409a31 2019-05-24 stsp s->selected = 0;
1847 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1848 941e9f74 2019-05-21 stsp if (slash)
1849 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1851 941e9f74 2019-05-21 stsp subpath = strdup(path);
1852 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1853 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1857 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1862 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1863 941e9f74 2019-05-21 stsp free(tree_id);
1867 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1868 941e9f74 2019-05-21 stsp if (err) {
1869 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1872 941e9f74 2019-05-21 stsp if (slash == NULL)
1874 941e9f74 2019-05-21 stsp free(subpath);
1875 941e9f74 2019-05-21 stsp subpath = NULL;
1876 941e9f74 2019-05-21 stsp p = slash;
1879 941e9f74 2019-05-21 stsp free(subpath);
1880 1a76625f 2018-10-22 stsp return err;
1883 61266923 2020-01-14 stsp static const struct got_error *
1884 61266923 2020-01-14 stsp block_signals_used_by_main_thread(void)
1886 61266923 2020-01-14 stsp sigset_t sigset;
1887 61266923 2020-01-14 stsp int errcode;
1889 61266923 2020-01-14 stsp if (sigemptyset(&sigset) == -1)
1890 61266923 2020-01-14 stsp return got_error_from_errno("sigemptyset");
1892 61266923 2020-01-14 stsp /* tog handles SIGWINCH and SIGCONT */
1893 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGWINCH) == -1)
1894 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1895 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGCONT) == -1)
1896 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1898 61266923 2020-01-14 stsp /* ncurses handles SIGTSTP */
1899 61266923 2020-01-14 stsp if (sigaddset(&sigset, SIGTSTP) == -1)
1900 61266923 2020-01-14 stsp return got_error_from_errno("sigaddset");
1902 61266923 2020-01-14 stsp errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1903 61266923 2020-01-14 stsp if (errcode)
1904 61266923 2020-01-14 stsp return got_error_set_errno(errcode, "pthread_sigmask");
1906 61266923 2020-01-14 stsp return NULL;
1909 1a76625f 2018-10-22 stsp static void *
1910 1a76625f 2018-10-22 stsp log_thread(void *arg)
1912 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1913 1a76625f 2018-10-22 stsp int errcode = 0;
1914 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1915 1a76625f 2018-10-22 stsp int done = 0;
1917 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
1919 61266923 2020-01-14 stsp return (void *)err;
1921 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1922 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1923 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1924 13add988 2019-10-15 stsp a->regex);
1925 1a76625f 2018-10-22 stsp if (err) {
1926 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1927 1a76625f 2018-10-22 stsp return (void *)err;
1928 1a76625f 2018-10-22 stsp err = NULL;
1930 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1931 1a76625f 2018-10-22 stsp a->commits_needed--;
1933 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1934 3abe8080 2019-04-10 stsp if (errcode) {
1935 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1936 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1938 3abe8080 2019-04-10 stsp } else if (*a->quit)
1940 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1941 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1942 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1943 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1947 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1948 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1949 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1950 1a76625f 2018-10-22 stsp &tog_mutex);
1951 1a76625f 2018-10-22 stsp if (errcode)
1952 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1953 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1956 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1957 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1958 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1959 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1961 3abe8080 2019-04-10 stsp a->log_complete = 1;
1962 1a76625f 2018-10-22 stsp return (void *)err;
1965 1a76625f 2018-10-22 stsp static const struct got_error *
1966 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1968 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1969 1a76625f 2018-10-22 stsp int errcode;
1971 1a76625f 2018-10-22 stsp if (s->thread) {
1972 1a76625f 2018-10-22 stsp s->quit = 1;
1973 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1974 1a76625f 2018-10-22 stsp if (errcode)
1975 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1976 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1977 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1978 1a76625f 2018-10-22 stsp if (errcode)
1979 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1980 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1981 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1982 1a76625f 2018-10-22 stsp if (errcode)
1983 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1984 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1985 1a76625f 2018-10-22 stsp if (errcode)
1986 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1987 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1988 1a76625f 2018-10-22 stsp s->thread = NULL;
1991 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1992 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1993 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1995 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1996 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1997 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
2000 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
2001 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
2002 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
2005 9343a5fb 2018-06-23 stsp return err;
2008 9343a5fb 2018-06-23 stsp static const struct got_error *
2009 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
2011 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
2012 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
2014 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
2015 1a76625f 2018-10-22 stsp free_commits(&s->commits);
2016 1a76625f 2018-10-22 stsp free(s->in_repo_path);
2017 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
2018 1a76625f 2018-10-22 stsp free(s->start_id);
2019 797bc7b9 2018-10-22 stsp s->start_id = NULL;
2020 1a76625f 2018-10-22 stsp return err;
2023 1a76625f 2018-10-22 stsp static const struct got_error *
2024 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
2026 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2028 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
2029 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2030 60493ae3 2019-06-20 stsp return NULL;
2033 60493ae3 2019-06-20 stsp static const struct got_error *
2034 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
2036 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
2037 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2038 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2040 60493ae3 2019-06-20 stsp if (!view->searching) {
2041 60493ae3 2019-06-20 stsp view->search_next_done = 1;
2042 60493ae3 2019-06-20 stsp return NULL;
2045 96e2b566 2019-07-08 stsp if (s->search_entry) {
2046 13add988 2019-10-15 stsp int errcode, ch;
2047 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2048 13add988 2019-10-15 stsp if (errcode)
2049 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2050 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2051 13add988 2019-10-15 stsp ch = wgetch(view->window);
2052 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2053 13add988 2019-10-15 stsp if (errcode)
2054 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2055 13add988 2019-10-15 stsp "pthread_mutex_lock");
2056 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2057 678cbce5 2019-07-28 stsp view->search_next_done = 1;
2058 678cbce5 2019-07-28 stsp return NULL;
2060 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2061 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2063 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2064 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2065 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2066 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2067 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2069 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
2070 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2072 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2073 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2075 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2078 60493ae3 2019-06-20 stsp while (1) {
2079 13add988 2019-10-15 stsp int have_match = 0;
2081 60493ae3 2019-06-20 stsp if (entry == NULL) {
2082 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2083 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2084 f801134a 2019-06-25 stsp view->search_next_done = 1;
2085 f801134a 2019-06-25 stsp return NULL;
2088 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2089 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2090 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2092 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2093 57b33b64 2019-07-08 stsp return trigger_log_thread(1,
2094 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
2095 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
2096 f801134a 2019-06-25 stsp &s->thread_args.need_commits);
2099 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2100 13add988 2019-10-15 stsp &view->regex);
2103 13add988 2019-10-15 stsp if (have_match) {
2104 60493ae3 2019-06-20 stsp view->search_next_done = 1;
2105 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2109 96e2b566 2019-07-08 stsp s->search_entry = entry;
2110 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2111 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2113 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2116 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2117 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2118 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2119 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2121 60493ae3 2019-06-20 stsp return err;
2124 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2125 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2127 60493ae3 2019-06-20 stsp return err;
2132 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2134 60493ae3 2019-06-20 stsp return NULL;
2137 60493ae3 2019-06-20 stsp static const struct got_error *
2138 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2139 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
2140 d01904d4 2019-06-25 stsp const char *head_ref_name, const char *path, int check_disk)
2142 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2143 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2144 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2145 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2146 1a76625f 2018-10-22 stsp int errcode;
2148 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2149 ecb28ae0 2018-07-16 stsp if (err != NULL)
2150 ecb28ae0 2018-07-16 stsp goto done;
2152 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2153 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2154 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2156 8b473291 2019-02-21 stsp s->refs = refs;
2157 fb2756b9 2018-08-04 stsp s->repo = repo;
2158 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
2159 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2160 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2161 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2162 5036bf37 2018-09-24 stsp goto done;
2165 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2166 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2167 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2168 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2170 11b20872 2019-11-08 stsp goto done;
2171 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2172 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2173 11b20872 2019-11-08 stsp if (err) {
2174 11b20872 2019-11-08 stsp free_colors(&s->colors);
2175 11b20872 2019-11-08 stsp goto done;
2177 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2178 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2179 11b20872 2019-11-08 stsp if (err) {
2180 11b20872 2019-11-08 stsp free_colors(&s->colors);
2181 11b20872 2019-11-08 stsp goto done;
2185 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2186 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2187 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2188 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2189 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2191 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2193 1a76625f 2018-10-22 stsp goto done;
2194 3d509237 2020-01-04 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path, 0);
2196 1a76625f 2018-10-22 stsp goto done;
2197 c5b78334 2020-01-12 stsp err = got_commit_graph_iter_start(thread_graph,
2198 c5b78334 2020-01-12 stsp s->start_id, s->repo, NULL, NULL);
2200 c5b78334 2020-01-12 stsp goto done;
2202 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2203 1a76625f 2018-10-22 stsp if (errcode) {
2204 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2205 1a76625f 2018-10-22 stsp goto done;
2208 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2209 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2210 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2211 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2212 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2213 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2214 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2215 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2216 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2217 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2218 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2219 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2220 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2223 1a76625f 2018-10-22 stsp close_log_view(view);
2224 ba4f502b 2018-08-04 stsp return err;
2227 e5a0f69f 2018-08-18 stsp static const struct got_error *
2228 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2230 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2232 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2233 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2234 2b380cc8 2018-10-24 stsp &s->thread_args);
2235 2b380cc8 2018-10-24 stsp if (errcode)
2236 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2239 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
2240 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
2241 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
2242 11b20872 2019-11-08 stsp s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2245 e5a0f69f 2018-08-18 stsp static const struct got_error *
2246 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2247 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2249 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2250 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2251 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2252 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2253 669b5ffa 2018-10-07 stsp int begin_x = 0;
2254 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2256 e5a0f69f 2018-08-18 stsp switch (ch) {
2258 1e37a5c2 2019-05-12 jcs s->quit = 1;
2261 1e37a5c2 2019-05-12 jcs case KEY_UP:
2264 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2266 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2267 1e37a5c2 2019-05-12 jcs s->selected--;
2269 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
2270 1144d21a 2019-06-21 stsp &s->commits);
2272 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2273 a4292ac5 2019-05-12 jcs case CTRL('b'):
2274 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2276 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
2277 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
2278 1e37a5c2 2019-05-12 jcs s->selected = 0;
2281 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
2282 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
2285 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2288 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2290 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2291 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2292 1e37a5c2 2019-05-12 jcs s->selected++;
2295 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
2296 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
2297 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
2298 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2299 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2301 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2302 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2303 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2304 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2305 1e37a5c2 2019-05-12 jcs if (first == NULL)
2307 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
2308 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
2309 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
2310 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2311 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2314 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2315 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2316 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2317 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2318 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2319 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2321 1e37a5c2 2019-05-12 jcs err = NULL;
2324 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2325 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2326 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2327 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2328 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2330 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2333 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2335 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2336 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2337 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2338 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2339 1e37a5c2 2019-05-12 jcs view, 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, diff_view);
2348 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2351 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2352 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2354 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2357 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2359 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2360 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2361 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2362 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2365 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2366 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2368 1e37a5c2 2019-05-12 jcs return err;
2369 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2371 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2374 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2375 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2377 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2379 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2380 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2382 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2383 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2384 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2386 1e37a5c2 2019-05-12 jcs return err;
2387 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2388 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2389 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2390 638f9024 2019-05-13 stsp return got_error_from_errno(
2391 1e37a5c2 2019-05-12 jcs "view_open");
2392 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2393 d01904d4 2019-06-25 stsp s->repo, s->head_ref_name, parent_path, 0);
2395 1e37a5c2 2019-05-12 jcs return err;;
2396 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2397 1e37a5c2 2019-05-12 jcs *new_view = lv;
2399 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2400 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2402 1e37a5c2 2019-05-12 jcs return NULL;
2405 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2406 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2408 d01904d4 2019-06-25 stsp return err;
2409 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2410 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2411 d01904d4 2019-06-25 stsp if (lv == NULL)
2412 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2413 d01904d4 2019-06-25 stsp err = get_head_commit_id(&start_id, s->head_ref_name ?
2414 d01904d4 2019-06-25 stsp s->head_ref_name : GOT_REF_HEAD, s->repo);
2415 ef129c5e 2019-08-03 stsp if (err) {
2416 ef129c5e 2019-08-03 stsp view_close(lv);
2417 d01904d4 2019-06-25 stsp return err;
2419 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2420 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2421 e6b23735 2019-10-04 stsp free(start_id);
2422 e6b23735 2019-10-04 stsp view_close(lv);
2423 e6b23735 2019-10-04 stsp return got_error_from_errno("strdup");
2425 e6b23735 2019-10-04 stsp got_ref_list_free(s->refs);
2426 e6b23735 2019-10-04 stsp err = got_ref_list(s->refs, s->repo, NULL,
2427 e6b23735 2019-10-04 stsp got_ref_cmp_by_name, NULL);
2428 e6b23735 2019-10-04 stsp if (err) {
2429 d01904d4 2019-06-25 stsp free(start_id);
2430 ef129c5e 2019-08-03 stsp view_close(lv);
2431 50284065 2019-10-04 stsp return err;
2433 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2434 d01904d4 2019-06-25 stsp s->head_ref_name, in_repo_path, 0);
2435 ef129c5e 2019-08-03 stsp if (err) {
2436 ef129c5e 2019-08-03 stsp free(start_id);
2437 ef129c5e 2019-08-03 stsp view_close(lv);
2438 d01904d4 2019-06-25 stsp return err;;
2440 d01904d4 2019-06-25 stsp *dead_view = view;
2441 d01904d4 2019-06-25 stsp *new_view = lv;
2447 80ddbec8 2018-04-29 stsp return err;
2450 4ed7e80c 2018-05-20 stsp static const struct got_error *
2451 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2453 c2db6724 2019-01-04 stsp const struct got_error *error;
2455 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2456 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2457 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2459 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2460 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2462 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2463 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2465 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2466 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2468 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2469 c2db6724 2019-01-04 stsp if (error != NULL)
2470 c2db6724 2019-01-04 stsp return error;
2472 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2473 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2475 c2db6724 2019-01-04 stsp return NULL;
2478 a915003a 2019-02-05 stsp static void
2479 a915003a 2019-02-05 stsp init_curses(void)
2481 a915003a 2019-02-05 stsp initscr();
2483 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2486 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2487 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2488 a915003a 2019-02-05 stsp curs_set(0);
2489 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2490 6d17833f 2019-11-08 stsp start_color();
2491 6d17833f 2019-11-08 stsp use_default_colors();
2493 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2494 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2495 61266923 2020-01-14 stsp signal(SIGCONT, tog_sigcont);
2498 c2db6724 2019-01-04 stsp static const struct got_error *
2499 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2501 80ddbec8 2018-04-29 stsp const struct got_error *error;
2502 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2503 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2504 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2505 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2506 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2507 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2509 04cc582a 2018-08-01 stsp struct tog_view *view;
2511 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2513 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2514 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2515 c2db6724 2019-01-04 stsp NULL) == -1)
2516 80ddbec8 2018-04-29 stsp err(1, "pledge");
2519 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2520 80ddbec8 2018-04-29 stsp switch (ch) {
2522 80ddbec8 2018-04-29 stsp start_commit = optarg;
2525 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2526 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2527 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2531 17020d27 2019-03-07 stsp usage_log();
2532 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2536 80ddbec8 2018-04-29 stsp argc -= optind;
2537 80ddbec8 2018-04-29 stsp argv += optind;
2539 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2540 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2541 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2542 ecb28ae0 2018-07-16 stsp goto done;
2544 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2545 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2546 963f97a1 2019-03-18 stsp goto done;
2547 963f97a1 2019-03-18 stsp error = NULL;
2549 963f97a1 2019-03-18 stsp if (argc == 0) {
2550 963f97a1 2019-03-18 stsp path = strdup("");
2551 963f97a1 2019-03-18 stsp if (path == NULL) {
2552 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2553 ecb28ae0 2018-07-16 stsp goto done;
2555 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2556 963f97a1 2019-03-18 stsp if (worktree) {
2557 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2559 963f97a1 2019-03-18 stsp if (error)
2560 963f97a1 2019-03-18 stsp goto done;
2562 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2563 963f97a1 2019-03-18 stsp if (path == NULL) {
2564 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2565 963f97a1 2019-03-18 stsp goto done;
2569 963f97a1 2019-03-18 stsp usage_log();
2571 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2572 a1fbf39a 2019-08-11 stsp if (worktree)
2573 b9d7675a 2019-08-11 stsp repo_path = strdup(
2574 b9d7675a 2019-08-11 stsp got_worktree_get_repo_path(worktree));
2576 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2578 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2579 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2580 963f97a1 2019-03-18 stsp goto done;
2583 a915003a 2019-02-05 stsp init_curses();
2585 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2586 80ddbec8 2018-04-29 stsp if (error != NULL)
2587 ecb28ae0 2018-07-16 stsp goto done;
2589 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2590 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2591 c02c541e 2019-03-29 stsp if (error)
2592 c02c541e 2019-03-29 stsp goto done;
2594 15a94983 2018-12-23 stsp if (start_commit == NULL)
2595 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&start_id, worktree ?
2596 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2599 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&start_id, start_commit, repo);
2600 f2b6a97d 2019-07-15 stsp if (error) {
2601 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
2602 f2b6a97d 2019-07-15 stsp goto done;
2603 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&start_id,
2604 f2b6a97d 2019-07-15 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2607 80ddbec8 2018-04-29 stsp if (error != NULL)
2608 8b473291 2019-02-21 stsp goto done;
2610 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2611 8b473291 2019-02-21 stsp if (error)
2612 ecb28ae0 2018-07-16 stsp goto done;
2614 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2615 04cc582a 2018-08-01 stsp if (view == NULL) {
2616 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2617 04cc582a 2018-08-01 stsp goto done;
2619 2fc00ff4 2019-08-31 stsp if (worktree) {
2620 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2621 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2622 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2623 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2624 2fc00ff4 2019-08-31 stsp goto done;
2627 2fc00ff4 2019-08-31 stsp error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2629 ba4f502b 2018-08-04 stsp if (error)
2630 ba4f502b 2018-08-04 stsp goto done;
2631 2fc00ff4 2019-08-31 stsp if (worktree) {
2632 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2633 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2634 2fc00ff4 2019-08-31 stsp worktree = NULL;
2636 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2638 ecb28ae0 2018-07-16 stsp free(repo_path);
2639 ecb28ae0 2018-07-16 stsp free(cwd);
2640 ecb28ae0 2018-07-16 stsp free(path);
2641 899d86c2 2018-05-10 stsp free(start_id);
2642 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2644 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2645 ec142235 2019-03-07 stsp if (worktree)
2646 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2647 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2648 80ddbec8 2018-04-29 stsp return error;
2651 4ed7e80c 2018-05-20 stsp __dead static void
2652 9f7d7167 2018-04-29 stsp usage_diff(void)
2655 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2656 9f7d7167 2018-04-29 stsp getprogname());
2660 b304db33 2018-05-20 stsp static char *
2661 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2663 b304db33 2018-05-20 stsp char *line;
2664 b304db33 2018-05-20 stsp size_t linelen;
2665 b304db33 2018-05-20 stsp size_t lineno;
2666 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2668 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2670 b304db33 2018-05-20 stsp *len = linelen;
2671 b304db33 2018-05-20 stsp return line;
2674 6d17833f 2019-11-08 stsp static int
2675 6d17833f 2019-11-08 stsp match_line(const char *line, regex_t *regex)
2677 6d17833f 2019-11-08 stsp regmatch_t regmatch;
2679 6d17833f 2019-11-08 stsp return regexec(regex, line, 1, ®match, 0) == 0;
2682 f26dddb7 2019-11-08 stsp struct tog_color *
2683 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2685 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2687 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2688 6d17833f 2019-11-08 stsp if (match_line(line, &tc->regex))
2689 6d17833f 2019-11-08 stsp return tc;
2692 6d17833f 2019-11-08 stsp return NULL;
2695 4ed7e80c 2018-05-20 stsp static const struct got_error *
2696 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2697 6d17833f 2019-11-08 stsp int *last_displayed_line, int *eof, int max_lines, char *header,
2698 f26dddb7 2019-11-08 stsp struct tog_colors *colors)
2700 61e69b96 2018-05-20 stsp const struct got_error *err;
2701 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2702 b304db33 2018-05-20 stsp char *line;
2703 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2704 b304db33 2018-05-20 stsp size_t len;
2705 61e69b96 2018-05-20 stsp wchar_t *wline;
2706 e0b650dd 2018-05-20 stsp int width;
2708 26ed57b2 2018-05-19 stsp rewind(f);
2709 f7d12f7e 2018-08-01 stsp werase(view->window);
2711 a3404814 2018-09-02 stsp if (header) {
2712 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
2713 a3404814 2018-09-02 stsp if (err) {
2714 a3404814 2018-09-02 stsp return err;
2717 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2718 a3404814 2018-09-02 stsp wstandout(view->window);
2719 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2720 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2721 a3404814 2018-09-02 stsp wstandend(view->window);
2722 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2723 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2725 a3404814 2018-09-02 stsp if (max_lines <= 1)
2726 a3404814 2018-09-02 stsp return NULL;
2727 a3404814 2018-09-02 stsp max_lines--;
2731 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2732 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2733 26ed57b2 2018-05-19 stsp if (line == NULL) {
2737 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2738 26ed57b2 2018-05-19 stsp free(line);
2742 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2743 61e69b96 2018-05-20 stsp if (err) {
2744 61e69b96 2018-05-20 stsp free(line);
2745 61e69b96 2018-05-20 stsp return err;
2748 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
2750 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2751 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2752 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2754 6d17833f 2019-11-08 stsp wattr_off(view->window,
2755 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2756 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2757 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2758 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2759 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2760 26ed57b2 2018-05-19 stsp free(line);
2761 2550e4c3 2018-07-13 stsp free(wline);
2762 2550e4c3 2018-07-13 stsp wline = NULL;
2764 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2766 1a57306a 2018-09-02 stsp view_vborder(view);
2768 c3e9aa98 2019-05-13 jcs if (*eof) {
2769 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2770 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2771 c3e9aa98 2019-05-13 jcs nprinted++;
2774 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2776 c3e9aa98 2019-05-13 jcs return err;
2779 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2780 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2781 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2784 26ed57b2 2018-05-19 stsp return NULL;
2787 abd2672a 2018-12-23 stsp static char *
2788 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2790 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2791 09867e48 2019-08-13 stsp char *p, *s;
2793 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2794 09867e48 2019-08-13 stsp if (tm == NULL)
2795 09867e48 2019-08-13 stsp return NULL;
2796 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2797 09867e48 2019-08-13 stsp if (s == NULL)
2798 09867e48 2019-08-13 stsp return NULL;
2799 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2801 abd2672a 2018-12-23 stsp *p = '\0';
2805 4ed7e80c 2018-05-20 stsp static const struct got_error *
2806 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2807 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2809 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2810 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
2811 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2812 5943eee2 2019-08-13 stsp char *id_str = NULL, *logmsg = NULL;
2813 45d799e2 2018-12-23 stsp time_t committer_time;
2814 45d799e2 2018-12-23 stsp const char *author, *committer;
2815 8b473291 2019-02-21 stsp char *refs_str = NULL;
2817 8b473291 2019-02-21 stsp if (refs) {
2818 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
2820 8b473291 2019-02-21 stsp return err;
2823 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2825 abd2672a 2018-12-23 stsp return err;
2827 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2828 15a94983 2018-12-23 stsp if (err) {
2829 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2830 15a94983 2018-12-23 stsp goto done;
2833 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2834 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2835 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2836 abd2672a 2018-12-23 stsp goto done;
2838 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2839 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2840 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2841 abd2672a 2018-12-23 stsp goto done;
2843 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2844 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
2845 09867e48 2019-08-13 stsp if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2846 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2847 abd2672a 2018-12-23 stsp goto done;
2849 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2850 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2851 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2852 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2853 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2854 abd2672a 2018-12-23 stsp goto done;
2856 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2858 5943eee2 2019-08-13 stsp goto done;
2859 5943eee2 2019-08-13 stsp if (fprintf(outfile, "%s\n", logmsg) < 0) {
2860 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2861 abd2672a 2018-12-23 stsp goto done;
2864 abd2672a 2018-12-23 stsp free(id_str);
2865 5943eee2 2019-08-13 stsp free(logmsg);
2866 8b473291 2019-02-21 stsp free(refs_str);
2867 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2868 abd2672a 2018-12-23 stsp return err;
2871 abd2672a 2018-12-23 stsp static const struct got_error *
2872 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2874 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2875 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2876 15a94983 2018-12-23 stsp int obj_type;
2878 511a516b 2018-05-19 stsp f = got_opentemp();
2879 48ae06ee 2018-10-18 stsp if (f == NULL) {
2880 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2881 48ae06ee 2018-10-18 stsp goto done;
2883 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2884 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2885 fb43ecf1 2019-02-11 stsp goto done;
2889 15a94983 2018-12-23 stsp if (s->id1)
2890 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2892 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2894 15a94983 2018-12-23 stsp goto done;
2896 15a94983 2018-12-23 stsp switch (obj_type) {
2897 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2898 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2899 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2901 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2902 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2903 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2905 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2906 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2907 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2908 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2910 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2913 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2914 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2915 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2917 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2918 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2919 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2920 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2921 8b473291 2019-02-21 stsp s->repo, f);
2926 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2928 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2929 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2933 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2937 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2938 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2939 48ae06ee 2018-10-18 stsp return err;
2942 f5215bb9 2019-02-22 stsp static void
2943 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2945 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2946 f5215bb9 2019-02-22 stsp update_panels();
2947 f5215bb9 2019-02-22 stsp doupdate();
2950 48ae06ee 2018-10-18 stsp static const struct got_error *
2951 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2952 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2953 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2955 48ae06ee 2018-10-18 stsp const struct got_error *err;
2957 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2958 15a94983 2018-12-23 stsp int type1, type2;
2959 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2961 15a94983 2018-12-23 stsp return err;
2962 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2964 15a94983 2018-12-23 stsp return err;
2966 15a94983 2018-12-23 stsp if (type1 != type2)
2967 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2970 15a94983 2018-12-23 stsp if (id1) {
2971 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2972 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2973 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2975 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2977 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2978 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2979 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2980 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2981 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2983 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2984 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2985 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2986 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2987 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2988 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2989 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2990 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&view->state.diff.colors);
2992 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2993 bddb1296 2019-11-08 stsp err = add_color(&view->state.diff.colors,
2994 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
2995 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
2997 6d17833f 2019-11-08 stsp return err;
2998 11b20872 2019-11-08 stsp err = add_color(&view->state.diff.colors, "^\\+",
2999 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
3000 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
3001 6d17833f 2019-11-08 stsp if (err) {
3002 bddb1296 2019-11-08 stsp free_colors(&view->state.diff.colors);
3003 6d17833f 2019-11-08 stsp return err;
3005 bddb1296 2019-11-08 stsp err = add_color(&view->state.diff.colors,
3006 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3007 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3008 6d17833f 2019-11-08 stsp if (err) {
3009 bddb1296 2019-11-08 stsp free_colors(&view->state.diff.colors);
3010 6d17833f 2019-11-08 stsp return err;
3013 bddb1296 2019-11-08 stsp err = add_color(&view->state.diff.colors,
3014 11b20872 2019-11-08 stsp "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3015 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
3016 6d17833f 2019-11-08 stsp if (err) {
3017 bddb1296 2019-11-08 stsp free_colors(&view->state.diff.colors);
3018 6d17833f 2019-11-08 stsp return err;
3021 11b20872 2019-11-08 stsp err = add_color(&view->state.diff.colors,
3022 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
3023 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
3024 11b20872 2019-11-08 stsp if (err) {
3025 11b20872 2019-11-08 stsp free_colors(&view->state.diff.colors);
3026 11b20872 2019-11-08 stsp return err;
3029 11b20872 2019-11-08 stsp err = add_color(&view->state.diff.colors,
3030 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
3031 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
3032 11b20872 2019-11-08 stsp if (err) {
3033 11b20872 2019-11-08 stsp free_colors(&view->state.diff.colors);
3034 11b20872 2019-11-08 stsp return err;
3038 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
3039 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
3040 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
3042 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
3043 48ae06ee 2018-10-18 stsp if (err) {
3044 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
3045 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
3046 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
3047 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
3048 48ae06ee 2018-10-18 stsp return err;
3051 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3052 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3053 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3055 5dc9f4bc 2018-08-04 stsp return NULL;
3058 e5a0f69f 2018-08-18 stsp static const struct got_error *
3059 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3061 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3063 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
3064 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
3065 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
3066 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
3067 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
3068 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3069 bddb1296 2019-11-08 stsp free_colors(&view->state.diff.colors);
3070 e5a0f69f 2018-08-18 stsp return err;
3073 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3074 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3076 a3404814 2018-09-02 stsp const struct got_error *err;
3077 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3078 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3080 a3404814 2018-09-02 stsp if (s->id1) {
3081 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3083 a3404814 2018-09-02 stsp return err;
3085 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3087 a3404814 2018-09-02 stsp return err;
3089 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
3090 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3091 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3092 a3404814 2018-09-02 stsp free(id_str1);
3093 a3404814 2018-09-02 stsp free(id_str2);
3094 a3404814 2018-09-02 stsp return err;
3096 a3404814 2018-09-02 stsp free(id_str1);
3097 a3404814 2018-09-02 stsp free(id_str2);
3099 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
3100 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
3101 bddb1296 2019-11-08 stsp header, &s->colors);
3104 15a087fe 2019-02-21 stsp static const struct got_error *
3105 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3106 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3108 d7a04538 2019-02-21 stsp const struct got_error *err;
3109 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3110 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3111 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3113 15a087fe 2019-02-21 stsp free(s->id2);
3114 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3115 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3116 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3118 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3120 d7a04538 2019-02-21 stsp return err;
3121 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3122 15a087fe 2019-02-21 stsp free(s->id1);
3123 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3124 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3125 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3126 15a087fe 2019-02-21 stsp return NULL;
3129 0cf4efb1 2018-09-29 stsp static const struct got_error *
3130 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3131 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3133 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3134 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3135 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3136 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
3139 e5a0f69f 2018-08-18 stsp switch (ch) {
3141 1e37a5c2 2019-05-12 jcs case KEY_UP:
3142 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3143 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3145 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3146 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3147 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3150 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3151 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3152 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3155 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3156 1e37a5c2 2019-05-12 jcs if (!s->eof)
3157 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3159 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3160 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3162 00ba99a7 2019-05-12 jcs if (s->eof)
3165 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3166 1e37a5c2 2019-05-12 jcs char *line;
3167 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
3168 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3169 1e37a5c2 2019-05-12 jcs if (line == NULL)
3174 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3175 1e37a5c2 2019-05-12 jcs s->diff_context--;
3176 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3177 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3181 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3182 1e37a5c2 2019-05-12 jcs s->diff_context++;
3183 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3184 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3189 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3191 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3192 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
3193 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
3194 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3197 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3202 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3206 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3207 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3209 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3210 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3214 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3216 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3218 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3219 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
3221 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
3222 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
3223 1e37a5c2 2019-05-12 jcs update_panels();
3224 1e37a5c2 2019-05-12 jcs doupdate();
3226 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
3227 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
3228 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
3229 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
3233 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3238 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
3239 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3242 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3246 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3247 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3249 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3250 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3256 bcbd79e2 2018-08-19 stsp return err;
3259 4ed7e80c 2018-05-20 stsp static const struct got_error *
3260 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3262 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3263 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3264 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3265 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3266 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
3267 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3269 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3271 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3273 26ed57b2 2018-05-19 stsp #ifndef PROFILE
3274 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3275 eb6600df 2019-01-04 stsp NULL) == -1)
3276 26ed57b2 2018-05-19 stsp err(1, "pledge");
3279 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3280 26ed57b2 2018-05-19 stsp switch (ch) {
3282 17020d27 2019-03-07 stsp usage_diff();
3283 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3287 26ed57b2 2018-05-19 stsp argc -= optind;
3288 26ed57b2 2018-05-19 stsp argv += optind;
3290 26ed57b2 2018-05-19 stsp if (argc == 0) {
3291 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3292 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3293 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
3294 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
3295 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
3296 15a94983 2018-12-23 stsp id_str1 = argv[0];
3297 15a94983 2018-12-23 stsp id_str2 = argv[1];
3298 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
3299 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
3300 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
3301 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
3302 15a94983 2018-12-23 stsp id_str1 = argv[1];
3303 15a94983 2018-12-23 stsp id_str2 = argv[2];
3305 26ed57b2 2018-05-19 stsp usage_diff();
3307 a915003a 2019-02-05 stsp init_curses();
3309 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3310 eb6600df 2019-01-04 stsp if (error)
3311 eb6600df 2019-01-04 stsp goto done;
3313 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3314 26ed57b2 2018-05-19 stsp if (error)
3315 26ed57b2 2018-05-19 stsp goto done;
3317 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
3318 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3319 26ed57b2 2018-05-19 stsp if (error)
3320 26ed57b2 2018-05-19 stsp goto done;
3322 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
3323 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3324 26ed57b2 2018-05-19 stsp if (error)
3325 26ed57b2 2018-05-19 stsp goto done;
3327 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3328 8b473291 2019-02-21 stsp if (error)
3329 8b473291 2019-02-21 stsp goto done;
3331 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3332 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3333 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3334 ea5e7bb5 2018-08-01 stsp goto done;
3336 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3337 5dc9f4bc 2018-08-04 stsp if (error)
3338 5dc9f4bc 2018-08-04 stsp goto done;
3339 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3341 c02c541e 2019-03-29 stsp free(repo_path);
3343 921be706 2019-06-28 stsp got_repo_close(repo);
3344 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3345 26ed57b2 2018-05-19 stsp return error;
3348 4ed7e80c 2018-05-20 stsp __dead static void
3349 9f7d7167 2018-04-29 stsp usage_blame(void)
3352 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3353 9f7d7167 2018-04-29 stsp getprogname());
3357 84451b3e 2018-07-10 stsp struct tog_blame_line {
3358 84451b3e 2018-07-10 stsp int annotated;
3359 84451b3e 2018-07-10 stsp struct got_object_id *id;
3362 4ed7e80c 2018-05-20 stsp static const struct got_error *
3363 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3364 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
3365 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
3366 11b20872 2019-11-08 stsp int *last_displayed_line, int *eof, int max_lines,
3367 11b20872 2019-11-08 stsp struct tog_colors *colors)
3369 84451b3e 2018-07-10 stsp const struct got_error *err;
3370 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3371 84451b3e 2018-07-10 stsp char *line;
3372 84451b3e 2018-07-10 stsp size_t len;
3373 84451b3e 2018-07-10 stsp wchar_t *wline;
3374 27a741e5 2019-09-11 stsp int width;
3375 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3376 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3377 ab089a2a 2018-07-12 stsp char *id_str;
3378 11b20872 2019-11-08 stsp struct tog_color *tc;
3380 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
3382 ab089a2a 2018-07-12 stsp return err;
3384 84451b3e 2018-07-10 stsp rewind(f);
3385 f7d12f7e 2018-08-01 stsp werase(view->window);
3387 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3388 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3389 ab089a2a 2018-07-12 stsp free(id_str);
3390 ab089a2a 2018-07-12 stsp return err;
3393 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3394 ab089a2a 2018-07-12 stsp free(line);
3395 2550e4c3 2018-07-13 stsp line = NULL;
3397 1cae65b4 2019-09-22 stsp return err;
3398 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3399 a3404814 2018-09-02 stsp wstandout(view->window);
3400 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3402 11b20872 2019-11-08 stsp wattr_on(view->window,
3403 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3404 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3406 11b20872 2019-11-08 stsp wattr_off(view->window,
3407 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3408 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3409 a3404814 2018-09-02 stsp wstandend(view->window);
3410 2550e4c3 2018-07-13 stsp free(wline);
3411 2550e4c3 2018-07-13 stsp wline = NULL;
3412 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3413 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3415 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3416 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
3417 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
3418 ab089a2a 2018-07-12 stsp free(id_str);
3419 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3421 ab089a2a 2018-07-12 stsp free(id_str);
3422 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3423 3f60a8ef 2018-07-10 stsp free(line);
3424 2550e4c3 2018-07-13 stsp line = NULL;
3426 3f60a8ef 2018-07-10 stsp return err;
3427 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3428 2550e4c3 2018-07-13 stsp free(wline);
3429 2550e4c3 2018-07-13 stsp wline = NULL;
3430 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3431 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3434 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
3435 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
3436 84451b3e 2018-07-10 stsp if (line == NULL) {
3440 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
3441 84451b3e 2018-07-10 stsp free(line);
3445 27a741e5 2019-09-11 stsp if (view->ncols <= 9) {
3446 27a741e5 2019-09-11 stsp width = 9;
3447 27a741e5 2019-09-11 stsp wline = wcsdup(L"");
3448 27a741e5 2019-09-11 stsp if (wline == NULL)
3449 27a741e5 2019-09-11 stsp err = got_error_from_errno("wcsdup");
3451 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line,
3452 27a741e5 2019-09-11 stsp view->ncols - 9, 9);
3453 27a741e5 2019-09-11 stsp width += 9;
3455 84451b3e 2018-07-10 stsp if (err) {
3456 84451b3e 2018-07-10 stsp free(line);
3457 84451b3e 2018-07-10 stsp return err;
3460 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3461 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3463 8d0fe45a 2019-08-12 stsp if (nlines > 0) {
3464 8d0fe45a 2019-08-12 stsp blame_line = &lines[lineno - 1];
3465 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3466 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3467 27a741e5 2019-09-11 stsp !(view->focussed &&
3468 27a741e5 2019-09-11 stsp nprinted == selected_line - 1)) {
3469 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3470 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3471 8d0fe45a 2019-08-12 stsp char *id_str;
3472 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3473 8d0fe45a 2019-08-12 stsp if (err) {
3474 8d0fe45a 2019-08-12 stsp free(line);
3475 8d0fe45a 2019-08-12 stsp free(wline);
3476 8d0fe45a 2019-08-12 stsp return err;
3478 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3480 11b20872 2019-11-08 stsp wattr_on(view->window,
3481 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3482 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3484 11b20872 2019-11-08 stsp wattr_off(view->window,
3485 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3486 8d0fe45a 2019-08-12 stsp free(id_str);
3487 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3489 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3490 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3493 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3494 ee41ec32 2018-07-10 stsp prev_id = NULL;
3497 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3498 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3499 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3501 27a741e5 2019-09-11 stsp waddwstr(view->window, wline);
3502 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
3503 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
3504 84451b3e 2018-07-10 stsp if (++nprinted == 1)
3505 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
3506 84451b3e 2018-07-10 stsp free(line);
3507 2550e4c3 2018-07-13 stsp free(wline);
3508 2550e4c3 2018-07-13 stsp wline = NULL;
3510 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
3512 1a57306a 2018-09-02 stsp view_vborder(view);
3514 84451b3e 2018-07-10 stsp return NULL;
3517 84451b3e 2018-07-10 stsp static const struct got_error *
3518 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3520 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
3521 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
3522 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
3523 1a76625f 2018-10-22 stsp int errcode;
3525 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
3526 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3527 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3529 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3530 1a76625f 2018-10-22 stsp if (errcode)
3531 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3533 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3534 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3535 d68a0a7d 2018-07-10 stsp goto done;
3538 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3539 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3541 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3542 d68a0a7d 2018-07-10 stsp if (line->annotated)
3543 d68a0a7d 2018-07-10 stsp goto done;
3545 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3546 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3547 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3548 84451b3e 2018-07-10 stsp goto done;
3550 84451b3e 2018-07-10 stsp line->annotated = 1;
3552 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3553 1a76625f 2018-10-22 stsp if (errcode)
3554 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3555 84451b3e 2018-07-10 stsp return err;
3558 84451b3e 2018-07-10 stsp static void *
3559 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3561 18430de3 2018-07-10 stsp const struct got_error *err;
3562 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3563 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3564 1a76625f 2018-10-22 stsp int errcode;
3566 61266923 2020-01-14 stsp err = block_signals_used_by_main_thread();
3568 61266923 2020-01-14 stsp return (void *)err;
3570 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
3571 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3572 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
3573 fc06ba56 2019-08-22 stsp err = NULL;
3575 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3576 1a76625f 2018-10-22 stsp if (errcode)
3577 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3578 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3580 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3581 c9beca56 2018-07-22 stsp ta->repo = NULL;
3582 c9beca56 2018-07-22 stsp *ta->complete = 1;
3584 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3585 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3586 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3588 18430de3 2018-07-10 stsp return (void *)err;
3591 245d91c1 2018-07-12 stsp static struct got_object_id *
3592 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3593 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
3595 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3597 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
3598 8d0fe45a 2019-08-12 stsp return NULL;
3600 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3601 245d91c1 2018-07-12 stsp if (!line->annotated)
3602 245d91c1 2018-07-12 stsp return NULL;
3604 245d91c1 2018-07-12 stsp return line->id;
3607 b880a918 2018-07-10 stsp static const struct got_error *
3608 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3610 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3613 245d91c1 2018-07-12 stsp if (blame->thread) {
3614 1a76625f 2018-10-22 stsp int errcode;
3615 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3616 1a76625f 2018-10-22 stsp if (errcode)
3617 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3618 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3619 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3620 1a76625f 2018-10-22 stsp if (errcode)
3621 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3622 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3623 1a76625f 2018-10-22 stsp if (errcode)
3624 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3625 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3626 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3627 245d91c1 2018-07-12 stsp err = NULL;
3628 245d91c1 2018-07-12 stsp blame->thread = NULL;
3630 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3631 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3632 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3634 245d91c1 2018-07-12 stsp if (blame->f) {
3635 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3636 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3637 245d91c1 2018-07-12 stsp blame->f = NULL;
3639 57670559 2018-12-23 stsp if (blame->lines) {
3640 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3641 57670559 2018-12-23 stsp free(blame->lines[i].id);
3642 57670559 2018-12-23 stsp free(blame->lines);
3643 57670559 2018-12-23 stsp blame->lines = NULL;
3645 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3646 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3648 245d91c1 2018-07-12 stsp return err;
3651 245d91c1 2018-07-12 stsp static const struct got_error *
3652 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
3654 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
3655 fc06ba56 2019-08-22 stsp int *done = arg;
3656 fc06ba56 2019-08-22 stsp int errcode;
3658 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3659 fc06ba56 2019-08-22 stsp if (errcode)
3660 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3661 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
3663 fc06ba56 2019-08-22 stsp if (*done)
3664 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
3666 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3667 fc06ba56 2019-08-22 stsp if (errcode)
3668 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3669 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
3671 fc06ba56 2019-08-22 stsp return err;
3674 fc06ba56 2019-08-22 stsp static const struct got_error *
3675 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3676 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3677 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3678 245d91c1 2018-07-12 stsp struct got_repository *repo)
3680 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3681 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3682 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3683 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3684 15a94983 2018-12-23 stsp int obj_type;
3686 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3688 15a94983 2018-12-23 stsp return err;
3689 15a94983 2018-12-23 stsp if (obj_id == NULL)
3690 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
3692 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3694 84451b3e 2018-07-10 stsp goto done;
3696 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3697 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3698 84451b3e 2018-07-10 stsp goto done;
3701 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3703 a70480e0 2018-06-23 stsp goto done;
3704 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3705 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3706 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3707 84451b3e 2018-07-10 stsp goto done;
3709 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3710 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
3711 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
3712 84451b3e 2018-07-10 stsp goto done;
3714 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3715 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3716 b02560ec 2019-08-19 stsp blame->nlines--;
3718 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3719 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3720 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3721 84451b3e 2018-07-10 stsp goto done;
3724 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3726 bd24772e 2018-07-11 stsp goto done;
3728 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3729 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3730 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3731 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3732 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3733 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3734 245d91c1 2018-07-12 stsp goto done;
3736 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3738 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3739 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3740 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3741 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3742 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
3743 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_arg = done;
3744 245d91c1 2018-07-12 stsp *blame_complete = 0;
3748 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3749 27d434c2 2018-09-15 stsp free(obj_id);
3751 245d91c1 2018-07-12 stsp stop_blame(blame);
3752 245d91c1 2018-07-12 stsp return err;
3755 245d91c1 2018-07-12 stsp static const struct got_error *
3756 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3757 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3758 8b473291 2019-02-21 stsp struct got_repository *repo)
3760 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3761 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3763 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3765 c4843652 2019-08-12 stsp s->path = strdup(path);
3766 c4843652 2019-08-12 stsp if (s->path == NULL)
3767 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
3769 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3770 c4843652 2019-08-12 stsp if (err) {
3771 c4843652 2019-08-12 stsp free(s->path);
3772 7cbe629d 2018-08-04 stsp return err;
3775 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3776 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3777 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3778 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3779 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3780 fb2756b9 2018-08-04 stsp s->repo = repo;
3781 8b473291 2019-02-21 stsp s->refs = refs;
3782 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3783 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3785 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
3786 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3787 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3788 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3790 11b20872 2019-11-08 stsp return err;
3793 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3794 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3795 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3796 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
3797 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
3799 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3800 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3801 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3802 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3805 e5a0f69f 2018-08-18 stsp static const struct got_error *
3806 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3808 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3809 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3811 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3812 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3814 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3815 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3816 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3817 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3818 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3821 e5a0f69f 2018-08-18 stsp free(s->path);
3822 11b20872 2019-11-08 stsp free_colors(&s->colors);
3824 e5a0f69f 2018-08-18 stsp return err;
3827 7cbe629d 2018-08-04 stsp static const struct got_error *
3828 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
3830 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3832 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
3833 6c4c42e0 2019-06-24 stsp return NULL;
3836 6c4c42e0 2019-06-24 stsp static const struct got_error *
3837 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
3839 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3840 6c4c42e0 2019-06-24 stsp int lineno;
3842 6c4c42e0 2019-06-24 stsp if (!view->searching) {
3843 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3844 6c4c42e0 2019-06-24 stsp return NULL;
3847 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3848 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3849 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
3851 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
3853 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3854 6c4c42e0 2019-06-24 stsp lineno = 1;
3856 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3859 6c4c42e0 2019-06-24 stsp while (1) {
3860 6c4c42e0 2019-06-24 stsp char *line = NULL;
3861 6c4c42e0 2019-06-24 stsp off_t offset;
3862 6c4c42e0 2019-06-24 stsp size_t len;
3864 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
3865 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
3866 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3867 6c4c42e0 2019-06-24 stsp free(line);
3871 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3872 6c4c42e0 2019-06-24 stsp lineno = 1;
3874 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3877 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
3878 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3879 6c4c42e0 2019-06-24 stsp free(line);
3880 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
3882 6c4c42e0 2019-06-24 stsp free(line);
3883 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
3884 2246482e 2019-06-25 stsp if (line && match_line(line, &view->regex)) {
3885 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3886 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
3887 6c4c42e0 2019-06-24 stsp free(line);
3890 6c4c42e0 2019-06-24 stsp free(line);
3891 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3897 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3898 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
3899 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
3902 6c4c42e0 2019-06-24 stsp return NULL;
3905 6c4c42e0 2019-06-24 stsp static const struct got_error *
3906 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
3908 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3909 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
3910 2b380cc8 2018-10-24 stsp int errcode;
3912 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
3913 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3914 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
3915 2b380cc8 2018-10-24 stsp if (errcode)
3916 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3918 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
3921 51fe7530 2019-08-19 stsp if (s->blame_complete)
3922 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
3924 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3925 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3926 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
3927 11b20872 2019-11-08 stsp &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
3929 669b5ffa 2018-10-07 stsp view_vborder(view);
3930 e5a0f69f 2018-08-18 stsp return err;
3933 e5a0f69f 2018-08-18 stsp static const struct got_error *
3934 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3935 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3937 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
3938 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
3939 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3940 669b5ffa 2018-10-07 stsp int begin_x = 0;
3942 e5a0f69f 2018-08-18 stsp switch (ch) {
3944 1e37a5c2 2019-05-12 jcs s->done = 1;
3947 1e37a5c2 2019-05-12 jcs case KEY_UP:
3948 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
3949 1e37a5c2 2019-05-12 jcs s->selected_line--;
3950 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
3951 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3952 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3954 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3955 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
3956 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3959 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3960 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3961 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3963 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3966 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3967 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3968 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3969 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3970 1e37a5c2 2019-05-12 jcs s->selected_line++;
3971 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3972 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3973 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3976 1e37a5c2 2019-05-12 jcs case 'p': {
3977 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3978 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3979 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3980 1e37a5c2 2019-05-12 jcs if (id == NULL)
3982 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3983 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3984 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3985 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3986 1e37a5c2 2019-05-12 jcs int obj_type;
3987 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3988 1e37a5c2 2019-05-12 jcs s->repo, id);
3991 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3992 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3993 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3994 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3997 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3998 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3999 1e37a5c2 2019-05-12 jcs pid->id, s->path);
4000 e5a0f69f 2018-08-18 stsp if (err) {
4001 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
4002 1e37a5c2 2019-05-12 jcs err = NULL;
4003 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4006 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
4008 1e37a5c2 2019-05-12 jcs free(blob_id);
4009 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
4010 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
4011 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4014 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4016 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4018 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
4019 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
4021 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
4026 1e37a5c2 2019-05-12 jcs s->done = 1;
4027 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4028 1e37a5c2 2019-05-12 jcs s->done = 0;
4029 1e37a5c2 2019-05-12 jcs if (thread_err)
4031 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4032 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
4033 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4034 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4035 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
4036 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
4041 1e37a5c2 2019-05-12 jcs case 'B': {
4042 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
4043 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
4044 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
4046 1e37a5c2 2019-05-12 jcs s->done = 1;
4047 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4048 1e37a5c2 2019-05-12 jcs s->done = 0;
4049 1e37a5c2 2019-05-12 jcs if (thread_err)
4051 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4052 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4053 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4054 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4055 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4056 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4057 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
4058 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
4063 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4064 1e37a5c2 2019-05-12 jcs case '\r': {
4065 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4066 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4067 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4068 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4069 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4070 1e37a5c2 2019-05-12 jcs if (id == NULL)
4072 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4075 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4076 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4077 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4078 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4079 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4080 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4081 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4082 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4085 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4086 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
4087 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4089 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4092 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4093 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4096 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
4098 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4101 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
4102 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4104 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4109 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4111 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4112 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4113 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4116 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4117 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4118 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4119 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4122 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4123 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4124 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4125 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4127 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4128 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4129 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4131 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4132 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4133 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4134 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4140 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4143 a70480e0 2018-06-23 stsp static const struct got_error *
4144 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4146 a70480e0 2018-06-23 stsp const struct got_error *error;
4147 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4148 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4149 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4150 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4151 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4152 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4154 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4156 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4158 a70480e0 2018-06-23 stsp #ifndef PROFILE
4159 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4160 8e94dd5b 2019-01-04 stsp NULL) == -1)
4161 a70480e0 2018-06-23 stsp err(1, "pledge");
4164 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4165 a70480e0 2018-06-23 stsp switch (ch) {
4167 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4170 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4171 69069811 2018-08-02 stsp if (repo_path == NULL)
4172 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4176 17020d27 2019-03-07 stsp usage_blame();
4177 a70480e0 2018-06-23 stsp /* NOTREACHED */
4181 a70480e0 2018-06-23 stsp argc -= optind;
4182 a70480e0 2018-06-23 stsp argv += optind;
4184 69069811 2018-08-02 stsp if (argc == 1)
4185 69069811 2018-08-02 stsp path = argv[0];
4187 a70480e0 2018-06-23 stsp usage_blame();
4189 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
4190 69069811 2018-08-02 stsp if (cwd == NULL) {
4191 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4192 69069811 2018-08-02 stsp goto done;
4194 69069811 2018-08-02 stsp if (repo_path == NULL) {
4195 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4196 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4197 69069811 2018-08-02 stsp goto done;
4199 eb41ed75 2019-02-05 stsp error = NULL;
4200 eb41ed75 2019-02-05 stsp if (worktree) {
4201 eb41ed75 2019-02-05 stsp repo_path =
4202 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4203 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
4204 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4205 eb41ed75 2019-02-05 stsp if (error)
4206 eb41ed75 2019-02-05 stsp goto done;
4208 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4209 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
4210 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4211 eb41ed75 2019-02-05 stsp goto done;
4216 a915003a 2019-02-05 stsp init_curses();
4218 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4219 c02c541e 2019-03-29 stsp if (error != NULL)
4220 8e94dd5b 2019-01-04 stsp goto done;
4222 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4223 c02c541e 2019-03-29 stsp if (error)
4224 92205607 2019-01-04 stsp goto done;
4226 eb41ed75 2019-02-05 stsp if (worktree) {
4227 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4228 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4229 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4230 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4231 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4232 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4233 eb41ed75 2019-02-05 stsp path) == -1) {
4234 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4235 eb41ed75 2019-02-05 stsp goto done;
4237 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4240 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4242 eb41ed75 2019-02-05 stsp if (error)
4243 69069811 2018-08-02 stsp goto done;
4245 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4246 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4247 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4248 a70480e0 2018-06-23 stsp if (error != NULL)
4249 66b4983c 2018-06-23 stsp goto done;
4250 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4251 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4253 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&commit_id, commit_id_str, repo);
4254 f2b6a97d 2019-07-15 stsp if (error) {
4255 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
4256 f2b6a97d 2019-07-15 stsp goto done;
4257 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&commit_id,
4258 f2b6a97d 2019-07-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
4261 a19e88aa 2018-06-23 stsp if (error != NULL)
4262 8b473291 2019-02-21 stsp goto done;
4264 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4265 8b473291 2019-02-21 stsp if (error)
4266 a19e88aa 2018-06-23 stsp goto done;
4268 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4269 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4270 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4271 e1cd8fed 2018-08-01 stsp goto done;
4273 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4274 7cbe629d 2018-08-04 stsp if (error)
4275 7cbe629d 2018-08-04 stsp goto done;
4276 12314ad4 2019-08-31 stsp if (worktree) {
4277 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4278 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4279 12314ad4 2019-08-31 stsp worktree = NULL;
4281 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4283 69069811 2018-08-02 stsp free(repo_path);
4284 69069811 2018-08-02 stsp free(cwd);
4285 a70480e0 2018-06-23 stsp free(commit_id);
4286 eb41ed75 2019-02-05 stsp if (worktree)
4287 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4289 a70480e0 2018-06-23 stsp got_repo_close(repo);
4290 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4291 a70480e0 2018-06-23 stsp return error;
4294 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4295 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
4296 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
4297 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
4298 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
4299 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
4300 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int selected, int limit,
4301 f26dddb7 2019-11-08 stsp int isroot, struct tog_colors *colors)
4303 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4304 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4305 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4306 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4307 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4309 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
4311 f7d12f7e 2018-08-01 stsp werase(view->window);
4313 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4314 ffd1d5e5 2018-06-23 stsp return NULL;
4316 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, label, view->ncols, 0);
4318 ffd1d5e5 2018-06-23 stsp return err;
4319 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4320 a3404814 2018-09-02 stsp wstandout(view->window);
4321 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
4323 11b20872 2019-11-08 stsp wattr_on(view->window,
4324 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4325 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4327 11b20872 2019-11-08 stsp wattr_off(view->window,
4328 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4329 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4330 a3404814 2018-09-02 stsp wstandend(view->window);
4331 2550e4c3 2018-07-13 stsp free(wline);
4332 2550e4c3 2018-07-13 stsp wline = NULL;
4333 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4334 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4335 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4336 ffd1d5e5 2018-06-23 stsp return NULL;
4337 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4339 ce52c690 2018-06-23 stsp return err;
4340 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4341 2550e4c3 2018-07-13 stsp free(wline);
4342 2550e4c3 2018-07-13 stsp wline = NULL;
4343 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4344 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4345 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4346 ffd1d5e5 2018-06-23 stsp return NULL;
4347 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4348 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4349 a1eca9bb 2018-06-23 stsp return NULL;
4351 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
4352 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(tree);
4353 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
4354 0cf4efb1 2018-09-29 stsp if (view->focussed)
4355 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4356 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
4358 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4359 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
4360 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4361 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4362 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4363 ffd1d5e5 2018-06-23 stsp return NULL;
4367 56e0773d 2019-11-28 stsp te = *first_displayed_entry;
4370 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4371 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4372 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
4373 848d6979 2019-08-12 stsp const char *modestr = "";
4374 56e0773d 2019-11-28 stsp mode_t mode;
4376 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4377 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4379 1d13200f 2018-07-12 stsp if (show_ids) {
4380 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4381 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4383 638f9024 2019-05-13 stsp return got_error_from_errno(
4384 230a42bd 2019-05-11 jcs "got_object_id_str");
4386 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4387 63c5ca5d 2019-08-24 stsp modestr = "$";
4388 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
4389 848d6979 2019-08-12 stsp modestr = "@";
4390 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4391 848d6979 2019-08-12 stsp modestr = "/";
4392 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4393 848d6979 2019-08-12 stsp modestr = "*";
4394 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4395 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te), modestr) == -1) {
4396 1d13200f 2018-07-12 stsp free(id_str);
4397 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4399 1d13200f 2018-07-12 stsp free(id_str);
4400 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4401 ffd1d5e5 2018-06-23 stsp if (err) {
4402 ffd1d5e5 2018-06-23 stsp free(line);
4405 ffd1d5e5 2018-06-23 stsp if (n == selected) {
4406 0cf4efb1 2018-09-29 stsp if (view->focussed)
4407 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4408 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
4410 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
4412 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4413 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4414 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4416 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4417 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4418 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4419 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4420 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
4421 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4422 ffd1d5e5 2018-06-23 stsp free(line);
4423 2550e4c3 2018-07-13 stsp free(wline);
4424 2550e4c3 2018-07-13 stsp wline = NULL;
4426 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4427 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
4428 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4432 ffd1d5e5 2018-06-23 stsp return err;
4435 ffd1d5e5 2018-06-23 stsp static void
4436 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
4437 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
4438 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int isroot)
4440 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4443 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
4446 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, 0);
4447 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
4448 ffd1d5e5 2018-06-23 stsp if (!isroot)
4449 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4454 56e0773d 2019-11-28 stsp while (*first_displayed_entry && i < maxscroll) {
4455 56e0773d 2019-11-28 stsp *first_displayed_entry = got_tree_entry_get_prev(tree,
4456 56e0773d 2019-11-28 stsp *first_displayed_entry);
4459 56e0773d 2019-11-28 stsp if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4460 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4463 768394f3 2019-01-24 stsp static int
4464 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4465 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
4466 56e0773d 2019-11-28 stsp struct got_tree_object *tree)
4468 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4469 ffd1d5e5 2018-06-23 stsp int n = 0;
4471 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
4472 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, *first_displayed_entry);
4474 56e0773d 2019-11-28 stsp next = got_object_tree_get_first_entry(tree);
4476 768394f3 2019-01-24 stsp last = last_displayed_entry;
4477 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4478 56e0773d 2019-11-28 stsp last = got_tree_entry_get_next(tree, last);
4479 768394f3 2019-01-24 stsp if (last) {
4480 768394f3 2019-01-24 stsp *first_displayed_entry = next;
4481 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, next);
4487 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4488 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4489 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4491 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4492 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4493 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4495 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4496 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
4497 56e0773d 2019-11-28 stsp + 1 /* slash */;
4499 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
4501 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4502 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4503 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4505 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4506 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4507 d9765a41 2018-06-23 stsp while (pt) {
4508 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
4509 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
4510 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4511 cb2ebc8a 2018-06-23 stsp goto done;
4513 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
4514 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4515 cb2ebc8a 2018-06-23 stsp goto done;
4517 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4520 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4521 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4522 ce52c690 2018-06-23 stsp goto done;
4526 ce52c690 2018-06-23 stsp if (err) {
4527 ce52c690 2018-06-23 stsp free(*path);
4528 ce52c690 2018-06-23 stsp *path = NULL;
4530 ce52c690 2018-06-23 stsp return err;
4533 ce52c690 2018-06-23 stsp static const struct got_error *
4534 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
4535 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4536 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4537 8b473291 2019-02-21 stsp struct got_repository *repo)
4539 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
4540 ce52c690 2018-06-23 stsp char *path;
4541 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
4543 a0de39f3 2019-08-09 stsp *new_view = NULL;
4545 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
4547 ce52c690 2018-06-23 stsp return err;
4549 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4550 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
4551 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
4552 83ce39e3 2019-08-12 stsp goto done;
4555 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
4556 e5a0f69f 2018-08-18 stsp if (err) {
4557 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
4558 fc06ba56 2019-08-22 stsp err = NULL;
4559 e5a0f69f 2018-08-18 stsp view_close(blame_view);
4561 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
4563 83ce39e3 2019-08-12 stsp free(path);
4564 69efd4c4 2018-07-18 stsp return err;
4567 69efd4c4 2018-07-18 stsp static const struct got_error *
4568 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
4569 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4570 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4571 8b473291 2019-02-21 stsp struct got_repository *repo)
4573 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
4574 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
4575 69efd4c4 2018-07-18 stsp char *path;
4577 a0de39f3 2019-08-09 stsp *new_view = NULL;
4579 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4580 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
4581 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
4583 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
4585 69efd4c4 2018-07-18 stsp return err;
4587 d01904d4 2019-06-25 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4589 e5a0f69f 2018-08-18 stsp view_close(log_view);
4591 e5a0f69f 2018-08-18 stsp *new_view = log_view;
4592 cb2ebc8a 2018-06-23 stsp free(path);
4593 cb2ebc8a 2018-06-23 stsp return err;
4596 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4597 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
4598 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4599 8b473291 2019-02-21 stsp struct got_repository *repo)
4601 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4602 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
4603 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4605 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
4607 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
4608 ffd1d5e5 2018-06-23 stsp if (err != NULL)
4609 ffd1d5e5 2018-06-23 stsp goto done;
4611 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4612 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4613 ffd1d5e5 2018-06-23 stsp goto done;
4616 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
4617 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4618 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4619 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
4620 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
4621 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4622 6484ec90 2018-09-29 stsp goto done;
4624 8b473291 2019-02-21 stsp s->refs = refs;
4625 fb2756b9 2018-08-04 stsp s->repo = repo;
4627 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4629 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4630 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
4631 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
4632 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4634 c0b01bdb 2019-11-08 stsp goto done;
4635 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4636 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
4637 c0b01bdb 2019-11-08 stsp if (err) {
4638 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4639 c0b01bdb 2019-11-08 stsp goto done;
4641 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
4642 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
4643 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4644 c0b01bdb 2019-11-08 stsp if (err) {
4645 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4646 c0b01bdb 2019-11-08 stsp goto done;
4649 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
4650 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
4651 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4652 11b20872 2019-11-08 stsp if (err) {
4653 11b20872 2019-11-08 stsp free_colors(&s->colors);
4654 11b20872 2019-11-08 stsp goto done;
4657 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4658 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4659 c0b01bdb 2019-11-08 stsp if (err) {
4660 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4661 c0b01bdb 2019-11-08 stsp goto done;
4665 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
4666 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
4667 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
4668 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
4669 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
4671 ad80ab7b 2018-08-04 stsp free(commit_id_str);
4672 6484ec90 2018-09-29 stsp if (err) {
4673 fb2756b9 2018-08-04 stsp free(s->tree_label);
4674 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4676 ad80ab7b 2018-08-04 stsp return err;
4679 e5a0f69f 2018-08-18 stsp static const struct got_error *
4680 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
4682 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4684 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4685 fb2756b9 2018-08-04 stsp free(s->tree_label);
4686 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4687 6484ec90 2018-09-29 stsp free(s->commit_id);
4688 6484ec90 2018-09-29 stsp s->commit_id = NULL;
4689 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
4690 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
4691 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
4692 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
4693 ad80ab7b 2018-08-04 stsp free(parent);
4696 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
4697 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
4698 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
4700 7c32bd05 2019-06-22 stsp return NULL;
4703 7c32bd05 2019-06-22 stsp static const struct got_error *
4704 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
4706 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4708 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
4709 7c32bd05 2019-06-22 stsp return NULL;
4712 7c32bd05 2019-06-22 stsp static int
4713 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4715 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
4717 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4721 7c32bd05 2019-06-22 stsp static const struct got_error *
4722 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
4724 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4725 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
4727 7c32bd05 2019-06-22 stsp if (!view->searching) {
4728 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4729 7c32bd05 2019-06-22 stsp return NULL;
4732 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4733 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
4734 7c32bd05 2019-06-22 stsp if (s->selected_entry)
4735 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
4736 56e0773d 2019-11-28 stsp s->selected_entry);
4738 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4740 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
4741 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4743 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
4744 56e0773d 2019-11-28 stsp s->selected_entry);
4747 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4748 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4750 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4753 7c32bd05 2019-06-22 stsp while (1) {
4754 56e0773d 2019-11-28 stsp if (te == NULL) {
4755 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
4756 ac66afb8 2019-06-24 stsp view->search_next_done = 1;
4757 ac66afb8 2019-06-24 stsp return NULL;
4759 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4760 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4762 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4765 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
4766 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4767 56e0773d 2019-11-28 stsp s->matched_entry = te;
4771 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4772 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
4774 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
4777 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4778 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
4779 7c32bd05 2019-06-22 stsp s->selected = 0;
4782 e5a0f69f 2018-08-18 stsp return NULL;
4785 ad80ab7b 2018-08-04 stsp static const struct got_error *
4786 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
4788 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
4789 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4790 e5a0f69f 2018-08-18 stsp char *parent_path;
4792 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
4794 e5a0f69f 2018-08-18 stsp return err;
4796 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
4797 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
4798 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4799 56e0773d 2019-11-28 stsp s->tree, s->selected, view->nlines, s->tree == s->root,
4800 bddb1296 2019-11-08 stsp &s->colors);
4801 e5a0f69f 2018-08-18 stsp free(parent_path);
4803 669b5ffa 2018-10-07 stsp view_vborder(view);
4804 e5a0f69f 2018-08-18 stsp return err;
4807 e5a0f69f 2018-08-18 stsp static const struct got_error *
4808 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4809 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4811 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4812 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
4813 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
4814 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
4816 e5a0f69f 2018-08-18 stsp switch (ch) {
4818 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
4821 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
4823 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4824 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4825 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
4826 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
4827 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
4828 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4829 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4831 1e37a5c2 2019-05-12 jcs return err;
4832 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
4834 1e37a5c2 2019-05-12 jcs view_close(log_view);
4837 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
4838 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4840 1e37a5c2 2019-05-12 jcs *new_view = log_view;
4843 1e37a5c2 2019-05-12 jcs case KEY_UP:
4844 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
4845 1e37a5c2 2019-05-12 jcs s->selected--;
4846 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
4849 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
4851 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
4852 56e0773d 2019-11-28 stsp s->tree, s->tree == s->root);
4854 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4855 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
4856 56e0773d 2019-11-28 stsp MAX(0, view->nlines - 4 - s->selected), s->tree,
4857 1e37a5c2 2019-05-12 jcs s->tree == s->root);
4858 1e37a5c2 2019-05-12 jcs s->selected = 0;
4859 56e0773d 2019-11-28 stsp if (got_object_tree_get_first_entry(s->tree) ==
4860 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
4861 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
4864 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4865 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
4866 1e37a5c2 2019-05-12 jcs s->selected++;
4869 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4871 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
4873 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
4874 56e0773d 2019-11-28 stsp s->last_displayed_entry, s->tree);
4876 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4877 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4879 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
4880 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
4881 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4884 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
4885 56e0773d 2019-11-28 stsp view->nlines, s->last_displayed_entry, s->tree);
4886 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
4887 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
4888 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
4889 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
4891 1e37a5c2 2019-05-12 jcs ndisplayed++;
4892 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
4893 1e37a5c2 2019-05-12 jcs } while (te);
4894 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
4897 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4899 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4900 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4901 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
4902 1e37a5c2 2019-05-12 jcs /* user selected '..' */
4903 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
4905 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
4906 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
4908 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
4909 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
4910 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
4911 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
4912 1e37a5c2 2019-05-12 jcs s->selected_entry =
4913 1e37a5c2 2019-05-12 jcs parent->selected_entry;
4914 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
4915 1e37a5c2 2019-05-12 jcs free(parent);
4916 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
4917 56e0773d 2019-11-28 stsp s->selected_entry))) {
4918 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
4919 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
4920 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
4923 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
4924 941e9f74 2019-05-21 stsp if (err) {
4925 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
4928 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
4929 56e0773d 2019-11-28 stsp s->selected_entry))) {
4930 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
4931 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
4932 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
4934 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
4935 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
4936 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
4939 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
4940 669b5ffa 2018-10-07 stsp err = view_close_child(view);
4942 669b5ffa 2018-10-07 stsp return err;
4943 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
4944 669b5ffa 2018-10-07 stsp if (err) {
4945 1e37a5c2 2019-05-12 jcs view_close(blame_view);
4948 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
4949 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
4951 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
4954 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4955 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
4956 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4962 ffd1d5e5 2018-06-23 stsp return err;
4965 ffd1d5e5 2018-06-23 stsp __dead static void
4966 ffd1d5e5 2018-06-23 stsp usage_tree(void)
4969 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4970 ffd1d5e5 2018-06-23 stsp getprogname());
4974 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4975 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
4977 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
4978 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
4979 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4980 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
4981 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4982 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
4983 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
4984 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
4986 5221c383 2018-08-01 stsp struct tog_view *view;
4988 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4990 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
4991 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4992 d188b9a6 2019-01-04 stsp NULL) == -1)
4993 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
4996 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
4997 ffd1d5e5 2018-06-23 stsp switch (ch) {
4999 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
5002 17020d27 2019-03-07 stsp usage_tree();
5003 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
5007 ffd1d5e5 2018-06-23 stsp argc -= optind;
5008 ffd1d5e5 2018-06-23 stsp argv += optind;
5010 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
5011 52185f70 2019-02-05 stsp struct got_worktree *worktree;
5012 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
5013 52185f70 2019-02-05 stsp if (cwd == NULL)
5014 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
5015 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5016 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5017 52185f70 2019-02-05 stsp goto done;
5018 52185f70 2019-02-05 stsp if (worktree) {
5019 52185f70 2019-02-05 stsp free(cwd);
5020 52185f70 2019-02-05 stsp repo_path =
5021 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5022 52185f70 2019-02-05 stsp got_worktree_close(worktree);
5024 52185f70 2019-02-05 stsp repo_path = cwd;
5025 52185f70 2019-02-05 stsp if (repo_path == NULL) {
5026 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5027 52185f70 2019-02-05 stsp goto done;
5029 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
5030 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
5031 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
5032 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
5034 f13374f4 2020-01-14 stsp usage_tree();
5036 a915003a 2019-02-05 stsp init_curses();
5038 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5039 c02c541e 2019-03-29 stsp if (error != NULL)
5040 52185f70 2019-02-05 stsp goto done;
5042 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
5043 c02c541e 2019-03-29 stsp if (error)
5044 52185f70 2019-02-05 stsp goto done;
5046 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
5047 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
5049 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&commit_id, commit_id_arg, repo);
5050 f2b6a97d 2019-07-15 stsp if (error) {
5051 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
5052 f2b6a97d 2019-07-15 stsp goto done;
5053 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&commit_id,
5054 f2b6a97d 2019-07-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
5057 ffd1d5e5 2018-06-23 stsp if (error != NULL)
5058 ffd1d5e5 2018-06-23 stsp goto done;
5060 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5061 ffd1d5e5 2018-06-23 stsp if (error != NULL)
5062 ffd1d5e5 2018-06-23 stsp goto done;
5064 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
5065 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5066 ffd1d5e5 2018-06-23 stsp if (error != NULL)
5067 8b473291 2019-02-21 stsp goto done;
5069 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5070 8b473291 2019-02-21 stsp if (error)
5071 ffd1d5e5 2018-06-23 stsp goto done;
5073 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5074 5221c383 2018-08-01 stsp if (view == NULL) {
5075 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5076 5221c383 2018-08-01 stsp goto done;
5078 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
5079 ad80ab7b 2018-08-04 stsp if (error)
5080 ad80ab7b 2018-08-04 stsp goto done;
5081 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5083 52185f70 2019-02-05 stsp free(repo_path);
5084 ffd1d5e5 2018-06-23 stsp free(commit_id);
5085 ffd1d5e5 2018-06-23 stsp if (commit)
5086 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5088 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5090 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5091 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5092 ffd1d5e5 2018-06-23 stsp return error;
5095 ce5b7c56 2019-07-09 stsp static void
5096 ce5b7c56 2019-07-09 stsp list_commands(void)
5100 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
5101 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
5102 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
5103 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->name);
5105 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
5108 4ed7e80c 2018-05-20 stsp __dead static void
5109 ce5b7c56 2019-07-09 stsp usage(int hflag)
5111 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5112 53ccebc2 2019-07-30 stsp getprogname());
5113 ce5b7c56 2019-07-09 stsp if (hflag)
5114 ce5b7c56 2019-07-09 stsp list_commands();
5118 c2301be8 2018-04-30 stsp static char **
5119 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
5121 c2301be8 2018-04-30 stsp char **argv;
5122 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
5124 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
5125 c2301be8 2018-04-30 stsp if (argv == NULL)
5126 c2301be8 2018-04-30 stsp err(1, "calloc");
5127 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
5128 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
5129 e10c916e 2019-09-15 hiltjo err(1, "strdup");
5130 c2301be8 2018-04-30 stsp if (arg1) {
5131 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
5132 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
5133 e10c916e 2019-09-15 hiltjo err(1, "strdup");
5136 c2301be8 2018-04-30 stsp return argv;
5140 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
5142 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
5143 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
5144 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
5145 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
5146 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
5147 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
5148 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
5151 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
5153 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5154 9f7d7167 2018-04-29 stsp switch (ch) {
5156 9f7d7167 2018-04-29 stsp hflag = 1;
5159 53ccebc2 2019-07-30 stsp Vflag = 1;
5162 ce5b7c56 2019-07-09 stsp usage(hflag);
5163 9f7d7167 2018-04-29 stsp /* NOTREACHED */
5167 9f7d7167 2018-04-29 stsp argc -= optind;
5168 9f7d7167 2018-04-29 stsp argv += optind;
5169 9f7d7167 2018-04-29 stsp optind = 0;
5170 c2301be8 2018-04-30 stsp optreset = 1;
5172 53ccebc2 2019-07-30 stsp if (Vflag) {
5173 53ccebc2 2019-07-30 stsp got_version_print_str();
5177 c2301be8 2018-04-30 stsp if (argc == 0) {
5178 f29d3e89 2018-06-23 stsp if (hflag)
5179 ce5b7c56 2019-07-09 stsp usage(hflag);
5180 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
5181 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
5182 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
5187 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
5188 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
5189 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
5190 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
5191 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
5196 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
5197 d70c3147 2019-07-09 stsp fprintf(stderr, "%s: unknown command '%s'\n",
5198 3642c4c6 2019-07-09 stsp getprogname(), argv[0]);
5199 ce5b7c56 2019-07-09 stsp list_commands();
5204 3642c4c6 2019-07-09 stsp if (hflag)
5205 3642c4c6 2019-07-09 stsp cmd->cmd_usage();
5207 3642c4c6 2019-07-09 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5210 c2301be8 2018-04-30 stsp free(cmd_argv);
5211 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
5212 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);