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 9f7d7167 2018-04-29 stsp #include "got_error.h"
42 80ddbec8 2018-04-29 stsp #include "got_object.h"
43 80ddbec8 2018-04-29 stsp #include "got_reference.h"
44 80ddbec8 2018-04-29 stsp #include "got_repository.h"
45 80ddbec8 2018-04-29 stsp #include "got_diff.h"
46 511a516b 2018-05-19 stsp #include "got_opentemp.h"
47 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
48 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
49 a70480e0 2018-06-23 stsp #include "got_blame.h"
50 c2db6724 2019-01-04 stsp #include "got_privsep.h"
51 1dd54920 2019-05-11 stsp #include "got_path.h"
52 b7165be3 2019-02-05 stsp #include "got_worktree.h"
55 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
64 9f7d7167 2018-04-29 stsp #ifndef nitems
65 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 9f7d7167 2018-04-29 stsp struct tog_cmd {
69 c2301be8 2018-04-30 stsp const char *name;
70 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
71 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
74 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
75 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
76 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
77 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
78 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
80 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
81 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
82 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
83 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
85 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
86 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
87 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
88 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
89 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
92 d6b05b5b 2018-08-04 stsp enum tog_view_type {
93 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
94 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
95 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
96 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
99 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
101 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
102 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
103 ba4f502b 2018-08-04 stsp struct got_object_id *id;
104 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
107 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
108 ba4f502b 2018-08-04 stsp struct commit_queue {
109 ba4f502b 2018-08-04 stsp int ncommits;
110 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
113 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
114 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
116 15a087fe 2019-02-21 stsp int first_displayed_line;
117 15a087fe 2019-02-21 stsp int last_displayed_line;
119 15a087fe 2019-02-21 stsp int diff_context;
120 15a087fe 2019-02-21 stsp struct got_repository *repo;
121 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
123 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
124 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
127 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
129 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
130 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
131 1a76625f 2018-10-22 stsp int commits_needed;
132 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
133 1a76625f 2018-10-22 stsp struct commit_queue *commits;
134 1a76625f 2018-10-22 stsp const char *in_repo_path;
135 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
136 1a76625f 2018-10-22 stsp struct got_repository *repo;
137 1a76625f 2018-10-22 stsp int log_complete;
138 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
139 1a76625f 2018-10-22 stsp struct tog_view *view;
140 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
141 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
144 1a76625f 2018-10-22 stsp struct tog_log_view_state {
145 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
146 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
147 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
148 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
149 b01e7d3b 2018-08-04 stsp int selected;
150 b01e7d3b 2018-08-04 stsp char *in_repo_path;
151 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
152 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
153 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
154 1a76625f 2018-10-22 stsp sig_atomic_t quit;
155 1a76625f 2018-10-22 stsp pthread_t thread;
156 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
157 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
160 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
161 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
162 e9424729 2018-08-04 stsp int nlines;
164 e9424729 2018-08-04 stsp struct tog_view *view;
165 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
169 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
170 e9424729 2018-08-04 stsp const char *path;
171 e9424729 2018-08-04 stsp struct got_repository *repo;
172 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
173 e9424729 2018-08-04 stsp int *complete;
176 e9424729 2018-08-04 stsp struct tog_blame {
178 e9424729 2018-08-04 stsp size_t filesize;
179 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
180 6fcac457 2018-11-19 stsp int nlines;
181 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
182 e9424729 2018-08-04 stsp pthread_t thread;
183 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
184 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
185 e9424729 2018-08-04 stsp const char *path;
188 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
189 7cbe629d 2018-08-04 stsp int first_displayed_line;
190 7cbe629d 2018-08-04 stsp int last_displayed_line;
191 7cbe629d 2018-08-04 stsp int selected_line;
192 7cbe629d 2018-08-04 stsp int blame_complete;
195 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
196 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
197 e5a0f69f 2018-08-18 stsp char *path;
198 7cbe629d 2018-08-04 stsp struct got_repository *repo;
199 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
200 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
201 e9424729 2018-08-04 stsp struct tog_blame blame;
202 6c4c42e0 2019-06-24 stsp int matched_line;
205 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
206 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
207 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
208 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
209 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
210 ad80ab7b 2018-08-04 stsp int selected;
213 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
215 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
216 ad80ab7b 2018-08-04 stsp char *tree_label;
217 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
218 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
219 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
220 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
221 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
222 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
223 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
224 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
225 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
226 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
227 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
228 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
232 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
234 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
235 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
236 669b5ffa 2018-10-07 stsp * there is enough screen estate.
238 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
239 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
241 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
242 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
243 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
245 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
246 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
248 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
250 cc3c9aac 2018-08-01 stsp struct tog_view {
251 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
252 26ed57b2 2018-05-19 stsp WINDOW *window;
253 26ed57b2 2018-05-19 stsp PANEL *panel;
254 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
255 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
256 1004088d 2018-09-29 stsp int focussed;
257 669b5ffa 2018-10-07 stsp struct tog_view *parent;
258 669b5ffa 2018-10-07 stsp struct tog_view *child;
259 669b5ffa 2018-10-07 stsp int child_focussed;
261 5dc9f4bc 2018-08-04 stsp /* type-specific state */
262 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
264 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
265 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
266 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
267 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
270 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
271 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
272 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
273 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
275 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
276 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
277 60493ae3 2019-06-20 stsp int searching;
278 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
279 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
280 60493ae3 2019-06-20 stsp int search_next_done;
281 1803e47f 2019-06-22 stsp regex_t regex;
284 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
285 fb872ab2 2019-02-21 stsp struct got_object_id *, struct got_object_id *, struct tog_view *,
286 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
287 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
288 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
289 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
290 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
292 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
293 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *,
294 8b473291 2019-02-21 stsp struct got_repository *, const char *, int);
295 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
296 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_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_log_view(struct tog_view *);
299 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
300 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
302 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
303 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *, struct got_repository *);
304 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
305 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
306 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
307 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
308 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
309 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
311 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
312 8b473291 2019-02-21 stsp struct got_tree_object *, struct got_object_id *,
313 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
314 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
315 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
316 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
317 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
318 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
319 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
321 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
323 25791caa 2018-10-24 stsp static void
324 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
326 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
329 e5a0f69f 2018-08-18 stsp static const struct got_error *
330 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
332 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
334 669b5ffa 2018-10-07 stsp if (view->child) {
335 669b5ffa 2018-10-07 stsp view_close(view->child);
336 669b5ffa 2018-10-07 stsp view->child = NULL;
338 e5a0f69f 2018-08-18 stsp if (view->close)
339 e5a0f69f 2018-08-18 stsp err = view->close(view);
340 ea5e7bb5 2018-08-01 stsp if (view->panel)
341 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
342 ea5e7bb5 2018-08-01 stsp if (view->window)
343 ea5e7bb5 2018-08-01 stsp delwin(view->window);
344 ea5e7bb5 2018-08-01 stsp free(view);
345 e5a0f69f 2018-08-18 stsp return err;
348 ea5e7bb5 2018-08-01 stsp static struct tog_view *
349 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
350 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
352 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
354 ea5e7bb5 2018-08-01 stsp if (view == NULL)
355 ea5e7bb5 2018-08-01 stsp return NULL;
357 d6b05b5b 2018-08-04 stsp view->type = type;
358 f7d12f7e 2018-08-01 stsp view->lines = LINES;
359 f7d12f7e 2018-08-01 stsp view->cols = COLS;
360 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
361 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
362 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
363 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
364 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
365 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
366 96a765a8 2018-08-04 stsp view_close(view);
367 ea5e7bb5 2018-08-01 stsp return NULL;
369 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
370 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
371 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
372 96a765a8 2018-08-04 stsp view_close(view);
373 ea5e7bb5 2018-08-01 stsp return NULL;
376 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
377 ea5e7bb5 2018-08-01 stsp return view;
381 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
383 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
385 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
388 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
390 5c60c32a 2018-10-18 stsp static const struct got_error *
391 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
393 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
395 5c60c32a 2018-10-18 stsp view->begin_y = 0;
396 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
397 5c60c32a 2018-10-18 stsp view->nlines = LINES;
398 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
399 5c60c32a 2018-10-18 stsp view->lines = LINES;
400 5c60c32a 2018-10-18 stsp view->cols = COLS;
401 5c60c32a 2018-10-18 stsp err = view_resize(view);
403 5c60c32a 2018-10-18 stsp return err;
405 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
406 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
408 5c60c32a 2018-10-18 stsp return NULL;
411 5c60c32a 2018-10-18 stsp static const struct got_error *
412 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
414 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
416 5c60c32a 2018-10-18 stsp view->begin_x = 0;
417 5c60c32a 2018-10-18 stsp view->begin_y = 0;
418 5c60c32a 2018-10-18 stsp view->nlines = LINES;
419 5c60c32a 2018-10-18 stsp view->ncols = COLS;
420 5c60c32a 2018-10-18 stsp view->lines = LINES;
421 5c60c32a 2018-10-18 stsp view->cols = COLS;
422 5c60c32a 2018-10-18 stsp err = view_resize(view);
424 5c60c32a 2018-10-18 stsp return err;
426 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
427 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
429 5c60c32a 2018-10-18 stsp return NULL;
433 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
435 5c60c32a 2018-10-18 stsp return view->parent == NULL;
438 4d8c2215 2018-08-19 stsp static const struct got_error *
439 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
441 f7d12f7e 2018-08-01 stsp int nlines, ncols;
443 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
444 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
446 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
448 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
449 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
451 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
453 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
454 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
455 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
456 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
457 25791caa 2018-10-24 stsp wclear(view->window);
459 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
460 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
461 0cf4efb1 2018-09-29 stsp view->lines = LINES;
462 0cf4efb1 2018-09-29 stsp view->cols = COLS;
464 6e3e5d9c 2018-10-18 stsp if (view->child) {
465 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
466 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
467 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
468 5c60c32a 2018-10-18 stsp if (view->child->focussed)
469 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
471 5c60c32a 2018-10-18 stsp show_panel(view->panel);
473 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
474 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
478 5c60c32a 2018-10-18 stsp return NULL;
481 669b5ffa 2018-10-07 stsp static const struct got_error *
482 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
484 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
486 669b5ffa 2018-10-07 stsp if (view->child == NULL)
487 669b5ffa 2018-10-07 stsp return NULL;
489 669b5ffa 2018-10-07 stsp err = view_close(view->child);
490 669b5ffa 2018-10-07 stsp view->child = NULL;
491 669b5ffa 2018-10-07 stsp return err;
494 669b5ffa 2018-10-07 stsp static const struct got_error *
495 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
497 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
499 669b5ffa 2018-10-07 stsp view->child = child;
500 669b5ffa 2018-10-07 stsp child->parent = view;
501 669b5ffa 2018-10-07 stsp return err;
505 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
507 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
510 34bc9ec9 2019-02-22 stsp static void
511 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
513 25791caa 2018-10-24 stsp int cols, lines;
514 25791caa 2018-10-24 stsp struct winsize size;
516 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
517 25791caa 2018-10-24 stsp cols = 80; /* Default */
518 25791caa 2018-10-24 stsp lines = 24;
520 25791caa 2018-10-24 stsp cols = size.ws_col;
521 25791caa 2018-10-24 stsp lines = size.ws_row;
523 25791caa 2018-10-24 stsp resize_term(lines, cols);
526 2b49a8ae 2019-06-22 stsp static const struct got_error *
527 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
529 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
530 2b49a8ae 2019-06-22 stsp char pattern[1024];
533 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
534 2b49a8ae 2019-06-22 stsp return NULL;
536 2b49a8ae 2019-06-22 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1,
537 2b49a8ae 2019-06-22 stsp view->begin_x, "/");
538 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
540 2b49a8ae 2019-06-22 stsp nocbreak();
542 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
545 2b49a8ae 2019-06-22 stsp if (ret == ERR)
546 2b49a8ae 2019-06-22 stsp return NULL;
548 2b49a8ae 2019-06-22 stsp if (view->searching) {
549 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
550 2b49a8ae 2019-06-22 stsp view->searching = 0;
553 2b49a8ae 2019-06-22 stsp if (regcomp(&view->regex, pattern,
554 f5daf9b1 2019-06-24 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
555 7c32bd05 2019-06-22 stsp err = view->search_start(view);
557 7c32bd05 2019-06-22 stsp regfree(&view->regex);
558 7c32bd05 2019-06-22 stsp return err;
560 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
561 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
562 2b49a8ae 2019-06-22 stsp view->search_next(view);
565 2b49a8ae 2019-06-22 stsp return NULL;
568 0cf4efb1 2018-09-29 stsp static const struct got_error *
569 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
570 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
571 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
573 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
574 669b5ffa 2018-10-07 stsp struct tog_view *v;
575 1a76625f 2018-10-22 stsp int ch, errcode;
577 e5a0f69f 2018-08-18 stsp *new = NULL;
578 e5a0f69f 2018-08-18 stsp *dead = NULL;
579 0cf4efb1 2018-09-29 stsp *focus = NULL;
581 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
582 60493ae3 2019-06-20 stsp errcode = pthread_mutex_unlock(&tog_mutex);
583 60493ae3 2019-06-20 stsp if (errcode)
584 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
585 60493ae3 2019-06-20 stsp "pthread_mutex_unlock");
586 60493ae3 2019-06-20 stsp pthread_yield();
587 60493ae3 2019-06-20 stsp errcode = pthread_mutex_lock(&tog_mutex);
588 60493ae3 2019-06-20 stsp if (errcode)
589 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
590 60493ae3 2019-06-20 stsp "pthread_mutex_lock");
591 60493ae3 2019-06-20 stsp view->search_next(view);
592 60493ae3 2019-06-20 stsp return NULL;
595 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
596 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
597 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
598 1a76625f 2018-10-22 stsp if (errcode)
599 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
600 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
601 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
602 1a76625f 2018-10-22 stsp if (errcode)
603 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
604 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
606 25791caa 2018-10-24 stsp if (tog_sigwinch_received) {
607 25791caa 2018-10-24 stsp tog_resizeterm();
608 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
609 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
610 25791caa 2018-10-24 stsp err = view_resize(v);
612 25791caa 2018-10-24 stsp return err;
613 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
615 25791caa 2018-10-24 stsp return err;
619 e5a0f69f 2018-08-18 stsp switch (ch) {
623 1e37a5c2 2019-05-12 jcs if (view->child) {
624 1e37a5c2 2019-05-12 jcs *focus = view->child;
625 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
626 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
627 1e37a5c2 2019-05-12 jcs *focus = view->parent;
628 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
632 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
633 1e37a5c2 2019-05-12 jcs *dead = view;
639 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
640 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
642 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
643 669b5ffa 2018-10-07 stsp *focus = view->child;
644 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
645 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
647 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
650 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
651 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
653 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
654 1e37a5c2 2019-05-12 jcs *focus = view;
655 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
656 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
658 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
662 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
663 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
666 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
669 60493ae3 2019-06-20 stsp if (view->search_start)
670 2b49a8ae 2019-06-22 stsp view_search_start(view);
672 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
676 60493ae3 2019-06-20 stsp if (view->search_next && view->searching) {
677 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
678 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
679 60493ae3 2019-06-20 stsp view->search_next_done = 0;
680 60493ae3 2019-06-20 stsp view->search_next(view);
682 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
685 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
689 e5a0f69f 2018-08-18 stsp return err;
693 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
695 0cf4efb1 2018-09-29 stsp PANEL *panel;
696 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
698 669b5ffa 2018-10-07 stsp if (view->parent)
699 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
701 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
702 0cf4efb1 2018-09-29 stsp if (panel == NULL)
705 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
706 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
707 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
711 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
713 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
714 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
716 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
718 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
721 669b5ffa 2018-10-07 stsp return view->focussed;
724 bcbd79e2 2018-08-19 stsp static const struct got_error *
725 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
727 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
728 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
729 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
730 fd823528 2018-10-22 stsp int fast_refresh = 10;
731 1a76625f 2018-10-22 stsp int done = 0, errcode;
733 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
734 1a76625f 2018-10-22 stsp if (errcode)
735 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
737 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
738 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
740 a81bf10d 2018-09-29 stsp main_view = view;
741 1004088d 2018-09-29 stsp view->focussed = 1;
742 878940b7 2018-09-29 stsp err = view->show(view);
744 0cf4efb1 2018-09-29 stsp return err;
745 0cf4efb1 2018-09-29 stsp update_panels();
746 0cf4efb1 2018-09-29 stsp doupdate();
747 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
748 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
749 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
750 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
752 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
753 e4197bf9 2018-08-18 stsp view, &views);
756 e5a0f69f 2018-08-18 stsp if (dead_view) {
757 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
759 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
760 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
761 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
762 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
763 669b5ffa 2018-10-07 stsp prev = view->parent;
765 669b5ffa 2018-10-07 stsp if (dead_view->parent)
766 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
768 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
770 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
771 a81bf10d 2018-09-29 stsp if (err || dead_view == main_view)
774 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
775 0cf4efb1 2018-09-29 stsp if (focus_view)
776 0cf4efb1 2018-09-29 stsp view = focus_view;
777 669b5ffa 2018-10-07 stsp else if (prev)
778 669b5ffa 2018-10-07 stsp view = prev;
779 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
780 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
781 0cf4efb1 2018-09-29 stsp tog_view_list_head);
783 0cf4efb1 2018-09-29 stsp view = NULL;
784 669b5ffa 2018-10-07 stsp if (view) {
785 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
786 669b5ffa 2018-10-07 stsp focus_view = view->child;
788 669b5ffa 2018-10-07 stsp focus_view = view;
792 bcbd79e2 2018-08-19 stsp if (new_view) {
793 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
794 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
795 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
796 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
798 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
799 86c66b02 2018-10-18 stsp err = view_close(v);
804 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
805 fed7eaa8 2018-10-24 stsp view = new_view;
806 878940b7 2018-09-29 stsp if (focus_view == NULL)
807 878940b7 2018-09-29 stsp focus_view = new_view;
809 0cf4efb1 2018-09-29 stsp if (focus_view) {
810 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
812 0cf4efb1 2018-09-29 stsp view->focussed = 0;
813 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
814 0cf4efb1 2018-09-29 stsp view = focus_view;
815 878940b7 2018-09-29 stsp if (new_view)
816 878940b7 2018-09-29 stsp show_panel(new_view->panel);
817 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
818 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
820 669b5ffa 2018-10-07 stsp if (view) {
821 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
822 758194b5 2018-10-24 stsp view->focussed = 1;
823 758194b5 2018-10-24 stsp show_panel(view->panel);
824 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
825 758194b5 2018-10-24 stsp show_panel(view->child->panel);
826 1a76625f 2018-10-22 stsp focus_view = view;
828 669b5ffa 2018-10-07 stsp if (view->parent) {
829 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
833 669b5ffa 2018-10-07 stsp err = view->show(view);
836 669b5ffa 2018-10-07 stsp if (view->child) {
837 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
841 1a76625f 2018-10-22 stsp update_panels();
842 1a76625f 2018-10-22 stsp doupdate();
846 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
847 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
848 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
849 e5a0f69f 2018-08-18 stsp view_close(view);
852 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
853 1a76625f 2018-10-22 stsp if (errcode)
854 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
856 e5a0f69f 2018-08-18 stsp return err;
859 4ed7e80c 2018-05-20 stsp __dead static void
860 9f7d7167 2018-04-29 stsp usage_log(void)
863 c70c5802 2018-08-01 stsp fprintf(stderr,
864 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
865 9f7d7167 2018-04-29 stsp getprogname());
869 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
870 80ddbec8 2018-04-29 stsp static const struct got_error *
871 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
873 00dfcb92 2018-06-11 stsp char *vis = NULL;
874 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
876 963b370f 2018-05-20 stsp *ws = NULL;
877 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
878 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
879 00dfcb92 2018-06-11 stsp int vislen;
880 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
881 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
883 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
884 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
886 00dfcb92 2018-06-11 stsp return err;
887 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
888 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
889 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
894 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
895 a7f50699 2018-06-11 stsp if (*ws == NULL) {
896 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
900 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
901 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
906 963b370f 2018-05-20 stsp *ws = NULL;
909 963b370f 2018-05-20 stsp return err;
912 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
913 963b370f 2018-05-20 stsp static const struct got_error *
914 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
916 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
917 963b370f 2018-05-20 stsp int cols = 0;
918 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
919 963b370f 2018-05-20 stsp size_t wlen;
922 963b370f 2018-05-20 stsp *wlinep = NULL;
923 b700b5d6 2018-07-10 stsp *widthp = 0;
925 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
927 963b370f 2018-05-20 stsp return err;
930 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
931 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
932 963b370f 2018-05-20 stsp switch (width) {
938 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
939 b700b5d6 2018-07-10 stsp cols += width;
943 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
944 67ceb59d 2019-03-03 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
948 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
952 963b370f 2018-05-20 stsp wline[i] = L'\0';
953 b700b5d6 2018-07-10 stsp if (widthp)
954 b700b5d6 2018-07-10 stsp *widthp = cols;
957 963b370f 2018-05-20 stsp free(wline);
959 963b370f 2018-05-20 stsp *wlinep = wline;
960 963b370f 2018-05-20 stsp return err;
963 8b473291 2019-02-21 stsp static const struct got_error*
964 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
965 8b473291 2019-02-21 stsp struct got_object_id *id)
967 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
968 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
970 8b473291 2019-02-21 stsp const char *name;
972 8b473291 2019-02-21 stsp *refs_str = NULL;
974 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
975 8b473291 2019-02-21 stsp if (got_object_id_cmp(re->id, id) != 0)
977 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
978 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
980 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
982 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
984 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
986 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
988 8b473291 2019-02-21 stsp s = *refs_str;
989 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
990 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
991 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
993 8b473291 2019-02-21 stsp *refs_str = NULL;
999 8b473291 2019-02-21 stsp return err;
1002 963b370f 2018-05-20 stsp static const struct got_error *
1003 5813d178 2019-03-09 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1005 5813d178 2019-03-09 stsp char *smallerthan, *at;
1007 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1008 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1009 5813d178 2019-03-09 stsp author = smallerthan + 1;
1010 5813d178 2019-03-09 stsp at = strchr(author, '@');
1012 5813d178 2019-03-09 stsp *at = '\0';
1013 5813d178 2019-03-09 stsp return format_line(wauthor, author_width, author, limit);
1016 5813d178 2019-03-09 stsp static const struct got_error *
1017 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1018 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
1019 5813d178 2019-03-09 stsp int author_display_cols)
1021 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1022 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1023 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1024 5813d178 2019-03-09 stsp char *author = NULL;
1025 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1026 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1027 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1028 bb737323 2018-05-20 stsp int col, limit;
1029 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
1030 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1031 ccb26ccd 2018-11-05 stsp struct tm tm;
1032 45d799e2 2018-12-23 stsp time_t committer_time;
1034 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1035 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1036 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1037 ccb26ccd 2018-11-05 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1038 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1039 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1041 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
1042 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1044 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1045 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1046 b39d25c7 2018-07-10 stsp col = limit + 1;
1047 b39d25c7 2018-07-10 stsp if (col > avail)
1048 b39d25c7 2018-07-10 stsp goto done;
1050 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1051 5813d178 2019-03-09 stsp if (author == NULL) {
1052 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1053 80ddbec8 2018-04-29 stsp goto done;
1055 5813d178 2019-03-09 stsp err = format_author(&wauthor, &author_width, author, avail - col);
1057 bb737323 2018-05-20 stsp goto done;
1058 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1059 bb737323 2018-05-20 stsp col += author_width;
1060 5813d178 2019-03-09 stsp while (col <= avail && author_width < author_display_cols + 2) {
1061 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1063 bb737323 2018-05-20 stsp author_width++;
1065 9c2eaf34 2018-05-20 stsp if (col > avail)
1066 9c2eaf34 2018-05-20 stsp goto done;
1068 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1069 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
1070 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1071 6d9fbc00 2018-04-29 stsp goto done;
1073 bb737323 2018-05-20 stsp logmsg = logmsg0;
1074 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1076 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1077 bb737323 2018-05-20 stsp if (newline)
1078 bb737323 2018-05-20 stsp *newline = '\0';
1079 bb737323 2018-05-20 stsp limit = avail - col;
1080 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1082 bb737323 2018-05-20 stsp goto done;
1083 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1084 bb737323 2018-05-20 stsp col += logmsg_width;
1085 bb737323 2018-05-20 stsp while (col <= avail) {
1086 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1090 80ddbec8 2018-04-29 stsp free(logmsg0);
1091 bb737323 2018-05-20 stsp free(wlogmsg);
1092 5813d178 2019-03-09 stsp free(author);
1093 bb737323 2018-05-20 stsp free(wauthor);
1094 80ddbec8 2018-04-29 stsp free(line);
1095 80ddbec8 2018-04-29 stsp return err;
1098 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1099 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1100 899d86c2 2018-05-10 stsp struct got_object_id *id)
1102 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1104 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1105 80ddbec8 2018-04-29 stsp if (entry == NULL)
1106 899d86c2 2018-05-10 stsp return NULL;
1108 899d86c2 2018-05-10 stsp entry->id = id;
1109 99db9666 2018-05-07 stsp entry->commit = commit;
1110 899d86c2 2018-05-10 stsp return entry;
1113 99db9666 2018-05-07 stsp static void
1114 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1116 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1118 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1119 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1120 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1121 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1122 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1123 99db9666 2018-05-07 stsp free(entry);
1126 99db9666 2018-05-07 stsp static void
1127 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1129 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1130 99db9666 2018-05-07 stsp pop_commit(commits);
1133 c4972b91 2018-05-07 stsp static const struct got_error *
1134 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1135 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
1137 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1138 93e45b7c 2018-09-24 stsp int nqueued = 0;
1141 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1142 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1143 1a76625f 2018-10-22 stsp * while updating the display.
1145 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
1146 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1147 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1148 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1149 1a76625f 2018-10-22 stsp int errcode;
1151 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1152 9ba79e04 2018-06-11 stsp if (err) {
1153 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1155 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
1156 93e45b7c 2018-09-24 stsp minqueue, repo);
1158 ecb28ae0 2018-07-16 stsp return err;
1162 ecb28ae0 2018-07-16 stsp if (id == NULL)
1165 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1168 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1169 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1170 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1174 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1175 1a76625f 2018-10-22 stsp if (errcode) {
1176 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_lock");
1180 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1181 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1182 ecb28ae0 2018-07-16 stsp nqueued++;
1183 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1185 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1186 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1187 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1188 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1191 9ba79e04 2018-06-11 stsp return err;
1194 99db9666 2018-05-07 stsp static const struct got_error *
1195 19e70ad6 2019-05-14 stsp get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1196 19e70ad6 2019-05-14 stsp struct got_repository *repo)
1198 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1199 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1201 9ba79e04 2018-06-11 stsp *head_id = NULL;
1203 19e70ad6 2019-05-14 stsp err = got_ref_open(&head_ref, repo, branch_name, 0);
1205 99db9666 2018-05-07 stsp return err;
1207 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1208 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1209 9ba79e04 2018-06-11 stsp if (err) {
1210 9ba79e04 2018-06-11 stsp *head_id = NULL;
1211 99db9666 2018-05-07 stsp return err;
1214 9ba79e04 2018-06-11 stsp return NULL;
1217 0553a4e3 2018-04-30 stsp static const struct got_error *
1218 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1219 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1220 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1221 8b473291 2019-02-21 stsp struct got_reflist_head *refs, const char *path, int commits_needed)
1223 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1224 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1225 60493ae3 2019-06-20 stsp int width;
1226 60493ae3 2019-06-20 stsp int ncommits, author_cols = 10;
1227 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1228 8b473291 2019-02-21 stsp char *refs_str = NULL;
1229 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1231 e0d42f60 2018-07-22 stsp entry = first;
1232 e0d42f60 2018-07-22 stsp ncommits = 0;
1233 e0d42f60 2018-07-22 stsp while (entry) {
1234 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1235 e0d42f60 2018-07-22 stsp *selected = entry;
1238 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1239 e0d42f60 2018-07-22 stsp ncommits++;
1242 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1243 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1245 ecb28ae0 2018-07-16 stsp return err;
1246 8b473291 2019-02-21 stsp if (refs) {
1247 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, (*selected)->id);
1249 8b473291 2019-02-21 stsp goto done;
1253 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1254 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1256 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1257 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1258 60493ae3 2019-06-20 stsp commits_needed > 0 ?
1259 60493ae3 2019-06-20 stsp (view->searching && view->search_next_done == 0
1260 60493ae3 2019-06-20 stsp ? "searching..." : "loading... ") :
1261 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1262 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1263 8b473291 2019-02-21 stsp goto done;
1266 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1267 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1268 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1269 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1270 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1271 1a76625f 2018-10-22 stsp header = NULL;
1272 1a76625f 2018-10-22 stsp goto done;
1274 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1275 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1276 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1277 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1278 1a76625f 2018-10-22 stsp header = NULL;
1279 1a76625f 2018-10-22 stsp goto done;
1281 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1283 1a76625f 2018-10-22 stsp goto done;
1285 2814baeb 2018-08-01 stsp werase(view->window);
1287 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1288 a3404814 2018-09-02 stsp wstandout(view->window);
1289 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1290 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1291 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1294 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1295 a3404814 2018-09-02 stsp wstandend(view->window);
1296 ecb28ae0 2018-07-16 stsp free(wline);
1297 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1298 1a76625f 2018-10-22 stsp goto done;
1300 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1301 899d86c2 2018-05-10 stsp entry = first;
1302 5813d178 2019-03-09 stsp ncommits = 0;
1303 5813d178 2019-03-09 stsp while (entry) {
1304 5813d178 2019-03-09 stsp char *author;
1305 5813d178 2019-03-09 stsp wchar_t *wauthor;
1306 5813d178 2019-03-09 stsp int width;
1307 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1309 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1310 5813d178 2019-03-09 stsp if (author == NULL) {
1311 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1312 5813d178 2019-03-09 stsp goto done;
1314 5813d178 2019-03-09 stsp err = format_author(&wauthor, &width, author, COLS);
1315 5813d178 2019-03-09 stsp if (author_cols < width)
1316 5813d178 2019-03-09 stsp author_cols = width;
1317 5813d178 2019-03-09 stsp free(wauthor);
1318 5813d178 2019-03-09 stsp free(author);
1319 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1322 5813d178 2019-03-09 stsp entry = first;
1323 899d86c2 2018-05-10 stsp *last = first;
1324 867c6645 2018-07-10 stsp ncommits = 0;
1325 899d86c2 2018-05-10 stsp while (entry) {
1326 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1328 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1329 2814baeb 2018-08-01 stsp wstandout(view->window);
1330 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1331 5813d178 2019-03-09 stsp author_cols);
1332 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1333 2814baeb 2018-08-01 stsp wstandend(view->window);
1335 60493ae3 2019-06-20 stsp goto done;
1336 0553a4e3 2018-04-30 stsp ncommits++;
1337 899d86c2 2018-05-10 stsp *last = entry;
1338 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1341 1a57306a 2018-09-02 stsp view_vborder(view);
1343 1a76625f 2018-10-22 stsp free(id_str);
1344 8b473291 2019-02-21 stsp free(refs_str);
1345 1a76625f 2018-10-22 stsp free(ncommits_str);
1346 1a76625f 2018-10-22 stsp free(header);
1347 80ddbec8 2018-04-29 stsp return err;
1350 07b55e75 2018-05-10 stsp static void
1351 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1352 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1353 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1355 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1356 07b55e75 2018-05-10 stsp int nscrolled = 0;
1358 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1359 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1362 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1363 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1364 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1365 07b55e75 2018-05-10 stsp if (entry) {
1366 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1367 07b55e75 2018-05-10 stsp nscrolled++;
1372 aa075928 2018-05-10 stsp static const struct got_error *
1373 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1374 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1376 5e224a3e 2019-02-22 stsp int errcode;
1377 8a42fca8 2019-02-22 stsp int max_wait = 20;
1379 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1381 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1382 5e224a3e 2019-02-22 stsp if (*log_complete)
1385 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1386 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1387 7aafa0d1 2019-02-22 stsp if (errcode)
1388 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1389 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1390 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1391 7aafa0d1 2019-02-22 stsp if (errcode)
1392 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1393 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1394 7aafa0d1 2019-02-22 stsp pthread_yield();
1395 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1396 7aafa0d1 2019-02-22 stsp if (errcode)
1397 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1398 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1400 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1402 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1403 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1404 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1405 5e224a3e 2019-02-22 stsp * with few commits in history.
1407 7aafa0d1 2019-02-22 stsp return NULL;
1411 5e224a3e 2019-02-22 stsp return NULL;
1414 5e224a3e 2019-02-22 stsp static const struct got_error *
1415 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1416 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1417 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1418 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1419 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1421 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1422 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1423 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1425 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1426 5e224a3e 2019-02-22 stsp return NULL;
1428 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1429 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1431 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1432 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1434 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1435 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1436 5e224a3e 2019-02-22 stsp need_commits);
1438 5e224a3e 2019-02-22 stsp return err;
1442 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1443 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1446 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1448 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1449 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1451 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1452 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1454 dd0a52c1 2018-05-20 stsp return err;
1457 cd0acaa7 2018-05-20 stsp static const struct got_error *
1458 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1459 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1460 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1461 8b473291 2019-02-21 stsp struct got_repository *repo)
1463 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1464 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1465 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1467 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1468 15a94983 2018-12-23 stsp if (diff_view == NULL)
1469 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1471 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1472 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1473 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1474 e5a0f69f 2018-08-18 stsp if (err == NULL)
1475 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1476 cd0acaa7 2018-05-20 stsp return err;
1479 80ddbec8 2018-04-29 stsp static const struct got_error *
1480 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1481 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1483 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1485 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1486 941e9f74 2019-05-21 stsp if (parent == NULL)
1487 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1489 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1490 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1491 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1492 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1493 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1494 941e9f74 2019-05-21 stsp s->tree = subtree;
1495 941e9f74 2019-05-21 stsp s->entries = got_object_tree_get_entries(s->tree);
1496 941e9f74 2019-05-21 stsp s->selected = 0;
1497 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1498 941e9f74 2019-05-21 stsp return NULL;
1502 941e9f74 2019-05-21 stsp static const struct got_error *
1503 941e9f74 2019-05-21 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1504 941e9f74 2019-05-21 stsp struct commit_queue_entry *entry, const char *path,
1505 941e9f74 2019-05-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
1507 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1508 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1509 941e9f74 2019-05-21 stsp struct got_tree_entry *te;
1510 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s;
1511 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1512 b03c880f 2019-05-21 stsp char *slash, *subpath = NULL;
1513 941e9f74 2019-05-21 stsp const char *p;
1515 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1516 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1518 9343a5fb 2018-06-23 stsp return err;
1520 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1521 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1522 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1524 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1525 941e9f74 2019-05-21 stsp if (err) {
1526 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1527 941e9f74 2019-05-21 stsp return err;
1529 941e9f74 2019-05-21 stsp s = &tree_view->state.tree;
1531 941e9f74 2019-05-21 stsp *new_view = tree_view;
1533 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1535 941e9f74 2019-05-21 stsp while (*p) {
1536 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1538 941e9f74 2019-05-21 stsp while (p[0] == '/')
1541 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1542 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1543 941e9f74 2019-05-21 stsp if (slash == NULL)
1544 941e9f74 2019-05-21 stsp slash = strchr(p, '\0');
1545 941e9f74 2019-05-21 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1546 941e9f74 2019-05-21 stsp if (strncmp(p, te->name, slash - p) == 0) {
1547 941e9f74 2019-05-21 stsp s->selected_entry = te;
1550 b03c880f 2019-05-21 stsp s->selected++;
1552 941e9f74 2019-05-21 stsp if (s->selected_entry == NULL) {
1553 941e9f74 2019-05-21 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1556 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1557 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1559 67409a31 2019-05-24 stsp if (!S_ISDIR(s->selected_entry->mode)) {
1560 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1561 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1562 67409a31 2019-05-24 stsp s->selected = 0;
1566 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1567 941e9f74 2019-05-21 stsp if (slash)
1568 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1570 941e9f74 2019-05-21 stsp subpath = strdup(path);
1571 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1572 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1576 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1581 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1582 941e9f74 2019-05-21 stsp free(tree_id);
1586 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1587 941e9f74 2019-05-21 stsp if (err) {
1588 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1591 941e9f74 2019-05-21 stsp if (slash == NULL)
1593 941e9f74 2019-05-21 stsp free(subpath);
1594 941e9f74 2019-05-21 stsp subpath = NULL;
1595 941e9f74 2019-05-21 stsp p = slash;
1598 941e9f74 2019-05-21 stsp free(subpath);
1599 1a76625f 2018-10-22 stsp return err;
1602 1a76625f 2018-10-22 stsp static void *
1603 1a76625f 2018-10-22 stsp log_thread(void *arg)
1605 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1606 1a76625f 2018-10-22 stsp int errcode = 0;
1607 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1608 1a76625f 2018-10-22 stsp int done = 0;
1610 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1612 1a76625f 2018-10-22 stsp return (void *)err;
1614 1a76625f 2018-10-22 stsp while (!done && !err) {
1615 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1616 1a76625f 2018-10-22 stsp a->in_repo_path);
1617 1a76625f 2018-10-22 stsp if (err) {
1618 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1619 1a76625f 2018-10-22 stsp return (void *)err;
1620 1a76625f 2018-10-22 stsp err = NULL;
1622 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1623 1a76625f 2018-10-22 stsp a->commits_needed--;
1625 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1626 3abe8080 2019-04-10 stsp if (errcode) {
1627 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1628 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1630 3abe8080 2019-04-10 stsp } else if (*a->quit)
1632 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1633 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1634 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1635 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1639 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1640 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1641 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1642 1a76625f 2018-10-22 stsp &tog_mutex);
1643 1a76625f 2018-10-22 stsp if (errcode)
1644 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1645 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1648 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1649 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1650 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1651 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1653 3abe8080 2019-04-10 stsp a->log_complete = 1;
1654 1a76625f 2018-10-22 stsp return (void *)err;
1657 1a76625f 2018-10-22 stsp static const struct got_error *
1658 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1660 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1661 1a76625f 2018-10-22 stsp int errcode;
1663 1a76625f 2018-10-22 stsp if (s->thread) {
1664 1a76625f 2018-10-22 stsp s->quit = 1;
1665 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1666 1a76625f 2018-10-22 stsp if (errcode)
1667 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1668 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1669 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1670 1a76625f 2018-10-22 stsp if (errcode)
1671 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1672 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1673 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1674 1a76625f 2018-10-22 stsp if (errcode)
1675 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1676 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1677 1a76625f 2018-10-22 stsp if (errcode)
1678 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1679 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1680 1a76625f 2018-10-22 stsp s->thread = NULL;
1683 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1684 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1685 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1687 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1688 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1689 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1692 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1693 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1694 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1697 9343a5fb 2018-06-23 stsp return err;
1700 9343a5fb 2018-06-23 stsp static const struct got_error *
1701 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1703 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1704 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1706 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1707 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1708 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1709 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1710 1a76625f 2018-10-22 stsp free(s->start_id);
1711 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1712 1a76625f 2018-10-22 stsp return err;
1715 1a76625f 2018-10-22 stsp static const struct got_error *
1716 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
1718 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1720 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
1721 60493ae3 2019-06-20 stsp return NULL;
1724 60493ae3 2019-06-20 stsp static int
1725 60493ae3 2019-06-20 stsp match_commit(struct got_commit_object *commit, regex_t *regex)
1727 60493ae3 2019-06-20 stsp regmatch_t regmatch;
1729 60493ae3 2019-06-20 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1730 60493ae3 2019-06-20 stsp ®match, 0) == 0 ||
1731 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1732 60493ae3 2019-06-20 stsp ®match, 0) == 0 ||
1733 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_logmsg(commit), 1,
1734 60493ae3 2019-06-20 stsp ®match, 0) == 0)
1740 60493ae3 2019-06-20 stsp static const struct got_error *
1741 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
1743 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
1744 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1745 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
1747 60493ae3 2019-06-20 stsp if (!view->searching) {
1748 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1749 60493ae3 2019-06-20 stsp return NULL;
1752 b1bf1435 2019-06-21 stsp if (s->matched_entry) {
1753 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1754 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
1756 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
1757 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
1759 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1760 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
1762 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1765 60493ae3 2019-06-20 stsp while (1) {
1766 60493ae3 2019-06-20 stsp if (entry == NULL) {
1767 60493ae3 2019-06-20 stsp if (s->thread_args.log_complete) {
1768 60493ae3 2019-06-20 stsp if (s->matched_entry == NULL) {
1769 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1770 60493ae3 2019-06-20 stsp return NULL;
1772 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1773 60493ae3 2019-06-20 stsp entry = TAILQ_FIRST(&s->commits.head);
1775 b1bf1435 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head,
1776 b1bf1435 2019-06-21 stsp commit_queue_head);
1778 60493ae3 2019-06-20 stsp s->thread_args.commits_needed = 1;
1779 60493ae3 2019-06-20 stsp return trigger_log_thread(0,
1780 60493ae3 2019-06-20 stsp &s->thread_args.commits_needed,
1781 60493ae3 2019-06-20 stsp &s->thread_args.log_complete,
1782 60493ae3 2019-06-20 stsp &s->thread_args.need_commits);
1786 1803e47f 2019-06-22 stsp if (match_commit(entry->commit, &view->regex)) {
1787 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1788 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
1791 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1792 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
1794 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1797 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
1798 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
1799 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
1800 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1802 60493ae3 2019-06-20 stsp return err;
1805 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
1806 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1808 60493ae3 2019-06-20 stsp return err;
1813 60493ae3 2019-06-20 stsp return NULL;
1816 60493ae3 2019-06-20 stsp static const struct got_error *
1817 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1818 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
1819 8b473291 2019-02-21 stsp const char *path, int check_disk)
1821 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1822 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1823 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1824 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1825 1a76625f 2018-10-22 stsp int errcode;
1827 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1828 ecb28ae0 2018-07-16 stsp if (err != NULL)
1829 ecb28ae0 2018-07-16 stsp goto done;
1831 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1832 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1833 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1835 8b473291 2019-02-21 stsp s->refs = refs;
1836 fb2756b9 2018-08-04 stsp s->repo = repo;
1837 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1838 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1839 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1840 5036bf37 2018-09-24 stsp goto done;
1843 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1844 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1845 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1846 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
1847 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
1849 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1851 1a76625f 2018-10-22 stsp goto done;
1852 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1853 1a76625f 2018-10-22 stsp 0, thread_repo);
1855 1a76625f 2018-10-22 stsp goto done;
1857 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1858 1a76625f 2018-10-22 stsp if (errcode) {
1859 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
1860 1a76625f 2018-10-22 stsp goto done;
1863 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1864 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1865 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1866 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1867 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1868 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1869 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1870 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1871 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1872 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1873 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1876 1a76625f 2018-10-22 stsp close_log_view(view);
1877 ba4f502b 2018-08-04 stsp return err;
1880 e5a0f69f 2018-08-18 stsp static const struct got_error *
1881 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1883 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1885 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1886 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1887 2b380cc8 2018-10-24 stsp &s->thread_args);
1888 2b380cc8 2018-10-24 stsp if (errcode)
1889 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
1892 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1893 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1894 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
1895 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1898 e5a0f69f 2018-08-18 stsp static const struct got_error *
1899 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1900 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1902 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1903 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1904 5036bf37 2018-09-24 stsp char *parent_path;
1905 669b5ffa 2018-10-07 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
1906 669b5ffa 2018-10-07 stsp int begin_x = 0;
1908 e5a0f69f 2018-08-18 stsp switch (ch) {
1910 1e37a5c2 2019-05-12 jcs s->quit = 1;
1913 1e37a5c2 2019-05-12 jcs case KEY_UP:
1916 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1918 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
1919 1e37a5c2 2019-05-12 jcs s->selected--;
1921 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
1922 1144d21a 2019-06-21 stsp &s->commits);
1924 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
1925 a4292ac5 2019-05-12 jcs case CTRL('b'):
1926 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1928 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
1929 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
1930 1e37a5c2 2019-05-12 jcs s->selected = 0;
1933 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
1934 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
1937 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
1940 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1942 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
1943 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1944 1e37a5c2 2019-05-12 jcs s->selected++;
1947 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
1948 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
1949 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
1950 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1951 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1953 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
1954 a4292ac5 2019-05-12 jcs case CTRL('f'): {
1955 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
1956 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
1957 1e37a5c2 2019-05-12 jcs if (first == NULL)
1959 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
1960 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
1961 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
1962 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1963 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1964 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
1965 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
1966 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1967 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
1968 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
1969 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
1971 1e37a5c2 2019-05-12 jcs err = NULL;
1974 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1975 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
1976 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
1977 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
1978 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
1980 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
1983 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
1985 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
1986 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
1987 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
1988 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
1989 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
1992 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1993 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
1995 1e37a5c2 2019-05-12 jcs return err;
1996 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
1998 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2001 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2002 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2004 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2007 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2009 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2010 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2011 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2012 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2015 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2016 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2018 1e37a5c2 2019-05-12 jcs return err;
2019 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2021 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2024 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2025 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2027 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2029 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2030 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2032 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2033 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2034 1e37a5c2 2019-05-12 jcs struct tog_view *lv;
2035 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2037 1e37a5c2 2019-05-12 jcs return err;
2038 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2039 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2040 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2041 638f9024 2019-05-13 stsp return got_error_from_errno(
2042 1e37a5c2 2019-05-12 jcs "view_open");
2043 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2044 1e37a5c2 2019-05-12 jcs s->repo, parent_path, 0);
2046 1e37a5c2 2019-05-12 jcs return err;;
2047 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2048 1e37a5c2 2019-05-12 jcs *new_view = lv;
2050 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2051 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2053 1e37a5c2 2019-05-12 jcs return NULL;
2060 80ddbec8 2018-04-29 stsp return err;
2063 4ed7e80c 2018-05-20 stsp static const struct got_error *
2064 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2066 c2db6724 2019-01-04 stsp const struct got_error *error;
2068 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2069 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2071 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2072 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2074 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2075 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2077 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2078 c2db6724 2019-01-04 stsp if (error != NULL)
2079 c2db6724 2019-01-04 stsp return error;
2081 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2082 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2084 c2db6724 2019-01-04 stsp return NULL;
2087 a915003a 2019-02-05 stsp static void
2088 a915003a 2019-02-05 stsp init_curses(void)
2090 a915003a 2019-02-05 stsp initscr();
2092 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2095 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2096 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2097 a915003a 2019-02-05 stsp curs_set(0);
2098 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2101 c2db6724 2019-01-04 stsp static const struct got_error *
2102 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2104 80ddbec8 2018-04-29 stsp const struct got_error *error;
2105 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2106 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2107 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2108 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2109 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2110 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
2112 04cc582a 2018-08-01 stsp struct tog_view *view;
2114 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2116 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2117 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2118 c2db6724 2019-01-04 stsp NULL) == -1)
2119 80ddbec8 2018-04-29 stsp err(1, "pledge");
2122 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2123 80ddbec8 2018-04-29 stsp switch (ch) {
2125 80ddbec8 2018-04-29 stsp start_commit = optarg;
2128 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2129 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2130 ecb28ae0 2018-07-16 stsp err(1, "-r option");
2133 17020d27 2019-03-07 stsp usage_log();
2134 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2138 80ddbec8 2018-04-29 stsp argc -= optind;
2139 80ddbec8 2018-04-29 stsp argv += optind;
2141 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2142 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2143 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2144 ecb28ae0 2018-07-16 stsp goto done;
2146 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2147 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2148 963f97a1 2019-03-18 stsp goto done;
2149 963f97a1 2019-03-18 stsp error = NULL;
2151 963f97a1 2019-03-18 stsp if (argc == 0) {
2152 963f97a1 2019-03-18 stsp path = strdup("");
2153 963f97a1 2019-03-18 stsp if (path == NULL) {
2154 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2155 ecb28ae0 2018-07-16 stsp goto done;
2157 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2158 963f97a1 2019-03-18 stsp if (worktree) {
2159 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2161 963f97a1 2019-03-18 stsp if (error)
2162 963f97a1 2019-03-18 stsp goto done;
2164 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2165 963f97a1 2019-03-18 stsp if (path == NULL) {
2166 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2167 963f97a1 2019-03-18 stsp goto done;
2171 963f97a1 2019-03-18 stsp usage_log();
2173 963f97a1 2019-03-18 stsp repo_path = worktree ?
2174 963f97a1 2019-03-18 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2175 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2176 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2177 963f97a1 2019-03-18 stsp goto done;
2180 a915003a 2019-02-05 stsp init_curses();
2182 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
2183 80ddbec8 2018-04-29 stsp if (error != NULL)
2184 ecb28ae0 2018-07-16 stsp goto done;
2186 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2187 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2188 c02c541e 2019-03-29 stsp if (error)
2189 c02c541e 2019-03-29 stsp goto done;
2191 15a94983 2018-12-23 stsp if (start_commit == NULL)
2192 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&start_id, worktree ?
2193 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2196 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&start_id, repo,
2197 15a94983 2018-12-23 stsp start_commit);
2198 80ddbec8 2018-04-29 stsp if (error != NULL)
2199 8b473291 2019-02-21 stsp goto done;
2201 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2202 8b473291 2019-02-21 stsp if (error)
2203 ecb28ae0 2018-07-16 stsp goto done;
2205 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2206 04cc582a 2018-08-01 stsp if (view == NULL) {
2207 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2208 04cc582a 2018-08-01 stsp goto done;
2210 8b473291 2019-02-21 stsp error = open_log_view(view, start_id, &refs, repo, path, 1);
2211 ba4f502b 2018-08-04 stsp if (error)
2212 ba4f502b 2018-08-04 stsp goto done;
2213 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2215 ecb28ae0 2018-07-16 stsp free(repo_path);
2216 ecb28ae0 2018-07-16 stsp free(cwd);
2217 ecb28ae0 2018-07-16 stsp free(path);
2218 899d86c2 2018-05-10 stsp free(start_id);
2220 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2221 ec142235 2019-03-07 stsp if (worktree)
2222 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2223 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2224 80ddbec8 2018-04-29 stsp return error;
2227 4ed7e80c 2018-05-20 stsp __dead static void
2228 9f7d7167 2018-04-29 stsp usage_diff(void)
2231 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2232 9f7d7167 2018-04-29 stsp getprogname());
2236 b304db33 2018-05-20 stsp static char *
2237 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2239 b304db33 2018-05-20 stsp char *line;
2240 b304db33 2018-05-20 stsp size_t linelen;
2241 b304db33 2018-05-20 stsp size_t lineno;
2242 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2244 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2246 b304db33 2018-05-20 stsp *len = linelen;
2247 b304db33 2018-05-20 stsp return line;
2250 4ed7e80c 2018-05-20 stsp static const struct got_error *
2251 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2252 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
2253 c3e9aa98 2019-05-13 jcs char *header)
2255 61e69b96 2018-05-20 stsp const struct got_error *err;
2256 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2257 b304db33 2018-05-20 stsp char *line;
2258 b304db33 2018-05-20 stsp size_t len;
2259 61e69b96 2018-05-20 stsp wchar_t *wline;
2260 e0b650dd 2018-05-20 stsp int width;
2262 26ed57b2 2018-05-19 stsp rewind(f);
2263 f7d12f7e 2018-08-01 stsp werase(view->window);
2265 a3404814 2018-09-02 stsp if (header) {
2266 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
2267 a3404814 2018-09-02 stsp if (err) {
2268 a3404814 2018-09-02 stsp return err;
2271 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2272 a3404814 2018-09-02 stsp wstandout(view->window);
2273 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2274 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2275 a3404814 2018-09-02 stsp wstandend(view->window);
2276 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2277 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2279 a3404814 2018-09-02 stsp if (max_lines <= 1)
2280 a3404814 2018-09-02 stsp return NULL;
2281 a3404814 2018-09-02 stsp max_lines--;
2285 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2286 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2287 26ed57b2 2018-05-19 stsp if (line == NULL) {
2291 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2292 26ed57b2 2018-05-19 stsp free(line);
2296 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2297 61e69b96 2018-05-20 stsp if (err) {
2298 61e69b96 2018-05-20 stsp free(line);
2299 61e69b96 2018-05-20 stsp return err;
2301 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2302 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2303 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2304 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2305 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2306 26ed57b2 2018-05-19 stsp free(line);
2307 2550e4c3 2018-07-13 stsp free(wline);
2308 2550e4c3 2018-07-13 stsp wline = NULL;
2310 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2312 1a57306a 2018-09-02 stsp view_vborder(view);
2314 c3e9aa98 2019-05-13 jcs if (*eof) {
2315 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2316 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2317 c3e9aa98 2019-05-13 jcs nprinted++;
2320 c3e9aa98 2019-05-13 jcs err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2322 c3e9aa98 2019-05-13 jcs return err;
2325 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2326 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2327 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2330 26ed57b2 2018-05-19 stsp return NULL;
2333 abd2672a 2018-12-23 stsp static char *
2334 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2336 abd2672a 2018-12-23 stsp char *p, *s = ctime_r(time, datebuf);
2337 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2339 abd2672a 2018-12-23 stsp *p = '\0';
2343 4ed7e80c 2018-05-20 stsp static const struct got_error *
2344 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2345 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2347 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2348 abd2672a 2018-12-23 stsp char datebuf[26];
2349 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2350 15a94983 2018-12-23 stsp char *id_str = NULL;
2351 45d799e2 2018-12-23 stsp time_t committer_time;
2352 45d799e2 2018-12-23 stsp const char *author, *committer;
2353 8b473291 2019-02-21 stsp char *refs_str = NULL;
2355 8b473291 2019-02-21 stsp if (refs) {
2356 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, commit_id);
2358 8b473291 2019-02-21 stsp return err;
2361 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2363 abd2672a 2018-12-23 stsp return err;
2365 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2366 15a94983 2018-12-23 stsp if (err) {
2367 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2368 15a94983 2018-12-23 stsp goto done;
2371 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2372 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2373 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2374 abd2672a 2018-12-23 stsp goto done;
2376 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2377 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2378 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2379 abd2672a 2018-12-23 stsp goto done;
2381 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2382 abd2672a 2018-12-23 stsp if (fprintf(outfile, "date: %s UTC\n",
2383 45d799e2 2018-12-23 stsp get_datestr(&committer_time, datebuf)) < 0) {
2384 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2385 abd2672a 2018-12-23 stsp goto done;
2387 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2388 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2389 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2390 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2391 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2392 abd2672a 2018-12-23 stsp goto done;
2394 45d799e2 2018-12-23 stsp if (fprintf(outfile, "%s\n",
2395 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(commit)) < 0) {
2396 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2397 abd2672a 2018-12-23 stsp goto done;
2400 abd2672a 2018-12-23 stsp free(id_str);
2401 8b473291 2019-02-21 stsp free(refs_str);
2402 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2403 abd2672a 2018-12-23 stsp return err;
2406 abd2672a 2018-12-23 stsp static const struct got_error *
2407 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2409 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2410 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2411 15a94983 2018-12-23 stsp int obj_type;
2413 511a516b 2018-05-19 stsp f = got_opentemp();
2414 48ae06ee 2018-10-18 stsp if (f == NULL) {
2415 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2416 48ae06ee 2018-10-18 stsp goto done;
2418 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2419 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2420 fb43ecf1 2019-02-11 stsp goto done;
2424 15a94983 2018-12-23 stsp if (s->id1)
2425 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2427 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2429 15a94983 2018-12-23 stsp goto done;
2431 15a94983 2018-12-23 stsp switch (obj_type) {
2432 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2433 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2434 54156555 2018-12-24 stsp s->diff_context, s->repo, f);
2436 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2437 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2438 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
2440 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2441 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2442 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2443 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2445 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2448 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2449 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2450 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2452 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2453 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2454 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2455 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2456 8b473291 2019-02-21 stsp s->repo, f);
2461 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2463 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2464 15a94983 2018-12-23 stsp s->diff_context, s->repo, f);
2468 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2472 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2473 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2474 48ae06ee 2018-10-18 stsp return err;
2477 f5215bb9 2019-02-22 stsp static void
2478 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2480 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2481 f5215bb9 2019-02-22 stsp update_panels();
2482 f5215bb9 2019-02-22 stsp doupdate();
2485 48ae06ee 2018-10-18 stsp static const struct got_error *
2486 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2487 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2488 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2490 48ae06ee 2018-10-18 stsp const struct got_error *err;
2492 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2493 15a94983 2018-12-23 stsp int type1, type2;
2494 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2496 15a94983 2018-12-23 stsp return err;
2497 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2499 15a94983 2018-12-23 stsp return err;
2501 15a94983 2018-12-23 stsp if (type1 != type2)
2502 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2505 15a94983 2018-12-23 stsp if (id1) {
2506 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2507 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2508 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2510 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2512 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2513 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2514 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2515 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2516 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2518 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2519 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2520 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2521 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2522 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2523 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2524 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2526 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2527 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2528 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2530 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2531 48ae06ee 2018-10-18 stsp if (err) {
2532 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2533 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2534 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2535 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2536 48ae06ee 2018-10-18 stsp return err;
2539 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2540 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2541 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2543 5dc9f4bc 2018-08-04 stsp return NULL;
2546 e5a0f69f 2018-08-18 stsp static const struct got_error *
2547 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2549 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2551 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2552 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2553 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2554 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2555 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2556 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2557 e5a0f69f 2018-08-18 stsp return err;
2560 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2561 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2563 a3404814 2018-09-02 stsp const struct got_error *err;
2564 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2565 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2567 a3404814 2018-09-02 stsp if (s->id1) {
2568 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2570 a3404814 2018-09-02 stsp return err;
2572 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2574 a3404814 2018-09-02 stsp return err;
2576 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2577 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2578 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2579 a3404814 2018-09-02 stsp free(id_str1);
2580 a3404814 2018-09-02 stsp free(id_str2);
2581 a3404814 2018-09-02 stsp return err;
2583 a3404814 2018-09-02 stsp free(id_str1);
2584 a3404814 2018-09-02 stsp free(id_str2);
2586 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2587 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2591 15a087fe 2019-02-21 stsp static const struct got_error *
2592 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2593 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2595 d7a04538 2019-02-21 stsp const struct got_error *err;
2596 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
2597 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
2598 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
2600 15a087fe 2019-02-21 stsp free(s->id2);
2601 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2602 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2603 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2605 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2607 d7a04538 2019-02-21 stsp return err;
2608 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
2609 15a087fe 2019-02-21 stsp free(s->id1);
2610 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
2611 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2612 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
2613 15a087fe 2019-02-21 stsp return NULL;
2616 0cf4efb1 2018-09-29 stsp static const struct got_error *
2617 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2618 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2620 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2621 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2622 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
2623 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
2626 e5a0f69f 2018-08-18 stsp switch (ch) {
2628 1e37a5c2 2019-05-12 jcs case KEY_UP:
2629 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
2630 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2632 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2633 a60a9dc4 2019-05-13 jcs case CTRL('b'):
2634 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
2637 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
2638 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
2639 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2642 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2643 1e37a5c2 2019-05-12 jcs if (!s->eof)
2644 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2646 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
2647 a60a9dc4 2019-05-13 jcs case CTRL('f'):
2649 00ba99a7 2019-05-12 jcs if (s->eof)
2652 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
2653 1e37a5c2 2019-05-12 jcs char *line;
2654 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
2655 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2656 1e37a5c2 2019-05-12 jcs if (line == NULL)
2661 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
2662 1e37a5c2 2019-05-12 jcs s->diff_context--;
2663 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2664 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2668 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2669 1e37a5c2 2019-05-12 jcs s->diff_context++;
2670 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2671 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2676 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2678 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2679 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
2680 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
2681 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2684 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2689 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2693 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2694 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2696 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2697 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2701 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2703 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2705 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2706 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
2708 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
2709 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
2710 1e37a5c2 2019-05-12 jcs update_panels();
2711 1e37a5c2 2019-05-12 jcs doupdate();
2713 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
2714 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
2715 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
2716 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
2720 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2725 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
2726 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2729 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2733 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2734 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2736 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2737 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2743 bcbd79e2 2018-08-19 stsp return err;
2746 4ed7e80c 2018-05-20 stsp static const struct got_error *
2747 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2749 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2750 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2751 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2752 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2753 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2754 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2756 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2758 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2760 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2761 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2762 eb6600df 2019-01-04 stsp NULL) == -1)
2763 26ed57b2 2018-05-19 stsp err(1, "pledge");
2766 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2767 26ed57b2 2018-05-19 stsp switch (ch) {
2769 17020d27 2019-03-07 stsp usage_diff();
2770 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2774 26ed57b2 2018-05-19 stsp argc -= optind;
2775 26ed57b2 2018-05-19 stsp argv += optind;
2777 26ed57b2 2018-05-19 stsp if (argc == 0) {
2778 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2779 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2780 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2781 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2782 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2783 15a94983 2018-12-23 stsp id_str1 = argv[0];
2784 15a94983 2018-12-23 stsp id_str2 = argv[1];
2785 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2786 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2787 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2788 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2789 15a94983 2018-12-23 stsp id_str1 = argv[1];
2790 15a94983 2018-12-23 stsp id_str2 = argv[2];
2792 26ed57b2 2018-05-19 stsp usage_diff();
2794 a915003a 2019-02-05 stsp init_curses();
2796 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2797 eb6600df 2019-01-04 stsp if (error)
2798 eb6600df 2019-01-04 stsp goto done;
2800 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
2801 26ed57b2 2018-05-19 stsp if (error)
2802 26ed57b2 2018-05-19 stsp goto done;
2804 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
2805 26ed57b2 2018-05-19 stsp if (error)
2806 26ed57b2 2018-05-19 stsp goto done;
2808 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
2809 26ed57b2 2018-05-19 stsp if (error)
2810 26ed57b2 2018-05-19 stsp goto done;
2812 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2813 8b473291 2019-02-21 stsp if (error)
2814 8b473291 2019-02-21 stsp goto done;
2816 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2817 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2818 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2819 ea5e7bb5 2018-08-01 stsp goto done;
2821 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2822 5dc9f4bc 2018-08-04 stsp if (error)
2823 5dc9f4bc 2018-08-04 stsp goto done;
2824 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2826 c02c541e 2019-03-29 stsp free(repo_path);
2827 26ed57b2 2018-05-19 stsp got_repo_close(repo);
2828 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2829 26ed57b2 2018-05-19 stsp return error;
2832 4ed7e80c 2018-05-20 stsp __dead static void
2833 9f7d7167 2018-04-29 stsp usage_blame(void)
2836 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2837 9f7d7167 2018-04-29 stsp getprogname());
2841 84451b3e 2018-07-10 stsp struct tog_blame_line {
2842 84451b3e 2018-07-10 stsp int annotated;
2843 84451b3e 2018-07-10 stsp struct got_object_id *id;
2846 4ed7e80c 2018-05-20 stsp static const struct got_error *
2847 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2848 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2849 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2850 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2852 84451b3e 2018-07-10 stsp const struct got_error *err;
2853 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2854 84451b3e 2018-07-10 stsp char *line;
2855 84451b3e 2018-07-10 stsp size_t len;
2856 84451b3e 2018-07-10 stsp wchar_t *wline;
2857 b700b5d6 2018-07-10 stsp int width, wlimit;
2858 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2859 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2860 ab089a2a 2018-07-12 stsp char *id_str;
2862 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2864 ab089a2a 2018-07-12 stsp return err;
2866 84451b3e 2018-07-10 stsp rewind(f);
2867 f7d12f7e 2018-08-01 stsp werase(view->window);
2869 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
2870 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2871 ab089a2a 2018-07-12 stsp free(id_str);
2872 ab089a2a 2018-07-12 stsp return err;
2875 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2876 ab089a2a 2018-07-12 stsp free(line);
2877 2550e4c3 2018-07-13 stsp line = NULL;
2878 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2879 a3404814 2018-09-02 stsp wstandout(view->window);
2880 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2881 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2882 a3404814 2018-09-02 stsp wstandend(view->window);
2883 2550e4c3 2018-07-13 stsp free(wline);
2884 2550e4c3 2018-07-13 stsp wline = NULL;
2885 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2886 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2888 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2889 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2890 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
2891 ab089a2a 2018-07-12 stsp free(id_str);
2892 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2894 ab089a2a 2018-07-12 stsp free(id_str);
2895 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2896 3f60a8ef 2018-07-10 stsp free(line);
2897 2550e4c3 2018-07-13 stsp line = NULL;
2899 3f60a8ef 2018-07-10 stsp return err;
2900 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2901 2550e4c3 2018-07-13 stsp free(wline);
2902 2550e4c3 2018-07-13 stsp wline = NULL;
2903 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2904 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2907 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2908 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2909 84451b3e 2018-07-10 stsp if (line == NULL) {
2913 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2914 84451b3e 2018-07-10 stsp free(line);
2918 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2919 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2920 84451b3e 2018-07-10 stsp if (err) {
2921 84451b3e 2018-07-10 stsp free(line);
2922 84451b3e 2018-07-10 stsp return err;
2925 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2926 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2928 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2929 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2930 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2931 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2932 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2933 84451b3e 2018-07-10 stsp char *id_str;
2934 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2935 84451b3e 2018-07-10 stsp if (err) {
2936 84451b3e 2018-07-10 stsp free(line);
2937 2550e4c3 2018-07-13 stsp free(wline);
2938 84451b3e 2018-07-10 stsp return err;
2940 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2941 84451b3e 2018-07-10 stsp free(id_str);
2942 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2944 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2945 ee41ec32 2018-07-10 stsp prev_id = NULL;
2948 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2949 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2950 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2953 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2954 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2955 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2956 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2957 84451b3e 2018-07-10 stsp free(line);
2958 2550e4c3 2018-07-13 stsp free(wline);
2959 2550e4c3 2018-07-13 stsp wline = NULL;
2961 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2963 1a57306a 2018-09-02 stsp view_vborder(view);
2965 84451b3e 2018-07-10 stsp return NULL;
2968 84451b3e 2018-07-10 stsp static const struct got_error *
2969 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2971 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2972 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2973 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2974 1a76625f 2018-10-22 stsp int errcode;
2976 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2977 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2978 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2980 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2981 1a76625f 2018-10-22 stsp if (errcode)
2982 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
2984 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2985 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2986 d68a0a7d 2018-07-10 stsp goto done;
2989 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2990 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2992 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2993 d68a0a7d 2018-07-10 stsp if (line->annotated)
2994 d68a0a7d 2018-07-10 stsp goto done;
2996 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2997 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2998 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2999 84451b3e 2018-07-10 stsp goto done;
3001 84451b3e 2018-07-10 stsp line->annotated = 1;
3003 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3004 1a76625f 2018-10-22 stsp if (errcode)
3005 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3006 84451b3e 2018-07-10 stsp return err;
3009 84451b3e 2018-07-10 stsp static void *
3010 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3012 18430de3 2018-07-10 stsp const struct got_error *err;
3013 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3014 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3015 1a76625f 2018-10-22 stsp int errcode;
3017 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3018 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
3020 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3021 1a76625f 2018-10-22 stsp if (errcode)
3022 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3023 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3025 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3026 c9beca56 2018-07-22 stsp ta->repo = NULL;
3027 c9beca56 2018-07-22 stsp *ta->complete = 1;
3029 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3030 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3031 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3033 18430de3 2018-07-10 stsp return (void *)err;
3036 245d91c1 2018-07-12 stsp static struct got_object_id *
3037 15a94983 2018-12-23 stsp get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3038 15a94983 2018-12-23 stsp int selected_line)
3040 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3042 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3043 245d91c1 2018-07-12 stsp if (!line->annotated)
3044 245d91c1 2018-07-12 stsp return NULL;
3046 245d91c1 2018-07-12 stsp return line->id;
3049 b880a918 2018-07-10 stsp static const struct got_error *
3050 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3052 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3055 245d91c1 2018-07-12 stsp if (blame->thread) {
3056 1a76625f 2018-10-22 stsp int errcode;
3057 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3058 1a76625f 2018-10-22 stsp if (errcode)
3059 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3060 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3061 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3062 1a76625f 2018-10-22 stsp if (errcode)
3063 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3064 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3065 1a76625f 2018-10-22 stsp if (errcode)
3066 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3067 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3068 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3069 245d91c1 2018-07-12 stsp err = NULL;
3070 245d91c1 2018-07-12 stsp blame->thread = NULL;
3072 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3073 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3074 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3076 245d91c1 2018-07-12 stsp if (blame->f) {
3077 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3078 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3079 245d91c1 2018-07-12 stsp blame->f = NULL;
3081 57670559 2018-12-23 stsp if (blame->lines) {
3082 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3083 57670559 2018-12-23 stsp free(blame->lines[i].id);
3084 57670559 2018-12-23 stsp free(blame->lines);
3085 57670559 2018-12-23 stsp blame->lines = NULL;
3087 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3088 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3090 245d91c1 2018-07-12 stsp return err;
3093 245d91c1 2018-07-12 stsp static const struct got_error *
3094 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3095 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3096 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3097 245d91c1 2018-07-12 stsp struct got_repository *repo)
3099 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3100 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3101 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3102 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3103 15a94983 2018-12-23 stsp int obj_type;
3105 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3107 15a94983 2018-12-23 stsp return err;
3108 15a94983 2018-12-23 stsp if (obj_id == NULL)
3109 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
3111 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3113 84451b3e 2018-07-10 stsp goto done;
3115 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3116 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3117 84451b3e 2018-07-10 stsp goto done;
3120 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3122 a70480e0 2018-06-23 stsp goto done;
3123 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3124 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3125 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3126 84451b3e 2018-07-10 stsp goto done;
3128 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3129 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
3131 84451b3e 2018-07-10 stsp goto done;
3133 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3134 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3135 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3136 84451b3e 2018-07-10 stsp goto done;
3139 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3141 bd24772e 2018-07-11 stsp goto done;
3143 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3144 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3145 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3146 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3147 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3148 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3149 245d91c1 2018-07-12 stsp goto done;
3151 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3153 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3154 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3155 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3156 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3157 245d91c1 2018-07-12 stsp *blame_complete = 0;
3161 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3162 27d434c2 2018-09-15 stsp free(obj_id);
3164 245d91c1 2018-07-12 stsp stop_blame(blame);
3165 245d91c1 2018-07-12 stsp return err;
3168 245d91c1 2018-07-12 stsp static const struct got_error *
3169 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3170 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3171 8b473291 2019-02-21 stsp struct got_repository *repo)
3173 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3174 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3176 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3178 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3180 7cbe629d 2018-08-04 stsp return err;
3182 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3183 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3184 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3185 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3186 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3187 fb2756b9 2018-08-04 stsp s->path = path;
3188 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
3189 638f9024 2019-05-13 stsp return got_error_from_errno("open_blame_view");
3190 fb2756b9 2018-08-04 stsp s->repo = repo;
3191 8b473291 2019-02-21 stsp s->refs = refs;
3192 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3193 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3195 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3196 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3197 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3198 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
3199 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
3201 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3202 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3203 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3204 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3207 e5a0f69f 2018-08-18 stsp static const struct got_error *
3208 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3210 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3211 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3213 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3214 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3216 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3217 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3218 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3219 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3220 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3223 e5a0f69f 2018-08-18 stsp free(s->path);
3225 e5a0f69f 2018-08-18 stsp return err;
3228 7cbe629d 2018-08-04 stsp static const struct got_error *
3229 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
3231 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3233 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
3234 6c4c42e0 2019-06-24 stsp return NULL;
3237 6c4c42e0 2019-06-24 stsp static int
3238 6c4c42e0 2019-06-24 stsp match_line(const char *line, regex_t *regex)
3240 6c4c42e0 2019-06-24 stsp regmatch_t regmatch;
3242 6c4c42e0 2019-06-24 stsp return regexec(regex, line, 1, ®match, 0) == 0;
3246 6c4c42e0 2019-06-24 stsp static const struct got_error *
3247 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
3249 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3250 6c4c42e0 2019-06-24 stsp int lineno;
3252 6c4c42e0 2019-06-24 stsp if (!view->searching) {
3253 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3254 6c4c42e0 2019-06-24 stsp return NULL;
3257 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3258 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3259 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
3261 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
3263 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3264 6c4c42e0 2019-06-24 stsp lineno = 1;
3266 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3269 6c4c42e0 2019-06-24 stsp while (1) {
3270 6c4c42e0 2019-06-24 stsp char *line = NULL;
3271 6c4c42e0 2019-06-24 stsp off_t offset;
3272 6c4c42e0 2019-06-24 stsp size_t len;
3274 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
3275 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
3276 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3277 6c4c42e0 2019-06-24 stsp free(line);
3281 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3282 6c4c42e0 2019-06-24 stsp lineno = 1;
3284 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3287 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
3288 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3289 6c4c42e0 2019-06-24 stsp free(line);
3290 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
3292 6c4c42e0 2019-06-24 stsp free(line);
3293 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
3294 2246482e 2019-06-25 stsp if (line && match_line(line, &view->regex)) {
3295 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3296 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
3297 6c4c42e0 2019-06-24 stsp free(line);
3300 6c4c42e0 2019-06-24 stsp free(line);
3301 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3307 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3308 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
3309 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
3312 6c4c42e0 2019-06-24 stsp return NULL;
3315 6c4c42e0 2019-06-24 stsp static const struct got_error *
3316 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
3318 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3319 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
3320 2b380cc8 2018-10-24 stsp int errcode;
3322 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
3323 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3324 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
3325 2b380cc8 2018-10-24 stsp if (errcode)
3326 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3329 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3330 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3331 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
3332 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
3334 669b5ffa 2018-10-07 stsp view_vborder(view);
3335 e5a0f69f 2018-08-18 stsp return err;
3338 e5a0f69f 2018-08-18 stsp static const struct got_error *
3339 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3340 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3342 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
3343 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
3344 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3345 669b5ffa 2018-10-07 stsp int begin_x = 0;
3347 e5a0f69f 2018-08-18 stsp switch (ch) {
3349 1e37a5c2 2019-05-12 jcs s->done = 1;
3352 1e37a5c2 2019-05-12 jcs case KEY_UP:
3353 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
3354 1e37a5c2 2019-05-12 jcs s->selected_line--;
3355 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
3356 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3357 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3359 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3360 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
3361 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3364 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3365 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3366 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3368 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3371 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3372 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3373 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3374 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3375 1e37a5c2 2019-05-12 jcs s->selected_line++;
3376 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3377 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3378 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3381 1e37a5c2 2019-05-12 jcs case 'p': {
3382 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3383 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3384 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3385 1e37a5c2 2019-05-12 jcs if (id == NULL)
3387 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3388 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3389 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3390 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3391 1e37a5c2 2019-05-12 jcs int obj_type;
3392 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3393 1e37a5c2 2019-05-12 jcs s->repo, id);
3396 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3397 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3398 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3399 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3402 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3403 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3404 1e37a5c2 2019-05-12 jcs pid->id, s->path);
3405 e5a0f69f 2018-08-18 stsp if (err) {
3406 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
3407 1e37a5c2 2019-05-12 jcs err = NULL;
3408 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3411 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
3413 1e37a5c2 2019-05-12 jcs free(blob_id);
3414 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
3415 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
3416 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3419 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3421 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3423 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
3424 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
3426 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3431 1e37a5c2 2019-05-12 jcs s->done = 1;
3432 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3433 1e37a5c2 2019-05-12 jcs s->done = 0;
3434 1e37a5c2 2019-05-12 jcs if (thread_err)
3436 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3437 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
3438 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3439 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3440 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
3441 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
3446 1e37a5c2 2019-05-12 jcs case 'B': {
3447 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
3448 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
3449 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
3451 1e37a5c2 2019-05-12 jcs s->done = 1;
3452 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3453 1e37a5c2 2019-05-12 jcs s->done = 0;
3454 1e37a5c2 2019-05-12 jcs if (thread_err)
3456 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3457 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
3458 1e37a5c2 2019-05-12 jcs s->blamed_commit =
3459 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
3460 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3461 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3462 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
3463 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
3468 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3469 1e37a5c2 2019-05-12 jcs case '\r': {
3470 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3471 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
3472 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
3473 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3474 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3475 1e37a5c2 2019-05-12 jcs if (id == NULL)
3477 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
3480 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
3481 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
3482 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3483 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3484 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3485 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
3486 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3487 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
3490 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
3491 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
3492 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3494 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3497 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3498 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3501 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
3503 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3506 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
3507 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
3509 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3514 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3516 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3517 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
3518 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
3521 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3522 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
3523 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3524 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3527 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
3528 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
3529 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
3530 1e37a5c2 2019-05-12 jcs view->nlines - 2;
3532 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
3533 1e37a5c2 2019-05-12 jcs s->blame.nlines -
3534 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
3536 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3537 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
3538 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3539 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3545 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
3548 a70480e0 2018-06-23 stsp static const struct got_error *
3549 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
3551 a70480e0 2018-06-23 stsp const struct got_error *error;
3552 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
3553 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3554 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
3555 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3556 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3557 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
3559 e1cd8fed 2018-08-01 stsp struct tog_view *view;
3561 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3563 a70480e0 2018-06-23 stsp #ifndef PROFILE
3564 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3565 8e94dd5b 2019-01-04 stsp NULL) == -1)
3566 a70480e0 2018-06-23 stsp err(1, "pledge");
3569 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3570 a70480e0 2018-06-23 stsp switch (ch) {
3572 a70480e0 2018-06-23 stsp commit_id_str = optarg;
3575 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3576 69069811 2018-08-02 stsp if (repo_path == NULL)
3577 69069811 2018-08-02 stsp err(1, "-r option");
3580 17020d27 2019-03-07 stsp usage_blame();
3581 a70480e0 2018-06-23 stsp /* NOTREACHED */
3585 a70480e0 2018-06-23 stsp argc -= optind;
3586 a70480e0 2018-06-23 stsp argv += optind;
3588 69069811 2018-08-02 stsp if (argc == 1)
3589 69069811 2018-08-02 stsp path = argv[0];
3591 a70480e0 2018-06-23 stsp usage_blame();
3593 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
3594 69069811 2018-08-02 stsp if (cwd == NULL) {
3595 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3596 69069811 2018-08-02 stsp goto done;
3598 69069811 2018-08-02 stsp if (repo_path == NULL) {
3599 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3600 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3601 69069811 2018-08-02 stsp goto done;
3603 eb41ed75 2019-02-05 stsp error = NULL;
3604 eb41ed75 2019-02-05 stsp if (worktree) {
3605 eb41ed75 2019-02-05 stsp repo_path =
3606 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3607 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
3608 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3609 eb41ed75 2019-02-05 stsp if (error)
3610 eb41ed75 2019-02-05 stsp goto done;
3612 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
3613 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
3614 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3615 eb41ed75 2019-02-05 stsp goto done;
3620 a915003a 2019-02-05 stsp init_curses();
3622 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
3623 c02c541e 2019-03-29 stsp if (error != NULL)
3624 8e94dd5b 2019-01-04 stsp goto done;
3626 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3627 c02c541e 2019-03-29 stsp if (error)
3628 92205607 2019-01-04 stsp goto done;
3630 eb41ed75 2019-02-05 stsp if (worktree) {
3631 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3632 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3633 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3634 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3635 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3636 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3637 eb41ed75 2019-02-05 stsp path) == -1) {
3638 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3639 eb41ed75 2019-02-05 stsp goto done;
3641 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3644 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3646 eb41ed75 2019-02-05 stsp if (error)
3647 69069811 2018-08-02 stsp goto done;
3649 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
3650 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
3651 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3652 a70480e0 2018-06-23 stsp if (error != NULL)
3653 66b4983c 2018-06-23 stsp goto done;
3654 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3655 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
3657 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
3658 15a94983 2018-12-23 stsp commit_id_str);
3660 a19e88aa 2018-06-23 stsp if (error != NULL)
3661 8b473291 2019-02-21 stsp goto done;
3663 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
3664 8b473291 2019-02-21 stsp if (error)
3665 a19e88aa 2018-06-23 stsp goto done;
3667 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3668 e1cd8fed 2018-08-01 stsp if (view == NULL) {
3669 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3670 e1cd8fed 2018-08-01 stsp goto done;
3672 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3673 7cbe629d 2018-08-04 stsp if (error)
3674 7cbe629d 2018-08-04 stsp goto done;
3675 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3677 69069811 2018-08-02 stsp free(repo_path);
3678 69069811 2018-08-02 stsp free(cwd);
3679 a70480e0 2018-06-23 stsp free(commit_id);
3680 eb41ed75 2019-02-05 stsp if (worktree)
3681 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
3683 a70480e0 2018-06-23 stsp got_repo_close(repo);
3684 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3685 a70480e0 2018-06-23 stsp return error;
3688 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3689 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
3690 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
3691 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
3692 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
3693 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
3694 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
3696 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3697 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
3698 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
3699 ffd1d5e5 2018-06-23 stsp int width, n;
3701 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
3703 f7d12f7e 2018-08-01 stsp werase(view->window);
3705 ffd1d5e5 2018-06-23 stsp if (limit == 0)
3706 ffd1d5e5 2018-06-23 stsp return NULL;
3708 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
3710 ffd1d5e5 2018-06-23 stsp return err;
3711 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3712 a3404814 2018-09-02 stsp wstandout(view->window);
3713 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3714 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3715 a3404814 2018-09-02 stsp wstandend(view->window);
3716 2550e4c3 2018-07-13 stsp free(wline);
3717 2550e4c3 2018-07-13 stsp wline = NULL;
3718 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3719 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3720 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3721 ffd1d5e5 2018-06-23 stsp return NULL;
3722 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
3724 ce52c690 2018-06-23 stsp return err;
3725 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3726 2550e4c3 2018-07-13 stsp free(wline);
3727 2550e4c3 2018-07-13 stsp wline = NULL;
3728 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3729 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3730 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3731 ffd1d5e5 2018-06-23 stsp return NULL;
3732 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3733 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
3734 a1eca9bb 2018-06-23 stsp return NULL;
3736 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3737 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
3738 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
3739 0cf4efb1 2018-09-29 stsp if (view->focussed)
3740 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3741 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
3743 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
3744 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
3745 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3746 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3747 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3748 ffd1d5e5 2018-06-23 stsp return NULL;
3752 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
3753 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3756 ffd1d5e5 2018-06-23 stsp while (te) {
3757 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
3759 1d13200f 2018-07-12 stsp if (show_ids) {
3760 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
3762 638f9024 2019-05-13 stsp return got_error_from_errno(
3763 230a42bd 2019-05-11 jcs "got_object_id_str");
3765 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3766 d8355ef1 2019-02-10 stsp te->name, S_ISDIR(te->mode) ? "/" :
3767 d8355ef1 2019-02-10 stsp ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3768 1d13200f 2018-07-12 stsp free(id_str);
3769 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3771 1d13200f 2018-07-12 stsp free(id_str);
3772 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
3773 ffd1d5e5 2018-06-23 stsp if (err) {
3774 ffd1d5e5 2018-06-23 stsp free(line);
3777 ffd1d5e5 2018-06-23 stsp if (n == selected) {
3778 0cf4efb1 2018-09-29 stsp if (view->focussed)
3779 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3780 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
3782 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3783 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3784 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3785 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
3786 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3787 ffd1d5e5 2018-06-23 stsp free(line);
3788 2550e4c3 2018-07-13 stsp free(wline);
3789 2550e4c3 2018-07-13 stsp wline = NULL;
3791 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3792 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
3793 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3795 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3798 ffd1d5e5 2018-06-23 stsp return err;
3801 ffd1d5e5 2018-06-23 stsp static void
3802 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
3803 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
3804 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
3806 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
3809 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
3812 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3813 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
3814 ffd1d5e5 2018-06-23 stsp if (!isroot)
3815 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3819 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
3820 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
3821 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
3822 ffd1d5e5 2018-06-23 stsp prev = te;
3823 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3825 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
3826 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3828 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3829 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3832 768394f3 2019-01-24 stsp static int
3833 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3834 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
3835 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
3837 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
3838 ffd1d5e5 2018-06-23 stsp int n = 0;
3840 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
3841 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3843 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
3844 768394f3 2019-01-24 stsp last = last_displayed_entry;
3845 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
3846 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
3847 768394f3 2019-01-24 stsp if (last) {
3848 768394f3 2019-01-24 stsp *first_displayed_entry = next;
3849 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
3855 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3856 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
3857 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
3859 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
3860 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
3861 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
3863 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
3864 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
3866 ce52c690 2018-06-23 stsp len += strlen(te->name);
3868 ce52c690 2018-06-23 stsp *path = calloc(1, len);
3869 ffd1d5e5 2018-06-23 stsp if (path == NULL)
3870 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
3872 ce52c690 2018-06-23 stsp (*path)[0] = '/';
3873 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
3874 d9765a41 2018-06-23 stsp while (pt) {
3875 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3876 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3877 cb2ebc8a 2018-06-23 stsp goto done;
3879 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
3880 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3881 cb2ebc8a 2018-06-23 stsp goto done;
3883 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3886 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3887 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3888 ce52c690 2018-06-23 stsp goto done;
3892 ce52c690 2018-06-23 stsp if (err) {
3893 ce52c690 2018-06-23 stsp free(*path);
3894 ce52c690 2018-06-23 stsp *path = NULL;
3896 ce52c690 2018-06-23 stsp return err;
3899 ce52c690 2018-06-23 stsp static const struct got_error *
3900 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3901 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3902 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3903 8b473291 2019-02-21 stsp struct got_repository *repo)
3905 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3906 ce52c690 2018-06-23 stsp char *path;
3907 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3909 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3911 ce52c690 2018-06-23 stsp return err;
3913 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3914 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3915 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3917 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
3918 e5a0f69f 2018-08-18 stsp if (err) {
3919 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3920 e5a0f69f 2018-08-18 stsp free(path);
3922 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3923 69efd4c4 2018-07-18 stsp return err;
3926 69efd4c4 2018-07-18 stsp static const struct got_error *
3927 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3928 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3929 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3930 8b473291 2019-02-21 stsp struct got_repository *repo)
3932 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3933 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3934 69efd4c4 2018-07-18 stsp char *path;
3936 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3937 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3938 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3940 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3942 69efd4c4 2018-07-18 stsp return err;
3944 8b473291 2019-02-21 stsp err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3946 e5a0f69f 2018-08-18 stsp view_close(log_view);
3948 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3949 cb2ebc8a 2018-06-23 stsp free(path);
3950 cb2ebc8a 2018-06-23 stsp return err;
3953 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3954 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3955 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3956 8b473291 2019-02-21 stsp struct got_repository *repo)
3958 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3959 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3960 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3962 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3964 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3965 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3966 ffd1d5e5 2018-06-23 stsp goto done;
3968 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3969 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3970 ffd1d5e5 2018-06-23 stsp goto done;
3973 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3974 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3975 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3976 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3977 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3978 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3979 6484ec90 2018-09-29 stsp goto done;
3981 8b473291 2019-02-21 stsp s->refs = refs;
3982 fb2756b9 2018-08-04 stsp s->repo = repo;
3984 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
3985 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
3986 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
3987 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
3988 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
3990 ad80ab7b 2018-08-04 stsp free(commit_id_str);
3991 6484ec90 2018-09-29 stsp if (err) {
3992 fb2756b9 2018-08-04 stsp free(s->tree_label);
3993 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3995 ad80ab7b 2018-08-04 stsp return err;
3998 e5a0f69f 2018-08-18 stsp static const struct got_error *
3999 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
4001 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4003 fb2756b9 2018-08-04 stsp free(s->tree_label);
4004 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4005 6484ec90 2018-09-29 stsp free(s->commit_id);
4006 6484ec90 2018-09-29 stsp s->commit_id = NULL;
4007 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
4008 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
4009 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
4010 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
4011 ad80ab7b 2018-08-04 stsp free(parent);
4014 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
4015 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
4016 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
4018 7c32bd05 2019-06-22 stsp return NULL;
4021 7c32bd05 2019-06-22 stsp static const struct got_error *
4022 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
4024 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4026 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
4027 7c32bd05 2019-06-22 stsp return NULL;
4030 7c32bd05 2019-06-22 stsp static int
4031 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4033 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
4035 7c32bd05 2019-06-22 stsp return regexec(regex, te->name, 1, ®match, 0) == 0;
4038 7c32bd05 2019-06-22 stsp static const struct got_error *
4039 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
4041 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4042 7c32bd05 2019-06-22 stsp struct got_tree_entry *entry, *te;
4044 7c32bd05 2019-06-22 stsp if (!view->searching) {
4045 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4046 7c32bd05 2019-06-22 stsp return NULL;
4049 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4050 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
4051 7c32bd05 2019-06-22 stsp if (s->selected_entry)
4052 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4054 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4057 7c32bd05 2019-06-22 stsp if (s->selected_entry == NULL) {
4058 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4059 7c32bd05 2019-06-22 stsp entry = te;
4061 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4062 7c32bd05 2019-06-22 stsp entry = te;
4063 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) ==
4064 7c32bd05 2019-06-22 stsp s->selected_entry)
4070 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4071 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4073 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4074 7c32bd05 2019-06-22 stsp entry = te;
4078 7c32bd05 2019-06-22 stsp while (1) {
4079 7c32bd05 2019-06-22 stsp if (entry == NULL) {
4080 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
4081 ac66afb8 2019-06-24 stsp view->search_next_done = 1;
4082 ac66afb8 2019-06-24 stsp return NULL;
4084 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4085 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4087 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4088 7c32bd05 2019-06-22 stsp entry = te;
4092 7c32bd05 2019-06-22 stsp if (match_tree_entry(entry, &view->regex)) {
4093 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4094 7c32bd05 2019-06-22 stsp s->matched_entry = entry;
4098 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4099 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(entry, entry);
4101 7c32bd05 2019-06-22 stsp if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4102 7c32bd05 2019-06-22 stsp entry = NULL;
4104 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4105 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) == entry) {
4106 7c32bd05 2019-06-22 stsp entry = te;
4114 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4115 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
4116 7c32bd05 2019-06-22 stsp s->selected = 0;
4119 e5a0f69f 2018-08-18 stsp return NULL;
4122 ad80ab7b 2018-08-04 stsp static const struct got_error *
4123 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
4125 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
4126 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4127 e5a0f69f 2018-08-18 stsp char *parent_path;
4129 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
4131 e5a0f69f 2018-08-18 stsp return err;
4133 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
4134 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
4135 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4136 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
4137 e5a0f69f 2018-08-18 stsp free(parent_path);
4139 669b5ffa 2018-10-07 stsp view_vborder(view);
4140 e5a0f69f 2018-08-18 stsp return err;
4143 e5a0f69f 2018-08-18 stsp static const struct got_error *
4144 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4145 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4147 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4148 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
4149 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
4150 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
4152 e5a0f69f 2018-08-18 stsp switch (ch) {
4154 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
4157 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
4159 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4160 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4161 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
4162 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
4163 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
4164 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4165 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4167 1e37a5c2 2019-05-12 jcs return err;
4168 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
4170 1e37a5c2 2019-05-12 jcs view_close(log_view);
4173 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
4174 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4176 1e37a5c2 2019-05-12 jcs *new_view = log_view;
4179 1e37a5c2 2019-05-12 jcs case KEY_UP:
4180 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
4181 1e37a5c2 2019-05-12 jcs s->selected--;
4182 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
4185 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
4187 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
4188 1e37a5c2 2019-05-12 jcs s->entries, s->tree == s->root);
4190 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4191 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
4192 1e37a5c2 2019-05-12 jcs MAX(0, view->nlines - 4 - s->selected), s->entries,
4193 1e37a5c2 2019-05-12 jcs s->tree == s->root);
4194 1e37a5c2 2019-05-12 jcs s->selected = 0;
4195 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_FIRST(&s->entries->head) ==
4196 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
4197 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
4200 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4201 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
4202 1e37a5c2 2019-05-12 jcs s->selected++;
4205 00ba99a7 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4206 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
4208 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
4209 1e37a5c2 2019-05-12 jcs s->last_displayed_entry, s->entries);
4211 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4212 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4214 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
4215 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
4216 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4219 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
4220 1e37a5c2 2019-05-12 jcs view->nlines, s->last_displayed_entry, s->entries);
4221 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
4222 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
4223 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
4224 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
4226 1e37a5c2 2019-05-12 jcs ndisplayed++;
4227 1e37a5c2 2019-05-12 jcs te = SIMPLEQ_NEXT(te, entry);
4228 1e37a5c2 2019-05-12 jcs } while (te);
4229 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
4232 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4234 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4235 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4236 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
4237 1e37a5c2 2019-05-12 jcs /* user selected '..' */
4238 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
4240 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
4241 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
4243 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
4244 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
4245 1e37a5c2 2019-05-12 jcs s->entries =
4246 1e37a5c2 2019-05-12 jcs got_object_tree_get_entries(s->tree);
4247 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
4248 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
4249 1e37a5c2 2019-05-12 jcs s->selected_entry =
4250 1e37a5c2 2019-05-12 jcs parent->selected_entry;
4251 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
4252 1e37a5c2 2019-05-12 jcs free(parent);
4253 1e37a5c2 2019-05-12 jcs } else if (S_ISDIR(s->selected_entry->mode)) {
4254 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
4255 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&subtree,
4256 1e37a5c2 2019-05-12 jcs s->repo, s->selected_entry->id);
4259 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
4260 941e9f74 2019-05-21 stsp if (err) {
4261 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
4264 1e37a5c2 2019-05-12 jcs } else if (S_ISREG(s->selected_entry->mode)) {
4265 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
4266 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
4267 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
4269 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
4270 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
4271 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
4274 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
4275 669b5ffa 2018-10-07 stsp err = view_close_child(view);
4277 669b5ffa 2018-10-07 stsp return err;
4278 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
4279 669b5ffa 2018-10-07 stsp if (err) {
4280 1e37a5c2 2019-05-12 jcs view_close(blame_view);
4283 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
4284 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
4286 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
4289 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4290 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
4291 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4297 ffd1d5e5 2018-06-23 stsp return err;
4300 ffd1d5e5 2018-06-23 stsp __dead static void
4301 ffd1d5e5 2018-06-23 stsp usage_tree(void)
4304 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4305 ffd1d5e5 2018-06-23 stsp getprogname());
4309 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4310 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
4312 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
4313 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
4314 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4315 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
4316 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4317 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
4318 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
4319 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
4321 5221c383 2018-08-01 stsp struct tog_view *view;
4323 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4325 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
4326 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4327 d188b9a6 2019-01-04 stsp NULL) == -1)
4328 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
4331 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
4332 ffd1d5e5 2018-06-23 stsp switch (ch) {
4334 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
4337 17020d27 2019-03-07 stsp usage_tree();
4338 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
4342 ffd1d5e5 2018-06-23 stsp argc -= optind;
4343 ffd1d5e5 2018-06-23 stsp argv += optind;
4345 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
4346 52185f70 2019-02-05 stsp struct got_worktree *worktree;
4347 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
4348 52185f70 2019-02-05 stsp if (cwd == NULL)
4349 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
4350 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4351 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4352 52185f70 2019-02-05 stsp goto done;
4353 52185f70 2019-02-05 stsp if (worktree) {
4354 52185f70 2019-02-05 stsp free(cwd);
4355 52185f70 2019-02-05 stsp repo_path =
4356 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4357 52185f70 2019-02-05 stsp got_worktree_close(worktree);
4359 52185f70 2019-02-05 stsp repo_path = cwd;
4360 52185f70 2019-02-05 stsp if (repo_path == NULL) {
4361 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4362 52185f70 2019-02-05 stsp goto done;
4364 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
4365 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
4366 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
4367 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
4369 ffd1d5e5 2018-06-23 stsp usage_log();
4371 a915003a 2019-02-05 stsp init_curses();
4373 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
4374 c02c541e 2019-03-29 stsp if (error != NULL)
4375 52185f70 2019-02-05 stsp goto done;
4377 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4378 c02c541e 2019-03-29 stsp if (error)
4379 52185f70 2019-02-05 stsp goto done;
4381 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
4382 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4384 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
4385 15a94983 2018-12-23 stsp commit_id_arg);
4386 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4387 ffd1d5e5 2018-06-23 stsp goto done;
4389 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4390 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4391 ffd1d5e5 2018-06-23 stsp goto done;
4393 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
4394 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
4395 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4396 8b473291 2019-02-21 stsp goto done;
4398 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
4399 8b473291 2019-02-21 stsp if (error)
4400 ffd1d5e5 2018-06-23 stsp goto done;
4402 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4403 5221c383 2018-08-01 stsp if (view == NULL) {
4404 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4405 5221c383 2018-08-01 stsp goto done;
4407 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
4408 ad80ab7b 2018-08-04 stsp if (error)
4409 ad80ab7b 2018-08-04 stsp goto done;
4410 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4412 52185f70 2019-02-05 stsp free(repo_path);
4413 ffd1d5e5 2018-06-23 stsp free(commit_id);
4414 ffd1d5e5 2018-06-23 stsp if (commit)
4415 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
4417 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
4419 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
4420 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4421 ffd1d5e5 2018-06-23 stsp return error;
4424 4ed7e80c 2018-05-20 stsp __dead static void
4425 9f7d7167 2018-04-29 stsp usage(void)
4427 5e070240 2019-06-22 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n", getprogname());
4431 c2301be8 2018-04-30 stsp static char **
4432 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
4434 c2301be8 2018-04-30 stsp char **argv;
4435 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
4437 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
4438 c2301be8 2018-04-30 stsp if (argv == NULL)
4439 c2301be8 2018-04-30 stsp err(1, "calloc");
4440 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
4441 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
4442 c2301be8 2018-04-30 stsp err(1, "calloc");
4443 c2301be8 2018-04-30 stsp if (arg1) {
4444 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
4445 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
4446 c2301be8 2018-04-30 stsp err(1, "calloc");
4449 c2301be8 2018-04-30 stsp return argv;
4453 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
4455 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
4456 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
4457 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
4458 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
4460 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
4462 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
4463 9f7d7167 2018-04-29 stsp switch (ch) {
4465 9f7d7167 2018-04-29 stsp hflag = 1;
4469 9f7d7167 2018-04-29 stsp /* NOTREACHED */
4473 9f7d7167 2018-04-29 stsp argc -= optind;
4474 9f7d7167 2018-04-29 stsp argv += optind;
4475 9f7d7167 2018-04-29 stsp optind = 0;
4476 c2301be8 2018-04-30 stsp optreset = 1;
4478 c2301be8 2018-04-30 stsp if (argc == 0) {
4479 f29d3e89 2018-06-23 stsp if (hflag)
4481 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
4482 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
4483 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
4488 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
4489 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
4490 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
4491 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
4492 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
4493 9f7d7167 2018-04-29 stsp if (hflag)
4494 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
4498 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
4499 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
4500 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
4501 c2301be8 2018-04-30 stsp if (repo_path) {
4502 c2301be8 2018-04-30 stsp struct got_repository *repo;
4503 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
4504 c2301be8 2018-04-30 stsp if (error == NULL)
4505 c2301be8 2018-04-30 stsp got_repo_close(repo);
4507 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath",
4509 c2301be8 2018-04-30 stsp if (error) {
4510 f29d3e89 2018-06-23 stsp if (hflag) {
4511 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
4512 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
4516 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
4517 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
4518 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
4519 ad7de8d9 2018-04-30 stsp free(repo_path);
4522 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
4523 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
4525 c2301be8 2018-04-30 stsp free(repo_path);
4529 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4530 9f7d7167 2018-04-29 stsp if (error)
4531 9f7d7167 2018-04-29 stsp goto done;
4534 c2301be8 2018-04-30 stsp free(cmd_argv);
4535 9f7d7167 2018-04-29 stsp if (error)
4536 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);