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 6570a66d 2019-11-08 stsp if (avail >= 120) {
1092 6570a66d 2019-11-08 stsp char *id_str;
1093 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1095 6570a66d 2019-11-08 stsp goto done;
1096 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1097 6570a66d 2019-11-08 stsp free(id_str);
1099 6570a66d 2019-11-08 stsp if (col > avail)
1100 6570a66d 2019-11-08 stsp goto done;
1103 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1104 5813d178 2019-03-09 stsp if (author == NULL) {
1105 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1106 80ddbec8 2018-04-29 stsp goto done;
1108 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1110 bb737323 2018-05-20 stsp goto done;
1111 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1112 bb737323 2018-05-20 stsp col += author_width;
1113 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1114 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1116 bb737323 2018-05-20 stsp author_width++;
1118 9c2eaf34 2018-05-20 stsp if (col > avail)
1119 9c2eaf34 2018-05-20 stsp goto done;
1121 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1123 6d9fbc00 2018-04-29 stsp goto done;
1124 bb737323 2018-05-20 stsp logmsg = logmsg0;
1125 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1127 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1128 bb737323 2018-05-20 stsp if (newline)
1129 bb737323 2018-05-20 stsp *newline = '\0';
1130 bb737323 2018-05-20 stsp limit = avail - col;
1131 27a741e5 2019-09-11 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, 0);
1133 bb737323 2018-05-20 stsp goto done;
1134 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1135 bb737323 2018-05-20 stsp col += logmsg_width;
1136 27a741e5 2019-09-11 stsp while (col < avail) {
1137 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1141 80ddbec8 2018-04-29 stsp free(logmsg0);
1142 bb737323 2018-05-20 stsp free(wlogmsg);
1143 5813d178 2019-03-09 stsp free(author);
1144 bb737323 2018-05-20 stsp free(wauthor);
1145 80ddbec8 2018-04-29 stsp free(line);
1146 80ddbec8 2018-04-29 stsp return err;
1149 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1150 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1151 899d86c2 2018-05-10 stsp struct got_object_id *id)
1153 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1155 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1156 80ddbec8 2018-04-29 stsp if (entry == NULL)
1157 899d86c2 2018-05-10 stsp return NULL;
1159 899d86c2 2018-05-10 stsp entry->id = id;
1160 99db9666 2018-05-07 stsp entry->commit = commit;
1161 899d86c2 2018-05-10 stsp return entry;
1164 99db9666 2018-05-07 stsp static void
1165 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1167 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1169 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1170 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1171 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1172 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1173 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1174 99db9666 2018-05-07 stsp free(entry);
1177 99db9666 2018-05-07 stsp static void
1178 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1180 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1181 99db9666 2018-05-07 stsp pop_commit(commits);
1184 c4972b91 2018-05-07 stsp static const struct got_error *
1185 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1186 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1188 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1189 13add988 2019-10-15 stsp regmatch_t regmatch;
1190 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1192 13add988 2019-10-15 stsp *have_match = 0;
1194 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1196 13add988 2019-10-15 stsp return err;
1198 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1200 13add988 2019-10-15 stsp goto done;
1202 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1203 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1204 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1205 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1206 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1207 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1208 13add988 2019-10-15 stsp *have_match = 1;
1210 13add988 2019-10-15 stsp free(id_str);
1211 13add988 2019-10-15 stsp free(logmsg);
1212 13add988 2019-10-15 stsp return err;
1215 13add988 2019-10-15 stsp static const struct got_error *
1216 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1217 13add988 2019-10-15 stsp int minqueue, struct got_repository *repo, const char *path,
1218 13add988 2019-10-15 stsp int *searching, int *search_next_done, regex_t *regex)
1220 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1221 13add988 2019-10-15 stsp int nqueued = 0, have_match = 0;
1224 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1225 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1226 1a76625f 2018-10-22 stsp * while updating the display.
1228 13add988 2019-10-15 stsp while (nqueued < minqueue ||
1229 13add988 2019-10-15 stsp (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1230 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1231 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1232 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1233 1a76625f 2018-10-22 stsp int errcode;
1235 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1236 9ba79e04 2018-06-11 stsp if (err) {
1237 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1239 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
1240 6fb7cd11 2019-08-22 stsp minqueue, repo, NULL, NULL);
1242 ecb28ae0 2018-07-16 stsp return err;
1246 ecb28ae0 2018-07-16 stsp if (id == NULL)
1249 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1252 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1253 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1254 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1258 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1259 1a76625f 2018-10-22 stsp if (errcode) {
1260 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1261 13add988 2019-10-15 stsp "pthread_mutex_lock");
1265 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1266 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1267 ecb28ae0 2018-07-16 stsp nqueued++;
1268 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1270 13add988 2019-10-15 stsp if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1271 13add988 2019-10-15 stsp err = match_commit(&have_match, id, commit, regex);
1272 13add988 2019-10-15 stsp if (err) {
1273 13add988 2019-10-15 stsp pthread_mutex_lock(&tog_mutex);
1278 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1279 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1280 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1281 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1283 13add988 2019-10-15 stsp if (have_match)
1287 9ba79e04 2018-06-11 stsp return err;
1290 99db9666 2018-05-07 stsp static const struct got_error *
1291 19e70ad6 2019-05-14 stsp get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1292 19e70ad6 2019-05-14 stsp struct got_repository *repo)
1294 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1295 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1297 9ba79e04 2018-06-11 stsp *head_id = NULL;
1299 19e70ad6 2019-05-14 stsp err = got_ref_open(&head_ref, repo, branch_name, 0);
1301 99db9666 2018-05-07 stsp return err;
1303 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1304 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1305 9ba79e04 2018-06-11 stsp if (err) {
1306 9ba79e04 2018-06-11 stsp *head_id = NULL;
1307 99db9666 2018-05-07 stsp return err;
1310 9ba79e04 2018-06-11 stsp return NULL;
1313 0553a4e3 2018-04-30 stsp static const struct got_error *
1314 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1315 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1316 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1317 8b473291 2019-02-21 stsp struct got_reflist_head *refs, const char *path, int commits_needed)
1319 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1320 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1321 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1322 60493ae3 2019-06-20 stsp int width;
1323 60493ae3 2019-06-20 stsp int ncommits, author_cols = 10;
1324 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1325 8b473291 2019-02-21 stsp char *refs_str = NULL;
1326 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1327 27a741e5 2019-09-11 stsp static const size_t date_display_cols = 9;
1329 e0d42f60 2018-07-22 stsp entry = first;
1330 e0d42f60 2018-07-22 stsp ncommits = 0;
1331 e0d42f60 2018-07-22 stsp while (entry) {
1332 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1333 e0d42f60 2018-07-22 stsp *selected = entry;
1336 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1337 e0d42f60 2018-07-22 stsp ncommits++;
1340 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1341 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1343 ecb28ae0 2018-07-16 stsp return err;
1344 8b473291 2019-02-21 stsp if (refs) {
1345 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, (*selected)->id,
1348 8b473291 2019-02-21 stsp goto done;
1352 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1353 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1355 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1356 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1357 60493ae3 2019-06-20 stsp commits_needed > 0 ?
1358 60493ae3 2019-06-20 stsp (view->searching && view->search_next_done == 0
1359 60493ae3 2019-06-20 stsp ? "searching..." : "loading... ") :
1360 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1361 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1362 8b473291 2019-02-21 stsp goto done;
1365 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1366 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1367 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1368 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1369 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1370 1a76625f 2018-10-22 stsp header = NULL;
1371 1a76625f 2018-10-22 stsp goto done;
1373 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1374 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1375 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1376 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1377 1a76625f 2018-10-22 stsp header = NULL;
1378 1a76625f 2018-10-22 stsp goto done;
1380 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1382 1a76625f 2018-10-22 stsp goto done;
1384 2814baeb 2018-08-01 stsp werase(view->window);
1386 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1387 a3404814 2018-09-02 stsp wstandout(view->window);
1388 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1389 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1390 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1393 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1394 a3404814 2018-09-02 stsp wstandend(view->window);
1395 ecb28ae0 2018-07-16 stsp free(wline);
1396 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1397 1a76625f 2018-10-22 stsp goto done;
1399 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1400 899d86c2 2018-05-10 stsp entry = first;
1401 5813d178 2019-03-09 stsp ncommits = 0;
1402 5813d178 2019-03-09 stsp while (entry) {
1403 5813d178 2019-03-09 stsp char *author;
1404 5813d178 2019-03-09 stsp wchar_t *wauthor;
1405 5813d178 2019-03-09 stsp int width;
1406 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1408 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1409 5813d178 2019-03-09 stsp if (author == NULL) {
1410 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1411 5813d178 2019-03-09 stsp goto done;
1413 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1414 27a741e5 2019-09-11 stsp date_display_cols);
1415 5813d178 2019-03-09 stsp if (author_cols < width)
1416 5813d178 2019-03-09 stsp author_cols = width;
1417 5813d178 2019-03-09 stsp free(wauthor);
1418 5813d178 2019-03-09 stsp free(author);
1419 7ca04879 2019-10-19 stsp ncommits++;
1420 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1423 5813d178 2019-03-09 stsp entry = first;
1424 899d86c2 2018-05-10 stsp *last = first;
1425 867c6645 2018-07-10 stsp ncommits = 0;
1426 899d86c2 2018-05-10 stsp while (entry) {
1427 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1429 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1430 2814baeb 2018-08-01 stsp wstandout(view->window);
1431 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1432 27a741e5 2019-09-11 stsp date_display_cols, author_cols);
1433 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1434 2814baeb 2018-08-01 stsp wstandend(view->window);
1436 60493ae3 2019-06-20 stsp goto done;
1437 0553a4e3 2018-04-30 stsp ncommits++;
1438 899d86c2 2018-05-10 stsp *last = entry;
1439 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1442 1a57306a 2018-09-02 stsp view_vborder(view);
1444 1a76625f 2018-10-22 stsp free(id_str);
1445 8b473291 2019-02-21 stsp free(refs_str);
1446 1a76625f 2018-10-22 stsp free(ncommits_str);
1447 1a76625f 2018-10-22 stsp free(header);
1448 80ddbec8 2018-04-29 stsp return err;
1451 07b55e75 2018-05-10 stsp static void
1452 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1453 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1454 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1456 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1457 07b55e75 2018-05-10 stsp int nscrolled = 0;
1459 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1460 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1463 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1464 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1465 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1466 07b55e75 2018-05-10 stsp if (entry) {
1467 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1468 07b55e75 2018-05-10 stsp nscrolled++;
1473 aa075928 2018-05-10 stsp static const struct got_error *
1474 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1475 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1477 5e224a3e 2019-02-22 stsp int errcode;
1478 8a42fca8 2019-02-22 stsp int max_wait = 20;
1480 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1482 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1483 5e224a3e 2019-02-22 stsp if (*log_complete)
1486 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1487 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1488 7aafa0d1 2019-02-22 stsp if (errcode)
1489 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1490 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1491 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1492 7aafa0d1 2019-02-22 stsp if (errcode)
1493 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1494 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1495 7aafa0d1 2019-02-22 stsp pthread_yield();
1496 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1497 7aafa0d1 2019-02-22 stsp if (errcode)
1498 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1499 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1501 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1503 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1504 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1505 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1506 5e224a3e 2019-02-22 stsp * with few commits in history.
1508 7aafa0d1 2019-02-22 stsp return NULL;
1512 5e224a3e 2019-02-22 stsp return NULL;
1515 5e224a3e 2019-02-22 stsp static const struct got_error *
1516 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1517 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1518 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1519 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1520 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1522 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1523 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1524 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1526 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1527 5e224a3e 2019-02-22 stsp return NULL;
1529 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1530 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1532 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1533 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1535 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1536 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1537 5e224a3e 2019-02-22 stsp need_commits);
1539 5e224a3e 2019-02-22 stsp return err;
1543 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1544 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1547 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1549 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1550 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1552 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1553 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1555 dd0a52c1 2018-05-20 stsp return err;
1558 cd0acaa7 2018-05-20 stsp static const struct got_error *
1559 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1560 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1561 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1562 8b473291 2019-02-21 stsp struct got_repository *repo)
1564 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1565 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1566 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1568 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1569 15a94983 2018-12-23 stsp if (diff_view == NULL)
1570 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1572 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1573 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1574 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1575 e5a0f69f 2018-08-18 stsp if (err == NULL)
1576 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1577 cd0acaa7 2018-05-20 stsp return err;
1580 80ddbec8 2018-04-29 stsp static const struct got_error *
1581 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1582 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1584 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1586 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1587 941e9f74 2019-05-21 stsp if (parent == NULL)
1588 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1590 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1591 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1592 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1593 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1594 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1595 941e9f74 2019-05-21 stsp s->tree = subtree;
1596 941e9f74 2019-05-21 stsp s->entries = got_object_tree_get_entries(s->tree);
1597 941e9f74 2019-05-21 stsp s->selected = 0;
1598 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1599 941e9f74 2019-05-21 stsp return NULL;
1603 941e9f74 2019-05-21 stsp static const struct got_error *
1604 941e9f74 2019-05-21 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1605 941e9f74 2019-05-21 stsp struct commit_queue_entry *entry, const char *path,
1606 941e9f74 2019-05-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
1608 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1609 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1610 941e9f74 2019-05-21 stsp struct got_tree_entry *te;
1611 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s;
1612 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1613 b03c880f 2019-05-21 stsp char *slash, *subpath = NULL;
1614 941e9f74 2019-05-21 stsp const char *p;
1616 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1617 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1619 9343a5fb 2018-06-23 stsp return err;
1621 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1622 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1623 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1625 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1626 941e9f74 2019-05-21 stsp if (err) {
1627 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1628 941e9f74 2019-05-21 stsp return err;
1630 941e9f74 2019-05-21 stsp s = &tree_view->state.tree;
1632 941e9f74 2019-05-21 stsp *new_view = tree_view;
1634 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1636 8d0fe45a 2019-08-12 stsp while (p[0] == '/')
1638 941e9f74 2019-05-21 stsp while (*p) {
1639 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1641 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1642 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1643 941e9f74 2019-05-21 stsp if (slash == NULL)
1644 941e9f74 2019-05-21 stsp slash = strchr(p, '\0');
1645 941e9f74 2019-05-21 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1646 941e9f74 2019-05-21 stsp if (strncmp(p, te->name, slash - p) == 0) {
1647 941e9f74 2019-05-21 stsp s->selected_entry = te;
1650 b03c880f 2019-05-21 stsp s->selected++;
1652 941e9f74 2019-05-21 stsp if (s->selected_entry == NULL) {
1653 941e9f74 2019-05-21 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1656 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1657 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1659 67409a31 2019-05-24 stsp if (!S_ISDIR(s->selected_entry->mode)) {
1660 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1661 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1662 67409a31 2019-05-24 stsp s->selected = 0;
1666 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1667 941e9f74 2019-05-21 stsp if (slash)
1668 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1670 941e9f74 2019-05-21 stsp subpath = strdup(path);
1671 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1672 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1676 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1681 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1682 941e9f74 2019-05-21 stsp free(tree_id);
1686 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1687 941e9f74 2019-05-21 stsp if (err) {
1688 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1691 941e9f74 2019-05-21 stsp if (slash == NULL)
1693 941e9f74 2019-05-21 stsp free(subpath);
1694 941e9f74 2019-05-21 stsp subpath = NULL;
1695 941e9f74 2019-05-21 stsp p = slash;
1698 941e9f74 2019-05-21 stsp free(subpath);
1699 1a76625f 2018-10-22 stsp return err;
1702 1a76625f 2018-10-22 stsp static void *
1703 1a76625f 2018-10-22 stsp log_thread(void *arg)
1705 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1706 1a76625f 2018-10-22 stsp int errcode = 0;
1707 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1708 1a76625f 2018-10-22 stsp int done = 0;
1710 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1711 6fb7cd11 2019-08-22 stsp NULL, NULL);
1713 1a76625f 2018-10-22 stsp return (void *)err;
1715 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1716 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1717 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1718 13add988 2019-10-15 stsp a->regex);
1719 1a76625f 2018-10-22 stsp if (err) {
1720 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1721 1a76625f 2018-10-22 stsp return (void *)err;
1722 1a76625f 2018-10-22 stsp err = NULL;
1724 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1725 1a76625f 2018-10-22 stsp a->commits_needed--;
1727 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1728 3abe8080 2019-04-10 stsp if (errcode) {
1729 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1730 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1732 3abe8080 2019-04-10 stsp } else if (*a->quit)
1734 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1735 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1736 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1737 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1741 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1742 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1743 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1744 1a76625f 2018-10-22 stsp &tog_mutex);
1745 1a76625f 2018-10-22 stsp if (errcode)
1746 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1747 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1750 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1751 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1752 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1753 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1755 3abe8080 2019-04-10 stsp a->log_complete = 1;
1756 1a76625f 2018-10-22 stsp return (void *)err;
1759 1a76625f 2018-10-22 stsp static const struct got_error *
1760 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1762 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1763 1a76625f 2018-10-22 stsp int errcode;
1765 1a76625f 2018-10-22 stsp if (s->thread) {
1766 1a76625f 2018-10-22 stsp s->quit = 1;
1767 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1768 1a76625f 2018-10-22 stsp if (errcode)
1769 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1770 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1771 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1772 1a76625f 2018-10-22 stsp if (errcode)
1773 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1774 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1775 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1776 1a76625f 2018-10-22 stsp if (errcode)
1777 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1778 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1779 1a76625f 2018-10-22 stsp if (errcode)
1780 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1781 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1782 1a76625f 2018-10-22 stsp s->thread = NULL;
1785 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1786 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1787 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1789 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1790 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1791 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1794 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1795 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1796 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1799 9343a5fb 2018-06-23 stsp return err;
1802 9343a5fb 2018-06-23 stsp static const struct got_error *
1803 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1805 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1806 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1808 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1809 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1810 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1811 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1812 1a76625f 2018-10-22 stsp free(s->start_id);
1813 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1814 1a76625f 2018-10-22 stsp return err;
1817 1a76625f 2018-10-22 stsp static const struct got_error *
1818 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
1820 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1822 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
1823 96e2b566 2019-07-08 stsp s->search_entry = NULL;
1824 60493ae3 2019-06-20 stsp return NULL;
1827 60493ae3 2019-06-20 stsp static const struct got_error *
1828 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
1830 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
1831 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1832 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
1834 60493ae3 2019-06-20 stsp if (!view->searching) {
1835 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1836 60493ae3 2019-06-20 stsp return NULL;
1839 96e2b566 2019-07-08 stsp if (s->search_entry) {
1840 13add988 2019-10-15 stsp int errcode, ch;
1841 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1842 13add988 2019-10-15 stsp if (errcode)
1843 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
1844 13add988 2019-10-15 stsp "pthread_mutex_unlock");
1845 13add988 2019-10-15 stsp ch = wgetch(view->window);
1846 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
1847 13add988 2019-10-15 stsp if (errcode)
1848 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
1849 13add988 2019-10-15 stsp "pthread_mutex_lock");
1850 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
1851 678cbce5 2019-07-28 stsp view->search_next_done = 1;
1852 678cbce5 2019-07-28 stsp return NULL;
1854 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
1855 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
1857 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
1858 96e2b566 2019-07-08 stsp commit_queue_head, entry);
1859 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
1860 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1861 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
1863 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
1864 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
1866 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1867 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
1869 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1872 60493ae3 2019-06-20 stsp while (1) {
1873 13add988 2019-10-15 stsp int have_match = 0;
1875 60493ae3 2019-06-20 stsp if (entry == NULL) {
1876 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
1877 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
1878 f801134a 2019-06-25 stsp view->search_next_done = 1;
1879 f801134a 2019-06-25 stsp return NULL;
1882 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
1883 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
1884 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
1886 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
1887 57b33b64 2019-07-08 stsp return trigger_log_thread(1,
1888 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
1889 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
1890 f801134a 2019-06-25 stsp &s->thread_args.need_commits);
1893 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
1894 13add988 2019-10-15 stsp &view->regex);
1897 13add988 2019-10-15 stsp if (have_match) {
1898 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1899 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
1903 96e2b566 2019-07-08 stsp s->search_entry = entry;
1904 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1905 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
1907 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1910 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
1911 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
1912 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
1913 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1915 60493ae3 2019-06-20 stsp return err;
1918 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
1919 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1921 60493ae3 2019-06-20 stsp return err;
1926 96e2b566 2019-07-08 stsp s->search_entry = NULL;
1928 60493ae3 2019-06-20 stsp return NULL;
1931 60493ae3 2019-06-20 stsp static const struct got_error *
1932 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1933 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
1934 d01904d4 2019-06-25 stsp const char *head_ref_name, const char *path, int check_disk)
1936 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1937 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1938 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1939 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1940 1a76625f 2018-10-22 stsp int errcode;
1942 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1943 ecb28ae0 2018-07-16 stsp if (err != NULL)
1944 ecb28ae0 2018-07-16 stsp goto done;
1946 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1947 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1948 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1950 8b473291 2019-02-21 stsp s->refs = refs;
1951 fb2756b9 2018-08-04 stsp s->repo = repo;
1952 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
1953 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1954 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1955 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1956 5036bf37 2018-09-24 stsp goto done;
1959 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1960 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1961 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1962 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
1963 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
1965 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
1967 1a76625f 2018-10-22 stsp goto done;
1968 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1969 1a76625f 2018-10-22 stsp 0, thread_repo);
1971 1a76625f 2018-10-22 stsp goto done;
1973 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1974 1a76625f 2018-10-22 stsp if (errcode) {
1975 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
1976 1a76625f 2018-10-22 stsp goto done;
1979 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1980 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1981 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1982 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1983 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1984 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1985 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1986 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1987 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1988 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1989 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
1990 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
1991 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
1994 1a76625f 2018-10-22 stsp close_log_view(view);
1995 ba4f502b 2018-08-04 stsp return err;
1998 e5a0f69f 2018-08-18 stsp static const struct got_error *
1999 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2001 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2003 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2004 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2005 2b380cc8 2018-10-24 stsp &s->thread_args);
2006 2b380cc8 2018-10-24 stsp if (errcode)
2007 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2010 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
2011 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
2012 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
2013 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
2016 e5a0f69f 2018-08-18 stsp static const struct got_error *
2017 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2018 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2020 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2021 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2022 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2023 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2024 669b5ffa 2018-10-07 stsp int begin_x = 0;
2025 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2027 e5a0f69f 2018-08-18 stsp switch (ch) {
2029 1e37a5c2 2019-05-12 jcs s->quit = 1;
2032 1e37a5c2 2019-05-12 jcs case KEY_UP:
2035 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2037 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2038 1e37a5c2 2019-05-12 jcs s->selected--;
2040 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
2041 1144d21a 2019-06-21 stsp &s->commits);
2043 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2044 a4292ac5 2019-05-12 jcs case CTRL('b'):
2045 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2047 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
2048 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
2049 1e37a5c2 2019-05-12 jcs s->selected = 0;
2052 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
2053 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
2056 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2059 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2061 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2062 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2063 1e37a5c2 2019-05-12 jcs s->selected++;
2066 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
2067 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
2068 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
2069 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2070 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2072 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2073 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2074 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2075 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2076 1e37a5c2 2019-05-12 jcs if (first == NULL)
2078 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
2079 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
2080 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
2081 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2082 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2085 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2086 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2087 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2088 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2089 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2090 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2092 1e37a5c2 2019-05-12 jcs err = NULL;
2095 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2096 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2097 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2098 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2099 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2101 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2104 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2106 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2107 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2108 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2109 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2110 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
2113 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2114 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2116 1e37a5c2 2019-05-12 jcs return err;
2117 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2119 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2122 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2123 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2125 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2128 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2130 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2131 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2132 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2133 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2136 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2137 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2139 1e37a5c2 2019-05-12 jcs return err;
2140 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2142 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2145 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2146 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2148 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2150 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2151 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2153 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2154 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2155 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2157 1e37a5c2 2019-05-12 jcs return err;
2158 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2159 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2160 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2161 638f9024 2019-05-13 stsp return got_error_from_errno(
2162 1e37a5c2 2019-05-12 jcs "view_open");
2163 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2164 d01904d4 2019-06-25 stsp s->repo, s->head_ref_name, parent_path, 0);
2166 1e37a5c2 2019-05-12 jcs return err;;
2167 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2168 1e37a5c2 2019-05-12 jcs *new_view = lv;
2170 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2171 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2173 1e37a5c2 2019-05-12 jcs return NULL;
2176 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2177 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2179 d01904d4 2019-06-25 stsp return err;
2180 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2181 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2182 d01904d4 2019-06-25 stsp if (lv == NULL)
2183 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2184 d01904d4 2019-06-25 stsp err = get_head_commit_id(&start_id, s->head_ref_name ?
2185 d01904d4 2019-06-25 stsp s->head_ref_name : GOT_REF_HEAD, s->repo);
2186 ef129c5e 2019-08-03 stsp if (err) {
2187 ef129c5e 2019-08-03 stsp view_close(lv);
2188 d01904d4 2019-06-25 stsp return err;
2190 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2191 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2192 e6b23735 2019-10-04 stsp free(start_id);
2193 e6b23735 2019-10-04 stsp view_close(lv);
2194 e6b23735 2019-10-04 stsp return got_error_from_errno("strdup");
2196 e6b23735 2019-10-04 stsp got_ref_list_free(s->refs);
2197 e6b23735 2019-10-04 stsp err = got_ref_list(s->refs, s->repo, NULL,
2198 e6b23735 2019-10-04 stsp got_ref_cmp_by_name, NULL);
2199 e6b23735 2019-10-04 stsp if (err) {
2200 d01904d4 2019-06-25 stsp free(start_id);
2201 ef129c5e 2019-08-03 stsp view_close(lv);
2202 50284065 2019-10-04 stsp return err;
2204 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2205 d01904d4 2019-06-25 stsp s->head_ref_name, in_repo_path, 0);
2206 ef129c5e 2019-08-03 stsp if (err) {
2207 ef129c5e 2019-08-03 stsp free(start_id);
2208 ef129c5e 2019-08-03 stsp view_close(lv);
2209 d01904d4 2019-06-25 stsp return err;;
2211 d01904d4 2019-06-25 stsp *dead_view = view;
2212 d01904d4 2019-06-25 stsp *new_view = lv;
2218 80ddbec8 2018-04-29 stsp return err;
2221 4ed7e80c 2018-05-20 stsp static const struct got_error *
2222 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2224 c2db6724 2019-01-04 stsp const struct got_error *error;
2226 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2227 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2228 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2230 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2231 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2233 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2234 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2236 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2237 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2239 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2240 c2db6724 2019-01-04 stsp if (error != NULL)
2241 c2db6724 2019-01-04 stsp return error;
2243 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2244 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2246 c2db6724 2019-01-04 stsp return NULL;
2249 a915003a 2019-02-05 stsp static void
2250 a915003a 2019-02-05 stsp init_curses(void)
2252 a915003a 2019-02-05 stsp initscr();
2254 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2257 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2258 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2259 a915003a 2019-02-05 stsp curs_set(0);
2260 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2261 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2264 c2db6724 2019-01-04 stsp static const struct got_error *
2265 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2267 80ddbec8 2018-04-29 stsp const struct got_error *error;
2268 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2269 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2270 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2271 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2272 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2273 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2275 04cc582a 2018-08-01 stsp struct tog_view *view;
2277 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2279 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2280 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2281 c2db6724 2019-01-04 stsp NULL) == -1)
2282 80ddbec8 2018-04-29 stsp err(1, "pledge");
2285 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2286 80ddbec8 2018-04-29 stsp switch (ch) {
2288 80ddbec8 2018-04-29 stsp start_commit = optarg;
2291 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2292 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2293 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2297 17020d27 2019-03-07 stsp usage_log();
2298 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2302 80ddbec8 2018-04-29 stsp argc -= optind;
2303 80ddbec8 2018-04-29 stsp argv += optind;
2305 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2306 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2307 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2308 ecb28ae0 2018-07-16 stsp goto done;
2310 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2311 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2312 963f97a1 2019-03-18 stsp goto done;
2313 963f97a1 2019-03-18 stsp error = NULL;
2315 963f97a1 2019-03-18 stsp if (argc == 0) {
2316 963f97a1 2019-03-18 stsp path = strdup("");
2317 963f97a1 2019-03-18 stsp if (path == NULL) {
2318 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2319 ecb28ae0 2018-07-16 stsp goto done;
2321 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2322 963f97a1 2019-03-18 stsp if (worktree) {
2323 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2325 963f97a1 2019-03-18 stsp if (error)
2326 963f97a1 2019-03-18 stsp goto done;
2328 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2329 963f97a1 2019-03-18 stsp if (path == NULL) {
2330 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2331 963f97a1 2019-03-18 stsp goto done;
2335 963f97a1 2019-03-18 stsp usage_log();
2337 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2338 a1fbf39a 2019-08-11 stsp if (worktree)
2339 b9d7675a 2019-08-11 stsp repo_path = strdup(
2340 b9d7675a 2019-08-11 stsp got_worktree_get_repo_path(worktree));
2342 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2344 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2345 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2346 963f97a1 2019-03-18 stsp goto done;
2349 a915003a 2019-02-05 stsp init_curses();
2351 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2352 80ddbec8 2018-04-29 stsp if (error != NULL)
2353 ecb28ae0 2018-07-16 stsp goto done;
2355 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2356 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2357 c02c541e 2019-03-29 stsp if (error)
2358 c02c541e 2019-03-29 stsp goto done;
2360 15a94983 2018-12-23 stsp if (start_commit == NULL)
2361 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&start_id, worktree ?
2362 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2365 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&start_id, start_commit, repo);
2366 f2b6a97d 2019-07-15 stsp if (error) {
2367 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
2368 f2b6a97d 2019-07-15 stsp goto done;
2369 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&start_id,
2370 f2b6a97d 2019-07-15 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2373 80ddbec8 2018-04-29 stsp if (error != NULL)
2374 8b473291 2019-02-21 stsp goto done;
2376 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2377 8b473291 2019-02-21 stsp if (error)
2378 ecb28ae0 2018-07-16 stsp goto done;
2380 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2381 04cc582a 2018-08-01 stsp if (view == NULL) {
2382 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2383 04cc582a 2018-08-01 stsp goto done;
2385 2fc00ff4 2019-08-31 stsp if (worktree) {
2386 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2387 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2388 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2389 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2390 2fc00ff4 2019-08-31 stsp goto done;
2393 2fc00ff4 2019-08-31 stsp error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2395 ba4f502b 2018-08-04 stsp if (error)
2396 ba4f502b 2018-08-04 stsp goto done;
2397 2fc00ff4 2019-08-31 stsp if (worktree) {
2398 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2399 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2400 2fc00ff4 2019-08-31 stsp worktree = NULL;
2402 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2404 ecb28ae0 2018-07-16 stsp free(repo_path);
2405 ecb28ae0 2018-07-16 stsp free(cwd);
2406 ecb28ae0 2018-07-16 stsp free(path);
2407 899d86c2 2018-05-10 stsp free(start_id);
2408 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2410 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2411 ec142235 2019-03-07 stsp if (worktree)
2412 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2413 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2414 80ddbec8 2018-04-29 stsp return error;
2417 4ed7e80c 2018-05-20 stsp __dead static void
2418 9f7d7167 2018-04-29 stsp usage_diff(void)
2421 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2422 9f7d7167 2018-04-29 stsp getprogname());
2426 b304db33 2018-05-20 stsp static char *
2427 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2429 b304db33 2018-05-20 stsp char *line;
2430 b304db33 2018-05-20 stsp size_t linelen;
2431 b304db33 2018-05-20 stsp size_t lineno;
2432 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2434 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2436 b304db33 2018-05-20 stsp *len = linelen;
2437 b304db33 2018-05-20 stsp return line;
2440 4ed7e80c 2018-05-20 stsp static const struct got_error *
2441 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2442 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
2443 c3e9aa98 2019-05-13 jcs char *header)
2445 61e69b96 2018-05-20 stsp const struct got_error *err;
2446 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2447 b304db33 2018-05-20 stsp char *line;
2448 b304db33 2018-05-20 stsp size_t len;
2449 61e69b96 2018-05-20 stsp wchar_t *wline;
2450 e0b650dd 2018-05-20 stsp int width;
2452 26ed57b2 2018-05-19 stsp rewind(f);
2453 f7d12f7e 2018-08-01 stsp werase(view->window);
2455 a3404814 2018-09-02 stsp if (header) {
2456 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
2457 a3404814 2018-09-02 stsp if (err) {
2458 a3404814 2018-09-02 stsp return err;
2461 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2462 a3404814 2018-09-02 stsp wstandout(view->window);
2463 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2464 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2465 a3404814 2018-09-02 stsp wstandend(view->window);
2466 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2467 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2469 a3404814 2018-09-02 stsp if (max_lines <= 1)
2470 a3404814 2018-09-02 stsp return NULL;
2471 a3404814 2018-09-02 stsp max_lines--;
2475 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2476 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2477 26ed57b2 2018-05-19 stsp if (line == NULL) {
2481 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2482 26ed57b2 2018-05-19 stsp free(line);
2486 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2487 61e69b96 2018-05-20 stsp if (err) {
2488 61e69b96 2018-05-20 stsp free(line);
2489 61e69b96 2018-05-20 stsp return err;
2491 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2492 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2493 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2494 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2495 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2496 26ed57b2 2018-05-19 stsp free(line);
2497 2550e4c3 2018-07-13 stsp free(wline);
2498 2550e4c3 2018-07-13 stsp wline = NULL;
2500 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2502 1a57306a 2018-09-02 stsp view_vborder(view);
2504 c3e9aa98 2019-05-13 jcs if (*eof) {
2505 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2506 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2507 c3e9aa98 2019-05-13 jcs nprinted++;
2510 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2512 c3e9aa98 2019-05-13 jcs return err;
2515 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2516 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2517 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2520 26ed57b2 2018-05-19 stsp return NULL;
2523 abd2672a 2018-12-23 stsp static char *
2524 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2526 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2527 09867e48 2019-08-13 stsp char *p, *s;
2529 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2530 09867e48 2019-08-13 stsp if (tm == NULL)
2531 09867e48 2019-08-13 stsp return NULL;
2532 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2533 09867e48 2019-08-13 stsp if (s == NULL)
2534 09867e48 2019-08-13 stsp return NULL;
2535 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2537 abd2672a 2018-12-23 stsp *p = '\0';
2541 4ed7e80c 2018-05-20 stsp static const struct got_error *
2542 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2543 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2545 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2546 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
2547 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2548 5943eee2 2019-08-13 stsp char *id_str = NULL, *logmsg = NULL;
2549 45d799e2 2018-12-23 stsp time_t committer_time;
2550 45d799e2 2018-12-23 stsp const char *author, *committer;
2551 8b473291 2019-02-21 stsp char *refs_str = NULL;
2553 8b473291 2019-02-21 stsp if (refs) {
2554 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
2556 8b473291 2019-02-21 stsp return err;
2559 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2561 abd2672a 2018-12-23 stsp return err;
2563 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2564 15a94983 2018-12-23 stsp if (err) {
2565 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2566 15a94983 2018-12-23 stsp goto done;
2569 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2570 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2571 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2572 abd2672a 2018-12-23 stsp goto done;
2574 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2575 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2576 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2577 abd2672a 2018-12-23 stsp goto done;
2579 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2580 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
2581 09867e48 2019-08-13 stsp if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2582 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2583 abd2672a 2018-12-23 stsp goto done;
2585 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2586 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2587 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2588 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2589 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2590 abd2672a 2018-12-23 stsp goto done;
2592 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2594 5943eee2 2019-08-13 stsp goto done;
2595 5943eee2 2019-08-13 stsp if (fprintf(outfile, "%s\n", logmsg) < 0) {
2596 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2597 abd2672a 2018-12-23 stsp goto done;
2600 abd2672a 2018-12-23 stsp free(id_str);
2601 5943eee2 2019-08-13 stsp free(logmsg);
2602 8b473291 2019-02-21 stsp free(refs_str);
2603 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2604 abd2672a 2018-12-23 stsp return err;
2607 abd2672a 2018-12-23 stsp static const struct got_error *
2608 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2610 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2611 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2612 15a94983 2018-12-23 stsp int obj_type;
2614 511a516b 2018-05-19 stsp f = got_opentemp();
2615 48ae06ee 2018-10-18 stsp if (f == NULL) {
2616 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2617 48ae06ee 2018-10-18 stsp goto done;
2619 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2620 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2621 fb43ecf1 2019-02-11 stsp goto done;
2625 15a94983 2018-12-23 stsp if (s->id1)
2626 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2628 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2630 15a94983 2018-12-23 stsp goto done;
2632 15a94983 2018-12-23 stsp switch (obj_type) {
2633 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2634 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2635 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2637 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2638 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2639 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2641 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2642 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2643 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2644 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2646 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2649 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2650 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2651 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2653 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2654 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2655 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2656 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2657 8b473291 2019-02-21 stsp s->repo, f);
2662 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2664 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2665 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2669 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2673 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2674 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2675 48ae06ee 2018-10-18 stsp return err;
2678 f5215bb9 2019-02-22 stsp static void
2679 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2681 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2682 f5215bb9 2019-02-22 stsp update_panels();
2683 f5215bb9 2019-02-22 stsp doupdate();
2686 48ae06ee 2018-10-18 stsp static const struct got_error *
2687 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2688 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2689 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2691 48ae06ee 2018-10-18 stsp const struct got_error *err;
2693 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2694 15a94983 2018-12-23 stsp int type1, type2;
2695 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2697 15a94983 2018-12-23 stsp return err;
2698 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2700 15a94983 2018-12-23 stsp return err;
2702 15a94983 2018-12-23 stsp if (type1 != type2)
2703 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2706 15a94983 2018-12-23 stsp if (id1) {
2707 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2708 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2709 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2711 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2713 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2714 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2715 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2716 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2717 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2719 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2720 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2721 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2722 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2723 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2724 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2725 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2727 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2728 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2729 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2731 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2732 48ae06ee 2018-10-18 stsp if (err) {
2733 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2734 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2735 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2736 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2737 48ae06ee 2018-10-18 stsp return err;
2740 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2741 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2742 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2744 5dc9f4bc 2018-08-04 stsp return NULL;
2747 e5a0f69f 2018-08-18 stsp static const struct got_error *
2748 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2750 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2752 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2753 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2754 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2755 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2756 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2757 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2758 e5a0f69f 2018-08-18 stsp return err;
2761 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2762 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2764 a3404814 2018-09-02 stsp const struct got_error *err;
2765 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2766 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2768 a3404814 2018-09-02 stsp if (s->id1) {
2769 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2771 a3404814 2018-09-02 stsp return err;
2773 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2775 a3404814 2018-09-02 stsp return err;
2777 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2778 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2779 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2780 a3404814 2018-09-02 stsp free(id_str1);
2781 a3404814 2018-09-02 stsp free(id_str2);
2782 a3404814 2018-09-02 stsp return err;
2784 a3404814 2018-09-02 stsp free(id_str1);
2785 a3404814 2018-09-02 stsp free(id_str2);
2787 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2788 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2792 15a087fe 2019-02-21 stsp static const struct got_error *
2793 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2794 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2796 d7a04538 2019-02-21 stsp const struct got_error *err;
2797 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
2798 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
2799 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
2801 15a087fe 2019-02-21 stsp free(s->id2);
2802 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2803 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2804 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2806 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2808 d7a04538 2019-02-21 stsp return err;
2809 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
2810 15a087fe 2019-02-21 stsp free(s->id1);
2811 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
2812 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2813 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
2814 15a087fe 2019-02-21 stsp return NULL;
2817 0cf4efb1 2018-09-29 stsp static const struct got_error *
2818 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2819 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2821 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2822 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2823 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
2824 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
2827 e5a0f69f 2018-08-18 stsp switch (ch) {
2829 1e37a5c2 2019-05-12 jcs case KEY_UP:
2830 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
2831 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2833 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2834 a60a9dc4 2019-05-13 jcs case CTRL('b'):
2835 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
2838 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
2839 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
2840 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2843 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2844 1e37a5c2 2019-05-12 jcs if (!s->eof)
2845 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2847 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
2848 a60a9dc4 2019-05-13 jcs case CTRL('f'):
2850 00ba99a7 2019-05-12 jcs if (s->eof)
2853 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
2854 1e37a5c2 2019-05-12 jcs char *line;
2855 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
2856 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2857 1e37a5c2 2019-05-12 jcs if (line == NULL)
2862 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
2863 1e37a5c2 2019-05-12 jcs s->diff_context--;
2864 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2865 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2869 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2870 1e37a5c2 2019-05-12 jcs s->diff_context++;
2871 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2872 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2877 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2879 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2880 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
2881 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
2882 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2885 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2890 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2894 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2895 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2897 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2898 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2902 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2904 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2906 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2907 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
2909 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
2910 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
2911 1e37a5c2 2019-05-12 jcs update_panels();
2912 1e37a5c2 2019-05-12 jcs doupdate();
2914 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
2915 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
2916 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
2917 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
2921 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2926 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
2927 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2930 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2934 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2935 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2937 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2938 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2944 bcbd79e2 2018-08-19 stsp return err;
2947 4ed7e80c 2018-05-20 stsp static const struct got_error *
2948 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2950 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2951 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2952 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2953 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2954 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2955 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2957 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2959 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2961 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2962 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2963 eb6600df 2019-01-04 stsp NULL) == -1)
2964 26ed57b2 2018-05-19 stsp err(1, "pledge");
2967 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2968 26ed57b2 2018-05-19 stsp switch (ch) {
2970 17020d27 2019-03-07 stsp usage_diff();
2971 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2975 26ed57b2 2018-05-19 stsp argc -= optind;
2976 26ed57b2 2018-05-19 stsp argv += optind;
2978 26ed57b2 2018-05-19 stsp if (argc == 0) {
2979 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2980 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2981 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2982 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2983 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2984 15a94983 2018-12-23 stsp id_str1 = argv[0];
2985 15a94983 2018-12-23 stsp id_str2 = argv[1];
2986 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2987 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2988 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2989 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2990 15a94983 2018-12-23 stsp id_str1 = argv[1];
2991 15a94983 2018-12-23 stsp id_str2 = argv[2];
2993 26ed57b2 2018-05-19 stsp usage_diff();
2995 a915003a 2019-02-05 stsp init_curses();
2997 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2998 eb6600df 2019-01-04 stsp if (error)
2999 eb6600df 2019-01-04 stsp goto done;
3001 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3002 26ed57b2 2018-05-19 stsp if (error)
3003 26ed57b2 2018-05-19 stsp goto done;
3005 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
3006 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3007 26ed57b2 2018-05-19 stsp if (error)
3008 26ed57b2 2018-05-19 stsp goto done;
3010 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
3011 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3012 26ed57b2 2018-05-19 stsp if (error)
3013 26ed57b2 2018-05-19 stsp goto done;
3015 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3016 8b473291 2019-02-21 stsp if (error)
3017 8b473291 2019-02-21 stsp goto done;
3019 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3020 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3021 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3022 ea5e7bb5 2018-08-01 stsp goto done;
3024 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3025 5dc9f4bc 2018-08-04 stsp if (error)
3026 5dc9f4bc 2018-08-04 stsp goto done;
3027 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3029 c02c541e 2019-03-29 stsp free(repo_path);
3031 921be706 2019-06-28 stsp got_repo_close(repo);
3032 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3033 26ed57b2 2018-05-19 stsp return error;
3036 4ed7e80c 2018-05-20 stsp __dead static void
3037 9f7d7167 2018-04-29 stsp usage_blame(void)
3040 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3041 9f7d7167 2018-04-29 stsp getprogname());
3045 84451b3e 2018-07-10 stsp struct tog_blame_line {
3046 84451b3e 2018-07-10 stsp int annotated;
3047 84451b3e 2018-07-10 stsp struct got_object_id *id;
3050 4ed7e80c 2018-05-20 stsp static const struct got_error *
3051 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3052 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
3053 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
3054 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
3056 84451b3e 2018-07-10 stsp const struct got_error *err;
3057 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3058 84451b3e 2018-07-10 stsp char *line;
3059 84451b3e 2018-07-10 stsp size_t len;
3060 84451b3e 2018-07-10 stsp wchar_t *wline;
3061 27a741e5 2019-09-11 stsp int width;
3062 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3063 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3064 ab089a2a 2018-07-12 stsp char *id_str;
3066 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
3068 ab089a2a 2018-07-12 stsp return err;
3070 84451b3e 2018-07-10 stsp rewind(f);
3071 f7d12f7e 2018-08-01 stsp werase(view->window);
3073 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3074 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3075 ab089a2a 2018-07-12 stsp free(id_str);
3076 ab089a2a 2018-07-12 stsp return err;
3079 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3080 ab089a2a 2018-07-12 stsp free(line);
3081 2550e4c3 2018-07-13 stsp line = NULL;
3083 1cae65b4 2019-09-22 stsp return err;
3084 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3085 a3404814 2018-09-02 stsp wstandout(view->window);
3086 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3087 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3088 a3404814 2018-09-02 stsp wstandend(view->window);
3089 2550e4c3 2018-07-13 stsp free(wline);
3090 2550e4c3 2018-07-13 stsp wline = NULL;
3091 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3092 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3094 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3095 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
3096 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
3097 ab089a2a 2018-07-12 stsp free(id_str);
3098 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3100 ab089a2a 2018-07-12 stsp free(id_str);
3101 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3102 3f60a8ef 2018-07-10 stsp free(line);
3103 2550e4c3 2018-07-13 stsp line = NULL;
3105 3f60a8ef 2018-07-10 stsp return err;
3106 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3107 2550e4c3 2018-07-13 stsp free(wline);
3108 2550e4c3 2018-07-13 stsp wline = NULL;
3109 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3110 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3113 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
3114 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
3115 84451b3e 2018-07-10 stsp if (line == NULL) {
3119 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
3120 84451b3e 2018-07-10 stsp free(line);
3124 27a741e5 2019-09-11 stsp if (view->ncols <= 9) {
3125 27a741e5 2019-09-11 stsp width = 9;
3126 27a741e5 2019-09-11 stsp wline = wcsdup(L"");
3127 27a741e5 2019-09-11 stsp if (wline == NULL)
3128 27a741e5 2019-09-11 stsp err = got_error_from_errno("wcsdup");
3130 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line,
3131 27a741e5 2019-09-11 stsp view->ncols - 9, 9);
3132 27a741e5 2019-09-11 stsp width += 9;
3134 84451b3e 2018-07-10 stsp if (err) {
3135 84451b3e 2018-07-10 stsp free(line);
3136 84451b3e 2018-07-10 stsp return err;
3139 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3140 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3142 8d0fe45a 2019-08-12 stsp if (nlines > 0) {
3143 8d0fe45a 2019-08-12 stsp blame_line = &lines[lineno - 1];
3144 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3145 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3146 27a741e5 2019-09-11 stsp !(view->focussed &&
3147 27a741e5 2019-09-11 stsp nprinted == selected_line - 1)) {
3148 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3149 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3150 8d0fe45a 2019-08-12 stsp char *id_str;
3151 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3152 8d0fe45a 2019-08-12 stsp if (err) {
3153 8d0fe45a 2019-08-12 stsp free(line);
3154 8d0fe45a 2019-08-12 stsp free(wline);
3155 8d0fe45a 2019-08-12 stsp return err;
3157 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3158 8d0fe45a 2019-08-12 stsp free(id_str);
3159 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3161 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3162 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3165 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3166 ee41ec32 2018-07-10 stsp prev_id = NULL;
3169 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3170 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3171 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3173 27a741e5 2019-09-11 stsp waddwstr(view->window, wline);
3174 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
3175 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
3176 84451b3e 2018-07-10 stsp if (++nprinted == 1)
3177 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
3178 84451b3e 2018-07-10 stsp free(line);
3179 2550e4c3 2018-07-13 stsp free(wline);
3180 2550e4c3 2018-07-13 stsp wline = NULL;
3182 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
3184 1a57306a 2018-09-02 stsp view_vborder(view);
3186 84451b3e 2018-07-10 stsp return NULL;
3189 84451b3e 2018-07-10 stsp static const struct got_error *
3190 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3192 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
3193 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
3194 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
3195 1a76625f 2018-10-22 stsp int errcode;
3197 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
3198 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3199 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3201 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3202 1a76625f 2018-10-22 stsp if (errcode)
3203 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3205 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3206 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3207 d68a0a7d 2018-07-10 stsp goto done;
3210 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3211 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3213 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3214 d68a0a7d 2018-07-10 stsp if (line->annotated)
3215 d68a0a7d 2018-07-10 stsp goto done;
3217 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3218 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3219 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3220 84451b3e 2018-07-10 stsp goto done;
3222 84451b3e 2018-07-10 stsp line->annotated = 1;
3224 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3225 1a76625f 2018-10-22 stsp if (errcode)
3226 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3227 84451b3e 2018-07-10 stsp return err;
3230 84451b3e 2018-07-10 stsp static void *
3231 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3233 18430de3 2018-07-10 stsp const struct got_error *err;
3234 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3235 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3236 1a76625f 2018-10-22 stsp int errcode;
3238 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
3239 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3240 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
3241 fc06ba56 2019-08-22 stsp err = NULL;
3243 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3244 1a76625f 2018-10-22 stsp if (errcode)
3245 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3246 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3248 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3249 c9beca56 2018-07-22 stsp ta->repo = NULL;
3250 c9beca56 2018-07-22 stsp *ta->complete = 1;
3252 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3253 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3254 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3256 18430de3 2018-07-10 stsp return (void *)err;
3259 245d91c1 2018-07-12 stsp static struct got_object_id *
3260 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3261 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
3263 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3265 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
3266 8d0fe45a 2019-08-12 stsp return NULL;
3268 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3269 245d91c1 2018-07-12 stsp if (!line->annotated)
3270 245d91c1 2018-07-12 stsp return NULL;
3272 245d91c1 2018-07-12 stsp return line->id;
3275 b880a918 2018-07-10 stsp static const struct got_error *
3276 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3278 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3281 245d91c1 2018-07-12 stsp if (blame->thread) {
3282 1a76625f 2018-10-22 stsp int errcode;
3283 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3284 1a76625f 2018-10-22 stsp if (errcode)
3285 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3286 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3287 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3288 1a76625f 2018-10-22 stsp if (errcode)
3289 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3290 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3291 1a76625f 2018-10-22 stsp if (errcode)
3292 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3293 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3294 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3295 245d91c1 2018-07-12 stsp err = NULL;
3296 245d91c1 2018-07-12 stsp blame->thread = NULL;
3298 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3299 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3300 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3302 245d91c1 2018-07-12 stsp if (blame->f) {
3303 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3304 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3305 245d91c1 2018-07-12 stsp blame->f = NULL;
3307 57670559 2018-12-23 stsp if (blame->lines) {
3308 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3309 57670559 2018-12-23 stsp free(blame->lines[i].id);
3310 57670559 2018-12-23 stsp free(blame->lines);
3311 57670559 2018-12-23 stsp blame->lines = NULL;
3313 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3314 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3316 245d91c1 2018-07-12 stsp return err;
3319 245d91c1 2018-07-12 stsp static const struct got_error *
3320 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
3322 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
3323 fc06ba56 2019-08-22 stsp int *done = arg;
3324 fc06ba56 2019-08-22 stsp int errcode;
3326 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3327 fc06ba56 2019-08-22 stsp if (errcode)
3328 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3329 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
3331 fc06ba56 2019-08-22 stsp if (*done)
3332 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
3334 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3335 fc06ba56 2019-08-22 stsp if (errcode)
3336 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3337 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
3339 fc06ba56 2019-08-22 stsp return err;
3342 fc06ba56 2019-08-22 stsp static const struct got_error *
3343 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3344 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3345 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3346 245d91c1 2018-07-12 stsp struct got_repository *repo)
3348 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3349 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3350 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3351 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3352 15a94983 2018-12-23 stsp int obj_type;
3354 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3356 15a94983 2018-12-23 stsp return err;
3357 15a94983 2018-12-23 stsp if (obj_id == NULL)
3358 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
3360 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3362 84451b3e 2018-07-10 stsp goto done;
3364 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3365 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3366 84451b3e 2018-07-10 stsp goto done;
3369 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3371 a70480e0 2018-06-23 stsp goto done;
3372 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3373 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3374 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3375 84451b3e 2018-07-10 stsp goto done;
3377 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3378 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
3379 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
3380 84451b3e 2018-07-10 stsp goto done;
3382 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3383 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3384 b02560ec 2019-08-19 stsp blame->nlines--;
3386 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3387 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3388 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3389 84451b3e 2018-07-10 stsp goto done;
3392 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3394 bd24772e 2018-07-11 stsp goto done;
3396 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3397 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3398 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3399 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3400 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3401 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3402 245d91c1 2018-07-12 stsp goto done;
3404 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3406 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3407 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3408 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3409 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3410 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
3411 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_arg = done;
3412 245d91c1 2018-07-12 stsp *blame_complete = 0;
3416 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3417 27d434c2 2018-09-15 stsp free(obj_id);
3419 245d91c1 2018-07-12 stsp stop_blame(blame);
3420 245d91c1 2018-07-12 stsp return err;
3423 245d91c1 2018-07-12 stsp static const struct got_error *
3424 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3425 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3426 8b473291 2019-02-21 stsp struct got_repository *repo)
3428 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3429 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3431 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3433 c4843652 2019-08-12 stsp s->path = strdup(path);
3434 c4843652 2019-08-12 stsp if (s->path == NULL)
3435 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
3437 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3438 c4843652 2019-08-12 stsp if (err) {
3439 c4843652 2019-08-12 stsp free(s->path);
3440 7cbe629d 2018-08-04 stsp return err;
3443 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3444 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3445 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3446 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3447 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3448 fb2756b9 2018-08-04 stsp s->repo = repo;
3449 8b473291 2019-02-21 stsp s->refs = refs;
3450 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3451 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3453 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3454 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3455 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3456 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
3457 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
3459 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3460 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3461 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3462 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3465 e5a0f69f 2018-08-18 stsp static const struct got_error *
3466 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3468 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3469 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3471 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3472 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3474 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3475 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3476 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3477 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3478 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3481 e5a0f69f 2018-08-18 stsp free(s->path);
3483 e5a0f69f 2018-08-18 stsp return err;
3486 7cbe629d 2018-08-04 stsp static const struct got_error *
3487 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
3489 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3491 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
3492 6c4c42e0 2019-06-24 stsp return NULL;
3495 6c4c42e0 2019-06-24 stsp static int
3496 6c4c42e0 2019-06-24 stsp match_line(const char *line, regex_t *regex)
3498 6c4c42e0 2019-06-24 stsp regmatch_t regmatch;
3500 6c4c42e0 2019-06-24 stsp return regexec(regex, line, 1, ®match, 0) == 0;
3504 6c4c42e0 2019-06-24 stsp static const struct got_error *
3505 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
3507 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3508 6c4c42e0 2019-06-24 stsp int lineno;
3510 6c4c42e0 2019-06-24 stsp if (!view->searching) {
3511 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3512 6c4c42e0 2019-06-24 stsp return NULL;
3515 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3516 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3517 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
3519 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
3521 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3522 6c4c42e0 2019-06-24 stsp lineno = 1;
3524 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3527 6c4c42e0 2019-06-24 stsp while (1) {
3528 6c4c42e0 2019-06-24 stsp char *line = NULL;
3529 6c4c42e0 2019-06-24 stsp off_t offset;
3530 6c4c42e0 2019-06-24 stsp size_t len;
3532 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
3533 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
3534 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3535 6c4c42e0 2019-06-24 stsp free(line);
3539 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3540 6c4c42e0 2019-06-24 stsp lineno = 1;
3542 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3545 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
3546 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3547 6c4c42e0 2019-06-24 stsp free(line);
3548 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
3550 6c4c42e0 2019-06-24 stsp free(line);
3551 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
3552 2246482e 2019-06-25 stsp if (line && match_line(line, &view->regex)) {
3553 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3554 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
3555 6c4c42e0 2019-06-24 stsp free(line);
3558 6c4c42e0 2019-06-24 stsp free(line);
3559 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3565 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3566 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
3567 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
3570 6c4c42e0 2019-06-24 stsp return NULL;
3573 6c4c42e0 2019-06-24 stsp static const struct got_error *
3574 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
3576 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3577 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
3578 2b380cc8 2018-10-24 stsp int errcode;
3580 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
3581 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3582 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
3583 2b380cc8 2018-10-24 stsp if (errcode)
3584 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3586 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
3589 51fe7530 2019-08-19 stsp if (s->blame_complete)
3590 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
3592 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3593 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3594 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
3595 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
3597 669b5ffa 2018-10-07 stsp view_vborder(view);
3598 e5a0f69f 2018-08-18 stsp return err;
3601 e5a0f69f 2018-08-18 stsp static const struct got_error *
3602 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3603 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3605 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
3606 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
3607 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3608 669b5ffa 2018-10-07 stsp int begin_x = 0;
3610 e5a0f69f 2018-08-18 stsp switch (ch) {
3612 1e37a5c2 2019-05-12 jcs s->done = 1;
3615 1e37a5c2 2019-05-12 jcs case KEY_UP:
3616 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
3617 1e37a5c2 2019-05-12 jcs s->selected_line--;
3618 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
3619 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3620 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3622 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3623 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
3624 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3627 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3628 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3629 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3631 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3634 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3635 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3636 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3637 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3638 1e37a5c2 2019-05-12 jcs s->selected_line++;
3639 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3640 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3641 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3644 1e37a5c2 2019-05-12 jcs case 'p': {
3645 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3646 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3647 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3648 1e37a5c2 2019-05-12 jcs if (id == NULL)
3650 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3651 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3652 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3653 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3654 1e37a5c2 2019-05-12 jcs int obj_type;
3655 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3656 1e37a5c2 2019-05-12 jcs s->repo, id);
3659 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3660 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3661 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3662 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3665 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3666 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3667 1e37a5c2 2019-05-12 jcs pid->id, s->path);
3668 e5a0f69f 2018-08-18 stsp if (err) {
3669 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
3670 1e37a5c2 2019-05-12 jcs err = NULL;
3671 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3674 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
3676 1e37a5c2 2019-05-12 jcs free(blob_id);
3677 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
3678 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
3679 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3682 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3684 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3686 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
3687 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
3689 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3694 1e37a5c2 2019-05-12 jcs s->done = 1;
3695 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3696 1e37a5c2 2019-05-12 jcs s->done = 0;
3697 1e37a5c2 2019-05-12 jcs if (thread_err)
3699 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3700 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
3701 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3702 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3703 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
3704 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
3709 1e37a5c2 2019-05-12 jcs case 'B': {
3710 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
3711 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
3712 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
3714 1e37a5c2 2019-05-12 jcs s->done = 1;
3715 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3716 1e37a5c2 2019-05-12 jcs s->done = 0;
3717 1e37a5c2 2019-05-12 jcs if (thread_err)
3719 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3720 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
3721 1e37a5c2 2019-05-12 jcs s->blamed_commit =
3722 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
3723 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3724 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3725 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
3726 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
3731 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3732 1e37a5c2 2019-05-12 jcs case '\r': {
3733 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3734 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
3735 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
3736 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3737 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3738 1e37a5c2 2019-05-12 jcs if (id == NULL)
3740 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
3743 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
3744 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
3745 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3746 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3747 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3748 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
3749 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3750 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
3753 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
3754 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
3755 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3757 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3760 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3761 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3764 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
3766 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3769 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
3770 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
3772 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3777 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3779 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3780 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
3781 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
3784 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3785 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
3786 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3787 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3790 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
3791 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
3792 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
3793 1e37a5c2 2019-05-12 jcs view->nlines - 2;
3795 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
3796 1e37a5c2 2019-05-12 jcs s->blame.nlines -
3797 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
3799 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3800 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
3801 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3802 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3808 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
3811 a70480e0 2018-06-23 stsp static const struct got_error *
3812 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
3814 a70480e0 2018-06-23 stsp const struct got_error *error;
3815 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
3816 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3817 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
3818 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3819 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3820 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
3822 e1cd8fed 2018-08-01 stsp struct tog_view *view;
3824 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3826 a70480e0 2018-06-23 stsp #ifndef PROFILE
3827 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3828 8e94dd5b 2019-01-04 stsp NULL) == -1)
3829 a70480e0 2018-06-23 stsp err(1, "pledge");
3832 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3833 a70480e0 2018-06-23 stsp switch (ch) {
3835 a70480e0 2018-06-23 stsp commit_id_str = optarg;
3838 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3839 69069811 2018-08-02 stsp if (repo_path == NULL)
3840 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3844 17020d27 2019-03-07 stsp usage_blame();
3845 a70480e0 2018-06-23 stsp /* NOTREACHED */
3849 a70480e0 2018-06-23 stsp argc -= optind;
3850 a70480e0 2018-06-23 stsp argv += optind;
3852 69069811 2018-08-02 stsp if (argc == 1)
3853 69069811 2018-08-02 stsp path = argv[0];
3855 a70480e0 2018-06-23 stsp usage_blame();
3857 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
3858 69069811 2018-08-02 stsp if (cwd == NULL) {
3859 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3860 69069811 2018-08-02 stsp goto done;
3862 69069811 2018-08-02 stsp if (repo_path == NULL) {
3863 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3864 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3865 69069811 2018-08-02 stsp goto done;
3867 eb41ed75 2019-02-05 stsp error = NULL;
3868 eb41ed75 2019-02-05 stsp if (worktree) {
3869 eb41ed75 2019-02-05 stsp repo_path =
3870 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3871 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
3872 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3873 eb41ed75 2019-02-05 stsp if (error)
3874 eb41ed75 2019-02-05 stsp goto done;
3876 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
3877 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
3878 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3879 eb41ed75 2019-02-05 stsp goto done;
3884 a915003a 2019-02-05 stsp init_curses();
3886 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3887 c02c541e 2019-03-29 stsp if (error != NULL)
3888 8e94dd5b 2019-01-04 stsp goto done;
3890 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3891 c02c541e 2019-03-29 stsp if (error)
3892 92205607 2019-01-04 stsp goto done;
3894 eb41ed75 2019-02-05 stsp if (worktree) {
3895 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3896 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3897 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3898 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3899 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3900 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3901 eb41ed75 2019-02-05 stsp path) == -1) {
3902 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3903 eb41ed75 2019-02-05 stsp goto done;
3905 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3908 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3910 eb41ed75 2019-02-05 stsp if (error)
3911 69069811 2018-08-02 stsp goto done;
3913 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
3914 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
3915 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3916 a70480e0 2018-06-23 stsp if (error != NULL)
3917 66b4983c 2018-06-23 stsp goto done;
3918 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3919 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
3921 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&commit_id, commit_id_str, repo);
3922 f2b6a97d 2019-07-15 stsp if (error) {
3923 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
3924 f2b6a97d 2019-07-15 stsp goto done;
3925 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&commit_id,
3926 f2b6a97d 2019-07-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3929 a19e88aa 2018-06-23 stsp if (error != NULL)
3930 8b473291 2019-02-21 stsp goto done;
3932 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3933 8b473291 2019-02-21 stsp if (error)
3934 a19e88aa 2018-06-23 stsp goto done;
3936 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3937 e1cd8fed 2018-08-01 stsp if (view == NULL) {
3938 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3939 e1cd8fed 2018-08-01 stsp goto done;
3941 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3942 7cbe629d 2018-08-04 stsp if (error)
3943 7cbe629d 2018-08-04 stsp goto done;
3944 12314ad4 2019-08-31 stsp if (worktree) {
3945 12314ad4 2019-08-31 stsp /* Release work tree lock. */
3946 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
3947 12314ad4 2019-08-31 stsp worktree = NULL;
3949 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3951 69069811 2018-08-02 stsp free(repo_path);
3952 69069811 2018-08-02 stsp free(cwd);
3953 a70480e0 2018-06-23 stsp free(commit_id);
3954 eb41ed75 2019-02-05 stsp if (worktree)
3955 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
3957 a70480e0 2018-06-23 stsp got_repo_close(repo);
3958 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3959 a70480e0 2018-06-23 stsp return error;
3962 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3963 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
3964 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
3965 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
3966 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
3967 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
3968 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
3970 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3971 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
3972 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
3973 ffd1d5e5 2018-06-23 stsp int width, n;
3975 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
3977 f7d12f7e 2018-08-01 stsp werase(view->window);
3979 ffd1d5e5 2018-06-23 stsp if (limit == 0)
3980 ffd1d5e5 2018-06-23 stsp return NULL;
3982 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, label, view->ncols, 0);
3984 ffd1d5e5 2018-06-23 stsp return err;
3985 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3986 a3404814 2018-09-02 stsp wstandout(view->window);
3987 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3988 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3989 a3404814 2018-09-02 stsp wstandend(view->window);
3990 2550e4c3 2018-07-13 stsp free(wline);
3991 2550e4c3 2018-07-13 stsp wline = NULL;
3992 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3993 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3994 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3995 ffd1d5e5 2018-06-23 stsp return NULL;
3996 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
3998 ce52c690 2018-06-23 stsp return err;
3999 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4000 2550e4c3 2018-07-13 stsp free(wline);
4001 2550e4c3 2018-07-13 stsp wline = NULL;
4002 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4003 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4004 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4005 ffd1d5e5 2018-06-23 stsp return NULL;
4006 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4007 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4008 a1eca9bb 2018-06-23 stsp return NULL;
4010 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
4011 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
4012 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
4013 0cf4efb1 2018-09-29 stsp if (view->focussed)
4014 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4015 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
4017 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4018 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
4019 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4020 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4021 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4022 ffd1d5e5 2018-06-23 stsp return NULL;
4026 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
4027 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
4030 ffd1d5e5 2018-06-23 stsp while (te) {
4031 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
4032 848d6979 2019-08-12 stsp const char *modestr = "";
4034 1d13200f 2018-07-12 stsp if (show_ids) {
4035 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
4037 638f9024 2019-05-13 stsp return got_error_from_errno(
4038 230a42bd 2019-05-11 jcs "got_object_id_str");
4040 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4041 63c5ca5d 2019-08-24 stsp modestr = "$";
4042 63c5ca5d 2019-08-24 stsp else if (S_ISLNK(te->mode))
4043 848d6979 2019-08-12 stsp modestr = "@";
4044 848d6979 2019-08-12 stsp else if (S_ISDIR(te->mode))
4045 848d6979 2019-08-12 stsp modestr = "/";
4046 848d6979 2019-08-12 stsp else if (te->mode & S_IXUSR)
4047 848d6979 2019-08-12 stsp modestr = "*";
4048 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4049 848d6979 2019-08-12 stsp te->name, modestr) == -1) {
4050 1d13200f 2018-07-12 stsp free(id_str);
4051 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4053 1d13200f 2018-07-12 stsp free(id_str);
4054 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4055 ffd1d5e5 2018-06-23 stsp if (err) {
4056 ffd1d5e5 2018-06-23 stsp free(line);
4059 ffd1d5e5 2018-06-23 stsp if (n == selected) {
4060 0cf4efb1 2018-09-29 stsp if (view->focussed)
4061 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4062 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
4064 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4065 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4066 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4067 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
4068 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4069 ffd1d5e5 2018-06-23 stsp free(line);
4070 2550e4c3 2018-07-13 stsp free(wline);
4071 2550e4c3 2018-07-13 stsp wline = NULL;
4073 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4074 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
4075 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4077 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
4080 ffd1d5e5 2018-06-23 stsp return err;
4083 ffd1d5e5 2018-06-23 stsp static void
4084 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
4085 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
4086 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
4088 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
4091 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
4094 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
4095 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
4096 ffd1d5e5 2018-06-23 stsp if (!isroot)
4097 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4101 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
4102 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
4103 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
4104 ffd1d5e5 2018-06-23 stsp prev = te;
4105 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
4107 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
4108 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
4110 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4111 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4114 768394f3 2019-01-24 stsp static int
4115 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4116 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
4117 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
4119 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4120 ffd1d5e5 2018-06-23 stsp int n = 0;
4122 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
4123 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4125 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
4126 768394f3 2019-01-24 stsp last = last_displayed_entry;
4127 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4128 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
4129 768394f3 2019-01-24 stsp if (last) {
4130 768394f3 2019-01-24 stsp *first_displayed_entry = next;
4131 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
4137 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4138 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4139 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4141 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4142 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4143 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4145 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4146 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
4148 ce52c690 2018-06-23 stsp len += strlen(te->name);
4150 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4151 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4152 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4154 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4155 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4156 d9765a41 2018-06-23 stsp while (pt) {
4157 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4158 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4159 cb2ebc8a 2018-06-23 stsp goto done;
4161 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
4162 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4163 cb2ebc8a 2018-06-23 stsp goto done;
4165 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4168 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
4169 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4170 ce52c690 2018-06-23 stsp goto done;
4174 ce52c690 2018-06-23 stsp if (err) {
4175 ce52c690 2018-06-23 stsp free(*path);
4176 ce52c690 2018-06-23 stsp *path = NULL;
4178 ce52c690 2018-06-23 stsp return err;
4181 ce52c690 2018-06-23 stsp static const struct got_error *
4182 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
4183 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4184 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4185 8b473291 2019-02-21 stsp struct got_repository *repo)
4187 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
4188 ce52c690 2018-06-23 stsp char *path;
4189 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
4191 a0de39f3 2019-08-09 stsp *new_view = NULL;
4193 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
4195 ce52c690 2018-06-23 stsp return err;
4197 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4198 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
4199 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
4200 83ce39e3 2019-08-12 stsp goto done;
4203 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
4204 e5a0f69f 2018-08-18 stsp if (err) {
4205 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
4206 fc06ba56 2019-08-22 stsp err = NULL;
4207 e5a0f69f 2018-08-18 stsp view_close(blame_view);
4209 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
4211 83ce39e3 2019-08-12 stsp free(path);
4212 69efd4c4 2018-07-18 stsp return err;
4215 69efd4c4 2018-07-18 stsp static const struct got_error *
4216 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
4217 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4218 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4219 8b473291 2019-02-21 stsp struct got_repository *repo)
4221 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
4222 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
4223 69efd4c4 2018-07-18 stsp char *path;
4225 a0de39f3 2019-08-09 stsp *new_view = NULL;
4227 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4228 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
4229 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
4231 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
4233 69efd4c4 2018-07-18 stsp return err;
4235 d01904d4 2019-06-25 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4237 e5a0f69f 2018-08-18 stsp view_close(log_view);
4239 e5a0f69f 2018-08-18 stsp *new_view = log_view;
4240 cb2ebc8a 2018-06-23 stsp free(path);
4241 cb2ebc8a 2018-06-23 stsp return err;
4244 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4245 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
4246 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4247 8b473291 2019-02-21 stsp struct got_repository *repo)
4249 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4250 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
4251 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4253 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
4255 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
4256 ffd1d5e5 2018-06-23 stsp if (err != NULL)
4257 ffd1d5e5 2018-06-23 stsp goto done;
4259 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4260 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4261 ffd1d5e5 2018-06-23 stsp goto done;
4264 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
4265 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
4266 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4267 8d0fe45a 2019-08-12 stsp s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4268 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
4269 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
4270 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4271 6484ec90 2018-09-29 stsp goto done;
4273 8b473291 2019-02-21 stsp s->refs = refs;
4274 fb2756b9 2018-08-04 stsp s->repo = repo;
4276 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
4277 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
4278 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
4279 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
4280 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
4282 ad80ab7b 2018-08-04 stsp free(commit_id_str);
4283 6484ec90 2018-09-29 stsp if (err) {
4284 fb2756b9 2018-08-04 stsp free(s->tree_label);
4285 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4287 ad80ab7b 2018-08-04 stsp return err;
4290 e5a0f69f 2018-08-18 stsp static const struct got_error *
4291 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
4293 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4295 fb2756b9 2018-08-04 stsp free(s->tree_label);
4296 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4297 6484ec90 2018-09-29 stsp free(s->commit_id);
4298 6484ec90 2018-09-29 stsp s->commit_id = NULL;
4299 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
4300 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
4301 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
4302 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
4303 ad80ab7b 2018-08-04 stsp free(parent);
4306 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
4307 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
4308 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
4310 7c32bd05 2019-06-22 stsp return NULL;
4313 7c32bd05 2019-06-22 stsp static const struct got_error *
4314 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
4316 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4318 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
4319 7c32bd05 2019-06-22 stsp return NULL;
4322 7c32bd05 2019-06-22 stsp static int
4323 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4325 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
4327 7c32bd05 2019-06-22 stsp return regexec(regex, te->name, 1, ®match, 0) == 0;
4330 7c32bd05 2019-06-22 stsp static const struct got_error *
4331 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
4333 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4334 a0de39f3 2019-08-09 stsp struct got_tree_entry *entry = NULL, *te;
4336 7c32bd05 2019-06-22 stsp if (!view->searching) {
4337 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4338 7c32bd05 2019-06-22 stsp return NULL;
4341 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4342 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
4343 7c32bd05 2019-06-22 stsp if (s->selected_entry)
4344 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4346 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4349 7c32bd05 2019-06-22 stsp if (s->selected_entry == NULL) {
4350 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4351 7c32bd05 2019-06-22 stsp entry = te;
4353 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4354 7c32bd05 2019-06-22 stsp entry = te;
4355 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) ==
4356 7c32bd05 2019-06-22 stsp s->selected_entry)
4362 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4363 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4365 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4366 7c32bd05 2019-06-22 stsp entry = te;
4370 7c32bd05 2019-06-22 stsp while (1) {
4371 7c32bd05 2019-06-22 stsp if (entry == NULL) {
4372 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
4373 ac66afb8 2019-06-24 stsp view->search_next_done = 1;
4374 ac66afb8 2019-06-24 stsp return NULL;
4376 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4377 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4379 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4380 7c32bd05 2019-06-22 stsp entry = te;
4384 7c32bd05 2019-06-22 stsp if (match_tree_entry(entry, &view->regex)) {
4385 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4386 7c32bd05 2019-06-22 stsp s->matched_entry = entry;
4390 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4391 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(entry, entry);
4393 7c32bd05 2019-06-22 stsp if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4394 7c32bd05 2019-06-22 stsp entry = NULL;
4396 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4397 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) == entry) {
4398 7c32bd05 2019-06-22 stsp entry = te;
4406 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4407 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
4408 7c32bd05 2019-06-22 stsp s->selected = 0;
4411 e5a0f69f 2018-08-18 stsp return NULL;
4414 ad80ab7b 2018-08-04 stsp static const struct got_error *
4415 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
4417 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
4418 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4419 e5a0f69f 2018-08-18 stsp char *parent_path;
4421 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
4423 e5a0f69f 2018-08-18 stsp return err;
4425 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
4426 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
4427 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4428 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
4429 e5a0f69f 2018-08-18 stsp free(parent_path);
4431 669b5ffa 2018-10-07 stsp view_vborder(view);
4432 e5a0f69f 2018-08-18 stsp return err;
4435 e5a0f69f 2018-08-18 stsp static const struct got_error *
4436 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4437 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4439 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4440 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
4441 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
4442 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
4444 e5a0f69f 2018-08-18 stsp switch (ch) {
4446 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
4449 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
4451 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4452 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4453 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
4454 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
4455 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
4456 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4457 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4459 1e37a5c2 2019-05-12 jcs return err;
4460 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
4462 1e37a5c2 2019-05-12 jcs view_close(log_view);
4465 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
4466 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4468 1e37a5c2 2019-05-12 jcs *new_view = log_view;
4471 1e37a5c2 2019-05-12 jcs case KEY_UP:
4472 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
4473 1e37a5c2 2019-05-12 jcs s->selected--;
4474 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
4477 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
4479 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
4480 1e37a5c2 2019-05-12 jcs s->entries, s->tree == s->root);
4482 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4483 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
4484 1e37a5c2 2019-05-12 jcs MAX(0, view->nlines - 4 - s->selected), s->entries,
4485 1e37a5c2 2019-05-12 jcs s->tree == s->root);
4486 1e37a5c2 2019-05-12 jcs s->selected = 0;
4487 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_FIRST(&s->entries->head) ==
4488 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
4489 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
4492 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4493 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
4494 1e37a5c2 2019-05-12 jcs s->selected++;
4497 00ba99a7 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4498 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
4500 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
4501 1e37a5c2 2019-05-12 jcs s->last_displayed_entry, s->entries);
4503 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4504 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4506 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
4507 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
4508 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4511 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
4512 1e37a5c2 2019-05-12 jcs view->nlines, s->last_displayed_entry, s->entries);
4513 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
4514 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
4515 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
4516 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
4518 1e37a5c2 2019-05-12 jcs ndisplayed++;
4519 1e37a5c2 2019-05-12 jcs te = SIMPLEQ_NEXT(te, entry);
4520 1e37a5c2 2019-05-12 jcs } while (te);
4521 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
4524 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4526 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4527 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4528 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
4529 1e37a5c2 2019-05-12 jcs /* user selected '..' */
4530 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
4532 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
4533 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
4535 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
4536 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
4537 1e37a5c2 2019-05-12 jcs s->entries =
4538 1e37a5c2 2019-05-12 jcs got_object_tree_get_entries(s->tree);
4539 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
4540 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
4541 1e37a5c2 2019-05-12 jcs s->selected_entry =
4542 1e37a5c2 2019-05-12 jcs parent->selected_entry;
4543 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
4544 1e37a5c2 2019-05-12 jcs free(parent);
4545 1e37a5c2 2019-05-12 jcs } else if (S_ISDIR(s->selected_entry->mode)) {
4546 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
4547 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&subtree,
4548 1e37a5c2 2019-05-12 jcs s->repo, s->selected_entry->id);
4551 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
4552 941e9f74 2019-05-21 stsp if (err) {
4553 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
4556 1e37a5c2 2019-05-12 jcs } else if (S_ISREG(s->selected_entry->mode)) {
4557 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
4558 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
4559 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
4561 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
4562 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
4563 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
4566 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
4567 669b5ffa 2018-10-07 stsp err = view_close_child(view);
4569 669b5ffa 2018-10-07 stsp return err;
4570 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
4571 669b5ffa 2018-10-07 stsp if (err) {
4572 1e37a5c2 2019-05-12 jcs view_close(blame_view);
4575 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
4576 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
4578 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
4581 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4582 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
4583 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4589 ffd1d5e5 2018-06-23 stsp return err;
4592 ffd1d5e5 2018-06-23 stsp __dead static void
4593 ffd1d5e5 2018-06-23 stsp usage_tree(void)
4596 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4597 ffd1d5e5 2018-06-23 stsp getprogname());
4601 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4602 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
4604 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
4605 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
4606 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4607 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
4608 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4609 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
4610 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
4611 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
4613 5221c383 2018-08-01 stsp struct tog_view *view;
4615 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4617 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
4618 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4619 d188b9a6 2019-01-04 stsp NULL) == -1)
4620 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
4623 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
4624 ffd1d5e5 2018-06-23 stsp switch (ch) {
4626 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
4629 17020d27 2019-03-07 stsp usage_tree();
4630 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
4634 ffd1d5e5 2018-06-23 stsp argc -= optind;
4635 ffd1d5e5 2018-06-23 stsp argv += optind;
4637 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
4638 52185f70 2019-02-05 stsp struct got_worktree *worktree;
4639 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
4640 52185f70 2019-02-05 stsp if (cwd == NULL)
4641 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
4642 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4643 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4644 52185f70 2019-02-05 stsp goto done;
4645 52185f70 2019-02-05 stsp if (worktree) {
4646 52185f70 2019-02-05 stsp free(cwd);
4647 52185f70 2019-02-05 stsp repo_path =
4648 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4649 52185f70 2019-02-05 stsp got_worktree_close(worktree);
4651 52185f70 2019-02-05 stsp repo_path = cwd;
4652 52185f70 2019-02-05 stsp if (repo_path == NULL) {
4653 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4654 52185f70 2019-02-05 stsp goto done;
4656 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
4657 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
4658 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
4659 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
4661 ffd1d5e5 2018-06-23 stsp usage_log();
4663 a915003a 2019-02-05 stsp init_curses();
4665 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4666 c02c541e 2019-03-29 stsp if (error != NULL)
4667 52185f70 2019-02-05 stsp goto done;
4669 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4670 c02c541e 2019-03-29 stsp if (error)
4671 52185f70 2019-02-05 stsp goto done;
4673 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
4674 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4676 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4677 f2b6a97d 2019-07-15 stsp if (error) {
4678 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
4679 f2b6a97d 2019-07-15 stsp goto done;
4680 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&commit_id,
4681 f2b6a97d 2019-07-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4684 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4685 ffd1d5e5 2018-06-23 stsp goto done;
4687 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4688 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4689 ffd1d5e5 2018-06-23 stsp goto done;
4691 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
4692 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
4693 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4694 8b473291 2019-02-21 stsp goto done;
4696 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4697 8b473291 2019-02-21 stsp if (error)
4698 ffd1d5e5 2018-06-23 stsp goto done;
4700 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4701 5221c383 2018-08-01 stsp if (view == NULL) {
4702 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4703 5221c383 2018-08-01 stsp goto done;
4705 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
4706 ad80ab7b 2018-08-04 stsp if (error)
4707 ad80ab7b 2018-08-04 stsp goto done;
4708 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4710 52185f70 2019-02-05 stsp free(repo_path);
4711 ffd1d5e5 2018-06-23 stsp free(commit_id);
4712 ffd1d5e5 2018-06-23 stsp if (commit)
4713 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
4715 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
4717 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
4718 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4719 ffd1d5e5 2018-06-23 stsp return error;
4722 ce5b7c56 2019-07-09 stsp static void
4723 ce5b7c56 2019-07-09 stsp list_commands(void)
4727 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
4728 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
4729 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
4730 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->name);
4732 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
4735 4ed7e80c 2018-05-20 stsp __dead static void
4736 ce5b7c56 2019-07-09 stsp usage(int hflag)
4738 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4739 53ccebc2 2019-07-30 stsp getprogname());
4740 ce5b7c56 2019-07-09 stsp if (hflag)
4741 ce5b7c56 2019-07-09 stsp list_commands();
4745 c2301be8 2018-04-30 stsp static char **
4746 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
4748 c2301be8 2018-04-30 stsp char **argv;
4749 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
4751 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
4752 c2301be8 2018-04-30 stsp if (argv == NULL)
4753 c2301be8 2018-04-30 stsp err(1, "calloc");
4754 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
4755 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
4756 e10c916e 2019-09-15 hiltjo err(1, "strdup");
4757 c2301be8 2018-04-30 stsp if (arg1) {
4758 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
4759 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
4760 e10c916e 2019-09-15 hiltjo err(1, "strdup");
4763 c2301be8 2018-04-30 stsp return argv;
4767 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
4769 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
4770 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
4771 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
4772 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
4774 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
4776 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
4777 9f7d7167 2018-04-29 stsp switch (ch) {
4779 9f7d7167 2018-04-29 stsp hflag = 1;
4782 53ccebc2 2019-07-30 stsp Vflag = 1;
4785 ce5b7c56 2019-07-09 stsp usage(hflag);
4786 9f7d7167 2018-04-29 stsp /* NOTREACHED */
4790 9f7d7167 2018-04-29 stsp argc -= optind;
4791 9f7d7167 2018-04-29 stsp argv += optind;
4792 9f7d7167 2018-04-29 stsp optind = 0;
4793 c2301be8 2018-04-30 stsp optreset = 1;
4795 53ccebc2 2019-07-30 stsp if (Vflag) {
4796 53ccebc2 2019-07-30 stsp got_version_print_str();
4800 c2301be8 2018-04-30 stsp if (argc == 0) {
4801 f29d3e89 2018-06-23 stsp if (hflag)
4802 ce5b7c56 2019-07-09 stsp usage(hflag);
4803 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
4804 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
4805 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
4810 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
4811 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
4812 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
4813 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
4814 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
4819 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
4820 d70c3147 2019-07-09 stsp fprintf(stderr, "%s: unknown command '%s'\n",
4821 3642c4c6 2019-07-09 stsp getprogname(), argv[0]);
4822 ce5b7c56 2019-07-09 stsp list_commands();
4827 3642c4c6 2019-07-09 stsp if (hflag)
4828 3642c4c6 2019-07-09 stsp cmd->cmd_usage();
4830 3642c4c6 2019-07-09 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4833 c2301be8 2018-04-30 stsp free(cmd_argv);
4834 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
4835 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);