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>
40 9f7d7167 2018-04-29 stsp #include "got_error.h"
41 80ddbec8 2018-04-29 stsp #include "got_object.h"
42 80ddbec8 2018-04-29 stsp #include "got_reference.h"
43 80ddbec8 2018-04-29 stsp #include "got_repository.h"
44 80ddbec8 2018-04-29 stsp #include "got_diff.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
47 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
48 a70480e0 2018-06-23 stsp #include "got_blame.h"
49 c2db6724 2019-01-04 stsp #include "got_privsep.h"
50 b7165be3 2019-02-05 stsp #include "got_worktree.h"
53 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
61 9f7d7167 2018-04-29 stsp #ifndef nitems
62 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
65 9f7d7167 2018-04-29 stsp struct tog_cmd {
66 c2301be8 2018-04-30 stsp const char *name;
67 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
68 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
69 c2301be8 2018-04-30 stsp const char *descr;
72 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
73 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
74 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
75 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
76 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
78 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
79 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
80 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
81 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
83 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
84 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
85 9f7d7167 2018-04-29 stsp "show repository history" },
86 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
87 9f7d7167 2018-04-29 stsp "compare files and directories" },
88 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
89 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
90 ffd1d5e5 2018-06-23 stsp { "tree", cmd_tree, usage_tree,
91 ffd1d5e5 2018-06-23 stsp "browse trees in repository" },
94 d6b05b5b 2018-08-04 stsp enum tog_view_type {
95 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
96 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
97 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
98 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
101 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
102 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
103 ba4f502b 2018-08-04 stsp struct got_object_id *id;
104 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
107 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
108 ba4f502b 2018-08-04 stsp struct commit_queue {
109 ba4f502b 2018-08-04 stsp int ncommits;
110 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
113 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
114 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
116 15a087fe 2019-02-21 stsp int first_displayed_line;
117 15a087fe 2019-02-21 stsp int last_displayed_line;
119 15a087fe 2019-02-21 stsp int diff_context;
120 15a087fe 2019-02-21 stsp struct got_repository *repo;
121 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
123 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
124 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
127 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
129 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
130 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
131 1a76625f 2018-10-22 stsp int commits_needed;
132 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
133 1a76625f 2018-10-22 stsp struct commit_queue *commits;
134 1a76625f 2018-10-22 stsp const char *in_repo_path;
135 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
136 1a76625f 2018-10-22 stsp struct got_repository *repo;
137 1a76625f 2018-10-22 stsp int log_complete;
138 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
139 1a76625f 2018-10-22 stsp struct tog_view *view;
140 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
141 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
144 1a76625f 2018-10-22 stsp struct tog_log_view_state {
145 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
146 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
147 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
148 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
149 b01e7d3b 2018-08-04 stsp int selected;
150 b01e7d3b 2018-08-04 stsp char *in_repo_path;
151 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
152 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
153 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
154 1a76625f 2018-10-22 stsp sig_atomic_t quit;
155 1a76625f 2018-10-22 stsp pthread_t thread;
156 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
159 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
160 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
161 e9424729 2018-08-04 stsp int nlines;
163 e9424729 2018-08-04 stsp struct tog_view *view;
164 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
168 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
169 e9424729 2018-08-04 stsp const char *path;
170 e9424729 2018-08-04 stsp struct got_repository *repo;
171 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
172 e9424729 2018-08-04 stsp int *complete;
175 e9424729 2018-08-04 stsp struct tog_blame {
177 e9424729 2018-08-04 stsp size_t filesize;
178 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
179 6fcac457 2018-11-19 stsp int nlines;
180 e9424729 2018-08-04 stsp pthread_t thread;
181 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
182 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
183 e9424729 2018-08-04 stsp const char *path;
186 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
187 7cbe629d 2018-08-04 stsp int first_displayed_line;
188 7cbe629d 2018-08-04 stsp int last_displayed_line;
189 7cbe629d 2018-08-04 stsp int selected_line;
190 7cbe629d 2018-08-04 stsp int blame_complete;
193 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
194 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
195 e5a0f69f 2018-08-18 stsp char *path;
196 7cbe629d 2018-08-04 stsp struct got_repository *repo;
197 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
198 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
199 e9424729 2018-08-04 stsp struct tog_blame blame;
202 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
203 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
204 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
205 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
206 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
207 ad80ab7b 2018-08-04 stsp int selected;
210 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
212 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
213 ad80ab7b 2018-08-04 stsp char *tree_label;
214 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
215 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
216 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
217 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
218 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
219 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
220 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
221 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
222 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
223 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
224 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
228 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
230 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
231 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
232 669b5ffa 2018-10-07 stsp * there is enough screen estate.
234 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
235 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
237 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
238 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
239 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
241 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
242 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
244 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
246 cc3c9aac 2018-08-01 stsp struct tog_view {
247 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
248 26ed57b2 2018-05-19 stsp WINDOW *window;
249 26ed57b2 2018-05-19 stsp PANEL *panel;
250 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
251 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
252 1004088d 2018-09-29 stsp int focussed;
253 669b5ffa 2018-10-07 stsp struct tog_view *parent;
254 669b5ffa 2018-10-07 stsp struct tog_view *child;
255 669b5ffa 2018-10-07 stsp int child_focussed;
257 5dc9f4bc 2018-08-04 stsp /* type-specific state */
258 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
260 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
261 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
262 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
263 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
266 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
267 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
268 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
269 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
272 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
273 fb872ab2 2019-02-21 stsp struct got_object_id *, struct got_object_id *, struct tog_view *,
274 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
275 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
276 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
277 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
278 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
280 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
281 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *,
282 8b473291 2019-02-21 stsp struct got_repository *, const char *, int);
283 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
284 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
285 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
286 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
288 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
289 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *, struct got_repository *);
290 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
291 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
292 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
293 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
295 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
296 8b473291 2019-02-21 stsp struct got_tree_object *, struct got_object_id *,
297 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
298 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
299 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
300 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
301 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
303 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
305 25791caa 2018-10-24 stsp static void
306 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
308 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
311 e5a0f69f 2018-08-18 stsp static const struct got_error *
312 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
314 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
316 669b5ffa 2018-10-07 stsp if (view->child) {
317 669b5ffa 2018-10-07 stsp view_close(view->child);
318 669b5ffa 2018-10-07 stsp view->child = NULL;
320 e5a0f69f 2018-08-18 stsp if (view->close)
321 e5a0f69f 2018-08-18 stsp err = view->close(view);
322 ea5e7bb5 2018-08-01 stsp if (view->panel)
323 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
324 ea5e7bb5 2018-08-01 stsp if (view->window)
325 ea5e7bb5 2018-08-01 stsp delwin(view->window);
326 ea5e7bb5 2018-08-01 stsp free(view);
327 e5a0f69f 2018-08-18 stsp return err;
330 ea5e7bb5 2018-08-01 stsp static struct tog_view *
331 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
332 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
334 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
336 ea5e7bb5 2018-08-01 stsp if (view == NULL)
337 ea5e7bb5 2018-08-01 stsp return NULL;
339 d6b05b5b 2018-08-04 stsp view->type = type;
340 f7d12f7e 2018-08-01 stsp view->lines = LINES;
341 f7d12f7e 2018-08-01 stsp view->cols = COLS;
342 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
343 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
344 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
345 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
346 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
347 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
348 96a765a8 2018-08-04 stsp view_close(view);
349 ea5e7bb5 2018-08-01 stsp return NULL;
351 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
352 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
353 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
354 96a765a8 2018-08-04 stsp view_close(view);
355 ea5e7bb5 2018-08-01 stsp return NULL;
358 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
359 ea5e7bb5 2018-08-01 stsp return view;
363 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
365 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
367 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
370 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
372 5c60c32a 2018-10-18 stsp static const struct got_error *
373 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
375 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
377 5c60c32a 2018-10-18 stsp view->begin_y = 0;
378 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
379 5c60c32a 2018-10-18 stsp view->nlines = LINES;
380 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
381 5c60c32a 2018-10-18 stsp view->lines = LINES;
382 5c60c32a 2018-10-18 stsp view->cols = COLS;
383 5c60c32a 2018-10-18 stsp err = view_resize(view);
385 5c60c32a 2018-10-18 stsp return err;
387 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
388 5c60c32a 2018-10-18 stsp return got_error_from_errno();
390 5c60c32a 2018-10-18 stsp return NULL;
393 5c60c32a 2018-10-18 stsp static const struct got_error *
394 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
396 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
398 5c60c32a 2018-10-18 stsp view->begin_x = 0;
399 5c60c32a 2018-10-18 stsp view->begin_y = 0;
400 5c60c32a 2018-10-18 stsp view->nlines = LINES;
401 5c60c32a 2018-10-18 stsp view->ncols = COLS;
402 5c60c32a 2018-10-18 stsp view->lines = LINES;
403 5c60c32a 2018-10-18 stsp view->cols = COLS;
404 5c60c32a 2018-10-18 stsp err = view_resize(view);
406 5c60c32a 2018-10-18 stsp return err;
408 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
409 5c60c32a 2018-10-18 stsp return got_error_from_errno();
411 5c60c32a 2018-10-18 stsp return NULL;
415 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
417 5c60c32a 2018-10-18 stsp return view->parent == NULL;
420 4d8c2215 2018-08-19 stsp static const struct got_error *
421 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
423 f7d12f7e 2018-08-01 stsp int nlines, ncols;
425 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
426 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
428 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
430 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
431 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
433 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
435 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
436 0cf4efb1 2018-09-29 stsp return got_error_from_errno();
437 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
438 a6d7eb8d 2018-10-24 stsp return got_error_from_errno();
439 25791caa 2018-10-24 stsp wclear(view->window);
441 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
442 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
443 0cf4efb1 2018-09-29 stsp view->lines = LINES;
444 0cf4efb1 2018-09-29 stsp view->cols = COLS;
446 6e3e5d9c 2018-10-18 stsp if (view->child) {
447 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
448 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
449 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
450 5c60c32a 2018-10-18 stsp if (view->child->focussed)
451 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
453 5c60c32a 2018-10-18 stsp show_panel(view->panel);
455 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
456 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
460 5c60c32a 2018-10-18 stsp return NULL;
463 669b5ffa 2018-10-07 stsp static const struct got_error *
464 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
466 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
468 669b5ffa 2018-10-07 stsp if (view->child == NULL)
469 669b5ffa 2018-10-07 stsp return NULL;
471 669b5ffa 2018-10-07 stsp err = view_close(view->child);
472 669b5ffa 2018-10-07 stsp view->child = NULL;
473 669b5ffa 2018-10-07 stsp return err;
476 669b5ffa 2018-10-07 stsp static const struct got_error *
477 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
479 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
481 669b5ffa 2018-10-07 stsp view->child = child;
482 669b5ffa 2018-10-07 stsp child->parent = view;
483 669b5ffa 2018-10-07 stsp return err;
487 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
489 669b5ffa 2018-10-07 stsp return !view_is_parent_view(view) && view->begin_x > 0;
492 25791caa 2018-10-24 stsp static void
493 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
495 25791caa 2018-10-24 stsp int cols, lines;
496 25791caa 2018-10-24 stsp struct winsize size;
498 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
499 25791caa 2018-10-24 stsp cols = 80; /* Default */
500 25791caa 2018-10-24 stsp lines = 24;
502 25791caa 2018-10-24 stsp cols = size.ws_col;
503 25791caa 2018-10-24 stsp lines = size.ws_row;
505 25791caa 2018-10-24 stsp resize_term(lines, cols);
508 0cf4efb1 2018-09-29 stsp static const struct got_error *
509 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
510 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
511 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
513 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
514 669b5ffa 2018-10-07 stsp struct tog_view *v;
515 1a76625f 2018-10-22 stsp int ch, errcode;
517 e5a0f69f 2018-08-18 stsp *new = NULL;
518 e5a0f69f 2018-08-18 stsp *dead = NULL;
519 0cf4efb1 2018-09-29 stsp *focus = NULL;
521 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
522 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
523 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
524 1a76625f 2018-10-22 stsp if (errcode)
525 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
526 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
527 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
528 1a76625f 2018-10-22 stsp if (errcode)
529 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
530 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
532 25791caa 2018-10-24 stsp if (tog_sigwinch_received) {
533 25791caa 2018-10-24 stsp tog_resizeterm();
534 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
535 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
536 25791caa 2018-10-24 stsp err = view_resize(v);
538 25791caa 2018-10-24 stsp return err;
539 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
541 25791caa 2018-10-24 stsp return err;
545 e5a0f69f 2018-08-18 stsp switch (ch) {
549 669b5ffa 2018-10-07 stsp if (view->child) {
550 669b5ffa 2018-10-07 stsp *focus = view->child;
551 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
552 669b5ffa 2018-10-07 stsp } else if (view->parent) {
553 669b5ffa 2018-10-07 stsp *focus = view->parent;
554 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 0;
558 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
559 e5a0f69f 2018-08-18 stsp *dead = view;
565 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
566 669b5ffa 2018-10-07 stsp if (view->child == NULL)
568 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view->child)) {
569 669b5ffa 2018-10-07 stsp *focus = view->child;
570 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
571 669b5ffa 2018-10-07 stsp err = view_fullscreen(view->child);
573 669b5ffa 2018-10-07 stsp err = view_splitscreen(view->child);
576 669b5ffa 2018-10-07 stsp err = view->child->input(new, dead, focus,
577 669b5ffa 2018-10-07 stsp view->child, KEY_RESIZE);
579 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view)) {
580 669b5ffa 2018-10-07 stsp *focus = view;
581 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 1;
582 669b5ffa 2018-10-07 stsp err = view_fullscreen(view);
584 669b5ffa 2018-10-07 stsp err = view_splitscreen(view);
588 669b5ffa 2018-10-07 stsp err = view->input(new, dead, focus, view,
589 669b5ffa 2018-10-07 stsp KEY_RESIZE);
592 0cf4efb1 2018-09-29 stsp case KEY_RESIZE:
595 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
599 e5a0f69f 2018-08-18 stsp return err;
603 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
605 0cf4efb1 2018-09-29 stsp PANEL *panel;
606 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
608 669b5ffa 2018-10-07 stsp if (view->parent)
609 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
611 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
612 0cf4efb1 2018-09-29 stsp if (panel == NULL)
615 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
616 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
617 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
621 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
623 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
624 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
626 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
628 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
631 669b5ffa 2018-10-07 stsp return view->focussed;
634 bcbd79e2 2018-08-19 stsp static const struct got_error *
635 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
637 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
638 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
639 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
640 fd823528 2018-10-22 stsp int fast_refresh = 10;
641 1a76625f 2018-10-22 stsp int done = 0, errcode;
643 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
644 1a76625f 2018-10-22 stsp if (errcode)
645 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
647 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
648 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
650 a81bf10d 2018-09-29 stsp main_view = view;
651 1004088d 2018-09-29 stsp view->focussed = 1;
652 878940b7 2018-09-29 stsp err = view->show(view);
654 0cf4efb1 2018-09-29 stsp return err;
655 0cf4efb1 2018-09-29 stsp update_panels();
656 0cf4efb1 2018-09-29 stsp doupdate();
657 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
658 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
659 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
660 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
662 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
663 e4197bf9 2018-08-18 stsp view, &views);
666 e5a0f69f 2018-08-18 stsp if (dead_view) {
667 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
669 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
670 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
671 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
672 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
673 669b5ffa 2018-10-07 stsp prev = view->parent;
675 669b5ffa 2018-10-07 stsp if (dead_view->parent)
676 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
678 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
680 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
681 a81bf10d 2018-09-29 stsp if (err || dead_view == main_view)
684 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
685 0cf4efb1 2018-09-29 stsp if (focus_view)
686 0cf4efb1 2018-09-29 stsp view = focus_view;
687 669b5ffa 2018-10-07 stsp else if (prev)
688 669b5ffa 2018-10-07 stsp view = prev;
689 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
690 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
691 0cf4efb1 2018-09-29 stsp tog_view_list_head);
693 0cf4efb1 2018-09-29 stsp view = NULL;
694 669b5ffa 2018-10-07 stsp if (view) {
695 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
696 669b5ffa 2018-10-07 stsp focus_view = view->child;
698 669b5ffa 2018-10-07 stsp focus_view = view;
702 bcbd79e2 2018-08-19 stsp if (new_view) {
703 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
704 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
705 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
706 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
708 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
709 86c66b02 2018-10-18 stsp err = view_close(v);
714 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
715 fed7eaa8 2018-10-24 stsp view = new_view;
716 878940b7 2018-09-29 stsp if (focus_view == NULL)
717 878940b7 2018-09-29 stsp focus_view = new_view;
719 0cf4efb1 2018-09-29 stsp if (focus_view) {
720 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
722 0cf4efb1 2018-09-29 stsp view->focussed = 0;
723 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
724 0cf4efb1 2018-09-29 stsp view = focus_view;
725 878940b7 2018-09-29 stsp if (new_view)
726 878940b7 2018-09-29 stsp show_panel(new_view->panel);
727 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
728 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
730 669b5ffa 2018-10-07 stsp if (view) {
731 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
732 758194b5 2018-10-24 stsp view->focussed = 1;
733 758194b5 2018-10-24 stsp show_panel(view->panel);
734 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
735 758194b5 2018-10-24 stsp show_panel(view->child->panel);
736 1a76625f 2018-10-22 stsp focus_view = view;
738 669b5ffa 2018-10-07 stsp if (view->parent) {
739 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
743 669b5ffa 2018-10-07 stsp err = view->show(view);
746 669b5ffa 2018-10-07 stsp if (view->child) {
747 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
751 1a76625f 2018-10-22 stsp update_panels();
752 1a76625f 2018-10-22 stsp doupdate();
756 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
757 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
758 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
759 e5a0f69f 2018-08-18 stsp view_close(view);
762 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
763 1a76625f 2018-10-22 stsp if (errcode)
764 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
766 e5a0f69f 2018-08-18 stsp return err;
769 4ed7e80c 2018-05-20 stsp __dead static void
770 9f7d7167 2018-04-29 stsp usage_log(void)
773 c70c5802 2018-08-01 stsp fprintf(stderr,
774 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
775 9f7d7167 2018-04-29 stsp getprogname());
779 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
780 80ddbec8 2018-04-29 stsp static const struct got_error *
781 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
783 00dfcb92 2018-06-11 stsp char *vis = NULL;
784 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
786 963b370f 2018-05-20 stsp *ws = NULL;
787 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
788 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
789 00dfcb92 2018-06-11 stsp int vislen;
790 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
791 00dfcb92 2018-06-11 stsp return got_error_from_errno();
793 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
794 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
796 00dfcb92 2018-06-11 stsp return err;
797 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
798 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
799 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
804 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
805 a7f50699 2018-06-11 stsp if (*ws == NULL) {
806 a7f50699 2018-06-11 stsp err = got_error_from_errno();
810 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
811 963b370f 2018-05-20 stsp err = got_error_from_errno();
816 963b370f 2018-05-20 stsp *ws = NULL;
819 963b370f 2018-05-20 stsp return err;
822 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
823 963b370f 2018-05-20 stsp static const struct got_error *
824 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
826 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
827 963b370f 2018-05-20 stsp int cols = 0;
828 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
829 963b370f 2018-05-20 stsp size_t wlen;
832 963b370f 2018-05-20 stsp *wlinep = NULL;
833 b700b5d6 2018-07-10 stsp *widthp = 0;
835 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
837 963b370f 2018-05-20 stsp return err;
840 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
841 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
842 963b370f 2018-05-20 stsp switch (width) {
848 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
849 b700b5d6 2018-07-10 stsp cols += width;
853 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
854 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
858 963b370f 2018-05-20 stsp err = got_error_from_errno();
862 963b370f 2018-05-20 stsp wline[i] = L'\0';
863 b700b5d6 2018-07-10 stsp if (widthp)
864 b700b5d6 2018-07-10 stsp *widthp = cols;
867 963b370f 2018-05-20 stsp free(wline);
869 963b370f 2018-05-20 stsp *wlinep = wline;
870 963b370f 2018-05-20 stsp return err;
873 8b473291 2019-02-21 stsp static const struct got_error*
874 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
875 8b473291 2019-02-21 stsp struct got_object_id *id)
877 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
878 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
880 8b473291 2019-02-21 stsp const char *name;
882 8b473291 2019-02-21 stsp *refs_str = NULL;
884 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
885 8b473291 2019-02-21 stsp if (got_object_id_cmp(re->id, id) != 0)
887 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
888 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
890 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
892 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
894 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
896 8b473291 2019-02-21 stsp s = *refs_str;
897 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
898 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
899 8b473291 2019-02-21 stsp err = got_error_from_errno();
901 8b473291 2019-02-21 stsp *refs_str = NULL;
907 8b473291 2019-02-21 stsp return err;
910 963b370f 2018-05-20 stsp static const struct got_error *
911 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
912 8b473291 2019-02-21 stsp struct got_object_id *id, struct got_reflist_head *refs)
914 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
915 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
916 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
917 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
918 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
919 bb737323 2018-05-20 stsp int author_width, logmsg_width;
920 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
921 80ddbec8 2018-04-29 stsp char *line = NULL;
922 bb737323 2018-05-20 stsp int col, limit;
923 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
924 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
925 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
926 ccb26ccd 2018-11-05 stsp struct tm tm;
927 45d799e2 2018-12-23 stsp time_t committer_time;
929 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
930 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
931 ccb26ccd 2018-11-05 stsp return got_error_from_errno();
932 ccb26ccd 2018-11-05 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
933 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
934 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
936 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
937 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
939 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
940 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
941 b39d25c7 2018-07-10 stsp col = limit + 1;
942 b39d25c7 2018-07-10 stsp if (col > avail)
945 45d799e2 2018-12-23 stsp author0 = strdup(got_object_commit_get_author(commit));
946 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
947 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
950 6d9fbc00 2018-04-29 stsp author = author0;
951 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
952 6d9fbc00 2018-04-29 stsp if (smallerthan)
953 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
955 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
957 6d9fbc00 2018-04-29 stsp *at = '\0';
959 b39d25c7 2018-07-10 stsp limit = avail - col;
960 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
963 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
964 bb737323 2018-05-20 stsp col += author_width;
965 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
966 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
968 bb737323 2018-05-20 stsp author_width++;
970 9c2eaf34 2018-05-20 stsp if (col > avail)
973 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
974 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
975 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
978 bb737323 2018-05-20 stsp logmsg = logmsg0;
979 bb737323 2018-05-20 stsp while (*logmsg == '\n')
981 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
982 bb737323 2018-05-20 stsp if (newline)
983 bb737323 2018-05-20 stsp *newline = '\0';
984 bb737323 2018-05-20 stsp limit = avail - col;
985 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
988 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
989 bb737323 2018-05-20 stsp col += logmsg_width;
990 bb737323 2018-05-20 stsp while (col <= avail) {
991 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
995 80ddbec8 2018-04-29 stsp free(logmsg0);
996 bb737323 2018-05-20 stsp free(wlogmsg);
997 6d9fbc00 2018-04-29 stsp free(author0);
998 bb737323 2018-05-20 stsp free(wauthor);
999 80ddbec8 2018-04-29 stsp free(line);
1000 80ddbec8 2018-04-29 stsp return err;
1003 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1004 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1005 899d86c2 2018-05-10 stsp struct got_object_id *id)
1007 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1009 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1010 80ddbec8 2018-04-29 stsp if (entry == NULL)
1011 899d86c2 2018-05-10 stsp return NULL;
1013 899d86c2 2018-05-10 stsp entry->id = id;
1014 99db9666 2018-05-07 stsp entry->commit = commit;
1015 899d86c2 2018-05-10 stsp return entry;
1018 99db9666 2018-05-07 stsp static void
1019 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1021 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1023 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1024 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1025 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1026 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1027 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1028 99db9666 2018-05-07 stsp free(entry);
1031 99db9666 2018-05-07 stsp static void
1032 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1034 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1035 99db9666 2018-05-07 stsp pop_commit(commits);
1038 c4972b91 2018-05-07 stsp static const struct got_error *
1039 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1040 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
1042 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1043 93e45b7c 2018-09-24 stsp int nqueued = 0;
1046 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1047 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1048 1a76625f 2018-10-22 stsp * while updating the display.
1050 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
1051 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1052 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1053 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1054 1a76625f 2018-10-22 stsp int errcode;
1056 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1057 9ba79e04 2018-06-11 stsp if (err) {
1058 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1060 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
1061 93e45b7c 2018-09-24 stsp minqueue, repo);
1063 ecb28ae0 2018-07-16 stsp return err;
1067 ecb28ae0 2018-07-16 stsp if (id == NULL)
1070 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1073 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1074 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1075 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
1079 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1080 1a76625f 2018-10-22 stsp if (errcode) {
1081 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1085 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1086 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1087 ecb28ae0 2018-07-16 stsp nqueued++;
1088 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1090 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1091 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1092 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1095 9ba79e04 2018-06-11 stsp return err;
1098 99db9666 2018-05-07 stsp static const struct got_error *
1099 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1101 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1102 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1104 9ba79e04 2018-06-11 stsp *head_id = NULL;
1106 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1108 99db9666 2018-05-07 stsp return err;
1110 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1111 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1112 9ba79e04 2018-06-11 stsp if (err) {
1113 9ba79e04 2018-06-11 stsp *head_id = NULL;
1114 99db9666 2018-05-07 stsp return err;
1117 9ba79e04 2018-06-11 stsp return NULL;
1120 0553a4e3 2018-04-30 stsp static const struct got_error *
1121 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1122 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1123 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1124 8b473291 2019-02-21 stsp struct got_reflist_head *refs, const char *path, int commits_needed)
1126 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1127 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1128 ecb28ae0 2018-07-16 stsp int ncommits, width;
1129 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1130 8b473291 2019-02-21 stsp char *refs_str = NULL;
1131 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1133 e0d42f60 2018-07-22 stsp entry = first;
1134 e0d42f60 2018-07-22 stsp ncommits = 0;
1135 e0d42f60 2018-07-22 stsp while (entry) {
1136 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1137 e0d42f60 2018-07-22 stsp *selected = entry;
1140 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1141 e0d42f60 2018-07-22 stsp ncommits++;
1144 1a76625f 2018-10-22 stsp if (*selected) {
1145 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1147 ecb28ae0 2018-07-16 stsp return err;
1148 8b473291 2019-02-21 stsp if (refs) {
1149 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, (*selected)->id);
1151 8b473291 2019-02-21 stsp goto done;
1155 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1156 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1158 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1159 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1160 8b473291 2019-02-21 stsp commits_needed > 0 ? "loading... " :
1161 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1162 8b473291 2019-02-21 stsp err = got_error_from_errno();
1163 8b473291 2019-02-21 stsp goto done;
1166 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1167 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1168 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1169 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1170 1a76625f 2018-10-22 stsp err = got_error_from_errno();
1171 1a76625f 2018-10-22 stsp header = NULL;
1172 1a76625f 2018-10-22 stsp goto done;
1174 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1175 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1176 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1177 1a76625f 2018-10-22 stsp err = got_error_from_errno();
1178 1a76625f 2018-10-22 stsp header = NULL;
1179 1a76625f 2018-10-22 stsp goto done;
1181 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1183 1a76625f 2018-10-22 stsp goto done;
1185 2814baeb 2018-08-01 stsp werase(view->window);
1187 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1188 a3404814 2018-09-02 stsp wstandout(view->window);
1189 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1190 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1191 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1194 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1195 a3404814 2018-09-02 stsp wstandend(view->window);
1196 ecb28ae0 2018-07-16 stsp free(wline);
1197 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1198 1a76625f 2018-10-22 stsp goto done;
1200 899d86c2 2018-05-10 stsp entry = first;
1201 899d86c2 2018-05-10 stsp *last = first;
1202 867c6645 2018-07-10 stsp ncommits = 0;
1203 899d86c2 2018-05-10 stsp while (entry) {
1204 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1206 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1207 2814baeb 2018-08-01 stsp wstandout(view->window);
1208 8b473291 2019-02-21 stsp err = draw_commit(view, entry->commit, entry->id, refs);
1209 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1210 2814baeb 2018-08-01 stsp wstandend(view->window);
1213 0553a4e3 2018-04-30 stsp ncommits++;
1214 899d86c2 2018-05-10 stsp *last = entry;
1215 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1218 1a57306a 2018-09-02 stsp view_vborder(view);
1220 1a76625f 2018-10-22 stsp free(id_str);
1221 8b473291 2019-02-21 stsp free(refs_str);
1222 1a76625f 2018-10-22 stsp free(ncommits_str);
1223 1a76625f 2018-10-22 stsp free(header);
1224 80ddbec8 2018-04-29 stsp return err;
1227 07b55e75 2018-05-10 stsp static void
1228 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1229 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1231 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1232 07b55e75 2018-05-10 stsp int nscrolled = 0;
1234 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1235 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
1238 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1239 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1240 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1241 07b55e75 2018-05-10 stsp if (entry) {
1242 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1243 07b55e75 2018-05-10 stsp nscrolled++;
1248 aa075928 2018-05-10 stsp static const struct got_error *
1249 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1250 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1252 5e224a3e 2019-02-22 stsp int errcode;
1254 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1255 5e224a3e 2019-02-22 stsp if (*log_complete)
1258 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1259 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1260 7aafa0d1 2019-02-22 stsp if (errcode)
1261 7aafa0d1 2019-02-22 stsp return got_error_set_errno(errcode);
1262 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1263 7aafa0d1 2019-02-22 stsp if (errcode)
1264 7aafa0d1 2019-02-22 stsp return got_error_set_errno(errcode);
1265 7aafa0d1 2019-02-22 stsp pthread_yield();
1266 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1267 7aafa0d1 2019-02-22 stsp if (errcode)
1268 7aafa0d1 2019-02-22 stsp return got_error_set_errno(errcode);
1270 5e224a3e 2019-02-22 stsp if (*commits_needed > 0 && !load_all) {
1272 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1273 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1274 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1275 5e224a3e 2019-02-22 stsp * with few commits in history.
1277 359bfafd 2019-02-22 stsp halfdelay(1); /* fast refresh while loading */
1278 7aafa0d1 2019-02-22 stsp return NULL;
1282 5e224a3e 2019-02-22 stsp return NULL;
1285 5e224a3e 2019-02-22 stsp static const struct got_error *
1286 5e224a3e 2019-02-22 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1287 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1288 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1289 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1291 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1292 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1293 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1295 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1296 5e224a3e 2019-02-22 stsp return NULL;
1298 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1299 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1300 5e224a3e 2019-02-22 stsp (*commits_needed) += maxscroll;
1301 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1302 5e224a3e 2019-02-22 stsp need_commits);
1304 5e224a3e 2019-02-22 stsp return err;
1308 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1309 88048b54 2019-02-21 stsp if (pentry == NULL)
1312 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1314 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1315 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1317 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1318 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1320 dd0a52c1 2018-05-20 stsp return err;
1323 cd0acaa7 2018-05-20 stsp static const struct got_error *
1324 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1325 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1326 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1327 8b473291 2019-02-21 stsp struct got_repository *repo)
1329 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1330 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1331 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1333 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1334 15a94983 2018-12-23 stsp if (diff_view == NULL)
1335 15a94983 2018-12-23 stsp return got_error_from_errno();
1337 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1338 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1339 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1340 e5a0f69f 2018-08-18 stsp if (err == NULL)
1341 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1342 cd0acaa7 2018-05-20 stsp return err;
1345 80ddbec8 2018-04-29 stsp static const struct got_error *
1346 0cf4efb1 2018-09-29 stsp browse_commit(struct tog_view **new_view, int begin_x,
1347 8b473291 2019-02-21 stsp struct commit_queue_entry *entry, struct got_reflist_head *refs,
1348 8b473291 2019-02-21 stsp struct got_repository *repo)
1350 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1351 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1352 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1354 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1355 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1357 9343a5fb 2018-06-23 stsp return err;
1359 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1360 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1361 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1363 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1365 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1367 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1368 1a76625f 2018-10-22 stsp return err;
1371 1a76625f 2018-10-22 stsp static void *
1372 1a76625f 2018-10-22 stsp log_thread(void *arg)
1374 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1375 1a76625f 2018-10-22 stsp int errcode = 0;
1376 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1377 1a76625f 2018-10-22 stsp int done = 0;
1379 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1381 1a76625f 2018-10-22 stsp return (void *)err;
1383 1a76625f 2018-10-22 stsp while (!done && !err) {
1384 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1385 1a76625f 2018-10-22 stsp a->in_repo_path);
1386 1a76625f 2018-10-22 stsp if (err) {
1387 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1388 1a76625f 2018-10-22 stsp return (void *)err;
1389 1a76625f 2018-10-22 stsp err = NULL;
1391 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1392 1a76625f 2018-10-22 stsp a->commits_needed--;
1394 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1395 1a76625f 2018-10-22 stsp if (errcode)
1396 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
1399 1a76625f 2018-10-22 stsp a->log_complete = 1;
1400 1a76625f 2018-10-22 stsp else if (*a->quit) {
1402 1a76625f 2018-10-22 stsp a->log_complete = 1;
1403 1a76625f 2018-10-22 stsp } else if (*a->first_displayed_entry == NULL) {
1404 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1405 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1406 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1410 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1411 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1412 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1413 1a76625f 2018-10-22 stsp &tog_mutex);
1414 1a76625f 2018-10-22 stsp if (errcode)
1415 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1418 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1419 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1420 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1422 1a76625f 2018-10-22 stsp return (void *)err;
1425 1a76625f 2018-10-22 stsp static const struct got_error *
1426 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1428 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1429 1a76625f 2018-10-22 stsp int errcode;
1431 1a76625f 2018-10-22 stsp if (s->thread) {
1432 1a76625f 2018-10-22 stsp s->quit = 1;
1433 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1434 1a76625f 2018-10-22 stsp if (errcode)
1435 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1436 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1437 1a76625f 2018-10-22 stsp if (errcode)
1438 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1439 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1440 1a76625f 2018-10-22 stsp if (errcode)
1441 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1442 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1443 1a76625f 2018-10-22 stsp if (errcode)
1444 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1445 1a76625f 2018-10-22 stsp s->thread = NULL;
1448 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1449 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1450 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1452 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1453 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1454 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1457 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1458 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1459 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1462 9343a5fb 2018-06-23 stsp return err;
1465 9343a5fb 2018-06-23 stsp static const struct got_error *
1466 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1468 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1469 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1471 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1472 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1473 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1474 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1475 1a76625f 2018-10-22 stsp free(s->start_id);
1476 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1477 1a76625f 2018-10-22 stsp return err;
1480 1a76625f 2018-10-22 stsp static const struct got_error *
1481 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1482 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
1483 8b473291 2019-02-21 stsp const char *path, int check_disk)
1485 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1486 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1487 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1488 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1489 1a76625f 2018-10-22 stsp int errcode;
1491 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1492 ecb28ae0 2018-07-16 stsp if (err != NULL)
1493 ecb28ae0 2018-07-16 stsp goto done;
1495 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1496 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1497 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1499 8b473291 2019-02-21 stsp s->refs = refs;
1500 fb2756b9 2018-08-04 stsp s->repo = repo;
1501 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1502 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1503 5036bf37 2018-09-24 stsp err = got_error_from_errno();
1504 5036bf37 2018-09-24 stsp goto done;
1507 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1508 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1509 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1511 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1513 1a76625f 2018-10-22 stsp goto done;
1514 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1515 1a76625f 2018-10-22 stsp 0, thread_repo);
1517 1a76625f 2018-10-22 stsp goto done;
1519 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1520 1a76625f 2018-10-22 stsp if (errcode) {
1521 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1522 1a76625f 2018-10-22 stsp goto done;
1525 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1526 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1527 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1528 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1529 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1530 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1531 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1532 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1533 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1534 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1535 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1538 1a76625f 2018-10-22 stsp close_log_view(view);
1539 ba4f502b 2018-08-04 stsp return err;
1542 e5a0f69f 2018-08-18 stsp static const struct got_error *
1543 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1545 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1547 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1548 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1549 2b380cc8 2018-10-24 stsp &s->thread_args);
1550 2b380cc8 2018-10-24 stsp if (errcode)
1551 2b380cc8 2018-10-24 stsp return got_error_set_errno(errcode);
1554 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1555 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1556 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
1557 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1560 e5a0f69f 2018-08-18 stsp static const struct got_error *
1561 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1562 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1564 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1565 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1566 5036bf37 2018-09-24 stsp char *parent_path;
1567 669b5ffa 2018-10-07 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
1568 669b5ffa 2018-10-07 stsp int begin_x = 0;
1570 e5a0f69f 2018-08-18 stsp switch (ch) {
1572 1a76625f 2018-10-22 stsp s->quit = 1;
1575 e5a0f69f 2018-08-18 stsp case KEY_UP:
1578 2b380cc8 2018-10-24 stsp if (s->first_displayed_entry == NULL)
1580 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1581 e5a0f69f 2018-08-18 stsp s->selected--;
1582 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1584 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1585 e5a0f69f 2018-08-18 stsp &s->commits);
1587 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1588 2b380cc8 2018-10-24 stsp if (s->first_displayed_entry == NULL)
1590 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1591 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1592 e5a0f69f 2018-08-18 stsp s->selected = 0;
1595 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1596 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1599 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1602 2b380cc8 2018-10-24 stsp if (s->first_displayed_entry == NULL)
1604 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1605 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1606 e5a0f69f 2018-08-18 stsp s->selected++;
1609 ca9338a5 2019-02-22 stsp err = scroll_down(&s->first_displayed_entry, 1,
1610 93e45b7c 2018-09-24 stsp &s->last_displayed_entry, &s->commits,
1611 1a76625f 2018-10-22 stsp &s->thread_args.log_complete,
1612 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1613 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1615 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1616 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1617 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1618 2b380cc8 2018-10-24 stsp if (first == NULL)
1620 ca9338a5 2019-02-22 stsp err = scroll_down(&s->first_displayed_entry,
1621 93e45b7c 2018-09-24 stsp view->nlines, &s->last_displayed_entry,
1622 1a76625f 2018-10-22 stsp &s->commits, &s->thread_args.log_complete,
1623 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1624 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1625 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1626 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1627 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1628 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1629 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1630 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1632 e5a0f69f 2018-08-18 stsp err = NULL;
1635 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1636 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1637 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1638 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1639 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1641 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1642 e5a0f69f 2018-08-18 stsp case '\r':
1643 2b380cc8 2018-10-24 stsp if (s->selected_entry == NULL)
1645 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1646 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1647 669b5ffa 2018-10-07 stsp err = open_diff_view_for_commit(&diff_view, begin_x,
1648 fb872ab2 2019-02-21 stsp s->selected_entry->commit, s->selected_entry->id,
1649 8b473291 2019-02-21 stsp view, s->refs, s->repo);
1652 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1653 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1655 669b5ffa 2018-10-07 stsp return err;
1656 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
1657 669b5ffa 2018-10-07 stsp if (err) {
1658 669b5ffa 2018-10-07 stsp view_close(diff_view);
1661 55566b34 2018-11-05 stsp *focus_view = diff_view;
1662 55566b34 2018-11-05 stsp view->child_focussed = 1;
1664 669b5ffa 2018-10-07 stsp *new_view = diff_view;
1667 2b380cc8 2018-10-24 stsp if (s->selected_entry == NULL)
1669 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1670 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1671 669b5ffa 2018-10-07 stsp err = browse_commit(&tree_view, begin_x,
1672 8b473291 2019-02-21 stsp s->selected_entry, s->refs, s->repo);
1675 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1676 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1678 669b5ffa 2018-10-07 stsp return err;
1679 669b5ffa 2018-10-07 stsp err = view_set_child(view, tree_view);
1680 669b5ffa 2018-10-07 stsp if (err) {
1681 669b5ffa 2018-10-07 stsp view_close(tree_view);
1684 669b5ffa 2018-10-07 stsp *focus_view = tree_view;
1685 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1687 669b5ffa 2018-10-07 stsp *new_view = tree_view;
1689 5036bf37 2018-09-24 stsp case KEY_BACKSPACE:
1690 5036bf37 2018-09-24 stsp if (strcmp(s->in_repo_path, "/") == 0)
1692 5036bf37 2018-09-24 stsp parent_path = dirname(s->in_repo_path);
1693 5036bf37 2018-09-24 stsp if (parent_path && strcmp(parent_path, ".") != 0) {
1694 5036bf37 2018-09-24 stsp struct tog_view *lv;
1695 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1697 1a76625f 2018-10-22 stsp return err;
1698 0cf4efb1 2018-09-29 stsp lv = view_open(view->nlines, view->ncols,
1699 0cf4efb1 2018-09-29 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
1700 5036bf37 2018-09-24 stsp if (lv == NULL)
1701 5036bf37 2018-09-24 stsp return got_error_from_errno();
1702 8b473291 2019-02-21 stsp err = open_log_view(lv, s->start_id, s->refs,
1703 8b473291 2019-02-21 stsp s->repo, parent_path, 0);
1705 1a76625f 2018-10-22 stsp return err;;
1706 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1707 669b5ffa 2018-10-07 stsp *new_view = lv;
1709 669b5ffa 2018-10-07 stsp view_set_child(view->parent, lv);
1710 34095730 2018-10-22 stsp *focus_view = lv;
1712 1a76625f 2018-10-22 stsp return NULL;
1719 80ddbec8 2018-04-29 stsp return err;
1722 4ed7e80c 2018-05-20 stsp static const struct got_error *
1723 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
1725 c2db6724 2019-01-04 stsp const struct got_error *error;
1727 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
1728 c2db6724 2019-01-04 stsp return got_error_from_errno();
1730 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
1731 c2db6724 2019-01-04 stsp return got_error_from_errno();
1733 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
1734 c2db6724 2019-01-04 stsp return got_error_from_errno();
1736 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
1737 c2db6724 2019-01-04 stsp if (error != NULL)
1738 c2db6724 2019-01-04 stsp return error;
1740 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
1741 c2db6724 2019-01-04 stsp return got_error_from_errno();
1743 c2db6724 2019-01-04 stsp return NULL;
1746 a915003a 2019-02-05 stsp static void
1747 a915003a 2019-02-05 stsp init_curses(void)
1749 a915003a 2019-02-05 stsp initscr();
1751 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
1754 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
1755 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
1756 a915003a 2019-02-05 stsp curs_set(0);
1757 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
1760 c2db6724 2019-01-04 stsp static const struct got_error *
1761 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1763 80ddbec8 2018-04-29 stsp const struct got_error *error;
1764 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1765 8b473291 2019-02-21 stsp struct got_reflist_head refs;
1766 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1767 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1768 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1770 04cc582a 2018-08-01 stsp struct tog_view *view;
1772 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1773 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1774 c2db6724 2019-01-04 stsp NULL) == -1)
1775 80ddbec8 2018-04-29 stsp err(1, "pledge");
1778 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1779 80ddbec8 2018-04-29 stsp switch (ch) {
1781 80ddbec8 2018-04-29 stsp start_commit = optarg;
1784 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1785 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1786 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1790 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1794 80ddbec8 2018-04-29 stsp argc -= optind;
1795 80ddbec8 2018-04-29 stsp argv += optind;
1797 ecb28ae0 2018-07-16 stsp if (argc == 0)
1798 ecb28ae0 2018-07-16 stsp path = strdup("");
1799 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1800 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1802 80ddbec8 2018-04-29 stsp usage_log();
1803 ecb28ae0 2018-07-16 stsp if (path == NULL)
1804 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1806 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1807 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1808 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1809 ecb28ae0 2018-07-16 stsp goto done;
1811 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1812 b7165be3 2019-02-05 stsp struct got_worktree *worktree;
1813 b7165be3 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1814 b7165be3 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1815 b7165be3 2019-02-05 stsp goto done;
1816 b7165be3 2019-02-05 stsp if (worktree) {
1817 b7165be3 2019-02-05 stsp repo_path =
1818 b7165be3 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1819 b7165be3 2019-02-05 stsp got_worktree_close(worktree);
1821 b7165be3 2019-02-05 stsp repo_path = strdup(cwd);
1822 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1823 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1824 ecb28ae0 2018-07-16 stsp goto done;
1828 a915003a 2019-02-05 stsp init_curses();
1830 c2db6724 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
1831 c2db6724 2019-01-04 stsp if (error)
1832 c2db6724 2019-01-04 stsp goto done;
1834 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1835 80ddbec8 2018-04-29 stsp if (error != NULL)
1836 ecb28ae0 2018-07-16 stsp goto done;
1838 15a94983 2018-12-23 stsp if (start_commit == NULL)
1839 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1841 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&start_id, repo,
1842 15a94983 2018-12-23 stsp start_commit);
1843 80ddbec8 2018-04-29 stsp if (error != NULL)
1844 8b473291 2019-02-21 stsp goto done;
1846 8b473291 2019-02-21 stsp SIMPLEQ_INIT(&refs);
1847 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
1848 8b473291 2019-02-21 stsp if (error)
1849 ecb28ae0 2018-07-16 stsp goto done;
1851 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1852 04cc582a 2018-08-01 stsp if (view == NULL) {
1853 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1854 04cc582a 2018-08-01 stsp goto done;
1856 8b473291 2019-02-21 stsp error = open_log_view(view, start_id, &refs, repo, path, 1);
1857 ba4f502b 2018-08-04 stsp if (error)
1858 ba4f502b 2018-08-04 stsp goto done;
1859 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1861 ecb28ae0 2018-07-16 stsp free(repo_path);
1862 ecb28ae0 2018-07-16 stsp free(cwd);
1863 ecb28ae0 2018-07-16 stsp free(path);
1864 899d86c2 2018-05-10 stsp free(start_id);
1866 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1867 80ddbec8 2018-04-29 stsp return error;
1870 4ed7e80c 2018-05-20 stsp __dead static void
1871 9f7d7167 2018-04-29 stsp usage_diff(void)
1874 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1875 9f7d7167 2018-04-29 stsp getprogname());
1879 b304db33 2018-05-20 stsp static char *
1880 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1882 b304db33 2018-05-20 stsp char *line;
1883 b304db33 2018-05-20 stsp size_t linelen;
1884 b304db33 2018-05-20 stsp size_t lineno;
1885 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1887 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1889 b304db33 2018-05-20 stsp *len = linelen;
1890 b304db33 2018-05-20 stsp return line;
1893 4ed7e80c 2018-05-20 stsp static const struct got_error *
1894 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1895 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1896 a3404814 2018-09-02 stsp char * header)
1898 61e69b96 2018-05-20 stsp const struct got_error *err;
1899 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1900 b304db33 2018-05-20 stsp char *line;
1901 b304db33 2018-05-20 stsp size_t len;
1902 61e69b96 2018-05-20 stsp wchar_t *wline;
1903 e0b650dd 2018-05-20 stsp int width;
1905 26ed57b2 2018-05-19 stsp rewind(f);
1906 f7d12f7e 2018-08-01 stsp werase(view->window);
1908 a3404814 2018-09-02 stsp if (header) {
1909 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1910 a3404814 2018-09-02 stsp if (err) {
1911 a3404814 2018-09-02 stsp return err;
1914 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1915 a3404814 2018-09-02 stsp wstandout(view->window);
1916 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1917 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1918 a3404814 2018-09-02 stsp wstandend(view->window);
1919 a3404814 2018-09-02 stsp if (width < view->ncols)
1920 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1922 a3404814 2018-09-02 stsp if (max_lines <= 1)
1923 a3404814 2018-09-02 stsp return NULL;
1924 a3404814 2018-09-02 stsp max_lines--;
1928 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1929 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1930 26ed57b2 2018-05-19 stsp if (line == NULL) {
1934 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1935 26ed57b2 2018-05-19 stsp free(line);
1939 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1940 61e69b96 2018-05-20 stsp if (err) {
1941 61e69b96 2018-05-20 stsp free(line);
1942 61e69b96 2018-05-20 stsp return err;
1944 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1945 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1946 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1947 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1948 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1949 26ed57b2 2018-05-19 stsp free(line);
1950 2550e4c3 2018-07-13 stsp free(wline);
1951 2550e4c3 2018-07-13 stsp wline = NULL;
1953 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1955 1a57306a 2018-09-02 stsp view_vborder(view);
1957 26ed57b2 2018-05-19 stsp return NULL;
1960 abd2672a 2018-12-23 stsp static char *
1961 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
1963 abd2672a 2018-12-23 stsp char *p, *s = ctime_r(time, datebuf);
1964 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
1966 abd2672a 2018-12-23 stsp *p = '\0';
1970 4ed7e80c 2018-05-20 stsp static const struct got_error *
1971 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
1972 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
1974 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
1975 abd2672a 2018-12-23 stsp char datebuf[26];
1976 15a94983 2018-12-23 stsp struct got_commit_object *commit;
1977 15a94983 2018-12-23 stsp char *id_str = NULL;
1978 45d799e2 2018-12-23 stsp time_t committer_time;
1979 45d799e2 2018-12-23 stsp const char *author, *committer;
1980 8b473291 2019-02-21 stsp char *refs_str = NULL;
1982 8b473291 2019-02-21 stsp if (refs) {
1983 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, commit_id);
1985 8b473291 2019-02-21 stsp return err;
1988 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1990 abd2672a 2018-12-23 stsp return err;
1992 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
1993 15a94983 2018-12-23 stsp if (err) {
1994 15a94983 2018-12-23 stsp err = got_error_from_errno();
1995 15a94983 2018-12-23 stsp goto done;
1998 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1999 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2000 abd2672a 2018-12-23 stsp err = got_error_from_errno();
2001 abd2672a 2018-12-23 stsp goto done;
2003 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2004 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2005 abd2672a 2018-12-23 stsp err = got_error_from_errno();
2006 abd2672a 2018-12-23 stsp goto done;
2008 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2009 abd2672a 2018-12-23 stsp if (fprintf(outfile, "date: %s UTC\n",
2010 45d799e2 2018-12-23 stsp get_datestr(&committer_time, datebuf)) < 0) {
2011 abd2672a 2018-12-23 stsp err = got_error_from_errno();
2012 abd2672a 2018-12-23 stsp goto done;
2014 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2015 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2016 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2017 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2018 abd2672a 2018-12-23 stsp err = got_error_from_errno();
2019 abd2672a 2018-12-23 stsp goto done;
2021 45d799e2 2018-12-23 stsp if (fprintf(outfile, "%s\n",
2022 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(commit)) < 0) {
2023 abd2672a 2018-12-23 stsp err = got_error_from_errno();
2024 abd2672a 2018-12-23 stsp goto done;
2027 abd2672a 2018-12-23 stsp free(id_str);
2028 8b473291 2019-02-21 stsp free(refs_str);
2029 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2030 abd2672a 2018-12-23 stsp return err;
2033 abd2672a 2018-12-23 stsp static const struct got_error *
2034 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2036 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2037 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2038 15a94983 2018-12-23 stsp int obj_type;
2040 511a516b 2018-05-19 stsp f = got_opentemp();
2041 48ae06ee 2018-10-18 stsp if (f == NULL) {
2042 48ae06ee 2018-10-18 stsp err = got_error_from_errno();
2043 48ae06ee 2018-10-18 stsp goto done;
2045 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2046 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
2047 fb43ecf1 2019-02-11 stsp goto done;
2051 15a94983 2018-12-23 stsp if (s->id1)
2052 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2054 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2056 15a94983 2018-12-23 stsp goto done;
2058 15a94983 2018-12-23 stsp switch (obj_type) {
2059 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2060 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2061 54156555 2018-12-24 stsp s->diff_context, s->repo, f);
2063 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2064 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2065 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
2067 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2068 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2069 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2070 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2072 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2075 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2076 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2077 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2079 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2080 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2081 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2082 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2083 8b473291 2019-02-21 stsp s->repo, f);
2088 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2090 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2091 15a94983 2018-12-23 stsp s->diff_context, s->repo, f);
2095 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2099 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2100 cbe7f848 2019-02-11 stsp err = got_error_from_errno();
2101 48ae06ee 2018-10-18 stsp return err;
2104 48ae06ee 2018-10-18 stsp static const struct got_error *
2105 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2106 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2107 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2109 48ae06ee 2018-10-18 stsp const struct got_error *err;
2111 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2112 15a94983 2018-12-23 stsp int type1, type2;
2113 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2115 15a94983 2018-12-23 stsp return err;
2116 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2118 15a94983 2018-12-23 stsp return err;
2120 15a94983 2018-12-23 stsp if (type1 != type2)
2121 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2124 15a94983 2018-12-23 stsp if (id1) {
2125 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2126 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2127 48ae06ee 2018-10-18 stsp return got_error_from_errno();
2129 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2131 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2132 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2133 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2134 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2135 48ae06ee 2018-10-18 stsp return got_error_from_errno();
2137 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2138 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2139 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2140 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2141 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2142 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2143 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2145 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2146 48ae06ee 2018-10-18 stsp if (err) {
2147 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2148 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2149 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2150 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2151 48ae06ee 2018-10-18 stsp return err;
2154 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2155 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2156 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2158 5dc9f4bc 2018-08-04 stsp return NULL;
2161 e5a0f69f 2018-08-18 stsp static const struct got_error *
2162 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2164 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2166 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2167 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2168 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2169 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2170 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2171 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2172 e5a0f69f 2018-08-18 stsp return err;
2175 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2176 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2178 a3404814 2018-09-02 stsp const struct got_error *err;
2179 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2180 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2182 a3404814 2018-09-02 stsp if (s->id1) {
2183 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2185 a3404814 2018-09-02 stsp return err;
2187 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2189 a3404814 2018-09-02 stsp return err;
2191 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2192 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2193 a3404814 2018-09-02 stsp err = got_error_from_errno();
2194 a3404814 2018-09-02 stsp free(id_str1);
2195 a3404814 2018-09-02 stsp free(id_str2);
2196 a3404814 2018-09-02 stsp return err;
2198 a3404814 2018-09-02 stsp free(id_str1);
2199 a3404814 2018-09-02 stsp free(id_str2);
2201 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2202 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2206 15a087fe 2019-02-21 stsp static const struct got_error *
2207 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2208 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2210 d7a04538 2019-02-21 stsp const struct got_error *err;
2211 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
2212 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
2213 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
2215 15a087fe 2019-02-21 stsp free(s->id2);
2216 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2217 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2218 15a087fe 2019-02-21 stsp return got_error_from_errno();
2220 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2222 d7a04538 2019-02-21 stsp return err;
2223 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
2224 15a087fe 2019-02-21 stsp free(s->id1);
2225 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
2226 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2227 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
2228 15a087fe 2019-02-21 stsp return NULL;
2231 0cf4efb1 2018-09-29 stsp static const struct got_error *
2232 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2233 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2235 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2236 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2237 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
2238 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
2241 e5a0f69f 2018-08-18 stsp switch (ch) {
2243 e5a0f69f 2018-08-18 stsp case KEY_UP:
2244 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
2245 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2247 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2249 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
2250 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2251 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2254 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2255 e5a0f69f 2018-08-18 stsp if (!s->eof)
2256 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2258 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2261 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
2262 e5a0f69f 2018-08-18 stsp char *line;
2263 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
2264 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2265 e5a0f69f 2018-08-18 stsp if (line == NULL)
2270 48ae06ee 2018-10-18 stsp if (s->diff_context > 0) {
2271 48ae06ee 2018-10-18 stsp s->diff_context--;
2272 48ae06ee 2018-10-18 stsp err = create_diff(s);
2276 4a8520aa 2018-10-18 stsp if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2277 48ae06ee 2018-10-18 stsp s->diff_context++;
2278 48ae06ee 2018-10-18 stsp err = create_diff(s);
2283 fb872ab2 2019-02-21 stsp if (s->log_view == NULL)
2285 fb872ab2 2019-02-21 stsp ls = &s->log_view->state.log;
2286 fb872ab2 2019-02-21 stsp entry = TAILQ_PREV(ls->selected_entry,
2287 15a087fe 2019-02-21 stsp commit_queue_head, entry);
2288 15a087fe 2019-02-21 stsp if (entry == NULL)
2291 6524637e 2019-02-21 stsp err = input_log_view(NULL, NULL, NULL, s->log_view,
2296 15a087fe 2019-02-21 stsp err = set_selected_commit(s, entry);
2300 15a087fe 2019-02-21 stsp s->first_displayed_line = 1;
2301 15a087fe 2019-02-21 stsp s->last_displayed_line = view->nlines;
2303 15a087fe 2019-02-21 stsp err = create_diff(s);
2307 fb872ab2 2019-02-21 stsp if (s->log_view == NULL)
2309 fb872ab2 2019-02-21 stsp ls = &s->log_view->state.log;
2311 5e224a3e 2019-02-22 stsp if (ls->thread_args.commits_needed == 0) {
2312 5e224a3e 2019-02-22 stsp ls->thread_args.commits_needed++;
2314 5e224a3e 2019-02-22 stsp /* Display "loading..." in log view. */
2315 5e224a3e 2019-02-22 stsp show_log_view(s->log_view);
2316 5e224a3e 2019-02-22 stsp update_panels();
2317 5e224a3e 2019-02-22 stsp doupdate();
2319 5e224a3e 2019-02-22 stsp err = trigger_log_thread(1 /* load_all */,
2320 5e224a3e 2019-02-22 stsp &ls->thread_args.commits_needed,
2321 5e224a3e 2019-02-22 stsp &ls->thread_args.log_complete,
2322 5e224a3e 2019-02-22 stsp &ls->thread_args.need_commits);
2326 fb872ab2 2019-02-21 stsp err = input_log_view(NULL, NULL, NULL, s->log_view,
2327 fb872ab2 2019-02-21 stsp KEY_DOWN);
2331 fb872ab2 2019-02-21 stsp entry = TAILQ_NEXT(ls->selected_entry, entry);
2332 15a087fe 2019-02-21 stsp if (entry == NULL)
2335 15a087fe 2019-02-21 stsp err = set_selected_commit(s, entry);
2339 15a087fe 2019-02-21 stsp s->first_displayed_line = 1;
2340 15a087fe 2019-02-21 stsp s->last_displayed_line = view->nlines;
2342 15a087fe 2019-02-21 stsp err = create_diff(s);
2348 bcbd79e2 2018-08-19 stsp return err;
2351 4ed7e80c 2018-05-20 stsp static const struct got_error *
2352 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2354 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2355 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2356 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2357 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2358 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2359 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2361 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2363 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2364 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2365 eb6600df 2019-01-04 stsp NULL) == -1)
2366 26ed57b2 2018-05-19 stsp err(1, "pledge");
2369 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2370 26ed57b2 2018-05-19 stsp switch (ch) {
2373 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2377 26ed57b2 2018-05-19 stsp argc -= optind;
2378 26ed57b2 2018-05-19 stsp argv += optind;
2380 26ed57b2 2018-05-19 stsp if (argc == 0) {
2381 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2382 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2383 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2384 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2385 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2386 15a94983 2018-12-23 stsp id_str1 = argv[0];
2387 15a94983 2018-12-23 stsp id_str2 = argv[1];
2388 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2389 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2390 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2391 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2392 15a94983 2018-12-23 stsp id_str1 = argv[1];
2393 15a94983 2018-12-23 stsp id_str2 = argv[2];
2395 26ed57b2 2018-05-19 stsp usage_diff();
2397 a915003a 2019-02-05 stsp init_curses();
2399 eb6600df 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
2400 eb6600df 2019-01-04 stsp if (error)
2401 eb6600df 2019-01-04 stsp goto done;
2403 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
2404 26ed57b2 2018-05-19 stsp free(repo_path);
2405 26ed57b2 2018-05-19 stsp if (error)
2406 26ed57b2 2018-05-19 stsp goto done;
2408 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
2409 26ed57b2 2018-05-19 stsp if (error)
2410 26ed57b2 2018-05-19 stsp goto done;
2412 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
2413 26ed57b2 2018-05-19 stsp if (error)
2414 26ed57b2 2018-05-19 stsp goto done;
2416 8b473291 2019-02-21 stsp SIMPLEQ_INIT(&refs);
2417 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2418 8b473291 2019-02-21 stsp if (error)
2419 8b473291 2019-02-21 stsp goto done;
2421 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2422 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2423 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
2424 ea5e7bb5 2018-08-01 stsp goto done;
2426 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2427 5dc9f4bc 2018-08-04 stsp if (error)
2428 5dc9f4bc 2018-08-04 stsp goto done;
2429 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2431 26ed57b2 2018-05-19 stsp got_repo_close(repo);
2432 26ed57b2 2018-05-19 stsp return error;
2435 4ed7e80c 2018-05-20 stsp __dead static void
2436 9f7d7167 2018-04-29 stsp usage_blame(void)
2439 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2440 9f7d7167 2018-04-29 stsp getprogname());
2444 84451b3e 2018-07-10 stsp struct tog_blame_line {
2445 84451b3e 2018-07-10 stsp int annotated;
2446 84451b3e 2018-07-10 stsp struct got_object_id *id;
2449 4ed7e80c 2018-05-20 stsp static const struct got_error *
2450 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2451 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2452 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2453 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2455 84451b3e 2018-07-10 stsp const struct got_error *err;
2456 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2457 84451b3e 2018-07-10 stsp char *line;
2458 84451b3e 2018-07-10 stsp size_t len;
2459 84451b3e 2018-07-10 stsp wchar_t *wline;
2460 b700b5d6 2018-07-10 stsp int width, wlimit;
2461 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2462 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2463 ab089a2a 2018-07-12 stsp char *id_str;
2465 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2467 ab089a2a 2018-07-12 stsp return err;
2469 84451b3e 2018-07-10 stsp rewind(f);
2470 f7d12f7e 2018-08-01 stsp werase(view->window);
2472 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
2473 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
2474 ab089a2a 2018-07-12 stsp free(id_str);
2475 ab089a2a 2018-07-12 stsp return err;
2478 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2479 ab089a2a 2018-07-12 stsp free(line);
2480 2550e4c3 2018-07-13 stsp line = NULL;
2481 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2482 a3404814 2018-09-02 stsp wstandout(view->window);
2483 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2484 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2485 a3404814 2018-09-02 stsp wstandend(view->window);
2486 2550e4c3 2018-07-13 stsp free(wline);
2487 2550e4c3 2018-07-13 stsp wline = NULL;
2488 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2489 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2491 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2492 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2493 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
2494 ab089a2a 2018-07-12 stsp free(id_str);
2495 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
2497 ab089a2a 2018-07-12 stsp free(id_str);
2498 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2499 3f60a8ef 2018-07-10 stsp free(line);
2500 2550e4c3 2018-07-13 stsp line = NULL;
2502 3f60a8ef 2018-07-10 stsp return err;
2503 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2504 2550e4c3 2018-07-13 stsp free(wline);
2505 2550e4c3 2018-07-13 stsp wline = NULL;
2506 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2507 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2510 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2511 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2512 84451b3e 2018-07-10 stsp if (line == NULL) {
2516 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2517 84451b3e 2018-07-10 stsp free(line);
2521 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2522 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2523 84451b3e 2018-07-10 stsp if (err) {
2524 84451b3e 2018-07-10 stsp free(line);
2525 84451b3e 2018-07-10 stsp return err;
2528 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2529 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2531 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2532 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2533 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2534 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2535 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2536 84451b3e 2018-07-10 stsp char *id_str;
2537 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2538 84451b3e 2018-07-10 stsp if (err) {
2539 84451b3e 2018-07-10 stsp free(line);
2540 2550e4c3 2018-07-13 stsp free(wline);
2541 84451b3e 2018-07-10 stsp return err;
2543 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2544 84451b3e 2018-07-10 stsp free(id_str);
2545 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2547 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2548 ee41ec32 2018-07-10 stsp prev_id = NULL;
2551 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2552 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2553 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2556 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2557 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2558 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2559 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2560 84451b3e 2018-07-10 stsp free(line);
2561 2550e4c3 2018-07-13 stsp free(wline);
2562 2550e4c3 2018-07-13 stsp wline = NULL;
2564 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2566 1a57306a 2018-09-02 stsp view_vborder(view);
2568 84451b3e 2018-07-10 stsp return NULL;
2571 84451b3e 2018-07-10 stsp static const struct got_error *
2572 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2574 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2575 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2576 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2577 1a76625f 2018-10-22 stsp int errcode;
2579 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2580 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2581 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2583 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2584 1a76625f 2018-10-22 stsp if (errcode)
2585 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2587 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2588 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2589 d68a0a7d 2018-07-10 stsp goto done;
2592 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2593 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2595 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2596 d68a0a7d 2018-07-10 stsp if (line->annotated)
2597 d68a0a7d 2018-07-10 stsp goto done;
2599 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2600 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2601 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2602 84451b3e 2018-07-10 stsp goto done;
2604 84451b3e 2018-07-10 stsp line->annotated = 1;
2606 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2607 1a76625f 2018-10-22 stsp if (errcode)
2608 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2609 84451b3e 2018-07-10 stsp return err;
2612 84451b3e 2018-07-10 stsp static void *
2613 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2615 18430de3 2018-07-10 stsp const struct got_error *err;
2616 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
2617 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
2618 1a76625f 2018-10-22 stsp int errcode;
2620 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2621 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
2623 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2624 1a76625f 2018-10-22 stsp if (errcode)
2625 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
2627 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
2628 c9beca56 2018-07-22 stsp ta->repo = NULL;
2629 c9beca56 2018-07-22 stsp *ta->complete = 1;
2631 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2632 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2633 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2635 18430de3 2018-07-10 stsp return (void *)err;
2638 245d91c1 2018-07-12 stsp static struct got_object_id *
2639 15a94983 2018-12-23 stsp get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2640 15a94983 2018-12-23 stsp int selected_line)
2642 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2644 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2645 245d91c1 2018-07-12 stsp if (!line->annotated)
2646 245d91c1 2018-07-12 stsp return NULL;
2648 245d91c1 2018-07-12 stsp return line->id;
2651 b880a918 2018-07-10 stsp static const struct got_error *
2652 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2654 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2657 245d91c1 2018-07-12 stsp if (blame->thread) {
2658 1a76625f 2018-10-22 stsp int errcode;
2659 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2660 1a76625f 2018-10-22 stsp if (errcode)
2661 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2662 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
2663 1a76625f 2018-10-22 stsp if (errcode)
2664 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2665 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2666 1a76625f 2018-10-22 stsp if (errcode)
2667 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2668 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2669 245d91c1 2018-07-12 stsp err = NULL;
2670 245d91c1 2018-07-12 stsp blame->thread = NULL;
2672 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2673 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2674 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2676 245d91c1 2018-07-12 stsp if (blame->f) {
2677 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
2678 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
2679 245d91c1 2018-07-12 stsp blame->f = NULL;
2681 57670559 2018-12-23 stsp if (blame->lines) {
2682 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
2683 57670559 2018-12-23 stsp free(blame->lines[i].id);
2684 57670559 2018-12-23 stsp free(blame->lines);
2685 57670559 2018-12-23 stsp blame->lines = NULL;
2687 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2688 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2690 245d91c1 2018-07-12 stsp return err;
2693 245d91c1 2018-07-12 stsp static const struct got_error *
2694 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2695 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
2696 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
2697 245d91c1 2018-07-12 stsp struct got_repository *repo)
2699 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2700 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2701 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2702 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2703 15a94983 2018-12-23 stsp int obj_type;
2705 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2707 15a94983 2018-12-23 stsp return err;
2708 15a94983 2018-12-23 stsp if (obj_id == NULL)
2709 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
2711 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
2713 84451b3e 2018-07-10 stsp goto done;
2715 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2716 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2717 84451b3e 2018-07-10 stsp goto done;
2720 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2722 a70480e0 2018-06-23 stsp goto done;
2723 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2724 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2725 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2726 84451b3e 2018-07-10 stsp goto done;
2728 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2729 245d91c1 2018-07-12 stsp blame->f, blob);
2731 84451b3e 2018-07-10 stsp goto done;
2733 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2734 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2735 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2736 84451b3e 2018-07-10 stsp goto done;
2739 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2741 bd24772e 2018-07-11 stsp goto done;
2743 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2744 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2745 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2746 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2747 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2748 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2749 245d91c1 2018-07-12 stsp goto done;
2751 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2753 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2754 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2755 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2756 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2757 245d91c1 2018-07-12 stsp *blame_complete = 0;
2761 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2762 27d434c2 2018-09-15 stsp free(obj_id);
2764 245d91c1 2018-07-12 stsp stop_blame(blame);
2765 245d91c1 2018-07-12 stsp return err;
2768 245d91c1 2018-07-12 stsp static const struct got_error *
2769 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2770 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
2771 8b473291 2019-02-21 stsp struct got_repository *repo)
2773 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2774 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2776 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2778 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2780 7cbe629d 2018-08-04 stsp return err;
2782 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2783 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2784 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2785 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2786 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2787 fb2756b9 2018-08-04 stsp s->path = path;
2788 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2789 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2790 fb2756b9 2018-08-04 stsp s->repo = repo;
2791 8b473291 2019-02-21 stsp s->refs = refs;
2792 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2793 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2795 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2796 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2797 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2799 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
2800 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2801 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2802 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2805 e5a0f69f 2018-08-18 stsp static const struct got_error *
2806 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2808 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2809 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2811 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2812 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2814 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2815 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2816 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2817 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2818 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2821 e5a0f69f 2018-08-18 stsp free(s->path);
2823 e5a0f69f 2018-08-18 stsp return err;
2826 7cbe629d 2018-08-04 stsp static const struct got_error *
2827 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2829 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2830 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2831 2b380cc8 2018-10-24 stsp int errcode;
2833 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
2834 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2835 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
2836 2b380cc8 2018-10-24 stsp if (errcode)
2837 2b380cc8 2018-10-24 stsp return got_error_set_errno(errcode);
2840 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2841 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2842 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2843 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2845 669b5ffa 2018-10-07 stsp view_vborder(view);
2846 e5a0f69f 2018-08-18 stsp return err;
2849 e5a0f69f 2018-08-18 stsp static const struct got_error *
2850 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2851 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2853 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2854 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2855 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2856 669b5ffa 2018-10-07 stsp int begin_x = 0;
2858 e5a0f69f 2018-08-18 stsp switch (ch) {
2860 e5a0f69f 2018-08-18 stsp s->done = 1;
2863 e5a0f69f 2018-08-18 stsp case KEY_UP:
2864 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2865 e5a0f69f 2018-08-18 stsp s->selected_line--;
2866 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2867 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2868 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2870 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2871 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2872 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2875 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2876 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2877 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2879 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2882 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2883 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2884 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2885 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2886 e5a0f69f 2018-08-18 stsp s->selected_line++;
2887 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2888 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2889 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2892 e5a0f69f 2018-08-18 stsp case 'p': {
2893 15a94983 2018-12-23 stsp struct got_object_id *id = NULL;
2894 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2895 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2896 15a94983 2018-12-23 stsp if (id == NULL)
2898 15a94983 2018-12-23 stsp if (ch == 'p') {
2899 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2900 15a94983 2018-12-23 stsp struct got_object_qid *pid;
2901 15a94983 2018-12-23 stsp struct got_object_id *blob_id = NULL;
2902 15a94983 2018-12-23 stsp int obj_type;
2903 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit,
2904 15a94983 2018-12-23 stsp s->repo, id);
2907 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
2908 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
2909 15a94983 2018-12-23 stsp if (pid == NULL) {
2910 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2913 15a94983 2018-12-23 stsp /* Check if path history ends here. */
2914 15a94983 2018-12-23 stsp err = got_object_id_by_path(&blob_id, s->repo,
2915 15a94983 2018-12-23 stsp pid->id, s->path);
2916 15a94983 2018-12-23 stsp if (err) {
2917 15a94983 2018-12-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
2918 15a94983 2018-12-23 stsp err = NULL;
2919 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2922 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo,
2924 15a94983 2018-12-23 stsp free(blob_id);
2925 15a94983 2018-12-23 stsp /* Can't blame non-blob type objects. */
2926 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2927 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2930 15a94983 2018-12-23 stsp err = got_object_qid_alloc(&s->blamed_commit,
2932 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2934 15a94983 2018-12-23 stsp if (got_object_id_cmp(id,
2935 15a94983 2018-12-23 stsp s->blamed_commit->id) == 0)
2937 15a94983 2018-12-23 stsp err = got_object_qid_alloc(&s->blamed_commit,
2942 e5a0f69f 2018-08-18 stsp s->done = 1;
2943 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2944 e5a0f69f 2018-08-18 stsp s->done = 0;
2945 e5a0f69f 2018-08-18 stsp if (thread_err)
2947 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2948 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2949 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2950 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2951 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2952 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2957 e5a0f69f 2018-08-18 stsp case 'B': {
2958 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2959 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2960 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2962 e5a0f69f 2018-08-18 stsp s->done = 1;
2963 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2964 e5a0f69f 2018-08-18 stsp s->done = 0;
2965 e5a0f69f 2018-08-18 stsp if (thread_err)
2967 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2968 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2969 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2970 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2971 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2972 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2973 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2974 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2979 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2980 15a94983 2018-12-23 stsp case '\r': {
2981 15a94983 2018-12-23 stsp struct got_object_id *id = NULL;
2982 15a94983 2018-12-23 stsp struct got_object_qid *pid;
2983 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2984 15a94983 2018-12-23 stsp id = get_selected_commit_id(s->blame.lines,
2985 15a94983 2018-12-23 stsp s->first_displayed_line, s->selected_line);
2986 3e9926ea 2019-01-04 stsp if (id == NULL)
2988 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, s->repo, id);
2991 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
2992 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
2993 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
2994 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
2995 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2996 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2997 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2998 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
3001 15a94983 2018-12-23 stsp err = open_diff_view(diff_view, pid ? pid->id : NULL,
3002 8b473291 2019-02-21 stsp id, NULL, s->refs, s->repo);
3003 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3004 e5a0f69f 2018-08-18 stsp if (err) {
3005 e5a0f69f 2018-08-18 stsp view_close(diff_view);
3008 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3009 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3012 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
3013 669b5ffa 2018-10-07 stsp if (err) {
3014 669b5ffa 2018-10-07 stsp view_close(diff_view);
3017 51c68690 2018-11-07 stsp *focus_view = diff_view;
3018 51c68690 2018-11-07 stsp view->child_focussed = 1;
3020 669b5ffa 2018-10-07 stsp *new_view = diff_view;
3025 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
3027 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
3028 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
3029 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
3030 e5a0f69f 2018-08-18 stsp view->nlines - 2);
3033 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
3034 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
3035 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
3036 e5a0f69f 2018-08-18 stsp view->nlines - 2;
3038 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
3039 e5a0f69f 2018-08-18 stsp s->blame.nlines -
3040 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
3042 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3043 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
3044 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
3045 e5a0f69f 2018-08-18 stsp view->nlines - 2);
3051 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
3054 a70480e0 2018-06-23 stsp static const struct got_error *
3055 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
3057 a70480e0 2018-06-23 stsp const struct got_error *error;
3058 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
3059 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3060 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
3061 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3062 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3063 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
3065 e1cd8fed 2018-08-01 stsp struct tog_view *view;
3067 a70480e0 2018-06-23 stsp #ifndef PROFILE
3068 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3069 8e94dd5b 2019-01-04 stsp NULL) == -1)
3070 a70480e0 2018-06-23 stsp err(1, "pledge");
3073 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3074 a70480e0 2018-06-23 stsp switch (ch) {
3076 a70480e0 2018-06-23 stsp commit_id_str = optarg;
3079 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3080 69069811 2018-08-02 stsp if (repo_path == NULL)
3081 69069811 2018-08-02 stsp err(1, "-r option");
3085 a70480e0 2018-06-23 stsp /* NOTREACHED */
3089 a70480e0 2018-06-23 stsp argc -= optind;
3090 a70480e0 2018-06-23 stsp argv += optind;
3092 69069811 2018-08-02 stsp if (argc == 1)
3093 69069811 2018-08-02 stsp path = argv[0];
3095 a70480e0 2018-06-23 stsp usage_blame();
3097 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
3098 69069811 2018-08-02 stsp if (cwd == NULL) {
3099 69069811 2018-08-02 stsp error = got_error_from_errno();
3100 69069811 2018-08-02 stsp goto done;
3102 69069811 2018-08-02 stsp if (repo_path == NULL) {
3103 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3104 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3105 69069811 2018-08-02 stsp goto done;
3107 eb41ed75 2019-02-05 stsp error = NULL;
3108 eb41ed75 2019-02-05 stsp if (worktree) {
3109 eb41ed75 2019-02-05 stsp repo_path =
3110 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3111 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
3112 eb41ed75 2019-02-05 stsp error = got_error_from_errno();
3113 eb41ed75 2019-02-05 stsp if (error)
3114 eb41ed75 2019-02-05 stsp goto done;
3116 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
3117 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
3118 eb41ed75 2019-02-05 stsp error = got_error_from_errno();
3119 eb41ed75 2019-02-05 stsp goto done;
3124 a915003a 2019-02-05 stsp init_curses();
3126 8e94dd5b 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
3127 8e94dd5b 2019-01-04 stsp if (error)
3128 8e94dd5b 2019-01-04 stsp goto done;
3130 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
3131 a70480e0 2018-06-23 stsp if (error != NULL)
3132 92205607 2019-01-04 stsp goto done;
3134 eb41ed75 2019-02-05 stsp if (worktree) {
3135 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3136 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3137 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3138 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3139 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3140 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3141 eb41ed75 2019-02-05 stsp path) == -1) {
3142 eb41ed75 2019-02-05 stsp error = got_error_from_errno();
3143 eb41ed75 2019-02-05 stsp goto done;
3145 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3148 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3150 eb41ed75 2019-02-05 stsp if (error)
3151 69069811 2018-08-02 stsp goto done;
3153 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
3154 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
3155 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3156 a70480e0 2018-06-23 stsp if (error != NULL)
3157 66b4983c 2018-06-23 stsp goto done;
3158 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3159 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
3161 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
3162 15a94983 2018-12-23 stsp commit_id_str);
3164 a19e88aa 2018-06-23 stsp if (error != NULL)
3165 8b473291 2019-02-21 stsp goto done;
3167 8b473291 2019-02-21 stsp SIMPLEQ_INIT(&refs);
3168 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
3169 8b473291 2019-02-21 stsp if (error)
3170 a19e88aa 2018-06-23 stsp goto done;
3172 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3173 e1cd8fed 2018-08-01 stsp if (view == NULL) {
3174 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
3175 e1cd8fed 2018-08-01 stsp goto done;
3177 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3178 7cbe629d 2018-08-04 stsp if (error)
3179 7cbe629d 2018-08-04 stsp goto done;
3180 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3182 69069811 2018-08-02 stsp free(repo_path);
3183 69069811 2018-08-02 stsp free(cwd);
3184 a70480e0 2018-06-23 stsp free(commit_id);
3185 eb41ed75 2019-02-05 stsp if (worktree)
3186 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
3188 a70480e0 2018-06-23 stsp got_repo_close(repo);
3189 a70480e0 2018-06-23 stsp return error;
3192 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3193 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
3194 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
3195 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
3196 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
3197 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
3198 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
3200 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3201 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
3202 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
3203 ffd1d5e5 2018-06-23 stsp int width, n;
3205 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
3207 f7d12f7e 2018-08-01 stsp werase(view->window);
3209 ffd1d5e5 2018-06-23 stsp if (limit == 0)
3210 ffd1d5e5 2018-06-23 stsp return NULL;
3212 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
3214 ffd1d5e5 2018-06-23 stsp return err;
3215 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3216 a3404814 2018-09-02 stsp wstandout(view->window);
3217 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3218 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3219 a3404814 2018-09-02 stsp wstandend(view->window);
3220 2550e4c3 2018-07-13 stsp free(wline);
3221 2550e4c3 2018-07-13 stsp wline = NULL;
3222 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
3223 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3224 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3225 ffd1d5e5 2018-06-23 stsp return NULL;
3226 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
3228 ce52c690 2018-06-23 stsp return err;
3229 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3230 2550e4c3 2018-07-13 stsp free(wline);
3231 2550e4c3 2018-07-13 stsp wline = NULL;
3232 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
3233 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3234 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3235 ffd1d5e5 2018-06-23 stsp return NULL;
3236 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3237 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
3238 a1eca9bb 2018-06-23 stsp return NULL;
3240 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3241 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
3242 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
3243 0cf4efb1 2018-09-29 stsp if (view->focussed)
3244 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3245 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
3247 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
3248 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
3249 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3250 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3251 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3252 ffd1d5e5 2018-06-23 stsp return NULL;
3256 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
3257 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3260 ffd1d5e5 2018-06-23 stsp while (te) {
3261 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
3263 1d13200f 2018-07-12 stsp if (show_ids) {
3264 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
3266 1d13200f 2018-07-12 stsp return got_error_from_errno();
3268 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3269 d8355ef1 2019-02-10 stsp te->name, S_ISDIR(te->mode) ? "/" :
3270 d8355ef1 2019-02-10 stsp ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3271 1d13200f 2018-07-12 stsp free(id_str);
3272 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3274 1d13200f 2018-07-12 stsp free(id_str);
3275 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
3276 ffd1d5e5 2018-06-23 stsp if (err) {
3277 ffd1d5e5 2018-06-23 stsp free(line);
3280 ffd1d5e5 2018-06-23 stsp if (n == selected) {
3281 0cf4efb1 2018-09-29 stsp if (view->focussed)
3282 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3283 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
3285 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3286 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
3287 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3288 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
3289 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3290 ffd1d5e5 2018-06-23 stsp free(line);
3291 2550e4c3 2018-07-13 stsp free(wline);
3292 2550e4c3 2018-07-13 stsp wline = NULL;
3294 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3295 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
3296 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3298 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3301 ffd1d5e5 2018-06-23 stsp return err;
3304 ffd1d5e5 2018-06-23 stsp static void
3305 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3306 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
3308 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
3311 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
3314 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3315 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
3316 ffd1d5e5 2018-06-23 stsp if (!isroot)
3317 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3321 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
3322 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
3323 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
3324 ffd1d5e5 2018-06-23 stsp prev = te;
3325 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3327 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
3328 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3330 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3331 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3334 768394f3 2019-01-24 stsp static int
3335 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3336 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
3337 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
3339 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
3340 ffd1d5e5 2018-06-23 stsp int n = 0;
3342 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
3343 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3345 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
3346 768394f3 2019-01-24 stsp last = last_displayed_entry;
3347 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
3348 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
3349 768394f3 2019-01-24 stsp if (last) {
3350 768394f3 2019-01-24 stsp *first_displayed_entry = next;
3351 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
3357 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3358 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
3359 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
3361 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
3362 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
3363 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
3365 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
3366 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
3368 ce52c690 2018-06-23 stsp len += strlen(te->name);
3370 ce52c690 2018-06-23 stsp *path = calloc(1, len);
3371 ffd1d5e5 2018-06-23 stsp if (path == NULL)
3372 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3374 ce52c690 2018-06-23 stsp (*path)[0] = '/';
3375 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
3376 d9765a41 2018-06-23 stsp while (pt) {
3377 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3378 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3379 cb2ebc8a 2018-06-23 stsp goto done;
3381 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
3382 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3383 cb2ebc8a 2018-06-23 stsp goto done;
3385 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3388 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3389 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3390 ce52c690 2018-06-23 stsp goto done;
3394 ce52c690 2018-06-23 stsp if (err) {
3395 ce52c690 2018-06-23 stsp free(*path);
3396 ce52c690 2018-06-23 stsp *path = NULL;
3398 ce52c690 2018-06-23 stsp return err;
3401 ce52c690 2018-06-23 stsp static const struct got_error *
3402 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3403 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3404 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3405 8b473291 2019-02-21 stsp struct got_repository *repo)
3407 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3408 ce52c690 2018-06-23 stsp char *path;
3409 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3411 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3413 ce52c690 2018-06-23 stsp return err;
3415 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3416 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3417 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3419 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
3420 e5a0f69f 2018-08-18 stsp if (err) {
3421 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3422 e5a0f69f 2018-08-18 stsp free(path);
3424 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3425 69efd4c4 2018-07-18 stsp return err;
3428 69efd4c4 2018-07-18 stsp static const struct got_error *
3429 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3430 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3431 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3432 8b473291 2019-02-21 stsp struct got_repository *repo)
3434 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3435 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3436 69efd4c4 2018-07-18 stsp char *path;
3438 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3439 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3440 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3442 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3444 69efd4c4 2018-07-18 stsp return err;
3446 8b473291 2019-02-21 stsp err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3448 e5a0f69f 2018-08-18 stsp view_close(log_view);
3450 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3451 cb2ebc8a 2018-06-23 stsp free(path);
3452 cb2ebc8a 2018-06-23 stsp return err;
3455 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3456 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3457 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3458 8b473291 2019-02-21 stsp struct got_repository *repo)
3460 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3461 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3462 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3464 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3466 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3467 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3468 ffd1d5e5 2018-06-23 stsp goto done;
3470 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3471 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
3472 ffd1d5e5 2018-06-23 stsp goto done;
3475 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3476 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3477 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3478 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3479 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3480 6484ec90 2018-09-29 stsp err = got_error_from_errno();
3481 6484ec90 2018-09-29 stsp goto done;
3483 8b473291 2019-02-21 stsp s->refs = refs;
3484 fb2756b9 2018-08-04 stsp s->repo = repo;
3486 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
3487 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
3488 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
3490 ad80ab7b 2018-08-04 stsp free(commit_id_str);
3491 6484ec90 2018-09-29 stsp if (err) {
3492 fb2756b9 2018-08-04 stsp free(s->tree_label);
3493 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3495 ad80ab7b 2018-08-04 stsp return err;
3498 e5a0f69f 2018-08-18 stsp static const struct got_error *
3499 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
3501 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3503 fb2756b9 2018-08-04 stsp free(s->tree_label);
3504 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3505 6484ec90 2018-09-29 stsp free(s->commit_id);
3506 6484ec90 2018-09-29 stsp s->commit_id = NULL;
3507 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
3508 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
3509 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
3510 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
3511 ad80ab7b 2018-08-04 stsp free(parent);
3514 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
3515 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
3516 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
3518 e5a0f69f 2018-08-18 stsp return NULL;
3521 ad80ab7b 2018-08-04 stsp static const struct got_error *
3522 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
3524 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
3525 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3526 e5a0f69f 2018-08-18 stsp char *parent_path;
3528 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
3530 e5a0f69f 2018-08-18 stsp return err;
3532 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
3533 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
3534 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3535 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
3536 e5a0f69f 2018-08-18 stsp free(parent_path);
3538 669b5ffa 2018-10-07 stsp view_vborder(view);
3539 e5a0f69f 2018-08-18 stsp return err;
3542 e5a0f69f 2018-08-18 stsp static const struct got_error *
3543 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3544 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3546 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3547 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
3548 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
3549 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
3551 e5a0f69f 2018-08-18 stsp switch (ch) {
3553 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
3556 669b5ffa 2018-10-07 stsp if (!s->selected_entry)
3558 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
3559 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
3560 669b5ffa 2018-10-07 stsp err = log_tree_entry(&log_view, begin_x,
3561 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
3562 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
3563 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3564 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3566 669b5ffa 2018-10-07 stsp return err;
3567 669b5ffa 2018-10-07 stsp err = view_set_child(view, log_view);
3568 669b5ffa 2018-10-07 stsp if (err) {
3569 669b5ffa 2018-10-07 stsp view_close(log_view);
3572 669b5ffa 2018-10-07 stsp *focus_view = log_view;
3573 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3575 669b5ffa 2018-10-07 stsp *new_view = log_view;
3578 e5a0f69f 2018-08-18 stsp case KEY_UP:
3579 bf979164 2019-01-24 stsp if (s->selected > 0) {
3580 e5a0f69f 2018-08-18 stsp s->selected--;
3581 bf979164 2019-01-24 stsp if (s->selected == 0)
3584 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3586 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
3587 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
3589 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
3590 67cc7991 2019-01-24 stsp tree_scroll_up(&s->first_displayed_entry,
3591 67cc7991 2019-01-24 stsp MAX(0, view->nlines - 4 - s->selected), s->entries,
3592 67cc7991 2019-01-24 stsp s->tree == s->root);
3593 8a0ead39 2018-11-03 stsp s->selected = 0;
3594 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
3595 67cc7991 2019-01-24 stsp s->first_displayed_entry && s->tree != s->root)
3596 67cc7991 2019-01-24 stsp s->first_displayed_entry = NULL;
3599 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
3600 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
3601 e5a0f69f 2018-08-18 stsp s->selected++;
3604 768394f3 2019-01-24 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3605 768394f3 2019-01-24 stsp == NULL) {
3606 768394f3 2019-01-24 stsp /* can't scroll any further */
3609 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
3610 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
3612 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
3613 768394f3 2019-01-24 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3614 768394f3 2019-01-24 stsp == NULL) {
3615 768394f3 2019-01-24 stsp /* can't scroll any further; move cursor down */
3616 768394f3 2019-01-24 stsp if (s->selected < s->ndisplayed - 1)
3617 768394f3 2019-01-24 stsp s->selected = s->ndisplayed - 1;
3620 768394f3 2019-01-24 stsp nscrolled = tree_scroll_down(&s->first_displayed_entry,
3621 768394f3 2019-01-24 stsp view->nlines, s->last_displayed_entry, s->entries);
3622 768394f3 2019-01-24 stsp if (nscrolled < view->nlines) {
3623 768394f3 2019-01-24 stsp int ndisplayed = 0;
3624 768394f3 2019-01-24 stsp struct got_tree_entry *te;
3625 768394f3 2019-01-24 stsp te = s->first_displayed_entry;
3627 768394f3 2019-01-24 stsp ndisplayed++;
3628 768394f3 2019-01-24 stsp te = SIMPLEQ_NEXT(te, entry);
3629 768394f3 2019-01-24 stsp } while (te);
3630 768394f3 2019-01-24 stsp s->selected = ndisplayed - 1;
3633 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
3634 e5a0f69f 2018-08-18 stsp case '\r':
3635 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
3636 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3637 7837eeac 2018-09-24 stsp case KEY_BACKSPACE:
3638 e5a0f69f 2018-08-18 stsp /* user selected '..' */
3639 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
3641 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
3642 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
3644 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
3645 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
3646 e5a0f69f 2018-08-18 stsp s->entries =
3647 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3648 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
3649 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
3650 e5a0f69f 2018-08-18 stsp s->selected_entry =
3651 e5a0f69f 2018-08-18 stsp parent->selected_entry;
3652 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
3653 e5a0f69f 2018-08-18 stsp free(parent);
3654 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
3655 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3656 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
3657 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
3658 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
3661 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
3662 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
3663 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
3666 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
3667 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
3668 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
3669 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
3670 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
3671 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3672 e5a0f69f 2018-08-18 stsp s->tree = child;
3673 e5a0f69f 2018-08-18 stsp s->entries =
3674 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3675 e5a0f69f 2018-08-18 stsp s->selected = 0;
3676 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3677 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
3678 669b5ffa 2018-10-07 stsp struct tog_view *blame_view;
3679 669b5ffa 2018-10-07 stsp int begin_x = view_is_parent_view(view) ?
3680 669b5ffa 2018-10-07 stsp view_split_begin_x(view->begin_x) : 0;
3682 669b5ffa 2018-10-07 stsp err = blame_tree_entry(&blame_view, begin_x,
3683 8b473291 2019-02-21 stsp s->selected_entry, &s->parents,
3684 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
3687 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3688 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3690 669b5ffa 2018-10-07 stsp return err;
3691 669b5ffa 2018-10-07 stsp err = view_set_child(view, blame_view);
3692 669b5ffa 2018-10-07 stsp if (err) {
3693 669b5ffa 2018-10-07 stsp view_close(blame_view);
3696 669b5ffa 2018-10-07 stsp *focus_view = blame_view;
3697 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3699 669b5ffa 2018-10-07 stsp *new_view = blame_view;
3702 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3703 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3704 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3710 ffd1d5e5 2018-06-23 stsp return err;
3713 ffd1d5e5 2018-06-23 stsp __dead static void
3714 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3717 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3718 ffd1d5e5 2018-06-23 stsp getprogname());
3722 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3723 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3725 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3726 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3727 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3728 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3729 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3730 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3731 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3732 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3734 5221c383 2018-08-01 stsp struct tog_view *view;
3736 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3737 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3738 d188b9a6 2019-01-04 stsp NULL) == -1)
3739 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3742 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3743 ffd1d5e5 2018-06-23 stsp switch (ch) {
3745 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3749 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3753 ffd1d5e5 2018-06-23 stsp argc -= optind;
3754 ffd1d5e5 2018-06-23 stsp argv += optind;
3756 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3757 52185f70 2019-02-05 stsp struct got_worktree *worktree;
3758 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
3759 52185f70 2019-02-05 stsp if (cwd == NULL)
3760 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3761 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3762 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3763 52185f70 2019-02-05 stsp goto done;
3764 52185f70 2019-02-05 stsp if (worktree) {
3765 52185f70 2019-02-05 stsp free(cwd);
3766 52185f70 2019-02-05 stsp repo_path =
3767 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3768 52185f70 2019-02-05 stsp got_worktree_close(worktree);
3770 52185f70 2019-02-05 stsp repo_path = cwd;
3771 52185f70 2019-02-05 stsp if (repo_path == NULL) {
3772 52185f70 2019-02-05 stsp error = got_error_from_errno();
3773 52185f70 2019-02-05 stsp goto done;
3775 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3776 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3777 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3778 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3780 ffd1d5e5 2018-06-23 stsp usage_log();
3782 a915003a 2019-02-05 stsp init_curses();
3784 d188b9a6 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
3785 d188b9a6 2019-01-04 stsp if (error)
3786 52185f70 2019-02-05 stsp goto done;
3788 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3789 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3790 52185f70 2019-02-05 stsp goto done;
3792 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
3793 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3795 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
3796 15a94983 2018-12-23 stsp commit_id_arg);
3797 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3798 ffd1d5e5 2018-06-23 stsp goto done;
3800 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3801 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3802 ffd1d5e5 2018-06-23 stsp goto done;
3804 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
3805 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
3806 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3807 8b473291 2019-02-21 stsp goto done;
3809 8b473291 2019-02-21 stsp SIMPLEQ_INIT(&refs);
3810 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
3811 8b473291 2019-02-21 stsp if (error)
3812 ffd1d5e5 2018-06-23 stsp goto done;
3814 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3815 5221c383 2018-08-01 stsp if (view == NULL) {
3816 5221c383 2018-08-01 stsp error = got_error_from_errno();
3817 5221c383 2018-08-01 stsp goto done;
3819 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
3820 ad80ab7b 2018-08-04 stsp if (error)
3821 ad80ab7b 2018-08-04 stsp goto done;
3822 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3824 52185f70 2019-02-05 stsp free(repo_path);
3825 ffd1d5e5 2018-06-23 stsp free(commit_id);
3826 ffd1d5e5 2018-06-23 stsp if (commit)
3827 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3829 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3831 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3832 ffd1d5e5 2018-06-23 stsp return error;
3835 4ed7e80c 2018-05-20 stsp __dead static void
3836 9f7d7167 2018-04-29 stsp usage(void)
3840 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3841 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3842 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3843 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3844 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3849 c2301be8 2018-04-30 stsp static char **
3850 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3852 c2301be8 2018-04-30 stsp char **argv;
3853 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3855 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3856 c2301be8 2018-04-30 stsp if (argv == NULL)
3857 c2301be8 2018-04-30 stsp err(1, "calloc");
3858 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3859 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3860 c2301be8 2018-04-30 stsp err(1, "calloc");
3861 c2301be8 2018-04-30 stsp if (arg1) {
3862 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3863 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3864 c2301be8 2018-04-30 stsp err(1, "calloc");
3867 c2301be8 2018-04-30 stsp return argv;
3871 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3873 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3874 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3875 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3876 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3878 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
3880 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3881 9f7d7167 2018-04-29 stsp switch (ch) {
3883 9f7d7167 2018-04-29 stsp hflag = 1;
3887 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3891 9f7d7167 2018-04-29 stsp argc -= optind;
3892 9f7d7167 2018-04-29 stsp argv += optind;
3893 9f7d7167 2018-04-29 stsp optind = 0;
3894 c2301be8 2018-04-30 stsp optreset = 1;
3896 c2301be8 2018-04-30 stsp if (argc == 0) {
3897 f29d3e89 2018-06-23 stsp if (hflag)
3899 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3900 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3901 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3906 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3907 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3908 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3909 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3910 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3911 9f7d7167 2018-04-29 stsp if (hflag)
3912 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3916 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3917 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3918 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3919 c2301be8 2018-04-30 stsp if (repo_path) {
3920 c2301be8 2018-04-30 stsp struct got_repository *repo;
3921 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3922 c2301be8 2018-04-30 stsp if (error == NULL)
3923 c2301be8 2018-04-30 stsp got_repo_close(repo);
3925 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3926 c2301be8 2018-04-30 stsp if (error) {
3927 f29d3e89 2018-06-23 stsp if (hflag) {
3928 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3929 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3933 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3934 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3935 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3936 ad7de8d9 2018-04-30 stsp free(repo_path);
3939 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3940 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3942 c2301be8 2018-04-30 stsp free(repo_path);
3946 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3947 9f7d7167 2018-04-29 stsp if (error)
3948 9f7d7167 2018-04-29 stsp goto done;
3951 c2301be8 2018-04-30 stsp free(cmd_argv);
3952 9f7d7167 2018-04-29 stsp if (error)
3953 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);