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 60493ae3 2019-06-20 stsp match_commit(struct got_commit_object *commit, regex_t *regex)
1728 60493ae3 2019-06-20 stsp regmatch_t regmatch;
1730 60493ae3 2019-06-20 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1731 60493ae3 2019-06-20 stsp ®match, 0) == 0 ||
1732 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1733 60493ae3 2019-06-20 stsp ®match, 0) == 0 ||
1734 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_logmsg(commit), 1,
1735 60493ae3 2019-06-20 stsp ®match, 0) == 0)
1741 60493ae3 2019-06-20 stsp static const struct got_error *
1742 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
1744 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
1745 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1746 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
1748 60493ae3 2019-06-20 stsp if (!view->searching) {
1749 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1750 60493ae3 2019-06-20 stsp return NULL;
1753 b1bf1435 2019-06-21 stsp if (s->matched_entry) {
1754 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1755 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
1757 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
1758 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
1760 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1761 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
1763 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1766 60493ae3 2019-06-20 stsp while (1) {
1767 60493ae3 2019-06-20 stsp if (entry == NULL) {
1768 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
1769 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
1770 f801134a 2019-06-25 stsp view->search_next_done = 1;
1771 f801134a 2019-06-25 stsp return NULL;
1773 f801134a 2019-06-25 stsp s->thread_args.commits_needed = 1;
1774 f801134a 2019-06-25 stsp return trigger_log_thread(0,
1775 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
1776 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
1777 f801134a 2019-06-25 stsp &s->thread_args.need_commits);
1780 1803e47f 2019-06-22 stsp if (match_commit(entry->commit, &view->regex)) {
1781 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1782 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
1785 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1786 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
1788 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1791 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
1792 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
1793 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
1794 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1796 60493ae3 2019-06-20 stsp return err;
1799 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
1800 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1802 60493ae3 2019-06-20 stsp return err;
1807 60493ae3 2019-06-20 stsp return NULL;
1810 60493ae3 2019-06-20 stsp static const struct got_error *
1811 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1812 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
1813 d01904d4 2019-06-25 stsp const char *head_ref_name, const char *path, int check_disk)
1815 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1816 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1817 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1818 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1819 1a76625f 2018-10-22 stsp int errcode;
1821 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1822 ecb28ae0 2018-07-16 stsp if (err != NULL)
1823 ecb28ae0 2018-07-16 stsp goto done;
1825 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1826 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1827 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1829 8b473291 2019-02-21 stsp s->refs = refs;
1830 fb2756b9 2018-08-04 stsp s->repo = repo;
1831 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
1832 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1833 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1834 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1835 5036bf37 2018-09-24 stsp goto done;
1838 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1839 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1840 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1841 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
1842 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
1844 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1846 1a76625f 2018-10-22 stsp goto done;
1847 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1848 1a76625f 2018-10-22 stsp 0, thread_repo);
1850 1a76625f 2018-10-22 stsp goto done;
1852 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1853 1a76625f 2018-10-22 stsp if (errcode) {
1854 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
1855 1a76625f 2018-10-22 stsp goto done;
1858 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1859 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1860 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1861 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1862 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1863 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1864 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1865 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1866 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1867 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1868 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1871 1a76625f 2018-10-22 stsp close_log_view(view);
1872 ba4f502b 2018-08-04 stsp return err;
1875 e5a0f69f 2018-08-18 stsp static const struct got_error *
1876 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1878 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1880 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1881 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1882 2b380cc8 2018-10-24 stsp &s->thread_args);
1883 2b380cc8 2018-10-24 stsp if (errcode)
1884 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
1887 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1888 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1889 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
1890 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1893 e5a0f69f 2018-08-18 stsp static const struct got_error *
1894 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1895 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1897 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1898 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1899 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
1900 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1901 669b5ffa 2018-10-07 stsp int begin_x = 0;
1902 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
1904 e5a0f69f 2018-08-18 stsp switch (ch) {
1906 1e37a5c2 2019-05-12 jcs s->quit = 1;
1909 1e37a5c2 2019-05-12 jcs case KEY_UP:
1912 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1914 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
1915 1e37a5c2 2019-05-12 jcs s->selected--;
1917 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
1918 1144d21a 2019-06-21 stsp &s->commits);
1920 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
1921 a4292ac5 2019-05-12 jcs case CTRL('b'):
1922 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1924 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
1925 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
1926 1e37a5c2 2019-05-12 jcs s->selected = 0;
1929 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
1930 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
1933 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
1936 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1938 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
1939 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1940 1e37a5c2 2019-05-12 jcs s->selected++;
1943 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
1944 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
1945 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
1946 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1947 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1949 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
1950 a4292ac5 2019-05-12 jcs case CTRL('f'): {
1951 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
1952 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
1953 1e37a5c2 2019-05-12 jcs if (first == NULL)
1955 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
1956 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
1957 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
1958 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1959 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1960 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
1961 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
1962 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1963 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
1964 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
1965 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
1967 1e37a5c2 2019-05-12 jcs err = NULL;
1970 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1971 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
1972 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
1973 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
1974 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
1976 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
1979 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
1981 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
1982 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
1983 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
1984 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
1985 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
1988 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1989 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
1991 1e37a5c2 2019-05-12 jcs return err;
1992 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
1994 1e37a5c2 2019-05-12 jcs view_close(diff_view);
1997 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
1998 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2000 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2003 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2005 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2006 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2007 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2008 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2011 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2012 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2014 1e37a5c2 2019-05-12 jcs return err;
2015 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2017 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2020 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2021 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2023 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2025 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2026 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2028 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2029 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2030 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2032 1e37a5c2 2019-05-12 jcs return err;
2033 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2034 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2035 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2036 638f9024 2019-05-13 stsp return got_error_from_errno(
2037 1e37a5c2 2019-05-12 jcs "view_open");
2038 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2039 d01904d4 2019-06-25 stsp s->repo, s->head_ref_name, parent_path, 0);
2041 1e37a5c2 2019-05-12 jcs return err;;
2042 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2043 1e37a5c2 2019-05-12 jcs *new_view = lv;
2045 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2046 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2048 1e37a5c2 2019-05-12 jcs return NULL;
2052 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2053 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2055 d01904d4 2019-06-25 stsp return err;
2056 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2057 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2058 d01904d4 2019-06-25 stsp if (lv == NULL)
2059 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2060 d01904d4 2019-06-25 stsp err = get_head_commit_id(&start_id, s->head_ref_name ?
2061 d01904d4 2019-06-25 stsp s->head_ref_name : GOT_REF_HEAD, s->repo);
2063 d01904d4 2019-06-25 stsp return err;
2064 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2065 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2066 d01904d4 2019-06-25 stsp free(start_id);
2067 d01904d4 2019-06-25 stsp return got_error_from_errno("strdup");
2069 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2070 d01904d4 2019-06-25 stsp s->head_ref_name, in_repo_path, 0);
2072 d01904d4 2019-06-25 stsp return err;;
2073 d01904d4 2019-06-25 stsp *dead_view = view;
2074 d01904d4 2019-06-25 stsp *new_view = lv;
2080 80ddbec8 2018-04-29 stsp return err;
2083 4ed7e80c 2018-05-20 stsp static const struct got_error *
2084 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2086 c2db6724 2019-01-04 stsp const struct got_error *error;
2088 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2089 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2091 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2092 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2094 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2095 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2097 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2098 c2db6724 2019-01-04 stsp if (error != NULL)
2099 c2db6724 2019-01-04 stsp return error;
2101 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2102 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2104 c2db6724 2019-01-04 stsp return NULL;
2107 a915003a 2019-02-05 stsp static void
2108 a915003a 2019-02-05 stsp init_curses(void)
2110 a915003a 2019-02-05 stsp initscr();
2112 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2115 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2116 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2117 a915003a 2019-02-05 stsp curs_set(0);
2118 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2121 c2db6724 2019-01-04 stsp static const struct got_error *
2122 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2124 80ddbec8 2018-04-29 stsp const struct got_error *error;
2125 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2126 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2127 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2128 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2129 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2130 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
2132 04cc582a 2018-08-01 stsp struct tog_view *view;
2134 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2136 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2137 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2138 c2db6724 2019-01-04 stsp NULL) == -1)
2139 80ddbec8 2018-04-29 stsp err(1, "pledge");
2142 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2143 80ddbec8 2018-04-29 stsp switch (ch) {
2145 80ddbec8 2018-04-29 stsp start_commit = optarg;
2148 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2149 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2150 ecb28ae0 2018-07-16 stsp err(1, "-r option");
2153 17020d27 2019-03-07 stsp usage_log();
2154 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2158 80ddbec8 2018-04-29 stsp argc -= optind;
2159 80ddbec8 2018-04-29 stsp argv += optind;
2161 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2162 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2163 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2164 ecb28ae0 2018-07-16 stsp goto done;
2166 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2167 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2168 963f97a1 2019-03-18 stsp goto done;
2169 963f97a1 2019-03-18 stsp error = NULL;
2171 963f97a1 2019-03-18 stsp if (argc == 0) {
2172 963f97a1 2019-03-18 stsp path = strdup("");
2173 963f97a1 2019-03-18 stsp if (path == NULL) {
2174 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2175 ecb28ae0 2018-07-16 stsp goto done;
2177 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2178 963f97a1 2019-03-18 stsp if (worktree) {
2179 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2181 963f97a1 2019-03-18 stsp if (error)
2182 963f97a1 2019-03-18 stsp goto done;
2184 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2185 963f97a1 2019-03-18 stsp if (path == NULL) {
2186 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2187 963f97a1 2019-03-18 stsp goto done;
2191 963f97a1 2019-03-18 stsp usage_log();
2193 963f97a1 2019-03-18 stsp repo_path = worktree ?
2194 963f97a1 2019-03-18 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2195 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2196 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2197 963f97a1 2019-03-18 stsp goto done;
2200 a915003a 2019-02-05 stsp init_curses();
2202 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
2203 80ddbec8 2018-04-29 stsp if (error != NULL)
2204 ecb28ae0 2018-07-16 stsp goto done;
2206 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2207 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2208 c02c541e 2019-03-29 stsp if (error)
2209 c02c541e 2019-03-29 stsp goto done;
2211 15a94983 2018-12-23 stsp if (start_commit == NULL)
2212 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&start_id, worktree ?
2213 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2216 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&start_id, repo,
2217 15a94983 2018-12-23 stsp start_commit);
2218 80ddbec8 2018-04-29 stsp if (error != NULL)
2219 8b473291 2019-02-21 stsp goto done;
2221 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2222 8b473291 2019-02-21 stsp if (error)
2223 ecb28ae0 2018-07-16 stsp goto done;
2225 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2226 04cc582a 2018-08-01 stsp if (view == NULL) {
2227 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2228 04cc582a 2018-08-01 stsp goto done;
2230 d01904d4 2019-06-25 stsp error = open_log_view(view, start_id, &refs, repo, worktree ?
2231 d01904d4 2019-06-25 stsp got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2232 ba4f502b 2018-08-04 stsp if (error)
2233 ba4f502b 2018-08-04 stsp goto done;
2234 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2236 ecb28ae0 2018-07-16 stsp free(repo_path);
2237 ecb28ae0 2018-07-16 stsp free(cwd);
2238 ecb28ae0 2018-07-16 stsp free(path);
2239 899d86c2 2018-05-10 stsp free(start_id);
2241 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2242 ec142235 2019-03-07 stsp if (worktree)
2243 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2244 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2245 80ddbec8 2018-04-29 stsp return error;
2248 4ed7e80c 2018-05-20 stsp __dead static void
2249 9f7d7167 2018-04-29 stsp usage_diff(void)
2252 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2253 9f7d7167 2018-04-29 stsp getprogname());
2257 b304db33 2018-05-20 stsp static char *
2258 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2260 b304db33 2018-05-20 stsp char *line;
2261 b304db33 2018-05-20 stsp size_t linelen;
2262 b304db33 2018-05-20 stsp size_t lineno;
2263 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2265 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2267 b304db33 2018-05-20 stsp *len = linelen;
2268 b304db33 2018-05-20 stsp return line;
2271 4ed7e80c 2018-05-20 stsp static const struct got_error *
2272 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2273 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
2274 c3e9aa98 2019-05-13 jcs char *header)
2276 61e69b96 2018-05-20 stsp const struct got_error *err;
2277 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2278 b304db33 2018-05-20 stsp char *line;
2279 b304db33 2018-05-20 stsp size_t len;
2280 61e69b96 2018-05-20 stsp wchar_t *wline;
2281 e0b650dd 2018-05-20 stsp int width;
2283 26ed57b2 2018-05-19 stsp rewind(f);
2284 f7d12f7e 2018-08-01 stsp werase(view->window);
2286 a3404814 2018-09-02 stsp if (header) {
2287 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
2288 a3404814 2018-09-02 stsp if (err) {
2289 a3404814 2018-09-02 stsp return err;
2292 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2293 a3404814 2018-09-02 stsp wstandout(view->window);
2294 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2295 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2296 a3404814 2018-09-02 stsp wstandend(view->window);
2297 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2298 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2300 a3404814 2018-09-02 stsp if (max_lines <= 1)
2301 a3404814 2018-09-02 stsp return NULL;
2302 a3404814 2018-09-02 stsp max_lines--;
2306 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2307 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2308 26ed57b2 2018-05-19 stsp if (line == NULL) {
2312 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2313 26ed57b2 2018-05-19 stsp free(line);
2317 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2318 61e69b96 2018-05-20 stsp if (err) {
2319 61e69b96 2018-05-20 stsp free(line);
2320 61e69b96 2018-05-20 stsp return err;
2322 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2323 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2324 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2325 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2326 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2327 26ed57b2 2018-05-19 stsp free(line);
2328 2550e4c3 2018-07-13 stsp free(wline);
2329 2550e4c3 2018-07-13 stsp wline = NULL;
2331 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2333 1a57306a 2018-09-02 stsp view_vborder(view);
2335 c3e9aa98 2019-05-13 jcs if (*eof) {
2336 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2337 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2338 c3e9aa98 2019-05-13 jcs nprinted++;
2341 c3e9aa98 2019-05-13 jcs err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2343 c3e9aa98 2019-05-13 jcs return err;
2346 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2347 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2348 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2351 26ed57b2 2018-05-19 stsp return NULL;
2354 abd2672a 2018-12-23 stsp static char *
2355 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2357 abd2672a 2018-12-23 stsp char *p, *s = ctime_r(time, datebuf);
2358 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2360 abd2672a 2018-12-23 stsp *p = '\0';
2364 4ed7e80c 2018-05-20 stsp static const struct got_error *
2365 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2366 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2368 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2369 abd2672a 2018-12-23 stsp char datebuf[26];
2370 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2371 15a94983 2018-12-23 stsp char *id_str = NULL;
2372 45d799e2 2018-12-23 stsp time_t committer_time;
2373 45d799e2 2018-12-23 stsp const char *author, *committer;
2374 8b473291 2019-02-21 stsp char *refs_str = NULL;
2376 8b473291 2019-02-21 stsp if (refs) {
2377 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, commit_id);
2379 8b473291 2019-02-21 stsp return err;
2382 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2384 abd2672a 2018-12-23 stsp return err;
2386 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2387 15a94983 2018-12-23 stsp if (err) {
2388 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2389 15a94983 2018-12-23 stsp goto done;
2392 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2393 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2394 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2395 abd2672a 2018-12-23 stsp goto done;
2397 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2398 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2399 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2400 abd2672a 2018-12-23 stsp goto done;
2402 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2403 abd2672a 2018-12-23 stsp if (fprintf(outfile, "date: %s UTC\n",
2404 45d799e2 2018-12-23 stsp get_datestr(&committer_time, datebuf)) < 0) {
2405 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2406 abd2672a 2018-12-23 stsp goto done;
2408 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2409 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2410 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2411 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2412 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2413 abd2672a 2018-12-23 stsp goto done;
2415 45d799e2 2018-12-23 stsp if (fprintf(outfile, "%s\n",
2416 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(commit)) < 0) {
2417 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2418 abd2672a 2018-12-23 stsp goto done;
2421 abd2672a 2018-12-23 stsp free(id_str);
2422 8b473291 2019-02-21 stsp free(refs_str);
2423 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2424 abd2672a 2018-12-23 stsp return err;
2427 abd2672a 2018-12-23 stsp static const struct got_error *
2428 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2430 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2431 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2432 15a94983 2018-12-23 stsp int obj_type;
2434 511a516b 2018-05-19 stsp f = got_opentemp();
2435 48ae06ee 2018-10-18 stsp if (f == NULL) {
2436 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2437 48ae06ee 2018-10-18 stsp goto done;
2439 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2440 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2441 fb43ecf1 2019-02-11 stsp goto done;
2445 15a94983 2018-12-23 stsp if (s->id1)
2446 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2448 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2450 15a94983 2018-12-23 stsp goto done;
2452 15a94983 2018-12-23 stsp switch (obj_type) {
2453 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2454 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2455 54156555 2018-12-24 stsp s->diff_context, s->repo, f);
2457 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2458 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2459 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
2461 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2462 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2463 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2464 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2466 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2469 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2470 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2471 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2473 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2474 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2475 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2476 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2477 8b473291 2019-02-21 stsp s->repo, f);
2482 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2484 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2485 15a94983 2018-12-23 stsp s->diff_context, s->repo, f);
2489 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2493 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2494 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2495 48ae06ee 2018-10-18 stsp return err;
2498 f5215bb9 2019-02-22 stsp static void
2499 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2501 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2502 f5215bb9 2019-02-22 stsp update_panels();
2503 f5215bb9 2019-02-22 stsp doupdate();
2506 48ae06ee 2018-10-18 stsp static const struct got_error *
2507 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2508 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2509 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2511 48ae06ee 2018-10-18 stsp const struct got_error *err;
2513 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2514 15a94983 2018-12-23 stsp int type1, type2;
2515 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2517 15a94983 2018-12-23 stsp return err;
2518 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2520 15a94983 2018-12-23 stsp return err;
2522 15a94983 2018-12-23 stsp if (type1 != type2)
2523 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2526 15a94983 2018-12-23 stsp if (id1) {
2527 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2528 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2529 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2531 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2533 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2534 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2535 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2536 48ae06ee 2018-10-18 stsp 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.f = NULL;
2540 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2541 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2542 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2543 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2544 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2545 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2547 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2548 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2549 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2551 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2552 48ae06ee 2018-10-18 stsp if (err) {
2553 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2554 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2555 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2556 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2557 48ae06ee 2018-10-18 stsp return err;
2560 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2561 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2562 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2564 5dc9f4bc 2018-08-04 stsp return NULL;
2567 e5a0f69f 2018-08-18 stsp static const struct got_error *
2568 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2570 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2572 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2573 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2574 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2575 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2576 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2577 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2578 e5a0f69f 2018-08-18 stsp return err;
2581 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2582 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2584 a3404814 2018-09-02 stsp const struct got_error *err;
2585 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2586 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2588 a3404814 2018-09-02 stsp if (s->id1) {
2589 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2591 a3404814 2018-09-02 stsp return err;
2593 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2595 a3404814 2018-09-02 stsp return err;
2597 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2598 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2599 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2600 a3404814 2018-09-02 stsp free(id_str1);
2601 a3404814 2018-09-02 stsp free(id_str2);
2602 a3404814 2018-09-02 stsp return err;
2604 a3404814 2018-09-02 stsp free(id_str1);
2605 a3404814 2018-09-02 stsp free(id_str2);
2607 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2608 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2612 15a087fe 2019-02-21 stsp static const struct got_error *
2613 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2614 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2616 d7a04538 2019-02-21 stsp const struct got_error *err;
2617 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
2618 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
2619 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
2621 15a087fe 2019-02-21 stsp free(s->id2);
2622 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2623 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2624 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2626 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2628 d7a04538 2019-02-21 stsp return err;
2629 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
2630 15a087fe 2019-02-21 stsp free(s->id1);
2631 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
2632 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2633 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
2634 15a087fe 2019-02-21 stsp return NULL;
2637 0cf4efb1 2018-09-29 stsp static const struct got_error *
2638 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2639 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2641 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2642 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2643 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
2644 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
2647 e5a0f69f 2018-08-18 stsp switch (ch) {
2649 1e37a5c2 2019-05-12 jcs case KEY_UP:
2650 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
2651 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2653 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2654 a60a9dc4 2019-05-13 jcs case CTRL('b'):
2655 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
2658 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
2659 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
2660 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2663 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2664 1e37a5c2 2019-05-12 jcs if (!s->eof)
2665 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2667 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
2668 a60a9dc4 2019-05-13 jcs case CTRL('f'):
2670 00ba99a7 2019-05-12 jcs if (s->eof)
2673 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
2674 1e37a5c2 2019-05-12 jcs char *line;
2675 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
2676 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2677 1e37a5c2 2019-05-12 jcs if (line == NULL)
2682 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
2683 1e37a5c2 2019-05-12 jcs s->diff_context--;
2684 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2685 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2689 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2690 1e37a5c2 2019-05-12 jcs s->diff_context++;
2691 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2692 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2697 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2699 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2700 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
2701 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
2702 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2705 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2710 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2714 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2715 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2717 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2718 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2722 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2724 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2726 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2727 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
2729 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
2730 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
2731 1e37a5c2 2019-05-12 jcs update_panels();
2732 1e37a5c2 2019-05-12 jcs doupdate();
2734 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
2735 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
2736 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
2737 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
2741 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2746 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
2747 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2750 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2754 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2755 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2757 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2758 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2764 bcbd79e2 2018-08-19 stsp return err;
2767 4ed7e80c 2018-05-20 stsp static const struct got_error *
2768 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2770 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2771 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2772 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2773 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2774 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2775 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2777 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2779 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2781 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2782 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2783 eb6600df 2019-01-04 stsp NULL) == -1)
2784 26ed57b2 2018-05-19 stsp err(1, "pledge");
2787 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2788 26ed57b2 2018-05-19 stsp switch (ch) {
2790 17020d27 2019-03-07 stsp usage_diff();
2791 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2795 26ed57b2 2018-05-19 stsp argc -= optind;
2796 26ed57b2 2018-05-19 stsp argv += optind;
2798 26ed57b2 2018-05-19 stsp if (argc == 0) {
2799 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2800 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2801 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2802 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2803 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2804 15a94983 2018-12-23 stsp id_str1 = argv[0];
2805 15a94983 2018-12-23 stsp id_str2 = argv[1];
2806 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2807 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2808 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2809 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2810 15a94983 2018-12-23 stsp id_str1 = argv[1];
2811 15a94983 2018-12-23 stsp id_str2 = argv[2];
2813 26ed57b2 2018-05-19 stsp usage_diff();
2815 a915003a 2019-02-05 stsp init_curses();
2817 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2818 eb6600df 2019-01-04 stsp if (error)
2819 eb6600df 2019-01-04 stsp goto done;
2821 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
2822 26ed57b2 2018-05-19 stsp if (error)
2823 26ed57b2 2018-05-19 stsp goto done;
2825 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
2826 26ed57b2 2018-05-19 stsp if (error)
2827 26ed57b2 2018-05-19 stsp goto done;
2829 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
2830 26ed57b2 2018-05-19 stsp if (error)
2831 26ed57b2 2018-05-19 stsp goto done;
2833 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2834 8b473291 2019-02-21 stsp if (error)
2835 8b473291 2019-02-21 stsp goto done;
2837 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2838 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2839 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2840 ea5e7bb5 2018-08-01 stsp goto done;
2842 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2843 5dc9f4bc 2018-08-04 stsp if (error)
2844 5dc9f4bc 2018-08-04 stsp goto done;
2845 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2847 c02c541e 2019-03-29 stsp free(repo_path);
2848 26ed57b2 2018-05-19 stsp got_repo_close(repo);
2849 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2850 26ed57b2 2018-05-19 stsp return error;
2853 4ed7e80c 2018-05-20 stsp __dead static void
2854 9f7d7167 2018-04-29 stsp usage_blame(void)
2857 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2858 9f7d7167 2018-04-29 stsp getprogname());
2862 84451b3e 2018-07-10 stsp struct tog_blame_line {
2863 84451b3e 2018-07-10 stsp int annotated;
2864 84451b3e 2018-07-10 stsp struct got_object_id *id;
2867 4ed7e80c 2018-05-20 stsp static const struct got_error *
2868 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2869 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2870 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2871 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2873 84451b3e 2018-07-10 stsp const struct got_error *err;
2874 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2875 84451b3e 2018-07-10 stsp char *line;
2876 84451b3e 2018-07-10 stsp size_t len;
2877 84451b3e 2018-07-10 stsp wchar_t *wline;
2878 b700b5d6 2018-07-10 stsp int width, wlimit;
2879 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2880 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2881 ab089a2a 2018-07-12 stsp char *id_str;
2883 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2885 ab089a2a 2018-07-12 stsp return err;
2887 84451b3e 2018-07-10 stsp rewind(f);
2888 f7d12f7e 2018-08-01 stsp werase(view->window);
2890 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
2891 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2892 ab089a2a 2018-07-12 stsp free(id_str);
2893 ab089a2a 2018-07-12 stsp return err;
2896 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2897 ab089a2a 2018-07-12 stsp free(line);
2898 2550e4c3 2018-07-13 stsp line = NULL;
2899 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2900 a3404814 2018-09-02 stsp wstandout(view->window);
2901 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2902 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2903 a3404814 2018-09-02 stsp wstandend(view->window);
2904 2550e4c3 2018-07-13 stsp free(wline);
2905 2550e4c3 2018-07-13 stsp wline = NULL;
2906 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2907 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2909 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2910 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2911 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
2912 ab089a2a 2018-07-12 stsp free(id_str);
2913 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2915 ab089a2a 2018-07-12 stsp free(id_str);
2916 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2917 3f60a8ef 2018-07-10 stsp free(line);
2918 2550e4c3 2018-07-13 stsp line = NULL;
2920 3f60a8ef 2018-07-10 stsp return err;
2921 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2922 2550e4c3 2018-07-13 stsp free(wline);
2923 2550e4c3 2018-07-13 stsp wline = NULL;
2924 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2925 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2928 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2929 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2930 84451b3e 2018-07-10 stsp if (line == NULL) {
2934 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2935 84451b3e 2018-07-10 stsp free(line);
2939 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2940 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2941 84451b3e 2018-07-10 stsp if (err) {
2942 84451b3e 2018-07-10 stsp free(line);
2943 84451b3e 2018-07-10 stsp return err;
2946 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2947 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2949 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2950 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2951 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2952 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2953 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2954 84451b3e 2018-07-10 stsp char *id_str;
2955 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2956 84451b3e 2018-07-10 stsp if (err) {
2957 84451b3e 2018-07-10 stsp free(line);
2958 2550e4c3 2018-07-13 stsp free(wline);
2959 84451b3e 2018-07-10 stsp return err;
2961 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2962 84451b3e 2018-07-10 stsp free(id_str);
2963 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2965 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2966 ee41ec32 2018-07-10 stsp prev_id = NULL;
2969 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2970 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2971 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2974 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2975 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2976 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2977 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2978 84451b3e 2018-07-10 stsp free(line);
2979 2550e4c3 2018-07-13 stsp free(wline);
2980 2550e4c3 2018-07-13 stsp wline = NULL;
2982 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2984 1a57306a 2018-09-02 stsp view_vborder(view);
2986 84451b3e 2018-07-10 stsp return NULL;
2989 84451b3e 2018-07-10 stsp static const struct got_error *
2990 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2992 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2993 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2994 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2995 1a76625f 2018-10-22 stsp int errcode;
2997 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2998 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2999 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3001 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3002 1a76625f 2018-10-22 stsp if (errcode)
3003 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3005 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3006 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3007 d68a0a7d 2018-07-10 stsp goto done;
3010 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3011 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3013 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3014 d68a0a7d 2018-07-10 stsp if (line->annotated)
3015 d68a0a7d 2018-07-10 stsp goto done;
3017 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3018 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3019 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3020 84451b3e 2018-07-10 stsp goto done;
3022 84451b3e 2018-07-10 stsp line->annotated = 1;
3024 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3025 1a76625f 2018-10-22 stsp if (errcode)
3026 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3027 84451b3e 2018-07-10 stsp return err;
3030 84451b3e 2018-07-10 stsp static void *
3031 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3033 18430de3 2018-07-10 stsp const struct got_error *err;
3034 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3035 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3036 1a76625f 2018-10-22 stsp int errcode;
3038 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3039 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
3041 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3042 1a76625f 2018-10-22 stsp if (errcode)
3043 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3044 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3046 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3047 c9beca56 2018-07-22 stsp ta->repo = NULL;
3048 c9beca56 2018-07-22 stsp *ta->complete = 1;
3050 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3051 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3052 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3054 18430de3 2018-07-10 stsp return (void *)err;
3057 245d91c1 2018-07-12 stsp static struct got_object_id *
3058 15a94983 2018-12-23 stsp get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3059 15a94983 2018-12-23 stsp int selected_line)
3061 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3063 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3064 245d91c1 2018-07-12 stsp if (!line->annotated)
3065 245d91c1 2018-07-12 stsp return NULL;
3067 245d91c1 2018-07-12 stsp return line->id;
3070 b880a918 2018-07-10 stsp static const struct got_error *
3071 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3073 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3076 245d91c1 2018-07-12 stsp if (blame->thread) {
3077 1a76625f 2018-10-22 stsp int errcode;
3078 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3079 1a76625f 2018-10-22 stsp if (errcode)
3080 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3081 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3082 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3083 1a76625f 2018-10-22 stsp if (errcode)
3084 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3085 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3086 1a76625f 2018-10-22 stsp if (errcode)
3087 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3088 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3089 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3090 245d91c1 2018-07-12 stsp err = NULL;
3091 245d91c1 2018-07-12 stsp blame->thread = NULL;
3093 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3094 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3095 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3097 245d91c1 2018-07-12 stsp if (blame->f) {
3098 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3099 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3100 245d91c1 2018-07-12 stsp blame->f = NULL;
3102 57670559 2018-12-23 stsp if (blame->lines) {
3103 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3104 57670559 2018-12-23 stsp free(blame->lines[i].id);
3105 57670559 2018-12-23 stsp free(blame->lines);
3106 57670559 2018-12-23 stsp blame->lines = NULL;
3108 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3109 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3111 245d91c1 2018-07-12 stsp return err;
3114 245d91c1 2018-07-12 stsp static const struct got_error *
3115 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3116 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3117 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3118 245d91c1 2018-07-12 stsp struct got_repository *repo)
3120 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3121 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3122 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3123 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3124 15a94983 2018-12-23 stsp int obj_type;
3126 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3128 15a94983 2018-12-23 stsp return err;
3129 15a94983 2018-12-23 stsp if (obj_id == NULL)
3130 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
3132 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3134 84451b3e 2018-07-10 stsp goto done;
3136 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3137 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3138 84451b3e 2018-07-10 stsp goto done;
3141 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3143 a70480e0 2018-06-23 stsp goto done;
3144 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3145 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3146 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3147 84451b3e 2018-07-10 stsp goto done;
3149 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3150 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
3152 84451b3e 2018-07-10 stsp goto done;
3154 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3155 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3156 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3157 84451b3e 2018-07-10 stsp goto done;
3160 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3162 bd24772e 2018-07-11 stsp goto done;
3164 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3165 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3166 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3167 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3168 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3169 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3170 245d91c1 2018-07-12 stsp goto done;
3172 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3174 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3175 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3176 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3177 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3178 245d91c1 2018-07-12 stsp *blame_complete = 0;
3182 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3183 27d434c2 2018-09-15 stsp free(obj_id);
3185 245d91c1 2018-07-12 stsp stop_blame(blame);
3186 245d91c1 2018-07-12 stsp return err;
3189 245d91c1 2018-07-12 stsp static const struct got_error *
3190 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3191 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3192 8b473291 2019-02-21 stsp struct got_repository *repo)
3194 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3195 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3197 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3199 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3201 7cbe629d 2018-08-04 stsp return err;
3203 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3204 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3205 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3206 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3207 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3208 fb2756b9 2018-08-04 stsp s->path = path;
3209 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
3210 638f9024 2019-05-13 stsp return got_error_from_errno("open_blame_view");
3211 fb2756b9 2018-08-04 stsp s->repo = repo;
3212 8b473291 2019-02-21 stsp s->refs = refs;
3213 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3214 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3216 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3217 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3218 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3219 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
3220 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
3222 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3223 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3224 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3225 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3228 e5a0f69f 2018-08-18 stsp static const struct got_error *
3229 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3231 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3232 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3234 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3235 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3237 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3238 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3239 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3240 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3241 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3244 e5a0f69f 2018-08-18 stsp free(s->path);
3246 e5a0f69f 2018-08-18 stsp return err;
3249 7cbe629d 2018-08-04 stsp static const struct got_error *
3250 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
3252 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3254 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
3255 6c4c42e0 2019-06-24 stsp return NULL;
3258 6c4c42e0 2019-06-24 stsp static int
3259 6c4c42e0 2019-06-24 stsp match_line(const char *line, regex_t *regex)
3261 6c4c42e0 2019-06-24 stsp regmatch_t regmatch;
3263 6c4c42e0 2019-06-24 stsp return regexec(regex, line, 1, ®match, 0) == 0;
3267 6c4c42e0 2019-06-24 stsp static const struct got_error *
3268 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
3270 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3271 6c4c42e0 2019-06-24 stsp int lineno;
3273 6c4c42e0 2019-06-24 stsp if (!view->searching) {
3274 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3275 6c4c42e0 2019-06-24 stsp return NULL;
3278 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3279 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3280 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
3282 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
3284 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3285 6c4c42e0 2019-06-24 stsp lineno = 1;
3287 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3290 6c4c42e0 2019-06-24 stsp while (1) {
3291 6c4c42e0 2019-06-24 stsp char *line = NULL;
3292 6c4c42e0 2019-06-24 stsp off_t offset;
3293 6c4c42e0 2019-06-24 stsp size_t len;
3295 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
3296 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
3297 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3298 6c4c42e0 2019-06-24 stsp free(line);
3302 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3303 6c4c42e0 2019-06-24 stsp lineno = 1;
3305 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3308 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
3309 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3310 6c4c42e0 2019-06-24 stsp free(line);
3311 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
3313 6c4c42e0 2019-06-24 stsp free(line);
3314 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
3315 2246482e 2019-06-25 stsp if (line && match_line(line, &view->regex)) {
3316 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3317 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
3318 6c4c42e0 2019-06-24 stsp free(line);
3321 6c4c42e0 2019-06-24 stsp free(line);
3322 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3328 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3329 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
3330 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
3333 6c4c42e0 2019-06-24 stsp return NULL;
3336 6c4c42e0 2019-06-24 stsp static const struct got_error *
3337 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
3339 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3340 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
3341 2b380cc8 2018-10-24 stsp int errcode;
3343 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
3344 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3345 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
3346 2b380cc8 2018-10-24 stsp if (errcode)
3347 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3350 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3351 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3352 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
3353 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
3355 669b5ffa 2018-10-07 stsp view_vborder(view);
3356 e5a0f69f 2018-08-18 stsp return err;
3359 e5a0f69f 2018-08-18 stsp static const struct got_error *
3360 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3361 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3363 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
3364 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
3365 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3366 669b5ffa 2018-10-07 stsp int begin_x = 0;
3368 e5a0f69f 2018-08-18 stsp switch (ch) {
3370 1e37a5c2 2019-05-12 jcs s->done = 1;
3373 1e37a5c2 2019-05-12 jcs case KEY_UP:
3374 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
3375 1e37a5c2 2019-05-12 jcs s->selected_line--;
3376 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
3377 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3378 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3380 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3381 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
3382 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3385 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3386 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3387 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3389 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3392 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3393 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3394 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3395 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3396 1e37a5c2 2019-05-12 jcs s->selected_line++;
3397 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3398 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3399 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3402 1e37a5c2 2019-05-12 jcs case 'p': {
3403 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3404 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3405 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3406 1e37a5c2 2019-05-12 jcs if (id == NULL)
3408 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3409 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3410 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3411 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3412 1e37a5c2 2019-05-12 jcs int obj_type;
3413 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3414 1e37a5c2 2019-05-12 jcs s->repo, id);
3417 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3418 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3419 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3420 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3423 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3424 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3425 1e37a5c2 2019-05-12 jcs pid->id, s->path);
3426 e5a0f69f 2018-08-18 stsp if (err) {
3427 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
3428 1e37a5c2 2019-05-12 jcs err = NULL;
3429 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3432 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
3434 1e37a5c2 2019-05-12 jcs free(blob_id);
3435 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
3436 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
3437 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3440 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3442 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3444 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
3445 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
3447 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3452 1e37a5c2 2019-05-12 jcs s->done = 1;
3453 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3454 1e37a5c2 2019-05-12 jcs s->done = 0;
3455 1e37a5c2 2019-05-12 jcs if (thread_err)
3457 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3458 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
3459 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3460 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3461 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
3462 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
3467 1e37a5c2 2019-05-12 jcs case 'B': {
3468 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
3469 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
3470 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
3472 1e37a5c2 2019-05-12 jcs s->done = 1;
3473 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3474 1e37a5c2 2019-05-12 jcs s->done = 0;
3475 1e37a5c2 2019-05-12 jcs if (thread_err)
3477 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3478 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
3479 1e37a5c2 2019-05-12 jcs s->blamed_commit =
3480 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
3481 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3482 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3483 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
3484 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
3489 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3490 1e37a5c2 2019-05-12 jcs case '\r': {
3491 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3492 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
3493 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
3494 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3495 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3496 1e37a5c2 2019-05-12 jcs if (id == NULL)
3498 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
3501 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
3502 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
3503 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3504 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3505 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3506 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
3507 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3508 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
3511 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
3512 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
3513 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3515 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3518 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3519 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3522 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
3524 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3527 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
3528 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
3530 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3535 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3537 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3538 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
3539 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
3542 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3543 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
3544 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3545 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3548 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
3549 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
3550 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
3551 1e37a5c2 2019-05-12 jcs view->nlines - 2;
3553 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
3554 1e37a5c2 2019-05-12 jcs s->blame.nlines -
3555 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
3557 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3558 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
3559 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3560 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3566 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
3569 a70480e0 2018-06-23 stsp static const struct got_error *
3570 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
3572 a70480e0 2018-06-23 stsp const struct got_error *error;
3573 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
3574 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3575 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
3576 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3577 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3578 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
3580 e1cd8fed 2018-08-01 stsp struct tog_view *view;
3582 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3584 a70480e0 2018-06-23 stsp #ifndef PROFILE
3585 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3586 8e94dd5b 2019-01-04 stsp NULL) == -1)
3587 a70480e0 2018-06-23 stsp err(1, "pledge");
3590 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3591 a70480e0 2018-06-23 stsp switch (ch) {
3593 a70480e0 2018-06-23 stsp commit_id_str = optarg;
3596 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3597 69069811 2018-08-02 stsp if (repo_path == NULL)
3598 69069811 2018-08-02 stsp err(1, "-r option");
3601 17020d27 2019-03-07 stsp usage_blame();
3602 a70480e0 2018-06-23 stsp /* NOTREACHED */
3606 a70480e0 2018-06-23 stsp argc -= optind;
3607 a70480e0 2018-06-23 stsp argv += optind;
3609 69069811 2018-08-02 stsp if (argc == 1)
3610 69069811 2018-08-02 stsp path = argv[0];
3612 a70480e0 2018-06-23 stsp usage_blame();
3614 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
3615 69069811 2018-08-02 stsp if (cwd == NULL) {
3616 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3617 69069811 2018-08-02 stsp goto done;
3619 69069811 2018-08-02 stsp if (repo_path == NULL) {
3620 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3621 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3622 69069811 2018-08-02 stsp goto done;
3624 eb41ed75 2019-02-05 stsp error = NULL;
3625 eb41ed75 2019-02-05 stsp if (worktree) {
3626 eb41ed75 2019-02-05 stsp repo_path =
3627 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3628 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
3629 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3630 eb41ed75 2019-02-05 stsp if (error)
3631 eb41ed75 2019-02-05 stsp goto done;
3633 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
3634 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
3635 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3636 eb41ed75 2019-02-05 stsp goto done;
3641 a915003a 2019-02-05 stsp init_curses();
3643 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
3644 c02c541e 2019-03-29 stsp if (error != NULL)
3645 8e94dd5b 2019-01-04 stsp goto done;
3647 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3648 c02c541e 2019-03-29 stsp if (error)
3649 92205607 2019-01-04 stsp goto done;
3651 eb41ed75 2019-02-05 stsp if (worktree) {
3652 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3653 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3654 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3655 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3656 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3657 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3658 eb41ed75 2019-02-05 stsp path) == -1) {
3659 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3660 eb41ed75 2019-02-05 stsp goto done;
3662 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3665 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3667 eb41ed75 2019-02-05 stsp if (error)
3668 69069811 2018-08-02 stsp goto done;
3670 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
3671 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
3672 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3673 a70480e0 2018-06-23 stsp if (error != NULL)
3674 66b4983c 2018-06-23 stsp goto done;
3675 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3676 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
3678 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
3679 15a94983 2018-12-23 stsp commit_id_str);
3681 a19e88aa 2018-06-23 stsp if (error != NULL)
3682 8b473291 2019-02-21 stsp goto done;
3684 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
3685 8b473291 2019-02-21 stsp if (error)
3686 a19e88aa 2018-06-23 stsp goto done;
3688 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3689 e1cd8fed 2018-08-01 stsp if (view == NULL) {
3690 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3691 e1cd8fed 2018-08-01 stsp goto done;
3693 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3694 7cbe629d 2018-08-04 stsp if (error)
3695 7cbe629d 2018-08-04 stsp goto done;
3696 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3698 69069811 2018-08-02 stsp free(repo_path);
3699 69069811 2018-08-02 stsp free(cwd);
3700 a70480e0 2018-06-23 stsp free(commit_id);
3701 eb41ed75 2019-02-05 stsp if (worktree)
3702 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
3704 a70480e0 2018-06-23 stsp got_repo_close(repo);
3705 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3706 a70480e0 2018-06-23 stsp return error;
3709 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3710 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
3711 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
3712 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
3713 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
3714 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
3715 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
3717 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3718 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
3719 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
3720 ffd1d5e5 2018-06-23 stsp int width, n;
3722 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
3724 f7d12f7e 2018-08-01 stsp werase(view->window);
3726 ffd1d5e5 2018-06-23 stsp if (limit == 0)
3727 ffd1d5e5 2018-06-23 stsp return NULL;
3729 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
3731 ffd1d5e5 2018-06-23 stsp return err;
3732 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3733 a3404814 2018-09-02 stsp wstandout(view->window);
3734 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3735 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3736 a3404814 2018-09-02 stsp wstandend(view->window);
3737 2550e4c3 2018-07-13 stsp free(wline);
3738 2550e4c3 2018-07-13 stsp wline = NULL;
3739 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3740 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3741 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3742 ffd1d5e5 2018-06-23 stsp return NULL;
3743 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
3745 ce52c690 2018-06-23 stsp return err;
3746 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3747 2550e4c3 2018-07-13 stsp free(wline);
3748 2550e4c3 2018-07-13 stsp wline = NULL;
3749 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3750 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3751 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3752 ffd1d5e5 2018-06-23 stsp return NULL;
3753 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3754 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
3755 a1eca9bb 2018-06-23 stsp return NULL;
3757 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3758 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
3759 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
3760 0cf4efb1 2018-09-29 stsp if (view->focussed)
3761 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3762 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
3764 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
3765 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
3766 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3767 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3768 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3769 ffd1d5e5 2018-06-23 stsp return NULL;
3773 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
3774 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3777 ffd1d5e5 2018-06-23 stsp while (te) {
3778 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
3780 1d13200f 2018-07-12 stsp if (show_ids) {
3781 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
3783 638f9024 2019-05-13 stsp return got_error_from_errno(
3784 230a42bd 2019-05-11 jcs "got_object_id_str");
3786 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3787 d8355ef1 2019-02-10 stsp te->name, S_ISDIR(te->mode) ? "/" :
3788 d8355ef1 2019-02-10 stsp ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3789 1d13200f 2018-07-12 stsp free(id_str);
3790 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3792 1d13200f 2018-07-12 stsp free(id_str);
3793 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
3794 ffd1d5e5 2018-06-23 stsp if (err) {
3795 ffd1d5e5 2018-06-23 stsp free(line);
3798 ffd1d5e5 2018-06-23 stsp if (n == selected) {
3799 0cf4efb1 2018-09-29 stsp if (view->focussed)
3800 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3801 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
3803 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3804 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3805 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3806 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
3807 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3808 ffd1d5e5 2018-06-23 stsp free(line);
3809 2550e4c3 2018-07-13 stsp free(wline);
3810 2550e4c3 2018-07-13 stsp wline = NULL;
3812 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3813 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
3814 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3816 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3819 ffd1d5e5 2018-06-23 stsp return err;
3822 ffd1d5e5 2018-06-23 stsp static void
3823 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
3824 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
3825 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
3827 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
3830 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
3833 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3834 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
3835 ffd1d5e5 2018-06-23 stsp if (!isroot)
3836 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3840 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
3841 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
3842 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
3843 ffd1d5e5 2018-06-23 stsp prev = te;
3844 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3846 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
3847 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3849 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3850 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3853 768394f3 2019-01-24 stsp static int
3854 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3855 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
3856 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
3858 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
3859 ffd1d5e5 2018-06-23 stsp int n = 0;
3861 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
3862 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3864 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
3865 768394f3 2019-01-24 stsp last = last_displayed_entry;
3866 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
3867 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
3868 768394f3 2019-01-24 stsp if (last) {
3869 768394f3 2019-01-24 stsp *first_displayed_entry = next;
3870 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
3876 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3877 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
3878 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
3880 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
3881 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
3882 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
3884 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
3885 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
3887 ce52c690 2018-06-23 stsp len += strlen(te->name);
3889 ce52c690 2018-06-23 stsp *path = calloc(1, len);
3890 ffd1d5e5 2018-06-23 stsp if (path == NULL)
3891 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
3893 ce52c690 2018-06-23 stsp (*path)[0] = '/';
3894 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
3895 d9765a41 2018-06-23 stsp while (pt) {
3896 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3897 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3898 cb2ebc8a 2018-06-23 stsp goto done;
3900 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
3901 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3902 cb2ebc8a 2018-06-23 stsp goto done;
3904 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3907 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3908 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3909 ce52c690 2018-06-23 stsp goto done;
3913 ce52c690 2018-06-23 stsp if (err) {
3914 ce52c690 2018-06-23 stsp free(*path);
3915 ce52c690 2018-06-23 stsp *path = NULL;
3917 ce52c690 2018-06-23 stsp return err;
3920 ce52c690 2018-06-23 stsp static const struct got_error *
3921 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3922 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3923 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3924 8b473291 2019-02-21 stsp struct got_repository *repo)
3926 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3927 ce52c690 2018-06-23 stsp char *path;
3928 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3930 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3932 ce52c690 2018-06-23 stsp return err;
3934 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3935 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3936 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3938 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
3939 e5a0f69f 2018-08-18 stsp if (err) {
3940 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3941 e5a0f69f 2018-08-18 stsp free(path);
3943 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3944 69efd4c4 2018-07-18 stsp return err;
3947 69efd4c4 2018-07-18 stsp static const struct got_error *
3948 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3949 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3950 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3951 8b473291 2019-02-21 stsp struct got_repository *repo)
3953 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3954 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3955 69efd4c4 2018-07-18 stsp char *path;
3957 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3958 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3959 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3961 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3963 69efd4c4 2018-07-18 stsp return err;
3965 d01904d4 2019-06-25 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
3967 e5a0f69f 2018-08-18 stsp view_close(log_view);
3969 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3970 cb2ebc8a 2018-06-23 stsp free(path);
3971 cb2ebc8a 2018-06-23 stsp return err;
3974 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3975 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3976 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3977 8b473291 2019-02-21 stsp struct got_repository *repo)
3979 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3980 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3981 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3983 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3985 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3986 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3987 ffd1d5e5 2018-06-23 stsp goto done;
3989 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3990 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3991 ffd1d5e5 2018-06-23 stsp goto done;
3994 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3995 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3996 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3997 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3998 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3999 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4000 6484ec90 2018-09-29 stsp goto done;
4002 8b473291 2019-02-21 stsp s->refs = refs;
4003 fb2756b9 2018-08-04 stsp s->repo = repo;
4005 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
4006 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
4007 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
4008 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
4009 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
4011 ad80ab7b 2018-08-04 stsp free(commit_id_str);
4012 6484ec90 2018-09-29 stsp if (err) {
4013 fb2756b9 2018-08-04 stsp free(s->tree_label);
4014 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4016 ad80ab7b 2018-08-04 stsp return err;
4019 e5a0f69f 2018-08-18 stsp static const struct got_error *
4020 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
4022 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4024 fb2756b9 2018-08-04 stsp free(s->tree_label);
4025 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4026 6484ec90 2018-09-29 stsp free(s->commit_id);
4027 6484ec90 2018-09-29 stsp s->commit_id = NULL;
4028 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
4029 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
4030 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
4031 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
4032 ad80ab7b 2018-08-04 stsp free(parent);
4035 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
4036 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
4037 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
4039 7c32bd05 2019-06-22 stsp return NULL;
4042 7c32bd05 2019-06-22 stsp static const struct got_error *
4043 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
4045 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4047 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
4048 7c32bd05 2019-06-22 stsp return NULL;
4051 7c32bd05 2019-06-22 stsp static int
4052 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4054 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
4056 7c32bd05 2019-06-22 stsp return regexec(regex, te->name, 1, ®match, 0) == 0;
4059 7c32bd05 2019-06-22 stsp static const struct got_error *
4060 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
4062 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4063 7c32bd05 2019-06-22 stsp struct got_tree_entry *entry, *te;
4065 7c32bd05 2019-06-22 stsp if (!view->searching) {
4066 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4067 7c32bd05 2019-06-22 stsp return NULL;
4070 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4071 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
4072 7c32bd05 2019-06-22 stsp if (s->selected_entry)
4073 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4075 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4078 7c32bd05 2019-06-22 stsp if (s->selected_entry == NULL) {
4079 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4080 7c32bd05 2019-06-22 stsp entry = te;
4082 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4083 7c32bd05 2019-06-22 stsp entry = te;
4084 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) ==
4085 7c32bd05 2019-06-22 stsp s->selected_entry)
4091 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4092 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4094 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4095 7c32bd05 2019-06-22 stsp entry = te;
4099 7c32bd05 2019-06-22 stsp while (1) {
4100 7c32bd05 2019-06-22 stsp if (entry == NULL) {
4101 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
4102 ac66afb8 2019-06-24 stsp view->search_next_done = 1;
4103 ac66afb8 2019-06-24 stsp return NULL;
4105 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4106 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4108 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4109 7c32bd05 2019-06-22 stsp entry = te;
4113 7c32bd05 2019-06-22 stsp if (match_tree_entry(entry, &view->regex)) {
4114 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4115 7c32bd05 2019-06-22 stsp s->matched_entry = entry;
4119 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4120 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(entry, entry);
4122 7c32bd05 2019-06-22 stsp if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4123 7c32bd05 2019-06-22 stsp entry = NULL;
4125 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4126 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) == entry) {
4127 7c32bd05 2019-06-22 stsp entry = te;
4135 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4136 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
4137 7c32bd05 2019-06-22 stsp s->selected = 0;
4140 e5a0f69f 2018-08-18 stsp return NULL;
4143 ad80ab7b 2018-08-04 stsp static const struct got_error *
4144 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
4146 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
4147 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4148 e5a0f69f 2018-08-18 stsp char *parent_path;
4150 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
4152 e5a0f69f 2018-08-18 stsp return err;
4154 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
4155 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
4156 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4157 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
4158 e5a0f69f 2018-08-18 stsp free(parent_path);
4160 669b5ffa 2018-10-07 stsp view_vborder(view);
4161 e5a0f69f 2018-08-18 stsp return err;
4164 e5a0f69f 2018-08-18 stsp static const struct got_error *
4165 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4166 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4168 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4169 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
4170 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
4171 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
4173 e5a0f69f 2018-08-18 stsp switch (ch) {
4175 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
4178 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
4180 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4181 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4182 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
4183 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
4184 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
4185 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4186 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4188 1e37a5c2 2019-05-12 jcs return err;
4189 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
4191 1e37a5c2 2019-05-12 jcs view_close(log_view);
4194 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
4195 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4197 1e37a5c2 2019-05-12 jcs *new_view = log_view;
4200 1e37a5c2 2019-05-12 jcs case KEY_UP:
4201 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
4202 1e37a5c2 2019-05-12 jcs s->selected--;
4203 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
4206 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
4208 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
4209 1e37a5c2 2019-05-12 jcs s->entries, s->tree == s->root);
4211 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4212 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
4213 1e37a5c2 2019-05-12 jcs MAX(0, view->nlines - 4 - s->selected), s->entries,
4214 1e37a5c2 2019-05-12 jcs s->tree == s->root);
4215 1e37a5c2 2019-05-12 jcs s->selected = 0;
4216 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_FIRST(&s->entries->head) ==
4217 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
4218 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
4221 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4222 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
4223 1e37a5c2 2019-05-12 jcs s->selected++;
4226 00ba99a7 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4227 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
4229 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
4230 1e37a5c2 2019-05-12 jcs s->last_displayed_entry, s->entries);
4232 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4233 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4235 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
4236 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
4237 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4240 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
4241 1e37a5c2 2019-05-12 jcs view->nlines, s->last_displayed_entry, s->entries);
4242 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
4243 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
4244 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
4245 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
4247 1e37a5c2 2019-05-12 jcs ndisplayed++;
4248 1e37a5c2 2019-05-12 jcs te = SIMPLEQ_NEXT(te, entry);
4249 1e37a5c2 2019-05-12 jcs } while (te);
4250 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
4253 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4255 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4256 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4257 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
4258 1e37a5c2 2019-05-12 jcs /* user selected '..' */
4259 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
4261 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
4262 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
4264 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
4265 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
4266 1e37a5c2 2019-05-12 jcs s->entries =
4267 1e37a5c2 2019-05-12 jcs got_object_tree_get_entries(s->tree);
4268 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
4269 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
4270 1e37a5c2 2019-05-12 jcs s->selected_entry =
4271 1e37a5c2 2019-05-12 jcs parent->selected_entry;
4272 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
4273 1e37a5c2 2019-05-12 jcs free(parent);
4274 1e37a5c2 2019-05-12 jcs } else if (S_ISDIR(s->selected_entry->mode)) {
4275 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
4276 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&subtree,
4277 1e37a5c2 2019-05-12 jcs s->repo, s->selected_entry->id);
4280 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
4281 941e9f74 2019-05-21 stsp if (err) {
4282 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
4285 1e37a5c2 2019-05-12 jcs } else if (S_ISREG(s->selected_entry->mode)) {
4286 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
4287 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
4288 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
4290 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
4291 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
4292 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
4295 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
4296 669b5ffa 2018-10-07 stsp err = view_close_child(view);
4298 669b5ffa 2018-10-07 stsp return err;
4299 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
4300 669b5ffa 2018-10-07 stsp if (err) {
4301 1e37a5c2 2019-05-12 jcs view_close(blame_view);
4304 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
4305 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
4307 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
4310 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4311 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
4312 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4318 ffd1d5e5 2018-06-23 stsp return err;
4321 ffd1d5e5 2018-06-23 stsp __dead static void
4322 ffd1d5e5 2018-06-23 stsp usage_tree(void)
4325 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4326 ffd1d5e5 2018-06-23 stsp getprogname());
4330 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4331 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
4333 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
4334 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
4335 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4336 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
4337 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4338 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
4339 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
4340 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
4342 5221c383 2018-08-01 stsp struct tog_view *view;
4344 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4346 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
4347 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4348 d188b9a6 2019-01-04 stsp NULL) == -1)
4349 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
4352 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
4353 ffd1d5e5 2018-06-23 stsp switch (ch) {
4355 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
4358 17020d27 2019-03-07 stsp usage_tree();
4359 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
4363 ffd1d5e5 2018-06-23 stsp argc -= optind;
4364 ffd1d5e5 2018-06-23 stsp argv += optind;
4366 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
4367 52185f70 2019-02-05 stsp struct got_worktree *worktree;
4368 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
4369 52185f70 2019-02-05 stsp if (cwd == NULL)
4370 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
4371 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4372 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4373 52185f70 2019-02-05 stsp goto done;
4374 52185f70 2019-02-05 stsp if (worktree) {
4375 52185f70 2019-02-05 stsp free(cwd);
4376 52185f70 2019-02-05 stsp repo_path =
4377 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4378 52185f70 2019-02-05 stsp got_worktree_close(worktree);
4380 52185f70 2019-02-05 stsp repo_path = cwd;
4381 52185f70 2019-02-05 stsp if (repo_path == NULL) {
4382 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4383 52185f70 2019-02-05 stsp goto done;
4385 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
4386 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
4387 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
4388 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
4390 ffd1d5e5 2018-06-23 stsp usage_log();
4392 a915003a 2019-02-05 stsp init_curses();
4394 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
4395 c02c541e 2019-03-29 stsp if (error != NULL)
4396 52185f70 2019-02-05 stsp goto done;
4398 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4399 c02c541e 2019-03-29 stsp if (error)
4400 52185f70 2019-02-05 stsp goto done;
4402 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
4403 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4405 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
4406 15a94983 2018-12-23 stsp commit_id_arg);
4407 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4408 ffd1d5e5 2018-06-23 stsp goto done;
4410 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4411 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4412 ffd1d5e5 2018-06-23 stsp goto done;
4414 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
4415 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
4416 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4417 8b473291 2019-02-21 stsp goto done;
4419 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
4420 8b473291 2019-02-21 stsp if (error)
4421 ffd1d5e5 2018-06-23 stsp goto done;
4423 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4424 5221c383 2018-08-01 stsp if (view == NULL) {
4425 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4426 5221c383 2018-08-01 stsp goto done;
4428 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
4429 ad80ab7b 2018-08-04 stsp if (error)
4430 ad80ab7b 2018-08-04 stsp goto done;
4431 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4433 52185f70 2019-02-05 stsp free(repo_path);
4434 ffd1d5e5 2018-06-23 stsp free(commit_id);
4435 ffd1d5e5 2018-06-23 stsp if (commit)
4436 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
4438 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
4440 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
4441 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4442 ffd1d5e5 2018-06-23 stsp return error;
4445 4ed7e80c 2018-05-20 stsp __dead static void
4446 9f7d7167 2018-04-29 stsp usage(void)
4448 5e070240 2019-06-22 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n", getprogname());
4452 c2301be8 2018-04-30 stsp static char **
4453 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
4455 c2301be8 2018-04-30 stsp char **argv;
4456 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
4458 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
4459 c2301be8 2018-04-30 stsp if (argv == NULL)
4460 c2301be8 2018-04-30 stsp err(1, "calloc");
4461 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
4462 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
4463 c2301be8 2018-04-30 stsp err(1, "calloc");
4464 c2301be8 2018-04-30 stsp if (arg1) {
4465 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
4466 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
4467 c2301be8 2018-04-30 stsp err(1, "calloc");
4470 c2301be8 2018-04-30 stsp return argv;
4474 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
4476 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
4477 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
4478 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
4479 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
4481 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
4483 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
4484 9f7d7167 2018-04-29 stsp switch (ch) {
4486 9f7d7167 2018-04-29 stsp hflag = 1;
4490 9f7d7167 2018-04-29 stsp /* NOTREACHED */
4494 9f7d7167 2018-04-29 stsp argc -= optind;
4495 9f7d7167 2018-04-29 stsp argv += optind;
4496 9f7d7167 2018-04-29 stsp optind = 0;
4497 c2301be8 2018-04-30 stsp optreset = 1;
4499 c2301be8 2018-04-30 stsp if (argc == 0) {
4500 f29d3e89 2018-06-23 stsp if (hflag)
4502 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
4503 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
4504 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
4509 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
4510 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
4511 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
4512 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
4513 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
4514 9f7d7167 2018-04-29 stsp if (hflag)
4515 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
4519 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
4520 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
4521 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
4522 c2301be8 2018-04-30 stsp if (repo_path) {
4523 c2301be8 2018-04-30 stsp struct got_repository *repo;
4524 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
4525 c2301be8 2018-04-30 stsp if (error == NULL)
4526 c2301be8 2018-04-30 stsp got_repo_close(repo);
4528 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath",
4530 c2301be8 2018-04-30 stsp if (error) {
4531 f29d3e89 2018-06-23 stsp if (hflag) {
4532 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
4533 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
4537 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
4538 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
4539 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
4540 ad7de8d9 2018-04-30 stsp free(repo_path);
4543 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
4544 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
4546 c2301be8 2018-04-30 stsp free(repo_path);
4550 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4551 9f7d7167 2018-04-29 stsp if (error)
4552 9f7d7167 2018-04-29 stsp goto done;
4555 c2301be8 2018-04-30 stsp free(cmd_argv);
4556 9f7d7167 2018-04-29 stsp if (error)
4557 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);