2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 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 9f7d7167 2018-04-29 stsp #include <stdlib.h>
28 26ed57b2 2018-05-19 stsp #include <stdio.h>
29 9f7d7167 2018-04-29 stsp #include <getopt.h>
30 9f7d7167 2018-04-29 stsp #include <string.h>
31 9f7d7167 2018-04-29 stsp #include <err.h>
32 80ddbec8 2018-04-29 stsp #include <unistd.h>
33 26ed57b2 2018-05-19 stsp #include <util.h>
34 26ed57b2 2018-05-19 stsp #include <limits.h>
35 61e69b96 2018-05-20 stsp #include <wchar.h>
36 788c352e 2018-06-16 stsp #include <time.h>
37 84451b3e 2018-07-10 stsp #include <pthread.h>
38 5036bf37 2018-09-24 stsp #include <libgen.h>
39 60493ae3 2019-06-20 stsp #include <regex.h>
41 53ccebc2 2019-07-30 stsp #include "got_version.h"
42 9f7d7167 2018-04-29 stsp #include "got_error.h"
43 80ddbec8 2018-04-29 stsp #include "got_object.h"
44 80ddbec8 2018-04-29 stsp #include "got_reference.h"
45 80ddbec8 2018-04-29 stsp #include "got_repository.h"
46 80ddbec8 2018-04-29 stsp #include "got_diff.h"
47 511a516b 2018-05-19 stsp #include "got_opentemp.h"
48 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
49 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
50 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
51 a70480e0 2018-06-23 stsp #include "got_blame.h"
52 c2db6724 2019-01-04 stsp #include "got_privsep.h"
53 1dd54920 2019-05-11 stsp #include "got_path.h"
54 b7165be3 2019-02-05 stsp #include "got_worktree.h"
57 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
64 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
66 9f7d7167 2018-04-29 stsp #ifndef nitems
67 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 9f7d7167 2018-04-29 stsp struct tog_cmd {
71 c2301be8 2018-04-30 stsp const char *name;
72 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
73 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
76 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
77 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
78 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
79 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
80 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
82 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
83 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
84 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
85 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
88 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
89 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
90 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
91 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
94 d6b05b5b 2018-08-04 stsp enum tog_view_type {
95 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
96 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
97 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
98 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
101 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
103 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
104 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
105 ba4f502b 2018-08-04 stsp struct got_object_id *id;
106 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
109 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
110 ba4f502b 2018-08-04 stsp struct commit_queue {
111 ba4f502b 2018-08-04 stsp int ncommits;
112 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
115 f26dddb7 2019-11-08 stsp struct tog_color {
116 f26dddb7 2019-11-08 stsp SIMPLEQ_ENTRY(tog_color) entry;
117 6d17833f 2019-11-08 stsp regex_t regex;
118 6d17833f 2019-11-08 stsp short colorpair;
120 f26dddb7 2019-11-08 stsp SIMPLEQ_HEAD(tog_colors, tog_color);
122 11b20872 2019-11-08 stsp static const struct got_error *
123 11b20872 2019-11-08 stsp add_color(struct tog_colors *colors, const char *pattern,
124 11b20872 2019-11-08 stsp int idx, short color)
126 11b20872 2019-11-08 stsp const struct got_error *err = NULL;
127 11b20872 2019-11-08 stsp struct tog_color *tc;
128 11b20872 2019-11-08 stsp int regerr = 0;
130 11b20872 2019-11-08 stsp if (idx < 1 || idx > COLOR_PAIRS - 1)
131 11b20872 2019-11-08 stsp return NULL;
133 11b20872 2019-11-08 stsp init_pair(idx, color, -1);
135 11b20872 2019-11-08 stsp tc = calloc(1, sizeof(*tc));
136 11b20872 2019-11-08 stsp if (tc == NULL)
137 11b20872 2019-11-08 stsp return got_error_from_errno("calloc");
138 11b20872 2019-11-08 stsp regerr = regcomp(&tc->regex, pattern,
139 11b20872 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
140 11b20872 2019-11-08 stsp if (regerr) {
141 11b20872 2019-11-08 stsp static char regerr_msg[512];
142 11b20872 2019-11-08 stsp static char err_msg[512];
143 11b20872 2019-11-08 stsp regerror(regerr, &tc->regex, regerr_msg,
144 11b20872 2019-11-08 stsp sizeof(regerr_msg));
145 11b20872 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
146 11b20872 2019-11-08 stsp regerr_msg);
147 11b20872 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
149 11b20872 2019-11-08 stsp return err;
151 11b20872 2019-11-08 stsp tc->colorpair = idx;
152 11b20872 2019-11-08 stsp SIMPLEQ_INSERT_HEAD(colors, tc, entry);
153 11b20872 2019-11-08 stsp return NULL;
156 11b20872 2019-11-08 stsp static void
157 11b20872 2019-11-08 stsp free_colors(struct tog_colors *colors)
159 11b20872 2019-11-08 stsp struct tog_color *tc;
161 11b20872 2019-11-08 stsp while (!SIMPLEQ_EMPTY(colors)) {
162 11b20872 2019-11-08 stsp tc = SIMPLEQ_FIRST(colors);
163 11b20872 2019-11-08 stsp SIMPLEQ_REMOVE_HEAD(colors, entry);
164 11b20872 2019-11-08 stsp regfree(&tc->regex);
169 11b20872 2019-11-08 stsp struct tog_color *
170 11b20872 2019-11-08 stsp get_color(struct tog_colors *colors, int colorpair)
172 11b20872 2019-11-08 stsp struct tog_color *tc = NULL;
174 11b20872 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
175 11b20872 2019-11-08 stsp if (tc->colorpair == colorpair)
179 11b20872 2019-11-08 stsp return NULL;
183 11b20872 2019-11-08 stsp default_color_value(const char *envvar)
185 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
186 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
187 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
188 11b20872 2019-11-08 stsp return COLOR_CYAN;
189 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
190 11b20872 2019-11-08 stsp return COLOR_YELLOW;
191 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
192 11b20872 2019-11-08 stsp return COLOR_GREEN;
193 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
194 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
195 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
196 11b20872 2019-11-08 stsp return COLOR_CYAN;
197 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
198 11b20872 2019-11-08 stsp return COLOR_BLUE;
199 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
200 11b20872 2019-11-08 stsp return COLOR_GREEN;
201 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
202 11b20872 2019-11-08 stsp return COLOR_GREEN;
203 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
204 11b20872 2019-11-08 stsp return COLOR_CYAN;
205 11b20872 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
206 11b20872 2019-11-08 stsp return COLOR_YELLOW;
212 11b20872 2019-11-08 stsp get_color_value(const char *envvar)
214 11b20872 2019-11-08 stsp const char *val = getenv(envvar);
216 11b20872 2019-11-08 stsp if (val == NULL)
217 11b20872 2019-11-08 stsp return default_color_value(envvar);
219 11b20872 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
220 11b20872 2019-11-08 stsp return COLOR_BLACK;
221 11b20872 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
222 11b20872 2019-11-08 stsp return COLOR_RED;
223 11b20872 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
224 11b20872 2019-11-08 stsp return COLOR_GREEN;
225 11b20872 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
226 11b20872 2019-11-08 stsp return COLOR_YELLOW;
227 11b20872 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
228 11b20872 2019-11-08 stsp return COLOR_BLUE;
229 11b20872 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
230 11b20872 2019-11-08 stsp return COLOR_MAGENTA;
231 11b20872 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
232 11b20872 2019-11-08 stsp return COLOR_CYAN;
233 11b20872 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
234 11b20872 2019-11-08 stsp return COLOR_WHITE;
235 11b20872 2019-11-08 stsp if (strcasecmp(val, "default") == 0)
238 11b20872 2019-11-08 stsp return default_color_value(envvar);
242 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
243 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
245 15a087fe 2019-02-21 stsp int first_displayed_line;
246 15a087fe 2019-02-21 stsp int last_displayed_line;
248 15a087fe 2019-02-21 stsp int diff_context;
249 15a087fe 2019-02-21 stsp struct got_repository *repo;
250 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
251 bddb1296 2019-11-08 stsp struct tog_colors colors;
253 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
254 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
257 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
259 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
260 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
261 1a76625f 2018-10-22 stsp int commits_needed;
262 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
263 1a76625f 2018-10-22 stsp struct commit_queue *commits;
264 1a76625f 2018-10-22 stsp const char *in_repo_path;
265 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
266 1a76625f 2018-10-22 stsp struct got_repository *repo;
267 1a76625f 2018-10-22 stsp int log_complete;
268 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
269 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
270 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
271 13add988 2019-10-15 stsp int *searching;
272 13add988 2019-10-15 stsp int *search_next_done;
273 13add988 2019-10-15 stsp regex_t *regex;
276 1a76625f 2018-10-22 stsp struct tog_log_view_state {
277 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
278 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
279 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
280 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
281 b01e7d3b 2018-08-04 stsp int selected;
282 b01e7d3b 2018-08-04 stsp char *in_repo_path;
283 d01904d4 2019-06-25 stsp const char *head_ref_name;
284 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
285 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
286 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
287 1a76625f 2018-10-22 stsp sig_atomic_t quit;
288 1a76625f 2018-10-22 stsp pthread_t thread;
289 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
290 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
291 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
292 11b20872 2019-11-08 stsp struct tog_colors colors;
295 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_MINUS 1
296 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_PLUS 2
297 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_CHUNK_HEADER 3
298 11b20872 2019-11-08 stsp #define TOG_COLOR_DIFF_META 4
299 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SUBMODULE 5
300 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_SYMLINK 6
301 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_DIRECTORY 7
302 11b20872 2019-11-08 stsp #define TOG_COLOR_TREE_EXECUTABLE 8
303 11b20872 2019-11-08 stsp #define TOG_COLOR_COMMIT 9
304 11b20872 2019-11-08 stsp #define TOG_COLOR_AUTHOR 10
305 11b20872 2019-11-08 stsp #define TOG_COLOR_DATE 11
307 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
308 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
309 e9424729 2018-08-04 stsp int nlines;
311 e9424729 2018-08-04 stsp struct tog_view *view;
312 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
316 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
317 e9424729 2018-08-04 stsp const char *path;
318 e9424729 2018-08-04 stsp struct got_repository *repo;
319 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
320 e9424729 2018-08-04 stsp int *complete;
321 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
322 fc06ba56 2019-08-22 stsp void *cancel_arg;
325 e9424729 2018-08-04 stsp struct tog_blame {
327 e9424729 2018-08-04 stsp size_t filesize;
328 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
329 6fcac457 2018-11-19 stsp int nlines;
330 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
331 e9424729 2018-08-04 stsp pthread_t thread;
332 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
333 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
334 e9424729 2018-08-04 stsp const char *path;
337 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
338 7cbe629d 2018-08-04 stsp int first_displayed_line;
339 7cbe629d 2018-08-04 stsp int last_displayed_line;
340 7cbe629d 2018-08-04 stsp int selected_line;
341 7cbe629d 2018-08-04 stsp int blame_complete;
344 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
345 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
346 e5a0f69f 2018-08-18 stsp char *path;
347 7cbe629d 2018-08-04 stsp struct got_repository *repo;
348 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
349 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
350 e9424729 2018-08-04 stsp struct tog_blame blame;
351 6c4c42e0 2019-06-24 stsp int matched_line;
352 11b20872 2019-11-08 stsp struct tog_colors colors;
355 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
356 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
357 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
358 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
359 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
360 ad80ab7b 2018-08-04 stsp int selected;
363 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
365 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
366 ad80ab7b 2018-08-04 stsp char *tree_label;
367 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
368 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
369 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
370 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
371 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
372 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
373 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
374 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
375 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
376 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
377 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
378 bddb1296 2019-11-08 stsp struct tog_colors colors;
382 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
384 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
385 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
386 669b5ffa 2018-10-07 stsp * there is enough screen estate.
388 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
389 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
391 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
392 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
393 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
395 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
396 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
398 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
400 cc3c9aac 2018-08-01 stsp struct tog_view {
401 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
402 26ed57b2 2018-05-19 stsp WINDOW *window;
403 26ed57b2 2018-05-19 stsp PANEL *panel;
404 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
405 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
406 1004088d 2018-09-29 stsp int focussed;
407 669b5ffa 2018-10-07 stsp struct tog_view *parent;
408 669b5ffa 2018-10-07 stsp struct tog_view *child;
409 669b5ffa 2018-10-07 stsp int child_focussed;
411 5dc9f4bc 2018-08-04 stsp /* type-specific state */
412 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
414 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
415 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
416 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
417 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
420 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
421 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
422 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
423 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
425 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
426 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
427 60493ae3 2019-06-20 stsp int searching;
428 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
429 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
430 60493ae3 2019-06-20 stsp int search_next_done;
431 1803e47f 2019-06-22 stsp regex_t regex;
434 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
435 fb872ab2 2019-02-21 stsp struct got_object_id *, struct got_object_id *, struct tog_view *,
436 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
437 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
438 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
439 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
440 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
442 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
443 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *,
444 d01904d4 2019-06-25 stsp struct got_repository *, const char *, const char *, int);
445 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
446 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
447 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
448 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
449 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
450 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
452 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
453 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *, struct got_repository *);
454 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
455 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
456 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
457 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
458 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
459 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
461 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
462 8b473291 2019-02-21 stsp struct got_tree_object *, struct got_object_id *,
463 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
464 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
465 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
466 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
467 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
468 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
469 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
471 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
472 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
474 25791caa 2018-10-24 stsp static void
475 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
477 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
480 83baff54 2019-08-12 stsp static void
481 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
483 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
486 e5a0f69f 2018-08-18 stsp static const struct got_error *
487 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
489 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
491 669b5ffa 2018-10-07 stsp if (view->child) {
492 669b5ffa 2018-10-07 stsp view_close(view->child);
493 669b5ffa 2018-10-07 stsp view->child = NULL;
495 e5a0f69f 2018-08-18 stsp if (view->close)
496 e5a0f69f 2018-08-18 stsp err = view->close(view);
497 ea5e7bb5 2018-08-01 stsp if (view->panel)
498 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
499 ea5e7bb5 2018-08-01 stsp if (view->window)
500 ea5e7bb5 2018-08-01 stsp delwin(view->window);
501 ea5e7bb5 2018-08-01 stsp free(view);
502 e5a0f69f 2018-08-18 stsp return err;
505 ea5e7bb5 2018-08-01 stsp static struct tog_view *
506 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
507 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
509 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
511 ea5e7bb5 2018-08-01 stsp if (view == NULL)
512 ea5e7bb5 2018-08-01 stsp return NULL;
514 d6b05b5b 2018-08-04 stsp view->type = type;
515 f7d12f7e 2018-08-01 stsp view->lines = LINES;
516 f7d12f7e 2018-08-01 stsp view->cols = COLS;
517 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
518 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
519 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
520 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
521 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
522 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
523 96a765a8 2018-08-04 stsp view_close(view);
524 ea5e7bb5 2018-08-01 stsp return NULL;
526 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
527 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
528 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
529 96a765a8 2018-08-04 stsp view_close(view);
530 ea5e7bb5 2018-08-01 stsp return NULL;
533 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
534 ea5e7bb5 2018-08-01 stsp return view;
538 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
540 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
542 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
545 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
547 5c60c32a 2018-10-18 stsp static const struct got_error *
548 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
550 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
552 5c60c32a 2018-10-18 stsp view->begin_y = 0;
553 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
554 5c60c32a 2018-10-18 stsp view->nlines = LINES;
555 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
556 5c60c32a 2018-10-18 stsp view->lines = LINES;
557 5c60c32a 2018-10-18 stsp view->cols = COLS;
558 5c60c32a 2018-10-18 stsp err = view_resize(view);
560 5c60c32a 2018-10-18 stsp return err;
562 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
563 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
565 5c60c32a 2018-10-18 stsp return NULL;
568 5c60c32a 2018-10-18 stsp static const struct got_error *
569 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
571 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
573 5c60c32a 2018-10-18 stsp view->begin_x = 0;
574 5c60c32a 2018-10-18 stsp view->begin_y = 0;
575 5c60c32a 2018-10-18 stsp view->nlines = LINES;
576 5c60c32a 2018-10-18 stsp view->ncols = COLS;
577 5c60c32a 2018-10-18 stsp view->lines = LINES;
578 5c60c32a 2018-10-18 stsp view->cols = COLS;
579 5c60c32a 2018-10-18 stsp err = view_resize(view);
581 5c60c32a 2018-10-18 stsp return err;
583 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
584 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
586 5c60c32a 2018-10-18 stsp return NULL;
590 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
592 5c60c32a 2018-10-18 stsp return view->parent == NULL;
595 4d8c2215 2018-08-19 stsp static const struct got_error *
596 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
598 f7d12f7e 2018-08-01 stsp int nlines, ncols;
600 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
601 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
603 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
605 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
606 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
608 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
610 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
611 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
612 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
613 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
614 25791caa 2018-10-24 stsp wclear(view->window);
616 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
617 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
618 0cf4efb1 2018-09-29 stsp view->lines = LINES;
619 0cf4efb1 2018-09-29 stsp view->cols = COLS;
621 6e3e5d9c 2018-10-18 stsp if (view->child) {
622 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
623 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
624 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
625 5c60c32a 2018-10-18 stsp if (view->child->focussed)
626 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
628 5c60c32a 2018-10-18 stsp show_panel(view->panel);
630 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
631 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
635 5c60c32a 2018-10-18 stsp return NULL;
638 669b5ffa 2018-10-07 stsp static const struct got_error *
639 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
641 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
643 669b5ffa 2018-10-07 stsp if (view->child == NULL)
644 669b5ffa 2018-10-07 stsp return NULL;
646 669b5ffa 2018-10-07 stsp err = view_close(view->child);
647 669b5ffa 2018-10-07 stsp view->child = NULL;
648 669b5ffa 2018-10-07 stsp return err;
651 669b5ffa 2018-10-07 stsp static const struct got_error *
652 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
654 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
656 669b5ffa 2018-10-07 stsp view->child = child;
657 669b5ffa 2018-10-07 stsp child->parent = view;
658 669b5ffa 2018-10-07 stsp return err;
662 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
664 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
667 34bc9ec9 2019-02-22 stsp static void
668 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
670 25791caa 2018-10-24 stsp int cols, lines;
671 25791caa 2018-10-24 stsp struct winsize size;
673 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
674 25791caa 2018-10-24 stsp cols = 80; /* Default */
675 25791caa 2018-10-24 stsp lines = 24;
677 25791caa 2018-10-24 stsp cols = size.ws_col;
678 25791caa 2018-10-24 stsp lines = size.ws_row;
680 25791caa 2018-10-24 stsp resize_term(lines, cols);
683 2b49a8ae 2019-06-22 stsp static const struct got_error *
684 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
686 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
687 2b49a8ae 2019-06-22 stsp char pattern[1024];
689 7d049c18 2019-08-21 stsp int begin_x = 0;
691 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
692 2b49a8ae 2019-06-22 stsp return NULL;
694 7d049c18 2019-08-21 stsp if (!view_is_parent_view(view))
695 7d049c18 2019-08-21 stsp begin_x = view_split_begin_x(view->begin_x);
696 2b49a8ae 2019-06-22 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1,
697 7d049c18 2019-08-21 stsp begin_x, "/");
698 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
700 2b49a8ae 2019-06-22 stsp nocbreak();
702 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
705 2b49a8ae 2019-06-22 stsp if (ret == ERR)
706 2b49a8ae 2019-06-22 stsp return NULL;
708 2b49a8ae 2019-06-22 stsp if (view->searching) {
709 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
710 2b49a8ae 2019-06-22 stsp view->searching = 0;
713 2b49a8ae 2019-06-22 stsp if (regcomp(&view->regex, pattern,
714 f5daf9b1 2019-06-24 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
715 7c32bd05 2019-06-22 stsp err = view->search_start(view);
717 7c32bd05 2019-06-22 stsp regfree(&view->regex);
718 7c32bd05 2019-06-22 stsp return err;
720 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
721 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
722 2b49a8ae 2019-06-22 stsp view->search_next(view);
725 2b49a8ae 2019-06-22 stsp return NULL;
728 0cf4efb1 2018-09-29 stsp static const struct got_error *
729 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
730 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
731 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
733 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
734 669b5ffa 2018-10-07 stsp struct tog_view *v;
735 1a76625f 2018-10-22 stsp int ch, errcode;
737 e5a0f69f 2018-08-18 stsp *new = NULL;
738 e5a0f69f 2018-08-18 stsp *dead = NULL;
739 0cf4efb1 2018-09-29 stsp *focus = NULL;
741 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
742 60493ae3 2019-06-20 stsp errcode = pthread_mutex_unlock(&tog_mutex);
743 60493ae3 2019-06-20 stsp if (errcode)
744 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
745 60493ae3 2019-06-20 stsp "pthread_mutex_unlock");
746 60493ae3 2019-06-20 stsp pthread_yield();
747 60493ae3 2019-06-20 stsp errcode = pthread_mutex_lock(&tog_mutex);
748 60493ae3 2019-06-20 stsp if (errcode)
749 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
750 60493ae3 2019-06-20 stsp "pthread_mutex_lock");
751 60493ae3 2019-06-20 stsp view->search_next(view);
752 60493ae3 2019-06-20 stsp return NULL;
755 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
756 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
757 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
758 1a76625f 2018-10-22 stsp if (errcode)
759 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
760 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
761 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
762 1a76625f 2018-10-22 stsp if (errcode)
763 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
764 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
766 25791caa 2018-10-24 stsp if (tog_sigwinch_received) {
767 25791caa 2018-10-24 stsp tog_resizeterm();
768 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
769 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
770 25791caa 2018-10-24 stsp err = view_resize(v);
772 25791caa 2018-10-24 stsp return err;
773 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
775 25791caa 2018-10-24 stsp return err;
779 e5a0f69f 2018-08-18 stsp switch (ch) {
783 1e37a5c2 2019-05-12 jcs if (view->child) {
784 1e37a5c2 2019-05-12 jcs *focus = view->child;
785 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
786 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
787 1e37a5c2 2019-05-12 jcs *focus = view->parent;
788 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
792 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
793 1e37a5c2 2019-05-12 jcs *dead = view;
799 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
800 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
802 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
803 669b5ffa 2018-10-07 stsp *focus = view->child;
804 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
805 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
807 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
810 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
811 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
813 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
814 1e37a5c2 2019-05-12 jcs *focus = view;
815 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
816 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
818 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
822 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
823 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
826 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
829 60493ae3 2019-06-20 stsp if (view->search_start)
830 2b49a8ae 2019-06-22 stsp view_search_start(view);
832 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
836 60493ae3 2019-06-20 stsp if (view->search_next && view->searching) {
837 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
838 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
839 60493ae3 2019-06-20 stsp view->search_next_done = 0;
840 60493ae3 2019-06-20 stsp view->search_next(view);
842 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
845 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
849 e5a0f69f 2018-08-18 stsp return err;
853 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
855 0cf4efb1 2018-09-29 stsp PANEL *panel;
856 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
858 669b5ffa 2018-10-07 stsp if (view->parent)
859 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
861 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
862 0cf4efb1 2018-09-29 stsp if (panel == NULL)
865 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
866 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
867 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
871 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
873 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
874 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
876 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
878 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
881 669b5ffa 2018-10-07 stsp return view->focussed;
884 bcbd79e2 2018-08-19 stsp static const struct got_error *
885 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
887 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
888 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
889 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
890 fd823528 2018-10-22 stsp int fast_refresh = 10;
891 1a76625f 2018-10-22 stsp int done = 0, errcode;
893 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
894 1a76625f 2018-10-22 stsp if (errcode)
895 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
897 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
898 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
900 a81bf10d 2018-09-29 stsp main_view = view;
901 1004088d 2018-09-29 stsp view->focussed = 1;
902 878940b7 2018-09-29 stsp err = view->show(view);
904 0cf4efb1 2018-09-29 stsp return err;
905 0cf4efb1 2018-09-29 stsp update_panels();
906 0cf4efb1 2018-09-29 stsp doupdate();
907 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
908 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
909 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
910 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
912 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
913 e4197bf9 2018-08-18 stsp view, &views);
916 e5a0f69f 2018-08-18 stsp if (dead_view) {
917 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
919 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
920 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
921 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
922 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
923 669b5ffa 2018-10-07 stsp prev = view->parent;
925 669b5ffa 2018-10-07 stsp if (dead_view->parent)
926 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
928 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
930 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
931 d01904d4 2019-06-25 stsp if (err || (dead_view == main_view && new_view == NULL))
934 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
935 0cf4efb1 2018-09-29 stsp if (focus_view)
936 0cf4efb1 2018-09-29 stsp view = focus_view;
937 669b5ffa 2018-10-07 stsp else if (prev)
938 669b5ffa 2018-10-07 stsp view = prev;
939 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
940 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
941 0cf4efb1 2018-09-29 stsp tog_view_list_head);
943 0cf4efb1 2018-09-29 stsp view = NULL;
944 669b5ffa 2018-10-07 stsp if (view) {
945 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
946 669b5ffa 2018-10-07 stsp focus_view = view->child;
948 669b5ffa 2018-10-07 stsp focus_view = view;
952 bcbd79e2 2018-08-19 stsp if (new_view) {
953 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
954 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
955 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
956 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
958 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
959 86c66b02 2018-10-18 stsp err = view_close(v);
964 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
965 fed7eaa8 2018-10-24 stsp view = new_view;
966 878940b7 2018-09-29 stsp if (focus_view == NULL)
967 878940b7 2018-09-29 stsp focus_view = new_view;
969 0cf4efb1 2018-09-29 stsp if (focus_view) {
970 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
972 0cf4efb1 2018-09-29 stsp view->focussed = 0;
973 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
974 0cf4efb1 2018-09-29 stsp view = focus_view;
975 878940b7 2018-09-29 stsp if (new_view)
976 878940b7 2018-09-29 stsp show_panel(new_view->panel);
977 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
978 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
980 669b5ffa 2018-10-07 stsp if (view) {
981 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
982 758194b5 2018-10-24 stsp view->focussed = 1;
983 758194b5 2018-10-24 stsp show_panel(view->panel);
984 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
985 758194b5 2018-10-24 stsp show_panel(view->child->panel);
986 1a76625f 2018-10-22 stsp focus_view = view;
988 669b5ffa 2018-10-07 stsp if (view->parent) {
989 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
993 669b5ffa 2018-10-07 stsp err = view->show(view);
996 669b5ffa 2018-10-07 stsp if (view->child) {
997 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
1001 1a76625f 2018-10-22 stsp update_panels();
1002 1a76625f 2018-10-22 stsp doupdate();
1006 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
1007 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
1008 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
1009 e5a0f69f 2018-08-18 stsp view_close(view);
1012 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1013 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
1014 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1016 e5a0f69f 2018-08-18 stsp return err;
1019 4ed7e80c 2018-05-20 stsp __dead static void
1020 9f7d7167 2018-04-29 stsp usage_log(void)
1023 c70c5802 2018-08-01 stsp fprintf(stderr,
1024 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
1025 9f7d7167 2018-04-29 stsp getprogname());
1029 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
1030 80ddbec8 2018-04-29 stsp static const struct got_error *
1031 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1033 00dfcb92 2018-06-11 stsp char *vis = NULL;
1034 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1036 963b370f 2018-05-20 stsp *ws = NULL;
1037 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
1038 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
1039 00dfcb92 2018-06-11 stsp int vislen;
1040 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
1041 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
1043 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
1044 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
1046 00dfcb92 2018-06-11 stsp return err;
1047 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
1048 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
1049 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
1050 a7f50699 2018-06-11 stsp goto done;
1054 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
1055 a7f50699 2018-06-11 stsp if (*ws == NULL) {
1056 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1057 a7f50699 2018-06-11 stsp goto done;
1060 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1061 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
1063 00dfcb92 2018-06-11 stsp free(vis);
1064 963b370f 2018-05-20 stsp if (err) {
1065 963b370f 2018-05-20 stsp free(*ws);
1066 963b370f 2018-05-20 stsp *ws = NULL;
1067 963b370f 2018-05-20 stsp *wlen = 0;
1069 963b370f 2018-05-20 stsp return err;
1072 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
1073 963b370f 2018-05-20 stsp static const struct got_error *
1074 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1075 27a741e5 2019-09-11 stsp int col_tab_align)
1077 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
1078 963b370f 2018-05-20 stsp int cols = 0;
1079 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
1080 963b370f 2018-05-20 stsp size_t wlen;
1083 963b370f 2018-05-20 stsp *wlinep = NULL;
1084 b700b5d6 2018-07-10 stsp *widthp = 0;
1086 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
1088 963b370f 2018-05-20 stsp return err;
1091 27a741e5 2019-09-11 stsp while (i < wlen) {
1092 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
1094 27a741e5 2019-09-11 stsp if (width == 0) {
1099 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
1100 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1102 27a741e5 2019-09-11 stsp cols += width;
1104 27a741e5 2019-09-11 stsp } else if (width == -1) {
1105 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
1106 27a741e5 2019-09-11 stsp width = TABSIZE -
1107 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
1108 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
1110 27a741e5 2019-09-11 stsp cols += width;
1114 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
1115 963b370f 2018-05-20 stsp goto done;
1118 963b370f 2018-05-20 stsp wline[i] = L'\0';
1119 b700b5d6 2018-07-10 stsp if (widthp)
1120 b700b5d6 2018-07-10 stsp *widthp = cols;
1123 963b370f 2018-05-20 stsp free(wline);
1125 963b370f 2018-05-20 stsp *wlinep = wline;
1126 963b370f 2018-05-20 stsp return err;
1129 8b473291 2019-02-21 stsp static const struct got_error*
1130 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
1131 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1133 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1134 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1136 8b473291 2019-02-21 stsp const char *name;
1138 8b473291 2019-02-21 stsp *refs_str = NULL;
1140 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1141 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1144 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1145 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1147 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1148 8b473291 2019-02-21 stsp name += 5;
1149 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1151 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1152 8b473291 2019-02-21 stsp name += 6;
1153 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
1154 8b473291 2019-02-21 stsp name += 8;
1155 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1156 52b5abe1 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1157 5d844a1e 2019-08-13 stsp if (err) {
1158 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1160 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1161 5d844a1e 2019-08-13 stsp err = NULL;
1162 5d844a1e 2019-08-13 stsp tag = NULL;
1165 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1166 52b5abe1 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1168 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1169 52b5abe1 2019-08-13 stsp if (cmp != 0)
1171 8b473291 2019-02-21 stsp s = *refs_str;
1172 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1173 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1174 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1176 8b473291 2019-02-21 stsp *refs_str = NULL;
1182 8b473291 2019-02-21 stsp return err;
1185 963b370f 2018-05-20 stsp static const struct got_error *
1186 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1187 27a741e5 2019-09-11 stsp int col_tab_align)
1189 5813d178 2019-03-09 stsp char *smallerthan, *at;
1191 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1192 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1193 5813d178 2019-03-09 stsp author = smallerthan + 1;
1194 5813d178 2019-03-09 stsp at = strchr(author, '@');
1196 5813d178 2019-03-09 stsp *at = '\0';
1197 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1200 5813d178 2019-03-09 stsp static const struct got_error *
1201 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1202 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
1203 11b20872 2019-11-08 stsp const size_t date_display_cols, int author_display_cols,
1204 11b20872 2019-11-08 stsp struct tog_colors *colors)
1206 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1207 6db9f7f6 2019-12-10 stsp char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1208 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1209 5813d178 2019-03-09 stsp char *author = NULL;
1210 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1211 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1212 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1213 bb737323 2018-05-20 stsp int col, limit;
1214 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1215 ccb26ccd 2018-11-05 stsp struct tm tm;
1216 45d799e2 2018-12-23 stsp time_t committer_time;
1217 11b20872 2019-11-08 stsp struct tog_color *tc;
1219 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1220 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1221 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1222 6db9f7f6 2019-12-10 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1223 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1224 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1226 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1227 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1229 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1230 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_DATE);
1232 11b20872 2019-11-08 stsp wattr_on(view->window,
1233 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1234 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1236 11b20872 2019-11-08 stsp wattr_off(view->window,
1237 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1238 27a741e5 2019-09-11 stsp col = limit;
1239 b39d25c7 2018-07-10 stsp if (col > avail)
1240 b39d25c7 2018-07-10 stsp goto done;
1242 6570a66d 2019-11-08 stsp if (avail >= 120) {
1243 6570a66d 2019-11-08 stsp char *id_str;
1244 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1246 6570a66d 2019-11-08 stsp goto done;
1247 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1249 11b20872 2019-11-08 stsp wattr_on(view->window,
1250 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1251 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1253 11b20872 2019-11-08 stsp wattr_off(view->window,
1254 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1255 6570a66d 2019-11-08 stsp free(id_str);
1257 6570a66d 2019-11-08 stsp if (col > avail)
1258 6570a66d 2019-11-08 stsp goto done;
1261 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1262 5813d178 2019-03-09 stsp if (author == NULL) {
1263 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1264 80ddbec8 2018-04-29 stsp goto done;
1266 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1268 bb737323 2018-05-20 stsp goto done;
1269 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_AUTHOR);
1271 11b20872 2019-11-08 stsp wattr_on(view->window,
1272 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1273 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1275 11b20872 2019-11-08 stsp wattr_off(view->window,
1276 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1277 bb737323 2018-05-20 stsp col += author_width;
1278 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1279 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1281 bb737323 2018-05-20 stsp author_width++;
1283 9c2eaf34 2018-05-20 stsp if (col > avail)
1284 9c2eaf34 2018-05-20 stsp goto done;
1286 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1288 6d9fbc00 2018-04-29 stsp goto done;
1289 bb737323 2018-05-20 stsp logmsg = logmsg0;
1290 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1292 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1293 bb737323 2018-05-20 stsp if (newline)
1294 bb737323 2018-05-20 stsp *newline = '\0';
1295 bb737323 2018-05-20 stsp limit = avail - col;
1296 dee2c213 2019-11-13 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1298 bb737323 2018-05-20 stsp goto done;
1299 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1300 bb737323 2018-05-20 stsp col += logmsg_width;
1301 27a741e5 2019-09-11 stsp while (col < avail) {
1302 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1306 80ddbec8 2018-04-29 stsp free(logmsg0);
1307 bb737323 2018-05-20 stsp free(wlogmsg);
1308 5813d178 2019-03-09 stsp free(author);
1309 bb737323 2018-05-20 stsp free(wauthor);
1310 80ddbec8 2018-04-29 stsp free(line);
1311 80ddbec8 2018-04-29 stsp return err;
1314 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1315 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1316 899d86c2 2018-05-10 stsp struct got_object_id *id)
1318 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1320 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1321 80ddbec8 2018-04-29 stsp if (entry == NULL)
1322 899d86c2 2018-05-10 stsp return NULL;
1324 899d86c2 2018-05-10 stsp entry->id = id;
1325 99db9666 2018-05-07 stsp entry->commit = commit;
1326 899d86c2 2018-05-10 stsp return entry;
1329 99db9666 2018-05-07 stsp static void
1330 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1332 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1334 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1335 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1336 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1337 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1338 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1339 99db9666 2018-05-07 stsp free(entry);
1342 99db9666 2018-05-07 stsp static void
1343 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1345 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1346 99db9666 2018-05-07 stsp pop_commit(commits);
1349 c4972b91 2018-05-07 stsp static const struct got_error *
1350 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1351 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1353 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1354 13add988 2019-10-15 stsp regmatch_t regmatch;
1355 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1357 13add988 2019-10-15 stsp *have_match = 0;
1359 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1361 13add988 2019-10-15 stsp return err;
1363 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1365 13add988 2019-10-15 stsp goto done;
1367 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1368 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1369 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1370 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1371 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1372 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1373 13add988 2019-10-15 stsp *have_match = 1;
1375 13add988 2019-10-15 stsp free(id_str);
1376 13add988 2019-10-15 stsp free(logmsg);
1377 13add988 2019-10-15 stsp return err;
1380 13add988 2019-10-15 stsp static const struct got_error *
1381 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1382 13add988 2019-10-15 stsp int minqueue, struct got_repository *repo, const char *path,
1383 13add988 2019-10-15 stsp int *searching, int *search_next_done, regex_t *regex)
1385 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1386 13add988 2019-10-15 stsp int nqueued = 0, have_match = 0;
1389 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1390 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1391 1a76625f 2018-10-22 stsp * while updating the display.
1393 13add988 2019-10-15 stsp while (nqueued < minqueue ||
1394 13add988 2019-10-15 stsp (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1395 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1396 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1397 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1398 1a76625f 2018-10-22 stsp int errcode;
1400 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1401 ee780d5c 2020-01-04 stsp if (err || id == NULL)
1404 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1407 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1408 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1409 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1413 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1414 1a76625f 2018-10-22 stsp if (errcode) {
1415 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1416 13add988 2019-10-15 stsp "pthread_mutex_lock");
1420 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1421 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1422 ecb28ae0 2018-07-16 stsp nqueued++;
1423 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1425 13add988 2019-10-15 stsp if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1426 13add988 2019-10-15 stsp err = match_commit(&have_match, id, commit, regex);
1427 13add988 2019-10-15 stsp if (err) {
1428 13add988 2019-10-15 stsp pthread_mutex_lock(&tog_mutex);
1433 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1434 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1435 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1436 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1438 13add988 2019-10-15 stsp if (have_match)
1442 9ba79e04 2018-06-11 stsp return err;
1445 99db9666 2018-05-07 stsp static const struct got_error *
1446 19e70ad6 2019-05-14 stsp get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1447 19e70ad6 2019-05-14 stsp struct got_repository *repo)
1449 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1450 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1452 9ba79e04 2018-06-11 stsp *head_id = NULL;
1454 19e70ad6 2019-05-14 stsp err = got_ref_open(&head_ref, repo, branch_name, 0);
1456 99db9666 2018-05-07 stsp return err;
1458 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1459 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1460 9ba79e04 2018-06-11 stsp if (err) {
1461 9ba79e04 2018-06-11 stsp *head_id = NULL;
1462 99db9666 2018-05-07 stsp return err;
1465 9ba79e04 2018-06-11 stsp return NULL;
1468 0553a4e3 2018-04-30 stsp static const struct got_error *
1469 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1470 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1471 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1472 11b20872 2019-11-08 stsp struct got_reflist_head *refs, const char *path, int commits_needed,
1473 11b20872 2019-11-08 stsp struct tog_colors *colors)
1475 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1476 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1477 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1478 60493ae3 2019-06-20 stsp int width;
1479 52e88aae 2019-11-08 stsp int ncommits, author_cols = 4;
1480 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1481 8b473291 2019-02-21 stsp char *refs_str = NULL;
1482 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1483 11b20872 2019-11-08 stsp struct tog_color *tc;
1484 6db9f7f6 2019-12-10 stsp static const size_t date_display_cols = 12;
1486 e0d42f60 2018-07-22 stsp entry = first;
1487 e0d42f60 2018-07-22 stsp ncommits = 0;
1488 e0d42f60 2018-07-22 stsp while (entry) {
1489 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1490 e0d42f60 2018-07-22 stsp *selected = entry;
1493 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1494 e0d42f60 2018-07-22 stsp ncommits++;
1497 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1498 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1500 ecb28ae0 2018-07-16 stsp return err;
1501 8b473291 2019-02-21 stsp if (refs) {
1502 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, (*selected)->id,
1505 8b473291 2019-02-21 stsp goto done;
1509 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1510 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1512 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1513 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1514 60493ae3 2019-06-20 stsp commits_needed > 0 ?
1515 60493ae3 2019-06-20 stsp (view->searching && view->search_next_done == 0
1516 60493ae3 2019-06-20 stsp ? "searching..." : "loading... ") :
1517 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1518 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1519 8b473291 2019-02-21 stsp goto done;
1522 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1523 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1524 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1525 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1526 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1527 1a76625f 2018-10-22 stsp header = NULL;
1528 1a76625f 2018-10-22 stsp goto done;
1530 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1531 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1532 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1533 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1534 1a76625f 2018-10-22 stsp header = NULL;
1535 1a76625f 2018-10-22 stsp goto done;
1537 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1539 1a76625f 2018-10-22 stsp goto done;
1541 2814baeb 2018-08-01 stsp werase(view->window);
1543 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1544 a3404814 2018-09-02 stsp wstandout(view->window);
1545 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
1547 11b20872 2019-11-08 stsp wattr_on(view->window,
1548 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1549 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1551 11b20872 2019-11-08 stsp wattr_off(view->window,
1552 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
1553 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1554 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1557 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1558 a3404814 2018-09-02 stsp wstandend(view->window);
1559 ecb28ae0 2018-07-16 stsp free(wline);
1560 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1561 1a76625f 2018-10-22 stsp goto done;
1563 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1564 899d86c2 2018-05-10 stsp entry = first;
1565 5813d178 2019-03-09 stsp ncommits = 0;
1566 5813d178 2019-03-09 stsp while (entry) {
1567 5813d178 2019-03-09 stsp char *author;
1568 5813d178 2019-03-09 stsp wchar_t *wauthor;
1569 5813d178 2019-03-09 stsp int width;
1570 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1572 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1573 5813d178 2019-03-09 stsp if (author == NULL) {
1574 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1575 5813d178 2019-03-09 stsp goto done;
1577 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1578 27a741e5 2019-09-11 stsp date_display_cols);
1579 5813d178 2019-03-09 stsp if (author_cols < width)
1580 5813d178 2019-03-09 stsp author_cols = width;
1581 5813d178 2019-03-09 stsp free(wauthor);
1582 5813d178 2019-03-09 stsp free(author);
1583 7ca04879 2019-10-19 stsp ncommits++;
1584 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1587 5813d178 2019-03-09 stsp entry = first;
1588 899d86c2 2018-05-10 stsp *last = first;
1589 867c6645 2018-07-10 stsp ncommits = 0;
1590 899d86c2 2018-05-10 stsp while (entry) {
1591 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1593 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1594 2814baeb 2018-08-01 stsp wstandout(view->window);
1595 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1596 11b20872 2019-11-08 stsp date_display_cols, author_cols, colors);
1597 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1598 2814baeb 2018-08-01 stsp wstandend(view->window);
1600 60493ae3 2019-06-20 stsp goto done;
1601 0553a4e3 2018-04-30 stsp ncommits++;
1602 899d86c2 2018-05-10 stsp *last = entry;
1603 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1606 1a57306a 2018-09-02 stsp view_vborder(view);
1608 1a76625f 2018-10-22 stsp free(id_str);
1609 8b473291 2019-02-21 stsp free(refs_str);
1610 1a76625f 2018-10-22 stsp free(ncommits_str);
1611 1a76625f 2018-10-22 stsp free(header);
1612 80ddbec8 2018-04-29 stsp return err;
1615 07b55e75 2018-05-10 stsp static void
1616 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1617 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1618 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1620 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1621 07b55e75 2018-05-10 stsp int nscrolled = 0;
1623 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1624 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1627 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1628 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1629 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1630 07b55e75 2018-05-10 stsp if (entry) {
1631 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1632 07b55e75 2018-05-10 stsp nscrolled++;
1637 aa075928 2018-05-10 stsp static const struct got_error *
1638 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1639 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1641 5e224a3e 2019-02-22 stsp int errcode;
1642 8a42fca8 2019-02-22 stsp int max_wait = 20;
1644 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1646 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1647 5e224a3e 2019-02-22 stsp if (*log_complete)
1650 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1651 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1652 7aafa0d1 2019-02-22 stsp if (errcode)
1653 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1654 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1655 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1656 7aafa0d1 2019-02-22 stsp if (errcode)
1657 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1658 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1659 7aafa0d1 2019-02-22 stsp pthread_yield();
1660 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
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_mutex_lock");
1665 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1667 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1668 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1669 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1670 5e224a3e 2019-02-22 stsp * with few commits in history.
1672 7aafa0d1 2019-02-22 stsp return NULL;
1676 5e224a3e 2019-02-22 stsp return NULL;
1679 5e224a3e 2019-02-22 stsp static const struct got_error *
1680 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1681 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1682 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1683 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1684 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1686 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1687 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1688 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1690 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1691 5e224a3e 2019-02-22 stsp return NULL;
1693 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1694 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1696 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1697 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1699 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1700 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1701 5e224a3e 2019-02-22 stsp need_commits);
1703 5e224a3e 2019-02-22 stsp return err;
1707 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1708 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1711 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1713 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1714 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1716 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1717 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1719 dd0a52c1 2018-05-20 stsp return err;
1722 cd0acaa7 2018-05-20 stsp static const struct got_error *
1723 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1724 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1725 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1726 8b473291 2019-02-21 stsp struct got_repository *repo)
1728 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1729 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1730 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1732 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1733 15a94983 2018-12-23 stsp if (diff_view == NULL)
1734 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1736 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1737 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1738 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1739 e5a0f69f 2018-08-18 stsp if (err == NULL)
1740 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1741 cd0acaa7 2018-05-20 stsp return err;
1744 80ddbec8 2018-04-29 stsp static const struct got_error *
1745 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1746 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1748 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1750 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1751 941e9f74 2019-05-21 stsp if (parent == NULL)
1752 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1754 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1755 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1756 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1757 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1758 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1759 941e9f74 2019-05-21 stsp s->tree = subtree;
1760 941e9f74 2019-05-21 stsp s->selected = 0;
1761 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1762 941e9f74 2019-05-21 stsp return NULL;
1766 941e9f74 2019-05-21 stsp static const struct got_error *
1767 941e9f74 2019-05-21 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1768 941e9f74 2019-05-21 stsp struct commit_queue_entry *entry, const char *path,
1769 941e9f74 2019-05-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
1771 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1772 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1773 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s;
1774 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1775 b03c880f 2019-05-21 stsp char *slash, *subpath = NULL;
1776 941e9f74 2019-05-21 stsp const char *p;
1778 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1779 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1781 9343a5fb 2018-06-23 stsp return err;
1783 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1784 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1785 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1787 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1788 941e9f74 2019-05-21 stsp if (err) {
1789 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1790 941e9f74 2019-05-21 stsp return err;
1792 941e9f74 2019-05-21 stsp s = &tree_view->state.tree;
1794 941e9f74 2019-05-21 stsp *new_view = tree_view;
1796 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1798 8d0fe45a 2019-08-12 stsp while (p[0] == '/')
1800 941e9f74 2019-05-21 stsp while (*p) {
1801 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
1802 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1803 56e0773d 2019-11-28 stsp char *te_name;
1805 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1806 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1807 941e9f74 2019-05-21 stsp if (slash == NULL)
1808 941e9f74 2019-05-21 stsp slash = strchr(p, '\0');
1810 56e0773d 2019-11-28 stsp te_name = strndup(p, slash -p);
1811 56e0773d 2019-11-28 stsp if (te_name == NULL) {
1812 56e0773d 2019-11-28 stsp err = got_error_from_errno("strndup");
1815 56e0773d 2019-11-28 stsp te = got_object_tree_find_entry(s->tree, te_name);
1816 56e0773d 2019-11-28 stsp if (te == NULL) {
1817 56e0773d 2019-11-28 stsp err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1818 56e0773d 2019-11-28 stsp free(te_name);
1821 56e0773d 2019-11-28 stsp free(te_name);
1822 56e0773d 2019-11-28 stsp s->selected_entry = te;
1823 56e0773d 2019-11-28 stsp s->selected = got_tree_entry_get_index(te);
1824 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1825 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1827 56e0773d 2019-11-28 stsp if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1828 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1829 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1830 67409a31 2019-05-24 stsp s->selected = 0;
1834 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1835 941e9f74 2019-05-21 stsp if (slash)
1836 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1838 941e9f74 2019-05-21 stsp subpath = strdup(path);
1839 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1840 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1844 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1849 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1850 941e9f74 2019-05-21 stsp free(tree_id);
1854 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1855 941e9f74 2019-05-21 stsp if (err) {
1856 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1859 941e9f74 2019-05-21 stsp if (slash == NULL)
1861 941e9f74 2019-05-21 stsp free(subpath);
1862 941e9f74 2019-05-21 stsp subpath = NULL;
1863 941e9f74 2019-05-21 stsp p = slash;
1866 941e9f74 2019-05-21 stsp free(subpath);
1867 1a76625f 2018-10-22 stsp return err;
1870 1a76625f 2018-10-22 stsp static void *
1871 1a76625f 2018-10-22 stsp log_thread(void *arg)
1873 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1874 1a76625f 2018-10-22 stsp int errcode = 0;
1875 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1876 1a76625f 2018-10-22 stsp int done = 0;
1878 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1879 6fb7cd11 2019-08-22 stsp NULL, NULL);
1881 1a76625f 2018-10-22 stsp return (void *)err;
1883 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1884 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1885 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1886 13add988 2019-10-15 stsp a->regex);
1887 1a76625f 2018-10-22 stsp if (err) {
1888 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1889 1a76625f 2018-10-22 stsp return (void *)err;
1890 1a76625f 2018-10-22 stsp err = NULL;
1892 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1893 1a76625f 2018-10-22 stsp a->commits_needed--;
1895 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1896 3abe8080 2019-04-10 stsp if (errcode) {
1897 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1898 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1900 3abe8080 2019-04-10 stsp } else if (*a->quit)
1902 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1903 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1904 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1905 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1909 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1910 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1911 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1912 1a76625f 2018-10-22 stsp &tog_mutex);
1913 1a76625f 2018-10-22 stsp if (errcode)
1914 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1915 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1918 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1919 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1920 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1921 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1923 3abe8080 2019-04-10 stsp a->log_complete = 1;
1924 1a76625f 2018-10-22 stsp return (void *)err;
1927 1a76625f 2018-10-22 stsp static const struct got_error *
1928 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1930 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1931 1a76625f 2018-10-22 stsp int errcode;
1933 1a76625f 2018-10-22 stsp if (s->thread) {
1934 1a76625f 2018-10-22 stsp s->quit = 1;
1935 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1936 1a76625f 2018-10-22 stsp if (errcode)
1937 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1938 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1939 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1940 1a76625f 2018-10-22 stsp if (errcode)
1941 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1942 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1943 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1944 1a76625f 2018-10-22 stsp if (errcode)
1945 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1946 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1947 1a76625f 2018-10-22 stsp if (errcode)
1948 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1949 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1950 1a76625f 2018-10-22 stsp s->thread = NULL;
1953 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1954 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1955 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1957 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1958 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1959 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1962 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1963 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1964 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1967 9343a5fb 2018-06-23 stsp return err;
1970 9343a5fb 2018-06-23 stsp static const struct got_error *
1971 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1973 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1974 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1976 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1977 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1978 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1979 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1980 1a76625f 2018-10-22 stsp free(s->start_id);
1981 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1982 1a76625f 2018-10-22 stsp return err;
1985 1a76625f 2018-10-22 stsp static const struct got_error *
1986 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
1988 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1990 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
1991 96e2b566 2019-07-08 stsp s->search_entry = NULL;
1992 60493ae3 2019-06-20 stsp return NULL;
1995 60493ae3 2019-06-20 stsp static const struct got_error *
1996 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
1998 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
1999 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
2000 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
2002 60493ae3 2019-06-20 stsp if (!view->searching) {
2003 60493ae3 2019-06-20 stsp view->search_next_done = 1;
2004 60493ae3 2019-06-20 stsp return NULL;
2007 96e2b566 2019-07-08 stsp if (s->search_entry) {
2008 13add988 2019-10-15 stsp int errcode, ch;
2009 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2010 13add988 2019-10-15 stsp if (errcode)
2011 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2012 13add988 2019-10-15 stsp "pthread_mutex_unlock");
2013 13add988 2019-10-15 stsp ch = wgetch(view->window);
2014 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
2015 13add988 2019-10-15 stsp if (errcode)
2016 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
2017 13add988 2019-10-15 stsp "pthread_mutex_lock");
2018 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
2019 678cbce5 2019-07-28 stsp view->search_next_done = 1;
2020 678cbce5 2019-07-28 stsp return NULL;
2022 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
2023 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
2025 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
2026 96e2b566 2019-07-08 stsp commit_queue_head, entry);
2027 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
2028 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2029 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2031 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
2032 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
2034 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2035 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
2037 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2040 60493ae3 2019-06-20 stsp while (1) {
2041 13add988 2019-10-15 stsp int have_match = 0;
2043 60493ae3 2019-06-20 stsp if (entry == NULL) {
2044 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
2045 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
2046 f801134a 2019-06-25 stsp view->search_next_done = 1;
2047 f801134a 2019-06-25 stsp return NULL;
2050 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
2051 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
2052 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
2054 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
2055 57b33b64 2019-07-08 stsp return trigger_log_thread(1,
2056 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
2057 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
2058 f801134a 2019-06-25 stsp &s->thread_args.need_commits);
2061 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
2062 13add988 2019-10-15 stsp &view->regex);
2065 13add988 2019-10-15 stsp if (have_match) {
2066 60493ae3 2019-06-20 stsp view->search_next_done = 1;
2067 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
2071 96e2b566 2019-07-08 stsp s->search_entry = entry;
2072 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
2073 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
2075 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
2078 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
2079 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
2080 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
2081 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2083 60493ae3 2019-06-20 stsp return err;
2086 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
2087 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2089 60493ae3 2019-06-20 stsp return err;
2094 96e2b566 2019-07-08 stsp s->search_entry = NULL;
2096 60493ae3 2019-06-20 stsp return NULL;
2099 60493ae3 2019-06-20 stsp static const struct got_error *
2100 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
2101 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
2102 d01904d4 2019-06-25 stsp const char *head_ref_name, const char *path, int check_disk)
2104 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
2105 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2106 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
2107 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
2108 1a76625f 2018-10-22 stsp int errcode;
2110 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2111 ecb28ae0 2018-07-16 stsp if (err != NULL)
2112 ecb28ae0 2018-07-16 stsp goto done;
2114 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
2115 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
2116 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
2118 8b473291 2019-02-21 stsp s->refs = refs;
2119 fb2756b9 2018-08-04 stsp s->repo = repo;
2120 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
2121 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
2122 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
2123 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2124 5036bf37 2018-09-24 stsp goto done;
2127 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
2128 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2129 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2130 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
2132 11b20872 2019-11-08 stsp goto done;
2133 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2134 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2135 11b20872 2019-11-08 stsp if (err) {
2136 11b20872 2019-11-08 stsp free_colors(&s->colors);
2137 11b20872 2019-11-08 stsp goto done;
2139 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2140 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2141 11b20872 2019-11-08 stsp if (err) {
2142 11b20872 2019-11-08 stsp free_colors(&s->colors);
2143 11b20872 2019-11-08 stsp goto done;
2147 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
2148 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
2149 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
2150 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
2151 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
2153 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2155 1a76625f 2018-10-22 stsp goto done;
2156 3d509237 2020-01-04 stsp err = got_commit_graph_open(&thread_graph, s->in_repo_path, 0);
2158 1a76625f 2018-10-22 stsp goto done;
2160 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2161 1a76625f 2018-10-22 stsp if (errcode) {
2162 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
2163 1a76625f 2018-10-22 stsp goto done;
2166 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
2167 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
2168 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
2169 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
2170 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
2171 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
2172 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
2173 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
2174 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2175 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
2176 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
2177 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2178 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2181 1a76625f 2018-10-22 stsp close_log_view(view);
2182 ba4f502b 2018-08-04 stsp return err;
2185 e5a0f69f 2018-08-18 stsp static const struct got_error *
2186 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2188 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2190 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2191 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2192 2b380cc8 2018-10-24 stsp &s->thread_args);
2193 2b380cc8 2018-10-24 stsp if (errcode)
2194 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2197 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
2198 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
2199 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
2200 11b20872 2019-11-08 stsp s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2203 e5a0f69f 2018-08-18 stsp static const struct got_error *
2204 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2205 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2207 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2208 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2209 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2210 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2211 669b5ffa 2018-10-07 stsp int begin_x = 0;
2212 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2214 e5a0f69f 2018-08-18 stsp switch (ch) {
2216 1e37a5c2 2019-05-12 jcs s->quit = 1;
2219 1e37a5c2 2019-05-12 jcs case KEY_UP:
2222 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2224 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2225 1e37a5c2 2019-05-12 jcs s->selected--;
2227 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
2228 1144d21a 2019-06-21 stsp &s->commits);
2230 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2231 a4292ac5 2019-05-12 jcs case CTRL('b'):
2232 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2234 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
2235 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
2236 1e37a5c2 2019-05-12 jcs s->selected = 0;
2239 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
2240 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
2243 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2246 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2248 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2249 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2250 1e37a5c2 2019-05-12 jcs s->selected++;
2253 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
2254 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
2255 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
2256 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2257 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2259 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2260 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2261 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2262 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2263 1e37a5c2 2019-05-12 jcs if (first == NULL)
2265 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
2266 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
2267 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
2268 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2269 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2272 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2273 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2274 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2275 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2276 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2277 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2279 1e37a5c2 2019-05-12 jcs err = NULL;
2282 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2283 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2284 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2285 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2286 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2288 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2291 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2293 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2294 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2295 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2296 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2297 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
2300 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2301 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2303 1e37a5c2 2019-05-12 jcs return err;
2304 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2306 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2309 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2310 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2312 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2315 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2317 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2318 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2319 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2320 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2323 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2324 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2326 1e37a5c2 2019-05-12 jcs return err;
2327 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2329 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2332 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2333 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2335 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2337 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2338 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2340 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2341 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2342 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2344 1e37a5c2 2019-05-12 jcs return err;
2345 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2346 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2347 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2348 638f9024 2019-05-13 stsp return got_error_from_errno(
2349 1e37a5c2 2019-05-12 jcs "view_open");
2350 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2351 d01904d4 2019-06-25 stsp s->repo, s->head_ref_name, parent_path, 0);
2353 1e37a5c2 2019-05-12 jcs return err;;
2354 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2355 1e37a5c2 2019-05-12 jcs *new_view = lv;
2357 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2358 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2360 1e37a5c2 2019-05-12 jcs return NULL;
2363 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2364 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2366 d01904d4 2019-06-25 stsp return err;
2367 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2368 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2369 d01904d4 2019-06-25 stsp if (lv == NULL)
2370 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2371 d01904d4 2019-06-25 stsp err = get_head_commit_id(&start_id, s->head_ref_name ?
2372 d01904d4 2019-06-25 stsp s->head_ref_name : GOT_REF_HEAD, s->repo);
2373 ef129c5e 2019-08-03 stsp if (err) {
2374 ef129c5e 2019-08-03 stsp view_close(lv);
2375 d01904d4 2019-06-25 stsp return err;
2377 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2378 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2379 e6b23735 2019-10-04 stsp free(start_id);
2380 e6b23735 2019-10-04 stsp view_close(lv);
2381 e6b23735 2019-10-04 stsp return got_error_from_errno("strdup");
2383 e6b23735 2019-10-04 stsp got_ref_list_free(s->refs);
2384 e6b23735 2019-10-04 stsp err = got_ref_list(s->refs, s->repo, NULL,
2385 e6b23735 2019-10-04 stsp got_ref_cmp_by_name, NULL);
2386 e6b23735 2019-10-04 stsp if (err) {
2387 d01904d4 2019-06-25 stsp free(start_id);
2388 ef129c5e 2019-08-03 stsp view_close(lv);
2389 50284065 2019-10-04 stsp return err;
2391 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2392 d01904d4 2019-06-25 stsp s->head_ref_name, in_repo_path, 0);
2393 ef129c5e 2019-08-03 stsp if (err) {
2394 ef129c5e 2019-08-03 stsp free(start_id);
2395 ef129c5e 2019-08-03 stsp view_close(lv);
2396 d01904d4 2019-06-25 stsp return err;;
2398 d01904d4 2019-06-25 stsp *dead_view = view;
2399 d01904d4 2019-06-25 stsp *new_view = lv;
2405 80ddbec8 2018-04-29 stsp return err;
2408 4ed7e80c 2018-05-20 stsp static const struct got_error *
2409 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2411 c2db6724 2019-01-04 stsp const struct got_error *error;
2413 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2414 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2415 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2417 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2418 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2420 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2421 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2423 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2424 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2426 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2427 c2db6724 2019-01-04 stsp if (error != NULL)
2428 c2db6724 2019-01-04 stsp return error;
2430 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2431 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2433 c2db6724 2019-01-04 stsp return NULL;
2436 a915003a 2019-02-05 stsp static void
2437 a915003a 2019-02-05 stsp init_curses(void)
2439 a915003a 2019-02-05 stsp initscr();
2441 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2444 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2445 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2446 a915003a 2019-02-05 stsp curs_set(0);
2447 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2448 6d17833f 2019-11-08 stsp start_color();
2449 6d17833f 2019-11-08 stsp use_default_colors();
2451 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2452 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2455 c2db6724 2019-01-04 stsp static const struct got_error *
2456 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2458 80ddbec8 2018-04-29 stsp const struct got_error *error;
2459 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2460 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2461 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2462 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2463 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2464 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2466 04cc582a 2018-08-01 stsp struct tog_view *view;
2468 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2470 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2471 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2472 c2db6724 2019-01-04 stsp NULL) == -1)
2473 80ddbec8 2018-04-29 stsp err(1, "pledge");
2476 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2477 80ddbec8 2018-04-29 stsp switch (ch) {
2479 80ddbec8 2018-04-29 stsp start_commit = optarg;
2482 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2483 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2484 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2488 17020d27 2019-03-07 stsp usage_log();
2489 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2493 80ddbec8 2018-04-29 stsp argc -= optind;
2494 80ddbec8 2018-04-29 stsp argv += optind;
2496 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2497 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2498 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2499 ecb28ae0 2018-07-16 stsp goto done;
2501 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2502 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2503 963f97a1 2019-03-18 stsp goto done;
2504 963f97a1 2019-03-18 stsp error = NULL;
2506 963f97a1 2019-03-18 stsp if (argc == 0) {
2507 963f97a1 2019-03-18 stsp path = strdup("");
2508 963f97a1 2019-03-18 stsp if (path == NULL) {
2509 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2510 ecb28ae0 2018-07-16 stsp goto done;
2512 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2513 963f97a1 2019-03-18 stsp if (worktree) {
2514 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2516 963f97a1 2019-03-18 stsp if (error)
2517 963f97a1 2019-03-18 stsp goto done;
2519 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2520 963f97a1 2019-03-18 stsp if (path == NULL) {
2521 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2522 963f97a1 2019-03-18 stsp goto done;
2526 963f97a1 2019-03-18 stsp usage_log();
2528 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2529 a1fbf39a 2019-08-11 stsp if (worktree)
2530 b9d7675a 2019-08-11 stsp repo_path = strdup(
2531 b9d7675a 2019-08-11 stsp got_worktree_get_repo_path(worktree));
2533 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2535 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2536 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2537 963f97a1 2019-03-18 stsp goto done;
2540 a915003a 2019-02-05 stsp init_curses();
2542 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2543 80ddbec8 2018-04-29 stsp if (error != NULL)
2544 ecb28ae0 2018-07-16 stsp goto done;
2546 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2547 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2548 c02c541e 2019-03-29 stsp if (error)
2549 c02c541e 2019-03-29 stsp goto done;
2551 15a94983 2018-12-23 stsp if (start_commit == NULL)
2552 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&start_id, worktree ?
2553 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2556 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&start_id, start_commit, repo);
2557 f2b6a97d 2019-07-15 stsp if (error) {
2558 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
2559 f2b6a97d 2019-07-15 stsp goto done;
2560 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&start_id,
2561 f2b6a97d 2019-07-15 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2564 80ddbec8 2018-04-29 stsp if (error != NULL)
2565 8b473291 2019-02-21 stsp goto done;
2567 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2568 8b473291 2019-02-21 stsp if (error)
2569 ecb28ae0 2018-07-16 stsp goto done;
2571 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2572 04cc582a 2018-08-01 stsp if (view == NULL) {
2573 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2574 04cc582a 2018-08-01 stsp goto done;
2576 2fc00ff4 2019-08-31 stsp if (worktree) {
2577 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2578 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2579 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2580 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2581 2fc00ff4 2019-08-31 stsp goto done;
2584 2fc00ff4 2019-08-31 stsp error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2586 ba4f502b 2018-08-04 stsp if (error)
2587 ba4f502b 2018-08-04 stsp goto done;
2588 2fc00ff4 2019-08-31 stsp if (worktree) {
2589 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2590 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2591 2fc00ff4 2019-08-31 stsp worktree = NULL;
2593 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2595 ecb28ae0 2018-07-16 stsp free(repo_path);
2596 ecb28ae0 2018-07-16 stsp free(cwd);
2597 ecb28ae0 2018-07-16 stsp free(path);
2598 899d86c2 2018-05-10 stsp free(start_id);
2599 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2601 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2602 ec142235 2019-03-07 stsp if (worktree)
2603 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2604 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2605 80ddbec8 2018-04-29 stsp return error;
2608 4ed7e80c 2018-05-20 stsp __dead static void
2609 9f7d7167 2018-04-29 stsp usage_diff(void)
2612 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2613 9f7d7167 2018-04-29 stsp getprogname());
2617 b304db33 2018-05-20 stsp static char *
2618 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2620 b304db33 2018-05-20 stsp char *line;
2621 b304db33 2018-05-20 stsp size_t linelen;
2622 b304db33 2018-05-20 stsp size_t lineno;
2623 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2625 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2627 b304db33 2018-05-20 stsp *len = linelen;
2628 b304db33 2018-05-20 stsp return line;
2631 6d17833f 2019-11-08 stsp static int
2632 6d17833f 2019-11-08 stsp match_line(const char *line, regex_t *regex)
2634 6d17833f 2019-11-08 stsp regmatch_t regmatch;
2636 6d17833f 2019-11-08 stsp return regexec(regex, line, 1, ®match, 0) == 0;
2639 f26dddb7 2019-11-08 stsp struct tog_color *
2640 bddb1296 2019-11-08 stsp match_color(struct tog_colors *colors, const char *line)
2642 f26dddb7 2019-11-08 stsp struct tog_color *tc = NULL;
2644 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2645 6d17833f 2019-11-08 stsp if (match_line(line, &tc->regex))
2646 6d17833f 2019-11-08 stsp return tc;
2649 6d17833f 2019-11-08 stsp return NULL;
2652 4ed7e80c 2018-05-20 stsp static const struct got_error *
2653 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2654 6d17833f 2019-11-08 stsp int *last_displayed_line, int *eof, int max_lines, char *header,
2655 f26dddb7 2019-11-08 stsp struct tog_colors *colors)
2657 61e69b96 2018-05-20 stsp const struct got_error *err;
2658 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2659 b304db33 2018-05-20 stsp char *line;
2660 f26dddb7 2019-11-08 stsp struct tog_color *tc;
2661 b304db33 2018-05-20 stsp size_t len;
2662 61e69b96 2018-05-20 stsp wchar_t *wline;
2663 e0b650dd 2018-05-20 stsp int width;
2665 26ed57b2 2018-05-19 stsp rewind(f);
2666 f7d12f7e 2018-08-01 stsp werase(view->window);
2668 a3404814 2018-09-02 stsp if (header) {
2669 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
2670 a3404814 2018-09-02 stsp if (err) {
2671 a3404814 2018-09-02 stsp return err;
2674 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2675 a3404814 2018-09-02 stsp wstandout(view->window);
2676 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2677 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2678 a3404814 2018-09-02 stsp wstandend(view->window);
2679 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2680 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2682 a3404814 2018-09-02 stsp if (max_lines <= 1)
2683 a3404814 2018-09-02 stsp return NULL;
2684 a3404814 2018-09-02 stsp max_lines--;
2688 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2689 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2690 26ed57b2 2018-05-19 stsp if (line == NULL) {
2694 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2695 26ed57b2 2018-05-19 stsp free(line);
2699 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2700 61e69b96 2018-05-20 stsp if (err) {
2701 61e69b96 2018-05-20 stsp free(line);
2702 61e69b96 2018-05-20 stsp return err;
2705 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
2707 f26dddb7 2019-11-08 stsp wattr_on(view->window,
2708 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2709 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2711 6d17833f 2019-11-08 stsp wattr_off(view->window,
2712 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
2713 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2714 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2715 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2716 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2717 26ed57b2 2018-05-19 stsp free(line);
2718 2550e4c3 2018-07-13 stsp free(wline);
2719 2550e4c3 2018-07-13 stsp wline = NULL;
2721 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2723 1a57306a 2018-09-02 stsp view_vborder(view);
2725 c3e9aa98 2019-05-13 jcs if (*eof) {
2726 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2727 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2728 c3e9aa98 2019-05-13 jcs nprinted++;
2731 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2733 c3e9aa98 2019-05-13 jcs return err;
2736 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2737 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2738 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2741 26ed57b2 2018-05-19 stsp return NULL;
2744 abd2672a 2018-12-23 stsp static char *
2745 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2747 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2748 09867e48 2019-08-13 stsp char *p, *s;
2750 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2751 09867e48 2019-08-13 stsp if (tm == NULL)
2752 09867e48 2019-08-13 stsp return NULL;
2753 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2754 09867e48 2019-08-13 stsp if (s == NULL)
2755 09867e48 2019-08-13 stsp return NULL;
2756 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2758 abd2672a 2018-12-23 stsp *p = '\0';
2762 4ed7e80c 2018-05-20 stsp static const struct got_error *
2763 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2764 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2766 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2767 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
2768 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2769 5943eee2 2019-08-13 stsp char *id_str = NULL, *logmsg = NULL;
2770 45d799e2 2018-12-23 stsp time_t committer_time;
2771 45d799e2 2018-12-23 stsp const char *author, *committer;
2772 8b473291 2019-02-21 stsp char *refs_str = NULL;
2774 8b473291 2019-02-21 stsp if (refs) {
2775 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
2777 8b473291 2019-02-21 stsp return err;
2780 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2782 abd2672a 2018-12-23 stsp return err;
2784 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2785 15a94983 2018-12-23 stsp if (err) {
2786 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2787 15a94983 2018-12-23 stsp goto done;
2790 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2791 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2792 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2793 abd2672a 2018-12-23 stsp goto done;
2795 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2796 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2797 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2798 abd2672a 2018-12-23 stsp goto done;
2800 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2801 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
2802 09867e48 2019-08-13 stsp if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2803 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2804 abd2672a 2018-12-23 stsp goto done;
2806 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2807 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2808 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2809 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2810 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2811 abd2672a 2018-12-23 stsp goto done;
2813 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2815 5943eee2 2019-08-13 stsp goto done;
2816 5943eee2 2019-08-13 stsp if (fprintf(outfile, "%s\n", logmsg) < 0) {
2817 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2818 abd2672a 2018-12-23 stsp goto done;
2821 abd2672a 2018-12-23 stsp free(id_str);
2822 5943eee2 2019-08-13 stsp free(logmsg);
2823 8b473291 2019-02-21 stsp free(refs_str);
2824 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2825 abd2672a 2018-12-23 stsp return err;
2828 abd2672a 2018-12-23 stsp static const struct got_error *
2829 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2831 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2832 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2833 15a94983 2018-12-23 stsp int obj_type;
2835 511a516b 2018-05-19 stsp f = got_opentemp();
2836 48ae06ee 2018-10-18 stsp if (f == NULL) {
2837 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2838 48ae06ee 2018-10-18 stsp goto done;
2840 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2841 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2842 fb43ecf1 2019-02-11 stsp goto done;
2846 15a94983 2018-12-23 stsp if (s->id1)
2847 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2849 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2851 15a94983 2018-12-23 stsp goto done;
2853 15a94983 2018-12-23 stsp switch (obj_type) {
2854 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2855 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2856 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2858 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2859 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2860 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2862 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2863 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2864 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2865 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2867 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2870 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2871 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2872 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2874 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2875 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2876 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2877 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2878 8b473291 2019-02-21 stsp s->repo, f);
2883 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2885 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2886 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2890 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2894 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2895 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2896 48ae06ee 2018-10-18 stsp return err;
2899 f5215bb9 2019-02-22 stsp static void
2900 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2902 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2903 f5215bb9 2019-02-22 stsp update_panels();
2904 f5215bb9 2019-02-22 stsp doupdate();
2907 48ae06ee 2018-10-18 stsp static const struct got_error *
2908 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2909 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2910 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2912 48ae06ee 2018-10-18 stsp const struct got_error *err;
2914 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2915 15a94983 2018-12-23 stsp int type1, type2;
2916 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2918 15a94983 2018-12-23 stsp return err;
2919 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2921 15a94983 2018-12-23 stsp return err;
2923 15a94983 2018-12-23 stsp if (type1 != type2)
2924 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2927 15a94983 2018-12-23 stsp if (id1) {
2928 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2929 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2930 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2932 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2934 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2935 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2936 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2937 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2938 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2940 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2941 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2942 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2943 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2944 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2945 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2946 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2947 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&view->state.diff.colors);
2949 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2950 bddb1296 2019-11-08 stsp err = add_color(&view->state.diff.colors,
2951 11b20872 2019-11-08 stsp "^-", TOG_COLOR_DIFF_MINUS,
2952 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_MINUS"));
2954 6d17833f 2019-11-08 stsp return err;
2955 11b20872 2019-11-08 stsp err = add_color(&view->state.diff.colors, "^\\+",
2956 11b20872 2019-11-08 stsp TOG_COLOR_DIFF_PLUS,
2957 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_PLUS"));
2958 6d17833f 2019-11-08 stsp if (err) {
2959 bddb1296 2019-11-08 stsp free_colors(&view->state.diff.colors);
2960 6d17833f 2019-11-08 stsp return err;
2962 bddb1296 2019-11-08 stsp err = add_color(&view->state.diff.colors,
2963 11b20872 2019-11-08 stsp "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
2964 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2965 6d17833f 2019-11-08 stsp if (err) {
2966 bddb1296 2019-11-08 stsp free_colors(&view->state.diff.colors);
2967 6d17833f 2019-11-08 stsp return err;
2970 bddb1296 2019-11-08 stsp err = add_color(&view->state.diff.colors,
2971 11b20872 2019-11-08 stsp "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
2972 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
2973 6d17833f 2019-11-08 stsp if (err) {
2974 bddb1296 2019-11-08 stsp free_colors(&view->state.diff.colors);
2975 6d17833f 2019-11-08 stsp return err;
2978 11b20872 2019-11-08 stsp err = add_color(&view->state.diff.colors,
2979 11b20872 2019-11-08 stsp "^(from|via): ", TOG_COLOR_AUTHOR,
2980 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_AUTHOR"));
2981 11b20872 2019-11-08 stsp if (err) {
2982 11b20872 2019-11-08 stsp free_colors(&view->state.diff.colors);
2983 11b20872 2019-11-08 stsp return err;
2986 11b20872 2019-11-08 stsp err = add_color(&view->state.diff.colors,
2987 11b20872 2019-11-08 stsp "^date: ", TOG_COLOR_DATE,
2988 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_DATE"));
2989 11b20872 2019-11-08 stsp if (err) {
2990 11b20872 2019-11-08 stsp free_colors(&view->state.diff.colors);
2991 11b20872 2019-11-08 stsp return err;
2995 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2996 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2997 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2999 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
3000 48ae06ee 2018-10-18 stsp if (err) {
3001 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
3002 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
3003 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
3004 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
3005 48ae06ee 2018-10-18 stsp return err;
3008 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
3009 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
3010 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
3012 5dc9f4bc 2018-08-04 stsp return NULL;
3015 e5a0f69f 2018-08-18 stsp static const struct got_error *
3016 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
3018 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3020 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
3021 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
3022 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
3023 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
3024 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
3025 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3026 bddb1296 2019-11-08 stsp free_colors(&view->state.diff.colors);
3027 e5a0f69f 2018-08-18 stsp return err;
3030 5dc9f4bc 2018-08-04 stsp static const struct got_error *
3031 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
3033 a3404814 2018-09-02 stsp const struct got_error *err;
3034 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
3035 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
3037 a3404814 2018-09-02 stsp if (s->id1) {
3038 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
3040 a3404814 2018-09-02 stsp return err;
3042 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
3044 a3404814 2018-09-02 stsp return err;
3046 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
3047 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3048 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3049 a3404814 2018-09-02 stsp free(id_str1);
3050 a3404814 2018-09-02 stsp free(id_str2);
3051 a3404814 2018-09-02 stsp return err;
3053 a3404814 2018-09-02 stsp free(id_str1);
3054 a3404814 2018-09-02 stsp free(id_str2);
3056 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
3057 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
3058 bddb1296 2019-11-08 stsp header, &s->colors);
3061 15a087fe 2019-02-21 stsp static const struct got_error *
3062 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
3063 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
3065 d7a04538 2019-02-21 stsp const struct got_error *err;
3066 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
3067 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
3068 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
3070 15a087fe 2019-02-21 stsp free(s->id2);
3071 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
3072 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
3073 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
3075 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3077 d7a04538 2019-02-21 stsp return err;
3078 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
3079 15a087fe 2019-02-21 stsp free(s->id1);
3080 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
3081 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3082 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
3083 15a087fe 2019-02-21 stsp return NULL;
3086 0cf4efb1 2018-09-29 stsp static const struct got_error *
3087 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3088 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3090 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
3091 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
3092 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
3093 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
3096 e5a0f69f 2018-08-18 stsp switch (ch) {
3098 1e37a5c2 2019-05-12 jcs case KEY_UP:
3099 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
3100 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3102 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3103 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3104 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3107 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3108 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3109 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3112 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3113 1e37a5c2 2019-05-12 jcs if (!s->eof)
3114 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3116 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3117 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3119 00ba99a7 2019-05-12 jcs if (s->eof)
3122 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3123 1e37a5c2 2019-05-12 jcs char *line;
3124 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
3125 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3126 1e37a5c2 2019-05-12 jcs if (line == NULL)
3131 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3132 1e37a5c2 2019-05-12 jcs s->diff_context--;
3133 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3134 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3138 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3139 1e37a5c2 2019-05-12 jcs s->diff_context++;
3140 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3141 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3146 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3148 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3149 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
3150 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
3151 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3154 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3159 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3163 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3164 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3166 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3167 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3171 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3173 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3175 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3176 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
3178 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
3179 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
3180 1e37a5c2 2019-05-12 jcs update_panels();
3181 1e37a5c2 2019-05-12 jcs doupdate();
3183 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
3184 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
3185 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
3186 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
3190 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3195 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
3196 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3199 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3203 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3204 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3206 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3207 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3213 bcbd79e2 2018-08-19 stsp return err;
3216 4ed7e80c 2018-05-20 stsp static const struct got_error *
3217 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3219 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3220 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3221 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3222 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3223 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
3224 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3226 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3228 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3230 26ed57b2 2018-05-19 stsp #ifndef PROFILE
3231 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3232 eb6600df 2019-01-04 stsp NULL) == -1)
3233 26ed57b2 2018-05-19 stsp err(1, "pledge");
3236 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3237 26ed57b2 2018-05-19 stsp switch (ch) {
3239 17020d27 2019-03-07 stsp usage_diff();
3240 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3244 26ed57b2 2018-05-19 stsp argc -= optind;
3245 26ed57b2 2018-05-19 stsp argv += optind;
3247 26ed57b2 2018-05-19 stsp if (argc == 0) {
3248 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3249 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3250 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
3251 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
3252 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
3253 15a94983 2018-12-23 stsp id_str1 = argv[0];
3254 15a94983 2018-12-23 stsp id_str2 = argv[1];
3255 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
3256 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
3257 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
3258 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
3259 15a94983 2018-12-23 stsp id_str1 = argv[1];
3260 15a94983 2018-12-23 stsp id_str2 = argv[2];
3262 26ed57b2 2018-05-19 stsp usage_diff();
3264 a915003a 2019-02-05 stsp init_curses();
3266 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3267 eb6600df 2019-01-04 stsp if (error)
3268 eb6600df 2019-01-04 stsp goto done;
3270 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3271 26ed57b2 2018-05-19 stsp if (error)
3272 26ed57b2 2018-05-19 stsp goto done;
3274 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
3275 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3276 26ed57b2 2018-05-19 stsp if (error)
3277 26ed57b2 2018-05-19 stsp goto done;
3279 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
3280 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3281 26ed57b2 2018-05-19 stsp if (error)
3282 26ed57b2 2018-05-19 stsp goto done;
3284 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3285 8b473291 2019-02-21 stsp if (error)
3286 8b473291 2019-02-21 stsp goto done;
3288 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3289 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3290 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3291 ea5e7bb5 2018-08-01 stsp goto done;
3293 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3294 5dc9f4bc 2018-08-04 stsp if (error)
3295 5dc9f4bc 2018-08-04 stsp goto done;
3296 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3298 c02c541e 2019-03-29 stsp free(repo_path);
3300 921be706 2019-06-28 stsp got_repo_close(repo);
3301 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3302 26ed57b2 2018-05-19 stsp return error;
3305 4ed7e80c 2018-05-20 stsp __dead static void
3306 9f7d7167 2018-04-29 stsp usage_blame(void)
3309 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3310 9f7d7167 2018-04-29 stsp getprogname());
3314 84451b3e 2018-07-10 stsp struct tog_blame_line {
3315 84451b3e 2018-07-10 stsp int annotated;
3316 84451b3e 2018-07-10 stsp struct got_object_id *id;
3319 4ed7e80c 2018-05-20 stsp static const struct got_error *
3320 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3321 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
3322 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
3323 11b20872 2019-11-08 stsp int *last_displayed_line, int *eof, int max_lines,
3324 11b20872 2019-11-08 stsp struct tog_colors *colors)
3326 84451b3e 2018-07-10 stsp const struct got_error *err;
3327 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3328 84451b3e 2018-07-10 stsp char *line;
3329 84451b3e 2018-07-10 stsp size_t len;
3330 84451b3e 2018-07-10 stsp wchar_t *wline;
3331 27a741e5 2019-09-11 stsp int width;
3332 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3333 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3334 ab089a2a 2018-07-12 stsp char *id_str;
3335 11b20872 2019-11-08 stsp struct tog_color *tc;
3337 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
3339 ab089a2a 2018-07-12 stsp return err;
3341 84451b3e 2018-07-10 stsp rewind(f);
3342 f7d12f7e 2018-08-01 stsp werase(view->window);
3344 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3345 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3346 ab089a2a 2018-07-12 stsp free(id_str);
3347 ab089a2a 2018-07-12 stsp return err;
3350 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3351 ab089a2a 2018-07-12 stsp free(line);
3352 2550e4c3 2018-07-13 stsp line = NULL;
3354 1cae65b4 2019-09-22 stsp return err;
3355 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3356 a3404814 2018-09-02 stsp wstandout(view->window);
3357 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3359 11b20872 2019-11-08 stsp wattr_on(view->window,
3360 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3361 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3363 11b20872 2019-11-08 stsp wattr_off(view->window,
3364 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3365 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3366 a3404814 2018-09-02 stsp wstandend(view->window);
3367 2550e4c3 2018-07-13 stsp free(wline);
3368 2550e4c3 2018-07-13 stsp wline = NULL;
3369 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3370 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3372 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3373 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
3374 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
3375 ab089a2a 2018-07-12 stsp free(id_str);
3376 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3378 ab089a2a 2018-07-12 stsp free(id_str);
3379 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3380 3f60a8ef 2018-07-10 stsp free(line);
3381 2550e4c3 2018-07-13 stsp line = NULL;
3383 3f60a8ef 2018-07-10 stsp return err;
3384 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3385 2550e4c3 2018-07-13 stsp free(wline);
3386 2550e4c3 2018-07-13 stsp wline = NULL;
3387 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3388 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3391 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
3392 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
3393 84451b3e 2018-07-10 stsp if (line == NULL) {
3397 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
3398 84451b3e 2018-07-10 stsp free(line);
3402 27a741e5 2019-09-11 stsp if (view->ncols <= 9) {
3403 27a741e5 2019-09-11 stsp width = 9;
3404 27a741e5 2019-09-11 stsp wline = wcsdup(L"");
3405 27a741e5 2019-09-11 stsp if (wline == NULL)
3406 27a741e5 2019-09-11 stsp err = got_error_from_errno("wcsdup");
3408 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line,
3409 27a741e5 2019-09-11 stsp view->ncols - 9, 9);
3410 27a741e5 2019-09-11 stsp width += 9;
3412 84451b3e 2018-07-10 stsp if (err) {
3413 84451b3e 2018-07-10 stsp free(line);
3414 84451b3e 2018-07-10 stsp return err;
3417 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3418 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3420 8d0fe45a 2019-08-12 stsp if (nlines > 0) {
3421 8d0fe45a 2019-08-12 stsp blame_line = &lines[lineno - 1];
3422 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3423 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3424 27a741e5 2019-09-11 stsp !(view->focussed &&
3425 27a741e5 2019-09-11 stsp nprinted == selected_line - 1)) {
3426 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3427 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3428 8d0fe45a 2019-08-12 stsp char *id_str;
3429 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3430 8d0fe45a 2019-08-12 stsp if (err) {
3431 8d0fe45a 2019-08-12 stsp free(line);
3432 8d0fe45a 2019-08-12 stsp free(wline);
3433 8d0fe45a 2019-08-12 stsp return err;
3435 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
3437 11b20872 2019-11-08 stsp wattr_on(view->window,
3438 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3439 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3441 11b20872 2019-11-08 stsp wattr_off(view->window,
3442 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
3443 8d0fe45a 2019-08-12 stsp free(id_str);
3444 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3446 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3447 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3450 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3451 ee41ec32 2018-07-10 stsp prev_id = NULL;
3454 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3455 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3456 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3458 27a741e5 2019-09-11 stsp waddwstr(view->window, wline);
3459 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
3460 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
3461 84451b3e 2018-07-10 stsp if (++nprinted == 1)
3462 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
3463 84451b3e 2018-07-10 stsp free(line);
3464 2550e4c3 2018-07-13 stsp free(wline);
3465 2550e4c3 2018-07-13 stsp wline = NULL;
3467 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
3469 1a57306a 2018-09-02 stsp view_vborder(view);
3471 84451b3e 2018-07-10 stsp return NULL;
3474 84451b3e 2018-07-10 stsp static const struct got_error *
3475 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3477 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
3478 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
3479 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
3480 1a76625f 2018-10-22 stsp int errcode;
3482 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
3483 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3484 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3486 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3487 1a76625f 2018-10-22 stsp if (errcode)
3488 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3490 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3491 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3492 d68a0a7d 2018-07-10 stsp goto done;
3495 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3496 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3498 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3499 d68a0a7d 2018-07-10 stsp if (line->annotated)
3500 d68a0a7d 2018-07-10 stsp goto done;
3502 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3503 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3504 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3505 84451b3e 2018-07-10 stsp goto done;
3507 84451b3e 2018-07-10 stsp line->annotated = 1;
3509 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3510 1a76625f 2018-10-22 stsp if (errcode)
3511 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3512 84451b3e 2018-07-10 stsp return err;
3515 84451b3e 2018-07-10 stsp static void *
3516 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3518 18430de3 2018-07-10 stsp const struct got_error *err;
3519 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3520 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3521 1a76625f 2018-10-22 stsp int errcode;
3523 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
3524 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3525 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
3526 fc06ba56 2019-08-22 stsp err = NULL;
3528 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3529 1a76625f 2018-10-22 stsp if (errcode)
3530 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3531 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3533 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3534 c9beca56 2018-07-22 stsp ta->repo = NULL;
3535 c9beca56 2018-07-22 stsp *ta->complete = 1;
3537 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3538 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3539 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3541 18430de3 2018-07-10 stsp return (void *)err;
3544 245d91c1 2018-07-12 stsp static struct got_object_id *
3545 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3546 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
3548 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3550 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
3551 8d0fe45a 2019-08-12 stsp return NULL;
3553 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3554 245d91c1 2018-07-12 stsp if (!line->annotated)
3555 245d91c1 2018-07-12 stsp return NULL;
3557 245d91c1 2018-07-12 stsp return line->id;
3560 b880a918 2018-07-10 stsp static const struct got_error *
3561 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3563 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3566 245d91c1 2018-07-12 stsp if (blame->thread) {
3567 1a76625f 2018-10-22 stsp int errcode;
3568 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3569 1a76625f 2018-10-22 stsp if (errcode)
3570 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3571 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3572 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3573 1a76625f 2018-10-22 stsp if (errcode)
3574 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
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 got_error_set_errno(errcode,
3578 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3579 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3580 245d91c1 2018-07-12 stsp err = NULL;
3581 245d91c1 2018-07-12 stsp blame->thread = NULL;
3583 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3584 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3585 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3587 245d91c1 2018-07-12 stsp if (blame->f) {
3588 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3589 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3590 245d91c1 2018-07-12 stsp blame->f = NULL;
3592 57670559 2018-12-23 stsp if (blame->lines) {
3593 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3594 57670559 2018-12-23 stsp free(blame->lines[i].id);
3595 57670559 2018-12-23 stsp free(blame->lines);
3596 57670559 2018-12-23 stsp blame->lines = NULL;
3598 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3599 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3601 245d91c1 2018-07-12 stsp return err;
3604 245d91c1 2018-07-12 stsp static const struct got_error *
3605 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
3607 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
3608 fc06ba56 2019-08-22 stsp int *done = arg;
3609 fc06ba56 2019-08-22 stsp int errcode;
3611 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3612 fc06ba56 2019-08-22 stsp if (errcode)
3613 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3614 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
3616 fc06ba56 2019-08-22 stsp if (*done)
3617 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
3619 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3620 fc06ba56 2019-08-22 stsp if (errcode)
3621 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3622 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
3624 fc06ba56 2019-08-22 stsp return err;
3627 fc06ba56 2019-08-22 stsp static const struct got_error *
3628 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3629 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3630 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3631 245d91c1 2018-07-12 stsp struct got_repository *repo)
3633 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3634 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3635 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3636 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3637 15a94983 2018-12-23 stsp int obj_type;
3639 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3641 15a94983 2018-12-23 stsp return err;
3642 15a94983 2018-12-23 stsp if (obj_id == NULL)
3643 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
3645 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3647 84451b3e 2018-07-10 stsp goto done;
3649 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3650 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3651 84451b3e 2018-07-10 stsp goto done;
3654 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3656 a70480e0 2018-06-23 stsp goto done;
3657 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3658 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3659 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3660 84451b3e 2018-07-10 stsp goto done;
3662 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3663 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
3664 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
3665 84451b3e 2018-07-10 stsp goto done;
3667 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3668 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3669 b02560ec 2019-08-19 stsp blame->nlines--;
3671 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3672 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3673 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3674 84451b3e 2018-07-10 stsp goto done;
3677 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3679 bd24772e 2018-07-11 stsp goto done;
3681 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3682 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3683 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3684 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3685 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3686 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3687 245d91c1 2018-07-12 stsp goto done;
3689 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3691 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3692 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3693 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3694 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3695 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
3696 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_arg = done;
3697 245d91c1 2018-07-12 stsp *blame_complete = 0;
3701 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3702 27d434c2 2018-09-15 stsp free(obj_id);
3704 245d91c1 2018-07-12 stsp stop_blame(blame);
3705 245d91c1 2018-07-12 stsp return err;
3708 245d91c1 2018-07-12 stsp static const struct got_error *
3709 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3710 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3711 8b473291 2019-02-21 stsp struct got_repository *repo)
3713 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3714 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3716 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3718 c4843652 2019-08-12 stsp s->path = strdup(path);
3719 c4843652 2019-08-12 stsp if (s->path == NULL)
3720 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
3722 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3723 c4843652 2019-08-12 stsp if (err) {
3724 c4843652 2019-08-12 stsp free(s->path);
3725 7cbe629d 2018-08-04 stsp return err;
3728 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3729 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3730 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3731 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3732 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3733 fb2756b9 2018-08-04 stsp s->repo = repo;
3734 8b473291 2019-02-21 stsp s->refs = refs;
3735 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3736 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3738 11b20872 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
3739 11b20872 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
3740 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3741 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
3743 11b20872 2019-11-08 stsp return err;
3746 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3747 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3748 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3749 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
3750 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
3752 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3753 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3754 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3755 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3758 e5a0f69f 2018-08-18 stsp static const struct got_error *
3759 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3761 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3762 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3764 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3765 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3767 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3768 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3769 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3770 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3771 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3774 e5a0f69f 2018-08-18 stsp free(s->path);
3775 11b20872 2019-11-08 stsp free_colors(&s->colors);
3777 e5a0f69f 2018-08-18 stsp return err;
3780 7cbe629d 2018-08-04 stsp static const struct got_error *
3781 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
3783 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3785 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
3786 6c4c42e0 2019-06-24 stsp return NULL;
3789 6c4c42e0 2019-06-24 stsp static const struct got_error *
3790 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
3792 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3793 6c4c42e0 2019-06-24 stsp int lineno;
3795 6c4c42e0 2019-06-24 stsp if (!view->searching) {
3796 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3797 6c4c42e0 2019-06-24 stsp return NULL;
3800 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3801 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3802 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
3804 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
3806 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3807 6c4c42e0 2019-06-24 stsp lineno = 1;
3809 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3812 6c4c42e0 2019-06-24 stsp while (1) {
3813 6c4c42e0 2019-06-24 stsp char *line = NULL;
3814 6c4c42e0 2019-06-24 stsp off_t offset;
3815 6c4c42e0 2019-06-24 stsp size_t len;
3817 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
3818 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
3819 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3820 6c4c42e0 2019-06-24 stsp free(line);
3824 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3825 6c4c42e0 2019-06-24 stsp lineno = 1;
3827 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3830 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
3831 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3832 6c4c42e0 2019-06-24 stsp free(line);
3833 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
3835 6c4c42e0 2019-06-24 stsp free(line);
3836 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
3837 2246482e 2019-06-25 stsp if (line && match_line(line, &view->regex)) {
3838 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3839 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
3840 6c4c42e0 2019-06-24 stsp free(line);
3843 6c4c42e0 2019-06-24 stsp free(line);
3844 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3850 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3851 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
3852 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
3855 6c4c42e0 2019-06-24 stsp return NULL;
3858 6c4c42e0 2019-06-24 stsp static const struct got_error *
3859 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
3861 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3862 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
3863 2b380cc8 2018-10-24 stsp int errcode;
3865 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
3866 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3867 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
3868 2b380cc8 2018-10-24 stsp if (errcode)
3869 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3871 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
3874 51fe7530 2019-08-19 stsp if (s->blame_complete)
3875 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
3877 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3878 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3879 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
3880 11b20872 2019-11-08 stsp &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
3882 669b5ffa 2018-10-07 stsp view_vborder(view);
3883 e5a0f69f 2018-08-18 stsp return err;
3886 e5a0f69f 2018-08-18 stsp static const struct got_error *
3887 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3888 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3890 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
3891 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
3892 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3893 669b5ffa 2018-10-07 stsp int begin_x = 0;
3895 e5a0f69f 2018-08-18 stsp switch (ch) {
3897 1e37a5c2 2019-05-12 jcs s->done = 1;
3900 1e37a5c2 2019-05-12 jcs case KEY_UP:
3901 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
3902 1e37a5c2 2019-05-12 jcs s->selected_line--;
3903 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
3904 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3905 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3907 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3908 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
3909 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3912 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3913 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3914 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3916 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3919 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3920 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3921 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3922 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3923 1e37a5c2 2019-05-12 jcs s->selected_line++;
3924 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3925 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3926 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3929 1e37a5c2 2019-05-12 jcs case 'p': {
3930 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3931 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3932 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3933 1e37a5c2 2019-05-12 jcs if (id == NULL)
3935 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3936 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3937 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3938 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3939 1e37a5c2 2019-05-12 jcs int obj_type;
3940 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3941 1e37a5c2 2019-05-12 jcs s->repo, id);
3944 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3945 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3946 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3947 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3950 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3951 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3952 1e37a5c2 2019-05-12 jcs pid->id, s->path);
3953 e5a0f69f 2018-08-18 stsp if (err) {
3954 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
3955 1e37a5c2 2019-05-12 jcs err = NULL;
3956 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3959 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
3961 1e37a5c2 2019-05-12 jcs free(blob_id);
3962 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
3963 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
3964 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3967 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3969 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3971 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
3972 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
3974 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3979 1e37a5c2 2019-05-12 jcs s->done = 1;
3980 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3981 1e37a5c2 2019-05-12 jcs s->done = 0;
3982 1e37a5c2 2019-05-12 jcs if (thread_err)
3984 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3985 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
3986 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3987 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3988 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
3989 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
3994 1e37a5c2 2019-05-12 jcs case 'B': {
3995 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
3996 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
3997 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
3999 1e37a5c2 2019-05-12 jcs s->done = 1;
4000 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
4001 1e37a5c2 2019-05-12 jcs s->done = 0;
4002 1e37a5c2 2019-05-12 jcs if (thread_err)
4004 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4005 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
4006 1e37a5c2 2019-05-12 jcs s->blamed_commit =
4007 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
4008 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
4009 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
4010 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
4011 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
4016 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4017 1e37a5c2 2019-05-12 jcs case '\r': {
4018 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
4019 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
4020 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
4021 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4022 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
4023 1e37a5c2 2019-05-12 jcs if (id == NULL)
4025 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
4028 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
4029 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
4030 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4031 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4032 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4033 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
4034 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4035 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
4038 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
4039 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
4040 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
4042 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4045 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4046 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4049 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
4051 1e37a5c2 2019-05-12 jcs view_close(diff_view);
4054 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
4055 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4057 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
4062 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4064 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4065 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
4066 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
4069 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
4070 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
4071 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4072 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4075 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
4076 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
4077 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
4078 1e37a5c2 2019-05-12 jcs view->nlines - 2;
4080 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
4081 1e37a5c2 2019-05-12 jcs s->blame.nlines -
4082 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
4084 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4085 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
4086 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
4087 1e37a5c2 2019-05-12 jcs view->nlines - 2);
4093 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
4096 a70480e0 2018-06-23 stsp static const struct got_error *
4097 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
4099 a70480e0 2018-06-23 stsp const struct got_error *error;
4100 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
4101 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4102 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
4103 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4104 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4105 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
4107 e1cd8fed 2018-08-01 stsp struct tog_view *view;
4109 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4111 a70480e0 2018-06-23 stsp #ifndef PROFILE
4112 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4113 8e94dd5b 2019-01-04 stsp NULL) == -1)
4114 a70480e0 2018-06-23 stsp err(1, "pledge");
4117 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4118 a70480e0 2018-06-23 stsp switch (ch) {
4120 a70480e0 2018-06-23 stsp commit_id_str = optarg;
4123 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4124 69069811 2018-08-02 stsp if (repo_path == NULL)
4125 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4129 17020d27 2019-03-07 stsp usage_blame();
4130 a70480e0 2018-06-23 stsp /* NOTREACHED */
4134 a70480e0 2018-06-23 stsp argc -= optind;
4135 a70480e0 2018-06-23 stsp argv += optind;
4137 69069811 2018-08-02 stsp if (argc == 1)
4138 69069811 2018-08-02 stsp path = argv[0];
4140 a70480e0 2018-06-23 stsp usage_blame();
4142 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
4143 69069811 2018-08-02 stsp if (cwd == NULL) {
4144 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4145 69069811 2018-08-02 stsp goto done;
4147 69069811 2018-08-02 stsp if (repo_path == NULL) {
4148 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4149 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4150 69069811 2018-08-02 stsp goto done;
4152 eb41ed75 2019-02-05 stsp error = NULL;
4153 eb41ed75 2019-02-05 stsp if (worktree) {
4154 eb41ed75 2019-02-05 stsp repo_path =
4155 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4156 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
4157 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4158 eb41ed75 2019-02-05 stsp if (error)
4159 eb41ed75 2019-02-05 stsp goto done;
4161 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4162 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
4163 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4164 eb41ed75 2019-02-05 stsp goto done;
4169 a915003a 2019-02-05 stsp init_curses();
4171 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4172 c02c541e 2019-03-29 stsp if (error != NULL)
4173 8e94dd5b 2019-01-04 stsp goto done;
4175 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4176 c02c541e 2019-03-29 stsp if (error)
4177 92205607 2019-01-04 stsp goto done;
4179 eb41ed75 2019-02-05 stsp if (worktree) {
4180 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4181 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4182 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4183 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4184 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4185 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4186 eb41ed75 2019-02-05 stsp path) == -1) {
4187 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4188 eb41ed75 2019-02-05 stsp goto done;
4190 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4193 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4195 eb41ed75 2019-02-05 stsp if (error)
4196 69069811 2018-08-02 stsp goto done;
4198 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4199 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4200 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4201 a70480e0 2018-06-23 stsp if (error != NULL)
4202 66b4983c 2018-06-23 stsp goto done;
4203 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4204 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4206 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&commit_id, commit_id_str, repo);
4207 f2b6a97d 2019-07-15 stsp if (error) {
4208 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
4209 f2b6a97d 2019-07-15 stsp goto done;
4210 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&commit_id,
4211 f2b6a97d 2019-07-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
4214 a19e88aa 2018-06-23 stsp if (error != NULL)
4215 8b473291 2019-02-21 stsp goto done;
4217 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4218 8b473291 2019-02-21 stsp if (error)
4219 a19e88aa 2018-06-23 stsp goto done;
4221 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4222 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4223 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4224 e1cd8fed 2018-08-01 stsp goto done;
4226 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4227 7cbe629d 2018-08-04 stsp if (error)
4228 7cbe629d 2018-08-04 stsp goto done;
4229 12314ad4 2019-08-31 stsp if (worktree) {
4230 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4231 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4232 12314ad4 2019-08-31 stsp worktree = NULL;
4234 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4236 69069811 2018-08-02 stsp free(repo_path);
4237 69069811 2018-08-02 stsp free(cwd);
4238 a70480e0 2018-06-23 stsp free(commit_id);
4239 eb41ed75 2019-02-05 stsp if (worktree)
4240 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4242 a70480e0 2018-06-23 stsp got_repo_close(repo);
4243 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4244 a70480e0 2018-06-23 stsp return error;
4247 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4248 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
4249 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
4250 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
4251 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
4252 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
4253 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int selected, int limit,
4254 f26dddb7 2019-11-08 stsp int isroot, struct tog_colors *colors)
4256 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4257 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4258 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4259 f26dddb7 2019-11-08 stsp struct tog_color *tc;
4260 56e0773d 2019-11-28 stsp int width, n, i, nentries;
4262 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
4264 f7d12f7e 2018-08-01 stsp werase(view->window);
4266 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4267 ffd1d5e5 2018-06-23 stsp return NULL;
4269 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, label, view->ncols, 0);
4271 ffd1d5e5 2018-06-23 stsp return err;
4272 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4273 a3404814 2018-09-02 stsp wstandout(view->window);
4274 11b20872 2019-11-08 stsp tc = get_color(colors, TOG_COLOR_COMMIT);
4276 11b20872 2019-11-08 stsp wattr_on(view->window,
4277 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4278 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4280 11b20872 2019-11-08 stsp wattr_off(view->window,
4281 11b20872 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4282 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4283 a3404814 2018-09-02 stsp wstandend(view->window);
4284 2550e4c3 2018-07-13 stsp free(wline);
4285 2550e4c3 2018-07-13 stsp wline = NULL;
4286 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4287 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4288 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4289 ffd1d5e5 2018-06-23 stsp return NULL;
4290 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4292 ce52c690 2018-06-23 stsp return err;
4293 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4294 2550e4c3 2018-07-13 stsp free(wline);
4295 2550e4c3 2018-07-13 stsp wline = NULL;
4296 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4297 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4298 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4299 ffd1d5e5 2018-06-23 stsp return NULL;
4300 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4301 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4302 a1eca9bb 2018-06-23 stsp return NULL;
4304 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
4305 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(tree);
4306 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
4307 0cf4efb1 2018-09-29 stsp if (view->focussed)
4308 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4309 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
4311 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4312 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
4313 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4314 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4315 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4316 ffd1d5e5 2018-06-23 stsp return NULL;
4320 56e0773d 2019-11-28 stsp te = *first_displayed_entry;
4323 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4324 56e0773d 2019-11-28 stsp for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4325 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
4326 848d6979 2019-08-12 stsp const char *modestr = "";
4327 56e0773d 2019-11-28 stsp mode_t mode;
4329 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4330 56e0773d 2019-11-28 stsp mode = got_tree_entry_get_mode(te);
4332 1d13200f 2018-07-12 stsp if (show_ids) {
4333 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4334 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4336 638f9024 2019-05-13 stsp return got_error_from_errno(
4337 230a42bd 2019-05-11 jcs "got_object_id_str");
4339 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4340 63c5ca5d 2019-08-24 stsp modestr = "$";
4341 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
4342 848d6979 2019-08-12 stsp modestr = "@";
4343 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4344 848d6979 2019-08-12 stsp modestr = "/";
4345 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4346 848d6979 2019-08-12 stsp modestr = "*";
4347 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4348 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te), modestr) == -1) {
4349 1d13200f 2018-07-12 stsp free(id_str);
4350 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4352 1d13200f 2018-07-12 stsp free(id_str);
4353 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4354 ffd1d5e5 2018-06-23 stsp if (err) {
4355 ffd1d5e5 2018-06-23 stsp free(line);
4358 ffd1d5e5 2018-06-23 stsp if (n == selected) {
4359 0cf4efb1 2018-09-29 stsp if (view->focussed)
4360 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4361 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
4363 bddb1296 2019-11-08 stsp tc = match_color(colors, line);
4365 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4366 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4367 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4369 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4370 f26dddb7 2019-11-08 stsp COLOR_PAIR(tc->colorpair), NULL);
4371 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4372 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4373 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
4374 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4375 ffd1d5e5 2018-06-23 stsp free(line);
4376 2550e4c3 2018-07-13 stsp free(wline);
4377 2550e4c3 2018-07-13 stsp wline = NULL;
4379 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4380 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
4381 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4385 ffd1d5e5 2018-06-23 stsp return err;
4388 ffd1d5e5 2018-06-23 stsp static void
4389 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
4390 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
4391 56e0773d 2019-11-28 stsp struct got_tree_object *tree, int isroot)
4393 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4396 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
4399 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, 0);
4400 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
4401 ffd1d5e5 2018-06-23 stsp if (!isroot)
4402 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4407 56e0773d 2019-11-28 stsp while (*first_displayed_entry && i < maxscroll) {
4408 56e0773d 2019-11-28 stsp *first_displayed_entry = got_tree_entry_get_prev(tree,
4409 56e0773d 2019-11-28 stsp *first_displayed_entry);
4412 56e0773d 2019-11-28 stsp if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4413 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4416 768394f3 2019-01-24 stsp static int
4417 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4418 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
4419 56e0773d 2019-11-28 stsp struct got_tree_object *tree)
4421 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4422 ffd1d5e5 2018-06-23 stsp int n = 0;
4424 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
4425 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, *first_displayed_entry);
4427 56e0773d 2019-11-28 stsp next = got_object_tree_get_first_entry(tree);
4429 768394f3 2019-01-24 stsp last = last_displayed_entry;
4430 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4431 56e0773d 2019-11-28 stsp last = got_tree_entry_get_next(tree, last);
4432 768394f3 2019-01-24 stsp if (last) {
4433 768394f3 2019-01-24 stsp *first_displayed_entry = next;
4434 56e0773d 2019-11-28 stsp next = got_tree_entry_get_next(tree, next);
4440 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4441 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4442 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4444 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4445 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4446 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4448 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4449 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(pt->selected_entry))
4450 56e0773d 2019-11-28 stsp + 1 /* slash */;
4452 56e0773d 2019-11-28 stsp len += strlen(got_tree_entry_get_name(te));
4454 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4455 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4456 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4458 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4459 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4460 d9765a41 2018-06-23 stsp while (pt) {
4461 56e0773d 2019-11-28 stsp const char *name = got_tree_entry_get_name(pt->selected_entry);
4462 56e0773d 2019-11-28 stsp if (strlcat(*path, name, len) >= len) {
4463 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4464 cb2ebc8a 2018-06-23 stsp goto done;
4466 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
4467 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4468 cb2ebc8a 2018-06-23 stsp goto done;
4470 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4473 56e0773d 2019-11-28 stsp if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4474 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4475 ce52c690 2018-06-23 stsp goto done;
4479 ce52c690 2018-06-23 stsp if (err) {
4480 ce52c690 2018-06-23 stsp free(*path);
4481 ce52c690 2018-06-23 stsp *path = NULL;
4483 ce52c690 2018-06-23 stsp return err;
4486 ce52c690 2018-06-23 stsp static const struct got_error *
4487 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
4488 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4489 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4490 8b473291 2019-02-21 stsp struct got_repository *repo)
4492 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
4493 ce52c690 2018-06-23 stsp char *path;
4494 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
4496 a0de39f3 2019-08-09 stsp *new_view = NULL;
4498 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
4500 ce52c690 2018-06-23 stsp return err;
4502 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4503 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
4504 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
4505 83ce39e3 2019-08-12 stsp goto done;
4508 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
4509 e5a0f69f 2018-08-18 stsp if (err) {
4510 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
4511 fc06ba56 2019-08-22 stsp err = NULL;
4512 e5a0f69f 2018-08-18 stsp view_close(blame_view);
4514 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
4516 83ce39e3 2019-08-12 stsp free(path);
4517 69efd4c4 2018-07-18 stsp return err;
4520 69efd4c4 2018-07-18 stsp static const struct got_error *
4521 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
4522 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4523 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4524 8b473291 2019-02-21 stsp struct got_repository *repo)
4526 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
4527 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
4528 69efd4c4 2018-07-18 stsp char *path;
4530 a0de39f3 2019-08-09 stsp *new_view = NULL;
4532 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4533 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
4534 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
4536 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
4538 69efd4c4 2018-07-18 stsp return err;
4540 d01904d4 2019-06-25 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4542 e5a0f69f 2018-08-18 stsp view_close(log_view);
4544 e5a0f69f 2018-08-18 stsp *new_view = log_view;
4545 cb2ebc8a 2018-06-23 stsp free(path);
4546 cb2ebc8a 2018-06-23 stsp return err;
4549 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4550 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
4551 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4552 8b473291 2019-02-21 stsp struct got_repository *repo)
4554 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4555 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
4556 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4558 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
4560 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
4561 ffd1d5e5 2018-06-23 stsp if (err != NULL)
4562 ffd1d5e5 2018-06-23 stsp goto done;
4564 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4565 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4566 ffd1d5e5 2018-06-23 stsp goto done;
4569 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
4570 56e0773d 2019-11-28 stsp s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4571 56e0773d 2019-11-28 stsp s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4572 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
4573 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
4574 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4575 6484ec90 2018-09-29 stsp goto done;
4577 8b473291 2019-02-21 stsp s->refs = refs;
4578 fb2756b9 2018-08-04 stsp s->repo = repo;
4580 bddb1296 2019-11-08 stsp SIMPLEQ_INIT(&s->colors);
4582 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4583 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\$$",
4584 11b20872 2019-11-08 stsp TOG_COLOR_TREE_SUBMODULE,
4585 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4587 c0b01bdb 2019-11-08 stsp goto done;
4588 11b20872 2019-11-08 stsp err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4589 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_SYMLINK"));
4590 c0b01bdb 2019-11-08 stsp if (err) {
4591 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4592 c0b01bdb 2019-11-08 stsp goto done;
4594 11b20872 2019-11-08 stsp err = add_color(&s->colors, "/$",
4595 11b20872 2019-11-08 stsp TOG_COLOR_TREE_DIRECTORY,
4596 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4597 c0b01bdb 2019-11-08 stsp if (err) {
4598 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4599 c0b01bdb 2019-11-08 stsp goto done;
4602 11b20872 2019-11-08 stsp err = add_color(&s->colors, "\\*$",
4603 11b20872 2019-11-08 stsp TOG_COLOR_TREE_EXECUTABLE,
4604 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4605 11b20872 2019-11-08 stsp if (err) {
4606 11b20872 2019-11-08 stsp free_colors(&s->colors);
4607 11b20872 2019-11-08 stsp goto done;
4610 11b20872 2019-11-08 stsp err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4611 11b20872 2019-11-08 stsp get_color_value("TOG_COLOR_COMMIT"));
4612 c0b01bdb 2019-11-08 stsp if (err) {
4613 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4614 c0b01bdb 2019-11-08 stsp goto done;
4618 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
4619 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
4620 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
4621 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
4622 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
4624 ad80ab7b 2018-08-04 stsp free(commit_id_str);
4625 6484ec90 2018-09-29 stsp if (err) {
4626 fb2756b9 2018-08-04 stsp free(s->tree_label);
4627 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4629 ad80ab7b 2018-08-04 stsp return err;
4632 e5a0f69f 2018-08-18 stsp static const struct got_error *
4633 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
4635 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4637 bddb1296 2019-11-08 stsp free_colors(&s->colors);
4638 fb2756b9 2018-08-04 stsp free(s->tree_label);
4639 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4640 6484ec90 2018-09-29 stsp free(s->commit_id);
4641 6484ec90 2018-09-29 stsp s->commit_id = NULL;
4642 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
4643 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
4644 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
4645 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
4646 ad80ab7b 2018-08-04 stsp free(parent);
4649 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
4650 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
4651 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
4653 7c32bd05 2019-06-22 stsp return NULL;
4656 7c32bd05 2019-06-22 stsp static const struct got_error *
4657 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
4659 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4661 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
4662 7c32bd05 2019-06-22 stsp return NULL;
4665 7c32bd05 2019-06-22 stsp static int
4666 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4668 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
4670 56e0773d 2019-11-28 stsp return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4674 7c32bd05 2019-06-22 stsp static const struct got_error *
4675 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
4677 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4678 56e0773d 2019-11-28 stsp struct got_tree_entry *te = NULL;
4680 7c32bd05 2019-06-22 stsp if (!view->searching) {
4681 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4682 7c32bd05 2019-06-22 stsp return NULL;
4685 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4686 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
4687 7c32bd05 2019-06-22 stsp if (s->selected_entry)
4688 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree,
4689 56e0773d 2019-11-28 stsp s->selected_entry);
4691 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4693 56e0773d 2019-11-28 stsp if (s->selected_entry == NULL)
4694 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4696 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree,
4697 56e0773d 2019-11-28 stsp s->selected_entry);
4700 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4701 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4703 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4706 7c32bd05 2019-06-22 stsp while (1) {
4707 56e0773d 2019-11-28 stsp if (te == NULL) {
4708 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
4709 ac66afb8 2019-06-24 stsp view->search_next_done = 1;
4710 ac66afb8 2019-06-24 stsp return NULL;
4712 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4713 56e0773d 2019-11-28 stsp te = got_object_tree_get_first_entry(s->tree);
4715 56e0773d 2019-11-28 stsp te = got_object_tree_get_last_entry(s->tree);
4718 56e0773d 2019-11-28 stsp if (match_tree_entry(te, &view->regex)) {
4719 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4720 56e0773d 2019-11-28 stsp s->matched_entry = te;
4724 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4725 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
4727 56e0773d 2019-11-28 stsp te = got_tree_entry_get_prev(s->tree, te);
4730 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4731 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
4732 7c32bd05 2019-06-22 stsp s->selected = 0;
4735 e5a0f69f 2018-08-18 stsp return NULL;
4738 ad80ab7b 2018-08-04 stsp static const struct got_error *
4739 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
4741 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
4742 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4743 e5a0f69f 2018-08-18 stsp char *parent_path;
4745 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
4747 e5a0f69f 2018-08-18 stsp return err;
4749 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
4750 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
4751 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4752 56e0773d 2019-11-28 stsp s->tree, s->selected, view->nlines, s->tree == s->root,
4753 bddb1296 2019-11-08 stsp &s->colors);
4754 e5a0f69f 2018-08-18 stsp free(parent_path);
4756 669b5ffa 2018-10-07 stsp view_vborder(view);
4757 e5a0f69f 2018-08-18 stsp return err;
4760 e5a0f69f 2018-08-18 stsp static const struct got_error *
4761 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4762 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4764 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4765 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
4766 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
4767 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
4769 e5a0f69f 2018-08-18 stsp switch (ch) {
4771 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
4774 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
4776 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4777 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4778 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
4779 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
4780 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
4781 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4782 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4784 1e37a5c2 2019-05-12 jcs return err;
4785 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
4787 1e37a5c2 2019-05-12 jcs view_close(log_view);
4790 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
4791 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4793 1e37a5c2 2019-05-12 jcs *new_view = log_view;
4796 1e37a5c2 2019-05-12 jcs case KEY_UP:
4797 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
4798 1e37a5c2 2019-05-12 jcs s->selected--;
4799 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
4802 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
4804 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
4805 56e0773d 2019-11-28 stsp s->tree, s->tree == s->root);
4807 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4808 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
4809 56e0773d 2019-11-28 stsp MAX(0, view->nlines - 4 - s->selected), s->tree,
4810 1e37a5c2 2019-05-12 jcs s->tree == s->root);
4811 1e37a5c2 2019-05-12 jcs s->selected = 0;
4812 56e0773d 2019-11-28 stsp if (got_object_tree_get_first_entry(s->tree) ==
4813 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
4814 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
4817 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4818 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
4819 1e37a5c2 2019-05-12 jcs s->selected++;
4822 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4824 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
4826 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
4827 56e0773d 2019-11-28 stsp s->last_displayed_entry, s->tree);
4829 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4830 56e0773d 2019-11-28 stsp if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4832 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
4833 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
4834 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4837 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
4838 56e0773d 2019-11-28 stsp view->nlines, s->last_displayed_entry, s->tree);
4839 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
4840 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
4841 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
4842 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
4844 1e37a5c2 2019-05-12 jcs ndisplayed++;
4845 56e0773d 2019-11-28 stsp te = got_tree_entry_get_next(s->tree, te);
4846 1e37a5c2 2019-05-12 jcs } while (te);
4847 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
4850 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4852 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4853 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4854 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
4855 1e37a5c2 2019-05-12 jcs /* user selected '..' */
4856 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
4858 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
4859 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
4861 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
4862 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
4863 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
4864 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
4865 1e37a5c2 2019-05-12 jcs s->selected_entry =
4866 1e37a5c2 2019-05-12 jcs parent->selected_entry;
4867 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
4868 1e37a5c2 2019-05-12 jcs free(parent);
4869 56e0773d 2019-11-28 stsp } else if (S_ISDIR(got_tree_entry_get_mode(
4870 56e0773d 2019-11-28 stsp s->selected_entry))) {
4871 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
4872 56e0773d 2019-11-28 stsp err = got_object_open_as_tree(&subtree, s->repo,
4873 56e0773d 2019-11-28 stsp got_tree_entry_get_id(s->selected_entry));
4876 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
4877 941e9f74 2019-05-21 stsp if (err) {
4878 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
4881 56e0773d 2019-11-28 stsp } else if (S_ISREG(got_tree_entry_get_mode(
4882 56e0773d 2019-11-28 stsp s->selected_entry))) {
4883 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
4884 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
4885 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
4887 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
4888 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
4889 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
4892 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
4893 669b5ffa 2018-10-07 stsp err = view_close_child(view);
4895 669b5ffa 2018-10-07 stsp return err;
4896 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
4897 669b5ffa 2018-10-07 stsp if (err) {
4898 1e37a5c2 2019-05-12 jcs view_close(blame_view);
4901 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
4902 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
4904 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
4907 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4908 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
4909 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4915 ffd1d5e5 2018-06-23 stsp return err;
4918 ffd1d5e5 2018-06-23 stsp __dead static void
4919 ffd1d5e5 2018-06-23 stsp usage_tree(void)
4922 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4923 ffd1d5e5 2018-06-23 stsp getprogname());
4927 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4928 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
4930 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
4931 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
4932 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4933 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
4934 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4935 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
4936 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
4937 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
4939 5221c383 2018-08-01 stsp struct tog_view *view;
4941 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4943 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
4944 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4945 d188b9a6 2019-01-04 stsp NULL) == -1)
4946 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
4949 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
4950 ffd1d5e5 2018-06-23 stsp switch (ch) {
4952 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
4955 17020d27 2019-03-07 stsp usage_tree();
4956 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
4960 ffd1d5e5 2018-06-23 stsp argc -= optind;
4961 ffd1d5e5 2018-06-23 stsp argv += optind;
4963 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
4964 52185f70 2019-02-05 stsp struct got_worktree *worktree;
4965 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
4966 52185f70 2019-02-05 stsp if (cwd == NULL)
4967 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
4968 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4969 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4970 52185f70 2019-02-05 stsp goto done;
4971 52185f70 2019-02-05 stsp if (worktree) {
4972 52185f70 2019-02-05 stsp free(cwd);
4973 52185f70 2019-02-05 stsp repo_path =
4974 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4975 52185f70 2019-02-05 stsp got_worktree_close(worktree);
4977 52185f70 2019-02-05 stsp repo_path = cwd;
4978 52185f70 2019-02-05 stsp if (repo_path == NULL) {
4979 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4980 52185f70 2019-02-05 stsp goto done;
4982 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
4983 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
4984 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
4985 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
4987 ffd1d5e5 2018-06-23 stsp usage_log();
4989 a915003a 2019-02-05 stsp init_curses();
4991 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4992 c02c541e 2019-03-29 stsp if (error != NULL)
4993 52185f70 2019-02-05 stsp goto done;
4995 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4996 c02c541e 2019-03-29 stsp if (error)
4997 52185f70 2019-02-05 stsp goto done;
4999 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
5000 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
5002 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&commit_id, commit_id_arg, repo);
5003 f2b6a97d 2019-07-15 stsp if (error) {
5004 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
5005 f2b6a97d 2019-07-15 stsp goto done;
5006 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&commit_id,
5007 f2b6a97d 2019-07-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
5010 ffd1d5e5 2018-06-23 stsp if (error != NULL)
5011 ffd1d5e5 2018-06-23 stsp goto done;
5013 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5014 ffd1d5e5 2018-06-23 stsp if (error != NULL)
5015 ffd1d5e5 2018-06-23 stsp goto done;
5017 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
5018 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
5019 ffd1d5e5 2018-06-23 stsp if (error != NULL)
5020 8b473291 2019-02-21 stsp goto done;
5022 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5023 8b473291 2019-02-21 stsp if (error)
5024 ffd1d5e5 2018-06-23 stsp goto done;
5026 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5027 5221c383 2018-08-01 stsp if (view == NULL) {
5028 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
5029 5221c383 2018-08-01 stsp goto done;
5031 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
5032 ad80ab7b 2018-08-04 stsp if (error)
5033 ad80ab7b 2018-08-04 stsp goto done;
5034 e5a0f69f 2018-08-18 stsp error = view_loop(view);
5036 52185f70 2019-02-05 stsp free(repo_path);
5037 ffd1d5e5 2018-06-23 stsp free(commit_id);
5038 ffd1d5e5 2018-06-23 stsp if (commit)
5039 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
5041 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
5043 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
5044 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5045 ffd1d5e5 2018-06-23 stsp return error;
5048 ce5b7c56 2019-07-09 stsp static void
5049 ce5b7c56 2019-07-09 stsp list_commands(void)
5053 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
5054 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
5055 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
5056 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->name);
5058 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
5061 4ed7e80c 2018-05-20 stsp __dead static void
5062 ce5b7c56 2019-07-09 stsp usage(int hflag)
5064 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
5065 53ccebc2 2019-07-30 stsp getprogname());
5066 ce5b7c56 2019-07-09 stsp if (hflag)
5067 ce5b7c56 2019-07-09 stsp list_commands();
5071 c2301be8 2018-04-30 stsp static char **
5072 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
5074 c2301be8 2018-04-30 stsp char **argv;
5075 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
5077 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
5078 c2301be8 2018-04-30 stsp if (argv == NULL)
5079 c2301be8 2018-04-30 stsp err(1, "calloc");
5080 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
5081 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
5082 e10c916e 2019-09-15 hiltjo err(1, "strdup");
5083 c2301be8 2018-04-30 stsp if (arg1) {
5084 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
5085 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
5086 e10c916e 2019-09-15 hiltjo err(1, "strdup");
5089 c2301be8 2018-04-30 stsp return argv;
5093 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
5095 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
5096 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
5097 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
5098 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
5100 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
5102 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
5103 9f7d7167 2018-04-29 stsp switch (ch) {
5105 9f7d7167 2018-04-29 stsp hflag = 1;
5108 53ccebc2 2019-07-30 stsp Vflag = 1;
5111 ce5b7c56 2019-07-09 stsp usage(hflag);
5112 9f7d7167 2018-04-29 stsp /* NOTREACHED */
5116 9f7d7167 2018-04-29 stsp argc -= optind;
5117 9f7d7167 2018-04-29 stsp argv += optind;
5118 9f7d7167 2018-04-29 stsp optind = 0;
5119 c2301be8 2018-04-30 stsp optreset = 1;
5121 53ccebc2 2019-07-30 stsp if (Vflag) {
5122 53ccebc2 2019-07-30 stsp got_version_print_str();
5126 c2301be8 2018-04-30 stsp if (argc == 0) {
5127 f29d3e89 2018-06-23 stsp if (hflag)
5128 ce5b7c56 2019-07-09 stsp usage(hflag);
5129 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
5130 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
5131 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
5136 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
5137 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
5138 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
5139 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
5140 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
5145 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
5146 d70c3147 2019-07-09 stsp fprintf(stderr, "%s: unknown command '%s'\n",
5147 3642c4c6 2019-07-09 stsp getprogname(), argv[0]);
5148 ce5b7c56 2019-07-09 stsp list_commands();
5153 3642c4c6 2019-07-09 stsp if (hflag)
5154 3642c4c6 2019-07-09 stsp cmd->cmd_usage();
5156 3642c4c6 2019-07-09 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5159 c2301be8 2018-04-30 stsp free(cmd_argv);
5160 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
5161 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);