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 d01904d4 2019-06-25 stsp const char *head_ref_name;
152 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
153 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
154 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
155 1a76625f 2018-10-22 stsp sig_atomic_t quit;
156 1a76625f 2018-10-22 stsp pthread_t thread;
157 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
158 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
161 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
162 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
163 e9424729 2018-08-04 stsp int nlines;
165 e9424729 2018-08-04 stsp struct tog_view *view;
166 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
170 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
171 e9424729 2018-08-04 stsp const char *path;
172 e9424729 2018-08-04 stsp struct got_repository *repo;
173 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
174 e9424729 2018-08-04 stsp int *complete;
177 e9424729 2018-08-04 stsp struct tog_blame {
179 e9424729 2018-08-04 stsp size_t filesize;
180 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
181 6fcac457 2018-11-19 stsp int nlines;
182 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
183 e9424729 2018-08-04 stsp pthread_t thread;
184 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
185 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
186 e9424729 2018-08-04 stsp const char *path;
189 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
190 7cbe629d 2018-08-04 stsp int first_displayed_line;
191 7cbe629d 2018-08-04 stsp int last_displayed_line;
192 7cbe629d 2018-08-04 stsp int selected_line;
193 7cbe629d 2018-08-04 stsp int blame_complete;
196 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
197 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
198 e5a0f69f 2018-08-18 stsp char *path;
199 7cbe629d 2018-08-04 stsp struct got_repository *repo;
200 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
201 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
202 e9424729 2018-08-04 stsp struct tog_blame blame;
203 6c4c42e0 2019-06-24 stsp int matched_line;
206 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
207 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
208 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
209 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
210 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
211 ad80ab7b 2018-08-04 stsp int selected;
214 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
216 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
217 ad80ab7b 2018-08-04 stsp char *tree_label;
218 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
219 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
220 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
221 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
222 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
223 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
224 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
225 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
226 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
227 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
228 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
229 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
233 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
235 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
236 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
237 669b5ffa 2018-10-07 stsp * there is enough screen estate.
239 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
240 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
242 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
243 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
244 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
246 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
247 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
249 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
251 cc3c9aac 2018-08-01 stsp struct tog_view {
252 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
253 26ed57b2 2018-05-19 stsp WINDOW *window;
254 26ed57b2 2018-05-19 stsp PANEL *panel;
255 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
256 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
257 1004088d 2018-09-29 stsp int focussed;
258 669b5ffa 2018-10-07 stsp struct tog_view *parent;
259 669b5ffa 2018-10-07 stsp struct tog_view *child;
260 669b5ffa 2018-10-07 stsp int child_focussed;
262 5dc9f4bc 2018-08-04 stsp /* type-specific state */
263 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
265 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
266 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
267 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
268 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
271 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
272 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
273 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
274 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
276 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
277 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
278 60493ae3 2019-06-20 stsp int searching;
279 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
280 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
281 60493ae3 2019-06-20 stsp int search_next_done;
282 1803e47f 2019-06-22 stsp regex_t regex;
285 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
286 fb872ab2 2019-02-21 stsp struct got_object_id *, struct got_object_id *, struct tog_view *,
287 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
288 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
289 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
290 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
291 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
293 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
294 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *,
295 d01904d4 2019-06-25 stsp struct got_repository *, const char *, const char *, int);
296 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
297 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
298 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
299 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
300 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
301 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
303 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
304 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *, struct got_repository *);
305 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
306 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
307 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
308 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
309 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
310 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
312 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
313 8b473291 2019-02-21 stsp struct got_tree_object *, struct got_object_id *,
314 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
315 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
316 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
317 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
318 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
319 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
320 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
322 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
324 25791caa 2018-10-24 stsp static void
325 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
327 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
330 e5a0f69f 2018-08-18 stsp static const struct got_error *
331 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
333 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
335 669b5ffa 2018-10-07 stsp if (view->child) {
336 669b5ffa 2018-10-07 stsp view_close(view->child);
337 669b5ffa 2018-10-07 stsp view->child = NULL;
339 e5a0f69f 2018-08-18 stsp if (view->close)
340 e5a0f69f 2018-08-18 stsp err = view->close(view);
341 ea5e7bb5 2018-08-01 stsp if (view->panel)
342 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
343 ea5e7bb5 2018-08-01 stsp if (view->window)
344 ea5e7bb5 2018-08-01 stsp delwin(view->window);
345 ea5e7bb5 2018-08-01 stsp free(view);
346 e5a0f69f 2018-08-18 stsp return err;
349 ea5e7bb5 2018-08-01 stsp static struct tog_view *
350 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
351 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
353 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
355 ea5e7bb5 2018-08-01 stsp if (view == NULL)
356 ea5e7bb5 2018-08-01 stsp return NULL;
358 d6b05b5b 2018-08-04 stsp view->type = type;
359 f7d12f7e 2018-08-01 stsp view->lines = LINES;
360 f7d12f7e 2018-08-01 stsp view->cols = COLS;
361 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
362 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
363 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
364 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
365 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
366 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
367 96a765a8 2018-08-04 stsp view_close(view);
368 ea5e7bb5 2018-08-01 stsp return NULL;
370 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
371 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
372 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
373 96a765a8 2018-08-04 stsp view_close(view);
374 ea5e7bb5 2018-08-01 stsp return NULL;
377 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
378 ea5e7bb5 2018-08-01 stsp return view;
382 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
384 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
386 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
389 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
391 5c60c32a 2018-10-18 stsp static const struct got_error *
392 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
394 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
396 5c60c32a 2018-10-18 stsp view->begin_y = 0;
397 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
398 5c60c32a 2018-10-18 stsp view->nlines = LINES;
399 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
400 5c60c32a 2018-10-18 stsp view->lines = LINES;
401 5c60c32a 2018-10-18 stsp view->cols = COLS;
402 5c60c32a 2018-10-18 stsp err = view_resize(view);
404 5c60c32a 2018-10-18 stsp return err;
406 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
407 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
409 5c60c32a 2018-10-18 stsp return NULL;
412 5c60c32a 2018-10-18 stsp static const struct got_error *
413 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
415 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
417 5c60c32a 2018-10-18 stsp view->begin_x = 0;
418 5c60c32a 2018-10-18 stsp view->begin_y = 0;
419 5c60c32a 2018-10-18 stsp view->nlines = LINES;
420 5c60c32a 2018-10-18 stsp view->ncols = COLS;
421 5c60c32a 2018-10-18 stsp view->lines = LINES;
422 5c60c32a 2018-10-18 stsp view->cols = COLS;
423 5c60c32a 2018-10-18 stsp err = view_resize(view);
425 5c60c32a 2018-10-18 stsp return err;
427 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
428 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
430 5c60c32a 2018-10-18 stsp return NULL;
434 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
436 5c60c32a 2018-10-18 stsp return view->parent == NULL;
439 4d8c2215 2018-08-19 stsp static const struct got_error *
440 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
442 f7d12f7e 2018-08-01 stsp int nlines, ncols;
444 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
445 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
447 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
449 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
450 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
452 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
454 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
455 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
456 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
457 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
458 25791caa 2018-10-24 stsp wclear(view->window);
460 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
461 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
462 0cf4efb1 2018-09-29 stsp view->lines = LINES;
463 0cf4efb1 2018-09-29 stsp view->cols = COLS;
465 6e3e5d9c 2018-10-18 stsp if (view->child) {
466 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
467 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
468 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
469 5c60c32a 2018-10-18 stsp if (view->child->focussed)
470 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
472 5c60c32a 2018-10-18 stsp show_panel(view->panel);
474 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
475 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
479 5c60c32a 2018-10-18 stsp return NULL;
482 669b5ffa 2018-10-07 stsp static const struct got_error *
483 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
485 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
487 669b5ffa 2018-10-07 stsp if (view->child == NULL)
488 669b5ffa 2018-10-07 stsp return NULL;
490 669b5ffa 2018-10-07 stsp err = view_close(view->child);
491 669b5ffa 2018-10-07 stsp view->child = NULL;
492 669b5ffa 2018-10-07 stsp return err;
495 669b5ffa 2018-10-07 stsp static const struct got_error *
496 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
498 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
500 669b5ffa 2018-10-07 stsp view->child = child;
501 669b5ffa 2018-10-07 stsp child->parent = view;
502 669b5ffa 2018-10-07 stsp return err;
506 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
508 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
511 34bc9ec9 2019-02-22 stsp static void
512 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
514 25791caa 2018-10-24 stsp int cols, lines;
515 25791caa 2018-10-24 stsp struct winsize size;
517 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
518 25791caa 2018-10-24 stsp cols = 80; /* Default */
519 25791caa 2018-10-24 stsp lines = 24;
521 25791caa 2018-10-24 stsp cols = size.ws_col;
522 25791caa 2018-10-24 stsp lines = size.ws_row;
524 25791caa 2018-10-24 stsp resize_term(lines, cols);
527 2b49a8ae 2019-06-22 stsp static const struct got_error *
528 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
530 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
531 2b49a8ae 2019-06-22 stsp char pattern[1024];
534 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
535 2b49a8ae 2019-06-22 stsp return NULL;
537 2b49a8ae 2019-06-22 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1,
538 2b49a8ae 2019-06-22 stsp view->begin_x, "/");
539 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
541 2b49a8ae 2019-06-22 stsp nocbreak();
543 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
546 2b49a8ae 2019-06-22 stsp if (ret == ERR)
547 2b49a8ae 2019-06-22 stsp return NULL;
549 2b49a8ae 2019-06-22 stsp if (view->searching) {
550 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
551 2b49a8ae 2019-06-22 stsp view->searching = 0;
554 2b49a8ae 2019-06-22 stsp if (regcomp(&view->regex, pattern,
555 f5daf9b1 2019-06-24 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
556 7c32bd05 2019-06-22 stsp err = view->search_start(view);
558 7c32bd05 2019-06-22 stsp regfree(&view->regex);
559 7c32bd05 2019-06-22 stsp return err;
561 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
562 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
563 2b49a8ae 2019-06-22 stsp view->search_next(view);
566 2b49a8ae 2019-06-22 stsp return NULL;
569 0cf4efb1 2018-09-29 stsp static const struct got_error *
570 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
571 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
572 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
574 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
575 669b5ffa 2018-10-07 stsp struct tog_view *v;
576 1a76625f 2018-10-22 stsp int ch, errcode;
578 e5a0f69f 2018-08-18 stsp *new = NULL;
579 e5a0f69f 2018-08-18 stsp *dead = NULL;
580 0cf4efb1 2018-09-29 stsp *focus = NULL;
582 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
583 60493ae3 2019-06-20 stsp errcode = pthread_mutex_unlock(&tog_mutex);
584 60493ae3 2019-06-20 stsp if (errcode)
585 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
586 60493ae3 2019-06-20 stsp "pthread_mutex_unlock");
587 60493ae3 2019-06-20 stsp pthread_yield();
588 60493ae3 2019-06-20 stsp errcode = pthread_mutex_lock(&tog_mutex);
589 60493ae3 2019-06-20 stsp if (errcode)
590 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
591 60493ae3 2019-06-20 stsp "pthread_mutex_lock");
592 60493ae3 2019-06-20 stsp view->search_next(view);
593 60493ae3 2019-06-20 stsp return NULL;
596 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
597 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
598 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
599 1a76625f 2018-10-22 stsp if (errcode)
600 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
601 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
602 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
603 1a76625f 2018-10-22 stsp if (errcode)
604 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
605 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
607 25791caa 2018-10-24 stsp if (tog_sigwinch_received) {
608 25791caa 2018-10-24 stsp tog_resizeterm();
609 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
610 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
611 25791caa 2018-10-24 stsp err = view_resize(v);
613 25791caa 2018-10-24 stsp return err;
614 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
616 25791caa 2018-10-24 stsp return err;
620 e5a0f69f 2018-08-18 stsp switch (ch) {
624 1e37a5c2 2019-05-12 jcs if (view->child) {
625 1e37a5c2 2019-05-12 jcs *focus = view->child;
626 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
627 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
628 1e37a5c2 2019-05-12 jcs *focus = view->parent;
629 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
633 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
634 1e37a5c2 2019-05-12 jcs *dead = view;
640 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
641 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
643 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
644 669b5ffa 2018-10-07 stsp *focus = view->child;
645 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
646 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
648 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
651 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
652 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
654 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
655 1e37a5c2 2019-05-12 jcs *focus = view;
656 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
657 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
659 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
663 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
664 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
667 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
670 60493ae3 2019-06-20 stsp if (view->search_start)
671 2b49a8ae 2019-06-22 stsp view_search_start(view);
673 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
677 60493ae3 2019-06-20 stsp if (view->search_next && view->searching) {
678 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
679 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
680 60493ae3 2019-06-20 stsp view->search_next_done = 0;
681 60493ae3 2019-06-20 stsp view->search_next(view);
683 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
686 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
690 e5a0f69f 2018-08-18 stsp return err;
694 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
696 0cf4efb1 2018-09-29 stsp PANEL *panel;
697 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
699 669b5ffa 2018-10-07 stsp if (view->parent)
700 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
702 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
703 0cf4efb1 2018-09-29 stsp if (panel == NULL)
706 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
707 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
708 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
712 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
714 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
715 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
717 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
719 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
722 669b5ffa 2018-10-07 stsp return view->focussed;
725 bcbd79e2 2018-08-19 stsp static const struct got_error *
726 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
728 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
729 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
730 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
731 fd823528 2018-10-22 stsp int fast_refresh = 10;
732 1a76625f 2018-10-22 stsp int done = 0, errcode;
734 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
735 1a76625f 2018-10-22 stsp if (errcode)
736 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
738 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
739 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
741 a81bf10d 2018-09-29 stsp main_view = view;
742 1004088d 2018-09-29 stsp view->focussed = 1;
743 878940b7 2018-09-29 stsp err = view->show(view);
745 0cf4efb1 2018-09-29 stsp return err;
746 0cf4efb1 2018-09-29 stsp update_panels();
747 0cf4efb1 2018-09-29 stsp doupdate();
748 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
749 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
750 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
751 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
753 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
754 e4197bf9 2018-08-18 stsp view, &views);
757 e5a0f69f 2018-08-18 stsp if (dead_view) {
758 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
760 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
761 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
762 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
763 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
764 669b5ffa 2018-10-07 stsp prev = view->parent;
766 669b5ffa 2018-10-07 stsp if (dead_view->parent)
767 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
769 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
771 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
772 d01904d4 2019-06-25 stsp if (err || (dead_view == main_view && new_view == NULL))
775 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
776 0cf4efb1 2018-09-29 stsp if (focus_view)
777 0cf4efb1 2018-09-29 stsp view = focus_view;
778 669b5ffa 2018-10-07 stsp else if (prev)
779 669b5ffa 2018-10-07 stsp view = prev;
780 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
781 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
782 0cf4efb1 2018-09-29 stsp tog_view_list_head);
784 0cf4efb1 2018-09-29 stsp view = NULL;
785 669b5ffa 2018-10-07 stsp if (view) {
786 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
787 669b5ffa 2018-10-07 stsp focus_view = view->child;
789 669b5ffa 2018-10-07 stsp focus_view = view;
793 bcbd79e2 2018-08-19 stsp if (new_view) {
794 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
795 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
796 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
797 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
799 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
800 86c66b02 2018-10-18 stsp err = view_close(v);
805 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
806 fed7eaa8 2018-10-24 stsp view = new_view;
807 878940b7 2018-09-29 stsp if (focus_view == NULL)
808 878940b7 2018-09-29 stsp focus_view = new_view;
810 0cf4efb1 2018-09-29 stsp if (focus_view) {
811 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
813 0cf4efb1 2018-09-29 stsp view->focussed = 0;
814 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
815 0cf4efb1 2018-09-29 stsp view = focus_view;
816 878940b7 2018-09-29 stsp if (new_view)
817 878940b7 2018-09-29 stsp show_panel(new_view->panel);
818 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
819 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
821 669b5ffa 2018-10-07 stsp if (view) {
822 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
823 758194b5 2018-10-24 stsp view->focussed = 1;
824 758194b5 2018-10-24 stsp show_panel(view->panel);
825 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
826 758194b5 2018-10-24 stsp show_panel(view->child->panel);
827 1a76625f 2018-10-22 stsp focus_view = view;
829 669b5ffa 2018-10-07 stsp if (view->parent) {
830 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
834 669b5ffa 2018-10-07 stsp err = view->show(view);
837 669b5ffa 2018-10-07 stsp if (view->child) {
838 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
842 1a76625f 2018-10-22 stsp update_panels();
843 1a76625f 2018-10-22 stsp doupdate();
847 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
848 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
849 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
850 e5a0f69f 2018-08-18 stsp view_close(view);
853 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
854 1a76625f 2018-10-22 stsp if (errcode)
855 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
857 e5a0f69f 2018-08-18 stsp return err;
860 4ed7e80c 2018-05-20 stsp __dead static void
861 9f7d7167 2018-04-29 stsp usage_log(void)
864 c70c5802 2018-08-01 stsp fprintf(stderr,
865 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
866 9f7d7167 2018-04-29 stsp getprogname());
870 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
871 80ddbec8 2018-04-29 stsp static const struct got_error *
872 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
874 00dfcb92 2018-06-11 stsp char *vis = NULL;
875 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
877 963b370f 2018-05-20 stsp *ws = NULL;
878 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
879 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
880 00dfcb92 2018-06-11 stsp int vislen;
881 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
882 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
884 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
885 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
887 00dfcb92 2018-06-11 stsp return err;
888 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
889 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
890 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
895 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
896 a7f50699 2018-06-11 stsp if (*ws == NULL) {
897 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
901 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
902 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
907 963b370f 2018-05-20 stsp *ws = NULL;
910 963b370f 2018-05-20 stsp return err;
913 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
914 963b370f 2018-05-20 stsp static const struct got_error *
915 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
917 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
918 963b370f 2018-05-20 stsp int cols = 0;
919 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
920 963b370f 2018-05-20 stsp size_t wlen;
923 963b370f 2018-05-20 stsp *wlinep = NULL;
924 b700b5d6 2018-07-10 stsp *widthp = 0;
926 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
928 963b370f 2018-05-20 stsp return err;
931 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
932 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
933 963b370f 2018-05-20 stsp switch (width) {
939 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
940 b700b5d6 2018-07-10 stsp cols += width;
944 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
945 67ceb59d 2019-03-03 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
949 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
953 963b370f 2018-05-20 stsp wline[i] = L'\0';
954 b700b5d6 2018-07-10 stsp if (widthp)
955 b700b5d6 2018-07-10 stsp *widthp = cols;
958 963b370f 2018-05-20 stsp free(wline);
960 963b370f 2018-05-20 stsp *wlinep = wline;
961 963b370f 2018-05-20 stsp return err;
964 8b473291 2019-02-21 stsp static const struct got_error*
965 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
966 8b473291 2019-02-21 stsp struct got_object_id *id)
968 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
969 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
971 8b473291 2019-02-21 stsp const char *name;
973 8b473291 2019-02-21 stsp *refs_str = NULL;
975 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
976 8b473291 2019-02-21 stsp if (got_object_id_cmp(re->id, id) != 0)
978 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
979 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
981 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
983 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
985 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
987 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
989 8b473291 2019-02-21 stsp s = *refs_str;
990 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
991 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
992 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
994 8b473291 2019-02-21 stsp *refs_str = NULL;
1000 8b473291 2019-02-21 stsp return err;
1003 963b370f 2018-05-20 stsp static const struct got_error *
1004 5813d178 2019-03-09 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1006 5813d178 2019-03-09 stsp char *smallerthan, *at;
1008 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1009 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1010 5813d178 2019-03-09 stsp author = smallerthan + 1;
1011 5813d178 2019-03-09 stsp at = strchr(author, '@');
1013 5813d178 2019-03-09 stsp *at = '\0';
1014 5813d178 2019-03-09 stsp return format_line(wauthor, author_width, author, limit);
1017 5813d178 2019-03-09 stsp static const struct got_error *
1018 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1019 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
1020 5813d178 2019-03-09 stsp int author_display_cols)
1022 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1023 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1024 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1025 5813d178 2019-03-09 stsp char *author = NULL;
1026 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1027 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1028 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1029 bb737323 2018-05-20 stsp int col, limit;
1030 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
1031 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1032 ccb26ccd 2018-11-05 stsp struct tm tm;
1033 45d799e2 2018-12-23 stsp time_t committer_time;
1035 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1036 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1037 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1038 ccb26ccd 2018-11-05 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1039 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1040 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1042 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
1043 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1045 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1046 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1047 b39d25c7 2018-07-10 stsp col = limit + 1;
1048 b39d25c7 2018-07-10 stsp if (col > avail)
1049 b39d25c7 2018-07-10 stsp goto done;
1051 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1052 5813d178 2019-03-09 stsp if (author == NULL) {
1053 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1054 80ddbec8 2018-04-29 stsp goto done;
1056 5813d178 2019-03-09 stsp err = format_author(&wauthor, &author_width, author, avail - col);
1058 bb737323 2018-05-20 stsp goto done;
1059 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1060 bb737323 2018-05-20 stsp col += author_width;
1061 5813d178 2019-03-09 stsp while (col <= avail && author_width < author_display_cols + 2) {
1062 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1064 bb737323 2018-05-20 stsp author_width++;
1066 9c2eaf34 2018-05-20 stsp if (col > avail)
1067 9c2eaf34 2018-05-20 stsp goto done;
1069 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1070 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
1071 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1072 6d9fbc00 2018-04-29 stsp goto done;
1074 bb737323 2018-05-20 stsp logmsg = logmsg0;
1075 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1077 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1078 bb737323 2018-05-20 stsp if (newline)
1079 bb737323 2018-05-20 stsp *newline = '\0';
1080 bb737323 2018-05-20 stsp limit = avail - col;
1081 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1083 bb737323 2018-05-20 stsp goto done;
1084 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1085 bb737323 2018-05-20 stsp col += logmsg_width;
1086 bb737323 2018-05-20 stsp while (col <= avail) {
1087 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1091 80ddbec8 2018-04-29 stsp free(logmsg0);
1092 bb737323 2018-05-20 stsp free(wlogmsg);
1093 5813d178 2019-03-09 stsp free(author);
1094 bb737323 2018-05-20 stsp free(wauthor);
1095 80ddbec8 2018-04-29 stsp free(line);
1096 80ddbec8 2018-04-29 stsp return err;
1099 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1100 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1101 899d86c2 2018-05-10 stsp struct got_object_id *id)
1103 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1105 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1106 80ddbec8 2018-04-29 stsp if (entry == NULL)
1107 899d86c2 2018-05-10 stsp return NULL;
1109 899d86c2 2018-05-10 stsp entry->id = id;
1110 99db9666 2018-05-07 stsp entry->commit = commit;
1111 899d86c2 2018-05-10 stsp return entry;
1114 99db9666 2018-05-07 stsp static void
1115 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1117 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1119 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1120 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1121 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1122 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1123 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1124 99db9666 2018-05-07 stsp free(entry);
1127 99db9666 2018-05-07 stsp static void
1128 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1130 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1131 99db9666 2018-05-07 stsp pop_commit(commits);
1134 c4972b91 2018-05-07 stsp static const struct got_error *
1135 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1136 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
1138 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1139 93e45b7c 2018-09-24 stsp int nqueued = 0;
1142 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1143 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1144 1a76625f 2018-10-22 stsp * while updating the display.
1146 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
1147 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1148 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1149 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1150 1a76625f 2018-10-22 stsp int errcode;
1152 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1153 9ba79e04 2018-06-11 stsp if (err) {
1154 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1156 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
1157 93e45b7c 2018-09-24 stsp minqueue, repo);
1159 ecb28ae0 2018-07-16 stsp return err;
1163 ecb28ae0 2018-07-16 stsp if (id == NULL)
1166 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1169 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1170 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1171 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1175 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1176 1a76625f 2018-10-22 stsp if (errcode) {
1177 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_lock");
1181 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1182 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1183 ecb28ae0 2018-07-16 stsp nqueued++;
1184 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1186 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1187 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1188 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1189 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1192 9ba79e04 2018-06-11 stsp return err;
1195 99db9666 2018-05-07 stsp static const struct got_error *
1196 19e70ad6 2019-05-14 stsp get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1197 19e70ad6 2019-05-14 stsp struct got_repository *repo)
1199 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1200 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1202 9ba79e04 2018-06-11 stsp *head_id = NULL;
1204 19e70ad6 2019-05-14 stsp err = got_ref_open(&head_ref, repo, branch_name, 0);
1206 99db9666 2018-05-07 stsp return err;
1208 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1209 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1210 9ba79e04 2018-06-11 stsp if (err) {
1211 9ba79e04 2018-06-11 stsp *head_id = NULL;
1212 99db9666 2018-05-07 stsp return err;
1215 9ba79e04 2018-06-11 stsp return NULL;
1218 0553a4e3 2018-04-30 stsp static const struct got_error *
1219 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1220 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1221 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1222 8b473291 2019-02-21 stsp struct got_reflist_head *refs, const char *path, int commits_needed)
1224 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1225 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1226 60493ae3 2019-06-20 stsp int width;
1227 60493ae3 2019-06-20 stsp int ncommits, author_cols = 10;
1228 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1229 8b473291 2019-02-21 stsp char *refs_str = NULL;
1230 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1232 e0d42f60 2018-07-22 stsp entry = first;
1233 e0d42f60 2018-07-22 stsp ncommits = 0;
1234 e0d42f60 2018-07-22 stsp while (entry) {
1235 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1236 e0d42f60 2018-07-22 stsp *selected = entry;
1239 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1240 e0d42f60 2018-07-22 stsp ncommits++;
1243 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1244 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1246 ecb28ae0 2018-07-16 stsp return err;
1247 8b473291 2019-02-21 stsp if (refs) {
1248 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, (*selected)->id);
1250 8b473291 2019-02-21 stsp goto done;
1254 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1255 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1257 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1258 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1259 60493ae3 2019-06-20 stsp commits_needed > 0 ?
1260 60493ae3 2019-06-20 stsp (view->searching && view->search_next_done == 0
1261 60493ae3 2019-06-20 stsp ? "searching..." : "loading... ") :
1262 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1263 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1264 8b473291 2019-02-21 stsp goto done;
1267 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1268 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1269 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1270 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1271 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1272 1a76625f 2018-10-22 stsp header = NULL;
1273 1a76625f 2018-10-22 stsp goto done;
1275 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1276 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1277 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1278 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1279 1a76625f 2018-10-22 stsp header = NULL;
1280 1a76625f 2018-10-22 stsp goto done;
1282 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1284 1a76625f 2018-10-22 stsp goto done;
1286 2814baeb 2018-08-01 stsp werase(view->window);
1288 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1289 a3404814 2018-09-02 stsp wstandout(view->window);
1290 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1291 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1292 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1295 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1296 a3404814 2018-09-02 stsp wstandend(view->window);
1297 ecb28ae0 2018-07-16 stsp free(wline);
1298 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1299 1a76625f 2018-10-22 stsp goto done;
1301 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1302 899d86c2 2018-05-10 stsp entry = first;
1303 5813d178 2019-03-09 stsp ncommits = 0;
1304 5813d178 2019-03-09 stsp while (entry) {
1305 5813d178 2019-03-09 stsp char *author;
1306 5813d178 2019-03-09 stsp wchar_t *wauthor;
1307 5813d178 2019-03-09 stsp int width;
1308 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1310 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1311 5813d178 2019-03-09 stsp if (author == NULL) {
1312 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1313 5813d178 2019-03-09 stsp goto done;
1315 5813d178 2019-03-09 stsp err = format_author(&wauthor, &width, author, COLS);
1316 5813d178 2019-03-09 stsp if (author_cols < width)
1317 5813d178 2019-03-09 stsp author_cols = width;
1318 5813d178 2019-03-09 stsp free(wauthor);
1319 5813d178 2019-03-09 stsp free(author);
1320 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1323 5813d178 2019-03-09 stsp entry = first;
1324 899d86c2 2018-05-10 stsp *last = first;
1325 867c6645 2018-07-10 stsp ncommits = 0;
1326 899d86c2 2018-05-10 stsp while (entry) {
1327 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1329 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1330 2814baeb 2018-08-01 stsp wstandout(view->window);
1331 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1332 5813d178 2019-03-09 stsp author_cols);
1333 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1334 2814baeb 2018-08-01 stsp wstandend(view->window);
1336 60493ae3 2019-06-20 stsp goto done;
1337 0553a4e3 2018-04-30 stsp ncommits++;
1338 899d86c2 2018-05-10 stsp *last = entry;
1339 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1342 1a57306a 2018-09-02 stsp view_vborder(view);
1344 1a76625f 2018-10-22 stsp free(id_str);
1345 8b473291 2019-02-21 stsp free(refs_str);
1346 1a76625f 2018-10-22 stsp free(ncommits_str);
1347 1a76625f 2018-10-22 stsp free(header);
1348 80ddbec8 2018-04-29 stsp return err;
1351 07b55e75 2018-05-10 stsp static void
1352 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1353 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1354 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1356 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1357 07b55e75 2018-05-10 stsp int nscrolled = 0;
1359 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1360 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1363 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1364 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1365 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1366 07b55e75 2018-05-10 stsp if (entry) {
1367 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1368 07b55e75 2018-05-10 stsp nscrolled++;
1373 aa075928 2018-05-10 stsp static const struct got_error *
1374 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1375 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1377 5e224a3e 2019-02-22 stsp int errcode;
1378 8a42fca8 2019-02-22 stsp int max_wait = 20;
1380 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1382 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1383 5e224a3e 2019-02-22 stsp if (*log_complete)
1386 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1387 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1388 7aafa0d1 2019-02-22 stsp if (errcode)
1389 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1390 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1391 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1392 7aafa0d1 2019-02-22 stsp if (errcode)
1393 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1394 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1395 7aafa0d1 2019-02-22 stsp pthread_yield();
1396 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1397 7aafa0d1 2019-02-22 stsp if (errcode)
1398 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1399 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1401 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1403 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1404 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1405 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1406 5e224a3e 2019-02-22 stsp * with few commits in history.
1408 7aafa0d1 2019-02-22 stsp return NULL;
1412 5e224a3e 2019-02-22 stsp return NULL;
1415 5e224a3e 2019-02-22 stsp static const struct got_error *
1416 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1417 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1418 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1419 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1420 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1422 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1423 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1424 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1426 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1427 5e224a3e 2019-02-22 stsp return NULL;
1429 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1430 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1432 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1433 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1435 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1436 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1437 5e224a3e 2019-02-22 stsp need_commits);
1439 5e224a3e 2019-02-22 stsp return err;
1443 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1444 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1447 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1449 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1450 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1452 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1453 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1455 dd0a52c1 2018-05-20 stsp return err;
1458 cd0acaa7 2018-05-20 stsp static const struct got_error *
1459 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1460 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1461 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1462 8b473291 2019-02-21 stsp struct got_repository *repo)
1464 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1465 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1466 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1468 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1469 15a94983 2018-12-23 stsp if (diff_view == NULL)
1470 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1472 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1473 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1474 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1475 e5a0f69f 2018-08-18 stsp if (err == NULL)
1476 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1477 cd0acaa7 2018-05-20 stsp return err;
1480 80ddbec8 2018-04-29 stsp static const struct got_error *
1481 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1482 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1484 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1486 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1487 941e9f74 2019-05-21 stsp if (parent == NULL)
1488 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1490 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1491 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1492 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1493 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1494 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1495 941e9f74 2019-05-21 stsp s->tree = subtree;
1496 941e9f74 2019-05-21 stsp s->entries = got_object_tree_get_entries(s->tree);
1497 941e9f74 2019-05-21 stsp s->selected = 0;
1498 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1499 941e9f74 2019-05-21 stsp return NULL;
1503 941e9f74 2019-05-21 stsp static const struct got_error *
1504 941e9f74 2019-05-21 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1505 941e9f74 2019-05-21 stsp struct commit_queue_entry *entry, const char *path,
1506 941e9f74 2019-05-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
1508 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1509 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1510 941e9f74 2019-05-21 stsp struct got_tree_entry *te;
1511 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s;
1512 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1513 b03c880f 2019-05-21 stsp char *slash, *subpath = NULL;
1514 941e9f74 2019-05-21 stsp const char *p;
1516 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1517 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1519 9343a5fb 2018-06-23 stsp return err;
1521 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1522 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1523 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1525 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1526 941e9f74 2019-05-21 stsp if (err) {
1527 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1528 941e9f74 2019-05-21 stsp return err;
1530 941e9f74 2019-05-21 stsp s = &tree_view->state.tree;
1532 941e9f74 2019-05-21 stsp *new_view = tree_view;
1534 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1536 941e9f74 2019-05-21 stsp while (*p) {
1537 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1539 941e9f74 2019-05-21 stsp while (p[0] == '/')
1542 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1543 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1544 941e9f74 2019-05-21 stsp if (slash == NULL)
1545 941e9f74 2019-05-21 stsp slash = strchr(p, '\0');
1546 941e9f74 2019-05-21 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1547 941e9f74 2019-05-21 stsp if (strncmp(p, te->name, slash - p) == 0) {
1548 941e9f74 2019-05-21 stsp s->selected_entry = te;
1551 b03c880f 2019-05-21 stsp s->selected++;
1553 941e9f74 2019-05-21 stsp if (s->selected_entry == NULL) {
1554 941e9f74 2019-05-21 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1557 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1558 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1560 67409a31 2019-05-24 stsp if (!S_ISDIR(s->selected_entry->mode)) {
1561 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1562 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1563 67409a31 2019-05-24 stsp s->selected = 0;
1567 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1568 941e9f74 2019-05-21 stsp if (slash)
1569 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1571 941e9f74 2019-05-21 stsp subpath = strdup(path);
1572 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1573 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1577 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1582 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1583 941e9f74 2019-05-21 stsp free(tree_id);
1587 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1588 941e9f74 2019-05-21 stsp if (err) {
1589 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1592 941e9f74 2019-05-21 stsp if (slash == NULL)
1594 941e9f74 2019-05-21 stsp free(subpath);
1595 941e9f74 2019-05-21 stsp subpath = NULL;
1596 941e9f74 2019-05-21 stsp p = slash;
1599 941e9f74 2019-05-21 stsp free(subpath);
1600 1a76625f 2018-10-22 stsp return err;
1603 1a76625f 2018-10-22 stsp static void *
1604 1a76625f 2018-10-22 stsp log_thread(void *arg)
1606 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1607 1a76625f 2018-10-22 stsp int errcode = 0;
1608 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1609 1a76625f 2018-10-22 stsp int done = 0;
1611 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1613 1a76625f 2018-10-22 stsp return (void *)err;
1615 1a76625f 2018-10-22 stsp while (!done && !err) {
1616 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1617 1a76625f 2018-10-22 stsp a->in_repo_path);
1618 1a76625f 2018-10-22 stsp if (err) {
1619 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1620 1a76625f 2018-10-22 stsp return (void *)err;
1621 1a76625f 2018-10-22 stsp err = NULL;
1623 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1624 1a76625f 2018-10-22 stsp a->commits_needed--;
1626 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1627 3abe8080 2019-04-10 stsp if (errcode) {
1628 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1629 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1631 3abe8080 2019-04-10 stsp } else if (*a->quit)
1633 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1634 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1635 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1636 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1640 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1641 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1642 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1643 1a76625f 2018-10-22 stsp &tog_mutex);
1644 1a76625f 2018-10-22 stsp if (errcode)
1645 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1646 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1649 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1650 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1651 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1652 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1654 3abe8080 2019-04-10 stsp a->log_complete = 1;
1655 1a76625f 2018-10-22 stsp return (void *)err;
1658 1a76625f 2018-10-22 stsp static const struct got_error *
1659 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1661 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1662 1a76625f 2018-10-22 stsp int errcode;
1664 1a76625f 2018-10-22 stsp if (s->thread) {
1665 1a76625f 2018-10-22 stsp s->quit = 1;
1666 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1667 1a76625f 2018-10-22 stsp if (errcode)
1668 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1669 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1670 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1671 1a76625f 2018-10-22 stsp if (errcode)
1672 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1673 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1674 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1675 1a76625f 2018-10-22 stsp if (errcode)
1676 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1677 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1678 1a76625f 2018-10-22 stsp if (errcode)
1679 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1680 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1681 1a76625f 2018-10-22 stsp s->thread = NULL;
1684 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1685 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1686 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1688 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1689 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1690 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1693 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1694 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1695 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1698 9343a5fb 2018-06-23 stsp return err;
1701 9343a5fb 2018-06-23 stsp static const struct got_error *
1702 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1704 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1705 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1707 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1708 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1709 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1710 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1711 1a76625f 2018-10-22 stsp free(s->start_id);
1712 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1713 1a76625f 2018-10-22 stsp return err;
1716 1a76625f 2018-10-22 stsp static const struct got_error *
1717 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
1719 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1721 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
1722 60493ae3 2019-06-20 stsp return NULL;
1725 60493ae3 2019-06-20 stsp static int
1726 df0b3d8a 2019-06-28 stsp match_commit(struct got_commit_object *commit, const char *id_str,
1727 df0b3d8a 2019-06-28 stsp regex_t *regex)
1729 60493ae3 2019-06-20 stsp regmatch_t regmatch;
1731 60493ae3 2019-06-20 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1732 60493ae3 2019-06-20 stsp ®match, 0) == 0 ||
1733 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1734 60493ae3 2019-06-20 stsp ®match, 0) == 0 ||
1735 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_logmsg(commit), 1,
1736 df0b3d8a 2019-06-28 stsp ®match, 0) == 0 ||
1737 df0b3d8a 2019-06-28 stsp regexec(regex, id_str, 1, ®match, 0) == 0)
1743 60493ae3 2019-06-20 stsp static const struct got_error *
1744 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
1746 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
1747 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1748 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
1750 60493ae3 2019-06-20 stsp if (!view->searching) {
1751 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1752 60493ae3 2019-06-20 stsp return NULL;
1755 b1bf1435 2019-06-21 stsp if (s->matched_entry) {
1756 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1757 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
1759 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
1760 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
1762 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1763 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
1765 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1768 60493ae3 2019-06-20 stsp while (1) {
1769 df0b3d8a 2019-06-28 stsp char *id_str;
1770 60493ae3 2019-06-20 stsp if (entry == NULL) {
1771 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
1772 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
1773 f801134a 2019-06-25 stsp view->search_next_done = 1;
1774 f801134a 2019-06-25 stsp return NULL;
1776 f801134a 2019-06-25 stsp s->thread_args.commits_needed = 1;
1777 f801134a 2019-06-25 stsp return trigger_log_thread(0,
1778 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
1779 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
1780 f801134a 2019-06-25 stsp &s->thread_args.need_commits);
1783 df0b3d8a 2019-06-28 stsp err = got_object_id_str(&id_str, entry->id);
1785 df0b3d8a 2019-06-28 stsp return err;
1787 df0b3d8a 2019-06-28 stsp if (match_commit(entry->commit, id_str, &view->regex)) {
1788 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1789 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
1790 df0b3d8a 2019-06-28 stsp free(id_str);
1793 df0b3d8a 2019-06-28 stsp free(id_str);
1794 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1795 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
1797 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1800 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
1801 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
1802 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
1803 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1805 60493ae3 2019-06-20 stsp return err;
1808 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
1809 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1811 60493ae3 2019-06-20 stsp return err;
1816 60493ae3 2019-06-20 stsp return NULL;
1819 60493ae3 2019-06-20 stsp static const struct got_error *
1820 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1821 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
1822 d01904d4 2019-06-25 stsp const char *head_ref_name, const char *path, int check_disk)
1824 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1825 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1826 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1827 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1828 1a76625f 2018-10-22 stsp int errcode;
1830 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1831 ecb28ae0 2018-07-16 stsp if (err != NULL)
1832 ecb28ae0 2018-07-16 stsp goto done;
1834 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1835 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1836 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1838 8b473291 2019-02-21 stsp s->refs = refs;
1839 fb2756b9 2018-08-04 stsp s->repo = repo;
1840 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
1841 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1842 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1843 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1844 5036bf37 2018-09-24 stsp goto done;
1847 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1848 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1849 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1850 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
1851 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
1853 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1855 1a76625f 2018-10-22 stsp goto done;
1856 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1857 1a76625f 2018-10-22 stsp 0, thread_repo);
1859 1a76625f 2018-10-22 stsp goto done;
1861 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1862 1a76625f 2018-10-22 stsp if (errcode) {
1863 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
1864 1a76625f 2018-10-22 stsp goto done;
1867 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1868 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1869 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1870 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1871 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1872 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1873 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1874 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1875 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1876 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1877 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1880 1a76625f 2018-10-22 stsp close_log_view(view);
1881 ba4f502b 2018-08-04 stsp return err;
1884 e5a0f69f 2018-08-18 stsp static const struct got_error *
1885 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1887 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1889 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1890 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1891 2b380cc8 2018-10-24 stsp &s->thread_args);
1892 2b380cc8 2018-10-24 stsp if (errcode)
1893 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
1896 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1897 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1898 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
1899 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1902 e5a0f69f 2018-08-18 stsp static const struct got_error *
1903 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1904 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1906 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1907 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1908 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
1909 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1910 669b5ffa 2018-10-07 stsp int begin_x = 0;
1911 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
1913 e5a0f69f 2018-08-18 stsp switch (ch) {
1915 1e37a5c2 2019-05-12 jcs s->quit = 1;
1918 1e37a5c2 2019-05-12 jcs case KEY_UP:
1921 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1923 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
1924 1e37a5c2 2019-05-12 jcs s->selected--;
1926 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
1927 1144d21a 2019-06-21 stsp &s->commits);
1929 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
1930 a4292ac5 2019-05-12 jcs case CTRL('b'):
1931 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1933 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
1934 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
1935 1e37a5c2 2019-05-12 jcs s->selected = 0;
1938 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
1939 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
1942 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
1945 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1947 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
1948 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1949 1e37a5c2 2019-05-12 jcs s->selected++;
1952 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
1953 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
1954 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
1955 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1956 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1958 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
1959 a4292ac5 2019-05-12 jcs case CTRL('f'): {
1960 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
1961 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
1962 1e37a5c2 2019-05-12 jcs if (first == NULL)
1964 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
1965 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
1966 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
1967 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1968 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1969 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
1970 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
1971 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1972 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
1973 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
1974 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
1976 1e37a5c2 2019-05-12 jcs err = NULL;
1979 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1980 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
1981 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
1982 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
1983 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
1985 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
1988 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
1990 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
1991 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
1992 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
1993 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
1994 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
1997 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1998 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2000 1e37a5c2 2019-05-12 jcs return err;
2001 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2003 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2006 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2007 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2009 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2012 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2014 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2015 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2016 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2017 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2020 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2021 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2023 1e37a5c2 2019-05-12 jcs return err;
2024 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2026 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2029 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2030 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2032 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2034 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2035 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2037 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2038 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2039 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2041 1e37a5c2 2019-05-12 jcs return err;
2042 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2043 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2044 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2045 638f9024 2019-05-13 stsp return got_error_from_errno(
2046 1e37a5c2 2019-05-12 jcs "view_open");
2047 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2048 d01904d4 2019-06-25 stsp s->repo, s->head_ref_name, parent_path, 0);
2050 1e37a5c2 2019-05-12 jcs return err;;
2051 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2052 1e37a5c2 2019-05-12 jcs *new_view = lv;
2054 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2055 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2057 1e37a5c2 2019-05-12 jcs return NULL;
2060 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2061 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2063 d01904d4 2019-06-25 stsp return err;
2064 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2065 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2066 d01904d4 2019-06-25 stsp if (lv == NULL)
2067 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2068 d01904d4 2019-06-25 stsp err = get_head_commit_id(&start_id, s->head_ref_name ?
2069 d01904d4 2019-06-25 stsp s->head_ref_name : GOT_REF_HEAD, s->repo);
2071 d01904d4 2019-06-25 stsp return err;
2072 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2073 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2074 d01904d4 2019-06-25 stsp free(start_id);
2075 d01904d4 2019-06-25 stsp return got_error_from_errno("strdup");
2077 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2078 d01904d4 2019-06-25 stsp s->head_ref_name, in_repo_path, 0);
2080 d01904d4 2019-06-25 stsp return err;;
2081 d01904d4 2019-06-25 stsp *dead_view = view;
2082 d01904d4 2019-06-25 stsp *new_view = lv;
2088 80ddbec8 2018-04-29 stsp return err;
2091 4ed7e80c 2018-05-20 stsp static const struct got_error *
2092 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2094 c2db6724 2019-01-04 stsp const struct got_error *error;
2096 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2097 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2099 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2100 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2102 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2103 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2105 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2106 c2db6724 2019-01-04 stsp if (error != NULL)
2107 c2db6724 2019-01-04 stsp return error;
2109 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2110 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2112 c2db6724 2019-01-04 stsp return NULL;
2115 a915003a 2019-02-05 stsp static void
2116 a915003a 2019-02-05 stsp init_curses(void)
2118 a915003a 2019-02-05 stsp initscr();
2120 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2123 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2124 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2125 a915003a 2019-02-05 stsp curs_set(0);
2126 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2129 c2db6724 2019-01-04 stsp static const struct got_error *
2130 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2132 80ddbec8 2018-04-29 stsp const struct got_error *error;
2133 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2134 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2135 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2136 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2137 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2138 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
2140 04cc582a 2018-08-01 stsp struct tog_view *view;
2142 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2144 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2145 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2146 c2db6724 2019-01-04 stsp NULL) == -1)
2147 80ddbec8 2018-04-29 stsp err(1, "pledge");
2150 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2151 80ddbec8 2018-04-29 stsp switch (ch) {
2153 80ddbec8 2018-04-29 stsp start_commit = optarg;
2156 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2157 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2158 ecb28ae0 2018-07-16 stsp err(1, "-r option");
2161 17020d27 2019-03-07 stsp usage_log();
2162 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2166 80ddbec8 2018-04-29 stsp argc -= optind;
2167 80ddbec8 2018-04-29 stsp argv += optind;
2169 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2170 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2171 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2172 ecb28ae0 2018-07-16 stsp goto done;
2174 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2175 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2176 963f97a1 2019-03-18 stsp goto done;
2177 963f97a1 2019-03-18 stsp error = NULL;
2179 963f97a1 2019-03-18 stsp if (argc == 0) {
2180 963f97a1 2019-03-18 stsp path = strdup("");
2181 963f97a1 2019-03-18 stsp if (path == NULL) {
2182 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2183 ecb28ae0 2018-07-16 stsp goto done;
2185 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2186 963f97a1 2019-03-18 stsp if (worktree) {
2187 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2189 963f97a1 2019-03-18 stsp if (error)
2190 963f97a1 2019-03-18 stsp goto done;
2192 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2193 963f97a1 2019-03-18 stsp if (path == NULL) {
2194 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2195 963f97a1 2019-03-18 stsp goto done;
2199 963f97a1 2019-03-18 stsp usage_log();
2201 963f97a1 2019-03-18 stsp repo_path = worktree ?
2202 963f97a1 2019-03-18 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2203 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2204 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2205 963f97a1 2019-03-18 stsp goto done;
2208 a915003a 2019-02-05 stsp init_curses();
2210 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
2211 80ddbec8 2018-04-29 stsp if (error != NULL)
2212 ecb28ae0 2018-07-16 stsp goto done;
2214 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2215 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2216 c02c541e 2019-03-29 stsp if (error)
2217 c02c541e 2019-03-29 stsp goto done;
2219 15a94983 2018-12-23 stsp if (start_commit == NULL)
2220 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&start_id, worktree ?
2221 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2224 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&start_id,
2225 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2226 80ddbec8 2018-04-29 stsp if (error != NULL)
2227 8b473291 2019-02-21 stsp goto done;
2229 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2230 8b473291 2019-02-21 stsp if (error)
2231 ecb28ae0 2018-07-16 stsp goto done;
2233 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2234 04cc582a 2018-08-01 stsp if (view == NULL) {
2235 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2236 04cc582a 2018-08-01 stsp goto done;
2238 d01904d4 2019-06-25 stsp error = open_log_view(view, start_id, &refs, repo, worktree ?
2239 d01904d4 2019-06-25 stsp got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2240 ba4f502b 2018-08-04 stsp if (error)
2241 ba4f502b 2018-08-04 stsp goto done;
2242 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2244 ecb28ae0 2018-07-16 stsp free(repo_path);
2245 ecb28ae0 2018-07-16 stsp free(cwd);
2246 ecb28ae0 2018-07-16 stsp free(path);
2247 899d86c2 2018-05-10 stsp free(start_id);
2249 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2250 ec142235 2019-03-07 stsp if (worktree)
2251 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2252 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2253 80ddbec8 2018-04-29 stsp return error;
2256 4ed7e80c 2018-05-20 stsp __dead static void
2257 9f7d7167 2018-04-29 stsp usage_diff(void)
2260 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2261 9f7d7167 2018-04-29 stsp getprogname());
2265 b304db33 2018-05-20 stsp static char *
2266 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2268 b304db33 2018-05-20 stsp char *line;
2269 b304db33 2018-05-20 stsp size_t linelen;
2270 b304db33 2018-05-20 stsp size_t lineno;
2271 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2273 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2275 b304db33 2018-05-20 stsp *len = linelen;
2276 b304db33 2018-05-20 stsp return line;
2279 4ed7e80c 2018-05-20 stsp static const struct got_error *
2280 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2281 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
2282 c3e9aa98 2019-05-13 jcs char *header)
2284 61e69b96 2018-05-20 stsp const struct got_error *err;
2285 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2286 b304db33 2018-05-20 stsp char *line;
2287 b304db33 2018-05-20 stsp size_t len;
2288 61e69b96 2018-05-20 stsp wchar_t *wline;
2289 e0b650dd 2018-05-20 stsp int width;
2291 26ed57b2 2018-05-19 stsp rewind(f);
2292 f7d12f7e 2018-08-01 stsp werase(view->window);
2294 a3404814 2018-09-02 stsp if (header) {
2295 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
2296 a3404814 2018-09-02 stsp if (err) {
2297 a3404814 2018-09-02 stsp return err;
2300 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2301 a3404814 2018-09-02 stsp wstandout(view->window);
2302 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2303 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2304 a3404814 2018-09-02 stsp wstandend(view->window);
2305 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2306 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2308 a3404814 2018-09-02 stsp if (max_lines <= 1)
2309 a3404814 2018-09-02 stsp return NULL;
2310 a3404814 2018-09-02 stsp max_lines--;
2314 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2315 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2316 26ed57b2 2018-05-19 stsp if (line == NULL) {
2320 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2321 26ed57b2 2018-05-19 stsp free(line);
2325 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2326 61e69b96 2018-05-20 stsp if (err) {
2327 61e69b96 2018-05-20 stsp free(line);
2328 61e69b96 2018-05-20 stsp return err;
2330 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2331 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2332 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2333 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2334 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2335 26ed57b2 2018-05-19 stsp free(line);
2336 2550e4c3 2018-07-13 stsp free(wline);
2337 2550e4c3 2018-07-13 stsp wline = NULL;
2339 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2341 1a57306a 2018-09-02 stsp view_vborder(view);
2343 c3e9aa98 2019-05-13 jcs if (*eof) {
2344 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2345 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2346 c3e9aa98 2019-05-13 jcs nprinted++;
2349 c3e9aa98 2019-05-13 jcs err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2351 c3e9aa98 2019-05-13 jcs return err;
2354 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2355 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2356 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2359 26ed57b2 2018-05-19 stsp return NULL;
2362 abd2672a 2018-12-23 stsp static char *
2363 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2365 abd2672a 2018-12-23 stsp char *p, *s = ctime_r(time, datebuf);
2366 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2368 abd2672a 2018-12-23 stsp *p = '\0';
2372 4ed7e80c 2018-05-20 stsp static const struct got_error *
2373 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2374 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2376 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2377 abd2672a 2018-12-23 stsp char datebuf[26];
2378 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2379 15a94983 2018-12-23 stsp char *id_str = NULL;
2380 45d799e2 2018-12-23 stsp time_t committer_time;
2381 45d799e2 2018-12-23 stsp const char *author, *committer;
2382 8b473291 2019-02-21 stsp char *refs_str = NULL;
2384 8b473291 2019-02-21 stsp if (refs) {
2385 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, commit_id);
2387 8b473291 2019-02-21 stsp return err;
2390 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2392 abd2672a 2018-12-23 stsp return err;
2394 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2395 15a94983 2018-12-23 stsp if (err) {
2396 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2397 15a94983 2018-12-23 stsp goto done;
2400 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2401 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2402 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2403 abd2672a 2018-12-23 stsp goto done;
2405 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2406 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2407 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2408 abd2672a 2018-12-23 stsp goto done;
2410 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2411 abd2672a 2018-12-23 stsp if (fprintf(outfile, "date: %s UTC\n",
2412 45d799e2 2018-12-23 stsp get_datestr(&committer_time, datebuf)) < 0) {
2413 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2414 abd2672a 2018-12-23 stsp goto done;
2416 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2417 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2418 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2419 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2420 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2421 abd2672a 2018-12-23 stsp goto done;
2423 45d799e2 2018-12-23 stsp if (fprintf(outfile, "%s\n",
2424 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(commit)) < 0) {
2425 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2426 abd2672a 2018-12-23 stsp goto done;
2429 abd2672a 2018-12-23 stsp free(id_str);
2430 8b473291 2019-02-21 stsp free(refs_str);
2431 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2432 abd2672a 2018-12-23 stsp return err;
2435 abd2672a 2018-12-23 stsp static const struct got_error *
2436 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2438 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2439 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2440 15a94983 2018-12-23 stsp int obj_type;
2442 511a516b 2018-05-19 stsp f = got_opentemp();
2443 48ae06ee 2018-10-18 stsp if (f == NULL) {
2444 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2445 48ae06ee 2018-10-18 stsp goto done;
2447 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2448 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2449 fb43ecf1 2019-02-11 stsp goto done;
2453 15a94983 2018-12-23 stsp if (s->id1)
2454 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2456 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2458 15a94983 2018-12-23 stsp goto done;
2460 15a94983 2018-12-23 stsp switch (obj_type) {
2461 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2462 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2463 54156555 2018-12-24 stsp s->diff_context, s->repo, f);
2465 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2466 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2467 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
2469 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2470 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2471 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2472 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2474 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2477 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2478 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2479 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2481 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2482 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2483 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2484 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2485 8b473291 2019-02-21 stsp s->repo, f);
2490 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2492 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2493 15a94983 2018-12-23 stsp s->diff_context, s->repo, f);
2497 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2501 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2502 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2503 48ae06ee 2018-10-18 stsp return err;
2506 f5215bb9 2019-02-22 stsp static void
2507 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2509 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2510 f5215bb9 2019-02-22 stsp update_panels();
2511 f5215bb9 2019-02-22 stsp doupdate();
2514 48ae06ee 2018-10-18 stsp static const struct got_error *
2515 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2516 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2517 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2519 48ae06ee 2018-10-18 stsp const struct got_error *err;
2521 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2522 15a94983 2018-12-23 stsp int type1, type2;
2523 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2525 15a94983 2018-12-23 stsp return err;
2526 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2528 15a94983 2018-12-23 stsp return err;
2530 15a94983 2018-12-23 stsp if (type1 != type2)
2531 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2534 15a94983 2018-12-23 stsp if (id1) {
2535 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2536 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2537 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2539 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2541 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2542 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2543 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2544 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2545 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2547 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2548 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2549 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2550 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2551 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2552 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2553 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2555 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2556 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2557 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2559 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2560 48ae06ee 2018-10-18 stsp if (err) {
2561 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2562 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2563 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2564 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2565 48ae06ee 2018-10-18 stsp return err;
2568 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2569 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2570 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2572 5dc9f4bc 2018-08-04 stsp return NULL;
2575 e5a0f69f 2018-08-18 stsp static const struct got_error *
2576 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2578 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2580 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2581 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2582 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2583 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2584 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2585 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2586 e5a0f69f 2018-08-18 stsp return err;
2589 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2590 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2592 a3404814 2018-09-02 stsp const struct got_error *err;
2593 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2594 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2596 a3404814 2018-09-02 stsp if (s->id1) {
2597 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2599 a3404814 2018-09-02 stsp return err;
2601 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2603 a3404814 2018-09-02 stsp return err;
2605 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2606 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2607 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2608 a3404814 2018-09-02 stsp free(id_str1);
2609 a3404814 2018-09-02 stsp free(id_str2);
2610 a3404814 2018-09-02 stsp return err;
2612 a3404814 2018-09-02 stsp free(id_str1);
2613 a3404814 2018-09-02 stsp free(id_str2);
2615 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2616 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2620 15a087fe 2019-02-21 stsp static const struct got_error *
2621 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2622 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2624 d7a04538 2019-02-21 stsp const struct got_error *err;
2625 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
2626 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
2627 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
2629 15a087fe 2019-02-21 stsp free(s->id2);
2630 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2631 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2632 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2634 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2636 d7a04538 2019-02-21 stsp return err;
2637 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
2638 15a087fe 2019-02-21 stsp free(s->id1);
2639 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
2640 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2641 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
2642 15a087fe 2019-02-21 stsp return NULL;
2645 0cf4efb1 2018-09-29 stsp static const struct got_error *
2646 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2647 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2649 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2650 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2651 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
2652 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
2655 e5a0f69f 2018-08-18 stsp switch (ch) {
2657 1e37a5c2 2019-05-12 jcs case KEY_UP:
2658 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
2659 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2661 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2662 a60a9dc4 2019-05-13 jcs case CTRL('b'):
2663 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
2666 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
2667 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
2668 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2671 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2672 1e37a5c2 2019-05-12 jcs if (!s->eof)
2673 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2675 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
2676 a60a9dc4 2019-05-13 jcs case CTRL('f'):
2678 00ba99a7 2019-05-12 jcs if (s->eof)
2681 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
2682 1e37a5c2 2019-05-12 jcs char *line;
2683 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
2684 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2685 1e37a5c2 2019-05-12 jcs if (line == NULL)
2690 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
2691 1e37a5c2 2019-05-12 jcs s->diff_context--;
2692 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2693 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2697 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2698 1e37a5c2 2019-05-12 jcs s->diff_context++;
2699 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2700 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2705 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2707 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2708 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
2709 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
2710 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2713 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2718 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2722 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2723 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2725 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2726 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2730 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2732 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2734 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2735 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
2737 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
2738 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
2739 1e37a5c2 2019-05-12 jcs update_panels();
2740 1e37a5c2 2019-05-12 jcs doupdate();
2742 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
2743 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
2744 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
2745 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
2749 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2754 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
2755 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2758 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2762 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2763 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2765 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2766 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2772 bcbd79e2 2018-08-19 stsp return err;
2775 4ed7e80c 2018-05-20 stsp static const struct got_error *
2776 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2778 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2779 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2780 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2781 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2782 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2783 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2785 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2787 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2789 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2790 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2791 eb6600df 2019-01-04 stsp NULL) == -1)
2792 26ed57b2 2018-05-19 stsp err(1, "pledge");
2795 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2796 26ed57b2 2018-05-19 stsp switch (ch) {
2798 17020d27 2019-03-07 stsp usage_diff();
2799 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2803 26ed57b2 2018-05-19 stsp argc -= optind;
2804 26ed57b2 2018-05-19 stsp argv += optind;
2806 26ed57b2 2018-05-19 stsp if (argc == 0) {
2807 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2808 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2809 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2810 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2811 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2812 15a94983 2018-12-23 stsp id_str1 = argv[0];
2813 15a94983 2018-12-23 stsp id_str2 = argv[1];
2814 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2815 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2816 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2817 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2818 15a94983 2018-12-23 stsp id_str1 = argv[1];
2819 15a94983 2018-12-23 stsp id_str2 = argv[2];
2821 26ed57b2 2018-05-19 stsp usage_diff();
2823 a915003a 2019-02-05 stsp init_curses();
2825 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2826 eb6600df 2019-01-04 stsp if (error)
2827 eb6600df 2019-01-04 stsp goto done;
2829 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
2830 26ed57b2 2018-05-19 stsp if (error)
2831 26ed57b2 2018-05-19 stsp goto done;
2833 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
2834 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
2835 26ed57b2 2018-05-19 stsp if (error)
2836 26ed57b2 2018-05-19 stsp goto done;
2838 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
2839 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
2840 26ed57b2 2018-05-19 stsp if (error)
2841 26ed57b2 2018-05-19 stsp goto done;
2843 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2844 8b473291 2019-02-21 stsp if (error)
2845 8b473291 2019-02-21 stsp goto done;
2847 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2848 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2849 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2850 ea5e7bb5 2018-08-01 stsp goto done;
2852 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2853 5dc9f4bc 2018-08-04 stsp if (error)
2854 5dc9f4bc 2018-08-04 stsp goto done;
2855 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2857 c02c541e 2019-03-29 stsp free(repo_path);
2859 921be706 2019-06-28 stsp got_repo_close(repo);
2860 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2861 26ed57b2 2018-05-19 stsp return error;
2864 4ed7e80c 2018-05-20 stsp __dead static void
2865 9f7d7167 2018-04-29 stsp usage_blame(void)
2868 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2869 9f7d7167 2018-04-29 stsp getprogname());
2873 84451b3e 2018-07-10 stsp struct tog_blame_line {
2874 84451b3e 2018-07-10 stsp int annotated;
2875 84451b3e 2018-07-10 stsp struct got_object_id *id;
2878 4ed7e80c 2018-05-20 stsp static const struct got_error *
2879 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2880 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2881 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2882 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2884 84451b3e 2018-07-10 stsp const struct got_error *err;
2885 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2886 84451b3e 2018-07-10 stsp char *line;
2887 84451b3e 2018-07-10 stsp size_t len;
2888 84451b3e 2018-07-10 stsp wchar_t *wline;
2889 b700b5d6 2018-07-10 stsp int width, wlimit;
2890 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2891 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2892 ab089a2a 2018-07-12 stsp char *id_str;
2894 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2896 ab089a2a 2018-07-12 stsp return err;
2898 84451b3e 2018-07-10 stsp rewind(f);
2899 f7d12f7e 2018-08-01 stsp werase(view->window);
2901 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
2902 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2903 ab089a2a 2018-07-12 stsp free(id_str);
2904 ab089a2a 2018-07-12 stsp return err;
2907 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2908 ab089a2a 2018-07-12 stsp free(line);
2909 2550e4c3 2018-07-13 stsp line = NULL;
2910 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2911 a3404814 2018-09-02 stsp wstandout(view->window);
2912 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2913 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2914 a3404814 2018-09-02 stsp wstandend(view->window);
2915 2550e4c3 2018-07-13 stsp free(wline);
2916 2550e4c3 2018-07-13 stsp wline = NULL;
2917 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2918 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2920 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2921 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2922 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
2923 ab089a2a 2018-07-12 stsp free(id_str);
2924 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2926 ab089a2a 2018-07-12 stsp free(id_str);
2927 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2928 3f60a8ef 2018-07-10 stsp free(line);
2929 2550e4c3 2018-07-13 stsp line = NULL;
2931 3f60a8ef 2018-07-10 stsp return err;
2932 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2933 2550e4c3 2018-07-13 stsp free(wline);
2934 2550e4c3 2018-07-13 stsp wline = NULL;
2935 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2936 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2939 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2940 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2941 84451b3e 2018-07-10 stsp if (line == NULL) {
2945 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2946 84451b3e 2018-07-10 stsp free(line);
2950 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2951 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2952 84451b3e 2018-07-10 stsp if (err) {
2953 84451b3e 2018-07-10 stsp free(line);
2954 84451b3e 2018-07-10 stsp return err;
2957 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2958 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2960 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2961 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2962 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2963 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2964 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2965 84451b3e 2018-07-10 stsp char *id_str;
2966 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2967 84451b3e 2018-07-10 stsp if (err) {
2968 84451b3e 2018-07-10 stsp free(line);
2969 2550e4c3 2018-07-13 stsp free(wline);
2970 84451b3e 2018-07-10 stsp return err;
2972 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2973 84451b3e 2018-07-10 stsp free(id_str);
2974 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2976 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2977 ee41ec32 2018-07-10 stsp prev_id = NULL;
2980 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2981 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2982 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2985 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2986 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2987 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2988 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2989 84451b3e 2018-07-10 stsp free(line);
2990 2550e4c3 2018-07-13 stsp free(wline);
2991 2550e4c3 2018-07-13 stsp wline = NULL;
2993 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2995 1a57306a 2018-09-02 stsp view_vborder(view);
2997 84451b3e 2018-07-10 stsp return NULL;
3000 84451b3e 2018-07-10 stsp static const struct got_error *
3001 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3003 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
3004 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
3005 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
3006 1a76625f 2018-10-22 stsp int errcode;
3008 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
3009 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3010 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3012 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3013 1a76625f 2018-10-22 stsp if (errcode)
3014 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3016 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3017 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3018 d68a0a7d 2018-07-10 stsp goto done;
3021 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3022 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3024 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3025 d68a0a7d 2018-07-10 stsp if (line->annotated)
3026 d68a0a7d 2018-07-10 stsp goto done;
3028 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3029 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3030 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3031 84451b3e 2018-07-10 stsp goto done;
3033 84451b3e 2018-07-10 stsp line->annotated = 1;
3035 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3036 1a76625f 2018-10-22 stsp if (errcode)
3037 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3038 84451b3e 2018-07-10 stsp return err;
3041 84451b3e 2018-07-10 stsp static void *
3042 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3044 18430de3 2018-07-10 stsp const struct got_error *err;
3045 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3046 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3047 1a76625f 2018-10-22 stsp int errcode;
3049 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3050 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
3052 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3053 1a76625f 2018-10-22 stsp if (errcode)
3054 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3055 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3057 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3058 c9beca56 2018-07-22 stsp ta->repo = NULL;
3059 c9beca56 2018-07-22 stsp *ta->complete = 1;
3061 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3062 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3063 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3065 18430de3 2018-07-10 stsp return (void *)err;
3068 245d91c1 2018-07-12 stsp static struct got_object_id *
3069 15a94983 2018-12-23 stsp get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3070 15a94983 2018-12-23 stsp int selected_line)
3072 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3074 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3075 245d91c1 2018-07-12 stsp if (!line->annotated)
3076 245d91c1 2018-07-12 stsp return NULL;
3078 245d91c1 2018-07-12 stsp return line->id;
3081 b880a918 2018-07-10 stsp static const struct got_error *
3082 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3084 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3087 245d91c1 2018-07-12 stsp if (blame->thread) {
3088 1a76625f 2018-10-22 stsp int errcode;
3089 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3090 1a76625f 2018-10-22 stsp if (errcode)
3091 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3092 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3093 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3094 1a76625f 2018-10-22 stsp if (errcode)
3095 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3096 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3097 1a76625f 2018-10-22 stsp if (errcode)
3098 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3099 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3100 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3101 245d91c1 2018-07-12 stsp err = NULL;
3102 245d91c1 2018-07-12 stsp blame->thread = NULL;
3104 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3105 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3106 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3108 245d91c1 2018-07-12 stsp if (blame->f) {
3109 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3110 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3111 245d91c1 2018-07-12 stsp blame->f = NULL;
3113 57670559 2018-12-23 stsp if (blame->lines) {
3114 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3115 57670559 2018-12-23 stsp free(blame->lines[i].id);
3116 57670559 2018-12-23 stsp free(blame->lines);
3117 57670559 2018-12-23 stsp blame->lines = NULL;
3119 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3120 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3122 245d91c1 2018-07-12 stsp return err;
3125 245d91c1 2018-07-12 stsp static const struct got_error *
3126 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3127 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3128 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3129 245d91c1 2018-07-12 stsp struct got_repository *repo)
3131 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3132 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3133 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3134 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3135 15a94983 2018-12-23 stsp int obj_type;
3137 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3139 15a94983 2018-12-23 stsp return err;
3140 15a94983 2018-12-23 stsp if (obj_id == NULL)
3141 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
3143 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3145 84451b3e 2018-07-10 stsp goto done;
3147 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3148 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3149 84451b3e 2018-07-10 stsp goto done;
3152 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3154 a70480e0 2018-06-23 stsp goto done;
3155 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3156 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3157 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3158 84451b3e 2018-07-10 stsp goto done;
3160 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3161 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
3163 84451b3e 2018-07-10 stsp goto done;
3165 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3166 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3167 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3168 84451b3e 2018-07-10 stsp goto done;
3171 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3173 bd24772e 2018-07-11 stsp goto done;
3175 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3176 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3177 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3178 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3179 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3180 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3181 245d91c1 2018-07-12 stsp goto done;
3183 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3185 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3186 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3187 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3188 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3189 245d91c1 2018-07-12 stsp *blame_complete = 0;
3193 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3194 27d434c2 2018-09-15 stsp free(obj_id);
3196 245d91c1 2018-07-12 stsp stop_blame(blame);
3197 245d91c1 2018-07-12 stsp return err;
3200 245d91c1 2018-07-12 stsp static const struct got_error *
3201 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3202 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3203 8b473291 2019-02-21 stsp struct got_repository *repo)
3205 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3206 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3208 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3210 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3212 7cbe629d 2018-08-04 stsp return err;
3214 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3215 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3216 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3217 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3218 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3219 fb2756b9 2018-08-04 stsp s->path = path;
3220 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
3221 638f9024 2019-05-13 stsp return got_error_from_errno("open_blame_view");
3222 fb2756b9 2018-08-04 stsp s->repo = repo;
3223 8b473291 2019-02-21 stsp s->refs = refs;
3224 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3225 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3227 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3228 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3229 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3230 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
3231 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
3233 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3234 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3235 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3236 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3239 e5a0f69f 2018-08-18 stsp static const struct got_error *
3240 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3242 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3243 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3245 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3246 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3248 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3249 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3250 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3251 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3252 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3255 e5a0f69f 2018-08-18 stsp free(s->path);
3257 e5a0f69f 2018-08-18 stsp return err;
3260 7cbe629d 2018-08-04 stsp static const struct got_error *
3261 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
3263 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3265 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
3266 6c4c42e0 2019-06-24 stsp return NULL;
3269 6c4c42e0 2019-06-24 stsp static int
3270 6c4c42e0 2019-06-24 stsp match_line(const char *line, regex_t *regex)
3272 6c4c42e0 2019-06-24 stsp regmatch_t regmatch;
3274 6c4c42e0 2019-06-24 stsp return regexec(regex, line, 1, ®match, 0) == 0;
3278 6c4c42e0 2019-06-24 stsp static const struct got_error *
3279 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
3281 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3282 6c4c42e0 2019-06-24 stsp int lineno;
3284 6c4c42e0 2019-06-24 stsp if (!view->searching) {
3285 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3286 6c4c42e0 2019-06-24 stsp return NULL;
3289 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3290 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3291 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
3293 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
3295 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3296 6c4c42e0 2019-06-24 stsp lineno = 1;
3298 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3301 6c4c42e0 2019-06-24 stsp while (1) {
3302 6c4c42e0 2019-06-24 stsp char *line = NULL;
3303 6c4c42e0 2019-06-24 stsp off_t offset;
3304 6c4c42e0 2019-06-24 stsp size_t len;
3306 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
3307 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
3308 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3309 6c4c42e0 2019-06-24 stsp free(line);
3313 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3314 6c4c42e0 2019-06-24 stsp lineno = 1;
3316 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3319 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
3320 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3321 6c4c42e0 2019-06-24 stsp free(line);
3322 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
3324 6c4c42e0 2019-06-24 stsp free(line);
3325 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
3326 2246482e 2019-06-25 stsp if (line && match_line(line, &view->regex)) {
3327 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3328 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
3329 6c4c42e0 2019-06-24 stsp free(line);
3332 6c4c42e0 2019-06-24 stsp free(line);
3333 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3339 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3340 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
3341 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
3344 6c4c42e0 2019-06-24 stsp return NULL;
3347 6c4c42e0 2019-06-24 stsp static const struct got_error *
3348 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
3350 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3351 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
3352 2b380cc8 2018-10-24 stsp int errcode;
3354 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
3355 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3356 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
3357 2b380cc8 2018-10-24 stsp if (errcode)
3358 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3361 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3362 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3363 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
3364 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
3366 669b5ffa 2018-10-07 stsp view_vborder(view);
3367 e5a0f69f 2018-08-18 stsp return err;
3370 e5a0f69f 2018-08-18 stsp static const struct got_error *
3371 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3372 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3374 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
3375 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
3376 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3377 669b5ffa 2018-10-07 stsp int begin_x = 0;
3379 e5a0f69f 2018-08-18 stsp switch (ch) {
3381 1e37a5c2 2019-05-12 jcs s->done = 1;
3384 1e37a5c2 2019-05-12 jcs case KEY_UP:
3385 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
3386 1e37a5c2 2019-05-12 jcs s->selected_line--;
3387 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
3388 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3389 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3391 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3392 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
3393 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3396 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3397 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3398 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3400 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3403 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3404 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3405 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3406 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3407 1e37a5c2 2019-05-12 jcs s->selected_line++;
3408 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3409 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3410 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3413 1e37a5c2 2019-05-12 jcs case 'p': {
3414 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3415 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3416 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3417 1e37a5c2 2019-05-12 jcs if (id == NULL)
3419 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3420 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3421 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3422 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3423 1e37a5c2 2019-05-12 jcs int obj_type;
3424 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3425 1e37a5c2 2019-05-12 jcs s->repo, id);
3428 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3429 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3430 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3431 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3434 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3435 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3436 1e37a5c2 2019-05-12 jcs pid->id, s->path);
3437 e5a0f69f 2018-08-18 stsp if (err) {
3438 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
3439 1e37a5c2 2019-05-12 jcs err = NULL;
3440 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3443 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
3445 1e37a5c2 2019-05-12 jcs free(blob_id);
3446 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
3447 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
3448 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3451 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3453 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3455 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
3456 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
3458 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3463 1e37a5c2 2019-05-12 jcs s->done = 1;
3464 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3465 1e37a5c2 2019-05-12 jcs s->done = 0;
3466 1e37a5c2 2019-05-12 jcs if (thread_err)
3468 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3469 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
3470 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3471 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3472 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
3473 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
3478 1e37a5c2 2019-05-12 jcs case 'B': {
3479 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
3480 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
3481 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
3483 1e37a5c2 2019-05-12 jcs s->done = 1;
3484 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3485 1e37a5c2 2019-05-12 jcs s->done = 0;
3486 1e37a5c2 2019-05-12 jcs if (thread_err)
3488 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3489 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
3490 1e37a5c2 2019-05-12 jcs s->blamed_commit =
3491 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
3492 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3493 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3494 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
3495 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
3500 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3501 1e37a5c2 2019-05-12 jcs case '\r': {
3502 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3503 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
3504 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
3505 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3506 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3507 1e37a5c2 2019-05-12 jcs if (id == NULL)
3509 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
3512 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
3513 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
3514 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3515 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3516 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3517 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
3518 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3519 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
3522 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
3523 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
3524 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3526 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3529 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3530 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3533 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
3535 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3538 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
3539 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
3541 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3546 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3548 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3549 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
3550 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
3553 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3554 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
3555 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3556 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3559 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
3560 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
3561 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
3562 1e37a5c2 2019-05-12 jcs view->nlines - 2;
3564 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
3565 1e37a5c2 2019-05-12 jcs s->blame.nlines -
3566 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
3568 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3569 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
3570 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3571 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3577 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
3580 a70480e0 2018-06-23 stsp static const struct got_error *
3581 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
3583 a70480e0 2018-06-23 stsp const struct got_error *error;
3584 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
3585 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3586 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
3587 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3588 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3589 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
3591 e1cd8fed 2018-08-01 stsp struct tog_view *view;
3593 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3595 a70480e0 2018-06-23 stsp #ifndef PROFILE
3596 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3597 8e94dd5b 2019-01-04 stsp NULL) == -1)
3598 a70480e0 2018-06-23 stsp err(1, "pledge");
3601 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3602 a70480e0 2018-06-23 stsp switch (ch) {
3604 a70480e0 2018-06-23 stsp commit_id_str = optarg;
3607 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3608 69069811 2018-08-02 stsp if (repo_path == NULL)
3609 69069811 2018-08-02 stsp err(1, "-r option");
3612 17020d27 2019-03-07 stsp usage_blame();
3613 a70480e0 2018-06-23 stsp /* NOTREACHED */
3617 a70480e0 2018-06-23 stsp argc -= optind;
3618 a70480e0 2018-06-23 stsp argv += optind;
3620 69069811 2018-08-02 stsp if (argc == 1)
3621 69069811 2018-08-02 stsp path = argv[0];
3623 a70480e0 2018-06-23 stsp usage_blame();
3625 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
3626 69069811 2018-08-02 stsp if (cwd == NULL) {
3627 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3628 69069811 2018-08-02 stsp goto done;
3630 69069811 2018-08-02 stsp if (repo_path == NULL) {
3631 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3632 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3633 69069811 2018-08-02 stsp goto done;
3635 eb41ed75 2019-02-05 stsp error = NULL;
3636 eb41ed75 2019-02-05 stsp if (worktree) {
3637 eb41ed75 2019-02-05 stsp repo_path =
3638 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3639 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
3640 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3641 eb41ed75 2019-02-05 stsp if (error)
3642 eb41ed75 2019-02-05 stsp goto done;
3644 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
3645 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
3646 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3647 eb41ed75 2019-02-05 stsp goto done;
3652 a915003a 2019-02-05 stsp init_curses();
3654 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
3655 c02c541e 2019-03-29 stsp if (error != NULL)
3656 8e94dd5b 2019-01-04 stsp goto done;
3658 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3659 c02c541e 2019-03-29 stsp if (error)
3660 92205607 2019-01-04 stsp goto done;
3662 eb41ed75 2019-02-05 stsp if (worktree) {
3663 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3664 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3665 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3666 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3667 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3668 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3669 eb41ed75 2019-02-05 stsp path) == -1) {
3670 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3671 eb41ed75 2019-02-05 stsp goto done;
3673 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3676 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3678 eb41ed75 2019-02-05 stsp if (error)
3679 69069811 2018-08-02 stsp goto done;
3681 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
3682 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
3683 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3684 a70480e0 2018-06-23 stsp if (error != NULL)
3685 66b4983c 2018-06-23 stsp goto done;
3686 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3687 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
3689 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
3690 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3692 a19e88aa 2018-06-23 stsp if (error != NULL)
3693 8b473291 2019-02-21 stsp goto done;
3695 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
3696 8b473291 2019-02-21 stsp if (error)
3697 a19e88aa 2018-06-23 stsp goto done;
3699 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3700 e1cd8fed 2018-08-01 stsp if (view == NULL) {
3701 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3702 e1cd8fed 2018-08-01 stsp goto done;
3704 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3705 7cbe629d 2018-08-04 stsp if (error)
3706 7cbe629d 2018-08-04 stsp goto done;
3707 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3709 69069811 2018-08-02 stsp free(repo_path);
3710 69069811 2018-08-02 stsp free(cwd);
3711 a70480e0 2018-06-23 stsp free(commit_id);
3712 eb41ed75 2019-02-05 stsp if (worktree)
3713 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
3715 a70480e0 2018-06-23 stsp got_repo_close(repo);
3716 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3717 a70480e0 2018-06-23 stsp return error;
3720 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3721 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
3722 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
3723 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
3724 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
3725 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
3726 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
3728 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3729 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
3730 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
3731 ffd1d5e5 2018-06-23 stsp int width, n;
3733 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
3735 f7d12f7e 2018-08-01 stsp werase(view->window);
3737 ffd1d5e5 2018-06-23 stsp if (limit == 0)
3738 ffd1d5e5 2018-06-23 stsp return NULL;
3740 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
3742 ffd1d5e5 2018-06-23 stsp return err;
3743 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3744 a3404814 2018-09-02 stsp wstandout(view->window);
3745 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3746 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3747 a3404814 2018-09-02 stsp wstandend(view->window);
3748 2550e4c3 2018-07-13 stsp free(wline);
3749 2550e4c3 2018-07-13 stsp wline = NULL;
3750 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3751 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3752 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3753 ffd1d5e5 2018-06-23 stsp return NULL;
3754 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
3756 ce52c690 2018-06-23 stsp return err;
3757 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3758 2550e4c3 2018-07-13 stsp free(wline);
3759 2550e4c3 2018-07-13 stsp wline = NULL;
3760 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3761 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3762 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3763 ffd1d5e5 2018-06-23 stsp return NULL;
3764 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3765 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
3766 a1eca9bb 2018-06-23 stsp return NULL;
3768 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3769 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
3770 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
3771 0cf4efb1 2018-09-29 stsp if (view->focussed)
3772 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3773 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
3775 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
3776 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
3777 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3778 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3779 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3780 ffd1d5e5 2018-06-23 stsp return NULL;
3784 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
3785 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3788 ffd1d5e5 2018-06-23 stsp while (te) {
3789 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
3791 1d13200f 2018-07-12 stsp if (show_ids) {
3792 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
3794 638f9024 2019-05-13 stsp return got_error_from_errno(
3795 230a42bd 2019-05-11 jcs "got_object_id_str");
3797 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3798 d8355ef1 2019-02-10 stsp te->name, S_ISDIR(te->mode) ? "/" :
3799 d8355ef1 2019-02-10 stsp ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3800 1d13200f 2018-07-12 stsp free(id_str);
3801 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3803 1d13200f 2018-07-12 stsp free(id_str);
3804 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
3805 ffd1d5e5 2018-06-23 stsp if (err) {
3806 ffd1d5e5 2018-06-23 stsp free(line);
3809 ffd1d5e5 2018-06-23 stsp if (n == selected) {
3810 0cf4efb1 2018-09-29 stsp if (view->focussed)
3811 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3812 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
3814 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3815 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3816 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3817 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
3818 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3819 ffd1d5e5 2018-06-23 stsp free(line);
3820 2550e4c3 2018-07-13 stsp free(wline);
3821 2550e4c3 2018-07-13 stsp wline = NULL;
3823 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3824 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
3825 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3827 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3830 ffd1d5e5 2018-06-23 stsp return err;
3833 ffd1d5e5 2018-06-23 stsp static void
3834 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
3835 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
3836 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
3838 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
3841 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
3844 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3845 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
3846 ffd1d5e5 2018-06-23 stsp if (!isroot)
3847 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3851 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
3852 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
3853 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
3854 ffd1d5e5 2018-06-23 stsp prev = te;
3855 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3857 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
3858 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3860 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3861 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3864 768394f3 2019-01-24 stsp static int
3865 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3866 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
3867 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
3869 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
3870 ffd1d5e5 2018-06-23 stsp int n = 0;
3872 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
3873 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3875 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
3876 768394f3 2019-01-24 stsp last = last_displayed_entry;
3877 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
3878 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
3879 768394f3 2019-01-24 stsp if (last) {
3880 768394f3 2019-01-24 stsp *first_displayed_entry = next;
3881 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
3887 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3888 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
3889 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
3891 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
3892 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
3893 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
3895 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
3896 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
3898 ce52c690 2018-06-23 stsp len += strlen(te->name);
3900 ce52c690 2018-06-23 stsp *path = calloc(1, len);
3901 ffd1d5e5 2018-06-23 stsp if (path == NULL)
3902 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
3904 ce52c690 2018-06-23 stsp (*path)[0] = '/';
3905 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
3906 d9765a41 2018-06-23 stsp while (pt) {
3907 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3908 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3909 cb2ebc8a 2018-06-23 stsp goto done;
3911 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
3912 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3913 cb2ebc8a 2018-06-23 stsp goto done;
3915 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3918 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3919 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3920 ce52c690 2018-06-23 stsp goto done;
3924 ce52c690 2018-06-23 stsp if (err) {
3925 ce52c690 2018-06-23 stsp free(*path);
3926 ce52c690 2018-06-23 stsp *path = NULL;
3928 ce52c690 2018-06-23 stsp return err;
3931 ce52c690 2018-06-23 stsp static const struct got_error *
3932 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3933 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3934 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3935 8b473291 2019-02-21 stsp struct got_repository *repo)
3937 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3938 ce52c690 2018-06-23 stsp char *path;
3939 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3941 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3943 ce52c690 2018-06-23 stsp return err;
3945 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3946 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3947 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3949 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
3950 e5a0f69f 2018-08-18 stsp if (err) {
3951 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3952 e5a0f69f 2018-08-18 stsp free(path);
3954 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3955 69efd4c4 2018-07-18 stsp return err;
3958 69efd4c4 2018-07-18 stsp static const struct got_error *
3959 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3960 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3961 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3962 8b473291 2019-02-21 stsp struct got_repository *repo)
3964 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3965 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3966 69efd4c4 2018-07-18 stsp char *path;
3968 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3969 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3970 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3972 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3974 69efd4c4 2018-07-18 stsp return err;
3976 d01904d4 2019-06-25 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
3978 e5a0f69f 2018-08-18 stsp view_close(log_view);
3980 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3981 cb2ebc8a 2018-06-23 stsp free(path);
3982 cb2ebc8a 2018-06-23 stsp return err;
3985 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3986 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3987 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3988 8b473291 2019-02-21 stsp struct got_repository *repo)
3990 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3991 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3992 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3994 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3996 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3997 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3998 ffd1d5e5 2018-06-23 stsp goto done;
4000 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4001 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4002 ffd1d5e5 2018-06-23 stsp goto done;
4005 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
4006 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
4007 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4008 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
4009 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
4010 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4011 6484ec90 2018-09-29 stsp goto done;
4013 8b473291 2019-02-21 stsp s->refs = refs;
4014 fb2756b9 2018-08-04 stsp s->repo = repo;
4016 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
4017 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
4018 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
4019 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
4020 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
4022 ad80ab7b 2018-08-04 stsp free(commit_id_str);
4023 6484ec90 2018-09-29 stsp if (err) {
4024 fb2756b9 2018-08-04 stsp free(s->tree_label);
4025 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4027 ad80ab7b 2018-08-04 stsp return err;
4030 e5a0f69f 2018-08-18 stsp static const struct got_error *
4031 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
4033 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4035 fb2756b9 2018-08-04 stsp free(s->tree_label);
4036 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4037 6484ec90 2018-09-29 stsp free(s->commit_id);
4038 6484ec90 2018-09-29 stsp s->commit_id = NULL;
4039 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
4040 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
4041 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
4042 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
4043 ad80ab7b 2018-08-04 stsp free(parent);
4046 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
4047 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
4048 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
4050 7c32bd05 2019-06-22 stsp return NULL;
4053 7c32bd05 2019-06-22 stsp static const struct got_error *
4054 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
4056 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4058 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
4059 7c32bd05 2019-06-22 stsp return NULL;
4062 7c32bd05 2019-06-22 stsp static int
4063 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4065 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
4067 7c32bd05 2019-06-22 stsp return regexec(regex, te->name, 1, ®match, 0) == 0;
4070 7c32bd05 2019-06-22 stsp static const struct got_error *
4071 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
4073 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4074 7c32bd05 2019-06-22 stsp struct got_tree_entry *entry, *te;
4076 7c32bd05 2019-06-22 stsp if (!view->searching) {
4077 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4078 7c32bd05 2019-06-22 stsp return NULL;
4081 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4082 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
4083 7c32bd05 2019-06-22 stsp if (s->selected_entry)
4084 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4086 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4089 7c32bd05 2019-06-22 stsp if (s->selected_entry == NULL) {
4090 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4091 7c32bd05 2019-06-22 stsp entry = te;
4093 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4094 7c32bd05 2019-06-22 stsp entry = te;
4095 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) ==
4096 7c32bd05 2019-06-22 stsp s->selected_entry)
4102 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4103 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4105 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4106 7c32bd05 2019-06-22 stsp entry = te;
4110 7c32bd05 2019-06-22 stsp while (1) {
4111 7c32bd05 2019-06-22 stsp if (entry == NULL) {
4112 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
4113 ac66afb8 2019-06-24 stsp view->search_next_done = 1;
4114 ac66afb8 2019-06-24 stsp return NULL;
4116 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4117 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4119 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4120 7c32bd05 2019-06-22 stsp entry = te;
4124 7c32bd05 2019-06-22 stsp if (match_tree_entry(entry, &view->regex)) {
4125 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4126 7c32bd05 2019-06-22 stsp s->matched_entry = entry;
4130 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4131 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(entry, entry);
4133 7c32bd05 2019-06-22 stsp if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4134 7c32bd05 2019-06-22 stsp entry = NULL;
4136 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4137 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) == entry) {
4138 7c32bd05 2019-06-22 stsp entry = te;
4146 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4147 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
4148 7c32bd05 2019-06-22 stsp s->selected = 0;
4151 e5a0f69f 2018-08-18 stsp return NULL;
4154 ad80ab7b 2018-08-04 stsp static const struct got_error *
4155 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
4157 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
4158 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4159 e5a0f69f 2018-08-18 stsp char *parent_path;
4161 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
4163 e5a0f69f 2018-08-18 stsp return err;
4165 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
4166 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
4167 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4168 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
4169 e5a0f69f 2018-08-18 stsp free(parent_path);
4171 669b5ffa 2018-10-07 stsp view_vborder(view);
4172 e5a0f69f 2018-08-18 stsp return err;
4175 e5a0f69f 2018-08-18 stsp static const struct got_error *
4176 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4177 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4179 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4180 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
4181 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
4182 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
4184 e5a0f69f 2018-08-18 stsp switch (ch) {
4186 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
4189 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
4191 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4192 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4193 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
4194 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
4195 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
4196 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4197 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4199 1e37a5c2 2019-05-12 jcs return err;
4200 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
4202 1e37a5c2 2019-05-12 jcs view_close(log_view);
4205 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
4206 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4208 1e37a5c2 2019-05-12 jcs *new_view = log_view;
4211 1e37a5c2 2019-05-12 jcs case KEY_UP:
4212 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
4213 1e37a5c2 2019-05-12 jcs s->selected--;
4214 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
4217 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
4219 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
4220 1e37a5c2 2019-05-12 jcs s->entries, s->tree == s->root);
4222 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4223 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
4224 1e37a5c2 2019-05-12 jcs MAX(0, view->nlines - 4 - s->selected), s->entries,
4225 1e37a5c2 2019-05-12 jcs s->tree == s->root);
4226 1e37a5c2 2019-05-12 jcs s->selected = 0;
4227 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_FIRST(&s->entries->head) ==
4228 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
4229 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
4232 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4233 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
4234 1e37a5c2 2019-05-12 jcs s->selected++;
4237 00ba99a7 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4238 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
4240 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
4241 1e37a5c2 2019-05-12 jcs s->last_displayed_entry, s->entries);
4243 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4244 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4246 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
4247 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
4248 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4251 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
4252 1e37a5c2 2019-05-12 jcs view->nlines, s->last_displayed_entry, s->entries);
4253 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
4254 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
4255 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
4256 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
4258 1e37a5c2 2019-05-12 jcs ndisplayed++;
4259 1e37a5c2 2019-05-12 jcs te = SIMPLEQ_NEXT(te, entry);
4260 1e37a5c2 2019-05-12 jcs } while (te);
4261 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
4264 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4266 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4267 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4268 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
4269 1e37a5c2 2019-05-12 jcs /* user selected '..' */
4270 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
4272 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
4273 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
4275 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
4276 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
4277 1e37a5c2 2019-05-12 jcs s->entries =
4278 1e37a5c2 2019-05-12 jcs got_object_tree_get_entries(s->tree);
4279 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
4280 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
4281 1e37a5c2 2019-05-12 jcs s->selected_entry =
4282 1e37a5c2 2019-05-12 jcs parent->selected_entry;
4283 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
4284 1e37a5c2 2019-05-12 jcs free(parent);
4285 1e37a5c2 2019-05-12 jcs } else if (S_ISDIR(s->selected_entry->mode)) {
4286 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
4287 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&subtree,
4288 1e37a5c2 2019-05-12 jcs s->repo, s->selected_entry->id);
4291 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
4292 941e9f74 2019-05-21 stsp if (err) {
4293 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
4296 1e37a5c2 2019-05-12 jcs } else if (S_ISREG(s->selected_entry->mode)) {
4297 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
4298 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
4299 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
4301 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
4302 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
4303 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
4306 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
4307 669b5ffa 2018-10-07 stsp err = view_close_child(view);
4309 669b5ffa 2018-10-07 stsp return err;
4310 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
4311 669b5ffa 2018-10-07 stsp if (err) {
4312 1e37a5c2 2019-05-12 jcs view_close(blame_view);
4315 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
4316 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
4318 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
4321 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4322 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
4323 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4329 ffd1d5e5 2018-06-23 stsp return err;
4332 ffd1d5e5 2018-06-23 stsp __dead static void
4333 ffd1d5e5 2018-06-23 stsp usage_tree(void)
4336 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4337 ffd1d5e5 2018-06-23 stsp getprogname());
4341 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4342 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
4344 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
4345 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
4346 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4347 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
4348 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4349 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
4350 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
4351 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
4353 5221c383 2018-08-01 stsp struct tog_view *view;
4355 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4357 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
4358 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4359 d188b9a6 2019-01-04 stsp NULL) == -1)
4360 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
4363 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
4364 ffd1d5e5 2018-06-23 stsp switch (ch) {
4366 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
4369 17020d27 2019-03-07 stsp usage_tree();
4370 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
4374 ffd1d5e5 2018-06-23 stsp argc -= optind;
4375 ffd1d5e5 2018-06-23 stsp argv += optind;
4377 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
4378 52185f70 2019-02-05 stsp struct got_worktree *worktree;
4379 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
4380 52185f70 2019-02-05 stsp if (cwd == NULL)
4381 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
4382 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4383 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4384 52185f70 2019-02-05 stsp goto done;
4385 52185f70 2019-02-05 stsp if (worktree) {
4386 52185f70 2019-02-05 stsp free(cwd);
4387 52185f70 2019-02-05 stsp repo_path =
4388 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4389 52185f70 2019-02-05 stsp got_worktree_close(worktree);
4391 52185f70 2019-02-05 stsp repo_path = cwd;
4392 52185f70 2019-02-05 stsp if (repo_path == NULL) {
4393 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4394 52185f70 2019-02-05 stsp goto done;
4396 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
4397 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
4398 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
4399 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
4401 ffd1d5e5 2018-06-23 stsp usage_log();
4403 a915003a 2019-02-05 stsp init_curses();
4405 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
4406 c02c541e 2019-03-29 stsp if (error != NULL)
4407 52185f70 2019-02-05 stsp goto done;
4409 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4410 c02c541e 2019-03-29 stsp if (error)
4411 52185f70 2019-02-05 stsp goto done;
4413 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
4414 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4416 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
4417 dd88155e 2019-06-29 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4418 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4419 ffd1d5e5 2018-06-23 stsp goto done;
4421 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4422 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4423 ffd1d5e5 2018-06-23 stsp goto done;
4425 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
4426 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
4427 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4428 8b473291 2019-02-21 stsp goto done;
4430 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
4431 8b473291 2019-02-21 stsp if (error)
4432 ffd1d5e5 2018-06-23 stsp goto done;
4434 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4435 5221c383 2018-08-01 stsp if (view == NULL) {
4436 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4437 5221c383 2018-08-01 stsp goto done;
4439 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
4440 ad80ab7b 2018-08-04 stsp if (error)
4441 ad80ab7b 2018-08-04 stsp goto done;
4442 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4444 52185f70 2019-02-05 stsp free(repo_path);
4445 ffd1d5e5 2018-06-23 stsp free(commit_id);
4446 ffd1d5e5 2018-06-23 stsp if (commit)
4447 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
4449 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
4451 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
4452 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4453 ffd1d5e5 2018-06-23 stsp return error;
4456 4ed7e80c 2018-05-20 stsp __dead static void
4457 9f7d7167 2018-04-29 stsp usage(void)
4459 5e070240 2019-06-22 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n", getprogname());
4463 c2301be8 2018-04-30 stsp static char **
4464 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
4466 c2301be8 2018-04-30 stsp char **argv;
4467 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
4469 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
4470 c2301be8 2018-04-30 stsp if (argv == NULL)
4471 c2301be8 2018-04-30 stsp err(1, "calloc");
4472 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
4473 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
4474 c2301be8 2018-04-30 stsp err(1, "calloc");
4475 c2301be8 2018-04-30 stsp if (arg1) {
4476 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
4477 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
4478 c2301be8 2018-04-30 stsp err(1, "calloc");
4481 c2301be8 2018-04-30 stsp return argv;
4485 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
4487 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
4488 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
4489 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
4490 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
4492 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
4494 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
4495 9f7d7167 2018-04-29 stsp switch (ch) {
4497 9f7d7167 2018-04-29 stsp hflag = 1;
4501 9f7d7167 2018-04-29 stsp /* NOTREACHED */
4505 9f7d7167 2018-04-29 stsp argc -= optind;
4506 9f7d7167 2018-04-29 stsp argv += optind;
4507 9f7d7167 2018-04-29 stsp optind = 0;
4508 c2301be8 2018-04-30 stsp optreset = 1;
4510 c2301be8 2018-04-30 stsp if (argc == 0) {
4511 f29d3e89 2018-06-23 stsp if (hflag)
4513 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
4514 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
4515 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
4520 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
4521 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
4522 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
4523 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
4524 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
4525 9f7d7167 2018-04-29 stsp if (hflag)
4526 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
4530 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
4531 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
4532 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
4533 c2301be8 2018-04-30 stsp if (repo_path) {
4534 c2301be8 2018-04-30 stsp struct got_repository *repo;
4535 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
4536 c2301be8 2018-04-30 stsp if (error == NULL)
4537 c2301be8 2018-04-30 stsp got_repo_close(repo);
4539 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath",
4541 c2301be8 2018-04-30 stsp if (error) {
4542 f29d3e89 2018-06-23 stsp if (hflag) {
4543 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
4544 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
4548 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
4549 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
4550 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
4551 ad7de8d9 2018-04-30 stsp free(repo_path);
4554 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
4555 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
4557 c2301be8 2018-04-30 stsp free(repo_path);
4561 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4562 9f7d7167 2018-04-29 stsp if (error)
4563 9f7d7167 2018-04-29 stsp goto done;
4566 c2301be8 2018-04-30 stsp free(cmd_argv);
4567 9f7d7167 2018-04-29 stsp if (error)
4568 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);