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 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1410 5813d178 2019-03-09 stsp entry = first;
1411 899d86c2 2018-05-10 stsp *last = first;
1412 867c6645 2018-07-10 stsp ncommits = 0;
1413 899d86c2 2018-05-10 stsp while (entry) {
1414 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1416 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1417 2814baeb 2018-08-01 stsp wstandout(view->window);
1418 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1419 27a741e5 2019-09-11 stsp date_display_cols, author_cols);
1420 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1421 2814baeb 2018-08-01 stsp wstandend(view->window);
1423 60493ae3 2019-06-20 stsp goto done;
1424 0553a4e3 2018-04-30 stsp ncommits++;
1425 899d86c2 2018-05-10 stsp *last = entry;
1426 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1429 1a57306a 2018-09-02 stsp view_vborder(view);
1431 1a76625f 2018-10-22 stsp free(id_str);
1432 8b473291 2019-02-21 stsp free(refs_str);
1433 1a76625f 2018-10-22 stsp free(ncommits_str);
1434 1a76625f 2018-10-22 stsp free(header);
1435 80ddbec8 2018-04-29 stsp return err;
1438 07b55e75 2018-05-10 stsp static void
1439 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1440 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1441 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1443 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1444 07b55e75 2018-05-10 stsp int nscrolled = 0;
1446 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1447 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1450 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1451 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1452 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1453 07b55e75 2018-05-10 stsp if (entry) {
1454 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1455 07b55e75 2018-05-10 stsp nscrolled++;
1460 aa075928 2018-05-10 stsp static const struct got_error *
1461 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1462 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1464 5e224a3e 2019-02-22 stsp int errcode;
1465 8a42fca8 2019-02-22 stsp int max_wait = 20;
1467 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1469 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1470 5e224a3e 2019-02-22 stsp if (*log_complete)
1473 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1474 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1475 7aafa0d1 2019-02-22 stsp if (errcode)
1476 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1477 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1478 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1479 7aafa0d1 2019-02-22 stsp if (errcode)
1480 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1481 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1482 7aafa0d1 2019-02-22 stsp pthread_yield();
1483 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1484 7aafa0d1 2019-02-22 stsp if (errcode)
1485 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1486 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1488 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1490 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1491 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1492 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1493 5e224a3e 2019-02-22 stsp * with few commits in history.
1495 7aafa0d1 2019-02-22 stsp return NULL;
1499 5e224a3e 2019-02-22 stsp return NULL;
1502 5e224a3e 2019-02-22 stsp static const struct got_error *
1503 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1504 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1505 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1506 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1507 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1509 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1510 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1511 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1513 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1514 5e224a3e 2019-02-22 stsp return NULL;
1516 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1517 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1519 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1520 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1522 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1523 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1524 5e224a3e 2019-02-22 stsp need_commits);
1526 5e224a3e 2019-02-22 stsp return err;
1530 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1531 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1534 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1536 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1537 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1539 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1540 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1542 dd0a52c1 2018-05-20 stsp return err;
1545 cd0acaa7 2018-05-20 stsp static const struct got_error *
1546 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1547 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1548 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1549 8b473291 2019-02-21 stsp struct got_repository *repo)
1551 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1552 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1553 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1555 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1556 15a94983 2018-12-23 stsp if (diff_view == NULL)
1557 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1559 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1560 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1561 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1562 e5a0f69f 2018-08-18 stsp if (err == NULL)
1563 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1564 cd0acaa7 2018-05-20 stsp return err;
1567 80ddbec8 2018-04-29 stsp static const struct got_error *
1568 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1569 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1571 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1573 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1574 941e9f74 2019-05-21 stsp if (parent == NULL)
1575 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1577 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1578 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1579 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1580 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1581 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1582 941e9f74 2019-05-21 stsp s->tree = subtree;
1583 941e9f74 2019-05-21 stsp s->entries = got_object_tree_get_entries(s->tree);
1584 941e9f74 2019-05-21 stsp s->selected = 0;
1585 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1586 941e9f74 2019-05-21 stsp return NULL;
1590 941e9f74 2019-05-21 stsp static const struct got_error *
1591 941e9f74 2019-05-21 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1592 941e9f74 2019-05-21 stsp struct commit_queue_entry *entry, const char *path,
1593 941e9f74 2019-05-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
1595 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1596 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1597 941e9f74 2019-05-21 stsp struct got_tree_entry *te;
1598 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s;
1599 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1600 b03c880f 2019-05-21 stsp char *slash, *subpath = NULL;
1601 941e9f74 2019-05-21 stsp const char *p;
1603 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1604 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1606 9343a5fb 2018-06-23 stsp return err;
1608 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1609 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1610 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1612 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1613 941e9f74 2019-05-21 stsp if (err) {
1614 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1615 941e9f74 2019-05-21 stsp return err;
1617 941e9f74 2019-05-21 stsp s = &tree_view->state.tree;
1619 941e9f74 2019-05-21 stsp *new_view = tree_view;
1621 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1623 8d0fe45a 2019-08-12 stsp while (p[0] == '/')
1625 941e9f74 2019-05-21 stsp while (*p) {
1626 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1628 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1629 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1630 941e9f74 2019-05-21 stsp if (slash == NULL)
1631 941e9f74 2019-05-21 stsp slash = strchr(p, '\0');
1632 941e9f74 2019-05-21 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1633 941e9f74 2019-05-21 stsp if (strncmp(p, te->name, slash - p) == 0) {
1634 941e9f74 2019-05-21 stsp s->selected_entry = te;
1637 b03c880f 2019-05-21 stsp s->selected++;
1639 941e9f74 2019-05-21 stsp if (s->selected_entry == NULL) {
1640 941e9f74 2019-05-21 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1643 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1644 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1646 67409a31 2019-05-24 stsp if (!S_ISDIR(s->selected_entry->mode)) {
1647 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1648 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1649 67409a31 2019-05-24 stsp s->selected = 0;
1653 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1654 941e9f74 2019-05-21 stsp if (slash)
1655 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1657 941e9f74 2019-05-21 stsp subpath = strdup(path);
1658 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1659 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1663 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1668 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1669 941e9f74 2019-05-21 stsp free(tree_id);
1673 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1674 941e9f74 2019-05-21 stsp if (err) {
1675 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1678 941e9f74 2019-05-21 stsp if (slash == NULL)
1680 941e9f74 2019-05-21 stsp free(subpath);
1681 941e9f74 2019-05-21 stsp subpath = NULL;
1682 941e9f74 2019-05-21 stsp p = slash;
1685 941e9f74 2019-05-21 stsp free(subpath);
1686 1a76625f 2018-10-22 stsp return err;
1689 1a76625f 2018-10-22 stsp static void *
1690 1a76625f 2018-10-22 stsp log_thread(void *arg)
1692 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1693 1a76625f 2018-10-22 stsp int errcode = 0;
1694 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1695 1a76625f 2018-10-22 stsp int done = 0;
1697 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1698 6fb7cd11 2019-08-22 stsp NULL, NULL);
1700 1a76625f 2018-10-22 stsp return (void *)err;
1702 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1703 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1704 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1705 13add988 2019-10-15 stsp a->regex);
1706 1a76625f 2018-10-22 stsp if (err) {
1707 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1708 1a76625f 2018-10-22 stsp return (void *)err;
1709 1a76625f 2018-10-22 stsp err = NULL;
1711 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1712 1a76625f 2018-10-22 stsp a->commits_needed--;
1714 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1715 3abe8080 2019-04-10 stsp if (errcode) {
1716 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1717 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1719 3abe8080 2019-04-10 stsp } else if (*a->quit)
1721 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1722 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1723 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1724 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1728 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1729 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1730 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1731 1a76625f 2018-10-22 stsp &tog_mutex);
1732 1a76625f 2018-10-22 stsp if (errcode)
1733 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1734 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1737 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1738 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1739 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1740 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1742 3abe8080 2019-04-10 stsp a->log_complete = 1;
1743 1a76625f 2018-10-22 stsp return (void *)err;
1746 1a76625f 2018-10-22 stsp static const struct got_error *
1747 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1749 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1750 1a76625f 2018-10-22 stsp int errcode;
1752 1a76625f 2018-10-22 stsp if (s->thread) {
1753 1a76625f 2018-10-22 stsp s->quit = 1;
1754 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1755 1a76625f 2018-10-22 stsp if (errcode)
1756 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1757 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1758 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1759 1a76625f 2018-10-22 stsp if (errcode)
1760 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1761 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1762 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1763 1a76625f 2018-10-22 stsp if (errcode)
1764 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1765 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1766 1a76625f 2018-10-22 stsp if (errcode)
1767 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1768 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1769 1a76625f 2018-10-22 stsp s->thread = NULL;
1772 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1773 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1774 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1776 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1777 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1778 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1781 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1782 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1783 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1786 9343a5fb 2018-06-23 stsp return err;
1789 9343a5fb 2018-06-23 stsp static const struct got_error *
1790 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1792 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1793 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1795 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1796 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1797 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1798 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1799 1a76625f 2018-10-22 stsp free(s->start_id);
1800 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1801 1a76625f 2018-10-22 stsp return err;
1804 1a76625f 2018-10-22 stsp static const struct got_error *
1805 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
1807 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1809 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
1810 96e2b566 2019-07-08 stsp s->search_entry = NULL;
1811 60493ae3 2019-06-20 stsp return NULL;
1814 60493ae3 2019-06-20 stsp static const struct got_error *
1815 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
1817 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
1818 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1819 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
1821 60493ae3 2019-06-20 stsp if (!view->searching) {
1822 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1823 60493ae3 2019-06-20 stsp return NULL;
1826 96e2b566 2019-07-08 stsp if (s->search_entry) {
1827 13add988 2019-10-15 stsp int errcode, ch;
1828 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1829 13add988 2019-10-15 stsp if (errcode)
1830 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
1831 13add988 2019-10-15 stsp "pthread_mutex_unlock");
1832 13add988 2019-10-15 stsp ch = wgetch(view->window);
1833 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
1834 13add988 2019-10-15 stsp if (errcode)
1835 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
1836 13add988 2019-10-15 stsp "pthread_mutex_lock");
1837 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
1838 678cbce5 2019-07-28 stsp view->search_next_done = 1;
1839 678cbce5 2019-07-28 stsp return NULL;
1841 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
1842 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
1844 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
1845 96e2b566 2019-07-08 stsp commit_queue_head, entry);
1846 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
1847 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1848 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
1850 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
1851 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
1853 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1854 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
1856 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1859 60493ae3 2019-06-20 stsp while (1) {
1860 13add988 2019-10-15 stsp int have_match = 0;
1862 60493ae3 2019-06-20 stsp if (entry == NULL) {
1863 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
1864 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
1865 f801134a 2019-06-25 stsp view->search_next_done = 1;
1866 f801134a 2019-06-25 stsp return NULL;
1869 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
1870 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
1871 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
1873 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
1874 57b33b64 2019-07-08 stsp return trigger_log_thread(1,
1875 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
1876 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
1877 f801134a 2019-06-25 stsp &s->thread_args.need_commits);
1880 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
1881 13add988 2019-10-15 stsp &view->regex);
1884 13add988 2019-10-15 stsp if (have_match) {
1885 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1886 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
1890 96e2b566 2019-07-08 stsp s->search_entry = entry;
1891 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1892 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
1894 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1897 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
1898 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
1899 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
1900 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1902 60493ae3 2019-06-20 stsp return err;
1905 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
1906 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1908 60493ae3 2019-06-20 stsp return err;
1913 96e2b566 2019-07-08 stsp s->search_entry = NULL;
1915 60493ae3 2019-06-20 stsp return NULL;
1918 60493ae3 2019-06-20 stsp static const struct got_error *
1919 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1920 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
1921 d01904d4 2019-06-25 stsp const char *head_ref_name, const char *path, int check_disk)
1923 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1924 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1925 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1926 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1927 1a76625f 2018-10-22 stsp int errcode;
1929 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1930 ecb28ae0 2018-07-16 stsp if (err != NULL)
1931 ecb28ae0 2018-07-16 stsp goto done;
1933 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1934 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1935 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1937 8b473291 2019-02-21 stsp s->refs = refs;
1938 fb2756b9 2018-08-04 stsp s->repo = repo;
1939 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
1940 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1941 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1942 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1943 5036bf37 2018-09-24 stsp goto done;
1946 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1947 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1948 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1949 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
1950 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
1952 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
1954 1a76625f 2018-10-22 stsp goto done;
1955 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1956 1a76625f 2018-10-22 stsp 0, thread_repo);
1958 1a76625f 2018-10-22 stsp goto done;
1960 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1961 1a76625f 2018-10-22 stsp if (errcode) {
1962 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
1963 1a76625f 2018-10-22 stsp goto done;
1966 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1967 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1968 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1969 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1970 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1971 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1972 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1973 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1974 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1975 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1976 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
1977 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
1978 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
1981 1a76625f 2018-10-22 stsp close_log_view(view);
1982 ba4f502b 2018-08-04 stsp return err;
1985 e5a0f69f 2018-08-18 stsp static const struct got_error *
1986 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1988 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1990 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1991 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1992 2b380cc8 2018-10-24 stsp &s->thread_args);
1993 2b380cc8 2018-10-24 stsp if (errcode)
1994 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
1997 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1998 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1999 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
2000 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
2003 e5a0f69f 2018-08-18 stsp static const struct got_error *
2004 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2005 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2007 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2008 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2009 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2010 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2011 669b5ffa 2018-10-07 stsp int begin_x = 0;
2012 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2014 e5a0f69f 2018-08-18 stsp switch (ch) {
2016 1e37a5c2 2019-05-12 jcs s->quit = 1;
2019 1e37a5c2 2019-05-12 jcs case KEY_UP:
2022 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2024 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2025 1e37a5c2 2019-05-12 jcs s->selected--;
2027 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
2028 1144d21a 2019-06-21 stsp &s->commits);
2030 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2031 a4292ac5 2019-05-12 jcs case CTRL('b'):
2032 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2034 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
2035 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
2036 1e37a5c2 2019-05-12 jcs s->selected = 0;
2039 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
2040 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
2043 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2046 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2048 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2049 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2050 1e37a5c2 2019-05-12 jcs s->selected++;
2053 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
2054 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
2055 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
2056 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2057 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2059 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2060 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2061 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2062 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2063 1e37a5c2 2019-05-12 jcs if (first == NULL)
2065 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
2066 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
2067 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
2068 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2069 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2072 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2073 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2074 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2075 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2076 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2077 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2079 1e37a5c2 2019-05-12 jcs err = NULL;
2082 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2083 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2084 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2085 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2086 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2088 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2091 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2093 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2094 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2095 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2096 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2097 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
2100 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2101 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2103 1e37a5c2 2019-05-12 jcs return err;
2104 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2106 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2109 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2110 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2112 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2115 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2117 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2118 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2119 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2120 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2123 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2124 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2126 1e37a5c2 2019-05-12 jcs return err;
2127 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2129 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2132 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2133 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2135 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2137 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2138 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2140 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2141 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2142 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2144 1e37a5c2 2019-05-12 jcs return err;
2145 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2146 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2147 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2148 638f9024 2019-05-13 stsp return got_error_from_errno(
2149 1e37a5c2 2019-05-12 jcs "view_open");
2150 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2151 d01904d4 2019-06-25 stsp s->repo, s->head_ref_name, parent_path, 0);
2153 1e37a5c2 2019-05-12 jcs return err;;
2154 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2155 1e37a5c2 2019-05-12 jcs *new_view = lv;
2157 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2158 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2160 1e37a5c2 2019-05-12 jcs return NULL;
2163 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2164 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2166 d01904d4 2019-06-25 stsp return err;
2167 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2168 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2169 d01904d4 2019-06-25 stsp if (lv == NULL)
2170 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2171 d01904d4 2019-06-25 stsp err = get_head_commit_id(&start_id, s->head_ref_name ?
2172 d01904d4 2019-06-25 stsp s->head_ref_name : GOT_REF_HEAD, s->repo);
2173 ef129c5e 2019-08-03 stsp if (err) {
2174 ef129c5e 2019-08-03 stsp view_close(lv);
2175 d01904d4 2019-06-25 stsp return err;
2177 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2178 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2179 e6b23735 2019-10-04 stsp free(start_id);
2180 e6b23735 2019-10-04 stsp view_close(lv);
2181 e6b23735 2019-10-04 stsp return got_error_from_errno("strdup");
2183 e6b23735 2019-10-04 stsp got_ref_list_free(s->refs);
2184 e6b23735 2019-10-04 stsp err = got_ref_list(s->refs, s->repo, NULL,
2185 e6b23735 2019-10-04 stsp got_ref_cmp_by_name, NULL);
2186 e6b23735 2019-10-04 stsp if (err) {
2187 d01904d4 2019-06-25 stsp free(start_id);
2188 ef129c5e 2019-08-03 stsp view_close(lv);
2189 50284065 2019-10-04 stsp return err;
2191 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2192 d01904d4 2019-06-25 stsp s->head_ref_name, in_repo_path, 0);
2193 ef129c5e 2019-08-03 stsp if (err) {
2194 ef129c5e 2019-08-03 stsp free(start_id);
2195 ef129c5e 2019-08-03 stsp view_close(lv);
2196 d01904d4 2019-06-25 stsp return err;;
2198 d01904d4 2019-06-25 stsp *dead_view = view;
2199 d01904d4 2019-06-25 stsp *new_view = lv;
2205 80ddbec8 2018-04-29 stsp return err;
2208 4ed7e80c 2018-05-20 stsp static const struct got_error *
2209 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2211 c2db6724 2019-01-04 stsp const struct got_error *error;
2213 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2214 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2215 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2217 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2218 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2220 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2221 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2223 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2224 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2226 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2227 c2db6724 2019-01-04 stsp if (error != NULL)
2228 c2db6724 2019-01-04 stsp return error;
2230 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2231 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2233 c2db6724 2019-01-04 stsp return NULL;
2236 a915003a 2019-02-05 stsp static void
2237 a915003a 2019-02-05 stsp init_curses(void)
2239 a915003a 2019-02-05 stsp initscr();
2241 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2244 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2245 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2246 a915003a 2019-02-05 stsp curs_set(0);
2247 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2248 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2251 c2db6724 2019-01-04 stsp static const struct got_error *
2252 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2254 80ddbec8 2018-04-29 stsp const struct got_error *error;
2255 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2256 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2257 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2258 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2259 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2260 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2262 04cc582a 2018-08-01 stsp struct tog_view *view;
2264 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2266 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2267 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2268 c2db6724 2019-01-04 stsp NULL) == -1)
2269 80ddbec8 2018-04-29 stsp err(1, "pledge");
2272 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2273 80ddbec8 2018-04-29 stsp switch (ch) {
2275 80ddbec8 2018-04-29 stsp start_commit = optarg;
2278 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2279 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2280 ecb28ae0 2018-07-16 stsp err(1, "-r option");
2283 17020d27 2019-03-07 stsp usage_log();
2284 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2288 80ddbec8 2018-04-29 stsp argc -= optind;
2289 80ddbec8 2018-04-29 stsp argv += optind;
2291 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2292 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2293 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2294 ecb28ae0 2018-07-16 stsp goto done;
2296 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2297 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2298 963f97a1 2019-03-18 stsp goto done;
2299 963f97a1 2019-03-18 stsp error = NULL;
2301 963f97a1 2019-03-18 stsp if (argc == 0) {
2302 963f97a1 2019-03-18 stsp path = strdup("");
2303 963f97a1 2019-03-18 stsp if (path == NULL) {
2304 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2305 ecb28ae0 2018-07-16 stsp goto done;
2307 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2308 963f97a1 2019-03-18 stsp if (worktree) {
2309 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2311 963f97a1 2019-03-18 stsp if (error)
2312 963f97a1 2019-03-18 stsp goto done;
2314 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2315 963f97a1 2019-03-18 stsp if (path == NULL) {
2316 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2317 963f97a1 2019-03-18 stsp goto done;
2321 963f97a1 2019-03-18 stsp usage_log();
2323 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2324 a1fbf39a 2019-08-11 stsp if (worktree)
2325 b9d7675a 2019-08-11 stsp repo_path = strdup(
2326 b9d7675a 2019-08-11 stsp got_worktree_get_repo_path(worktree));
2328 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2330 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2331 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2332 963f97a1 2019-03-18 stsp goto done;
2335 a915003a 2019-02-05 stsp init_curses();
2337 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2338 80ddbec8 2018-04-29 stsp if (error != NULL)
2339 ecb28ae0 2018-07-16 stsp goto done;
2341 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2342 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2343 c02c541e 2019-03-29 stsp if (error)
2344 c02c541e 2019-03-29 stsp goto done;
2346 15a94983 2018-12-23 stsp if (start_commit == NULL)
2347 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&start_id, worktree ?
2348 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2351 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&start_id, start_commit, repo);
2352 f2b6a97d 2019-07-15 stsp if (error) {
2353 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
2354 f2b6a97d 2019-07-15 stsp goto done;
2355 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&start_id,
2356 f2b6a97d 2019-07-15 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2359 80ddbec8 2018-04-29 stsp if (error != NULL)
2360 8b473291 2019-02-21 stsp goto done;
2362 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2363 8b473291 2019-02-21 stsp if (error)
2364 ecb28ae0 2018-07-16 stsp goto done;
2366 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2367 04cc582a 2018-08-01 stsp if (view == NULL) {
2368 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2369 04cc582a 2018-08-01 stsp goto done;
2371 2fc00ff4 2019-08-31 stsp if (worktree) {
2372 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2373 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2374 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2375 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2376 2fc00ff4 2019-08-31 stsp goto done;
2379 2fc00ff4 2019-08-31 stsp error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2381 ba4f502b 2018-08-04 stsp if (error)
2382 ba4f502b 2018-08-04 stsp goto done;
2383 2fc00ff4 2019-08-31 stsp if (worktree) {
2384 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2385 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2386 2fc00ff4 2019-08-31 stsp worktree = NULL;
2388 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2390 ecb28ae0 2018-07-16 stsp free(repo_path);
2391 ecb28ae0 2018-07-16 stsp free(cwd);
2392 ecb28ae0 2018-07-16 stsp free(path);
2393 899d86c2 2018-05-10 stsp free(start_id);
2394 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2396 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2397 ec142235 2019-03-07 stsp if (worktree)
2398 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2399 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2400 80ddbec8 2018-04-29 stsp return error;
2403 4ed7e80c 2018-05-20 stsp __dead static void
2404 9f7d7167 2018-04-29 stsp usage_diff(void)
2407 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2408 9f7d7167 2018-04-29 stsp getprogname());
2412 b304db33 2018-05-20 stsp static char *
2413 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2415 b304db33 2018-05-20 stsp char *line;
2416 b304db33 2018-05-20 stsp size_t linelen;
2417 b304db33 2018-05-20 stsp size_t lineno;
2418 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2420 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2422 b304db33 2018-05-20 stsp *len = linelen;
2423 b304db33 2018-05-20 stsp return line;
2426 4ed7e80c 2018-05-20 stsp static const struct got_error *
2427 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2428 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
2429 c3e9aa98 2019-05-13 jcs char *header)
2431 61e69b96 2018-05-20 stsp const struct got_error *err;
2432 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2433 b304db33 2018-05-20 stsp char *line;
2434 b304db33 2018-05-20 stsp size_t len;
2435 61e69b96 2018-05-20 stsp wchar_t *wline;
2436 e0b650dd 2018-05-20 stsp int width;
2438 26ed57b2 2018-05-19 stsp rewind(f);
2439 f7d12f7e 2018-08-01 stsp werase(view->window);
2441 a3404814 2018-09-02 stsp if (header) {
2442 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
2443 a3404814 2018-09-02 stsp if (err) {
2444 a3404814 2018-09-02 stsp return err;
2447 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2448 a3404814 2018-09-02 stsp wstandout(view->window);
2449 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2450 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2451 a3404814 2018-09-02 stsp wstandend(view->window);
2452 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2453 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2455 a3404814 2018-09-02 stsp if (max_lines <= 1)
2456 a3404814 2018-09-02 stsp return NULL;
2457 a3404814 2018-09-02 stsp max_lines--;
2461 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2462 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2463 26ed57b2 2018-05-19 stsp if (line == NULL) {
2467 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2468 26ed57b2 2018-05-19 stsp free(line);
2472 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2473 61e69b96 2018-05-20 stsp if (err) {
2474 61e69b96 2018-05-20 stsp free(line);
2475 61e69b96 2018-05-20 stsp return err;
2477 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2478 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2479 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2480 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2481 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2482 26ed57b2 2018-05-19 stsp free(line);
2483 2550e4c3 2018-07-13 stsp free(wline);
2484 2550e4c3 2018-07-13 stsp wline = NULL;
2486 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2488 1a57306a 2018-09-02 stsp view_vborder(view);
2490 c3e9aa98 2019-05-13 jcs if (*eof) {
2491 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2492 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2493 c3e9aa98 2019-05-13 jcs nprinted++;
2496 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2498 c3e9aa98 2019-05-13 jcs return err;
2501 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2502 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2503 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2506 26ed57b2 2018-05-19 stsp return NULL;
2509 abd2672a 2018-12-23 stsp static char *
2510 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2512 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2513 09867e48 2019-08-13 stsp char *p, *s;
2515 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2516 09867e48 2019-08-13 stsp if (tm == NULL)
2517 09867e48 2019-08-13 stsp return NULL;
2518 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2519 09867e48 2019-08-13 stsp if (s == NULL)
2520 09867e48 2019-08-13 stsp return NULL;
2521 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2523 abd2672a 2018-12-23 stsp *p = '\0';
2527 4ed7e80c 2018-05-20 stsp static const struct got_error *
2528 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2529 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2531 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2532 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
2533 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2534 5943eee2 2019-08-13 stsp char *id_str = NULL, *logmsg = NULL;
2535 45d799e2 2018-12-23 stsp time_t committer_time;
2536 45d799e2 2018-12-23 stsp const char *author, *committer;
2537 8b473291 2019-02-21 stsp char *refs_str = NULL;
2539 8b473291 2019-02-21 stsp if (refs) {
2540 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
2542 8b473291 2019-02-21 stsp return err;
2545 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2547 abd2672a 2018-12-23 stsp return err;
2549 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2550 15a94983 2018-12-23 stsp if (err) {
2551 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2552 15a94983 2018-12-23 stsp goto done;
2555 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2556 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2557 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2558 abd2672a 2018-12-23 stsp goto done;
2560 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2561 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2562 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2563 abd2672a 2018-12-23 stsp goto done;
2565 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2566 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
2567 09867e48 2019-08-13 stsp if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2568 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2569 abd2672a 2018-12-23 stsp goto done;
2571 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2572 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2573 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2574 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2575 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2576 abd2672a 2018-12-23 stsp goto done;
2578 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2580 5943eee2 2019-08-13 stsp goto done;
2581 5943eee2 2019-08-13 stsp if (fprintf(outfile, "%s\n", logmsg) < 0) {
2582 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2583 abd2672a 2018-12-23 stsp goto done;
2586 abd2672a 2018-12-23 stsp free(id_str);
2587 5943eee2 2019-08-13 stsp free(logmsg);
2588 8b473291 2019-02-21 stsp free(refs_str);
2589 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2590 abd2672a 2018-12-23 stsp return err;
2593 abd2672a 2018-12-23 stsp static const struct got_error *
2594 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2596 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2597 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2598 15a94983 2018-12-23 stsp int obj_type;
2600 511a516b 2018-05-19 stsp f = got_opentemp();
2601 48ae06ee 2018-10-18 stsp if (f == NULL) {
2602 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2603 48ae06ee 2018-10-18 stsp goto done;
2605 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2606 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2607 fb43ecf1 2019-02-11 stsp goto done;
2611 15a94983 2018-12-23 stsp if (s->id1)
2612 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2614 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2616 15a94983 2018-12-23 stsp goto done;
2618 15a94983 2018-12-23 stsp switch (obj_type) {
2619 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2620 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2621 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2623 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2624 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2625 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2627 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2628 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2629 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2630 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2632 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2635 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2636 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2637 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2639 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2640 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2641 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2642 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2643 8b473291 2019-02-21 stsp s->repo, f);
2648 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2650 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2651 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2655 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2659 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2660 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2661 48ae06ee 2018-10-18 stsp return err;
2664 f5215bb9 2019-02-22 stsp static void
2665 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2667 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2668 f5215bb9 2019-02-22 stsp update_panels();
2669 f5215bb9 2019-02-22 stsp doupdate();
2672 48ae06ee 2018-10-18 stsp static const struct got_error *
2673 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2674 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2675 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2677 48ae06ee 2018-10-18 stsp const struct got_error *err;
2679 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2680 15a94983 2018-12-23 stsp int type1, type2;
2681 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2683 15a94983 2018-12-23 stsp return err;
2684 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2686 15a94983 2018-12-23 stsp return err;
2688 15a94983 2018-12-23 stsp if (type1 != type2)
2689 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2692 15a94983 2018-12-23 stsp if (id1) {
2693 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2694 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2695 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2697 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2699 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2700 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2701 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2702 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2703 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2705 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2706 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2707 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2708 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2709 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2710 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2711 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2713 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2714 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2715 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2717 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2718 48ae06ee 2018-10-18 stsp if (err) {
2719 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2720 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2721 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2722 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2723 48ae06ee 2018-10-18 stsp return err;
2726 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2727 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2728 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2730 5dc9f4bc 2018-08-04 stsp return NULL;
2733 e5a0f69f 2018-08-18 stsp static const struct got_error *
2734 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2736 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2738 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2739 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2740 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2741 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2742 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2743 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2744 e5a0f69f 2018-08-18 stsp return err;
2747 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2748 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2750 a3404814 2018-09-02 stsp const struct got_error *err;
2751 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2752 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2754 a3404814 2018-09-02 stsp if (s->id1) {
2755 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2757 a3404814 2018-09-02 stsp return err;
2759 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2761 a3404814 2018-09-02 stsp return err;
2763 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2764 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2765 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2766 a3404814 2018-09-02 stsp free(id_str1);
2767 a3404814 2018-09-02 stsp free(id_str2);
2768 a3404814 2018-09-02 stsp return err;
2770 a3404814 2018-09-02 stsp free(id_str1);
2771 a3404814 2018-09-02 stsp free(id_str2);
2773 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2774 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2778 15a087fe 2019-02-21 stsp static const struct got_error *
2779 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2780 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2782 d7a04538 2019-02-21 stsp const struct got_error *err;
2783 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
2784 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
2785 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
2787 15a087fe 2019-02-21 stsp free(s->id2);
2788 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2789 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2790 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2792 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2794 d7a04538 2019-02-21 stsp return err;
2795 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
2796 15a087fe 2019-02-21 stsp free(s->id1);
2797 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
2798 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2799 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
2800 15a087fe 2019-02-21 stsp return NULL;
2803 0cf4efb1 2018-09-29 stsp static const struct got_error *
2804 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2805 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2807 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2808 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2809 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
2810 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
2813 e5a0f69f 2018-08-18 stsp switch (ch) {
2815 1e37a5c2 2019-05-12 jcs case KEY_UP:
2816 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
2817 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2819 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2820 a60a9dc4 2019-05-13 jcs case CTRL('b'):
2821 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
2824 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
2825 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
2826 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2829 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2830 1e37a5c2 2019-05-12 jcs if (!s->eof)
2831 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2833 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
2834 a60a9dc4 2019-05-13 jcs case CTRL('f'):
2836 00ba99a7 2019-05-12 jcs if (s->eof)
2839 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
2840 1e37a5c2 2019-05-12 jcs char *line;
2841 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
2842 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2843 1e37a5c2 2019-05-12 jcs if (line == NULL)
2848 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
2849 1e37a5c2 2019-05-12 jcs s->diff_context--;
2850 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2851 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2855 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2856 1e37a5c2 2019-05-12 jcs s->diff_context++;
2857 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2858 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2863 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2865 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2866 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
2867 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
2868 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2871 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2876 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2880 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2881 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2883 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2884 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2888 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2890 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2892 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2893 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
2895 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
2896 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
2897 1e37a5c2 2019-05-12 jcs update_panels();
2898 1e37a5c2 2019-05-12 jcs doupdate();
2900 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
2901 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
2902 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
2903 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
2907 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2912 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
2913 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2916 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2920 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2921 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2923 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2924 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2930 bcbd79e2 2018-08-19 stsp return err;
2933 4ed7e80c 2018-05-20 stsp static const struct got_error *
2934 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2936 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2937 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2938 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2939 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2940 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2941 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2943 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2945 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2947 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2948 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2949 eb6600df 2019-01-04 stsp NULL) == -1)
2950 26ed57b2 2018-05-19 stsp err(1, "pledge");
2953 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2954 26ed57b2 2018-05-19 stsp switch (ch) {
2956 17020d27 2019-03-07 stsp usage_diff();
2957 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2961 26ed57b2 2018-05-19 stsp argc -= optind;
2962 26ed57b2 2018-05-19 stsp argv += optind;
2964 26ed57b2 2018-05-19 stsp if (argc == 0) {
2965 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2966 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2967 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2968 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2969 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2970 15a94983 2018-12-23 stsp id_str1 = argv[0];
2971 15a94983 2018-12-23 stsp id_str2 = argv[1];
2972 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2973 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2974 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2975 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2976 15a94983 2018-12-23 stsp id_str1 = argv[1];
2977 15a94983 2018-12-23 stsp id_str2 = argv[2];
2979 26ed57b2 2018-05-19 stsp usage_diff();
2981 a915003a 2019-02-05 stsp init_curses();
2983 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2984 eb6600df 2019-01-04 stsp if (error)
2985 eb6600df 2019-01-04 stsp goto done;
2987 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
2988 26ed57b2 2018-05-19 stsp if (error)
2989 26ed57b2 2018-05-19 stsp goto done;
2991 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
2992 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
2993 26ed57b2 2018-05-19 stsp if (error)
2994 26ed57b2 2018-05-19 stsp goto done;
2996 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
2997 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
2998 26ed57b2 2018-05-19 stsp if (error)
2999 26ed57b2 2018-05-19 stsp goto done;
3001 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3002 8b473291 2019-02-21 stsp if (error)
3003 8b473291 2019-02-21 stsp goto done;
3005 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3006 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3007 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3008 ea5e7bb5 2018-08-01 stsp goto done;
3010 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3011 5dc9f4bc 2018-08-04 stsp if (error)
3012 5dc9f4bc 2018-08-04 stsp goto done;
3013 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3015 c02c541e 2019-03-29 stsp free(repo_path);
3017 921be706 2019-06-28 stsp got_repo_close(repo);
3018 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3019 26ed57b2 2018-05-19 stsp return error;
3022 4ed7e80c 2018-05-20 stsp __dead static void
3023 9f7d7167 2018-04-29 stsp usage_blame(void)
3026 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3027 9f7d7167 2018-04-29 stsp getprogname());
3031 84451b3e 2018-07-10 stsp struct tog_blame_line {
3032 84451b3e 2018-07-10 stsp int annotated;
3033 84451b3e 2018-07-10 stsp struct got_object_id *id;
3036 4ed7e80c 2018-05-20 stsp static const struct got_error *
3037 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3038 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
3039 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
3040 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
3042 84451b3e 2018-07-10 stsp const struct got_error *err;
3043 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3044 84451b3e 2018-07-10 stsp char *line;
3045 84451b3e 2018-07-10 stsp size_t len;
3046 84451b3e 2018-07-10 stsp wchar_t *wline;
3047 27a741e5 2019-09-11 stsp int width;
3048 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3049 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3050 ab089a2a 2018-07-12 stsp char *id_str;
3052 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
3054 ab089a2a 2018-07-12 stsp return err;
3056 84451b3e 2018-07-10 stsp rewind(f);
3057 f7d12f7e 2018-08-01 stsp werase(view->window);
3059 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3060 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3061 ab089a2a 2018-07-12 stsp free(id_str);
3062 ab089a2a 2018-07-12 stsp return err;
3065 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3066 ab089a2a 2018-07-12 stsp free(line);
3067 2550e4c3 2018-07-13 stsp line = NULL;
3069 1cae65b4 2019-09-22 stsp return err;
3070 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3071 a3404814 2018-09-02 stsp wstandout(view->window);
3072 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3073 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3074 a3404814 2018-09-02 stsp wstandend(view->window);
3075 2550e4c3 2018-07-13 stsp free(wline);
3076 2550e4c3 2018-07-13 stsp wline = NULL;
3077 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3078 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3080 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3081 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
3082 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
3083 ab089a2a 2018-07-12 stsp free(id_str);
3084 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3086 ab089a2a 2018-07-12 stsp free(id_str);
3087 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3088 3f60a8ef 2018-07-10 stsp free(line);
3089 2550e4c3 2018-07-13 stsp line = NULL;
3091 3f60a8ef 2018-07-10 stsp return err;
3092 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3093 2550e4c3 2018-07-13 stsp free(wline);
3094 2550e4c3 2018-07-13 stsp wline = NULL;
3095 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3096 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3099 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
3100 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
3101 84451b3e 2018-07-10 stsp if (line == NULL) {
3105 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
3106 84451b3e 2018-07-10 stsp free(line);
3110 27a741e5 2019-09-11 stsp if (view->ncols <= 9) {
3111 27a741e5 2019-09-11 stsp width = 9;
3112 27a741e5 2019-09-11 stsp wline = wcsdup(L"");
3113 27a741e5 2019-09-11 stsp if (wline == NULL)
3114 27a741e5 2019-09-11 stsp err = got_error_from_errno("wcsdup");
3116 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line,
3117 27a741e5 2019-09-11 stsp view->ncols - 9, 9);
3118 27a741e5 2019-09-11 stsp width += 9;
3120 84451b3e 2018-07-10 stsp if (err) {
3121 84451b3e 2018-07-10 stsp free(line);
3122 84451b3e 2018-07-10 stsp return err;
3125 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3126 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3128 8d0fe45a 2019-08-12 stsp if (nlines > 0) {
3129 8d0fe45a 2019-08-12 stsp blame_line = &lines[lineno - 1];
3130 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3131 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3132 27a741e5 2019-09-11 stsp !(view->focussed &&
3133 27a741e5 2019-09-11 stsp nprinted == selected_line - 1)) {
3134 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3135 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3136 8d0fe45a 2019-08-12 stsp char *id_str;
3137 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3138 8d0fe45a 2019-08-12 stsp if (err) {
3139 8d0fe45a 2019-08-12 stsp free(line);
3140 8d0fe45a 2019-08-12 stsp free(wline);
3141 8d0fe45a 2019-08-12 stsp return err;
3143 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3144 8d0fe45a 2019-08-12 stsp free(id_str);
3145 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3147 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3148 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3151 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3152 ee41ec32 2018-07-10 stsp prev_id = NULL;
3155 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3156 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3157 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3159 27a741e5 2019-09-11 stsp waddwstr(view->window, wline);
3160 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
3161 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
3162 84451b3e 2018-07-10 stsp if (++nprinted == 1)
3163 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
3164 84451b3e 2018-07-10 stsp free(line);
3165 2550e4c3 2018-07-13 stsp free(wline);
3166 2550e4c3 2018-07-13 stsp wline = NULL;
3168 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
3170 1a57306a 2018-09-02 stsp view_vborder(view);
3172 84451b3e 2018-07-10 stsp return NULL;
3175 84451b3e 2018-07-10 stsp static const struct got_error *
3176 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3178 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
3179 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
3180 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
3181 1a76625f 2018-10-22 stsp int errcode;
3183 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
3184 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3185 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3187 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3188 1a76625f 2018-10-22 stsp if (errcode)
3189 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3191 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3192 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3193 d68a0a7d 2018-07-10 stsp goto done;
3196 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3197 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3199 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3200 d68a0a7d 2018-07-10 stsp if (line->annotated)
3201 d68a0a7d 2018-07-10 stsp goto done;
3203 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3204 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3205 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3206 84451b3e 2018-07-10 stsp goto done;
3208 84451b3e 2018-07-10 stsp line->annotated = 1;
3210 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3211 1a76625f 2018-10-22 stsp if (errcode)
3212 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3213 84451b3e 2018-07-10 stsp return err;
3216 84451b3e 2018-07-10 stsp static void *
3217 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3219 18430de3 2018-07-10 stsp const struct got_error *err;
3220 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3221 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3222 1a76625f 2018-10-22 stsp int errcode;
3224 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
3225 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3226 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
3227 fc06ba56 2019-08-22 stsp err = NULL;
3229 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3230 1a76625f 2018-10-22 stsp if (errcode)
3231 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3232 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3234 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3235 c9beca56 2018-07-22 stsp ta->repo = NULL;
3236 c9beca56 2018-07-22 stsp *ta->complete = 1;
3238 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3239 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3240 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3242 18430de3 2018-07-10 stsp return (void *)err;
3245 245d91c1 2018-07-12 stsp static struct got_object_id *
3246 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3247 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
3249 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3251 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
3252 8d0fe45a 2019-08-12 stsp return NULL;
3254 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3255 245d91c1 2018-07-12 stsp if (!line->annotated)
3256 245d91c1 2018-07-12 stsp return NULL;
3258 245d91c1 2018-07-12 stsp return line->id;
3261 b880a918 2018-07-10 stsp static const struct got_error *
3262 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3264 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3267 245d91c1 2018-07-12 stsp if (blame->thread) {
3268 1a76625f 2018-10-22 stsp int errcode;
3269 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3270 1a76625f 2018-10-22 stsp if (errcode)
3271 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3272 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3273 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3274 1a76625f 2018-10-22 stsp if (errcode)
3275 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3276 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3277 1a76625f 2018-10-22 stsp if (errcode)
3278 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3279 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3280 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3281 245d91c1 2018-07-12 stsp err = NULL;
3282 245d91c1 2018-07-12 stsp blame->thread = NULL;
3284 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3285 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3286 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3288 245d91c1 2018-07-12 stsp if (blame->f) {
3289 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3290 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3291 245d91c1 2018-07-12 stsp blame->f = NULL;
3293 57670559 2018-12-23 stsp if (blame->lines) {
3294 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3295 57670559 2018-12-23 stsp free(blame->lines[i].id);
3296 57670559 2018-12-23 stsp free(blame->lines);
3297 57670559 2018-12-23 stsp blame->lines = NULL;
3299 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3300 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3302 245d91c1 2018-07-12 stsp return err;
3305 245d91c1 2018-07-12 stsp static const struct got_error *
3306 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
3308 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
3309 fc06ba56 2019-08-22 stsp int *done = arg;
3310 fc06ba56 2019-08-22 stsp int errcode;
3312 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3313 fc06ba56 2019-08-22 stsp if (errcode)
3314 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3315 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
3317 fc06ba56 2019-08-22 stsp if (*done)
3318 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
3320 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3321 fc06ba56 2019-08-22 stsp if (errcode)
3322 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3323 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
3325 fc06ba56 2019-08-22 stsp return err;
3328 fc06ba56 2019-08-22 stsp static const struct got_error *
3329 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3330 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3331 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3332 245d91c1 2018-07-12 stsp struct got_repository *repo)
3334 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3335 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3336 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3337 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3338 15a94983 2018-12-23 stsp int obj_type;
3340 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3342 15a94983 2018-12-23 stsp return err;
3343 15a94983 2018-12-23 stsp if (obj_id == NULL)
3344 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
3346 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3348 84451b3e 2018-07-10 stsp goto done;
3350 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3351 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3352 84451b3e 2018-07-10 stsp goto done;
3355 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3357 a70480e0 2018-06-23 stsp goto done;
3358 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3359 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3360 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3361 84451b3e 2018-07-10 stsp goto done;
3363 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3364 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
3365 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
3366 84451b3e 2018-07-10 stsp goto done;
3368 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3369 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3370 b02560ec 2019-08-19 stsp blame->nlines--;
3372 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3373 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3374 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3375 84451b3e 2018-07-10 stsp goto done;
3378 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3380 bd24772e 2018-07-11 stsp goto done;
3382 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3383 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3384 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3385 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3386 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3387 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3388 245d91c1 2018-07-12 stsp goto done;
3390 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3392 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3393 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3394 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3395 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3396 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
3397 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_arg = done;
3398 245d91c1 2018-07-12 stsp *blame_complete = 0;
3402 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3403 27d434c2 2018-09-15 stsp free(obj_id);
3405 245d91c1 2018-07-12 stsp stop_blame(blame);
3406 245d91c1 2018-07-12 stsp return err;
3409 245d91c1 2018-07-12 stsp static const struct got_error *
3410 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3411 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3412 8b473291 2019-02-21 stsp struct got_repository *repo)
3414 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3415 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3417 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3419 c4843652 2019-08-12 stsp s->path = strdup(path);
3420 c4843652 2019-08-12 stsp if (s->path == NULL)
3421 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
3423 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3424 c4843652 2019-08-12 stsp if (err) {
3425 c4843652 2019-08-12 stsp free(s->path);
3426 7cbe629d 2018-08-04 stsp return err;
3429 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3430 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3431 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3432 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3433 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3434 fb2756b9 2018-08-04 stsp s->repo = repo;
3435 8b473291 2019-02-21 stsp s->refs = refs;
3436 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3437 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3439 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3440 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3441 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3442 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
3443 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
3445 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3446 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3447 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3448 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3451 e5a0f69f 2018-08-18 stsp static const struct got_error *
3452 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3454 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3455 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3457 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3458 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3460 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3461 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3462 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3463 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3464 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3467 e5a0f69f 2018-08-18 stsp free(s->path);
3469 e5a0f69f 2018-08-18 stsp return err;
3472 7cbe629d 2018-08-04 stsp static const struct got_error *
3473 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
3475 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3477 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
3478 6c4c42e0 2019-06-24 stsp return NULL;
3481 6c4c42e0 2019-06-24 stsp static int
3482 6c4c42e0 2019-06-24 stsp match_line(const char *line, regex_t *regex)
3484 6c4c42e0 2019-06-24 stsp regmatch_t regmatch;
3486 6c4c42e0 2019-06-24 stsp return regexec(regex, line, 1, ®match, 0) == 0;
3490 6c4c42e0 2019-06-24 stsp static const struct got_error *
3491 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
3493 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3494 6c4c42e0 2019-06-24 stsp int lineno;
3496 6c4c42e0 2019-06-24 stsp if (!view->searching) {
3497 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3498 6c4c42e0 2019-06-24 stsp return NULL;
3501 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3502 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3503 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
3505 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
3507 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3508 6c4c42e0 2019-06-24 stsp lineno = 1;
3510 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3513 6c4c42e0 2019-06-24 stsp while (1) {
3514 6c4c42e0 2019-06-24 stsp char *line = NULL;
3515 6c4c42e0 2019-06-24 stsp off_t offset;
3516 6c4c42e0 2019-06-24 stsp size_t len;
3518 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
3519 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
3520 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3521 6c4c42e0 2019-06-24 stsp free(line);
3525 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3526 6c4c42e0 2019-06-24 stsp lineno = 1;
3528 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3531 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
3532 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3533 6c4c42e0 2019-06-24 stsp free(line);
3534 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
3536 6c4c42e0 2019-06-24 stsp free(line);
3537 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
3538 2246482e 2019-06-25 stsp if (line && match_line(line, &view->regex)) {
3539 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3540 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
3541 6c4c42e0 2019-06-24 stsp free(line);
3544 6c4c42e0 2019-06-24 stsp free(line);
3545 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3551 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3552 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
3553 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
3556 6c4c42e0 2019-06-24 stsp return NULL;
3559 6c4c42e0 2019-06-24 stsp static const struct got_error *
3560 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
3562 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3563 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
3564 2b380cc8 2018-10-24 stsp int errcode;
3566 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
3567 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3568 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
3569 2b380cc8 2018-10-24 stsp if (errcode)
3570 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3572 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
3575 51fe7530 2019-08-19 stsp if (s->blame_complete)
3576 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
3578 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3579 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3580 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
3581 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
3583 669b5ffa 2018-10-07 stsp view_vborder(view);
3584 e5a0f69f 2018-08-18 stsp return err;
3587 e5a0f69f 2018-08-18 stsp static const struct got_error *
3588 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3589 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3591 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
3592 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
3593 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3594 669b5ffa 2018-10-07 stsp int begin_x = 0;
3596 e5a0f69f 2018-08-18 stsp switch (ch) {
3598 1e37a5c2 2019-05-12 jcs s->done = 1;
3601 1e37a5c2 2019-05-12 jcs case KEY_UP:
3602 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
3603 1e37a5c2 2019-05-12 jcs s->selected_line--;
3604 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
3605 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3606 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3608 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3609 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
3610 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3613 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3614 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3615 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3617 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3620 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3621 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3622 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3623 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3624 1e37a5c2 2019-05-12 jcs s->selected_line++;
3625 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3626 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3627 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3630 1e37a5c2 2019-05-12 jcs case 'p': {
3631 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3632 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3633 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3634 1e37a5c2 2019-05-12 jcs if (id == NULL)
3636 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3637 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3638 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3639 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3640 1e37a5c2 2019-05-12 jcs int obj_type;
3641 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3642 1e37a5c2 2019-05-12 jcs s->repo, id);
3645 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3646 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3647 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3648 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3651 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3652 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3653 1e37a5c2 2019-05-12 jcs pid->id, s->path);
3654 e5a0f69f 2018-08-18 stsp if (err) {
3655 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
3656 1e37a5c2 2019-05-12 jcs err = NULL;
3657 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3660 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
3662 1e37a5c2 2019-05-12 jcs free(blob_id);
3663 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
3664 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
3665 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3668 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3670 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3672 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
3673 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
3675 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3680 1e37a5c2 2019-05-12 jcs s->done = 1;
3681 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3682 1e37a5c2 2019-05-12 jcs s->done = 0;
3683 1e37a5c2 2019-05-12 jcs if (thread_err)
3685 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3686 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
3687 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3688 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3689 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
3690 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
3695 1e37a5c2 2019-05-12 jcs case 'B': {
3696 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
3697 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
3698 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
3700 1e37a5c2 2019-05-12 jcs s->done = 1;
3701 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3702 1e37a5c2 2019-05-12 jcs s->done = 0;
3703 1e37a5c2 2019-05-12 jcs if (thread_err)
3705 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3706 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
3707 1e37a5c2 2019-05-12 jcs s->blamed_commit =
3708 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
3709 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3710 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3711 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
3712 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
3717 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3718 1e37a5c2 2019-05-12 jcs case '\r': {
3719 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3720 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
3721 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
3722 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3723 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3724 1e37a5c2 2019-05-12 jcs if (id == NULL)
3726 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
3729 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
3730 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
3731 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3732 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3733 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3734 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
3735 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3736 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
3739 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
3740 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
3741 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3743 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3746 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3747 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3750 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
3752 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3755 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
3756 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
3758 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3763 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3765 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3766 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
3767 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
3770 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3771 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
3772 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3773 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3776 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
3777 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
3778 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
3779 1e37a5c2 2019-05-12 jcs view->nlines - 2;
3781 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
3782 1e37a5c2 2019-05-12 jcs s->blame.nlines -
3783 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
3785 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3786 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
3787 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3788 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3794 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
3797 a70480e0 2018-06-23 stsp static const struct got_error *
3798 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
3800 a70480e0 2018-06-23 stsp const struct got_error *error;
3801 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
3802 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3803 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
3804 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3805 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3806 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
3808 e1cd8fed 2018-08-01 stsp struct tog_view *view;
3810 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3812 a70480e0 2018-06-23 stsp #ifndef PROFILE
3813 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3814 8e94dd5b 2019-01-04 stsp NULL) == -1)
3815 a70480e0 2018-06-23 stsp err(1, "pledge");
3818 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3819 a70480e0 2018-06-23 stsp switch (ch) {
3821 a70480e0 2018-06-23 stsp commit_id_str = optarg;
3824 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3825 69069811 2018-08-02 stsp if (repo_path == NULL)
3826 69069811 2018-08-02 stsp err(1, "-r option");
3829 17020d27 2019-03-07 stsp usage_blame();
3830 a70480e0 2018-06-23 stsp /* NOTREACHED */
3834 a70480e0 2018-06-23 stsp argc -= optind;
3835 a70480e0 2018-06-23 stsp argv += optind;
3837 69069811 2018-08-02 stsp if (argc == 1)
3838 69069811 2018-08-02 stsp path = argv[0];
3840 a70480e0 2018-06-23 stsp usage_blame();
3842 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
3843 69069811 2018-08-02 stsp if (cwd == NULL) {
3844 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3845 69069811 2018-08-02 stsp goto done;
3847 69069811 2018-08-02 stsp if (repo_path == NULL) {
3848 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3849 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3850 69069811 2018-08-02 stsp goto done;
3852 eb41ed75 2019-02-05 stsp error = NULL;
3853 eb41ed75 2019-02-05 stsp if (worktree) {
3854 eb41ed75 2019-02-05 stsp repo_path =
3855 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3856 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
3857 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3858 eb41ed75 2019-02-05 stsp if (error)
3859 eb41ed75 2019-02-05 stsp goto done;
3861 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
3862 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
3863 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3864 eb41ed75 2019-02-05 stsp goto done;
3869 a915003a 2019-02-05 stsp init_curses();
3871 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3872 c02c541e 2019-03-29 stsp if (error != NULL)
3873 8e94dd5b 2019-01-04 stsp goto done;
3875 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3876 c02c541e 2019-03-29 stsp if (error)
3877 92205607 2019-01-04 stsp goto done;
3879 eb41ed75 2019-02-05 stsp if (worktree) {
3880 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3881 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3882 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3883 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3884 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3885 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3886 eb41ed75 2019-02-05 stsp path) == -1) {
3887 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3888 eb41ed75 2019-02-05 stsp goto done;
3890 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3893 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3895 eb41ed75 2019-02-05 stsp if (error)
3896 69069811 2018-08-02 stsp goto done;
3898 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
3899 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
3900 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3901 a70480e0 2018-06-23 stsp if (error != NULL)
3902 66b4983c 2018-06-23 stsp goto done;
3903 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3904 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
3906 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&commit_id, commit_id_str, repo);
3907 f2b6a97d 2019-07-15 stsp if (error) {
3908 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
3909 f2b6a97d 2019-07-15 stsp goto done;
3910 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&commit_id,
3911 f2b6a97d 2019-07-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3914 a19e88aa 2018-06-23 stsp if (error != NULL)
3915 8b473291 2019-02-21 stsp goto done;
3917 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3918 8b473291 2019-02-21 stsp if (error)
3919 a19e88aa 2018-06-23 stsp goto done;
3921 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3922 e1cd8fed 2018-08-01 stsp if (view == NULL) {
3923 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3924 e1cd8fed 2018-08-01 stsp goto done;
3926 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3927 7cbe629d 2018-08-04 stsp if (error)
3928 7cbe629d 2018-08-04 stsp goto done;
3929 12314ad4 2019-08-31 stsp if (worktree) {
3930 12314ad4 2019-08-31 stsp /* Release work tree lock. */
3931 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
3932 12314ad4 2019-08-31 stsp worktree = NULL;
3934 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3936 69069811 2018-08-02 stsp free(repo_path);
3937 69069811 2018-08-02 stsp free(cwd);
3938 a70480e0 2018-06-23 stsp free(commit_id);
3939 eb41ed75 2019-02-05 stsp if (worktree)
3940 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
3942 a70480e0 2018-06-23 stsp got_repo_close(repo);
3943 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3944 a70480e0 2018-06-23 stsp return error;
3947 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3948 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
3949 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
3950 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
3951 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
3952 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
3953 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
3955 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3956 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
3957 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
3958 ffd1d5e5 2018-06-23 stsp int width, n;
3960 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
3962 f7d12f7e 2018-08-01 stsp werase(view->window);
3964 ffd1d5e5 2018-06-23 stsp if (limit == 0)
3965 ffd1d5e5 2018-06-23 stsp return NULL;
3967 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, label, view->ncols, 0);
3969 ffd1d5e5 2018-06-23 stsp return err;
3970 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3971 a3404814 2018-09-02 stsp wstandout(view->window);
3972 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3973 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3974 a3404814 2018-09-02 stsp wstandend(view->window);
3975 2550e4c3 2018-07-13 stsp free(wline);
3976 2550e4c3 2018-07-13 stsp wline = NULL;
3977 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3978 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3979 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3980 ffd1d5e5 2018-06-23 stsp return NULL;
3981 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
3983 ce52c690 2018-06-23 stsp return err;
3984 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3985 2550e4c3 2018-07-13 stsp free(wline);
3986 2550e4c3 2018-07-13 stsp wline = NULL;
3987 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3988 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3989 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3990 ffd1d5e5 2018-06-23 stsp return NULL;
3991 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3992 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
3993 a1eca9bb 2018-06-23 stsp return NULL;
3995 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3996 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
3997 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
3998 0cf4efb1 2018-09-29 stsp if (view->focussed)
3999 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4000 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
4002 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4003 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
4004 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4005 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4006 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4007 ffd1d5e5 2018-06-23 stsp return NULL;
4011 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
4012 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
4015 ffd1d5e5 2018-06-23 stsp while (te) {
4016 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
4017 848d6979 2019-08-12 stsp const char *modestr = "";
4019 1d13200f 2018-07-12 stsp if (show_ids) {
4020 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
4022 638f9024 2019-05-13 stsp return got_error_from_errno(
4023 230a42bd 2019-05-11 jcs "got_object_id_str");
4025 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4026 63c5ca5d 2019-08-24 stsp modestr = "$";
4027 63c5ca5d 2019-08-24 stsp else if (S_ISLNK(te->mode))
4028 848d6979 2019-08-12 stsp modestr = "@";
4029 848d6979 2019-08-12 stsp else if (S_ISDIR(te->mode))
4030 848d6979 2019-08-12 stsp modestr = "/";
4031 848d6979 2019-08-12 stsp else if (te->mode & S_IXUSR)
4032 848d6979 2019-08-12 stsp modestr = "*";
4033 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4034 848d6979 2019-08-12 stsp te->name, modestr) == -1) {
4035 1d13200f 2018-07-12 stsp free(id_str);
4036 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4038 1d13200f 2018-07-12 stsp free(id_str);
4039 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4040 ffd1d5e5 2018-06-23 stsp if (err) {
4041 ffd1d5e5 2018-06-23 stsp free(line);
4044 ffd1d5e5 2018-06-23 stsp if (n == selected) {
4045 0cf4efb1 2018-09-29 stsp if (view->focussed)
4046 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4047 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
4049 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4050 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4051 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4052 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
4053 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4054 ffd1d5e5 2018-06-23 stsp free(line);
4055 2550e4c3 2018-07-13 stsp free(wline);
4056 2550e4c3 2018-07-13 stsp wline = NULL;
4058 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4059 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
4060 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4062 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
4065 ffd1d5e5 2018-06-23 stsp return err;
4068 ffd1d5e5 2018-06-23 stsp static void
4069 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
4070 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
4071 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
4073 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
4076 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
4079 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
4080 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
4081 ffd1d5e5 2018-06-23 stsp if (!isroot)
4082 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4086 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
4087 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
4088 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
4089 ffd1d5e5 2018-06-23 stsp prev = te;
4090 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
4092 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
4093 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
4095 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4096 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4099 768394f3 2019-01-24 stsp static int
4100 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4101 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
4102 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
4104 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4105 ffd1d5e5 2018-06-23 stsp int n = 0;
4107 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
4108 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4110 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
4111 768394f3 2019-01-24 stsp last = last_displayed_entry;
4112 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4113 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
4114 768394f3 2019-01-24 stsp if (last) {
4115 768394f3 2019-01-24 stsp *first_displayed_entry = next;
4116 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
4122 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4123 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4124 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4126 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4127 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4128 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4130 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4131 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
4133 ce52c690 2018-06-23 stsp len += strlen(te->name);
4135 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4136 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4137 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4139 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4140 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4141 d9765a41 2018-06-23 stsp while (pt) {
4142 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4143 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4144 cb2ebc8a 2018-06-23 stsp goto done;
4146 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
4147 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4148 cb2ebc8a 2018-06-23 stsp goto done;
4150 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4153 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
4154 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4155 ce52c690 2018-06-23 stsp goto done;
4159 ce52c690 2018-06-23 stsp if (err) {
4160 ce52c690 2018-06-23 stsp free(*path);
4161 ce52c690 2018-06-23 stsp *path = NULL;
4163 ce52c690 2018-06-23 stsp return err;
4166 ce52c690 2018-06-23 stsp static const struct got_error *
4167 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
4168 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4169 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4170 8b473291 2019-02-21 stsp struct got_repository *repo)
4172 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
4173 ce52c690 2018-06-23 stsp char *path;
4174 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
4176 a0de39f3 2019-08-09 stsp *new_view = NULL;
4178 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
4180 ce52c690 2018-06-23 stsp return err;
4182 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4183 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
4184 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
4185 83ce39e3 2019-08-12 stsp goto done;
4188 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
4189 e5a0f69f 2018-08-18 stsp if (err) {
4190 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
4191 fc06ba56 2019-08-22 stsp err = NULL;
4192 e5a0f69f 2018-08-18 stsp view_close(blame_view);
4194 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
4196 83ce39e3 2019-08-12 stsp free(path);
4197 69efd4c4 2018-07-18 stsp return err;
4200 69efd4c4 2018-07-18 stsp static const struct got_error *
4201 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
4202 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4203 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4204 8b473291 2019-02-21 stsp struct got_repository *repo)
4206 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
4207 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
4208 69efd4c4 2018-07-18 stsp char *path;
4210 a0de39f3 2019-08-09 stsp *new_view = NULL;
4212 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4213 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
4214 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
4216 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
4218 69efd4c4 2018-07-18 stsp return err;
4220 d01904d4 2019-06-25 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4222 e5a0f69f 2018-08-18 stsp view_close(log_view);
4224 e5a0f69f 2018-08-18 stsp *new_view = log_view;
4225 cb2ebc8a 2018-06-23 stsp free(path);
4226 cb2ebc8a 2018-06-23 stsp return err;
4229 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4230 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
4231 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4232 8b473291 2019-02-21 stsp struct got_repository *repo)
4234 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4235 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
4236 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4238 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
4240 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
4241 ffd1d5e5 2018-06-23 stsp if (err != NULL)
4242 ffd1d5e5 2018-06-23 stsp goto done;
4244 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4245 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4246 ffd1d5e5 2018-06-23 stsp goto done;
4249 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
4250 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
4251 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4252 8d0fe45a 2019-08-12 stsp s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4253 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
4254 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
4255 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4256 6484ec90 2018-09-29 stsp goto done;
4258 8b473291 2019-02-21 stsp s->refs = refs;
4259 fb2756b9 2018-08-04 stsp s->repo = repo;
4261 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
4262 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
4263 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
4264 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
4265 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
4267 ad80ab7b 2018-08-04 stsp free(commit_id_str);
4268 6484ec90 2018-09-29 stsp if (err) {
4269 fb2756b9 2018-08-04 stsp free(s->tree_label);
4270 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4272 ad80ab7b 2018-08-04 stsp return err;
4275 e5a0f69f 2018-08-18 stsp static const struct got_error *
4276 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
4278 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4280 fb2756b9 2018-08-04 stsp free(s->tree_label);
4281 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4282 6484ec90 2018-09-29 stsp free(s->commit_id);
4283 6484ec90 2018-09-29 stsp s->commit_id = NULL;
4284 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
4285 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
4286 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
4287 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
4288 ad80ab7b 2018-08-04 stsp free(parent);
4291 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
4292 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
4293 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
4295 7c32bd05 2019-06-22 stsp return NULL;
4298 7c32bd05 2019-06-22 stsp static const struct got_error *
4299 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
4301 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4303 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
4304 7c32bd05 2019-06-22 stsp return NULL;
4307 7c32bd05 2019-06-22 stsp static int
4308 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4310 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
4312 7c32bd05 2019-06-22 stsp return regexec(regex, te->name, 1, ®match, 0) == 0;
4315 7c32bd05 2019-06-22 stsp static const struct got_error *
4316 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
4318 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4319 a0de39f3 2019-08-09 stsp struct got_tree_entry *entry = NULL, *te;
4321 7c32bd05 2019-06-22 stsp if (!view->searching) {
4322 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4323 7c32bd05 2019-06-22 stsp return NULL;
4326 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4327 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
4328 7c32bd05 2019-06-22 stsp if (s->selected_entry)
4329 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4331 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4334 7c32bd05 2019-06-22 stsp if (s->selected_entry == NULL) {
4335 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4336 7c32bd05 2019-06-22 stsp entry = te;
4338 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4339 7c32bd05 2019-06-22 stsp entry = te;
4340 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) ==
4341 7c32bd05 2019-06-22 stsp s->selected_entry)
4347 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4348 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4350 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4351 7c32bd05 2019-06-22 stsp entry = te;
4355 7c32bd05 2019-06-22 stsp while (1) {
4356 7c32bd05 2019-06-22 stsp if (entry == NULL) {
4357 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
4358 ac66afb8 2019-06-24 stsp view->search_next_done = 1;
4359 ac66afb8 2019-06-24 stsp return NULL;
4361 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4362 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4364 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4365 7c32bd05 2019-06-22 stsp entry = te;
4369 7c32bd05 2019-06-22 stsp if (match_tree_entry(entry, &view->regex)) {
4370 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4371 7c32bd05 2019-06-22 stsp s->matched_entry = entry;
4375 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4376 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(entry, entry);
4378 7c32bd05 2019-06-22 stsp if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4379 7c32bd05 2019-06-22 stsp entry = NULL;
4381 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4382 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) == entry) {
4383 7c32bd05 2019-06-22 stsp entry = te;
4391 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4392 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
4393 7c32bd05 2019-06-22 stsp s->selected = 0;
4396 e5a0f69f 2018-08-18 stsp return NULL;
4399 ad80ab7b 2018-08-04 stsp static const struct got_error *
4400 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
4402 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
4403 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4404 e5a0f69f 2018-08-18 stsp char *parent_path;
4406 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
4408 e5a0f69f 2018-08-18 stsp return err;
4410 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
4411 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
4412 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4413 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
4414 e5a0f69f 2018-08-18 stsp free(parent_path);
4416 669b5ffa 2018-10-07 stsp view_vborder(view);
4417 e5a0f69f 2018-08-18 stsp return err;
4420 e5a0f69f 2018-08-18 stsp static const struct got_error *
4421 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4422 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4424 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4425 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
4426 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
4427 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
4429 e5a0f69f 2018-08-18 stsp switch (ch) {
4431 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
4434 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
4436 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4437 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4438 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
4439 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
4440 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
4441 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4442 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4444 1e37a5c2 2019-05-12 jcs return err;
4445 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
4447 1e37a5c2 2019-05-12 jcs view_close(log_view);
4450 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
4451 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4453 1e37a5c2 2019-05-12 jcs *new_view = log_view;
4456 1e37a5c2 2019-05-12 jcs case KEY_UP:
4457 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
4458 1e37a5c2 2019-05-12 jcs s->selected--;
4459 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
4462 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
4464 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
4465 1e37a5c2 2019-05-12 jcs s->entries, s->tree == s->root);
4467 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4468 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
4469 1e37a5c2 2019-05-12 jcs MAX(0, view->nlines - 4 - s->selected), s->entries,
4470 1e37a5c2 2019-05-12 jcs s->tree == s->root);
4471 1e37a5c2 2019-05-12 jcs s->selected = 0;
4472 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_FIRST(&s->entries->head) ==
4473 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
4474 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
4477 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4478 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
4479 1e37a5c2 2019-05-12 jcs s->selected++;
4482 00ba99a7 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4483 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
4485 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
4486 1e37a5c2 2019-05-12 jcs s->last_displayed_entry, s->entries);
4488 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4489 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4491 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
4492 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
4493 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4496 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
4497 1e37a5c2 2019-05-12 jcs view->nlines, s->last_displayed_entry, s->entries);
4498 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
4499 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
4500 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
4501 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
4503 1e37a5c2 2019-05-12 jcs ndisplayed++;
4504 1e37a5c2 2019-05-12 jcs te = SIMPLEQ_NEXT(te, entry);
4505 1e37a5c2 2019-05-12 jcs } while (te);
4506 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
4509 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4511 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4512 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4513 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
4514 1e37a5c2 2019-05-12 jcs /* user selected '..' */
4515 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
4517 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
4518 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
4520 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
4521 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
4522 1e37a5c2 2019-05-12 jcs s->entries =
4523 1e37a5c2 2019-05-12 jcs got_object_tree_get_entries(s->tree);
4524 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
4525 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
4526 1e37a5c2 2019-05-12 jcs s->selected_entry =
4527 1e37a5c2 2019-05-12 jcs parent->selected_entry;
4528 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
4529 1e37a5c2 2019-05-12 jcs free(parent);
4530 1e37a5c2 2019-05-12 jcs } else if (S_ISDIR(s->selected_entry->mode)) {
4531 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
4532 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&subtree,
4533 1e37a5c2 2019-05-12 jcs s->repo, s->selected_entry->id);
4536 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
4537 941e9f74 2019-05-21 stsp if (err) {
4538 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
4541 1e37a5c2 2019-05-12 jcs } else if (S_ISREG(s->selected_entry->mode)) {
4542 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
4543 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
4544 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
4546 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
4547 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
4548 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
4551 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
4552 669b5ffa 2018-10-07 stsp err = view_close_child(view);
4554 669b5ffa 2018-10-07 stsp return err;
4555 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
4556 669b5ffa 2018-10-07 stsp if (err) {
4557 1e37a5c2 2019-05-12 jcs view_close(blame_view);
4560 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
4561 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
4563 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
4566 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4567 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
4568 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4574 ffd1d5e5 2018-06-23 stsp return err;
4577 ffd1d5e5 2018-06-23 stsp __dead static void
4578 ffd1d5e5 2018-06-23 stsp usage_tree(void)
4581 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4582 ffd1d5e5 2018-06-23 stsp getprogname());
4586 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4587 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
4589 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
4590 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
4591 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4592 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
4593 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4594 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
4595 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
4596 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
4598 5221c383 2018-08-01 stsp struct tog_view *view;
4600 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4602 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
4603 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4604 d188b9a6 2019-01-04 stsp NULL) == -1)
4605 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
4608 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
4609 ffd1d5e5 2018-06-23 stsp switch (ch) {
4611 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
4614 17020d27 2019-03-07 stsp usage_tree();
4615 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
4619 ffd1d5e5 2018-06-23 stsp argc -= optind;
4620 ffd1d5e5 2018-06-23 stsp argv += optind;
4622 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
4623 52185f70 2019-02-05 stsp struct got_worktree *worktree;
4624 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
4625 52185f70 2019-02-05 stsp if (cwd == NULL)
4626 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
4627 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4628 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4629 52185f70 2019-02-05 stsp goto done;
4630 52185f70 2019-02-05 stsp if (worktree) {
4631 52185f70 2019-02-05 stsp free(cwd);
4632 52185f70 2019-02-05 stsp repo_path =
4633 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4634 52185f70 2019-02-05 stsp got_worktree_close(worktree);
4636 52185f70 2019-02-05 stsp repo_path = cwd;
4637 52185f70 2019-02-05 stsp if (repo_path == NULL) {
4638 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4639 52185f70 2019-02-05 stsp goto done;
4641 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
4642 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
4643 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
4644 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
4646 ffd1d5e5 2018-06-23 stsp usage_log();
4648 a915003a 2019-02-05 stsp init_curses();
4650 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4651 c02c541e 2019-03-29 stsp if (error != NULL)
4652 52185f70 2019-02-05 stsp goto done;
4654 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4655 c02c541e 2019-03-29 stsp if (error)
4656 52185f70 2019-02-05 stsp goto done;
4658 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
4659 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4661 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4662 f2b6a97d 2019-07-15 stsp if (error) {
4663 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
4664 f2b6a97d 2019-07-15 stsp goto done;
4665 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&commit_id,
4666 f2b6a97d 2019-07-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4669 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4670 ffd1d5e5 2018-06-23 stsp goto done;
4672 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4673 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4674 ffd1d5e5 2018-06-23 stsp goto done;
4676 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
4677 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
4678 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4679 8b473291 2019-02-21 stsp goto done;
4681 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4682 8b473291 2019-02-21 stsp if (error)
4683 ffd1d5e5 2018-06-23 stsp goto done;
4685 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4686 5221c383 2018-08-01 stsp if (view == NULL) {
4687 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4688 5221c383 2018-08-01 stsp goto done;
4690 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
4691 ad80ab7b 2018-08-04 stsp if (error)
4692 ad80ab7b 2018-08-04 stsp goto done;
4693 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4695 52185f70 2019-02-05 stsp free(repo_path);
4696 ffd1d5e5 2018-06-23 stsp free(commit_id);
4697 ffd1d5e5 2018-06-23 stsp if (commit)
4698 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
4700 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
4702 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
4703 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4704 ffd1d5e5 2018-06-23 stsp return error;
4707 ce5b7c56 2019-07-09 stsp static void
4708 ce5b7c56 2019-07-09 stsp list_commands(void)
4712 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
4713 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
4714 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
4715 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->name);
4717 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
4720 4ed7e80c 2018-05-20 stsp __dead static void
4721 ce5b7c56 2019-07-09 stsp usage(int hflag)
4723 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4724 53ccebc2 2019-07-30 stsp getprogname());
4725 ce5b7c56 2019-07-09 stsp if (hflag)
4726 ce5b7c56 2019-07-09 stsp list_commands();
4730 c2301be8 2018-04-30 stsp static char **
4731 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
4733 c2301be8 2018-04-30 stsp char **argv;
4734 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
4736 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
4737 c2301be8 2018-04-30 stsp if (argv == NULL)
4738 c2301be8 2018-04-30 stsp err(1, "calloc");
4739 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
4740 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
4741 e10c916e 2019-09-15 hiltjo err(1, "strdup");
4742 c2301be8 2018-04-30 stsp if (arg1) {
4743 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
4744 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
4745 e10c916e 2019-09-15 hiltjo err(1, "strdup");
4748 c2301be8 2018-04-30 stsp return argv;
4752 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
4754 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
4755 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
4756 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
4757 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
4759 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
4761 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
4762 9f7d7167 2018-04-29 stsp switch (ch) {
4764 9f7d7167 2018-04-29 stsp hflag = 1;
4767 53ccebc2 2019-07-30 stsp Vflag = 1;
4770 ce5b7c56 2019-07-09 stsp usage(hflag);
4771 9f7d7167 2018-04-29 stsp /* NOTREACHED */
4775 9f7d7167 2018-04-29 stsp argc -= optind;
4776 9f7d7167 2018-04-29 stsp argv += optind;
4777 9f7d7167 2018-04-29 stsp optind = 0;
4778 c2301be8 2018-04-30 stsp optreset = 1;
4780 53ccebc2 2019-07-30 stsp if (Vflag) {
4781 53ccebc2 2019-07-30 stsp got_version_print_str();
4785 c2301be8 2018-04-30 stsp if (argc == 0) {
4786 f29d3e89 2018-06-23 stsp if (hflag)
4787 ce5b7c56 2019-07-09 stsp usage(hflag);
4788 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
4789 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
4790 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
4795 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
4796 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
4797 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
4798 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
4799 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
4804 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
4805 d70c3147 2019-07-09 stsp fprintf(stderr, "%s: unknown command '%s'\n",
4806 3642c4c6 2019-07-09 stsp getprogname(), argv[0]);
4807 ce5b7c56 2019-07-09 stsp list_commands();
4812 3642c4c6 2019-07-09 stsp if (hflag)
4813 3642c4c6 2019-07-09 stsp cmd->cmd_usage();
4815 3642c4c6 2019-07-09 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4818 c2301be8 2018-04-30 stsp free(cmd_argv);
4819 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
4820 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);