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;
122 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
123 15a087fe 2019-02-21 stsp struct commit_queue_entry *selected_entry;
124 15a087fe 2019-02-21 stsp struct commit_queue *commits;
125 15a087fe 2019-02-21 stsp int *log_complete;
126 15a087fe 2019-02-21 stsp pthread_cond_t *need_commits;
127 15a087fe 2019-02-21 stsp int *commits_needed;
130 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
132 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
133 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
134 1a76625f 2018-10-22 stsp int commits_needed;
135 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
136 1a76625f 2018-10-22 stsp struct commit_queue *commits;
137 1a76625f 2018-10-22 stsp const char *in_repo_path;
138 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
139 1a76625f 2018-10-22 stsp struct got_repository *repo;
140 1a76625f 2018-10-22 stsp int log_complete;
141 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
142 1a76625f 2018-10-22 stsp struct tog_view *view;
143 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
144 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
147 1a76625f 2018-10-22 stsp struct tog_log_view_state {
148 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
149 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
150 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
151 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
152 b01e7d3b 2018-08-04 stsp int selected;
153 b01e7d3b 2018-08-04 stsp char *in_repo_path;
154 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
155 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
156 1a76625f 2018-10-22 stsp sig_atomic_t quit;
157 1a76625f 2018-10-22 stsp pthread_t thread;
158 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
161 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
162 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
163 e9424729 2018-08-04 stsp int nlines;
165 e9424729 2018-08-04 stsp struct tog_view *view;
166 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
170 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
171 e9424729 2018-08-04 stsp const char *path;
172 e9424729 2018-08-04 stsp struct got_repository *repo;
173 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
174 e9424729 2018-08-04 stsp int *complete;
177 e9424729 2018-08-04 stsp struct tog_blame {
179 e9424729 2018-08-04 stsp size_t filesize;
180 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
181 6fcac457 2018-11-19 stsp int nlines;
182 e9424729 2018-08-04 stsp pthread_t thread;
183 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
184 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
185 e9424729 2018-08-04 stsp const char *path;
188 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
189 7cbe629d 2018-08-04 stsp int first_displayed_line;
190 7cbe629d 2018-08-04 stsp int last_displayed_line;
191 7cbe629d 2018-08-04 stsp int selected_line;
192 7cbe629d 2018-08-04 stsp int blame_complete;
195 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
196 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
197 e5a0f69f 2018-08-18 stsp char *path;
198 7cbe629d 2018-08-04 stsp struct got_repository *repo;
199 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
200 e9424729 2018-08-04 stsp struct tog_blame blame;
203 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
204 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
205 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
206 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
207 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
208 ad80ab7b 2018-08-04 stsp int selected;
211 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
213 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
214 ad80ab7b 2018-08-04 stsp char *tree_label;
215 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
216 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
217 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
218 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
219 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
220 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
221 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
222 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
223 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
224 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
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 15a087fe 2019-02-21 stsp struct got_object_id *, struct got_object_id *, struct commit_queue_entry *,
274 15a087fe 2019-02-21 stsp struct commit_queue *, int *, pthread_cond_t *, int *,
275 15a087fe 2019-02-21 stsp struct got_repository *);
276 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
277 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
278 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
279 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
281 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
282 23721109 2018-10-22 stsp struct got_object_id *, 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 5221c383 2018-08-01 stsp struct got_object_id *, 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 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
297 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
298 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
299 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
300 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
302 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
304 25791caa 2018-10-24 stsp static void
305 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
307 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
310 e5a0f69f 2018-08-18 stsp static const struct got_error *
311 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
313 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
315 669b5ffa 2018-10-07 stsp if (view->child) {
316 669b5ffa 2018-10-07 stsp view_close(view->child);
317 669b5ffa 2018-10-07 stsp view->child = NULL;
319 e5a0f69f 2018-08-18 stsp if (view->close)
320 e5a0f69f 2018-08-18 stsp err = view->close(view);
321 ea5e7bb5 2018-08-01 stsp if (view->panel)
322 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
323 ea5e7bb5 2018-08-01 stsp if (view->window)
324 ea5e7bb5 2018-08-01 stsp delwin(view->window);
325 ea5e7bb5 2018-08-01 stsp free(view);
326 e5a0f69f 2018-08-18 stsp return err;
329 ea5e7bb5 2018-08-01 stsp static struct tog_view *
330 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
331 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
333 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
335 ea5e7bb5 2018-08-01 stsp if (view == NULL)
336 ea5e7bb5 2018-08-01 stsp return NULL;
338 d6b05b5b 2018-08-04 stsp view->type = type;
339 f7d12f7e 2018-08-01 stsp view->lines = LINES;
340 f7d12f7e 2018-08-01 stsp view->cols = COLS;
341 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
342 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
343 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
344 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
345 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
346 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
347 96a765a8 2018-08-04 stsp view_close(view);
348 ea5e7bb5 2018-08-01 stsp return NULL;
350 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
351 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
352 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
353 96a765a8 2018-08-04 stsp view_close(view);
354 ea5e7bb5 2018-08-01 stsp return NULL;
357 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
358 ea5e7bb5 2018-08-01 stsp return view;
362 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
364 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
366 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
369 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
371 5c60c32a 2018-10-18 stsp static const struct got_error *
372 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
374 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
376 5c60c32a 2018-10-18 stsp view->begin_y = 0;
377 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
378 5c60c32a 2018-10-18 stsp view->nlines = LINES;
379 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
380 5c60c32a 2018-10-18 stsp view->lines = LINES;
381 5c60c32a 2018-10-18 stsp view->cols = COLS;
382 5c60c32a 2018-10-18 stsp err = view_resize(view);
384 5c60c32a 2018-10-18 stsp return err;
386 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
387 5c60c32a 2018-10-18 stsp return got_error_from_errno();
389 5c60c32a 2018-10-18 stsp return NULL;
392 5c60c32a 2018-10-18 stsp static const struct got_error *
393 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
395 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
397 5c60c32a 2018-10-18 stsp view->begin_x = 0;
398 5c60c32a 2018-10-18 stsp view->begin_y = 0;
399 5c60c32a 2018-10-18 stsp view->nlines = LINES;
400 5c60c32a 2018-10-18 stsp view->ncols = COLS;
401 5c60c32a 2018-10-18 stsp view->lines = LINES;
402 5c60c32a 2018-10-18 stsp view->cols = COLS;
403 5c60c32a 2018-10-18 stsp err = view_resize(view);
405 5c60c32a 2018-10-18 stsp return err;
407 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
408 5c60c32a 2018-10-18 stsp return got_error_from_errno();
410 5c60c32a 2018-10-18 stsp return NULL;
414 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
416 5c60c32a 2018-10-18 stsp return view->parent == NULL;
419 4d8c2215 2018-08-19 stsp static const struct got_error *
420 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
422 f7d12f7e 2018-08-01 stsp int nlines, ncols;
424 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
425 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
427 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
429 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
430 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
432 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
434 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
435 0cf4efb1 2018-09-29 stsp return got_error_from_errno();
436 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
437 a6d7eb8d 2018-10-24 stsp return got_error_from_errno();
438 25791caa 2018-10-24 stsp wclear(view->window);
440 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
441 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
442 0cf4efb1 2018-09-29 stsp view->lines = LINES;
443 0cf4efb1 2018-09-29 stsp view->cols = COLS;
445 6e3e5d9c 2018-10-18 stsp if (view->child) {
446 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
447 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
448 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
449 5c60c32a 2018-10-18 stsp if (view->child->focussed)
450 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
452 5c60c32a 2018-10-18 stsp show_panel(view->panel);
454 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
455 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
459 5c60c32a 2018-10-18 stsp return NULL;
462 669b5ffa 2018-10-07 stsp static const struct got_error *
463 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
465 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
467 669b5ffa 2018-10-07 stsp if (view->child == NULL)
468 669b5ffa 2018-10-07 stsp return NULL;
470 669b5ffa 2018-10-07 stsp err = view_close(view->child);
471 669b5ffa 2018-10-07 stsp view->child = NULL;
472 669b5ffa 2018-10-07 stsp return err;
475 669b5ffa 2018-10-07 stsp static const struct got_error *
476 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
478 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
480 669b5ffa 2018-10-07 stsp view->child = child;
481 669b5ffa 2018-10-07 stsp child->parent = view;
482 669b5ffa 2018-10-07 stsp return err;
486 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
488 669b5ffa 2018-10-07 stsp return !view_is_parent_view(view) && view->begin_x > 0;
491 25791caa 2018-10-24 stsp static void
492 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
494 25791caa 2018-10-24 stsp int cols, lines;
495 25791caa 2018-10-24 stsp struct winsize size;
497 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
498 25791caa 2018-10-24 stsp cols = 80; /* Default */
499 25791caa 2018-10-24 stsp lines = 24;
501 25791caa 2018-10-24 stsp cols = size.ws_col;
502 25791caa 2018-10-24 stsp lines = size.ws_row;
504 25791caa 2018-10-24 stsp resize_term(lines, cols);
507 0cf4efb1 2018-09-29 stsp static const struct got_error *
508 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
509 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
510 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
512 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
513 669b5ffa 2018-10-07 stsp struct tog_view *v;
514 1a76625f 2018-10-22 stsp int ch, errcode;
516 e5a0f69f 2018-08-18 stsp *new = NULL;
517 e5a0f69f 2018-08-18 stsp *dead = NULL;
518 0cf4efb1 2018-09-29 stsp *focus = NULL;
520 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
521 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
522 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
523 1a76625f 2018-10-22 stsp if (errcode)
524 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
525 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
526 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
527 1a76625f 2018-10-22 stsp if (errcode)
528 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
529 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
531 25791caa 2018-10-24 stsp if (tog_sigwinch_received) {
532 25791caa 2018-10-24 stsp tog_resizeterm();
533 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
534 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
535 25791caa 2018-10-24 stsp err = view_resize(v);
537 25791caa 2018-10-24 stsp return err;
538 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
540 25791caa 2018-10-24 stsp return err;
544 e5a0f69f 2018-08-18 stsp switch (ch) {
548 669b5ffa 2018-10-07 stsp if (view->child) {
549 669b5ffa 2018-10-07 stsp *focus = view->child;
550 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
551 669b5ffa 2018-10-07 stsp } else if (view->parent) {
552 669b5ffa 2018-10-07 stsp *focus = view->parent;
553 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 0;
557 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
558 e5a0f69f 2018-08-18 stsp *dead = view;
564 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
565 669b5ffa 2018-10-07 stsp if (view->child == NULL)
567 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view->child)) {
568 669b5ffa 2018-10-07 stsp *focus = view->child;
569 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
570 669b5ffa 2018-10-07 stsp err = view_fullscreen(view->child);
572 669b5ffa 2018-10-07 stsp err = view_splitscreen(view->child);
575 669b5ffa 2018-10-07 stsp err = view->child->input(new, dead, focus,
576 669b5ffa 2018-10-07 stsp view->child, KEY_RESIZE);
578 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view)) {
579 669b5ffa 2018-10-07 stsp *focus = view;
580 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 1;
581 669b5ffa 2018-10-07 stsp err = view_fullscreen(view);
583 669b5ffa 2018-10-07 stsp err = view_splitscreen(view);
587 669b5ffa 2018-10-07 stsp err = view->input(new, dead, focus, view,
588 669b5ffa 2018-10-07 stsp KEY_RESIZE);
591 0cf4efb1 2018-09-29 stsp case KEY_RESIZE:
594 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
598 e5a0f69f 2018-08-18 stsp return err;
602 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
604 0cf4efb1 2018-09-29 stsp PANEL *panel;
605 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
607 669b5ffa 2018-10-07 stsp if (view->parent)
608 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
610 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
611 0cf4efb1 2018-09-29 stsp if (panel == NULL)
614 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
615 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
616 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
620 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
622 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
623 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
625 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
627 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
630 669b5ffa 2018-10-07 stsp return view->focussed;
633 bcbd79e2 2018-08-19 stsp static const struct got_error *
634 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
636 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
637 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
638 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
639 fd823528 2018-10-22 stsp int fast_refresh = 10;
640 1a76625f 2018-10-22 stsp int done = 0, errcode;
642 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
643 1a76625f 2018-10-22 stsp if (errcode)
644 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
646 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
647 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
649 a81bf10d 2018-09-29 stsp main_view = view;
650 1004088d 2018-09-29 stsp view->focussed = 1;
651 878940b7 2018-09-29 stsp err = view->show(view);
653 0cf4efb1 2018-09-29 stsp return err;
654 0cf4efb1 2018-09-29 stsp update_panels();
655 0cf4efb1 2018-09-29 stsp doupdate();
656 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
657 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
658 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
659 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
661 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
662 e4197bf9 2018-08-18 stsp view, &views);
665 e5a0f69f 2018-08-18 stsp if (dead_view) {
666 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
668 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
669 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
670 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
671 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
672 669b5ffa 2018-10-07 stsp prev = view->parent;
674 669b5ffa 2018-10-07 stsp if (dead_view->parent)
675 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
677 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
679 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
680 a81bf10d 2018-09-29 stsp if (err || dead_view == main_view)
683 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
684 0cf4efb1 2018-09-29 stsp if (focus_view)
685 0cf4efb1 2018-09-29 stsp view = focus_view;
686 669b5ffa 2018-10-07 stsp else if (prev)
687 669b5ffa 2018-10-07 stsp view = prev;
688 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
689 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
690 0cf4efb1 2018-09-29 stsp tog_view_list_head);
692 0cf4efb1 2018-09-29 stsp view = NULL;
693 669b5ffa 2018-10-07 stsp if (view) {
694 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
695 669b5ffa 2018-10-07 stsp focus_view = view->child;
697 669b5ffa 2018-10-07 stsp focus_view = view;
701 bcbd79e2 2018-08-19 stsp if (new_view) {
702 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
703 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
704 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
705 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
707 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
708 86c66b02 2018-10-18 stsp err = view_close(v);
713 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
714 fed7eaa8 2018-10-24 stsp view = new_view;
715 878940b7 2018-09-29 stsp if (focus_view == NULL)
716 878940b7 2018-09-29 stsp focus_view = new_view;
718 0cf4efb1 2018-09-29 stsp if (focus_view) {
719 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
721 0cf4efb1 2018-09-29 stsp view->focussed = 0;
722 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
723 0cf4efb1 2018-09-29 stsp view = focus_view;
724 878940b7 2018-09-29 stsp if (new_view)
725 878940b7 2018-09-29 stsp show_panel(new_view->panel);
726 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
727 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
729 669b5ffa 2018-10-07 stsp if (view) {
730 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
731 758194b5 2018-10-24 stsp view->focussed = 1;
732 758194b5 2018-10-24 stsp show_panel(view->panel);
733 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
734 758194b5 2018-10-24 stsp show_panel(view->child->panel);
735 1a76625f 2018-10-22 stsp focus_view = view;
737 669b5ffa 2018-10-07 stsp if (view->parent) {
738 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
742 669b5ffa 2018-10-07 stsp err = view->show(view);
745 669b5ffa 2018-10-07 stsp if (view->child) {
746 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
750 1a76625f 2018-10-22 stsp update_panels();
751 1a76625f 2018-10-22 stsp doupdate();
755 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
756 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
757 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
758 e5a0f69f 2018-08-18 stsp view_close(view);
761 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
762 1a76625f 2018-10-22 stsp if (errcode)
763 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
765 e5a0f69f 2018-08-18 stsp return err;
768 4ed7e80c 2018-05-20 stsp __dead static void
769 9f7d7167 2018-04-29 stsp usage_log(void)
772 c70c5802 2018-08-01 stsp fprintf(stderr,
773 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
774 9f7d7167 2018-04-29 stsp getprogname());
778 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
779 80ddbec8 2018-04-29 stsp static const struct got_error *
780 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
782 00dfcb92 2018-06-11 stsp char *vis = NULL;
783 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
785 963b370f 2018-05-20 stsp *ws = NULL;
786 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
787 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
788 00dfcb92 2018-06-11 stsp int vislen;
789 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
790 00dfcb92 2018-06-11 stsp return got_error_from_errno();
792 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
793 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
795 00dfcb92 2018-06-11 stsp return err;
796 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
797 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
798 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
803 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
804 a7f50699 2018-06-11 stsp if (*ws == NULL) {
805 a7f50699 2018-06-11 stsp err = got_error_from_errno();
809 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
810 963b370f 2018-05-20 stsp err = got_error_from_errno();
815 963b370f 2018-05-20 stsp *ws = NULL;
818 963b370f 2018-05-20 stsp return err;
821 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
822 963b370f 2018-05-20 stsp static const struct got_error *
823 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
825 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
826 963b370f 2018-05-20 stsp int cols = 0;
827 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
828 963b370f 2018-05-20 stsp size_t wlen;
831 963b370f 2018-05-20 stsp *wlinep = NULL;
832 b700b5d6 2018-07-10 stsp *widthp = 0;
834 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
836 963b370f 2018-05-20 stsp return err;
839 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
840 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
841 963b370f 2018-05-20 stsp switch (width) {
847 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
848 b700b5d6 2018-07-10 stsp cols += width;
852 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
853 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
857 963b370f 2018-05-20 stsp err = got_error_from_errno();
861 963b370f 2018-05-20 stsp wline[i] = L'\0';
862 b700b5d6 2018-07-10 stsp if (widthp)
863 b700b5d6 2018-07-10 stsp *widthp = cols;
866 963b370f 2018-05-20 stsp free(wline);
868 963b370f 2018-05-20 stsp *wlinep = wline;
869 963b370f 2018-05-20 stsp return err;
872 963b370f 2018-05-20 stsp static const struct got_error *
873 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
874 2814baeb 2018-08-01 stsp struct got_object_id *id)
876 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
877 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
878 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
879 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
880 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
881 bb737323 2018-05-20 stsp int author_width, logmsg_width;
882 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
883 80ddbec8 2018-04-29 stsp char *line = NULL;
884 bb737323 2018-05-20 stsp int col, limit;
885 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
886 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
887 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
888 ccb26ccd 2018-11-05 stsp struct tm tm;
889 45d799e2 2018-12-23 stsp time_t committer_time;
891 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
892 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
893 ccb26ccd 2018-11-05 stsp return got_error_from_errno();
894 ccb26ccd 2018-11-05 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
895 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
896 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
898 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
899 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
901 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
902 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
903 b39d25c7 2018-07-10 stsp col = limit + 1;
904 b39d25c7 2018-07-10 stsp if (col > avail)
907 45d799e2 2018-12-23 stsp author0 = strdup(got_object_commit_get_author(commit));
908 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
909 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
912 6d9fbc00 2018-04-29 stsp author = author0;
913 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
914 6d9fbc00 2018-04-29 stsp if (smallerthan)
915 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
917 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
919 6d9fbc00 2018-04-29 stsp *at = '\0';
921 b39d25c7 2018-07-10 stsp limit = avail - col;
922 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
925 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
926 bb737323 2018-05-20 stsp col += author_width;
927 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
928 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
930 bb737323 2018-05-20 stsp author_width++;
932 9c2eaf34 2018-05-20 stsp if (col > avail)
935 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
936 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
937 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
940 bb737323 2018-05-20 stsp logmsg = logmsg0;
941 bb737323 2018-05-20 stsp while (*logmsg == '\n')
943 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
944 bb737323 2018-05-20 stsp if (newline)
945 bb737323 2018-05-20 stsp *newline = '\0';
946 bb737323 2018-05-20 stsp limit = avail - col;
947 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
950 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
951 bb737323 2018-05-20 stsp col += logmsg_width;
952 bb737323 2018-05-20 stsp while (col <= avail) {
953 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
957 80ddbec8 2018-04-29 stsp free(logmsg0);
958 bb737323 2018-05-20 stsp free(wlogmsg);
959 6d9fbc00 2018-04-29 stsp free(author0);
960 bb737323 2018-05-20 stsp free(wauthor);
961 80ddbec8 2018-04-29 stsp free(line);
962 80ddbec8 2018-04-29 stsp return err;
965 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
966 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
967 899d86c2 2018-05-10 stsp struct got_object_id *id)
969 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
971 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
972 80ddbec8 2018-04-29 stsp if (entry == NULL)
973 899d86c2 2018-05-10 stsp return NULL;
975 899d86c2 2018-05-10 stsp entry->id = id;
976 99db9666 2018-05-07 stsp entry->commit = commit;
977 899d86c2 2018-05-10 stsp return entry;
980 99db9666 2018-05-07 stsp static void
981 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
983 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
985 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
986 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
987 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
988 ecb28ae0 2018-07-16 stsp commits->ncommits--;
989 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
990 99db9666 2018-05-07 stsp free(entry);
993 99db9666 2018-05-07 stsp static void
994 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
996 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
997 99db9666 2018-05-07 stsp pop_commit(commits);
1000 c4972b91 2018-05-07 stsp static const struct got_error *
1001 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1002 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
1004 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1005 93e45b7c 2018-09-24 stsp int nqueued = 0;
1008 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1009 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1010 1a76625f 2018-10-22 stsp * while updating the display.
1012 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
1013 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1014 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1015 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1016 1a76625f 2018-10-22 stsp int errcode;
1018 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1019 9ba79e04 2018-06-11 stsp if (err) {
1020 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1022 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
1023 93e45b7c 2018-09-24 stsp minqueue, repo);
1025 ecb28ae0 2018-07-16 stsp return err;
1029 ecb28ae0 2018-07-16 stsp if (id == NULL)
1032 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1035 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1036 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1037 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
1041 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1042 1a76625f 2018-10-22 stsp if (errcode) {
1043 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1047 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1048 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1049 ecb28ae0 2018-07-16 stsp nqueued++;
1050 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1052 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1053 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1054 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1057 9ba79e04 2018-06-11 stsp return err;
1060 99db9666 2018-05-07 stsp static const struct got_error *
1061 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1063 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1064 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1066 9ba79e04 2018-06-11 stsp *head_id = NULL;
1068 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1070 99db9666 2018-05-07 stsp return err;
1072 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1073 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1074 9ba79e04 2018-06-11 stsp if (err) {
1075 9ba79e04 2018-06-11 stsp *head_id = NULL;
1076 99db9666 2018-05-07 stsp return err;
1079 9ba79e04 2018-06-11 stsp return NULL;
1082 0553a4e3 2018-04-30 stsp static const struct got_error *
1083 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1084 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1085 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1086 1a76625f 2018-10-22 stsp const char *path, int commits_needed)
1088 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1089 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1090 ecb28ae0 2018-07-16 stsp int ncommits, width;
1091 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1092 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1094 e0d42f60 2018-07-22 stsp entry = first;
1095 e0d42f60 2018-07-22 stsp ncommits = 0;
1096 e0d42f60 2018-07-22 stsp while (entry) {
1097 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1098 e0d42f60 2018-07-22 stsp *selected = entry;
1101 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1102 e0d42f60 2018-07-22 stsp ncommits++;
1105 1a76625f 2018-10-22 stsp if (*selected) {
1106 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1108 ecb28ae0 2018-07-16 stsp return err;
1111 1a76625f 2018-10-22 stsp if (asprintf(&ncommits_str, " [%d/%d]%s ",
1112 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1113 1a76625f 2018-10-22 stsp commits_needed == 0 ? "" : " loading...") == -1)
1114 1a76625f 2018-10-22 stsp return got_error_from_errno();
1116 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1117 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1118 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1119 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1120 1a76625f 2018-10-22 stsp err = got_error_from_errno();
1121 1a76625f 2018-10-22 stsp header = NULL;
1122 1a76625f 2018-10-22 stsp goto done;
1124 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1125 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1126 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1127 1a76625f 2018-10-22 stsp err = got_error_from_errno();
1128 1a76625f 2018-10-22 stsp header = NULL;
1129 1a76625f 2018-10-22 stsp goto done;
1131 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1133 1a76625f 2018-10-22 stsp goto done;
1135 2814baeb 2018-08-01 stsp werase(view->window);
1137 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1138 a3404814 2018-09-02 stsp wstandout(view->window);
1139 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1140 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1141 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1144 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1145 a3404814 2018-09-02 stsp wstandend(view->window);
1146 ecb28ae0 2018-07-16 stsp free(wline);
1147 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1148 1a76625f 2018-10-22 stsp goto done;
1150 899d86c2 2018-05-10 stsp entry = first;
1151 899d86c2 2018-05-10 stsp *last = first;
1152 867c6645 2018-07-10 stsp ncommits = 0;
1153 899d86c2 2018-05-10 stsp while (entry) {
1154 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1156 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1157 2814baeb 2018-08-01 stsp wstandout(view->window);
1158 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
1159 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1160 2814baeb 2018-08-01 stsp wstandend(view->window);
1163 0553a4e3 2018-04-30 stsp ncommits++;
1164 899d86c2 2018-05-10 stsp *last = entry;
1165 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1168 1a57306a 2018-09-02 stsp view_vborder(view);
1170 1a76625f 2018-10-22 stsp free(id_str);
1171 1a76625f 2018-10-22 stsp free(ncommits_str);
1172 1a76625f 2018-10-22 stsp free(header);
1173 80ddbec8 2018-04-29 stsp return err;
1176 07b55e75 2018-05-10 stsp static void
1177 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1178 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1180 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1181 07b55e75 2018-05-10 stsp int nscrolled = 0;
1183 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1184 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
1187 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1188 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1189 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1190 07b55e75 2018-05-10 stsp if (entry) {
1191 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1192 07b55e75 2018-05-10 stsp nscrolled++;
1197 aa075928 2018-05-10 stsp static const struct got_error *
1198 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1199 93e45b7c 2018-09-24 stsp struct commit_queue_entry **last_displayed_entry,
1200 1a76625f 2018-10-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1201 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1203 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
1204 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
1205 aa075928 2018-05-10 stsp int nscrolled = 0;
1207 1a76625f 2018-10-22 stsp if (*last_displayed_entry == NULL)
1208 1a76625f 2018-10-22 stsp return NULL;
1211 93e45b7c 2018-09-24 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1212 aa075928 2018-05-10 stsp if (pentry == NULL) {
1213 1a76625f 2018-10-22 stsp int errcode;
1214 1a76625f 2018-10-22 stsp if (*log_complete)
1215 1a76625f 2018-10-22 stsp return NULL;
1216 1a76625f 2018-10-22 stsp *commits_needed = maxscroll + 20;
1217 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(need_commits);
1218 1a76625f 2018-10-22 stsp if (errcode)
1219 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1220 1a76625f 2018-10-22 stsp return NULL;
1222 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1224 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1225 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1227 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1228 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1230 dd0a52c1 2018-05-20 stsp return err;
1233 cd0acaa7 2018-05-20 stsp static const struct got_error *
1234 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1235 15a087fe 2019-02-21 stsp struct commit_queue_entry *selected_entry,
1236 15a087fe 2019-02-21 stsp struct commit_queue *commits, int *log_complete,
1237 15a087fe 2019-02-21 stsp pthread_cond_t *need_commits, int *commits_needed,
1238 2a4718d3 2018-09-29 stsp struct got_repository *repo)
1240 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1241 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1242 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1243 15a087fe 2019-02-21 stsp struct got_commit_object *commit = selected_entry->commit;
1244 15a087fe 2019-02-21 stsp struct got_object_id *commit_id = selected_entry->id;
1246 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1247 15a94983 2018-12-23 stsp if (diff_view == NULL)
1248 15a94983 2018-12-23 stsp return got_error_from_errno();
1250 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1251 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1252 15a087fe 2019-02-21 stsp commit_id, selected_entry, commits, log_complete, need_commits,
1253 15a087fe 2019-02-21 stsp commits_needed, repo);
1254 e5a0f69f 2018-08-18 stsp if (err == NULL)
1255 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1256 cd0acaa7 2018-05-20 stsp return err;
1259 80ddbec8 2018-04-29 stsp static const struct got_error *
1260 0cf4efb1 2018-09-29 stsp browse_commit(struct tog_view **new_view, int begin_x,
1261 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1263 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1264 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1265 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1267 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1268 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1270 9343a5fb 2018-06-23 stsp return err;
1272 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1273 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1274 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1276 e5a0f69f 2018-08-18 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1278 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1280 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1281 1a76625f 2018-10-22 stsp return err;
1284 1a76625f 2018-10-22 stsp static void *
1285 1a76625f 2018-10-22 stsp log_thread(void *arg)
1287 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1288 1a76625f 2018-10-22 stsp int errcode = 0;
1289 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1290 1a76625f 2018-10-22 stsp int done = 0;
1292 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1294 1a76625f 2018-10-22 stsp return (void *)err;
1296 1a76625f 2018-10-22 stsp while (!done && !err) {
1297 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1298 1a76625f 2018-10-22 stsp a->in_repo_path);
1299 1a76625f 2018-10-22 stsp if (err) {
1300 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1301 1a76625f 2018-10-22 stsp return (void *)err;
1302 1a76625f 2018-10-22 stsp err = NULL;
1304 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1305 1a76625f 2018-10-22 stsp a->commits_needed--;
1307 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1308 1a76625f 2018-10-22 stsp if (errcode)
1309 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
1312 1a76625f 2018-10-22 stsp a->log_complete = 1;
1313 1a76625f 2018-10-22 stsp else if (*a->quit) {
1315 1a76625f 2018-10-22 stsp a->log_complete = 1;
1316 1a76625f 2018-10-22 stsp } else if (*a->first_displayed_entry == NULL) {
1317 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1318 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1319 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1323 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1324 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1325 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1326 1a76625f 2018-10-22 stsp &tog_mutex);
1327 1a76625f 2018-10-22 stsp if (errcode)
1328 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1331 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1332 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1333 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1335 1a76625f 2018-10-22 stsp return (void *)err;
1338 1a76625f 2018-10-22 stsp static const struct got_error *
1339 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1341 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1342 1a76625f 2018-10-22 stsp int errcode;
1344 1a76625f 2018-10-22 stsp if (s->thread) {
1345 1a76625f 2018-10-22 stsp s->quit = 1;
1346 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1347 1a76625f 2018-10-22 stsp if (errcode)
1348 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1349 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1350 1a76625f 2018-10-22 stsp if (errcode)
1351 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1352 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1353 1a76625f 2018-10-22 stsp if (errcode)
1354 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1355 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1356 1a76625f 2018-10-22 stsp if (errcode)
1357 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1358 1a76625f 2018-10-22 stsp s->thread = NULL;
1361 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1362 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1363 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1365 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1366 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1367 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1370 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1371 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1372 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1375 9343a5fb 2018-06-23 stsp return err;
1378 9343a5fb 2018-06-23 stsp static const struct got_error *
1379 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1381 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1382 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1384 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1385 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1386 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1387 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1388 1a76625f 2018-10-22 stsp free(s->start_id);
1389 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1390 1a76625f 2018-10-22 stsp return err;
1393 1a76625f 2018-10-22 stsp static const struct got_error *
1394 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1395 23721109 2018-10-22 stsp struct got_repository *repo, const char *path, int check_disk)
1397 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1398 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1399 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1400 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1401 1a76625f 2018-10-22 stsp int errcode;
1403 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1404 ecb28ae0 2018-07-16 stsp if (err != NULL)
1405 ecb28ae0 2018-07-16 stsp goto done;
1407 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1408 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1409 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1411 fb2756b9 2018-08-04 stsp s->repo = repo;
1412 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1413 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1414 5036bf37 2018-09-24 stsp err = got_error_from_errno();
1415 5036bf37 2018-09-24 stsp goto done;
1418 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1419 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1420 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1422 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1424 1a76625f 2018-10-22 stsp goto done;
1425 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1426 1a76625f 2018-10-22 stsp 0, thread_repo);
1428 1a76625f 2018-10-22 stsp goto done;
1430 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1431 1a76625f 2018-10-22 stsp if (errcode) {
1432 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1433 1a76625f 2018-10-22 stsp goto done;
1436 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1437 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1438 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1439 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1440 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1441 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1442 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1443 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1444 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1445 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1446 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1449 1a76625f 2018-10-22 stsp close_log_view(view);
1450 ba4f502b 2018-08-04 stsp return err;
1453 e5a0f69f 2018-08-18 stsp static const struct got_error *
1454 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1456 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1458 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1459 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1460 2b380cc8 2018-10-24 stsp &s->thread_args);
1461 2b380cc8 2018-10-24 stsp if (errcode)
1462 2b380cc8 2018-10-24 stsp return got_error_set_errno(errcode);
1465 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1466 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1467 1a76625f 2018-10-22 stsp &s->commits, s->selected, view->nlines,
1468 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1471 e5a0f69f 2018-08-18 stsp static const struct got_error *
1472 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1473 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1475 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1476 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1477 5036bf37 2018-09-24 stsp char *parent_path;
1478 669b5ffa 2018-10-07 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
1479 669b5ffa 2018-10-07 stsp int begin_x = 0;
1481 e5a0f69f 2018-08-18 stsp switch (ch) {
1483 1a76625f 2018-10-22 stsp s->quit = 1;
1486 e5a0f69f 2018-08-18 stsp case KEY_UP:
1487 2b380cc8 2018-10-24 stsp if (s->first_displayed_entry == NULL)
1489 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1490 e5a0f69f 2018-08-18 stsp s->selected--;
1491 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1493 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1494 e5a0f69f 2018-08-18 stsp &s->commits);
1496 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1497 2b380cc8 2018-10-24 stsp if (s->first_displayed_entry == NULL)
1499 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1500 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1501 e5a0f69f 2018-08-18 stsp s->selected = 0;
1504 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1505 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1508 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1509 2b380cc8 2018-10-24 stsp if (s->first_displayed_entry == NULL)
1511 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1512 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1513 e5a0f69f 2018-08-18 stsp s->selected++;
1516 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1517 93e45b7c 2018-09-24 stsp &s->last_displayed_entry, &s->commits,
1518 1a76625f 2018-10-22 stsp &s->thread_args.log_complete,
1519 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1520 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1522 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1523 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1524 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1525 2b380cc8 2018-10-24 stsp if (first == NULL)
1527 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1528 93e45b7c 2018-09-24 stsp view->nlines, &s->last_displayed_entry,
1529 1a76625f 2018-10-22 stsp &s->commits, &s->thread_args.log_complete,
1530 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1531 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1532 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1533 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1534 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1535 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1536 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1537 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1539 e5a0f69f 2018-08-18 stsp err = NULL;
1542 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1543 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1544 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1545 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1546 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1548 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1549 e5a0f69f 2018-08-18 stsp case '\r':
1550 2b380cc8 2018-10-24 stsp if (s->selected_entry == NULL)
1552 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1553 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1554 669b5ffa 2018-10-07 stsp err = open_diff_view_for_commit(&diff_view, begin_x,
1555 15a087fe 2019-02-21 stsp s->selected_entry, &s->commits,
1556 15a087fe 2019-02-21 stsp &s->thread_args.log_complete,
1557 15a087fe 2019-02-21 stsp &s->thread_args.need_commits,
1558 15a087fe 2019-02-21 stsp &s->thread_args.commits_needed, s->repo);
1561 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1562 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1564 669b5ffa 2018-10-07 stsp return err;
1565 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
1566 669b5ffa 2018-10-07 stsp if (err) {
1567 669b5ffa 2018-10-07 stsp view_close(diff_view);
1570 55566b34 2018-11-05 stsp *focus_view = diff_view;
1571 55566b34 2018-11-05 stsp view->child_focussed = 1;
1573 669b5ffa 2018-10-07 stsp *new_view = diff_view;
1576 2b380cc8 2018-10-24 stsp if (s->selected_entry == NULL)
1578 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1579 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1580 669b5ffa 2018-10-07 stsp err = browse_commit(&tree_view, begin_x,
1581 0cf4efb1 2018-09-29 stsp s->selected_entry, s->repo);
1584 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1585 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1587 669b5ffa 2018-10-07 stsp return err;
1588 669b5ffa 2018-10-07 stsp err = view_set_child(view, tree_view);
1589 669b5ffa 2018-10-07 stsp if (err) {
1590 669b5ffa 2018-10-07 stsp view_close(tree_view);
1593 669b5ffa 2018-10-07 stsp *focus_view = tree_view;
1594 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1596 669b5ffa 2018-10-07 stsp *new_view = tree_view;
1598 5036bf37 2018-09-24 stsp case KEY_BACKSPACE:
1599 5036bf37 2018-09-24 stsp if (strcmp(s->in_repo_path, "/") == 0)
1601 5036bf37 2018-09-24 stsp parent_path = dirname(s->in_repo_path);
1602 5036bf37 2018-09-24 stsp if (parent_path && strcmp(parent_path, ".") != 0) {
1603 5036bf37 2018-09-24 stsp struct tog_view *lv;
1604 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1606 1a76625f 2018-10-22 stsp return err;
1607 0cf4efb1 2018-09-29 stsp lv = view_open(view->nlines, view->ncols,
1608 0cf4efb1 2018-09-29 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
1609 5036bf37 2018-09-24 stsp if (lv == NULL)
1610 5036bf37 2018-09-24 stsp return got_error_from_errno();
1611 5036bf37 2018-09-24 stsp err = open_log_view(lv, s->start_id, s->repo,
1612 23721109 2018-10-22 stsp parent_path, 0);
1614 1a76625f 2018-10-22 stsp return err;;
1615 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1616 669b5ffa 2018-10-07 stsp *new_view = lv;
1618 669b5ffa 2018-10-07 stsp view_set_child(view->parent, lv);
1619 34095730 2018-10-22 stsp *focus_view = lv;
1621 1a76625f 2018-10-22 stsp return NULL;
1628 80ddbec8 2018-04-29 stsp return err;
1631 4ed7e80c 2018-05-20 stsp static const struct got_error *
1632 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
1634 c2db6724 2019-01-04 stsp const struct got_error *error;
1636 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
1637 c2db6724 2019-01-04 stsp return got_error_from_errno();
1639 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
1640 c2db6724 2019-01-04 stsp return got_error_from_errno();
1642 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
1643 c2db6724 2019-01-04 stsp return got_error_from_errno();
1645 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
1646 c2db6724 2019-01-04 stsp if (error != NULL)
1647 c2db6724 2019-01-04 stsp return error;
1649 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
1650 c2db6724 2019-01-04 stsp return got_error_from_errno();
1652 c2db6724 2019-01-04 stsp return NULL;
1655 a915003a 2019-02-05 stsp static void
1656 a915003a 2019-02-05 stsp init_curses(void)
1658 a915003a 2019-02-05 stsp initscr();
1660 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
1663 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
1664 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
1665 a915003a 2019-02-05 stsp curs_set(0);
1666 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
1669 c2db6724 2019-01-04 stsp static const struct got_error *
1670 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1672 80ddbec8 2018-04-29 stsp const struct got_error *error;
1673 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1674 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1675 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1676 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1678 04cc582a 2018-08-01 stsp struct tog_view *view;
1680 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1681 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1682 c2db6724 2019-01-04 stsp NULL) == -1)
1683 80ddbec8 2018-04-29 stsp err(1, "pledge");
1686 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1687 80ddbec8 2018-04-29 stsp switch (ch) {
1689 80ddbec8 2018-04-29 stsp start_commit = optarg;
1692 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1693 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1694 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1698 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1702 80ddbec8 2018-04-29 stsp argc -= optind;
1703 80ddbec8 2018-04-29 stsp argv += optind;
1705 ecb28ae0 2018-07-16 stsp if (argc == 0)
1706 ecb28ae0 2018-07-16 stsp path = strdup("");
1707 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1708 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1710 80ddbec8 2018-04-29 stsp usage_log();
1711 ecb28ae0 2018-07-16 stsp if (path == NULL)
1712 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1714 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1715 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1716 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1717 ecb28ae0 2018-07-16 stsp goto done;
1719 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1720 b7165be3 2019-02-05 stsp struct got_worktree *worktree;
1721 b7165be3 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1722 b7165be3 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1723 b7165be3 2019-02-05 stsp goto done;
1724 b7165be3 2019-02-05 stsp if (worktree) {
1725 b7165be3 2019-02-05 stsp repo_path =
1726 b7165be3 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1727 b7165be3 2019-02-05 stsp got_worktree_close(worktree);
1729 b7165be3 2019-02-05 stsp repo_path = strdup(cwd);
1730 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1731 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1732 ecb28ae0 2018-07-16 stsp goto done;
1736 a915003a 2019-02-05 stsp init_curses();
1738 c2db6724 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
1739 c2db6724 2019-01-04 stsp if (error)
1740 c2db6724 2019-01-04 stsp goto done;
1742 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1743 80ddbec8 2018-04-29 stsp if (error != NULL)
1744 ecb28ae0 2018-07-16 stsp goto done;
1746 15a94983 2018-12-23 stsp if (start_commit == NULL)
1747 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1749 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&start_id, repo,
1750 15a94983 2018-12-23 stsp start_commit);
1751 80ddbec8 2018-04-29 stsp if (error != NULL)
1752 ecb28ae0 2018-07-16 stsp goto done;
1754 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1755 04cc582a 2018-08-01 stsp if (view == NULL) {
1756 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1757 04cc582a 2018-08-01 stsp goto done;
1759 23721109 2018-10-22 stsp error = open_log_view(view, start_id, repo, path, 1);
1760 ba4f502b 2018-08-04 stsp if (error)
1761 ba4f502b 2018-08-04 stsp goto done;
1762 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1764 ecb28ae0 2018-07-16 stsp free(repo_path);
1765 ecb28ae0 2018-07-16 stsp free(cwd);
1766 ecb28ae0 2018-07-16 stsp free(path);
1767 899d86c2 2018-05-10 stsp free(start_id);
1769 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1770 80ddbec8 2018-04-29 stsp return error;
1773 4ed7e80c 2018-05-20 stsp __dead static void
1774 9f7d7167 2018-04-29 stsp usage_diff(void)
1777 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1778 9f7d7167 2018-04-29 stsp getprogname());
1782 b304db33 2018-05-20 stsp static char *
1783 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1785 b304db33 2018-05-20 stsp char *line;
1786 b304db33 2018-05-20 stsp size_t linelen;
1787 b304db33 2018-05-20 stsp size_t lineno;
1788 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1790 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1792 b304db33 2018-05-20 stsp *len = linelen;
1793 b304db33 2018-05-20 stsp return line;
1796 4ed7e80c 2018-05-20 stsp static const struct got_error *
1797 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1798 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1799 a3404814 2018-09-02 stsp char * header)
1801 61e69b96 2018-05-20 stsp const struct got_error *err;
1802 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1803 b304db33 2018-05-20 stsp char *line;
1804 b304db33 2018-05-20 stsp size_t len;
1805 61e69b96 2018-05-20 stsp wchar_t *wline;
1806 e0b650dd 2018-05-20 stsp int width;
1808 26ed57b2 2018-05-19 stsp rewind(f);
1809 f7d12f7e 2018-08-01 stsp werase(view->window);
1811 a3404814 2018-09-02 stsp if (header) {
1812 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1813 a3404814 2018-09-02 stsp if (err) {
1814 a3404814 2018-09-02 stsp return err;
1817 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1818 a3404814 2018-09-02 stsp wstandout(view->window);
1819 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1820 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1821 a3404814 2018-09-02 stsp wstandend(view->window);
1822 a3404814 2018-09-02 stsp if (width < view->ncols)
1823 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1825 a3404814 2018-09-02 stsp if (max_lines <= 1)
1826 a3404814 2018-09-02 stsp return NULL;
1827 a3404814 2018-09-02 stsp max_lines--;
1831 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1832 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1833 26ed57b2 2018-05-19 stsp if (line == NULL) {
1837 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1838 26ed57b2 2018-05-19 stsp free(line);
1842 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1843 61e69b96 2018-05-20 stsp if (err) {
1844 61e69b96 2018-05-20 stsp free(line);
1845 61e69b96 2018-05-20 stsp return err;
1847 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1848 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1849 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1850 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1851 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1852 26ed57b2 2018-05-19 stsp free(line);
1853 2550e4c3 2018-07-13 stsp free(wline);
1854 2550e4c3 2018-07-13 stsp wline = NULL;
1856 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1858 1a57306a 2018-09-02 stsp view_vborder(view);
1860 26ed57b2 2018-05-19 stsp return NULL;
1863 abd2672a 2018-12-23 stsp static char *
1864 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
1866 abd2672a 2018-12-23 stsp char *p, *s = ctime_r(time, datebuf);
1867 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
1869 abd2672a 2018-12-23 stsp *p = '\0';
1873 4ed7e80c 2018-05-20 stsp static const struct got_error *
1874 15a94983 2018-12-23 stsp write_commit_info(struct got_object_id *commit_id, struct got_repository *repo,
1875 abd2672a 2018-12-23 stsp FILE *outfile)
1877 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
1878 abd2672a 2018-12-23 stsp char datebuf[26];
1879 15a94983 2018-12-23 stsp struct got_commit_object *commit;
1880 15a94983 2018-12-23 stsp char *id_str = NULL;
1881 45d799e2 2018-12-23 stsp time_t committer_time;
1882 45d799e2 2018-12-23 stsp const char *author, *committer;
1884 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1886 abd2672a 2018-12-23 stsp return err;
1888 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
1889 15a94983 2018-12-23 stsp if (err) {
1890 15a94983 2018-12-23 stsp err = got_error_from_errno();
1891 15a94983 2018-12-23 stsp goto done;
1894 c1124f18 2018-12-23 stsp if (fprintf(outfile, "commit %s\n", id_str) < 0) {
1895 abd2672a 2018-12-23 stsp err = got_error_from_errno();
1896 abd2672a 2018-12-23 stsp goto done;
1898 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
1899 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
1900 abd2672a 2018-12-23 stsp err = got_error_from_errno();
1901 abd2672a 2018-12-23 stsp goto done;
1903 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1904 abd2672a 2018-12-23 stsp if (fprintf(outfile, "date: %s UTC\n",
1905 45d799e2 2018-12-23 stsp get_datestr(&committer_time, datebuf)) < 0) {
1906 abd2672a 2018-12-23 stsp err = got_error_from_errno();
1907 abd2672a 2018-12-23 stsp goto done;
1909 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1910 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1911 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
1912 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
1913 abd2672a 2018-12-23 stsp err = got_error_from_errno();
1914 abd2672a 2018-12-23 stsp goto done;
1916 45d799e2 2018-12-23 stsp if (fprintf(outfile, "%s\n",
1917 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(commit)) < 0) {
1918 abd2672a 2018-12-23 stsp err = got_error_from_errno();
1919 abd2672a 2018-12-23 stsp goto done;
1922 abd2672a 2018-12-23 stsp free(id_str);
1923 15a94983 2018-12-23 stsp got_object_commit_close(commit);
1924 abd2672a 2018-12-23 stsp return err;
1927 abd2672a 2018-12-23 stsp static const struct got_error *
1928 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
1930 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
1931 48ae06ee 2018-10-18 stsp FILE *f = NULL;
1932 15a94983 2018-12-23 stsp int obj_type;
1934 511a516b 2018-05-19 stsp f = got_opentemp();
1935 48ae06ee 2018-10-18 stsp if (f == NULL) {
1936 48ae06ee 2018-10-18 stsp err = got_error_from_errno();
1937 48ae06ee 2018-10-18 stsp goto done;
1939 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
1940 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
1941 fb43ecf1 2019-02-11 stsp goto done;
1945 15a94983 2018-12-23 stsp if (s->id1)
1946 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
1948 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
1950 15a94983 2018-12-23 stsp goto done;
1952 15a94983 2018-12-23 stsp switch (obj_type) {
1953 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1954 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
1955 54156555 2018-12-24 stsp s->diff_context, s->repo, f);
1957 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1958 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
1959 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1961 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
1962 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1963 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
1964 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
1966 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
1969 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
1970 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
1971 15a087fe 2019-02-21 stsp write_commit_info(s->id2, s->repo, f);
1973 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
1974 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
1975 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
1976 15a087fe 2019-02-21 stsp write_commit_info(s->id2, s->repo, f);
1981 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
1983 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
1984 15a94983 2018-12-23 stsp s->diff_context, s->repo, f);
1988 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1992 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
1993 cbe7f848 2019-02-11 stsp err = got_error_from_errno();
1994 48ae06ee 2018-10-18 stsp return err;
1997 48ae06ee 2018-10-18 stsp static const struct got_error *
1998 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
1999 15a087fe 2019-02-21 stsp struct got_object_id *id2, struct commit_queue_entry *selected_entry,
2000 15a087fe 2019-02-21 stsp struct commit_queue *commits, int *log_complete,
2001 15a087fe 2019-02-21 stsp pthread_cond_t *need_commits, int *commits_needed,
2002 15a087fe 2019-02-21 stsp struct got_repository *repo)
2004 48ae06ee 2018-10-18 stsp const struct got_error *err;
2006 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2007 15a94983 2018-12-23 stsp int type1, type2;
2008 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2010 15a94983 2018-12-23 stsp return err;
2011 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2013 15a94983 2018-12-23 stsp return err;
2015 15a94983 2018-12-23 stsp if (type1 != type2)
2016 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2019 15a94983 2018-12-23 stsp if (id1) {
2020 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2021 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2022 48ae06ee 2018-10-18 stsp return got_error_from_errno();
2024 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2026 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2027 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2028 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2029 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2030 48ae06ee 2018-10-18 stsp return got_error_from_errno();
2032 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2033 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2034 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2035 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2036 15a087fe 2019-02-21 stsp view->state.diff.selected_entry = selected_entry;
2037 15a087fe 2019-02-21 stsp view->state.diff.commits = commits;
2038 15a087fe 2019-02-21 stsp view->state.diff.log_complete = log_complete;
2039 15a087fe 2019-02-21 stsp view->state.diff.need_commits = need_commits;
2040 15a087fe 2019-02-21 stsp view->state.diff.commits_needed = commits_needed;
2041 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2043 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2044 48ae06ee 2018-10-18 stsp if (err) {
2045 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2046 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2047 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2048 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2049 48ae06ee 2018-10-18 stsp return err;
2052 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2053 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2054 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2056 5dc9f4bc 2018-08-04 stsp return NULL;
2059 e5a0f69f 2018-08-18 stsp static const struct got_error *
2060 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2062 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2064 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2065 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2066 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2067 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2068 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2069 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2070 e5a0f69f 2018-08-18 stsp return err;
2073 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2074 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2076 a3404814 2018-09-02 stsp const struct got_error *err;
2077 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2078 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2080 a3404814 2018-09-02 stsp if (s->id1) {
2081 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2083 a3404814 2018-09-02 stsp return err;
2085 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2087 a3404814 2018-09-02 stsp return err;
2089 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2090 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2091 a3404814 2018-09-02 stsp err = got_error_from_errno();
2092 a3404814 2018-09-02 stsp free(id_str1);
2093 a3404814 2018-09-02 stsp free(id_str2);
2094 a3404814 2018-09-02 stsp return err;
2096 a3404814 2018-09-02 stsp free(id_str1);
2097 a3404814 2018-09-02 stsp free(id_str2);
2099 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2100 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2104 15a087fe 2019-02-21 stsp static const struct got_error *
2105 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2106 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2108 15a087fe 2019-02-21 stsp struct commit_queue_entry *pentry;
2110 15a087fe 2019-02-21 stsp free(s->id2);
2111 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2112 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2113 15a087fe 2019-02-21 stsp return got_error_from_errno();
2115 15a087fe 2019-02-21 stsp free(s->id1);
2116 15a087fe 2019-02-21 stsp s->id1 = NULL;
2117 15a087fe 2019-02-21 stsp pentry = TAILQ_NEXT(entry, entry);
2118 15a087fe 2019-02-21 stsp if (pentry) {
2119 15a087fe 2019-02-21 stsp s->id1 = got_object_id_dup(pentry->id);
2120 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2121 15a087fe 2019-02-21 stsp return got_error_from_errno();
2124 15a087fe 2019-02-21 stsp s->selected_entry = entry;
2125 15a087fe 2019-02-21 stsp return NULL;
2128 0cf4efb1 2018-09-29 stsp static const struct got_error *
2129 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2130 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2132 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2133 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2134 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry, *pentry;
2137 e5a0f69f 2018-08-18 stsp switch (ch) {
2139 e5a0f69f 2018-08-18 stsp case KEY_UP:
2140 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
2141 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2143 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2145 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
2146 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2147 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2150 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2151 e5a0f69f 2018-08-18 stsp if (!s->eof)
2152 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2154 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2157 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
2158 e5a0f69f 2018-08-18 stsp char *line;
2159 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
2160 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2161 e5a0f69f 2018-08-18 stsp if (line == NULL)
2166 48ae06ee 2018-10-18 stsp if (s->diff_context > 0) {
2167 48ae06ee 2018-10-18 stsp s->diff_context--;
2168 48ae06ee 2018-10-18 stsp err = create_diff(s);
2172 4a8520aa 2018-10-18 stsp if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2173 48ae06ee 2018-10-18 stsp s->diff_context++;
2174 48ae06ee 2018-10-18 stsp err = create_diff(s);
2179 15a087fe 2019-02-21 stsp if (s->commits == NULL)
2181 15a087fe 2019-02-21 stsp entry = TAILQ_PREV(s->selected_entry,
2182 15a087fe 2019-02-21 stsp commit_queue_head, entry);
2183 15a087fe 2019-02-21 stsp if (entry == NULL)
2186 15a087fe 2019-02-21 stsp err = set_selected_commit(s, entry);
2190 15a087fe 2019-02-21 stsp s->first_displayed_line = 1;
2191 15a087fe 2019-02-21 stsp s->last_displayed_line = view->nlines;
2193 15a087fe 2019-02-21 stsp err = create_diff(s);
2197 15a087fe 2019-02-21 stsp if (s->commits == NULL)
2199 15a087fe 2019-02-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2200 15a087fe 2019-02-21 stsp if (entry)
2201 15a087fe 2019-02-21 stsp pentry = TAILQ_NEXT(entry, entry);
2202 15a087fe 2019-02-21 stsp if (entry == NULL || pentry == NULL) {
2203 15a087fe 2019-02-21 stsp if (!*s->log_complete)
2204 15a087fe 2019-02-21 stsp *s->commits_needed = 2;
2206 15a087fe 2019-02-21 stsp while (entry == NULL || pentry == NULL) {
2207 15a087fe 2019-02-21 stsp int errcode;
2208 15a087fe 2019-02-21 stsp if (*s->log_complete)
2210 15a087fe 2019-02-21 stsp errcode = pthread_cond_signal(s->need_commits);
2211 15a087fe 2019-02-21 stsp if (errcode)
2212 15a087fe 2019-02-21 stsp return got_error_set_errno(errcode);
2213 15a087fe 2019-02-21 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2214 15a087fe 2019-02-21 stsp if (errcode)
2215 15a087fe 2019-02-21 stsp return got_error_set_errno(errcode);
2216 15a087fe 2019-02-21 stsp pthread_yield();
2217 15a087fe 2019-02-21 stsp errcode = pthread_mutex_lock(&tog_mutex);
2218 15a087fe 2019-02-21 stsp if (errcode)
2219 15a087fe 2019-02-21 stsp return got_error_set_errno(errcode);
2220 15a087fe 2019-02-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
2221 15a087fe 2019-02-21 stsp if (entry)
2222 15a087fe 2019-02-21 stsp pentry = TAILQ_NEXT(entry, entry);
2225 15a087fe 2019-02-21 stsp if (entry == NULL)
2228 15a087fe 2019-02-21 stsp err = set_selected_commit(s, entry);
2232 15a087fe 2019-02-21 stsp s->first_displayed_line = 1;
2233 15a087fe 2019-02-21 stsp s->last_displayed_line = view->nlines;
2235 15a087fe 2019-02-21 stsp err = create_diff(s);
2241 bcbd79e2 2018-08-19 stsp return err;
2244 4ed7e80c 2018-05-20 stsp static const struct got_error *
2245 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2247 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2248 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2249 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2250 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2251 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2253 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2255 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2256 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2257 eb6600df 2019-01-04 stsp NULL) == -1)
2258 26ed57b2 2018-05-19 stsp err(1, "pledge");
2261 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2262 26ed57b2 2018-05-19 stsp switch (ch) {
2265 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2269 26ed57b2 2018-05-19 stsp argc -= optind;
2270 26ed57b2 2018-05-19 stsp argv += optind;
2272 26ed57b2 2018-05-19 stsp if (argc == 0) {
2273 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2274 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2275 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2276 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2277 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2278 15a94983 2018-12-23 stsp id_str1 = argv[0];
2279 15a94983 2018-12-23 stsp id_str2 = argv[1];
2280 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2281 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2282 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2283 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2284 15a94983 2018-12-23 stsp id_str1 = argv[1];
2285 15a94983 2018-12-23 stsp id_str2 = argv[2];
2287 26ed57b2 2018-05-19 stsp usage_diff();
2289 a915003a 2019-02-05 stsp init_curses();
2291 eb6600df 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
2292 eb6600df 2019-01-04 stsp if (error)
2293 eb6600df 2019-01-04 stsp goto done;
2295 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
2296 26ed57b2 2018-05-19 stsp free(repo_path);
2297 26ed57b2 2018-05-19 stsp if (error)
2298 26ed57b2 2018-05-19 stsp goto done;
2300 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
2301 26ed57b2 2018-05-19 stsp if (error)
2302 26ed57b2 2018-05-19 stsp goto done;
2304 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
2305 26ed57b2 2018-05-19 stsp if (error)
2306 26ed57b2 2018-05-19 stsp goto done;
2308 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2309 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2310 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
2311 ea5e7bb5 2018-08-01 stsp goto done;
2313 15a087fe 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, NULL, NULL, NULL, NULL,
2315 5dc9f4bc 2018-08-04 stsp if (error)
2316 5dc9f4bc 2018-08-04 stsp goto done;
2317 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2319 26ed57b2 2018-05-19 stsp got_repo_close(repo);
2320 26ed57b2 2018-05-19 stsp return error;
2323 4ed7e80c 2018-05-20 stsp __dead static void
2324 9f7d7167 2018-04-29 stsp usage_blame(void)
2327 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2328 9f7d7167 2018-04-29 stsp getprogname());
2332 84451b3e 2018-07-10 stsp struct tog_blame_line {
2333 84451b3e 2018-07-10 stsp int annotated;
2334 84451b3e 2018-07-10 stsp struct got_object_id *id;
2337 4ed7e80c 2018-05-20 stsp static const struct got_error *
2338 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2339 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2340 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2341 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2343 84451b3e 2018-07-10 stsp const struct got_error *err;
2344 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2345 84451b3e 2018-07-10 stsp char *line;
2346 84451b3e 2018-07-10 stsp size_t len;
2347 84451b3e 2018-07-10 stsp wchar_t *wline;
2348 b700b5d6 2018-07-10 stsp int width, wlimit;
2349 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2350 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2351 ab089a2a 2018-07-12 stsp char *id_str;
2353 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2355 ab089a2a 2018-07-12 stsp return err;
2357 84451b3e 2018-07-10 stsp rewind(f);
2358 f7d12f7e 2018-08-01 stsp werase(view->window);
2360 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
2361 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
2362 ab089a2a 2018-07-12 stsp free(id_str);
2363 ab089a2a 2018-07-12 stsp return err;
2366 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2367 ab089a2a 2018-07-12 stsp free(line);
2368 2550e4c3 2018-07-13 stsp line = NULL;
2369 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2370 a3404814 2018-09-02 stsp wstandout(view->window);
2371 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2372 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2373 a3404814 2018-09-02 stsp wstandend(view->window);
2374 2550e4c3 2018-07-13 stsp free(wline);
2375 2550e4c3 2018-07-13 stsp wline = NULL;
2376 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2377 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2379 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2380 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2381 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
2382 ab089a2a 2018-07-12 stsp free(id_str);
2383 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
2385 ab089a2a 2018-07-12 stsp free(id_str);
2386 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2387 3f60a8ef 2018-07-10 stsp free(line);
2388 2550e4c3 2018-07-13 stsp line = NULL;
2390 3f60a8ef 2018-07-10 stsp return err;
2391 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2392 2550e4c3 2018-07-13 stsp free(wline);
2393 2550e4c3 2018-07-13 stsp wline = NULL;
2394 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2395 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2398 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2399 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2400 84451b3e 2018-07-10 stsp if (line == NULL) {
2404 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2405 84451b3e 2018-07-10 stsp free(line);
2409 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2410 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2411 84451b3e 2018-07-10 stsp if (err) {
2412 84451b3e 2018-07-10 stsp free(line);
2413 84451b3e 2018-07-10 stsp return err;
2416 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2417 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2419 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2420 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2421 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2422 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2423 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2424 84451b3e 2018-07-10 stsp char *id_str;
2425 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2426 84451b3e 2018-07-10 stsp if (err) {
2427 84451b3e 2018-07-10 stsp free(line);
2428 2550e4c3 2018-07-13 stsp free(wline);
2429 84451b3e 2018-07-10 stsp return err;
2431 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2432 84451b3e 2018-07-10 stsp free(id_str);
2433 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2435 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2436 ee41ec32 2018-07-10 stsp prev_id = NULL;
2439 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2440 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2441 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2444 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2445 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2446 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2447 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2448 84451b3e 2018-07-10 stsp free(line);
2449 2550e4c3 2018-07-13 stsp free(wline);
2450 2550e4c3 2018-07-13 stsp wline = NULL;
2452 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2454 1a57306a 2018-09-02 stsp view_vborder(view);
2456 84451b3e 2018-07-10 stsp return NULL;
2459 84451b3e 2018-07-10 stsp static const struct got_error *
2460 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2462 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2463 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2464 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2465 1a76625f 2018-10-22 stsp int errcode;
2467 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2468 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2469 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2471 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2472 1a76625f 2018-10-22 stsp if (errcode)
2473 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2475 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2476 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2477 d68a0a7d 2018-07-10 stsp goto done;
2480 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2481 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2483 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2484 d68a0a7d 2018-07-10 stsp if (line->annotated)
2485 d68a0a7d 2018-07-10 stsp goto done;
2487 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2488 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2489 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2490 84451b3e 2018-07-10 stsp goto done;
2492 84451b3e 2018-07-10 stsp line->annotated = 1;
2494 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2495 1a76625f 2018-10-22 stsp if (errcode)
2496 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2497 84451b3e 2018-07-10 stsp return err;
2500 84451b3e 2018-07-10 stsp static void *
2501 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2503 18430de3 2018-07-10 stsp const struct got_error *err;
2504 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
2505 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
2506 1a76625f 2018-10-22 stsp int errcode;
2508 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2509 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
2511 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2512 1a76625f 2018-10-22 stsp if (errcode)
2513 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
2515 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
2516 c9beca56 2018-07-22 stsp ta->repo = NULL;
2517 c9beca56 2018-07-22 stsp *ta->complete = 1;
2519 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2520 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2521 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2523 18430de3 2018-07-10 stsp return (void *)err;
2526 245d91c1 2018-07-12 stsp static struct got_object_id *
2527 15a94983 2018-12-23 stsp get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2528 15a94983 2018-12-23 stsp int selected_line)
2530 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2532 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2533 245d91c1 2018-07-12 stsp if (!line->annotated)
2534 245d91c1 2018-07-12 stsp return NULL;
2536 245d91c1 2018-07-12 stsp return line->id;
2539 b880a918 2018-07-10 stsp static const struct got_error *
2540 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2542 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2545 245d91c1 2018-07-12 stsp if (blame->thread) {
2546 1a76625f 2018-10-22 stsp int errcode;
2547 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2548 1a76625f 2018-10-22 stsp if (errcode)
2549 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2550 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
2551 1a76625f 2018-10-22 stsp if (errcode)
2552 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2553 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2554 1a76625f 2018-10-22 stsp if (errcode)
2555 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2556 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2557 245d91c1 2018-07-12 stsp err = NULL;
2558 245d91c1 2018-07-12 stsp blame->thread = NULL;
2560 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2561 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2562 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2564 245d91c1 2018-07-12 stsp if (blame->f) {
2565 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
2566 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
2567 245d91c1 2018-07-12 stsp blame->f = NULL;
2569 57670559 2018-12-23 stsp if (blame->lines) {
2570 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
2571 57670559 2018-12-23 stsp free(blame->lines[i].id);
2572 57670559 2018-12-23 stsp free(blame->lines);
2573 57670559 2018-12-23 stsp blame->lines = NULL;
2575 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2576 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2578 245d91c1 2018-07-12 stsp return err;
2581 245d91c1 2018-07-12 stsp static const struct got_error *
2582 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2583 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
2584 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
2585 245d91c1 2018-07-12 stsp struct got_repository *repo)
2587 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2588 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2589 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2590 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2591 15a94983 2018-12-23 stsp int obj_type;
2593 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2595 15a94983 2018-12-23 stsp return err;
2596 15a94983 2018-12-23 stsp if (obj_id == NULL)
2597 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
2599 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
2601 84451b3e 2018-07-10 stsp goto done;
2603 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2604 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2605 84451b3e 2018-07-10 stsp goto done;
2608 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2610 a70480e0 2018-06-23 stsp goto done;
2611 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2612 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2613 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2614 84451b3e 2018-07-10 stsp goto done;
2616 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2617 245d91c1 2018-07-12 stsp blame->f, blob);
2619 84451b3e 2018-07-10 stsp goto done;
2621 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2622 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2623 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2624 84451b3e 2018-07-10 stsp goto done;
2627 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2629 bd24772e 2018-07-11 stsp goto done;
2631 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2632 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2633 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2634 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2635 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2636 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2637 245d91c1 2018-07-12 stsp goto done;
2639 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2641 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2642 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2643 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2644 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2645 245d91c1 2018-07-12 stsp *blame_complete = 0;
2649 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2650 27d434c2 2018-09-15 stsp free(obj_id);
2652 245d91c1 2018-07-12 stsp stop_blame(blame);
2653 245d91c1 2018-07-12 stsp return err;
2656 245d91c1 2018-07-12 stsp static const struct got_error *
2657 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2658 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2660 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2661 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2663 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2665 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2667 7cbe629d 2018-08-04 stsp return err;
2669 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2670 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2671 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2672 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2673 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2674 fb2756b9 2018-08-04 stsp s->path = path;
2675 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2676 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2677 fb2756b9 2018-08-04 stsp s->repo = repo;
2678 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2679 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2681 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2682 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2683 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2685 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
2686 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2687 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2688 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2691 e5a0f69f 2018-08-18 stsp static const struct got_error *
2692 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2694 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2695 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2697 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2698 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2700 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2701 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2702 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2703 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2704 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2707 e5a0f69f 2018-08-18 stsp free(s->path);
2709 e5a0f69f 2018-08-18 stsp return err;
2712 7cbe629d 2018-08-04 stsp static const struct got_error *
2713 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2715 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2716 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2717 2b380cc8 2018-10-24 stsp int errcode;
2719 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
2720 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2721 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
2722 2b380cc8 2018-10-24 stsp if (errcode)
2723 2b380cc8 2018-10-24 stsp return got_error_set_errno(errcode);
2726 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2727 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2728 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2729 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2731 669b5ffa 2018-10-07 stsp view_vborder(view);
2732 e5a0f69f 2018-08-18 stsp return err;
2735 e5a0f69f 2018-08-18 stsp static const struct got_error *
2736 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2737 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2739 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2740 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2741 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2742 669b5ffa 2018-10-07 stsp int begin_x = 0;
2744 e5a0f69f 2018-08-18 stsp switch (ch) {
2746 e5a0f69f 2018-08-18 stsp s->done = 1;
2749 e5a0f69f 2018-08-18 stsp case KEY_UP:
2750 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2751 e5a0f69f 2018-08-18 stsp s->selected_line--;
2752 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2753 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2754 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2756 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2757 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2758 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2761 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2762 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2763 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2765 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2768 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2769 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2770 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2771 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2772 e5a0f69f 2018-08-18 stsp s->selected_line++;
2773 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2774 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2775 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2778 e5a0f69f 2018-08-18 stsp case 'p': {
2779 15a94983 2018-12-23 stsp struct got_object_id *id = NULL;
2780 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2781 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2782 15a94983 2018-12-23 stsp if (id == NULL)
2784 15a94983 2018-12-23 stsp if (ch == 'p') {
2785 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2786 15a94983 2018-12-23 stsp struct got_object_qid *pid;
2787 15a94983 2018-12-23 stsp struct got_object_id *blob_id = NULL;
2788 15a94983 2018-12-23 stsp int obj_type;
2789 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit,
2790 15a94983 2018-12-23 stsp s->repo, id);
2793 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
2794 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
2795 15a94983 2018-12-23 stsp if (pid == NULL) {
2796 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2799 15a94983 2018-12-23 stsp /* Check if path history ends here. */
2800 15a94983 2018-12-23 stsp err = got_object_id_by_path(&blob_id, s->repo,
2801 15a94983 2018-12-23 stsp pid->id, s->path);
2802 15a94983 2018-12-23 stsp if (err) {
2803 15a94983 2018-12-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
2804 15a94983 2018-12-23 stsp err = NULL;
2805 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2808 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo,
2810 15a94983 2018-12-23 stsp free(blob_id);
2811 15a94983 2018-12-23 stsp /* Can't blame non-blob type objects. */
2812 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2813 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2816 15a94983 2018-12-23 stsp err = got_object_qid_alloc(&s->blamed_commit,
2818 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2820 15a94983 2018-12-23 stsp if (got_object_id_cmp(id,
2821 15a94983 2018-12-23 stsp s->blamed_commit->id) == 0)
2823 15a94983 2018-12-23 stsp err = got_object_qid_alloc(&s->blamed_commit,
2828 e5a0f69f 2018-08-18 stsp s->done = 1;
2829 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2830 e5a0f69f 2018-08-18 stsp s->done = 0;
2831 e5a0f69f 2018-08-18 stsp if (thread_err)
2833 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2834 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2835 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2836 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2837 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2838 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2843 e5a0f69f 2018-08-18 stsp case 'B': {
2844 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2845 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2846 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2848 e5a0f69f 2018-08-18 stsp s->done = 1;
2849 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2850 e5a0f69f 2018-08-18 stsp s->done = 0;
2851 e5a0f69f 2018-08-18 stsp if (thread_err)
2853 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2854 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2855 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2856 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2857 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2858 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2859 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2860 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2865 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2866 15a94983 2018-12-23 stsp case '\r': {
2867 15a94983 2018-12-23 stsp struct got_object_id *id = NULL;
2868 15a94983 2018-12-23 stsp struct got_object_qid *pid;
2869 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2870 15a94983 2018-12-23 stsp id = get_selected_commit_id(s->blame.lines,
2871 15a94983 2018-12-23 stsp s->first_displayed_line, s->selected_line);
2872 3e9926ea 2019-01-04 stsp if (id == NULL)
2874 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, s->repo, id);
2877 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
2878 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
2879 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
2880 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
2881 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2882 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2883 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2884 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2887 15a94983 2018-12-23 stsp err = open_diff_view(diff_view, pid ? pid->id : NULL,
2888 15a087fe 2019-02-21 stsp id, NULL, NULL, NULL, NULL, NULL, s->repo);
2889 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2890 e5a0f69f 2018-08-18 stsp if (err) {
2891 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2894 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
2895 669b5ffa 2018-10-07 stsp err = view_close_child(view);
2898 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
2899 669b5ffa 2018-10-07 stsp if (err) {
2900 669b5ffa 2018-10-07 stsp view_close(diff_view);
2903 51c68690 2018-11-07 stsp *focus_view = diff_view;
2904 51c68690 2018-11-07 stsp view->child_focussed = 1;
2906 669b5ffa 2018-10-07 stsp *new_view = diff_view;
2911 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2913 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2914 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2915 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2916 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2919 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2920 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2921 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2922 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2924 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2925 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2926 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2928 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2929 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2930 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2931 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2937 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2940 a70480e0 2018-06-23 stsp static const struct got_error *
2941 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2943 a70480e0 2018-06-23 stsp const struct got_error *error;
2944 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2945 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
2946 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2947 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2948 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2950 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2952 a70480e0 2018-06-23 stsp #ifndef PROFILE
2953 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2954 8e94dd5b 2019-01-04 stsp NULL) == -1)
2955 a70480e0 2018-06-23 stsp err(1, "pledge");
2958 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2959 a70480e0 2018-06-23 stsp switch (ch) {
2961 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2964 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2965 69069811 2018-08-02 stsp if (repo_path == NULL)
2966 69069811 2018-08-02 stsp err(1, "-r option");
2970 a70480e0 2018-06-23 stsp /* NOTREACHED */
2974 a70480e0 2018-06-23 stsp argc -= optind;
2975 a70480e0 2018-06-23 stsp argv += optind;
2977 69069811 2018-08-02 stsp if (argc == 1)
2978 69069811 2018-08-02 stsp path = argv[0];
2980 a70480e0 2018-06-23 stsp usage_blame();
2982 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2983 69069811 2018-08-02 stsp if (cwd == NULL) {
2984 69069811 2018-08-02 stsp error = got_error_from_errno();
2985 69069811 2018-08-02 stsp goto done;
2987 69069811 2018-08-02 stsp if (repo_path == NULL) {
2988 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2989 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2990 69069811 2018-08-02 stsp goto done;
2992 eb41ed75 2019-02-05 stsp error = NULL;
2993 eb41ed75 2019-02-05 stsp if (worktree) {
2994 eb41ed75 2019-02-05 stsp repo_path =
2995 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2996 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
2997 eb41ed75 2019-02-05 stsp error = got_error_from_errno();
2998 eb41ed75 2019-02-05 stsp if (error)
2999 eb41ed75 2019-02-05 stsp goto done;
3001 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
3002 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
3003 eb41ed75 2019-02-05 stsp error = got_error_from_errno();
3004 eb41ed75 2019-02-05 stsp goto done;
3009 a915003a 2019-02-05 stsp init_curses();
3011 8e94dd5b 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
3012 8e94dd5b 2019-01-04 stsp if (error)
3013 8e94dd5b 2019-01-04 stsp goto done;
3015 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
3016 a70480e0 2018-06-23 stsp if (error != NULL)
3017 92205607 2019-01-04 stsp goto done;
3019 eb41ed75 2019-02-05 stsp if (worktree) {
3020 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3021 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3022 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3023 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3024 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3025 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3026 eb41ed75 2019-02-05 stsp path) == -1) {
3027 eb41ed75 2019-02-05 stsp error = got_error_from_errno();
3028 eb41ed75 2019-02-05 stsp goto done;
3030 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3033 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3035 eb41ed75 2019-02-05 stsp if (error)
3036 69069811 2018-08-02 stsp goto done;
3038 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
3039 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
3040 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3041 a70480e0 2018-06-23 stsp if (error != NULL)
3042 66b4983c 2018-06-23 stsp goto done;
3043 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3044 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
3046 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
3047 15a94983 2018-12-23 stsp commit_id_str);
3049 a19e88aa 2018-06-23 stsp if (error != NULL)
3050 a19e88aa 2018-06-23 stsp goto done;
3052 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3053 e1cd8fed 2018-08-01 stsp if (view == NULL) {
3054 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
3055 e1cd8fed 2018-08-01 stsp goto done;
3057 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
3058 7cbe629d 2018-08-04 stsp if (error)
3059 7cbe629d 2018-08-04 stsp goto done;
3060 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3062 69069811 2018-08-02 stsp free(repo_path);
3063 69069811 2018-08-02 stsp free(cwd);
3064 a70480e0 2018-06-23 stsp free(commit_id);
3065 eb41ed75 2019-02-05 stsp if (worktree)
3066 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
3068 a70480e0 2018-06-23 stsp got_repo_close(repo);
3069 a70480e0 2018-06-23 stsp return error;
3072 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3073 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
3074 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
3075 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
3076 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
3077 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
3078 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
3080 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3081 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
3082 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
3083 ffd1d5e5 2018-06-23 stsp int width, n;
3085 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
3087 f7d12f7e 2018-08-01 stsp werase(view->window);
3089 ffd1d5e5 2018-06-23 stsp if (limit == 0)
3090 ffd1d5e5 2018-06-23 stsp return NULL;
3092 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
3094 ffd1d5e5 2018-06-23 stsp return err;
3095 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3096 a3404814 2018-09-02 stsp wstandout(view->window);
3097 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3098 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3099 a3404814 2018-09-02 stsp wstandend(view->window);
3100 2550e4c3 2018-07-13 stsp free(wline);
3101 2550e4c3 2018-07-13 stsp wline = NULL;
3102 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
3103 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3104 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3105 ffd1d5e5 2018-06-23 stsp return NULL;
3106 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
3108 ce52c690 2018-06-23 stsp return err;
3109 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3110 2550e4c3 2018-07-13 stsp free(wline);
3111 2550e4c3 2018-07-13 stsp wline = NULL;
3112 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
3113 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3114 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3115 ffd1d5e5 2018-06-23 stsp return NULL;
3116 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3117 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
3118 a1eca9bb 2018-06-23 stsp return NULL;
3120 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3121 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
3122 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
3123 0cf4efb1 2018-09-29 stsp if (view->focussed)
3124 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3125 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
3127 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
3128 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
3129 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3130 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3131 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3132 ffd1d5e5 2018-06-23 stsp return NULL;
3136 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
3137 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3140 ffd1d5e5 2018-06-23 stsp while (te) {
3141 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
3143 1d13200f 2018-07-12 stsp if (show_ids) {
3144 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
3146 1d13200f 2018-07-12 stsp return got_error_from_errno();
3148 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3149 d8355ef1 2019-02-10 stsp te->name, S_ISDIR(te->mode) ? "/" :
3150 d8355ef1 2019-02-10 stsp ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3151 1d13200f 2018-07-12 stsp free(id_str);
3152 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3154 1d13200f 2018-07-12 stsp free(id_str);
3155 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
3156 ffd1d5e5 2018-06-23 stsp if (err) {
3157 ffd1d5e5 2018-06-23 stsp free(line);
3160 ffd1d5e5 2018-06-23 stsp if (n == selected) {
3161 0cf4efb1 2018-09-29 stsp if (view->focussed)
3162 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3163 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
3165 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3166 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
3167 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3168 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
3169 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3170 ffd1d5e5 2018-06-23 stsp free(line);
3171 2550e4c3 2018-07-13 stsp free(wline);
3172 2550e4c3 2018-07-13 stsp wline = NULL;
3174 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3175 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
3176 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3178 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3181 ffd1d5e5 2018-06-23 stsp return err;
3184 ffd1d5e5 2018-06-23 stsp static void
3185 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3186 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
3188 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
3191 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
3194 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3195 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
3196 ffd1d5e5 2018-06-23 stsp if (!isroot)
3197 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3201 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
3202 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
3203 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
3204 ffd1d5e5 2018-06-23 stsp prev = te;
3205 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3207 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
3208 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3210 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3211 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3214 768394f3 2019-01-24 stsp static int
3215 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3216 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
3217 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
3219 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
3220 ffd1d5e5 2018-06-23 stsp int n = 0;
3222 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
3223 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3225 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
3226 768394f3 2019-01-24 stsp last = last_displayed_entry;
3227 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
3228 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
3229 768394f3 2019-01-24 stsp if (last) {
3230 768394f3 2019-01-24 stsp *first_displayed_entry = next;
3231 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
3237 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3238 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
3239 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
3241 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
3242 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
3243 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
3245 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
3246 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
3248 ce52c690 2018-06-23 stsp len += strlen(te->name);
3250 ce52c690 2018-06-23 stsp *path = calloc(1, len);
3251 ffd1d5e5 2018-06-23 stsp if (path == NULL)
3252 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3254 ce52c690 2018-06-23 stsp (*path)[0] = '/';
3255 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
3256 d9765a41 2018-06-23 stsp while (pt) {
3257 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3258 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3259 cb2ebc8a 2018-06-23 stsp goto done;
3261 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
3262 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3263 cb2ebc8a 2018-06-23 stsp goto done;
3265 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3268 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3269 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3270 ce52c690 2018-06-23 stsp goto done;
3274 ce52c690 2018-06-23 stsp if (err) {
3275 ce52c690 2018-06-23 stsp free(*path);
3276 ce52c690 2018-06-23 stsp *path = NULL;
3278 ce52c690 2018-06-23 stsp return err;
3281 ce52c690 2018-06-23 stsp static const struct got_error *
3282 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3283 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3284 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
3286 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3287 ce52c690 2018-06-23 stsp char *path;
3288 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3290 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3292 ce52c690 2018-06-23 stsp return err;
3294 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3295 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3296 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3298 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
3299 e5a0f69f 2018-08-18 stsp if (err) {
3300 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3301 e5a0f69f 2018-08-18 stsp free(path);
3303 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3304 69efd4c4 2018-07-18 stsp return err;
3307 69efd4c4 2018-07-18 stsp static const struct got_error *
3308 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3309 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3310 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
3312 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3313 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3314 69efd4c4 2018-07-18 stsp char *path;
3316 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3317 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3318 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3320 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3322 69efd4c4 2018-07-18 stsp return err;
3324 23721109 2018-10-22 stsp err = open_log_view(log_view, commit_id, repo, path, 0);
3326 e5a0f69f 2018-08-18 stsp view_close(log_view);
3328 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3329 cb2ebc8a 2018-06-23 stsp free(path);
3330 cb2ebc8a 2018-06-23 stsp return err;
3333 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3334 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3335 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
3337 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3338 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3339 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3341 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3343 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3344 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3345 ffd1d5e5 2018-06-23 stsp goto done;
3347 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3348 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
3349 ffd1d5e5 2018-06-23 stsp goto done;
3352 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3353 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3354 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3355 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3356 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3357 6484ec90 2018-09-29 stsp err = got_error_from_errno();
3358 6484ec90 2018-09-29 stsp goto done;
3360 fb2756b9 2018-08-04 stsp s->repo = repo;
3362 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
3363 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
3364 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
3366 ad80ab7b 2018-08-04 stsp free(commit_id_str);
3367 6484ec90 2018-09-29 stsp if (err) {
3368 fb2756b9 2018-08-04 stsp free(s->tree_label);
3369 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3371 ad80ab7b 2018-08-04 stsp return err;
3374 e5a0f69f 2018-08-18 stsp static const struct got_error *
3375 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
3377 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3379 fb2756b9 2018-08-04 stsp free(s->tree_label);
3380 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3381 6484ec90 2018-09-29 stsp free(s->commit_id);
3382 6484ec90 2018-09-29 stsp s->commit_id = NULL;
3383 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
3384 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
3385 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
3386 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
3387 ad80ab7b 2018-08-04 stsp free(parent);
3390 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
3391 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
3392 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
3394 e5a0f69f 2018-08-18 stsp return NULL;
3397 ad80ab7b 2018-08-04 stsp static const struct got_error *
3398 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
3400 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
3401 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3402 e5a0f69f 2018-08-18 stsp char *parent_path;
3404 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
3406 e5a0f69f 2018-08-18 stsp return err;
3408 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
3409 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
3410 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3411 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
3412 e5a0f69f 2018-08-18 stsp free(parent_path);
3414 669b5ffa 2018-10-07 stsp view_vborder(view);
3415 e5a0f69f 2018-08-18 stsp return err;
3418 e5a0f69f 2018-08-18 stsp static const struct got_error *
3419 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3420 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3422 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3423 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
3424 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
3425 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
3427 e5a0f69f 2018-08-18 stsp switch (ch) {
3429 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
3432 669b5ffa 2018-10-07 stsp if (!s->selected_entry)
3434 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
3435 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
3436 669b5ffa 2018-10-07 stsp err = log_tree_entry(&log_view, begin_x,
3437 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
3438 669b5ffa 2018-10-07 stsp s->commit_id, s->repo);
3439 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3440 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3442 669b5ffa 2018-10-07 stsp return err;
3443 669b5ffa 2018-10-07 stsp err = view_set_child(view, log_view);
3444 669b5ffa 2018-10-07 stsp if (err) {
3445 669b5ffa 2018-10-07 stsp view_close(log_view);
3448 669b5ffa 2018-10-07 stsp *focus_view = log_view;
3449 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3451 669b5ffa 2018-10-07 stsp *new_view = log_view;
3454 e5a0f69f 2018-08-18 stsp case KEY_UP:
3455 bf979164 2019-01-24 stsp if (s->selected > 0) {
3456 e5a0f69f 2018-08-18 stsp s->selected--;
3457 bf979164 2019-01-24 stsp if (s->selected == 0)
3460 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3462 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
3463 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
3465 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
3466 67cc7991 2019-01-24 stsp tree_scroll_up(&s->first_displayed_entry,
3467 67cc7991 2019-01-24 stsp MAX(0, view->nlines - 4 - s->selected), s->entries,
3468 67cc7991 2019-01-24 stsp s->tree == s->root);
3469 8a0ead39 2018-11-03 stsp s->selected = 0;
3470 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
3471 67cc7991 2019-01-24 stsp s->first_displayed_entry && s->tree != s->root)
3472 67cc7991 2019-01-24 stsp s->first_displayed_entry = NULL;
3475 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
3476 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
3477 e5a0f69f 2018-08-18 stsp s->selected++;
3480 768394f3 2019-01-24 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3481 768394f3 2019-01-24 stsp == NULL) {
3482 768394f3 2019-01-24 stsp /* can't scroll any further */
3485 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
3486 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
3488 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
3489 768394f3 2019-01-24 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3490 768394f3 2019-01-24 stsp == NULL) {
3491 768394f3 2019-01-24 stsp /* can't scroll any further; move cursor down */
3492 768394f3 2019-01-24 stsp if (s->selected < s->ndisplayed - 1)
3493 768394f3 2019-01-24 stsp s->selected = s->ndisplayed - 1;
3496 768394f3 2019-01-24 stsp nscrolled = tree_scroll_down(&s->first_displayed_entry,
3497 768394f3 2019-01-24 stsp view->nlines, s->last_displayed_entry, s->entries);
3498 768394f3 2019-01-24 stsp if (nscrolled < view->nlines) {
3499 768394f3 2019-01-24 stsp int ndisplayed = 0;
3500 768394f3 2019-01-24 stsp struct got_tree_entry *te;
3501 768394f3 2019-01-24 stsp te = s->first_displayed_entry;
3503 768394f3 2019-01-24 stsp ndisplayed++;
3504 768394f3 2019-01-24 stsp te = SIMPLEQ_NEXT(te, entry);
3505 768394f3 2019-01-24 stsp } while (te);
3506 768394f3 2019-01-24 stsp s->selected = ndisplayed - 1;
3509 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
3510 e5a0f69f 2018-08-18 stsp case '\r':
3511 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
3512 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3513 7837eeac 2018-09-24 stsp case KEY_BACKSPACE:
3514 e5a0f69f 2018-08-18 stsp /* user selected '..' */
3515 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
3517 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
3518 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
3520 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
3521 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
3522 e5a0f69f 2018-08-18 stsp s->entries =
3523 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3524 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
3525 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
3526 e5a0f69f 2018-08-18 stsp s->selected_entry =
3527 e5a0f69f 2018-08-18 stsp parent->selected_entry;
3528 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
3529 e5a0f69f 2018-08-18 stsp free(parent);
3530 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
3531 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3532 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
3533 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
3534 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
3537 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
3538 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
3539 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
3542 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
3543 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
3544 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
3545 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
3546 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
3547 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3548 e5a0f69f 2018-08-18 stsp s->tree = child;
3549 e5a0f69f 2018-08-18 stsp s->entries =
3550 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3551 e5a0f69f 2018-08-18 stsp s->selected = 0;
3552 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3553 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
3554 669b5ffa 2018-10-07 stsp struct tog_view *blame_view;
3555 669b5ffa 2018-10-07 stsp int begin_x = view_is_parent_view(view) ?
3556 669b5ffa 2018-10-07 stsp view_split_begin_x(view->begin_x) : 0;
3558 669b5ffa 2018-10-07 stsp err = blame_tree_entry(&blame_view, begin_x,
3559 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents, s->commit_id,
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, blame_view);
3568 669b5ffa 2018-10-07 stsp if (err) {
3569 669b5ffa 2018-10-07 stsp view_close(blame_view);
3572 669b5ffa 2018-10-07 stsp *focus_view = blame_view;
3573 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3575 669b5ffa 2018-10-07 stsp *new_view = blame_view;
3578 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3579 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3580 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3586 ffd1d5e5 2018-06-23 stsp return err;
3589 ffd1d5e5 2018-06-23 stsp __dead static void
3590 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3593 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3594 ffd1d5e5 2018-06-23 stsp getprogname());
3598 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3599 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3601 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3602 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3603 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3604 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3605 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3606 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3607 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3609 5221c383 2018-08-01 stsp struct tog_view *view;
3611 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3612 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3613 d188b9a6 2019-01-04 stsp NULL) == -1)
3614 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3617 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3618 ffd1d5e5 2018-06-23 stsp switch (ch) {
3620 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3624 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3628 ffd1d5e5 2018-06-23 stsp argc -= optind;
3629 ffd1d5e5 2018-06-23 stsp argv += optind;
3631 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3632 52185f70 2019-02-05 stsp struct got_worktree *worktree;
3633 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
3634 52185f70 2019-02-05 stsp if (cwd == NULL)
3635 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3636 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3637 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3638 52185f70 2019-02-05 stsp goto done;
3639 52185f70 2019-02-05 stsp if (worktree) {
3640 52185f70 2019-02-05 stsp free(cwd);
3641 52185f70 2019-02-05 stsp repo_path =
3642 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3643 52185f70 2019-02-05 stsp got_worktree_close(worktree);
3645 52185f70 2019-02-05 stsp repo_path = cwd;
3646 52185f70 2019-02-05 stsp if (repo_path == NULL) {
3647 52185f70 2019-02-05 stsp error = got_error_from_errno();
3648 52185f70 2019-02-05 stsp goto done;
3650 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3651 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3652 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3653 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3655 ffd1d5e5 2018-06-23 stsp usage_log();
3657 a915003a 2019-02-05 stsp init_curses();
3659 d188b9a6 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
3660 d188b9a6 2019-01-04 stsp if (error)
3661 52185f70 2019-02-05 stsp goto done;
3663 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3664 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3665 52185f70 2019-02-05 stsp goto done;
3667 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
3668 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3670 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
3671 15a94983 2018-12-23 stsp commit_id_arg);
3672 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3673 ffd1d5e5 2018-06-23 stsp goto done;
3675 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3676 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3677 ffd1d5e5 2018-06-23 stsp goto done;
3679 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
3680 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
3681 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3682 ffd1d5e5 2018-06-23 stsp goto done;
3684 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3685 5221c383 2018-08-01 stsp if (view == NULL) {
3686 5221c383 2018-08-01 stsp error = got_error_from_errno();
3687 5221c383 2018-08-01 stsp goto done;
3689 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3690 ad80ab7b 2018-08-04 stsp if (error)
3691 ad80ab7b 2018-08-04 stsp goto done;
3692 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3694 52185f70 2019-02-05 stsp free(repo_path);
3695 ffd1d5e5 2018-06-23 stsp free(commit_id);
3696 ffd1d5e5 2018-06-23 stsp if (commit)
3697 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3699 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3701 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3702 ffd1d5e5 2018-06-23 stsp return error;
3705 4ed7e80c 2018-05-20 stsp __dead static void
3706 9f7d7167 2018-04-29 stsp usage(void)
3710 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3711 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3712 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3713 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3714 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3719 c2301be8 2018-04-30 stsp static char **
3720 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3722 c2301be8 2018-04-30 stsp char **argv;
3723 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3725 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3726 c2301be8 2018-04-30 stsp if (argv == NULL)
3727 c2301be8 2018-04-30 stsp err(1, "calloc");
3728 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3729 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3730 c2301be8 2018-04-30 stsp err(1, "calloc");
3731 c2301be8 2018-04-30 stsp if (arg1) {
3732 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3733 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3734 c2301be8 2018-04-30 stsp err(1, "calloc");
3737 c2301be8 2018-04-30 stsp return argv;
3741 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3743 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3744 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3745 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3746 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3748 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
3750 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3751 9f7d7167 2018-04-29 stsp switch (ch) {
3753 9f7d7167 2018-04-29 stsp hflag = 1;
3757 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3761 9f7d7167 2018-04-29 stsp argc -= optind;
3762 9f7d7167 2018-04-29 stsp argv += optind;
3763 9f7d7167 2018-04-29 stsp optind = 0;
3764 c2301be8 2018-04-30 stsp optreset = 1;
3766 c2301be8 2018-04-30 stsp if (argc == 0) {
3767 f29d3e89 2018-06-23 stsp if (hflag)
3769 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3770 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3771 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3776 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3777 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3778 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3779 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3780 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3781 9f7d7167 2018-04-29 stsp if (hflag)
3782 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3786 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3787 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3788 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3789 c2301be8 2018-04-30 stsp if (repo_path) {
3790 c2301be8 2018-04-30 stsp struct got_repository *repo;
3791 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3792 c2301be8 2018-04-30 stsp if (error == NULL)
3793 c2301be8 2018-04-30 stsp got_repo_close(repo);
3795 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3796 c2301be8 2018-04-30 stsp if (error) {
3797 f29d3e89 2018-06-23 stsp if (hflag) {
3798 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3799 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3803 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3804 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3805 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3806 ad7de8d9 2018-04-30 stsp free(repo_path);
3809 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3810 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3812 c2301be8 2018-04-30 stsp free(repo_path);
3816 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3817 9f7d7167 2018-04-29 stsp if (error)
3818 9f7d7167 2018-04-29 stsp goto done;
3821 c2301be8 2018-04-30 stsp free(cmd_argv);
3822 9f7d7167 2018-04-29 stsp if (error)
3823 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);