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 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
116 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
118 15a087fe 2019-02-21 stsp int first_displayed_line;
119 15a087fe 2019-02-21 stsp int last_displayed_line;
121 15a087fe 2019-02-21 stsp int diff_context;
122 15a087fe 2019-02-21 stsp struct got_repository *repo;
123 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
125 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
126 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
129 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
131 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
132 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
133 1a76625f 2018-10-22 stsp int commits_needed;
134 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
135 1a76625f 2018-10-22 stsp struct commit_queue *commits;
136 1a76625f 2018-10-22 stsp const char *in_repo_path;
137 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
138 1a76625f 2018-10-22 stsp struct got_repository *repo;
139 1a76625f 2018-10-22 stsp int log_complete;
140 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
141 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
142 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
143 13add988 2019-10-15 stsp int *searching;
144 13add988 2019-10-15 stsp int *search_next_done;
145 13add988 2019-10-15 stsp regex_t *regex;
148 1a76625f 2018-10-22 stsp struct tog_log_view_state {
149 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
150 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
151 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
152 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
153 b01e7d3b 2018-08-04 stsp int selected;
154 b01e7d3b 2018-08-04 stsp char *in_repo_path;
155 d01904d4 2019-06-25 stsp const char *head_ref_name;
156 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
157 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
158 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
159 1a76625f 2018-10-22 stsp sig_atomic_t quit;
160 1a76625f 2018-10-22 stsp pthread_t thread;
161 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
162 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
163 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
166 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
167 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
168 e9424729 2018-08-04 stsp int nlines;
170 e9424729 2018-08-04 stsp struct tog_view *view;
171 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
175 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
176 e9424729 2018-08-04 stsp const char *path;
177 e9424729 2018-08-04 stsp struct got_repository *repo;
178 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
179 e9424729 2018-08-04 stsp int *complete;
180 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
181 fc06ba56 2019-08-22 stsp void *cancel_arg;
184 e9424729 2018-08-04 stsp struct tog_blame {
186 e9424729 2018-08-04 stsp size_t filesize;
187 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
188 6fcac457 2018-11-19 stsp int nlines;
189 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
190 e9424729 2018-08-04 stsp pthread_t thread;
191 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
192 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
193 e9424729 2018-08-04 stsp const char *path;
196 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
197 7cbe629d 2018-08-04 stsp int first_displayed_line;
198 7cbe629d 2018-08-04 stsp int last_displayed_line;
199 7cbe629d 2018-08-04 stsp int selected_line;
200 7cbe629d 2018-08-04 stsp int blame_complete;
203 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
204 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
205 e5a0f69f 2018-08-18 stsp char *path;
206 7cbe629d 2018-08-04 stsp struct got_repository *repo;
207 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
208 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
209 e9424729 2018-08-04 stsp struct tog_blame blame;
210 6c4c42e0 2019-06-24 stsp int matched_line;
213 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
214 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
215 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
216 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
217 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
218 ad80ab7b 2018-08-04 stsp int selected;
221 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
223 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
224 ad80ab7b 2018-08-04 stsp char *tree_label;
225 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
226 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
227 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
228 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
229 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
230 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
231 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
232 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
233 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
234 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
235 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
236 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
240 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
242 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
243 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
244 669b5ffa 2018-10-07 stsp * there is enough screen estate.
246 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
247 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
249 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
250 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
251 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
253 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
254 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
256 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
258 cc3c9aac 2018-08-01 stsp struct tog_view {
259 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
260 26ed57b2 2018-05-19 stsp WINDOW *window;
261 26ed57b2 2018-05-19 stsp PANEL *panel;
262 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
263 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
264 1004088d 2018-09-29 stsp int focussed;
265 669b5ffa 2018-10-07 stsp struct tog_view *parent;
266 669b5ffa 2018-10-07 stsp struct tog_view *child;
267 669b5ffa 2018-10-07 stsp int child_focussed;
269 5dc9f4bc 2018-08-04 stsp /* type-specific state */
270 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
272 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
273 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
274 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
275 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
278 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
279 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
280 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
281 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
283 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
284 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
285 60493ae3 2019-06-20 stsp int searching;
286 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
287 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
288 60493ae3 2019-06-20 stsp int search_next_done;
289 1803e47f 2019-06-22 stsp regex_t regex;
292 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
293 fb872ab2 2019-02-21 stsp struct got_object_id *, struct got_object_id *, struct tog_view *,
294 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
295 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
296 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
297 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
298 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
300 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
301 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *,
302 d01904d4 2019-06-25 stsp struct got_repository *, const char *, const char *, int);
303 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
304 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
305 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
306 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
307 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
308 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
310 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
311 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *, struct got_repository *);
312 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
313 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
314 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
315 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
316 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
317 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
319 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
320 8b473291 2019-02-21 stsp struct got_tree_object *, struct got_object_id *,
321 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
322 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
323 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
324 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
325 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
326 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
327 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
329 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
330 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
332 25791caa 2018-10-24 stsp static void
333 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
335 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
338 83baff54 2019-08-12 stsp static void
339 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
341 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
344 e5a0f69f 2018-08-18 stsp static const struct got_error *
345 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
347 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
349 669b5ffa 2018-10-07 stsp if (view->child) {
350 669b5ffa 2018-10-07 stsp view_close(view->child);
351 669b5ffa 2018-10-07 stsp view->child = NULL;
353 e5a0f69f 2018-08-18 stsp if (view->close)
354 e5a0f69f 2018-08-18 stsp err = view->close(view);
355 ea5e7bb5 2018-08-01 stsp if (view->panel)
356 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
357 ea5e7bb5 2018-08-01 stsp if (view->window)
358 ea5e7bb5 2018-08-01 stsp delwin(view->window);
359 ea5e7bb5 2018-08-01 stsp free(view);
360 e5a0f69f 2018-08-18 stsp return err;
363 ea5e7bb5 2018-08-01 stsp static struct tog_view *
364 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
365 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
367 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
369 ea5e7bb5 2018-08-01 stsp if (view == NULL)
370 ea5e7bb5 2018-08-01 stsp return NULL;
372 d6b05b5b 2018-08-04 stsp view->type = type;
373 f7d12f7e 2018-08-01 stsp view->lines = LINES;
374 f7d12f7e 2018-08-01 stsp view->cols = COLS;
375 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
376 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
377 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
378 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
379 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
380 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
381 96a765a8 2018-08-04 stsp view_close(view);
382 ea5e7bb5 2018-08-01 stsp return NULL;
384 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
385 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
386 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
387 96a765a8 2018-08-04 stsp view_close(view);
388 ea5e7bb5 2018-08-01 stsp return NULL;
391 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
392 ea5e7bb5 2018-08-01 stsp return view;
396 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
398 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
400 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
403 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
405 5c60c32a 2018-10-18 stsp static const struct got_error *
406 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
408 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
410 5c60c32a 2018-10-18 stsp view->begin_y = 0;
411 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
412 5c60c32a 2018-10-18 stsp view->nlines = LINES;
413 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
414 5c60c32a 2018-10-18 stsp view->lines = LINES;
415 5c60c32a 2018-10-18 stsp view->cols = COLS;
416 5c60c32a 2018-10-18 stsp err = view_resize(view);
418 5c60c32a 2018-10-18 stsp return err;
420 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
421 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
423 5c60c32a 2018-10-18 stsp return NULL;
426 5c60c32a 2018-10-18 stsp static const struct got_error *
427 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
429 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
431 5c60c32a 2018-10-18 stsp view->begin_x = 0;
432 5c60c32a 2018-10-18 stsp view->begin_y = 0;
433 5c60c32a 2018-10-18 stsp view->nlines = LINES;
434 5c60c32a 2018-10-18 stsp view->ncols = COLS;
435 5c60c32a 2018-10-18 stsp view->lines = LINES;
436 5c60c32a 2018-10-18 stsp view->cols = COLS;
437 5c60c32a 2018-10-18 stsp err = view_resize(view);
439 5c60c32a 2018-10-18 stsp return err;
441 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
442 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
444 5c60c32a 2018-10-18 stsp return NULL;
448 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
450 5c60c32a 2018-10-18 stsp return view->parent == NULL;
453 4d8c2215 2018-08-19 stsp static const struct got_error *
454 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
456 f7d12f7e 2018-08-01 stsp int nlines, ncols;
458 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
459 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
461 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
463 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
464 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
466 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
468 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
469 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
470 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
471 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
472 25791caa 2018-10-24 stsp wclear(view->window);
474 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
475 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
476 0cf4efb1 2018-09-29 stsp view->lines = LINES;
477 0cf4efb1 2018-09-29 stsp view->cols = COLS;
479 6e3e5d9c 2018-10-18 stsp if (view->child) {
480 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
481 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
482 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
483 5c60c32a 2018-10-18 stsp if (view->child->focussed)
484 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
486 5c60c32a 2018-10-18 stsp show_panel(view->panel);
488 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
489 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
493 5c60c32a 2018-10-18 stsp return NULL;
496 669b5ffa 2018-10-07 stsp static const struct got_error *
497 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
499 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
501 669b5ffa 2018-10-07 stsp if (view->child == NULL)
502 669b5ffa 2018-10-07 stsp return NULL;
504 669b5ffa 2018-10-07 stsp err = view_close(view->child);
505 669b5ffa 2018-10-07 stsp view->child = NULL;
506 669b5ffa 2018-10-07 stsp return err;
509 669b5ffa 2018-10-07 stsp static const struct got_error *
510 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
512 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
514 669b5ffa 2018-10-07 stsp view->child = child;
515 669b5ffa 2018-10-07 stsp child->parent = view;
516 669b5ffa 2018-10-07 stsp return err;
520 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
522 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
525 34bc9ec9 2019-02-22 stsp static void
526 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
528 25791caa 2018-10-24 stsp int cols, lines;
529 25791caa 2018-10-24 stsp struct winsize size;
531 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
532 25791caa 2018-10-24 stsp cols = 80; /* Default */
533 25791caa 2018-10-24 stsp lines = 24;
535 25791caa 2018-10-24 stsp cols = size.ws_col;
536 25791caa 2018-10-24 stsp lines = size.ws_row;
538 25791caa 2018-10-24 stsp resize_term(lines, cols);
541 2b49a8ae 2019-06-22 stsp static const struct got_error *
542 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
544 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
545 2b49a8ae 2019-06-22 stsp char pattern[1024];
547 7d049c18 2019-08-21 stsp int begin_x = 0;
549 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
550 2b49a8ae 2019-06-22 stsp return NULL;
552 7d049c18 2019-08-21 stsp if (!view_is_parent_view(view))
553 7d049c18 2019-08-21 stsp begin_x = view_split_begin_x(view->begin_x);
554 2b49a8ae 2019-06-22 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1,
555 7d049c18 2019-08-21 stsp begin_x, "/");
556 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
558 2b49a8ae 2019-06-22 stsp nocbreak();
560 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
563 2b49a8ae 2019-06-22 stsp if (ret == ERR)
564 2b49a8ae 2019-06-22 stsp return NULL;
566 2b49a8ae 2019-06-22 stsp if (view->searching) {
567 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
568 2b49a8ae 2019-06-22 stsp view->searching = 0;
571 2b49a8ae 2019-06-22 stsp if (regcomp(&view->regex, pattern,
572 f5daf9b1 2019-06-24 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
573 7c32bd05 2019-06-22 stsp err = view->search_start(view);
575 7c32bd05 2019-06-22 stsp regfree(&view->regex);
576 7c32bd05 2019-06-22 stsp return err;
578 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
579 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
580 2b49a8ae 2019-06-22 stsp view->search_next(view);
583 2b49a8ae 2019-06-22 stsp return NULL;
586 0cf4efb1 2018-09-29 stsp static const struct got_error *
587 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
588 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
589 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
591 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
592 669b5ffa 2018-10-07 stsp struct tog_view *v;
593 1a76625f 2018-10-22 stsp int ch, errcode;
595 e5a0f69f 2018-08-18 stsp *new = NULL;
596 e5a0f69f 2018-08-18 stsp *dead = NULL;
597 0cf4efb1 2018-09-29 stsp *focus = NULL;
599 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
600 60493ae3 2019-06-20 stsp errcode = pthread_mutex_unlock(&tog_mutex);
601 60493ae3 2019-06-20 stsp if (errcode)
602 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
603 60493ae3 2019-06-20 stsp "pthread_mutex_unlock");
604 60493ae3 2019-06-20 stsp pthread_yield();
605 60493ae3 2019-06-20 stsp errcode = pthread_mutex_lock(&tog_mutex);
606 60493ae3 2019-06-20 stsp if (errcode)
607 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
608 60493ae3 2019-06-20 stsp "pthread_mutex_lock");
609 60493ae3 2019-06-20 stsp view->search_next(view);
610 60493ae3 2019-06-20 stsp return NULL;
613 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
614 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
615 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
616 1a76625f 2018-10-22 stsp if (errcode)
617 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
618 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
619 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
620 1a76625f 2018-10-22 stsp if (errcode)
621 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
622 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
624 25791caa 2018-10-24 stsp if (tog_sigwinch_received) {
625 25791caa 2018-10-24 stsp tog_resizeterm();
626 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
627 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
628 25791caa 2018-10-24 stsp err = view_resize(v);
630 25791caa 2018-10-24 stsp return err;
631 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
633 25791caa 2018-10-24 stsp return err;
637 e5a0f69f 2018-08-18 stsp switch (ch) {
641 1e37a5c2 2019-05-12 jcs if (view->child) {
642 1e37a5c2 2019-05-12 jcs *focus = view->child;
643 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
644 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
645 1e37a5c2 2019-05-12 jcs *focus = view->parent;
646 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
650 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
651 1e37a5c2 2019-05-12 jcs *dead = view;
657 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
658 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
660 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
661 669b5ffa 2018-10-07 stsp *focus = view->child;
662 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
663 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
665 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
668 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
669 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
671 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
672 1e37a5c2 2019-05-12 jcs *focus = view;
673 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
674 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
676 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
680 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
681 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
684 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
687 60493ae3 2019-06-20 stsp if (view->search_start)
688 2b49a8ae 2019-06-22 stsp view_search_start(view);
690 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
694 60493ae3 2019-06-20 stsp if (view->search_next && view->searching) {
695 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
696 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
697 60493ae3 2019-06-20 stsp view->search_next_done = 0;
698 60493ae3 2019-06-20 stsp view->search_next(view);
700 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
703 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
707 e5a0f69f 2018-08-18 stsp return err;
711 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
713 0cf4efb1 2018-09-29 stsp PANEL *panel;
714 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
716 669b5ffa 2018-10-07 stsp if (view->parent)
717 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
719 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
720 0cf4efb1 2018-09-29 stsp if (panel == NULL)
723 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
724 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
725 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
729 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
731 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
732 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
734 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
736 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
739 669b5ffa 2018-10-07 stsp return view->focussed;
742 bcbd79e2 2018-08-19 stsp static const struct got_error *
743 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
745 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
746 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
747 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
748 fd823528 2018-10-22 stsp int fast_refresh = 10;
749 1a76625f 2018-10-22 stsp int done = 0, errcode;
751 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
752 1a76625f 2018-10-22 stsp if (errcode)
753 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
755 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
756 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
758 a81bf10d 2018-09-29 stsp main_view = view;
759 1004088d 2018-09-29 stsp view->focussed = 1;
760 878940b7 2018-09-29 stsp err = view->show(view);
762 0cf4efb1 2018-09-29 stsp return err;
763 0cf4efb1 2018-09-29 stsp update_panels();
764 0cf4efb1 2018-09-29 stsp doupdate();
765 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
766 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
767 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
768 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
770 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
771 e4197bf9 2018-08-18 stsp view, &views);
774 e5a0f69f 2018-08-18 stsp if (dead_view) {
775 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
777 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
778 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
779 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
780 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
781 669b5ffa 2018-10-07 stsp prev = view->parent;
783 669b5ffa 2018-10-07 stsp if (dead_view->parent)
784 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
786 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
788 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
789 d01904d4 2019-06-25 stsp if (err || (dead_view == main_view && new_view == NULL))
792 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
793 0cf4efb1 2018-09-29 stsp if (focus_view)
794 0cf4efb1 2018-09-29 stsp view = focus_view;
795 669b5ffa 2018-10-07 stsp else if (prev)
796 669b5ffa 2018-10-07 stsp view = prev;
797 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
798 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
799 0cf4efb1 2018-09-29 stsp tog_view_list_head);
801 0cf4efb1 2018-09-29 stsp view = NULL;
802 669b5ffa 2018-10-07 stsp if (view) {
803 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
804 669b5ffa 2018-10-07 stsp focus_view = view->child;
806 669b5ffa 2018-10-07 stsp focus_view = view;
810 bcbd79e2 2018-08-19 stsp if (new_view) {
811 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
812 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
813 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
814 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
816 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
817 86c66b02 2018-10-18 stsp err = view_close(v);
822 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
823 fed7eaa8 2018-10-24 stsp view = new_view;
824 878940b7 2018-09-29 stsp if (focus_view == NULL)
825 878940b7 2018-09-29 stsp focus_view = new_view;
827 0cf4efb1 2018-09-29 stsp if (focus_view) {
828 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
830 0cf4efb1 2018-09-29 stsp view->focussed = 0;
831 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
832 0cf4efb1 2018-09-29 stsp view = focus_view;
833 878940b7 2018-09-29 stsp if (new_view)
834 878940b7 2018-09-29 stsp show_panel(new_view->panel);
835 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
836 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
838 669b5ffa 2018-10-07 stsp if (view) {
839 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
840 758194b5 2018-10-24 stsp view->focussed = 1;
841 758194b5 2018-10-24 stsp show_panel(view->panel);
842 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
843 758194b5 2018-10-24 stsp show_panel(view->child->panel);
844 1a76625f 2018-10-22 stsp focus_view = view;
846 669b5ffa 2018-10-07 stsp if (view->parent) {
847 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
851 669b5ffa 2018-10-07 stsp err = view->show(view);
854 669b5ffa 2018-10-07 stsp if (view->child) {
855 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
859 1a76625f 2018-10-22 stsp update_panels();
860 1a76625f 2018-10-22 stsp doupdate();
864 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
865 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
866 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
867 e5a0f69f 2018-08-18 stsp view_close(view);
870 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
871 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
872 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
874 e5a0f69f 2018-08-18 stsp return err;
877 4ed7e80c 2018-05-20 stsp __dead static void
878 9f7d7167 2018-04-29 stsp usage_log(void)
881 c70c5802 2018-08-01 stsp fprintf(stderr,
882 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
883 9f7d7167 2018-04-29 stsp getprogname());
887 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
888 80ddbec8 2018-04-29 stsp static const struct got_error *
889 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
891 00dfcb92 2018-06-11 stsp char *vis = NULL;
892 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
894 963b370f 2018-05-20 stsp *ws = NULL;
895 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
896 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
897 00dfcb92 2018-06-11 stsp int vislen;
898 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
899 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
901 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
902 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
904 00dfcb92 2018-06-11 stsp return err;
905 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
906 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
907 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
912 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
913 a7f50699 2018-06-11 stsp if (*ws == NULL) {
914 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
918 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
919 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
924 963b370f 2018-05-20 stsp *ws = NULL;
927 963b370f 2018-05-20 stsp return err;
930 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
931 963b370f 2018-05-20 stsp static const struct got_error *
932 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
933 27a741e5 2019-09-11 stsp int col_tab_align)
935 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
936 963b370f 2018-05-20 stsp int cols = 0;
937 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
938 963b370f 2018-05-20 stsp size_t wlen;
941 963b370f 2018-05-20 stsp *wlinep = NULL;
942 b700b5d6 2018-07-10 stsp *widthp = 0;
944 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
946 963b370f 2018-05-20 stsp return err;
949 27a741e5 2019-09-11 stsp while (i < wlen) {
950 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
952 27a741e5 2019-09-11 stsp if (width == 0) {
957 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
958 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
960 27a741e5 2019-09-11 stsp cols += width;
962 27a741e5 2019-09-11 stsp } else if (width == -1) {
963 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
964 27a741e5 2019-09-11 stsp width = TABSIZE -
965 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
966 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
968 27a741e5 2019-09-11 stsp cols += width;
972 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
976 963b370f 2018-05-20 stsp wline[i] = L'\0';
977 b700b5d6 2018-07-10 stsp if (widthp)
978 b700b5d6 2018-07-10 stsp *widthp = cols;
981 963b370f 2018-05-20 stsp free(wline);
983 963b370f 2018-05-20 stsp *wlinep = wline;
984 963b370f 2018-05-20 stsp return err;
987 8b473291 2019-02-21 stsp static const struct got_error*
988 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
989 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
991 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
992 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
994 8b473291 2019-02-21 stsp const char *name;
996 8b473291 2019-02-21 stsp *refs_str = NULL;
998 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
999 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1002 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1003 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1005 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1006 8b473291 2019-02-21 stsp name += 5;
1007 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1009 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1010 8b473291 2019-02-21 stsp name += 6;
1011 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
1012 8b473291 2019-02-21 stsp name += 8;
1013 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1014 52b5abe1 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1015 5d844a1e 2019-08-13 stsp if (err) {
1016 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1018 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1019 5d844a1e 2019-08-13 stsp err = NULL;
1020 5d844a1e 2019-08-13 stsp tag = NULL;
1023 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1024 52b5abe1 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1026 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1027 52b5abe1 2019-08-13 stsp if (cmp != 0)
1029 8b473291 2019-02-21 stsp s = *refs_str;
1030 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1031 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1032 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1034 8b473291 2019-02-21 stsp *refs_str = NULL;
1040 8b473291 2019-02-21 stsp return err;
1043 963b370f 2018-05-20 stsp static const struct got_error *
1044 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1045 27a741e5 2019-09-11 stsp int col_tab_align)
1047 5813d178 2019-03-09 stsp char *smallerthan, *at;
1049 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1050 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1051 5813d178 2019-03-09 stsp author = smallerthan + 1;
1052 5813d178 2019-03-09 stsp at = strchr(author, '@');
1054 5813d178 2019-03-09 stsp *at = '\0';
1055 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1058 5813d178 2019-03-09 stsp static const struct got_error *
1059 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1060 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
1061 27a741e5 2019-09-11 stsp const size_t date_display_cols, int author_display_cols)
1063 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1064 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1065 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1066 5813d178 2019-03-09 stsp char *author = NULL;
1067 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1068 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1069 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1070 bb737323 2018-05-20 stsp int col, limit;
1071 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1072 ccb26ccd 2018-11-05 stsp struct tm tm;
1073 45d799e2 2018-12-23 stsp time_t committer_time;
1075 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1076 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1077 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1078 ccb26ccd 2018-11-05 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1079 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1080 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1082 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1083 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1085 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1086 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1087 27a741e5 2019-09-11 stsp col = limit;
1088 b39d25c7 2018-07-10 stsp if (col > avail)
1089 b39d25c7 2018-07-10 stsp goto done;
1091 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1092 5813d178 2019-03-09 stsp if (author == NULL) {
1093 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1094 80ddbec8 2018-04-29 stsp goto done;
1096 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1098 bb737323 2018-05-20 stsp goto done;
1099 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1100 bb737323 2018-05-20 stsp col += author_width;
1101 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1102 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1104 bb737323 2018-05-20 stsp author_width++;
1106 9c2eaf34 2018-05-20 stsp if (col > avail)
1107 9c2eaf34 2018-05-20 stsp goto done;
1109 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1111 6d9fbc00 2018-04-29 stsp goto done;
1112 bb737323 2018-05-20 stsp logmsg = logmsg0;
1113 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1115 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1116 bb737323 2018-05-20 stsp if (newline)
1117 bb737323 2018-05-20 stsp *newline = '\0';
1118 bb737323 2018-05-20 stsp limit = avail - col;
1119 27a741e5 2019-09-11 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, 0);
1121 bb737323 2018-05-20 stsp goto done;
1122 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1123 bb737323 2018-05-20 stsp col += logmsg_width;
1124 27a741e5 2019-09-11 stsp while (col < avail) {
1125 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1129 80ddbec8 2018-04-29 stsp free(logmsg0);
1130 bb737323 2018-05-20 stsp free(wlogmsg);
1131 5813d178 2019-03-09 stsp free(author);
1132 bb737323 2018-05-20 stsp free(wauthor);
1133 80ddbec8 2018-04-29 stsp free(line);
1134 80ddbec8 2018-04-29 stsp return err;
1137 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1138 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1139 899d86c2 2018-05-10 stsp struct got_object_id *id)
1141 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1143 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1144 80ddbec8 2018-04-29 stsp if (entry == NULL)
1145 899d86c2 2018-05-10 stsp return NULL;
1147 899d86c2 2018-05-10 stsp entry->id = id;
1148 99db9666 2018-05-07 stsp entry->commit = commit;
1149 899d86c2 2018-05-10 stsp return entry;
1152 99db9666 2018-05-07 stsp static void
1153 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1155 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1157 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1158 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1159 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1160 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1161 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1162 99db9666 2018-05-07 stsp free(entry);
1165 99db9666 2018-05-07 stsp static void
1166 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1168 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1169 99db9666 2018-05-07 stsp pop_commit(commits);
1172 c4972b91 2018-05-07 stsp static const struct got_error *
1173 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1174 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1176 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1177 13add988 2019-10-15 stsp regmatch_t regmatch;
1178 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1180 13add988 2019-10-15 stsp *have_match = 0;
1182 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1184 13add988 2019-10-15 stsp return err;
1186 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1188 13add988 2019-10-15 stsp goto done;
1190 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1191 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1192 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1193 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1194 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1195 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1196 13add988 2019-10-15 stsp *have_match = 1;
1198 13add988 2019-10-15 stsp free(id_str);
1199 13add988 2019-10-15 stsp free(logmsg);
1200 13add988 2019-10-15 stsp return err;
1203 13add988 2019-10-15 stsp static const struct got_error *
1204 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1205 13add988 2019-10-15 stsp int minqueue, struct got_repository *repo, const char *path,
1206 13add988 2019-10-15 stsp int *searching, int *search_next_done, regex_t *regex)
1208 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1209 13add988 2019-10-15 stsp int nqueued = 0, have_match = 0;
1212 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1213 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1214 1a76625f 2018-10-22 stsp * while updating the display.
1216 13add988 2019-10-15 stsp while (nqueued < minqueue ||
1217 13add988 2019-10-15 stsp (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1218 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1219 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1220 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1221 1a76625f 2018-10-22 stsp int errcode;
1223 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1224 9ba79e04 2018-06-11 stsp if (err) {
1225 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1227 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
1228 6fb7cd11 2019-08-22 stsp minqueue, repo, NULL, NULL);
1230 ecb28ae0 2018-07-16 stsp return err;
1234 ecb28ae0 2018-07-16 stsp if (id == NULL)
1237 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1240 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1241 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1242 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1246 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1247 1a76625f 2018-10-22 stsp if (errcode) {
1248 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1249 13add988 2019-10-15 stsp "pthread_mutex_lock");
1253 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1254 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1255 ecb28ae0 2018-07-16 stsp nqueued++;
1256 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1258 13add988 2019-10-15 stsp if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1259 13add988 2019-10-15 stsp err = match_commit(&have_match, id, commit, regex);
1260 13add988 2019-10-15 stsp if (err) {
1261 13add988 2019-10-15 stsp pthread_mutex_lock(&tog_mutex);
1266 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1267 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1268 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1269 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1271 13add988 2019-10-15 stsp if (have_match)
1275 9ba79e04 2018-06-11 stsp return err;
1278 99db9666 2018-05-07 stsp static const struct got_error *
1279 19e70ad6 2019-05-14 stsp get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1280 19e70ad6 2019-05-14 stsp struct got_repository *repo)
1282 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1283 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1285 9ba79e04 2018-06-11 stsp *head_id = NULL;
1287 19e70ad6 2019-05-14 stsp err = got_ref_open(&head_ref, repo, branch_name, 0);
1289 99db9666 2018-05-07 stsp return err;
1291 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1292 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1293 9ba79e04 2018-06-11 stsp if (err) {
1294 9ba79e04 2018-06-11 stsp *head_id = NULL;
1295 99db9666 2018-05-07 stsp return err;
1298 9ba79e04 2018-06-11 stsp return NULL;
1301 0553a4e3 2018-04-30 stsp static const struct got_error *
1302 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1303 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1304 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1305 8b473291 2019-02-21 stsp struct got_reflist_head *refs, const char *path, int commits_needed)
1307 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1308 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1309 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1310 60493ae3 2019-06-20 stsp int width;
1311 60493ae3 2019-06-20 stsp int ncommits, author_cols = 10;
1312 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1313 8b473291 2019-02-21 stsp char *refs_str = NULL;
1314 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1315 27a741e5 2019-09-11 stsp static const size_t date_display_cols = 9;
1317 e0d42f60 2018-07-22 stsp entry = first;
1318 e0d42f60 2018-07-22 stsp ncommits = 0;
1319 e0d42f60 2018-07-22 stsp while (entry) {
1320 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1321 e0d42f60 2018-07-22 stsp *selected = entry;
1324 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1325 e0d42f60 2018-07-22 stsp ncommits++;
1328 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1329 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1331 ecb28ae0 2018-07-16 stsp return err;
1332 8b473291 2019-02-21 stsp if (refs) {
1333 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, (*selected)->id,
1336 8b473291 2019-02-21 stsp goto done;
1340 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1341 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1343 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1344 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1345 60493ae3 2019-06-20 stsp commits_needed > 0 ?
1346 60493ae3 2019-06-20 stsp (view->searching && view->search_next_done == 0
1347 60493ae3 2019-06-20 stsp ? "searching..." : "loading... ") :
1348 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1349 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1350 8b473291 2019-02-21 stsp goto done;
1353 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1354 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1355 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1356 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1357 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1358 1a76625f 2018-10-22 stsp header = NULL;
1359 1a76625f 2018-10-22 stsp goto done;
1361 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1362 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1363 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1364 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1365 1a76625f 2018-10-22 stsp header = NULL;
1366 1a76625f 2018-10-22 stsp goto done;
1368 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1370 1a76625f 2018-10-22 stsp goto done;
1372 2814baeb 2018-08-01 stsp werase(view->window);
1374 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1375 a3404814 2018-09-02 stsp wstandout(view->window);
1376 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1377 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1378 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1381 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1382 a3404814 2018-09-02 stsp wstandend(view->window);
1383 ecb28ae0 2018-07-16 stsp free(wline);
1384 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1385 1a76625f 2018-10-22 stsp goto done;
1387 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1388 899d86c2 2018-05-10 stsp entry = first;
1389 5813d178 2019-03-09 stsp ncommits = 0;
1390 5813d178 2019-03-09 stsp while (entry) {
1391 5813d178 2019-03-09 stsp char *author;
1392 5813d178 2019-03-09 stsp wchar_t *wauthor;
1393 5813d178 2019-03-09 stsp int width;
1394 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1396 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1397 5813d178 2019-03-09 stsp if (author == NULL) {
1398 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1399 5813d178 2019-03-09 stsp goto done;
1401 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1402 27a741e5 2019-09-11 stsp date_display_cols);
1403 5813d178 2019-03-09 stsp if (author_cols < width)
1404 5813d178 2019-03-09 stsp author_cols = width;
1405 5813d178 2019-03-09 stsp free(wauthor);
1406 5813d178 2019-03-09 stsp free(author);
1407 7ca04879 2019-10-19 stsp ncommits++;
1408 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1411 5813d178 2019-03-09 stsp entry = first;
1412 899d86c2 2018-05-10 stsp *last = first;
1413 867c6645 2018-07-10 stsp ncommits = 0;
1414 899d86c2 2018-05-10 stsp while (entry) {
1415 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1417 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1418 2814baeb 2018-08-01 stsp wstandout(view->window);
1419 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1420 27a741e5 2019-09-11 stsp date_display_cols, author_cols);
1421 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1422 2814baeb 2018-08-01 stsp wstandend(view->window);
1424 60493ae3 2019-06-20 stsp goto done;
1425 0553a4e3 2018-04-30 stsp ncommits++;
1426 899d86c2 2018-05-10 stsp *last = entry;
1427 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1430 1a57306a 2018-09-02 stsp view_vborder(view);
1432 1a76625f 2018-10-22 stsp free(id_str);
1433 8b473291 2019-02-21 stsp free(refs_str);
1434 1a76625f 2018-10-22 stsp free(ncommits_str);
1435 1a76625f 2018-10-22 stsp free(header);
1436 80ddbec8 2018-04-29 stsp return err;
1439 07b55e75 2018-05-10 stsp static void
1440 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1441 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1442 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1444 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1445 07b55e75 2018-05-10 stsp int nscrolled = 0;
1447 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1448 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1451 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1452 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1453 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1454 07b55e75 2018-05-10 stsp if (entry) {
1455 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1456 07b55e75 2018-05-10 stsp nscrolled++;
1461 aa075928 2018-05-10 stsp static const struct got_error *
1462 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1463 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1465 5e224a3e 2019-02-22 stsp int errcode;
1466 8a42fca8 2019-02-22 stsp int max_wait = 20;
1468 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1470 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1471 5e224a3e 2019-02-22 stsp if (*log_complete)
1474 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1475 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1476 7aafa0d1 2019-02-22 stsp if (errcode)
1477 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1478 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1479 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1480 7aafa0d1 2019-02-22 stsp if (errcode)
1481 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1482 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1483 7aafa0d1 2019-02-22 stsp pthread_yield();
1484 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1485 7aafa0d1 2019-02-22 stsp if (errcode)
1486 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1487 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1489 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1491 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1492 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1493 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1494 5e224a3e 2019-02-22 stsp * with few commits in history.
1496 7aafa0d1 2019-02-22 stsp return NULL;
1500 5e224a3e 2019-02-22 stsp return NULL;
1503 5e224a3e 2019-02-22 stsp static const struct got_error *
1504 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1505 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1506 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1507 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1508 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1510 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1511 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1512 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1514 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1515 5e224a3e 2019-02-22 stsp return NULL;
1517 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1518 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1520 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1521 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1523 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1524 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1525 5e224a3e 2019-02-22 stsp need_commits);
1527 5e224a3e 2019-02-22 stsp return err;
1531 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1532 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1535 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1537 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1538 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1540 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1541 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1543 dd0a52c1 2018-05-20 stsp return err;
1546 cd0acaa7 2018-05-20 stsp static const struct got_error *
1547 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1548 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1549 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1550 8b473291 2019-02-21 stsp struct got_repository *repo)
1552 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1553 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1554 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1556 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1557 15a94983 2018-12-23 stsp if (diff_view == NULL)
1558 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1560 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1561 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1562 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1563 e5a0f69f 2018-08-18 stsp if (err == NULL)
1564 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1565 cd0acaa7 2018-05-20 stsp return err;
1568 80ddbec8 2018-04-29 stsp static const struct got_error *
1569 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1570 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1572 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1574 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1575 941e9f74 2019-05-21 stsp if (parent == NULL)
1576 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1578 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1579 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1580 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1581 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1582 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1583 941e9f74 2019-05-21 stsp s->tree = subtree;
1584 941e9f74 2019-05-21 stsp s->entries = got_object_tree_get_entries(s->tree);
1585 941e9f74 2019-05-21 stsp s->selected = 0;
1586 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1587 941e9f74 2019-05-21 stsp return NULL;
1591 941e9f74 2019-05-21 stsp static const struct got_error *
1592 941e9f74 2019-05-21 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1593 941e9f74 2019-05-21 stsp struct commit_queue_entry *entry, const char *path,
1594 941e9f74 2019-05-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
1596 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1597 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1598 941e9f74 2019-05-21 stsp struct got_tree_entry *te;
1599 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s;
1600 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1601 b03c880f 2019-05-21 stsp char *slash, *subpath = NULL;
1602 941e9f74 2019-05-21 stsp const char *p;
1604 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1605 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1607 9343a5fb 2018-06-23 stsp return err;
1609 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1610 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1611 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1613 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1614 941e9f74 2019-05-21 stsp if (err) {
1615 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1616 941e9f74 2019-05-21 stsp return err;
1618 941e9f74 2019-05-21 stsp s = &tree_view->state.tree;
1620 941e9f74 2019-05-21 stsp *new_view = tree_view;
1622 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1624 8d0fe45a 2019-08-12 stsp while (p[0] == '/')
1626 941e9f74 2019-05-21 stsp while (*p) {
1627 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1629 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1630 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1631 941e9f74 2019-05-21 stsp if (slash == NULL)
1632 941e9f74 2019-05-21 stsp slash = strchr(p, '\0');
1633 941e9f74 2019-05-21 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1634 941e9f74 2019-05-21 stsp if (strncmp(p, te->name, slash - p) == 0) {
1635 941e9f74 2019-05-21 stsp s->selected_entry = te;
1638 b03c880f 2019-05-21 stsp s->selected++;
1640 941e9f74 2019-05-21 stsp if (s->selected_entry == NULL) {
1641 941e9f74 2019-05-21 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1644 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1645 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1647 67409a31 2019-05-24 stsp if (!S_ISDIR(s->selected_entry->mode)) {
1648 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1649 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1650 67409a31 2019-05-24 stsp s->selected = 0;
1654 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1655 941e9f74 2019-05-21 stsp if (slash)
1656 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1658 941e9f74 2019-05-21 stsp subpath = strdup(path);
1659 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1660 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1664 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1669 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1670 941e9f74 2019-05-21 stsp free(tree_id);
1674 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1675 941e9f74 2019-05-21 stsp if (err) {
1676 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1679 941e9f74 2019-05-21 stsp if (slash == NULL)
1681 941e9f74 2019-05-21 stsp free(subpath);
1682 941e9f74 2019-05-21 stsp subpath = NULL;
1683 941e9f74 2019-05-21 stsp p = slash;
1686 941e9f74 2019-05-21 stsp free(subpath);
1687 1a76625f 2018-10-22 stsp return err;
1690 1a76625f 2018-10-22 stsp static void *
1691 1a76625f 2018-10-22 stsp log_thread(void *arg)
1693 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1694 1a76625f 2018-10-22 stsp int errcode = 0;
1695 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1696 1a76625f 2018-10-22 stsp int done = 0;
1698 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1699 6fb7cd11 2019-08-22 stsp NULL, NULL);
1701 1a76625f 2018-10-22 stsp return (void *)err;
1703 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1704 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1705 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1706 13add988 2019-10-15 stsp a->regex);
1707 1a76625f 2018-10-22 stsp if (err) {
1708 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1709 1a76625f 2018-10-22 stsp return (void *)err;
1710 1a76625f 2018-10-22 stsp err = NULL;
1712 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1713 1a76625f 2018-10-22 stsp a->commits_needed--;
1715 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1716 3abe8080 2019-04-10 stsp if (errcode) {
1717 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1718 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1720 3abe8080 2019-04-10 stsp } else if (*a->quit)
1722 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1723 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1724 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1725 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1729 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1730 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1731 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1732 1a76625f 2018-10-22 stsp &tog_mutex);
1733 1a76625f 2018-10-22 stsp if (errcode)
1734 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1735 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1738 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1739 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1740 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1741 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1743 3abe8080 2019-04-10 stsp a->log_complete = 1;
1744 1a76625f 2018-10-22 stsp return (void *)err;
1747 1a76625f 2018-10-22 stsp static const struct got_error *
1748 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1750 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1751 1a76625f 2018-10-22 stsp int errcode;
1753 1a76625f 2018-10-22 stsp if (s->thread) {
1754 1a76625f 2018-10-22 stsp s->quit = 1;
1755 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1756 1a76625f 2018-10-22 stsp if (errcode)
1757 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1758 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1759 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1760 1a76625f 2018-10-22 stsp if (errcode)
1761 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1762 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1763 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1764 1a76625f 2018-10-22 stsp if (errcode)
1765 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1766 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1767 1a76625f 2018-10-22 stsp if (errcode)
1768 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1769 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1770 1a76625f 2018-10-22 stsp s->thread = NULL;
1773 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1774 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1775 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1777 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1778 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1779 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1782 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1783 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1784 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1787 9343a5fb 2018-06-23 stsp return err;
1790 9343a5fb 2018-06-23 stsp static const struct got_error *
1791 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1793 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1794 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1796 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1797 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1798 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1799 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1800 1a76625f 2018-10-22 stsp free(s->start_id);
1801 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1802 1a76625f 2018-10-22 stsp return err;
1805 1a76625f 2018-10-22 stsp static const struct got_error *
1806 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
1808 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1810 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
1811 96e2b566 2019-07-08 stsp s->search_entry = NULL;
1812 60493ae3 2019-06-20 stsp return NULL;
1815 60493ae3 2019-06-20 stsp static const struct got_error *
1816 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
1818 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
1819 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1820 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
1822 60493ae3 2019-06-20 stsp if (!view->searching) {
1823 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1824 60493ae3 2019-06-20 stsp return NULL;
1827 96e2b566 2019-07-08 stsp if (s->search_entry) {
1828 13add988 2019-10-15 stsp int errcode, ch;
1829 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1830 13add988 2019-10-15 stsp if (errcode)
1831 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
1832 13add988 2019-10-15 stsp "pthread_mutex_unlock");
1833 13add988 2019-10-15 stsp ch = wgetch(view->window);
1834 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
1835 13add988 2019-10-15 stsp if (errcode)
1836 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
1837 13add988 2019-10-15 stsp "pthread_mutex_lock");
1838 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
1839 678cbce5 2019-07-28 stsp view->search_next_done = 1;
1840 678cbce5 2019-07-28 stsp return NULL;
1842 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
1843 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
1845 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
1846 96e2b566 2019-07-08 stsp commit_queue_head, entry);
1847 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
1848 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1849 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
1851 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
1852 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
1854 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1855 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
1857 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1860 60493ae3 2019-06-20 stsp while (1) {
1861 13add988 2019-10-15 stsp int have_match = 0;
1863 60493ae3 2019-06-20 stsp if (entry == NULL) {
1864 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
1865 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
1866 f801134a 2019-06-25 stsp view->search_next_done = 1;
1867 f801134a 2019-06-25 stsp return NULL;
1870 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
1871 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
1872 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
1874 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
1875 57b33b64 2019-07-08 stsp return trigger_log_thread(1,
1876 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
1877 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
1878 f801134a 2019-06-25 stsp &s->thread_args.need_commits);
1881 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
1882 13add988 2019-10-15 stsp &view->regex);
1885 13add988 2019-10-15 stsp if (have_match) {
1886 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1887 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
1891 96e2b566 2019-07-08 stsp s->search_entry = entry;
1892 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1893 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
1895 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1898 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
1899 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
1900 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
1901 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1903 60493ae3 2019-06-20 stsp return err;
1906 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
1907 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1909 60493ae3 2019-06-20 stsp return err;
1914 96e2b566 2019-07-08 stsp s->search_entry = NULL;
1916 60493ae3 2019-06-20 stsp return NULL;
1919 60493ae3 2019-06-20 stsp static const struct got_error *
1920 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1921 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
1922 d01904d4 2019-06-25 stsp const char *head_ref_name, const char *path, int check_disk)
1924 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1925 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1926 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1927 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1928 1a76625f 2018-10-22 stsp int errcode;
1930 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1931 ecb28ae0 2018-07-16 stsp if (err != NULL)
1932 ecb28ae0 2018-07-16 stsp goto done;
1934 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1935 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1936 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1938 8b473291 2019-02-21 stsp s->refs = refs;
1939 fb2756b9 2018-08-04 stsp s->repo = repo;
1940 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
1941 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1942 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1943 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1944 5036bf37 2018-09-24 stsp goto done;
1947 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1948 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1949 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1950 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
1951 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
1953 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
1955 1a76625f 2018-10-22 stsp goto done;
1956 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1957 1a76625f 2018-10-22 stsp 0, thread_repo);
1959 1a76625f 2018-10-22 stsp goto done;
1961 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1962 1a76625f 2018-10-22 stsp if (errcode) {
1963 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
1964 1a76625f 2018-10-22 stsp goto done;
1967 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1968 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1969 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1970 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1971 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1972 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1973 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1974 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1975 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1976 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1977 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
1978 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
1979 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
1982 1a76625f 2018-10-22 stsp close_log_view(view);
1983 ba4f502b 2018-08-04 stsp return err;
1986 e5a0f69f 2018-08-18 stsp static const struct got_error *
1987 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1989 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1991 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1992 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1993 2b380cc8 2018-10-24 stsp &s->thread_args);
1994 2b380cc8 2018-10-24 stsp if (errcode)
1995 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
1998 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1999 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
2000 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
2001 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
2004 e5a0f69f 2018-08-18 stsp static const struct got_error *
2005 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2006 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2008 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2009 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2010 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2011 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2012 669b5ffa 2018-10-07 stsp int begin_x = 0;
2013 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2015 e5a0f69f 2018-08-18 stsp switch (ch) {
2017 1e37a5c2 2019-05-12 jcs s->quit = 1;
2020 1e37a5c2 2019-05-12 jcs case KEY_UP:
2023 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2025 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2026 1e37a5c2 2019-05-12 jcs s->selected--;
2028 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
2029 1144d21a 2019-06-21 stsp &s->commits);
2031 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2032 a4292ac5 2019-05-12 jcs case CTRL('b'):
2033 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2035 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
2036 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
2037 1e37a5c2 2019-05-12 jcs s->selected = 0;
2040 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
2041 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
2044 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2047 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2049 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2050 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2051 1e37a5c2 2019-05-12 jcs s->selected++;
2054 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
2055 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
2056 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
2057 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2058 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2060 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2061 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2062 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2063 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2064 1e37a5c2 2019-05-12 jcs if (first == NULL)
2066 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
2067 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
2068 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
2069 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2070 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2073 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2074 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2075 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2076 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2077 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2078 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2080 1e37a5c2 2019-05-12 jcs err = NULL;
2083 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2084 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2085 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2086 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2087 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2089 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2092 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2094 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2095 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2096 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2097 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2098 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
2101 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2102 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2104 1e37a5c2 2019-05-12 jcs return err;
2105 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2107 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2110 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2111 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2113 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2116 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2118 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2119 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2120 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2121 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2124 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2125 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2127 1e37a5c2 2019-05-12 jcs return err;
2128 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2130 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2133 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2134 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2136 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2138 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2139 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2141 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2142 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2143 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2145 1e37a5c2 2019-05-12 jcs return err;
2146 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2147 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2148 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2149 638f9024 2019-05-13 stsp return got_error_from_errno(
2150 1e37a5c2 2019-05-12 jcs "view_open");
2151 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2152 d01904d4 2019-06-25 stsp s->repo, s->head_ref_name, parent_path, 0);
2154 1e37a5c2 2019-05-12 jcs return err;;
2155 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2156 1e37a5c2 2019-05-12 jcs *new_view = lv;
2158 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2159 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2161 1e37a5c2 2019-05-12 jcs return NULL;
2164 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2165 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2167 d01904d4 2019-06-25 stsp return err;
2168 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2169 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2170 d01904d4 2019-06-25 stsp if (lv == NULL)
2171 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2172 d01904d4 2019-06-25 stsp err = get_head_commit_id(&start_id, s->head_ref_name ?
2173 d01904d4 2019-06-25 stsp s->head_ref_name : GOT_REF_HEAD, s->repo);
2174 ef129c5e 2019-08-03 stsp if (err) {
2175 ef129c5e 2019-08-03 stsp view_close(lv);
2176 d01904d4 2019-06-25 stsp return err;
2178 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2179 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2180 e6b23735 2019-10-04 stsp free(start_id);
2181 e6b23735 2019-10-04 stsp view_close(lv);
2182 e6b23735 2019-10-04 stsp return got_error_from_errno("strdup");
2184 e6b23735 2019-10-04 stsp got_ref_list_free(s->refs);
2185 e6b23735 2019-10-04 stsp err = got_ref_list(s->refs, s->repo, NULL,
2186 e6b23735 2019-10-04 stsp got_ref_cmp_by_name, NULL);
2187 e6b23735 2019-10-04 stsp if (err) {
2188 d01904d4 2019-06-25 stsp free(start_id);
2189 ef129c5e 2019-08-03 stsp view_close(lv);
2190 50284065 2019-10-04 stsp return err;
2192 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2193 d01904d4 2019-06-25 stsp s->head_ref_name, in_repo_path, 0);
2194 ef129c5e 2019-08-03 stsp if (err) {
2195 ef129c5e 2019-08-03 stsp free(start_id);
2196 ef129c5e 2019-08-03 stsp view_close(lv);
2197 d01904d4 2019-06-25 stsp return err;;
2199 d01904d4 2019-06-25 stsp *dead_view = view;
2200 d01904d4 2019-06-25 stsp *new_view = lv;
2206 80ddbec8 2018-04-29 stsp return err;
2209 4ed7e80c 2018-05-20 stsp static const struct got_error *
2210 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2212 c2db6724 2019-01-04 stsp const struct got_error *error;
2214 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2215 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2216 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2218 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2219 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2221 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2222 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2224 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2225 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2227 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2228 c2db6724 2019-01-04 stsp if (error != NULL)
2229 c2db6724 2019-01-04 stsp return error;
2231 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2232 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2234 c2db6724 2019-01-04 stsp return NULL;
2237 a915003a 2019-02-05 stsp static void
2238 a915003a 2019-02-05 stsp init_curses(void)
2240 a915003a 2019-02-05 stsp initscr();
2242 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2245 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2246 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2247 a915003a 2019-02-05 stsp curs_set(0);
2248 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2249 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2252 c2db6724 2019-01-04 stsp static const struct got_error *
2253 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2255 80ddbec8 2018-04-29 stsp const struct got_error *error;
2256 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2257 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2258 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2259 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2260 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2261 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2263 04cc582a 2018-08-01 stsp struct tog_view *view;
2265 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2267 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2268 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2269 c2db6724 2019-01-04 stsp NULL) == -1)
2270 80ddbec8 2018-04-29 stsp err(1, "pledge");
2273 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2274 80ddbec8 2018-04-29 stsp switch (ch) {
2276 80ddbec8 2018-04-29 stsp start_commit = optarg;
2279 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2280 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2281 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2285 17020d27 2019-03-07 stsp usage_log();
2286 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2290 80ddbec8 2018-04-29 stsp argc -= optind;
2291 80ddbec8 2018-04-29 stsp argv += optind;
2293 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2294 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2295 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2296 ecb28ae0 2018-07-16 stsp goto done;
2298 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2299 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2300 963f97a1 2019-03-18 stsp goto done;
2301 963f97a1 2019-03-18 stsp error = NULL;
2303 963f97a1 2019-03-18 stsp if (argc == 0) {
2304 963f97a1 2019-03-18 stsp path = strdup("");
2305 963f97a1 2019-03-18 stsp if (path == NULL) {
2306 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2307 ecb28ae0 2018-07-16 stsp goto done;
2309 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2310 963f97a1 2019-03-18 stsp if (worktree) {
2311 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2313 963f97a1 2019-03-18 stsp if (error)
2314 963f97a1 2019-03-18 stsp goto done;
2316 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2317 963f97a1 2019-03-18 stsp if (path == NULL) {
2318 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2319 963f97a1 2019-03-18 stsp goto done;
2323 963f97a1 2019-03-18 stsp usage_log();
2325 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2326 a1fbf39a 2019-08-11 stsp if (worktree)
2327 b9d7675a 2019-08-11 stsp repo_path = strdup(
2328 b9d7675a 2019-08-11 stsp got_worktree_get_repo_path(worktree));
2330 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2332 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2333 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2334 963f97a1 2019-03-18 stsp goto done;
2337 a915003a 2019-02-05 stsp init_curses();
2339 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2340 80ddbec8 2018-04-29 stsp if (error != NULL)
2341 ecb28ae0 2018-07-16 stsp goto done;
2343 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2344 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2345 c02c541e 2019-03-29 stsp if (error)
2346 c02c541e 2019-03-29 stsp goto done;
2348 15a94983 2018-12-23 stsp if (start_commit == NULL)
2349 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&start_id, worktree ?
2350 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2353 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&start_id, start_commit, repo);
2354 f2b6a97d 2019-07-15 stsp if (error) {
2355 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
2356 f2b6a97d 2019-07-15 stsp goto done;
2357 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&start_id,
2358 f2b6a97d 2019-07-15 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2361 80ddbec8 2018-04-29 stsp if (error != NULL)
2362 8b473291 2019-02-21 stsp goto done;
2364 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2365 8b473291 2019-02-21 stsp if (error)
2366 ecb28ae0 2018-07-16 stsp goto done;
2368 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2369 04cc582a 2018-08-01 stsp if (view == NULL) {
2370 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2371 04cc582a 2018-08-01 stsp goto done;
2373 2fc00ff4 2019-08-31 stsp if (worktree) {
2374 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2375 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2376 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2377 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2378 2fc00ff4 2019-08-31 stsp goto done;
2381 2fc00ff4 2019-08-31 stsp error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2383 ba4f502b 2018-08-04 stsp if (error)
2384 ba4f502b 2018-08-04 stsp goto done;
2385 2fc00ff4 2019-08-31 stsp if (worktree) {
2386 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2387 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2388 2fc00ff4 2019-08-31 stsp worktree = NULL;
2390 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2392 ecb28ae0 2018-07-16 stsp free(repo_path);
2393 ecb28ae0 2018-07-16 stsp free(cwd);
2394 ecb28ae0 2018-07-16 stsp free(path);
2395 899d86c2 2018-05-10 stsp free(start_id);
2396 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2398 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2399 ec142235 2019-03-07 stsp if (worktree)
2400 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2401 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2402 80ddbec8 2018-04-29 stsp return error;
2405 4ed7e80c 2018-05-20 stsp __dead static void
2406 9f7d7167 2018-04-29 stsp usage_diff(void)
2409 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2410 9f7d7167 2018-04-29 stsp getprogname());
2414 b304db33 2018-05-20 stsp static char *
2415 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2417 b304db33 2018-05-20 stsp char *line;
2418 b304db33 2018-05-20 stsp size_t linelen;
2419 b304db33 2018-05-20 stsp size_t lineno;
2420 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2422 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2424 b304db33 2018-05-20 stsp *len = linelen;
2425 b304db33 2018-05-20 stsp return line;
2428 4ed7e80c 2018-05-20 stsp static const struct got_error *
2429 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2430 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
2431 c3e9aa98 2019-05-13 jcs char *header)
2433 61e69b96 2018-05-20 stsp const struct got_error *err;
2434 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2435 b304db33 2018-05-20 stsp char *line;
2436 b304db33 2018-05-20 stsp size_t len;
2437 61e69b96 2018-05-20 stsp wchar_t *wline;
2438 e0b650dd 2018-05-20 stsp int width;
2440 26ed57b2 2018-05-19 stsp rewind(f);
2441 f7d12f7e 2018-08-01 stsp werase(view->window);
2443 a3404814 2018-09-02 stsp if (header) {
2444 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
2445 a3404814 2018-09-02 stsp if (err) {
2446 a3404814 2018-09-02 stsp return err;
2449 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2450 a3404814 2018-09-02 stsp wstandout(view->window);
2451 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2452 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2453 a3404814 2018-09-02 stsp wstandend(view->window);
2454 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2455 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2457 a3404814 2018-09-02 stsp if (max_lines <= 1)
2458 a3404814 2018-09-02 stsp return NULL;
2459 a3404814 2018-09-02 stsp max_lines--;
2463 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2464 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2465 26ed57b2 2018-05-19 stsp if (line == NULL) {
2469 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2470 26ed57b2 2018-05-19 stsp free(line);
2474 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2475 61e69b96 2018-05-20 stsp if (err) {
2476 61e69b96 2018-05-20 stsp free(line);
2477 61e69b96 2018-05-20 stsp return err;
2479 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2480 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2481 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2482 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2483 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2484 26ed57b2 2018-05-19 stsp free(line);
2485 2550e4c3 2018-07-13 stsp free(wline);
2486 2550e4c3 2018-07-13 stsp wline = NULL;
2488 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2490 1a57306a 2018-09-02 stsp view_vborder(view);
2492 c3e9aa98 2019-05-13 jcs if (*eof) {
2493 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2494 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2495 c3e9aa98 2019-05-13 jcs nprinted++;
2498 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2500 c3e9aa98 2019-05-13 jcs return err;
2503 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2504 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2505 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2508 26ed57b2 2018-05-19 stsp return NULL;
2511 abd2672a 2018-12-23 stsp static char *
2512 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2514 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2515 09867e48 2019-08-13 stsp char *p, *s;
2517 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2518 09867e48 2019-08-13 stsp if (tm == NULL)
2519 09867e48 2019-08-13 stsp return NULL;
2520 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2521 09867e48 2019-08-13 stsp if (s == NULL)
2522 09867e48 2019-08-13 stsp return NULL;
2523 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2525 abd2672a 2018-12-23 stsp *p = '\0';
2529 4ed7e80c 2018-05-20 stsp static const struct got_error *
2530 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2531 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2533 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2534 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
2535 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2536 5943eee2 2019-08-13 stsp char *id_str = NULL, *logmsg = NULL;
2537 45d799e2 2018-12-23 stsp time_t committer_time;
2538 45d799e2 2018-12-23 stsp const char *author, *committer;
2539 8b473291 2019-02-21 stsp char *refs_str = NULL;
2541 8b473291 2019-02-21 stsp if (refs) {
2542 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
2544 8b473291 2019-02-21 stsp return err;
2547 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2549 abd2672a 2018-12-23 stsp return err;
2551 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2552 15a94983 2018-12-23 stsp if (err) {
2553 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2554 15a94983 2018-12-23 stsp goto done;
2557 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2558 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2559 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2560 abd2672a 2018-12-23 stsp goto done;
2562 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2563 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2564 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2565 abd2672a 2018-12-23 stsp goto done;
2567 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2568 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
2569 09867e48 2019-08-13 stsp if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2570 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2571 abd2672a 2018-12-23 stsp goto done;
2573 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2574 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2575 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2576 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2577 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2578 abd2672a 2018-12-23 stsp goto done;
2580 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2582 5943eee2 2019-08-13 stsp goto done;
2583 5943eee2 2019-08-13 stsp if (fprintf(outfile, "%s\n", logmsg) < 0) {
2584 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2585 abd2672a 2018-12-23 stsp goto done;
2588 abd2672a 2018-12-23 stsp free(id_str);
2589 5943eee2 2019-08-13 stsp free(logmsg);
2590 8b473291 2019-02-21 stsp free(refs_str);
2591 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2592 abd2672a 2018-12-23 stsp return err;
2595 abd2672a 2018-12-23 stsp static const struct got_error *
2596 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2598 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2599 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2600 15a94983 2018-12-23 stsp int obj_type;
2602 511a516b 2018-05-19 stsp f = got_opentemp();
2603 48ae06ee 2018-10-18 stsp if (f == NULL) {
2604 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2605 48ae06ee 2018-10-18 stsp goto done;
2607 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2608 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2609 fb43ecf1 2019-02-11 stsp goto done;
2613 15a94983 2018-12-23 stsp if (s->id1)
2614 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2616 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2618 15a94983 2018-12-23 stsp goto done;
2620 15a94983 2018-12-23 stsp switch (obj_type) {
2621 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2622 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2623 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2625 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2626 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2627 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2629 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2630 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2631 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2632 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2634 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2637 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2638 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2639 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2641 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2642 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2643 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2644 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2645 8b473291 2019-02-21 stsp s->repo, f);
2650 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2652 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2653 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2657 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2661 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2662 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2663 48ae06ee 2018-10-18 stsp return err;
2666 f5215bb9 2019-02-22 stsp static void
2667 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2669 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2670 f5215bb9 2019-02-22 stsp update_panels();
2671 f5215bb9 2019-02-22 stsp doupdate();
2674 48ae06ee 2018-10-18 stsp static const struct got_error *
2675 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2676 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2677 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2679 48ae06ee 2018-10-18 stsp const struct got_error *err;
2681 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2682 15a94983 2018-12-23 stsp int type1, type2;
2683 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2685 15a94983 2018-12-23 stsp return err;
2686 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2688 15a94983 2018-12-23 stsp return err;
2690 15a94983 2018-12-23 stsp if (type1 != type2)
2691 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2694 15a94983 2018-12-23 stsp if (id1) {
2695 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2696 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2697 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2699 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2701 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2702 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2703 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2704 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2705 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2707 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2708 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2709 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2710 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2711 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2712 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2713 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2715 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2716 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2717 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2719 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2720 48ae06ee 2018-10-18 stsp if (err) {
2721 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2722 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2723 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2724 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2725 48ae06ee 2018-10-18 stsp return err;
2728 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2729 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2730 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2732 5dc9f4bc 2018-08-04 stsp return NULL;
2735 e5a0f69f 2018-08-18 stsp static const struct got_error *
2736 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2738 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2740 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2741 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2742 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2743 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2744 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2745 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2746 e5a0f69f 2018-08-18 stsp return err;
2749 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2750 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2752 a3404814 2018-09-02 stsp const struct got_error *err;
2753 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2754 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2756 a3404814 2018-09-02 stsp if (s->id1) {
2757 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2759 a3404814 2018-09-02 stsp return err;
2761 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2763 a3404814 2018-09-02 stsp return err;
2765 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2766 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2767 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2768 a3404814 2018-09-02 stsp free(id_str1);
2769 a3404814 2018-09-02 stsp free(id_str2);
2770 a3404814 2018-09-02 stsp return err;
2772 a3404814 2018-09-02 stsp free(id_str1);
2773 a3404814 2018-09-02 stsp free(id_str2);
2775 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2776 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2780 15a087fe 2019-02-21 stsp static const struct got_error *
2781 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2782 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2784 d7a04538 2019-02-21 stsp const struct got_error *err;
2785 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
2786 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
2787 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
2789 15a087fe 2019-02-21 stsp free(s->id2);
2790 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2791 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2792 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2794 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2796 d7a04538 2019-02-21 stsp return err;
2797 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
2798 15a087fe 2019-02-21 stsp free(s->id1);
2799 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
2800 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2801 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
2802 15a087fe 2019-02-21 stsp return NULL;
2805 0cf4efb1 2018-09-29 stsp static const struct got_error *
2806 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2807 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2809 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2810 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2811 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
2812 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
2815 e5a0f69f 2018-08-18 stsp switch (ch) {
2817 1e37a5c2 2019-05-12 jcs case KEY_UP:
2818 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
2819 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2821 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2822 a60a9dc4 2019-05-13 jcs case CTRL('b'):
2823 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
2826 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
2827 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
2828 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2831 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2832 1e37a5c2 2019-05-12 jcs if (!s->eof)
2833 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2835 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
2836 a60a9dc4 2019-05-13 jcs case CTRL('f'):
2838 00ba99a7 2019-05-12 jcs if (s->eof)
2841 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
2842 1e37a5c2 2019-05-12 jcs char *line;
2843 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
2844 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2845 1e37a5c2 2019-05-12 jcs if (line == NULL)
2850 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
2851 1e37a5c2 2019-05-12 jcs s->diff_context--;
2852 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2853 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2857 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2858 1e37a5c2 2019-05-12 jcs s->diff_context++;
2859 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2860 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2865 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2867 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2868 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
2869 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
2870 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2873 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2878 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2882 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2883 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2885 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2886 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2890 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2892 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2894 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2895 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
2897 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
2898 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
2899 1e37a5c2 2019-05-12 jcs update_panels();
2900 1e37a5c2 2019-05-12 jcs doupdate();
2902 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
2903 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
2904 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
2905 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
2909 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2914 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
2915 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2918 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2922 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2923 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2925 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2926 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2932 bcbd79e2 2018-08-19 stsp return err;
2935 4ed7e80c 2018-05-20 stsp static const struct got_error *
2936 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2938 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2939 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2940 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2941 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2942 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2943 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2945 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2947 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2949 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2950 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2951 eb6600df 2019-01-04 stsp NULL) == -1)
2952 26ed57b2 2018-05-19 stsp err(1, "pledge");
2955 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2956 26ed57b2 2018-05-19 stsp switch (ch) {
2958 17020d27 2019-03-07 stsp usage_diff();
2959 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2963 26ed57b2 2018-05-19 stsp argc -= optind;
2964 26ed57b2 2018-05-19 stsp argv += optind;
2966 26ed57b2 2018-05-19 stsp if (argc == 0) {
2967 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2968 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2969 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2970 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2971 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2972 15a94983 2018-12-23 stsp id_str1 = argv[0];
2973 15a94983 2018-12-23 stsp id_str2 = argv[1];
2974 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2975 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2976 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2977 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2978 15a94983 2018-12-23 stsp id_str1 = argv[1];
2979 15a94983 2018-12-23 stsp id_str2 = argv[2];
2981 26ed57b2 2018-05-19 stsp usage_diff();
2983 a915003a 2019-02-05 stsp init_curses();
2985 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2986 eb6600df 2019-01-04 stsp if (error)
2987 eb6600df 2019-01-04 stsp goto done;
2989 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
2990 26ed57b2 2018-05-19 stsp if (error)
2991 26ed57b2 2018-05-19 stsp goto done;
2993 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
2994 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
2995 26ed57b2 2018-05-19 stsp if (error)
2996 26ed57b2 2018-05-19 stsp goto done;
2998 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
2999 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3000 26ed57b2 2018-05-19 stsp if (error)
3001 26ed57b2 2018-05-19 stsp goto done;
3003 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3004 8b473291 2019-02-21 stsp if (error)
3005 8b473291 2019-02-21 stsp goto done;
3007 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3008 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3009 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3010 ea5e7bb5 2018-08-01 stsp goto done;
3012 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3013 5dc9f4bc 2018-08-04 stsp if (error)
3014 5dc9f4bc 2018-08-04 stsp goto done;
3015 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3017 c02c541e 2019-03-29 stsp free(repo_path);
3019 921be706 2019-06-28 stsp got_repo_close(repo);
3020 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3021 26ed57b2 2018-05-19 stsp return error;
3024 4ed7e80c 2018-05-20 stsp __dead static void
3025 9f7d7167 2018-04-29 stsp usage_blame(void)
3028 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3029 9f7d7167 2018-04-29 stsp getprogname());
3033 84451b3e 2018-07-10 stsp struct tog_blame_line {
3034 84451b3e 2018-07-10 stsp int annotated;
3035 84451b3e 2018-07-10 stsp struct got_object_id *id;
3038 4ed7e80c 2018-05-20 stsp static const struct got_error *
3039 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3040 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
3041 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
3042 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
3044 84451b3e 2018-07-10 stsp const struct got_error *err;
3045 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3046 84451b3e 2018-07-10 stsp char *line;
3047 84451b3e 2018-07-10 stsp size_t len;
3048 84451b3e 2018-07-10 stsp wchar_t *wline;
3049 27a741e5 2019-09-11 stsp int width;
3050 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3051 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3052 ab089a2a 2018-07-12 stsp char *id_str;
3054 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
3056 ab089a2a 2018-07-12 stsp return err;
3058 84451b3e 2018-07-10 stsp rewind(f);
3059 f7d12f7e 2018-08-01 stsp werase(view->window);
3061 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3062 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3063 ab089a2a 2018-07-12 stsp free(id_str);
3064 ab089a2a 2018-07-12 stsp return err;
3067 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3068 ab089a2a 2018-07-12 stsp free(line);
3069 2550e4c3 2018-07-13 stsp line = NULL;
3071 1cae65b4 2019-09-22 stsp return err;
3072 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3073 a3404814 2018-09-02 stsp wstandout(view->window);
3074 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3075 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3076 a3404814 2018-09-02 stsp wstandend(view->window);
3077 2550e4c3 2018-07-13 stsp free(wline);
3078 2550e4c3 2018-07-13 stsp wline = NULL;
3079 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3080 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3082 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3083 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
3084 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
3085 ab089a2a 2018-07-12 stsp free(id_str);
3086 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3088 ab089a2a 2018-07-12 stsp free(id_str);
3089 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3090 3f60a8ef 2018-07-10 stsp free(line);
3091 2550e4c3 2018-07-13 stsp line = NULL;
3093 3f60a8ef 2018-07-10 stsp return err;
3094 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3095 2550e4c3 2018-07-13 stsp free(wline);
3096 2550e4c3 2018-07-13 stsp wline = NULL;
3097 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3098 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3101 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
3102 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
3103 84451b3e 2018-07-10 stsp if (line == NULL) {
3107 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
3108 84451b3e 2018-07-10 stsp free(line);
3112 27a741e5 2019-09-11 stsp if (view->ncols <= 9) {
3113 27a741e5 2019-09-11 stsp width = 9;
3114 27a741e5 2019-09-11 stsp wline = wcsdup(L"");
3115 27a741e5 2019-09-11 stsp if (wline == NULL)
3116 27a741e5 2019-09-11 stsp err = got_error_from_errno("wcsdup");
3118 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line,
3119 27a741e5 2019-09-11 stsp view->ncols - 9, 9);
3120 27a741e5 2019-09-11 stsp width += 9;
3122 84451b3e 2018-07-10 stsp if (err) {
3123 84451b3e 2018-07-10 stsp free(line);
3124 84451b3e 2018-07-10 stsp return err;
3127 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3128 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3130 8d0fe45a 2019-08-12 stsp if (nlines > 0) {
3131 8d0fe45a 2019-08-12 stsp blame_line = &lines[lineno - 1];
3132 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3133 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3134 27a741e5 2019-09-11 stsp !(view->focussed &&
3135 27a741e5 2019-09-11 stsp nprinted == selected_line - 1)) {
3136 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3137 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3138 8d0fe45a 2019-08-12 stsp char *id_str;
3139 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3140 8d0fe45a 2019-08-12 stsp if (err) {
3141 8d0fe45a 2019-08-12 stsp free(line);
3142 8d0fe45a 2019-08-12 stsp free(wline);
3143 8d0fe45a 2019-08-12 stsp return err;
3145 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3146 8d0fe45a 2019-08-12 stsp free(id_str);
3147 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3149 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3150 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3153 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3154 ee41ec32 2018-07-10 stsp prev_id = NULL;
3157 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3158 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3159 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3161 27a741e5 2019-09-11 stsp waddwstr(view->window, wline);
3162 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
3163 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
3164 84451b3e 2018-07-10 stsp if (++nprinted == 1)
3165 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
3166 84451b3e 2018-07-10 stsp free(line);
3167 2550e4c3 2018-07-13 stsp free(wline);
3168 2550e4c3 2018-07-13 stsp wline = NULL;
3170 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
3172 1a57306a 2018-09-02 stsp view_vborder(view);
3174 84451b3e 2018-07-10 stsp return NULL;
3177 84451b3e 2018-07-10 stsp static const struct got_error *
3178 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3180 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
3181 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
3182 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
3183 1a76625f 2018-10-22 stsp int errcode;
3185 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
3186 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3187 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3189 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3190 1a76625f 2018-10-22 stsp if (errcode)
3191 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3193 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3194 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3195 d68a0a7d 2018-07-10 stsp goto done;
3198 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3199 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3201 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3202 d68a0a7d 2018-07-10 stsp if (line->annotated)
3203 d68a0a7d 2018-07-10 stsp goto done;
3205 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3206 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3207 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3208 84451b3e 2018-07-10 stsp goto done;
3210 84451b3e 2018-07-10 stsp line->annotated = 1;
3212 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3213 1a76625f 2018-10-22 stsp if (errcode)
3214 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3215 84451b3e 2018-07-10 stsp return err;
3218 84451b3e 2018-07-10 stsp static void *
3219 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3221 18430de3 2018-07-10 stsp const struct got_error *err;
3222 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3223 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3224 1a76625f 2018-10-22 stsp int errcode;
3226 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
3227 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3228 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
3229 fc06ba56 2019-08-22 stsp err = NULL;
3231 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3232 1a76625f 2018-10-22 stsp if (errcode)
3233 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3234 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3236 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3237 c9beca56 2018-07-22 stsp ta->repo = NULL;
3238 c9beca56 2018-07-22 stsp *ta->complete = 1;
3240 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3241 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3242 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3244 18430de3 2018-07-10 stsp return (void *)err;
3247 245d91c1 2018-07-12 stsp static struct got_object_id *
3248 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3249 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
3251 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3253 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
3254 8d0fe45a 2019-08-12 stsp return NULL;
3256 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3257 245d91c1 2018-07-12 stsp if (!line->annotated)
3258 245d91c1 2018-07-12 stsp return NULL;
3260 245d91c1 2018-07-12 stsp return line->id;
3263 b880a918 2018-07-10 stsp static const struct got_error *
3264 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3266 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3269 245d91c1 2018-07-12 stsp if (blame->thread) {
3270 1a76625f 2018-10-22 stsp int errcode;
3271 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3272 1a76625f 2018-10-22 stsp if (errcode)
3273 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3274 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3275 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3276 1a76625f 2018-10-22 stsp if (errcode)
3277 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3278 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3279 1a76625f 2018-10-22 stsp if (errcode)
3280 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3281 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3282 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3283 245d91c1 2018-07-12 stsp err = NULL;
3284 245d91c1 2018-07-12 stsp blame->thread = NULL;
3286 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3287 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3288 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3290 245d91c1 2018-07-12 stsp if (blame->f) {
3291 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3292 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3293 245d91c1 2018-07-12 stsp blame->f = NULL;
3295 57670559 2018-12-23 stsp if (blame->lines) {
3296 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3297 57670559 2018-12-23 stsp free(blame->lines[i].id);
3298 57670559 2018-12-23 stsp free(blame->lines);
3299 57670559 2018-12-23 stsp blame->lines = NULL;
3301 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3302 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3304 245d91c1 2018-07-12 stsp return err;
3307 245d91c1 2018-07-12 stsp static const struct got_error *
3308 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
3310 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
3311 fc06ba56 2019-08-22 stsp int *done = arg;
3312 fc06ba56 2019-08-22 stsp int errcode;
3314 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3315 fc06ba56 2019-08-22 stsp if (errcode)
3316 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3317 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
3319 fc06ba56 2019-08-22 stsp if (*done)
3320 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
3322 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3323 fc06ba56 2019-08-22 stsp if (errcode)
3324 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3325 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
3327 fc06ba56 2019-08-22 stsp return err;
3330 fc06ba56 2019-08-22 stsp static const struct got_error *
3331 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3332 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3333 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3334 245d91c1 2018-07-12 stsp struct got_repository *repo)
3336 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3337 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3338 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3339 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3340 15a94983 2018-12-23 stsp int obj_type;
3342 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3344 15a94983 2018-12-23 stsp return err;
3345 15a94983 2018-12-23 stsp if (obj_id == NULL)
3346 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
3348 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3350 84451b3e 2018-07-10 stsp goto done;
3352 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3353 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3354 84451b3e 2018-07-10 stsp goto done;
3357 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3359 a70480e0 2018-06-23 stsp goto done;
3360 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3361 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3362 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3363 84451b3e 2018-07-10 stsp goto done;
3365 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3366 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
3367 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
3368 84451b3e 2018-07-10 stsp goto done;
3370 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3371 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3372 b02560ec 2019-08-19 stsp blame->nlines--;
3374 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3375 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3376 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3377 84451b3e 2018-07-10 stsp goto done;
3380 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3382 bd24772e 2018-07-11 stsp goto done;
3384 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3385 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3386 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3387 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3388 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3389 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3390 245d91c1 2018-07-12 stsp goto done;
3392 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3394 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3395 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3396 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3397 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3398 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
3399 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_arg = done;
3400 245d91c1 2018-07-12 stsp *blame_complete = 0;
3404 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3405 27d434c2 2018-09-15 stsp free(obj_id);
3407 245d91c1 2018-07-12 stsp stop_blame(blame);
3408 245d91c1 2018-07-12 stsp return err;
3411 245d91c1 2018-07-12 stsp static const struct got_error *
3412 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3413 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3414 8b473291 2019-02-21 stsp struct got_repository *repo)
3416 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3417 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3419 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3421 c4843652 2019-08-12 stsp s->path = strdup(path);
3422 c4843652 2019-08-12 stsp if (s->path == NULL)
3423 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
3425 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3426 c4843652 2019-08-12 stsp if (err) {
3427 c4843652 2019-08-12 stsp free(s->path);
3428 7cbe629d 2018-08-04 stsp return err;
3431 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3432 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3433 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3434 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3435 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3436 fb2756b9 2018-08-04 stsp s->repo = repo;
3437 8b473291 2019-02-21 stsp s->refs = refs;
3438 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3439 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3441 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3442 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3443 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3444 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
3445 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
3447 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3448 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3449 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3450 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3453 e5a0f69f 2018-08-18 stsp static const struct got_error *
3454 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3456 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3457 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3459 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3460 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3462 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3463 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3464 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3465 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3466 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3469 e5a0f69f 2018-08-18 stsp free(s->path);
3471 e5a0f69f 2018-08-18 stsp return err;
3474 7cbe629d 2018-08-04 stsp static const struct got_error *
3475 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
3477 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3479 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
3480 6c4c42e0 2019-06-24 stsp return NULL;
3483 6c4c42e0 2019-06-24 stsp static int
3484 6c4c42e0 2019-06-24 stsp match_line(const char *line, regex_t *regex)
3486 6c4c42e0 2019-06-24 stsp regmatch_t regmatch;
3488 6c4c42e0 2019-06-24 stsp return regexec(regex, line, 1, ®match, 0) == 0;
3492 6c4c42e0 2019-06-24 stsp static const struct got_error *
3493 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
3495 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3496 6c4c42e0 2019-06-24 stsp int lineno;
3498 6c4c42e0 2019-06-24 stsp if (!view->searching) {
3499 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3500 6c4c42e0 2019-06-24 stsp return NULL;
3503 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3504 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3505 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
3507 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
3509 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3510 6c4c42e0 2019-06-24 stsp lineno = 1;
3512 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3515 6c4c42e0 2019-06-24 stsp while (1) {
3516 6c4c42e0 2019-06-24 stsp char *line = NULL;
3517 6c4c42e0 2019-06-24 stsp off_t offset;
3518 6c4c42e0 2019-06-24 stsp size_t len;
3520 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
3521 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
3522 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3523 6c4c42e0 2019-06-24 stsp free(line);
3527 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3528 6c4c42e0 2019-06-24 stsp lineno = 1;
3530 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3533 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
3534 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3535 6c4c42e0 2019-06-24 stsp free(line);
3536 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
3538 6c4c42e0 2019-06-24 stsp free(line);
3539 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
3540 2246482e 2019-06-25 stsp if (line && match_line(line, &view->regex)) {
3541 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3542 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
3543 6c4c42e0 2019-06-24 stsp free(line);
3546 6c4c42e0 2019-06-24 stsp free(line);
3547 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3553 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3554 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
3555 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
3558 6c4c42e0 2019-06-24 stsp return NULL;
3561 6c4c42e0 2019-06-24 stsp static const struct got_error *
3562 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
3564 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3565 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
3566 2b380cc8 2018-10-24 stsp int errcode;
3568 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
3569 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3570 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
3571 2b380cc8 2018-10-24 stsp if (errcode)
3572 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3574 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
3577 51fe7530 2019-08-19 stsp if (s->blame_complete)
3578 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
3580 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3581 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3582 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
3583 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
3585 669b5ffa 2018-10-07 stsp view_vborder(view);
3586 e5a0f69f 2018-08-18 stsp return err;
3589 e5a0f69f 2018-08-18 stsp static const struct got_error *
3590 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3591 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3593 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
3594 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
3595 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3596 669b5ffa 2018-10-07 stsp int begin_x = 0;
3598 e5a0f69f 2018-08-18 stsp switch (ch) {
3600 1e37a5c2 2019-05-12 jcs s->done = 1;
3603 1e37a5c2 2019-05-12 jcs case KEY_UP:
3604 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
3605 1e37a5c2 2019-05-12 jcs s->selected_line--;
3606 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
3607 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3608 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3610 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3611 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
3612 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3615 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3616 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3617 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3619 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3622 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3623 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3624 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3625 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3626 1e37a5c2 2019-05-12 jcs s->selected_line++;
3627 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3628 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3629 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3632 1e37a5c2 2019-05-12 jcs case 'p': {
3633 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3634 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3635 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3636 1e37a5c2 2019-05-12 jcs if (id == NULL)
3638 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3639 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3640 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3641 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3642 1e37a5c2 2019-05-12 jcs int obj_type;
3643 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3644 1e37a5c2 2019-05-12 jcs s->repo, id);
3647 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3648 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3649 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3650 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3653 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3654 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3655 1e37a5c2 2019-05-12 jcs pid->id, s->path);
3656 e5a0f69f 2018-08-18 stsp if (err) {
3657 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
3658 1e37a5c2 2019-05-12 jcs err = NULL;
3659 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3662 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
3664 1e37a5c2 2019-05-12 jcs free(blob_id);
3665 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
3666 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
3667 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3670 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3672 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3674 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
3675 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
3677 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3682 1e37a5c2 2019-05-12 jcs s->done = 1;
3683 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3684 1e37a5c2 2019-05-12 jcs s->done = 0;
3685 1e37a5c2 2019-05-12 jcs if (thread_err)
3687 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3688 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
3689 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3690 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3691 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
3692 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
3697 1e37a5c2 2019-05-12 jcs case 'B': {
3698 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
3699 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
3700 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
3702 1e37a5c2 2019-05-12 jcs s->done = 1;
3703 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3704 1e37a5c2 2019-05-12 jcs s->done = 0;
3705 1e37a5c2 2019-05-12 jcs if (thread_err)
3707 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3708 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
3709 1e37a5c2 2019-05-12 jcs s->blamed_commit =
3710 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
3711 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3712 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3713 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
3714 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
3719 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3720 1e37a5c2 2019-05-12 jcs case '\r': {
3721 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3722 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
3723 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
3724 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3725 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3726 1e37a5c2 2019-05-12 jcs if (id == NULL)
3728 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
3731 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
3732 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
3733 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3734 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3735 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3736 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
3737 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3738 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
3741 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
3742 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
3743 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3745 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3748 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3749 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3752 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
3754 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3757 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
3758 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
3760 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3765 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3767 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3768 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
3769 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
3772 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3773 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
3774 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3775 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3778 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
3779 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
3780 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
3781 1e37a5c2 2019-05-12 jcs view->nlines - 2;
3783 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
3784 1e37a5c2 2019-05-12 jcs s->blame.nlines -
3785 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
3787 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3788 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
3789 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3790 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3796 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
3799 a70480e0 2018-06-23 stsp static const struct got_error *
3800 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
3802 a70480e0 2018-06-23 stsp const struct got_error *error;
3803 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
3804 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3805 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
3806 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3807 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3808 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
3810 e1cd8fed 2018-08-01 stsp struct tog_view *view;
3812 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3814 a70480e0 2018-06-23 stsp #ifndef PROFILE
3815 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3816 8e94dd5b 2019-01-04 stsp NULL) == -1)
3817 a70480e0 2018-06-23 stsp err(1, "pledge");
3820 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3821 a70480e0 2018-06-23 stsp switch (ch) {
3823 a70480e0 2018-06-23 stsp commit_id_str = optarg;
3826 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3827 69069811 2018-08-02 stsp if (repo_path == NULL)
3828 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3832 17020d27 2019-03-07 stsp usage_blame();
3833 a70480e0 2018-06-23 stsp /* NOTREACHED */
3837 a70480e0 2018-06-23 stsp argc -= optind;
3838 a70480e0 2018-06-23 stsp argv += optind;
3840 69069811 2018-08-02 stsp if (argc == 1)
3841 69069811 2018-08-02 stsp path = argv[0];
3843 a70480e0 2018-06-23 stsp usage_blame();
3845 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
3846 69069811 2018-08-02 stsp if (cwd == NULL) {
3847 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3848 69069811 2018-08-02 stsp goto done;
3850 69069811 2018-08-02 stsp if (repo_path == NULL) {
3851 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3852 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3853 69069811 2018-08-02 stsp goto done;
3855 eb41ed75 2019-02-05 stsp error = NULL;
3856 eb41ed75 2019-02-05 stsp if (worktree) {
3857 eb41ed75 2019-02-05 stsp repo_path =
3858 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3859 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
3860 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3861 eb41ed75 2019-02-05 stsp if (error)
3862 eb41ed75 2019-02-05 stsp goto done;
3864 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
3865 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
3866 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3867 eb41ed75 2019-02-05 stsp goto done;
3872 a915003a 2019-02-05 stsp init_curses();
3874 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3875 c02c541e 2019-03-29 stsp if (error != NULL)
3876 8e94dd5b 2019-01-04 stsp goto done;
3878 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3879 c02c541e 2019-03-29 stsp if (error)
3880 92205607 2019-01-04 stsp goto done;
3882 eb41ed75 2019-02-05 stsp if (worktree) {
3883 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3884 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3885 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3886 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3887 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3888 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3889 eb41ed75 2019-02-05 stsp path) == -1) {
3890 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3891 eb41ed75 2019-02-05 stsp goto done;
3893 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3896 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3898 eb41ed75 2019-02-05 stsp if (error)
3899 69069811 2018-08-02 stsp goto done;
3901 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
3902 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
3903 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3904 a70480e0 2018-06-23 stsp if (error != NULL)
3905 66b4983c 2018-06-23 stsp goto done;
3906 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3907 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
3909 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&commit_id, commit_id_str, repo);
3910 f2b6a97d 2019-07-15 stsp if (error) {
3911 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
3912 f2b6a97d 2019-07-15 stsp goto done;
3913 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&commit_id,
3914 f2b6a97d 2019-07-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3917 a19e88aa 2018-06-23 stsp if (error != NULL)
3918 8b473291 2019-02-21 stsp goto done;
3920 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3921 8b473291 2019-02-21 stsp if (error)
3922 a19e88aa 2018-06-23 stsp goto done;
3924 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3925 e1cd8fed 2018-08-01 stsp if (view == NULL) {
3926 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3927 e1cd8fed 2018-08-01 stsp goto done;
3929 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3930 7cbe629d 2018-08-04 stsp if (error)
3931 7cbe629d 2018-08-04 stsp goto done;
3932 12314ad4 2019-08-31 stsp if (worktree) {
3933 12314ad4 2019-08-31 stsp /* Release work tree lock. */
3934 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
3935 12314ad4 2019-08-31 stsp worktree = NULL;
3937 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3939 69069811 2018-08-02 stsp free(repo_path);
3940 69069811 2018-08-02 stsp free(cwd);
3941 a70480e0 2018-06-23 stsp free(commit_id);
3942 eb41ed75 2019-02-05 stsp if (worktree)
3943 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
3945 a70480e0 2018-06-23 stsp got_repo_close(repo);
3946 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3947 a70480e0 2018-06-23 stsp return error;
3950 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3951 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
3952 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
3953 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
3954 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
3955 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
3956 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
3958 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3959 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
3960 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
3961 ffd1d5e5 2018-06-23 stsp int width, n;
3963 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
3965 f7d12f7e 2018-08-01 stsp werase(view->window);
3967 ffd1d5e5 2018-06-23 stsp if (limit == 0)
3968 ffd1d5e5 2018-06-23 stsp return NULL;
3970 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, label, view->ncols, 0);
3972 ffd1d5e5 2018-06-23 stsp return err;
3973 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3974 a3404814 2018-09-02 stsp wstandout(view->window);
3975 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3976 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3977 a3404814 2018-09-02 stsp wstandend(view->window);
3978 2550e4c3 2018-07-13 stsp free(wline);
3979 2550e4c3 2018-07-13 stsp wline = NULL;
3980 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3981 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3982 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3983 ffd1d5e5 2018-06-23 stsp return NULL;
3984 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
3986 ce52c690 2018-06-23 stsp return err;
3987 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3988 2550e4c3 2018-07-13 stsp free(wline);
3989 2550e4c3 2018-07-13 stsp wline = NULL;
3990 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3991 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3992 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3993 ffd1d5e5 2018-06-23 stsp return NULL;
3994 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3995 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
3996 a1eca9bb 2018-06-23 stsp return NULL;
3998 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3999 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
4000 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
4001 0cf4efb1 2018-09-29 stsp if (view->focussed)
4002 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4003 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
4005 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4006 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
4007 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4008 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4009 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4010 ffd1d5e5 2018-06-23 stsp return NULL;
4014 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
4015 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
4018 ffd1d5e5 2018-06-23 stsp while (te) {
4019 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
4020 848d6979 2019-08-12 stsp const char *modestr = "";
4022 1d13200f 2018-07-12 stsp if (show_ids) {
4023 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
4025 638f9024 2019-05-13 stsp return got_error_from_errno(
4026 230a42bd 2019-05-11 jcs "got_object_id_str");
4028 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4029 63c5ca5d 2019-08-24 stsp modestr = "$";
4030 63c5ca5d 2019-08-24 stsp else if (S_ISLNK(te->mode))
4031 848d6979 2019-08-12 stsp modestr = "@";
4032 848d6979 2019-08-12 stsp else if (S_ISDIR(te->mode))
4033 848d6979 2019-08-12 stsp modestr = "/";
4034 848d6979 2019-08-12 stsp else if (te->mode & S_IXUSR)
4035 848d6979 2019-08-12 stsp modestr = "*";
4036 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4037 848d6979 2019-08-12 stsp te->name, modestr) == -1) {
4038 1d13200f 2018-07-12 stsp free(id_str);
4039 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4041 1d13200f 2018-07-12 stsp free(id_str);
4042 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4043 ffd1d5e5 2018-06-23 stsp if (err) {
4044 ffd1d5e5 2018-06-23 stsp free(line);
4047 ffd1d5e5 2018-06-23 stsp if (n == selected) {
4048 0cf4efb1 2018-09-29 stsp if (view->focussed)
4049 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4050 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
4052 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4053 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4054 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4055 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
4056 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4057 ffd1d5e5 2018-06-23 stsp free(line);
4058 2550e4c3 2018-07-13 stsp free(wline);
4059 2550e4c3 2018-07-13 stsp wline = NULL;
4061 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4062 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
4063 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4065 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
4068 ffd1d5e5 2018-06-23 stsp return err;
4071 ffd1d5e5 2018-06-23 stsp static void
4072 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
4073 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
4074 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
4076 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
4079 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
4082 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
4083 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
4084 ffd1d5e5 2018-06-23 stsp if (!isroot)
4085 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4089 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
4090 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
4091 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
4092 ffd1d5e5 2018-06-23 stsp prev = te;
4093 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
4095 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
4096 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
4098 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4099 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4102 768394f3 2019-01-24 stsp static int
4103 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4104 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
4105 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
4107 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4108 ffd1d5e5 2018-06-23 stsp int n = 0;
4110 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
4111 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4113 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
4114 768394f3 2019-01-24 stsp last = last_displayed_entry;
4115 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4116 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
4117 768394f3 2019-01-24 stsp if (last) {
4118 768394f3 2019-01-24 stsp *first_displayed_entry = next;
4119 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
4125 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4126 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4127 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4129 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4130 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4131 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4133 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4134 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
4136 ce52c690 2018-06-23 stsp len += strlen(te->name);
4138 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4139 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4140 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4142 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4143 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4144 d9765a41 2018-06-23 stsp while (pt) {
4145 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4146 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4147 cb2ebc8a 2018-06-23 stsp goto done;
4149 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
4150 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4151 cb2ebc8a 2018-06-23 stsp goto done;
4153 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4156 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
4157 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4158 ce52c690 2018-06-23 stsp goto done;
4162 ce52c690 2018-06-23 stsp if (err) {
4163 ce52c690 2018-06-23 stsp free(*path);
4164 ce52c690 2018-06-23 stsp *path = NULL;
4166 ce52c690 2018-06-23 stsp return err;
4169 ce52c690 2018-06-23 stsp static const struct got_error *
4170 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
4171 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4172 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4173 8b473291 2019-02-21 stsp struct got_repository *repo)
4175 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
4176 ce52c690 2018-06-23 stsp char *path;
4177 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
4179 a0de39f3 2019-08-09 stsp *new_view = NULL;
4181 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
4183 ce52c690 2018-06-23 stsp return err;
4185 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4186 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
4187 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
4188 83ce39e3 2019-08-12 stsp goto done;
4191 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
4192 e5a0f69f 2018-08-18 stsp if (err) {
4193 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
4194 fc06ba56 2019-08-22 stsp err = NULL;
4195 e5a0f69f 2018-08-18 stsp view_close(blame_view);
4197 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
4199 83ce39e3 2019-08-12 stsp free(path);
4200 69efd4c4 2018-07-18 stsp return err;
4203 69efd4c4 2018-07-18 stsp static const struct got_error *
4204 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
4205 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4206 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4207 8b473291 2019-02-21 stsp struct got_repository *repo)
4209 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
4210 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
4211 69efd4c4 2018-07-18 stsp char *path;
4213 a0de39f3 2019-08-09 stsp *new_view = NULL;
4215 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4216 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
4217 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
4219 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
4221 69efd4c4 2018-07-18 stsp return err;
4223 d01904d4 2019-06-25 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4225 e5a0f69f 2018-08-18 stsp view_close(log_view);
4227 e5a0f69f 2018-08-18 stsp *new_view = log_view;
4228 cb2ebc8a 2018-06-23 stsp free(path);
4229 cb2ebc8a 2018-06-23 stsp return err;
4232 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4233 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
4234 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4235 8b473291 2019-02-21 stsp struct got_repository *repo)
4237 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4238 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
4239 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4241 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
4243 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
4244 ffd1d5e5 2018-06-23 stsp if (err != NULL)
4245 ffd1d5e5 2018-06-23 stsp goto done;
4247 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4248 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4249 ffd1d5e5 2018-06-23 stsp goto done;
4252 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
4253 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
4254 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4255 8d0fe45a 2019-08-12 stsp s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4256 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
4257 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
4258 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4259 6484ec90 2018-09-29 stsp goto done;
4261 8b473291 2019-02-21 stsp s->refs = refs;
4262 fb2756b9 2018-08-04 stsp s->repo = repo;
4264 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
4265 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
4266 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
4267 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
4268 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
4270 ad80ab7b 2018-08-04 stsp free(commit_id_str);
4271 6484ec90 2018-09-29 stsp if (err) {
4272 fb2756b9 2018-08-04 stsp free(s->tree_label);
4273 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4275 ad80ab7b 2018-08-04 stsp return err;
4278 e5a0f69f 2018-08-18 stsp static const struct got_error *
4279 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
4281 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4283 fb2756b9 2018-08-04 stsp free(s->tree_label);
4284 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4285 6484ec90 2018-09-29 stsp free(s->commit_id);
4286 6484ec90 2018-09-29 stsp s->commit_id = NULL;
4287 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
4288 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
4289 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
4290 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
4291 ad80ab7b 2018-08-04 stsp free(parent);
4294 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
4295 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
4296 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
4298 7c32bd05 2019-06-22 stsp return NULL;
4301 7c32bd05 2019-06-22 stsp static const struct got_error *
4302 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
4304 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4306 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
4307 7c32bd05 2019-06-22 stsp return NULL;
4310 7c32bd05 2019-06-22 stsp static int
4311 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4313 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
4315 7c32bd05 2019-06-22 stsp return regexec(regex, te->name, 1, ®match, 0) == 0;
4318 7c32bd05 2019-06-22 stsp static const struct got_error *
4319 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
4321 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4322 a0de39f3 2019-08-09 stsp struct got_tree_entry *entry = NULL, *te;
4324 7c32bd05 2019-06-22 stsp if (!view->searching) {
4325 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4326 7c32bd05 2019-06-22 stsp return NULL;
4329 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4330 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
4331 7c32bd05 2019-06-22 stsp if (s->selected_entry)
4332 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4334 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4337 7c32bd05 2019-06-22 stsp if (s->selected_entry == NULL) {
4338 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4339 7c32bd05 2019-06-22 stsp entry = te;
4341 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4342 7c32bd05 2019-06-22 stsp entry = te;
4343 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) ==
4344 7c32bd05 2019-06-22 stsp s->selected_entry)
4350 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4351 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4353 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4354 7c32bd05 2019-06-22 stsp entry = te;
4358 7c32bd05 2019-06-22 stsp while (1) {
4359 7c32bd05 2019-06-22 stsp if (entry == NULL) {
4360 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
4361 ac66afb8 2019-06-24 stsp view->search_next_done = 1;
4362 ac66afb8 2019-06-24 stsp return NULL;
4364 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4365 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4367 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4368 7c32bd05 2019-06-22 stsp entry = te;
4372 7c32bd05 2019-06-22 stsp if (match_tree_entry(entry, &view->regex)) {
4373 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4374 7c32bd05 2019-06-22 stsp s->matched_entry = entry;
4378 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4379 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(entry, entry);
4381 7c32bd05 2019-06-22 stsp if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4382 7c32bd05 2019-06-22 stsp entry = NULL;
4384 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4385 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) == entry) {
4386 7c32bd05 2019-06-22 stsp entry = te;
4394 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4395 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
4396 7c32bd05 2019-06-22 stsp s->selected = 0;
4399 e5a0f69f 2018-08-18 stsp return NULL;
4402 ad80ab7b 2018-08-04 stsp static const struct got_error *
4403 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
4405 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
4406 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4407 e5a0f69f 2018-08-18 stsp char *parent_path;
4409 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
4411 e5a0f69f 2018-08-18 stsp return err;
4413 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
4414 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
4415 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4416 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
4417 e5a0f69f 2018-08-18 stsp free(parent_path);
4419 669b5ffa 2018-10-07 stsp view_vborder(view);
4420 e5a0f69f 2018-08-18 stsp return err;
4423 e5a0f69f 2018-08-18 stsp static const struct got_error *
4424 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4425 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4427 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4428 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
4429 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
4430 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
4432 e5a0f69f 2018-08-18 stsp switch (ch) {
4434 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
4437 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
4439 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4440 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4441 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
4442 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
4443 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
4444 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4445 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4447 1e37a5c2 2019-05-12 jcs return err;
4448 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
4450 1e37a5c2 2019-05-12 jcs view_close(log_view);
4453 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
4454 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4456 1e37a5c2 2019-05-12 jcs *new_view = log_view;
4459 1e37a5c2 2019-05-12 jcs case KEY_UP:
4460 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
4461 1e37a5c2 2019-05-12 jcs s->selected--;
4462 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
4465 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
4467 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
4468 1e37a5c2 2019-05-12 jcs s->entries, s->tree == s->root);
4470 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4471 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
4472 1e37a5c2 2019-05-12 jcs MAX(0, view->nlines - 4 - s->selected), s->entries,
4473 1e37a5c2 2019-05-12 jcs s->tree == s->root);
4474 1e37a5c2 2019-05-12 jcs s->selected = 0;
4475 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_FIRST(&s->entries->head) ==
4476 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
4477 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
4480 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4481 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
4482 1e37a5c2 2019-05-12 jcs s->selected++;
4485 00ba99a7 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4486 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
4488 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
4489 1e37a5c2 2019-05-12 jcs s->last_displayed_entry, s->entries);
4491 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4492 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4494 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
4495 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
4496 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4499 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
4500 1e37a5c2 2019-05-12 jcs view->nlines, s->last_displayed_entry, s->entries);
4501 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
4502 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
4503 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
4504 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
4506 1e37a5c2 2019-05-12 jcs ndisplayed++;
4507 1e37a5c2 2019-05-12 jcs te = SIMPLEQ_NEXT(te, entry);
4508 1e37a5c2 2019-05-12 jcs } while (te);
4509 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
4512 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4514 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4515 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4516 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
4517 1e37a5c2 2019-05-12 jcs /* user selected '..' */
4518 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
4520 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
4521 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
4523 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
4524 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
4525 1e37a5c2 2019-05-12 jcs s->entries =
4526 1e37a5c2 2019-05-12 jcs got_object_tree_get_entries(s->tree);
4527 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
4528 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
4529 1e37a5c2 2019-05-12 jcs s->selected_entry =
4530 1e37a5c2 2019-05-12 jcs parent->selected_entry;
4531 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
4532 1e37a5c2 2019-05-12 jcs free(parent);
4533 1e37a5c2 2019-05-12 jcs } else if (S_ISDIR(s->selected_entry->mode)) {
4534 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
4535 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&subtree,
4536 1e37a5c2 2019-05-12 jcs s->repo, s->selected_entry->id);
4539 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
4540 941e9f74 2019-05-21 stsp if (err) {
4541 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
4544 1e37a5c2 2019-05-12 jcs } else if (S_ISREG(s->selected_entry->mode)) {
4545 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
4546 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
4547 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
4549 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
4550 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
4551 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
4554 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
4555 669b5ffa 2018-10-07 stsp err = view_close_child(view);
4557 669b5ffa 2018-10-07 stsp return err;
4558 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
4559 669b5ffa 2018-10-07 stsp if (err) {
4560 1e37a5c2 2019-05-12 jcs view_close(blame_view);
4563 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
4564 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
4566 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
4569 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4570 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
4571 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4577 ffd1d5e5 2018-06-23 stsp return err;
4580 ffd1d5e5 2018-06-23 stsp __dead static void
4581 ffd1d5e5 2018-06-23 stsp usage_tree(void)
4584 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4585 ffd1d5e5 2018-06-23 stsp getprogname());
4589 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4590 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
4592 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
4593 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
4594 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4595 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
4596 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4597 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
4598 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
4599 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
4601 5221c383 2018-08-01 stsp struct tog_view *view;
4603 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4605 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
4606 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4607 d188b9a6 2019-01-04 stsp NULL) == -1)
4608 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
4611 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
4612 ffd1d5e5 2018-06-23 stsp switch (ch) {
4614 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
4617 17020d27 2019-03-07 stsp usage_tree();
4618 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
4622 ffd1d5e5 2018-06-23 stsp argc -= optind;
4623 ffd1d5e5 2018-06-23 stsp argv += optind;
4625 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
4626 52185f70 2019-02-05 stsp struct got_worktree *worktree;
4627 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
4628 52185f70 2019-02-05 stsp if (cwd == NULL)
4629 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
4630 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4631 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4632 52185f70 2019-02-05 stsp goto done;
4633 52185f70 2019-02-05 stsp if (worktree) {
4634 52185f70 2019-02-05 stsp free(cwd);
4635 52185f70 2019-02-05 stsp repo_path =
4636 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4637 52185f70 2019-02-05 stsp got_worktree_close(worktree);
4639 52185f70 2019-02-05 stsp repo_path = cwd;
4640 52185f70 2019-02-05 stsp if (repo_path == NULL) {
4641 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4642 52185f70 2019-02-05 stsp goto done;
4644 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
4645 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
4646 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
4647 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
4649 ffd1d5e5 2018-06-23 stsp usage_log();
4651 a915003a 2019-02-05 stsp init_curses();
4653 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4654 c02c541e 2019-03-29 stsp if (error != NULL)
4655 52185f70 2019-02-05 stsp goto done;
4657 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4658 c02c541e 2019-03-29 stsp if (error)
4659 52185f70 2019-02-05 stsp goto done;
4661 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
4662 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4664 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4665 f2b6a97d 2019-07-15 stsp if (error) {
4666 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
4667 f2b6a97d 2019-07-15 stsp goto done;
4668 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&commit_id,
4669 f2b6a97d 2019-07-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4672 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4673 ffd1d5e5 2018-06-23 stsp goto done;
4675 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4676 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4677 ffd1d5e5 2018-06-23 stsp goto done;
4679 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
4680 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
4681 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4682 8b473291 2019-02-21 stsp goto done;
4684 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4685 8b473291 2019-02-21 stsp if (error)
4686 ffd1d5e5 2018-06-23 stsp goto done;
4688 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4689 5221c383 2018-08-01 stsp if (view == NULL) {
4690 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4691 5221c383 2018-08-01 stsp goto done;
4693 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
4694 ad80ab7b 2018-08-04 stsp if (error)
4695 ad80ab7b 2018-08-04 stsp goto done;
4696 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4698 52185f70 2019-02-05 stsp free(repo_path);
4699 ffd1d5e5 2018-06-23 stsp free(commit_id);
4700 ffd1d5e5 2018-06-23 stsp if (commit)
4701 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
4703 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
4705 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
4706 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4707 ffd1d5e5 2018-06-23 stsp return error;
4710 ce5b7c56 2019-07-09 stsp static void
4711 ce5b7c56 2019-07-09 stsp list_commands(void)
4715 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
4716 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
4717 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
4718 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->name);
4720 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
4723 4ed7e80c 2018-05-20 stsp __dead static void
4724 ce5b7c56 2019-07-09 stsp usage(int hflag)
4726 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4727 53ccebc2 2019-07-30 stsp getprogname());
4728 ce5b7c56 2019-07-09 stsp if (hflag)
4729 ce5b7c56 2019-07-09 stsp list_commands();
4733 c2301be8 2018-04-30 stsp static char **
4734 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
4736 c2301be8 2018-04-30 stsp char **argv;
4737 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
4739 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
4740 c2301be8 2018-04-30 stsp if (argv == NULL)
4741 c2301be8 2018-04-30 stsp err(1, "calloc");
4742 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
4743 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
4744 e10c916e 2019-09-15 hiltjo err(1, "strdup");
4745 c2301be8 2018-04-30 stsp if (arg1) {
4746 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
4747 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
4748 e10c916e 2019-09-15 hiltjo err(1, "strdup");
4751 c2301be8 2018-04-30 stsp return argv;
4755 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
4757 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
4758 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
4759 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
4760 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
4762 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
4764 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
4765 9f7d7167 2018-04-29 stsp switch (ch) {
4767 9f7d7167 2018-04-29 stsp hflag = 1;
4770 53ccebc2 2019-07-30 stsp Vflag = 1;
4773 ce5b7c56 2019-07-09 stsp usage(hflag);
4774 9f7d7167 2018-04-29 stsp /* NOTREACHED */
4778 9f7d7167 2018-04-29 stsp argc -= optind;
4779 9f7d7167 2018-04-29 stsp argv += optind;
4780 9f7d7167 2018-04-29 stsp optind = 0;
4781 c2301be8 2018-04-30 stsp optreset = 1;
4783 53ccebc2 2019-07-30 stsp if (Vflag) {
4784 53ccebc2 2019-07-30 stsp got_version_print_str();
4788 c2301be8 2018-04-30 stsp if (argc == 0) {
4789 f29d3e89 2018-06-23 stsp if (hflag)
4790 ce5b7c56 2019-07-09 stsp usage(hflag);
4791 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
4792 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
4793 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
4798 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
4799 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
4800 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
4801 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
4802 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
4807 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
4808 d70c3147 2019-07-09 stsp fprintf(stderr, "%s: unknown command '%s'\n",
4809 3642c4c6 2019-07-09 stsp getprogname(), argv[0]);
4810 ce5b7c56 2019-07-09 stsp list_commands();
4815 3642c4c6 2019-07-09 stsp if (hflag)
4816 3642c4c6 2019-07-09 stsp cmd->cmd_usage();
4818 3642c4c6 2019-07-09 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4821 c2301be8 2018-04-30 stsp free(cmd_argv);
4822 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
4823 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);