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 ad80ab7b 2018-08-04 stsp struct tog_diff_view_state {
102 a3404814 2018-09-02 stsp struct got_object_id *id1, *id2;
104 ad80ab7b 2018-08-04 stsp int first_displayed_line;
105 ad80ab7b 2018-08-04 stsp int last_displayed_line;
107 48ae06ee 2018-10-18 stsp int diff_context;
108 48ae06ee 2018-10-18 stsp struct got_repository *repo;
111 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
112 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
113 ba4f502b 2018-08-04 stsp struct got_object_id *id;
114 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
117 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
118 ba4f502b 2018-08-04 stsp struct commit_queue {
119 ba4f502b 2018-08-04 stsp int ncommits;
120 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
123 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
125 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
126 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
127 1a76625f 2018-10-22 stsp int commits_needed;
128 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
129 1a76625f 2018-10-22 stsp struct commit_queue *commits;
130 1a76625f 2018-10-22 stsp const char *in_repo_path;
131 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
132 1a76625f 2018-10-22 stsp struct got_repository *repo;
133 1a76625f 2018-10-22 stsp int log_complete;
134 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
135 1a76625f 2018-10-22 stsp struct tog_view *view;
136 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
137 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
140 1a76625f 2018-10-22 stsp struct tog_log_view_state {
141 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
142 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
143 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
144 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
145 b01e7d3b 2018-08-04 stsp int selected;
146 b01e7d3b 2018-08-04 stsp char *in_repo_path;
147 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
148 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
149 1a76625f 2018-10-22 stsp sig_atomic_t quit;
150 1a76625f 2018-10-22 stsp pthread_t thread;
151 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
154 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
155 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
156 e9424729 2018-08-04 stsp int nlines;
158 e9424729 2018-08-04 stsp struct tog_view *view;
159 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
163 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
164 e9424729 2018-08-04 stsp const char *path;
165 e9424729 2018-08-04 stsp struct got_repository *repo;
166 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
167 e9424729 2018-08-04 stsp int *complete;
170 e9424729 2018-08-04 stsp struct tog_blame {
172 e9424729 2018-08-04 stsp size_t filesize;
173 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
174 6fcac457 2018-11-19 stsp int nlines;
175 e9424729 2018-08-04 stsp pthread_t thread;
176 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
177 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
178 e9424729 2018-08-04 stsp const char *path;
181 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
182 7cbe629d 2018-08-04 stsp int first_displayed_line;
183 7cbe629d 2018-08-04 stsp int last_displayed_line;
184 7cbe629d 2018-08-04 stsp int selected_line;
185 7cbe629d 2018-08-04 stsp int blame_complete;
188 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
189 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
190 e5a0f69f 2018-08-18 stsp char *path;
191 7cbe629d 2018-08-04 stsp struct got_repository *repo;
192 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
193 e9424729 2018-08-04 stsp struct tog_blame blame;
196 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
197 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
198 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
199 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
200 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
201 ad80ab7b 2018-08-04 stsp int selected;
204 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
206 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
207 ad80ab7b 2018-08-04 stsp char *tree_label;
208 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
209 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
210 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
211 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
212 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
213 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
214 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
215 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
216 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
217 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
221 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
223 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
224 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
225 669b5ffa 2018-10-07 stsp * there is enough screen estate.
227 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
228 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
230 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
231 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
232 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
234 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
235 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
237 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
239 cc3c9aac 2018-08-01 stsp struct tog_view {
240 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
241 26ed57b2 2018-05-19 stsp WINDOW *window;
242 26ed57b2 2018-05-19 stsp PANEL *panel;
243 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
244 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
245 1004088d 2018-09-29 stsp int focussed;
246 669b5ffa 2018-10-07 stsp struct tog_view *parent;
247 669b5ffa 2018-10-07 stsp struct tog_view *child;
248 669b5ffa 2018-10-07 stsp int child_focussed;
250 5dc9f4bc 2018-08-04 stsp /* type-specific state */
251 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
253 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
254 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
255 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
256 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
259 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
260 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
261 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
262 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
265 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
266 15a94983 2018-12-23 stsp struct got_object_id *, struct got_object_id *, struct got_repository *);
267 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
268 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
269 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
270 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
272 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
273 23721109 2018-10-22 stsp struct got_object_id *, struct got_repository *, const char *, int);
274 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
275 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
276 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
277 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
279 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
280 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
281 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
282 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
283 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
284 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
286 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
287 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
288 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
289 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
290 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
291 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
293 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
295 25791caa 2018-10-24 stsp static void
296 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
298 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
301 e5a0f69f 2018-08-18 stsp static const struct got_error *
302 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
304 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
306 669b5ffa 2018-10-07 stsp if (view->child) {
307 669b5ffa 2018-10-07 stsp view_close(view->child);
308 669b5ffa 2018-10-07 stsp view->child = NULL;
310 e5a0f69f 2018-08-18 stsp if (view->close)
311 e5a0f69f 2018-08-18 stsp err = view->close(view);
312 ea5e7bb5 2018-08-01 stsp if (view->panel)
313 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
314 ea5e7bb5 2018-08-01 stsp if (view->window)
315 ea5e7bb5 2018-08-01 stsp delwin(view->window);
316 ea5e7bb5 2018-08-01 stsp free(view);
317 e5a0f69f 2018-08-18 stsp return err;
320 ea5e7bb5 2018-08-01 stsp static struct tog_view *
321 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
322 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
324 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
326 ea5e7bb5 2018-08-01 stsp if (view == NULL)
327 ea5e7bb5 2018-08-01 stsp return NULL;
329 d6b05b5b 2018-08-04 stsp view->type = type;
330 f7d12f7e 2018-08-01 stsp view->lines = LINES;
331 f7d12f7e 2018-08-01 stsp view->cols = COLS;
332 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
333 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
334 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
335 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
336 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
337 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
338 96a765a8 2018-08-04 stsp view_close(view);
339 ea5e7bb5 2018-08-01 stsp return NULL;
341 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
342 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
343 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
344 96a765a8 2018-08-04 stsp view_close(view);
345 ea5e7bb5 2018-08-01 stsp return NULL;
348 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
349 ea5e7bb5 2018-08-01 stsp return view;
353 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
355 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
357 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
360 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
362 5c60c32a 2018-10-18 stsp static const struct got_error *
363 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
365 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
367 5c60c32a 2018-10-18 stsp view->begin_y = 0;
368 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
369 5c60c32a 2018-10-18 stsp view->nlines = LINES;
370 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
371 5c60c32a 2018-10-18 stsp view->lines = LINES;
372 5c60c32a 2018-10-18 stsp view->cols = COLS;
373 5c60c32a 2018-10-18 stsp err = view_resize(view);
375 5c60c32a 2018-10-18 stsp return err;
377 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
378 5c60c32a 2018-10-18 stsp return got_error_from_errno();
380 5c60c32a 2018-10-18 stsp return NULL;
383 5c60c32a 2018-10-18 stsp static const struct got_error *
384 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
386 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
388 5c60c32a 2018-10-18 stsp view->begin_x = 0;
389 5c60c32a 2018-10-18 stsp view->begin_y = 0;
390 5c60c32a 2018-10-18 stsp view->nlines = LINES;
391 5c60c32a 2018-10-18 stsp view->ncols = COLS;
392 5c60c32a 2018-10-18 stsp view->lines = LINES;
393 5c60c32a 2018-10-18 stsp view->cols = COLS;
394 5c60c32a 2018-10-18 stsp err = view_resize(view);
396 5c60c32a 2018-10-18 stsp return err;
398 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
399 5c60c32a 2018-10-18 stsp return got_error_from_errno();
401 5c60c32a 2018-10-18 stsp return NULL;
405 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
407 5c60c32a 2018-10-18 stsp return view->parent == NULL;
410 4d8c2215 2018-08-19 stsp static const struct got_error *
411 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
413 f7d12f7e 2018-08-01 stsp int nlines, ncols;
415 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
416 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
418 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
420 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
421 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
423 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
425 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
426 0cf4efb1 2018-09-29 stsp return got_error_from_errno();
427 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
428 a6d7eb8d 2018-10-24 stsp return got_error_from_errno();
429 25791caa 2018-10-24 stsp wclear(view->window);
431 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
432 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
433 0cf4efb1 2018-09-29 stsp view->lines = LINES;
434 0cf4efb1 2018-09-29 stsp view->cols = COLS;
436 6e3e5d9c 2018-10-18 stsp if (view->child) {
437 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
438 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
439 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
440 5c60c32a 2018-10-18 stsp if (view->child->focussed)
441 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
443 5c60c32a 2018-10-18 stsp show_panel(view->panel);
445 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
446 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
450 5c60c32a 2018-10-18 stsp return NULL;
453 669b5ffa 2018-10-07 stsp static const struct got_error *
454 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
456 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
458 669b5ffa 2018-10-07 stsp if (view->child == NULL)
459 669b5ffa 2018-10-07 stsp return NULL;
461 669b5ffa 2018-10-07 stsp err = view_close(view->child);
462 669b5ffa 2018-10-07 stsp view->child = NULL;
463 669b5ffa 2018-10-07 stsp return err;
466 669b5ffa 2018-10-07 stsp static const struct got_error *
467 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
469 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
471 669b5ffa 2018-10-07 stsp view->child = child;
472 669b5ffa 2018-10-07 stsp child->parent = view;
473 669b5ffa 2018-10-07 stsp return err;
477 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
479 669b5ffa 2018-10-07 stsp return !view_is_parent_view(view) && view->begin_x > 0;
482 25791caa 2018-10-24 stsp static void
483 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
485 25791caa 2018-10-24 stsp int cols, lines;
486 25791caa 2018-10-24 stsp struct winsize size;
488 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
489 25791caa 2018-10-24 stsp cols = 80; /* Default */
490 25791caa 2018-10-24 stsp lines = 24;
492 25791caa 2018-10-24 stsp cols = size.ws_col;
493 25791caa 2018-10-24 stsp lines = size.ws_row;
495 25791caa 2018-10-24 stsp resize_term(lines, cols);
498 0cf4efb1 2018-09-29 stsp static const struct got_error *
499 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
500 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
501 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
503 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
504 669b5ffa 2018-10-07 stsp struct tog_view *v;
505 1a76625f 2018-10-22 stsp int ch, errcode;
507 e5a0f69f 2018-08-18 stsp *new = NULL;
508 e5a0f69f 2018-08-18 stsp *dead = NULL;
509 0cf4efb1 2018-09-29 stsp *focus = NULL;
511 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
512 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
513 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
514 1a76625f 2018-10-22 stsp if (errcode)
515 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
516 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
517 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
518 1a76625f 2018-10-22 stsp if (errcode)
519 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
520 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
522 25791caa 2018-10-24 stsp if (tog_sigwinch_received) {
523 25791caa 2018-10-24 stsp tog_resizeterm();
524 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
525 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
526 25791caa 2018-10-24 stsp err = view_resize(v);
528 25791caa 2018-10-24 stsp return err;
529 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
531 25791caa 2018-10-24 stsp return err;
535 e5a0f69f 2018-08-18 stsp switch (ch) {
539 669b5ffa 2018-10-07 stsp if (view->child) {
540 669b5ffa 2018-10-07 stsp *focus = view->child;
541 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
542 669b5ffa 2018-10-07 stsp } else if (view->parent) {
543 669b5ffa 2018-10-07 stsp *focus = view->parent;
544 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 0;
548 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
549 e5a0f69f 2018-08-18 stsp *dead = view;
555 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
556 669b5ffa 2018-10-07 stsp if (view->child == NULL)
558 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view->child)) {
559 669b5ffa 2018-10-07 stsp *focus = view->child;
560 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
561 669b5ffa 2018-10-07 stsp err = view_fullscreen(view->child);
563 669b5ffa 2018-10-07 stsp err = view_splitscreen(view->child);
566 669b5ffa 2018-10-07 stsp err = view->child->input(new, dead, focus,
567 669b5ffa 2018-10-07 stsp view->child, KEY_RESIZE);
569 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view)) {
570 669b5ffa 2018-10-07 stsp *focus = view;
571 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 1;
572 669b5ffa 2018-10-07 stsp err = view_fullscreen(view);
574 669b5ffa 2018-10-07 stsp err = view_splitscreen(view);
578 669b5ffa 2018-10-07 stsp err = view->input(new, dead, focus, view,
579 669b5ffa 2018-10-07 stsp KEY_RESIZE);
582 0cf4efb1 2018-09-29 stsp case KEY_RESIZE:
585 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
589 e5a0f69f 2018-08-18 stsp return err;
593 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
595 0cf4efb1 2018-09-29 stsp PANEL *panel;
596 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
598 669b5ffa 2018-10-07 stsp if (view->parent)
599 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
601 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
602 0cf4efb1 2018-09-29 stsp if (panel == NULL)
605 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
606 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
607 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
611 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
613 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
614 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
616 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
618 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
621 669b5ffa 2018-10-07 stsp return view->focussed;
624 bcbd79e2 2018-08-19 stsp static const struct got_error *
625 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
627 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
628 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
629 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
630 fd823528 2018-10-22 stsp int fast_refresh = 10;
631 1a76625f 2018-10-22 stsp int done = 0, errcode;
633 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
634 1a76625f 2018-10-22 stsp if (errcode)
635 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
637 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
638 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
640 a81bf10d 2018-09-29 stsp main_view = view;
641 1004088d 2018-09-29 stsp view->focussed = 1;
642 878940b7 2018-09-29 stsp err = view->show(view);
644 0cf4efb1 2018-09-29 stsp return err;
645 0cf4efb1 2018-09-29 stsp update_panels();
646 0cf4efb1 2018-09-29 stsp doupdate();
647 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
648 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
649 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
650 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
652 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
653 e4197bf9 2018-08-18 stsp view, &views);
656 e5a0f69f 2018-08-18 stsp if (dead_view) {
657 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
659 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
660 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
661 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
662 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
663 669b5ffa 2018-10-07 stsp prev = view->parent;
665 669b5ffa 2018-10-07 stsp if (dead_view->parent)
666 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
668 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
670 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
671 a81bf10d 2018-09-29 stsp if (err || dead_view == main_view)
674 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
675 0cf4efb1 2018-09-29 stsp if (focus_view)
676 0cf4efb1 2018-09-29 stsp view = focus_view;
677 669b5ffa 2018-10-07 stsp else if (prev)
678 669b5ffa 2018-10-07 stsp view = prev;
679 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
680 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
681 0cf4efb1 2018-09-29 stsp tog_view_list_head);
683 0cf4efb1 2018-09-29 stsp view = NULL;
684 669b5ffa 2018-10-07 stsp if (view) {
685 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
686 669b5ffa 2018-10-07 stsp focus_view = view->child;
688 669b5ffa 2018-10-07 stsp focus_view = view;
692 bcbd79e2 2018-08-19 stsp if (new_view) {
693 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
694 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
695 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
696 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
698 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
699 86c66b02 2018-10-18 stsp err = view_close(v);
704 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
705 fed7eaa8 2018-10-24 stsp view = new_view;
706 878940b7 2018-09-29 stsp if (focus_view == NULL)
707 878940b7 2018-09-29 stsp focus_view = new_view;
709 0cf4efb1 2018-09-29 stsp if (focus_view) {
710 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
712 0cf4efb1 2018-09-29 stsp view->focussed = 0;
713 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
714 0cf4efb1 2018-09-29 stsp view = focus_view;
715 878940b7 2018-09-29 stsp if (new_view)
716 878940b7 2018-09-29 stsp show_panel(new_view->panel);
717 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
718 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
720 669b5ffa 2018-10-07 stsp if (view) {
721 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
722 758194b5 2018-10-24 stsp view->focussed = 1;
723 758194b5 2018-10-24 stsp show_panel(view->panel);
724 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
725 758194b5 2018-10-24 stsp show_panel(view->child->panel);
726 1a76625f 2018-10-22 stsp focus_view = view;
728 669b5ffa 2018-10-07 stsp if (view->parent) {
729 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
733 669b5ffa 2018-10-07 stsp err = view->show(view);
736 669b5ffa 2018-10-07 stsp if (view->child) {
737 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
741 1a76625f 2018-10-22 stsp update_panels();
742 1a76625f 2018-10-22 stsp doupdate();
746 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
747 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
748 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
749 e5a0f69f 2018-08-18 stsp view_close(view);
752 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
753 1a76625f 2018-10-22 stsp if (errcode)
754 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
756 e5a0f69f 2018-08-18 stsp return err;
759 4ed7e80c 2018-05-20 stsp __dead static void
760 9f7d7167 2018-04-29 stsp usage_log(void)
763 c70c5802 2018-08-01 stsp fprintf(stderr,
764 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
765 9f7d7167 2018-04-29 stsp getprogname());
769 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
770 80ddbec8 2018-04-29 stsp static const struct got_error *
771 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
773 00dfcb92 2018-06-11 stsp char *vis = NULL;
774 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
776 963b370f 2018-05-20 stsp *ws = NULL;
777 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
778 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
779 00dfcb92 2018-06-11 stsp int vislen;
780 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
781 00dfcb92 2018-06-11 stsp return got_error_from_errno();
783 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
784 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
786 00dfcb92 2018-06-11 stsp return err;
787 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
788 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
789 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
794 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
795 a7f50699 2018-06-11 stsp if (*ws == NULL) {
796 a7f50699 2018-06-11 stsp err = got_error_from_errno();
800 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
801 963b370f 2018-05-20 stsp err = got_error_from_errno();
806 963b370f 2018-05-20 stsp *ws = NULL;
809 963b370f 2018-05-20 stsp return err;
812 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
813 963b370f 2018-05-20 stsp static const struct got_error *
814 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
816 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
817 963b370f 2018-05-20 stsp int cols = 0;
818 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
819 963b370f 2018-05-20 stsp size_t wlen;
822 963b370f 2018-05-20 stsp *wlinep = NULL;
823 b700b5d6 2018-07-10 stsp *widthp = 0;
825 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
827 963b370f 2018-05-20 stsp return err;
830 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
831 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
832 963b370f 2018-05-20 stsp switch (width) {
838 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
839 b700b5d6 2018-07-10 stsp cols += width;
843 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
844 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
848 963b370f 2018-05-20 stsp err = got_error_from_errno();
852 963b370f 2018-05-20 stsp wline[i] = L'\0';
853 b700b5d6 2018-07-10 stsp if (widthp)
854 b700b5d6 2018-07-10 stsp *widthp = cols;
857 963b370f 2018-05-20 stsp free(wline);
859 963b370f 2018-05-20 stsp *wlinep = wline;
860 963b370f 2018-05-20 stsp return err;
863 963b370f 2018-05-20 stsp static const struct got_error *
864 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
865 2814baeb 2018-08-01 stsp struct got_object_id *id)
867 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
868 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
869 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
870 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
871 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
872 bb737323 2018-05-20 stsp int author_width, logmsg_width;
873 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
874 80ddbec8 2018-04-29 stsp char *line = NULL;
875 bb737323 2018-05-20 stsp int col, limit;
876 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
877 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
878 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
879 ccb26ccd 2018-11-05 stsp struct tm tm;
880 45d799e2 2018-12-23 stsp time_t committer_time;
882 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
883 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
884 ccb26ccd 2018-11-05 stsp return got_error_from_errno();
885 ccb26ccd 2018-11-05 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
886 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
887 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
889 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
890 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
892 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
893 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
894 b39d25c7 2018-07-10 stsp col = limit + 1;
895 b39d25c7 2018-07-10 stsp if (col > avail)
898 45d799e2 2018-12-23 stsp author0 = strdup(got_object_commit_get_author(commit));
899 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
900 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
903 6d9fbc00 2018-04-29 stsp author = author0;
904 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
905 6d9fbc00 2018-04-29 stsp if (smallerthan)
906 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
908 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
910 6d9fbc00 2018-04-29 stsp *at = '\0';
912 b39d25c7 2018-07-10 stsp limit = avail - col;
913 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
916 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
917 bb737323 2018-05-20 stsp col += author_width;
918 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
919 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
921 bb737323 2018-05-20 stsp author_width++;
923 9c2eaf34 2018-05-20 stsp if (col > avail)
926 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
927 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
928 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
931 bb737323 2018-05-20 stsp logmsg = logmsg0;
932 bb737323 2018-05-20 stsp while (*logmsg == '\n')
934 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
935 bb737323 2018-05-20 stsp if (newline)
936 bb737323 2018-05-20 stsp *newline = '\0';
937 bb737323 2018-05-20 stsp limit = avail - col;
938 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
941 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
942 bb737323 2018-05-20 stsp col += logmsg_width;
943 bb737323 2018-05-20 stsp while (col <= avail) {
944 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
948 80ddbec8 2018-04-29 stsp free(logmsg0);
949 bb737323 2018-05-20 stsp free(wlogmsg);
950 6d9fbc00 2018-04-29 stsp free(author0);
951 bb737323 2018-05-20 stsp free(wauthor);
952 80ddbec8 2018-04-29 stsp free(line);
953 80ddbec8 2018-04-29 stsp return err;
956 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
957 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
958 899d86c2 2018-05-10 stsp struct got_object_id *id)
960 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
962 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
963 80ddbec8 2018-04-29 stsp if (entry == NULL)
964 899d86c2 2018-05-10 stsp return NULL;
966 899d86c2 2018-05-10 stsp entry->id = id;
967 99db9666 2018-05-07 stsp entry->commit = commit;
968 899d86c2 2018-05-10 stsp return entry;
971 99db9666 2018-05-07 stsp static void
972 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
974 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
976 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
977 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
978 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
979 ecb28ae0 2018-07-16 stsp commits->ncommits--;
980 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
981 99db9666 2018-05-07 stsp free(entry);
984 99db9666 2018-05-07 stsp static void
985 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
987 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
988 99db9666 2018-05-07 stsp pop_commit(commits);
991 c4972b91 2018-05-07 stsp static const struct got_error *
992 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
993 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
995 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
996 93e45b7c 2018-09-24 stsp int nqueued = 0;
999 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1000 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1001 1a76625f 2018-10-22 stsp * while updating the display.
1003 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
1004 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1005 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1006 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1007 1a76625f 2018-10-22 stsp int errcode;
1009 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1010 9ba79e04 2018-06-11 stsp if (err) {
1011 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1013 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
1014 93e45b7c 2018-09-24 stsp minqueue, repo);
1016 ecb28ae0 2018-07-16 stsp return err;
1020 ecb28ae0 2018-07-16 stsp if (id == NULL)
1023 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1026 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1027 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1028 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
1032 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1033 1a76625f 2018-10-22 stsp if (errcode) {
1034 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1038 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1039 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1040 ecb28ae0 2018-07-16 stsp nqueued++;
1041 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1043 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1044 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1045 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1048 9ba79e04 2018-06-11 stsp return err;
1051 99db9666 2018-05-07 stsp static const struct got_error *
1052 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1054 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1055 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1057 9ba79e04 2018-06-11 stsp *head_id = NULL;
1059 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1061 99db9666 2018-05-07 stsp return err;
1063 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1064 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1065 9ba79e04 2018-06-11 stsp if (err) {
1066 9ba79e04 2018-06-11 stsp *head_id = NULL;
1067 99db9666 2018-05-07 stsp return err;
1070 9ba79e04 2018-06-11 stsp return NULL;
1073 0553a4e3 2018-04-30 stsp static const struct got_error *
1074 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1075 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1076 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1077 1a76625f 2018-10-22 stsp const char *path, int commits_needed)
1079 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1080 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1081 ecb28ae0 2018-07-16 stsp int ncommits, width;
1082 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1083 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1085 e0d42f60 2018-07-22 stsp entry = first;
1086 e0d42f60 2018-07-22 stsp ncommits = 0;
1087 e0d42f60 2018-07-22 stsp while (entry) {
1088 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1089 e0d42f60 2018-07-22 stsp *selected = entry;
1092 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1093 e0d42f60 2018-07-22 stsp ncommits++;
1096 1a76625f 2018-10-22 stsp if (*selected) {
1097 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1099 ecb28ae0 2018-07-16 stsp return err;
1102 1a76625f 2018-10-22 stsp if (asprintf(&ncommits_str, " [%d/%d]%s ",
1103 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1104 1a76625f 2018-10-22 stsp commits_needed == 0 ? "" : " loading...") == -1)
1105 1a76625f 2018-10-22 stsp return got_error_from_errno();
1107 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1108 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1109 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1110 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1111 1a76625f 2018-10-22 stsp err = got_error_from_errno();
1112 1a76625f 2018-10-22 stsp header = NULL;
1113 1a76625f 2018-10-22 stsp goto done;
1115 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1116 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1117 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1118 1a76625f 2018-10-22 stsp err = got_error_from_errno();
1119 1a76625f 2018-10-22 stsp header = NULL;
1120 1a76625f 2018-10-22 stsp goto done;
1122 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1124 1a76625f 2018-10-22 stsp goto done;
1126 2814baeb 2018-08-01 stsp werase(view->window);
1128 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1129 a3404814 2018-09-02 stsp wstandout(view->window);
1130 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1131 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1132 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1135 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1136 a3404814 2018-09-02 stsp wstandend(view->window);
1137 ecb28ae0 2018-07-16 stsp free(wline);
1138 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1139 1a76625f 2018-10-22 stsp goto done;
1141 899d86c2 2018-05-10 stsp entry = first;
1142 899d86c2 2018-05-10 stsp *last = first;
1143 867c6645 2018-07-10 stsp ncommits = 0;
1144 899d86c2 2018-05-10 stsp while (entry) {
1145 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1147 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1148 2814baeb 2018-08-01 stsp wstandout(view->window);
1149 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
1150 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1151 2814baeb 2018-08-01 stsp wstandend(view->window);
1154 0553a4e3 2018-04-30 stsp ncommits++;
1155 899d86c2 2018-05-10 stsp *last = entry;
1156 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1159 1a57306a 2018-09-02 stsp view_vborder(view);
1161 1a76625f 2018-10-22 stsp free(id_str);
1162 1a76625f 2018-10-22 stsp free(ncommits_str);
1163 1a76625f 2018-10-22 stsp free(header);
1164 80ddbec8 2018-04-29 stsp return err;
1167 07b55e75 2018-05-10 stsp static void
1168 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1169 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1171 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1172 07b55e75 2018-05-10 stsp int nscrolled = 0;
1174 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1175 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
1178 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1179 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1180 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1181 07b55e75 2018-05-10 stsp if (entry) {
1182 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1183 07b55e75 2018-05-10 stsp nscrolled++;
1188 aa075928 2018-05-10 stsp static const struct got_error *
1189 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1190 93e45b7c 2018-09-24 stsp struct commit_queue_entry **last_displayed_entry,
1191 1a76625f 2018-10-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1192 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1194 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
1195 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
1196 aa075928 2018-05-10 stsp int nscrolled = 0;
1198 1a76625f 2018-10-22 stsp if (*last_displayed_entry == NULL)
1199 1a76625f 2018-10-22 stsp return NULL;
1202 93e45b7c 2018-09-24 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1203 aa075928 2018-05-10 stsp if (pentry == NULL) {
1204 1a76625f 2018-10-22 stsp int errcode;
1205 1a76625f 2018-10-22 stsp if (*log_complete)
1206 1a76625f 2018-10-22 stsp return NULL;
1207 1a76625f 2018-10-22 stsp *commits_needed = maxscroll + 20;
1208 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(need_commits);
1209 1a76625f 2018-10-22 stsp if (errcode)
1210 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1211 1a76625f 2018-10-22 stsp return NULL;
1213 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1215 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1216 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1218 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1219 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1221 dd0a52c1 2018-05-20 stsp return err;
1224 cd0acaa7 2018-05-20 stsp static const struct got_error *
1225 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1226 0cf4efb1 2018-09-29 stsp struct got_object_id *commit_id, struct got_commit_object *commit,
1227 2a4718d3 2018-09-29 stsp struct got_repository *repo)
1229 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1230 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1231 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1233 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1234 15a94983 2018-12-23 stsp if (diff_view == NULL)
1235 15a94983 2018-12-23 stsp return got_error_from_errno();
1237 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1238 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1239 4acef5ee 2018-12-24 stsp commit_id, repo);
1240 e5a0f69f 2018-08-18 stsp if (err == NULL)
1241 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1242 cd0acaa7 2018-05-20 stsp return err;
1245 80ddbec8 2018-04-29 stsp static const struct got_error *
1246 0cf4efb1 2018-09-29 stsp browse_commit(struct tog_view **new_view, int begin_x,
1247 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1249 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1250 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1251 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1253 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1254 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1256 9343a5fb 2018-06-23 stsp return err;
1258 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1259 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1260 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1262 e5a0f69f 2018-08-18 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1264 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1266 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1267 1a76625f 2018-10-22 stsp return err;
1270 1a76625f 2018-10-22 stsp static void *
1271 1a76625f 2018-10-22 stsp log_thread(void *arg)
1273 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1274 1a76625f 2018-10-22 stsp int errcode = 0;
1275 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1276 1a76625f 2018-10-22 stsp int done = 0;
1278 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1280 1a76625f 2018-10-22 stsp return (void *)err;
1282 1a76625f 2018-10-22 stsp while (!done && !err) {
1283 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1284 1a76625f 2018-10-22 stsp a->in_repo_path);
1285 1a76625f 2018-10-22 stsp if (err) {
1286 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1287 1a76625f 2018-10-22 stsp return (void *)err;
1288 1a76625f 2018-10-22 stsp err = NULL;
1290 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1291 1a76625f 2018-10-22 stsp a->commits_needed--;
1293 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1294 1a76625f 2018-10-22 stsp if (errcode)
1295 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
1298 1a76625f 2018-10-22 stsp a->log_complete = 1;
1299 1a76625f 2018-10-22 stsp else if (*a->quit) {
1301 1a76625f 2018-10-22 stsp a->log_complete = 1;
1302 1a76625f 2018-10-22 stsp } else if (*a->first_displayed_entry == NULL) {
1303 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1304 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1305 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1309 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1310 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1311 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1312 1a76625f 2018-10-22 stsp &tog_mutex);
1313 1a76625f 2018-10-22 stsp if (errcode)
1314 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1317 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1318 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1319 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1321 1a76625f 2018-10-22 stsp return (void *)err;
1324 1a76625f 2018-10-22 stsp static const struct got_error *
1325 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1327 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1328 1a76625f 2018-10-22 stsp int errcode;
1330 1a76625f 2018-10-22 stsp if (s->thread) {
1331 1a76625f 2018-10-22 stsp s->quit = 1;
1332 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1333 1a76625f 2018-10-22 stsp if (errcode)
1334 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1335 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1336 1a76625f 2018-10-22 stsp if (errcode)
1337 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1338 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1339 1a76625f 2018-10-22 stsp if (errcode)
1340 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1341 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1342 1a76625f 2018-10-22 stsp if (errcode)
1343 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1344 1a76625f 2018-10-22 stsp s->thread = NULL;
1347 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1348 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1349 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1351 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1352 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1353 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1356 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1357 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1358 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1361 9343a5fb 2018-06-23 stsp return err;
1364 9343a5fb 2018-06-23 stsp static const struct got_error *
1365 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1367 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1368 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1370 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1371 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1372 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1373 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1374 1a76625f 2018-10-22 stsp free(s->start_id);
1375 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1376 1a76625f 2018-10-22 stsp return err;
1379 1a76625f 2018-10-22 stsp static const struct got_error *
1380 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1381 23721109 2018-10-22 stsp struct got_repository *repo, const char *path, int check_disk)
1383 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1384 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1385 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1386 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1387 1a76625f 2018-10-22 stsp int errcode;
1389 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1390 ecb28ae0 2018-07-16 stsp if (err != NULL)
1391 ecb28ae0 2018-07-16 stsp goto done;
1393 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1394 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1395 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1397 fb2756b9 2018-08-04 stsp s->repo = repo;
1398 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1399 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1400 5036bf37 2018-09-24 stsp err = got_error_from_errno();
1401 5036bf37 2018-09-24 stsp goto done;
1404 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1405 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1406 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1408 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1410 1a76625f 2018-10-22 stsp goto done;
1411 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1412 1a76625f 2018-10-22 stsp 0, thread_repo);
1414 1a76625f 2018-10-22 stsp goto done;
1416 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1417 1a76625f 2018-10-22 stsp if (errcode) {
1418 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1419 1a76625f 2018-10-22 stsp goto done;
1422 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1423 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1424 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1425 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1426 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1427 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1428 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1429 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1430 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1431 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1432 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1435 1a76625f 2018-10-22 stsp close_log_view(view);
1436 ba4f502b 2018-08-04 stsp return err;
1439 e5a0f69f 2018-08-18 stsp static const struct got_error *
1440 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1442 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1444 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1445 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1446 2b380cc8 2018-10-24 stsp &s->thread_args);
1447 2b380cc8 2018-10-24 stsp if (errcode)
1448 2b380cc8 2018-10-24 stsp return got_error_set_errno(errcode);
1451 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1452 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1453 1a76625f 2018-10-22 stsp &s->commits, s->selected, view->nlines,
1454 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1457 e5a0f69f 2018-08-18 stsp static const struct got_error *
1458 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1459 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1461 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1462 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1463 5036bf37 2018-09-24 stsp char *parent_path;
1464 669b5ffa 2018-10-07 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
1465 669b5ffa 2018-10-07 stsp int begin_x = 0;
1467 e5a0f69f 2018-08-18 stsp switch (ch) {
1469 1a76625f 2018-10-22 stsp s->quit = 1;
1472 e5a0f69f 2018-08-18 stsp case KEY_UP:
1473 2b380cc8 2018-10-24 stsp if (s->first_displayed_entry == NULL)
1475 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1476 e5a0f69f 2018-08-18 stsp s->selected--;
1477 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1479 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1480 e5a0f69f 2018-08-18 stsp &s->commits);
1482 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1483 2b380cc8 2018-10-24 stsp if (s->first_displayed_entry == NULL)
1485 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1486 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1487 e5a0f69f 2018-08-18 stsp s->selected = 0;
1490 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1491 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1494 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1495 2b380cc8 2018-10-24 stsp if (s->first_displayed_entry == NULL)
1497 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1498 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1499 e5a0f69f 2018-08-18 stsp s->selected++;
1502 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1503 93e45b7c 2018-09-24 stsp &s->last_displayed_entry, &s->commits,
1504 1a76625f 2018-10-22 stsp &s->thread_args.log_complete,
1505 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1506 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1508 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1509 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1510 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1511 2b380cc8 2018-10-24 stsp if (first == NULL)
1513 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1514 93e45b7c 2018-09-24 stsp view->nlines, &s->last_displayed_entry,
1515 1a76625f 2018-10-22 stsp &s->commits, &s->thread_args.log_complete,
1516 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1517 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1518 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1519 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1520 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1521 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1522 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1523 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1525 e5a0f69f 2018-08-18 stsp err = NULL;
1528 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1529 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1530 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1531 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1532 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1534 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1535 e5a0f69f 2018-08-18 stsp case '\r':
1536 2b380cc8 2018-10-24 stsp if (s->selected_entry == NULL)
1538 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1539 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1540 669b5ffa 2018-10-07 stsp err = open_diff_view_for_commit(&diff_view, begin_x,
1541 2a4718d3 2018-09-29 stsp s->selected_entry->id, s->selected_entry->commit,
1545 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1546 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1548 669b5ffa 2018-10-07 stsp return err;
1549 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
1550 669b5ffa 2018-10-07 stsp if (err) {
1551 669b5ffa 2018-10-07 stsp view_close(diff_view);
1554 55566b34 2018-11-05 stsp *focus_view = diff_view;
1555 55566b34 2018-11-05 stsp view->child_focussed = 1;
1557 669b5ffa 2018-10-07 stsp *new_view = diff_view;
1560 2b380cc8 2018-10-24 stsp if (s->selected_entry == NULL)
1562 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1563 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1564 669b5ffa 2018-10-07 stsp err = browse_commit(&tree_view, begin_x,
1565 0cf4efb1 2018-09-29 stsp s->selected_entry, s->repo);
1568 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1569 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1571 669b5ffa 2018-10-07 stsp return err;
1572 669b5ffa 2018-10-07 stsp err = view_set_child(view, tree_view);
1573 669b5ffa 2018-10-07 stsp if (err) {
1574 669b5ffa 2018-10-07 stsp view_close(tree_view);
1577 669b5ffa 2018-10-07 stsp *focus_view = tree_view;
1578 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1580 669b5ffa 2018-10-07 stsp *new_view = tree_view;
1582 5036bf37 2018-09-24 stsp case KEY_BACKSPACE:
1583 5036bf37 2018-09-24 stsp if (strcmp(s->in_repo_path, "/") == 0)
1585 5036bf37 2018-09-24 stsp parent_path = dirname(s->in_repo_path);
1586 5036bf37 2018-09-24 stsp if (parent_path && strcmp(parent_path, ".") != 0) {
1587 5036bf37 2018-09-24 stsp struct tog_view *lv;
1588 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1590 1a76625f 2018-10-22 stsp return err;
1591 0cf4efb1 2018-09-29 stsp lv = view_open(view->nlines, view->ncols,
1592 0cf4efb1 2018-09-29 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
1593 5036bf37 2018-09-24 stsp if (lv == NULL)
1594 5036bf37 2018-09-24 stsp return got_error_from_errno();
1595 5036bf37 2018-09-24 stsp err = open_log_view(lv, s->start_id, s->repo,
1596 23721109 2018-10-22 stsp parent_path, 0);
1598 1a76625f 2018-10-22 stsp return err;;
1599 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1600 669b5ffa 2018-10-07 stsp *new_view = lv;
1602 669b5ffa 2018-10-07 stsp view_set_child(view->parent, lv);
1603 34095730 2018-10-22 stsp *focus_view = lv;
1605 1a76625f 2018-10-22 stsp return NULL;
1612 80ddbec8 2018-04-29 stsp return err;
1615 4ed7e80c 2018-05-20 stsp static const struct got_error *
1616 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
1618 c2db6724 2019-01-04 stsp const struct got_error *error;
1620 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
1621 c2db6724 2019-01-04 stsp return got_error_from_errno();
1623 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
1624 c2db6724 2019-01-04 stsp return got_error_from_errno();
1626 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
1627 c2db6724 2019-01-04 stsp return got_error_from_errno();
1629 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
1630 c2db6724 2019-01-04 stsp if (error != NULL)
1631 c2db6724 2019-01-04 stsp return error;
1633 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
1634 c2db6724 2019-01-04 stsp return got_error_from_errno();
1636 c2db6724 2019-01-04 stsp return NULL;
1639 c2db6724 2019-01-04 stsp static const struct got_error *
1640 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1642 80ddbec8 2018-04-29 stsp const struct got_error *error;
1643 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1644 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1645 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1646 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1648 04cc582a 2018-08-01 stsp struct tog_view *view;
1650 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1651 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1652 c2db6724 2019-01-04 stsp NULL) == -1)
1653 80ddbec8 2018-04-29 stsp err(1, "pledge");
1656 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1657 80ddbec8 2018-04-29 stsp switch (ch) {
1659 80ddbec8 2018-04-29 stsp start_commit = optarg;
1662 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1663 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1664 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1668 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1672 80ddbec8 2018-04-29 stsp argc -= optind;
1673 80ddbec8 2018-04-29 stsp argv += optind;
1675 ecb28ae0 2018-07-16 stsp if (argc == 0)
1676 ecb28ae0 2018-07-16 stsp path = strdup("");
1677 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1678 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1680 80ddbec8 2018-04-29 stsp usage_log();
1681 ecb28ae0 2018-07-16 stsp if (path == NULL)
1682 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1684 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1685 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1686 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1687 ecb28ae0 2018-07-16 stsp goto done;
1689 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1690 b7165be3 2019-02-05 stsp struct got_worktree *worktree;
1691 b7165be3 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1692 b7165be3 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1693 b7165be3 2019-02-05 stsp goto done;
1694 b7165be3 2019-02-05 stsp if (worktree) {
1695 b7165be3 2019-02-05 stsp repo_path =
1696 b7165be3 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1697 b7165be3 2019-02-05 stsp got_worktree_close(worktree);
1699 b7165be3 2019-02-05 stsp repo_path = strdup(cwd);
1700 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1701 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1702 ecb28ae0 2018-07-16 stsp goto done;
1706 c2db6724 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
1707 c2db6724 2019-01-04 stsp if (error)
1708 c2db6724 2019-01-04 stsp goto done;
1710 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1711 80ddbec8 2018-04-29 stsp if (error != NULL)
1712 ecb28ae0 2018-07-16 stsp goto done;
1714 15a94983 2018-12-23 stsp if (start_commit == NULL)
1715 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1717 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&start_id, repo,
1718 15a94983 2018-12-23 stsp start_commit);
1719 80ddbec8 2018-04-29 stsp if (error != NULL)
1720 ecb28ae0 2018-07-16 stsp goto done;
1722 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1723 04cc582a 2018-08-01 stsp if (view == NULL) {
1724 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1725 04cc582a 2018-08-01 stsp goto done;
1727 23721109 2018-10-22 stsp error = open_log_view(view, start_id, repo, path, 1);
1728 ba4f502b 2018-08-04 stsp if (error)
1729 ba4f502b 2018-08-04 stsp goto done;
1730 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1732 ecb28ae0 2018-07-16 stsp free(repo_path);
1733 ecb28ae0 2018-07-16 stsp free(cwd);
1734 ecb28ae0 2018-07-16 stsp free(path);
1735 899d86c2 2018-05-10 stsp free(start_id);
1737 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1738 80ddbec8 2018-04-29 stsp return error;
1741 4ed7e80c 2018-05-20 stsp __dead static void
1742 9f7d7167 2018-04-29 stsp usage_diff(void)
1745 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1746 9f7d7167 2018-04-29 stsp getprogname());
1750 b304db33 2018-05-20 stsp static char *
1751 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1753 b304db33 2018-05-20 stsp char *line;
1754 b304db33 2018-05-20 stsp size_t linelen;
1755 b304db33 2018-05-20 stsp size_t lineno;
1756 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1758 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1760 b304db33 2018-05-20 stsp *len = linelen;
1761 b304db33 2018-05-20 stsp return line;
1764 4ed7e80c 2018-05-20 stsp static const struct got_error *
1765 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1766 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1767 a3404814 2018-09-02 stsp char * header)
1769 61e69b96 2018-05-20 stsp const struct got_error *err;
1770 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1771 b304db33 2018-05-20 stsp char *line;
1772 b304db33 2018-05-20 stsp size_t len;
1773 61e69b96 2018-05-20 stsp wchar_t *wline;
1774 e0b650dd 2018-05-20 stsp int width;
1776 26ed57b2 2018-05-19 stsp rewind(f);
1777 f7d12f7e 2018-08-01 stsp werase(view->window);
1779 a3404814 2018-09-02 stsp if (header) {
1780 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1781 a3404814 2018-09-02 stsp if (err) {
1782 a3404814 2018-09-02 stsp return err;
1785 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1786 a3404814 2018-09-02 stsp wstandout(view->window);
1787 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1788 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1789 a3404814 2018-09-02 stsp wstandend(view->window);
1790 a3404814 2018-09-02 stsp if (width < view->ncols)
1791 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1793 a3404814 2018-09-02 stsp if (max_lines <= 1)
1794 a3404814 2018-09-02 stsp return NULL;
1795 a3404814 2018-09-02 stsp max_lines--;
1799 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1800 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1801 26ed57b2 2018-05-19 stsp if (line == NULL) {
1805 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1806 26ed57b2 2018-05-19 stsp free(line);
1810 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1811 61e69b96 2018-05-20 stsp if (err) {
1812 61e69b96 2018-05-20 stsp free(line);
1813 61e69b96 2018-05-20 stsp return err;
1815 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1816 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1817 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1818 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1819 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1820 26ed57b2 2018-05-19 stsp free(line);
1821 2550e4c3 2018-07-13 stsp free(wline);
1822 2550e4c3 2018-07-13 stsp wline = NULL;
1824 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1826 1a57306a 2018-09-02 stsp view_vborder(view);
1828 26ed57b2 2018-05-19 stsp return NULL;
1831 abd2672a 2018-12-23 stsp static char *
1832 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
1834 abd2672a 2018-12-23 stsp char *p, *s = ctime_r(time, datebuf);
1835 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
1837 abd2672a 2018-12-23 stsp *p = '\0';
1841 4ed7e80c 2018-05-20 stsp static const struct got_error *
1842 15a94983 2018-12-23 stsp write_commit_info(struct got_object_id *commit_id, struct got_repository *repo,
1843 abd2672a 2018-12-23 stsp FILE *outfile)
1845 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
1846 abd2672a 2018-12-23 stsp char datebuf[26];
1847 15a94983 2018-12-23 stsp struct got_commit_object *commit;
1848 15a94983 2018-12-23 stsp char *id_str = NULL;
1849 45d799e2 2018-12-23 stsp time_t committer_time;
1850 45d799e2 2018-12-23 stsp const char *author, *committer;
1852 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1854 abd2672a 2018-12-23 stsp return err;
1856 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
1857 15a94983 2018-12-23 stsp if (err) {
1858 15a94983 2018-12-23 stsp err = got_error_from_errno();
1859 15a94983 2018-12-23 stsp goto done;
1862 c1124f18 2018-12-23 stsp if (fprintf(outfile, "commit %s\n", id_str) < 0) {
1863 abd2672a 2018-12-23 stsp err = got_error_from_errno();
1864 abd2672a 2018-12-23 stsp goto done;
1866 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
1867 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
1868 abd2672a 2018-12-23 stsp err = got_error_from_errno();
1869 abd2672a 2018-12-23 stsp goto done;
1871 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1872 abd2672a 2018-12-23 stsp if (fprintf(outfile, "date: %s UTC\n",
1873 45d799e2 2018-12-23 stsp get_datestr(&committer_time, datebuf)) < 0) {
1874 abd2672a 2018-12-23 stsp err = got_error_from_errno();
1875 abd2672a 2018-12-23 stsp goto done;
1877 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1878 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1879 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
1880 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
1881 abd2672a 2018-12-23 stsp err = got_error_from_errno();
1882 abd2672a 2018-12-23 stsp goto done;
1884 45d799e2 2018-12-23 stsp if (fprintf(outfile, "%s\n",
1885 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(commit)) < 0) {
1886 abd2672a 2018-12-23 stsp err = got_error_from_errno();
1887 abd2672a 2018-12-23 stsp goto done;
1890 abd2672a 2018-12-23 stsp free(id_str);
1891 15a94983 2018-12-23 stsp got_object_commit_close(commit);
1892 abd2672a 2018-12-23 stsp return err;
1895 abd2672a 2018-12-23 stsp static const struct got_error *
1896 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
1898 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
1899 48ae06ee 2018-10-18 stsp FILE *f = NULL;
1900 15a94983 2018-12-23 stsp int obj_type;
1902 511a516b 2018-05-19 stsp f = got_opentemp();
1903 48ae06ee 2018-10-18 stsp if (f == NULL) {
1904 48ae06ee 2018-10-18 stsp err = got_error_from_errno();
1905 48ae06ee 2018-10-18 stsp goto done;
1908 48ae06ee 2018-10-18 stsp fclose(s->f);
1911 15a94983 2018-12-23 stsp if (s->id1)
1912 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
1914 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
1916 15a94983 2018-12-23 stsp goto done;
1918 15a94983 2018-12-23 stsp switch (obj_type) {
1919 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1920 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
1921 54156555 2018-12-24 stsp s->diff_context, s->repo, f);
1923 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1924 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
1925 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1927 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
1928 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1929 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
1930 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
1932 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
1935 abd2672a 2018-12-23 stsp /* Show commit info if we're diffing to a parent commit. */
1936 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
1937 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
1938 15a94983 2018-12-23 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
1939 15a94983 2018-12-23 stsp write_commit_info(s->id2, s->repo, f);
1943 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
1945 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
1946 15a94983 2018-12-23 stsp s->diff_context, s->repo, f);
1950 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1955 48ae06ee 2018-10-18 stsp fflush(f);
1956 48ae06ee 2018-10-18 stsp return err;
1959 48ae06ee 2018-10-18 stsp static const struct got_error *
1960 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
1961 15a94983 2018-12-23 stsp struct got_object_id *id2, struct got_repository *repo)
1963 48ae06ee 2018-10-18 stsp const struct got_error *err;
1965 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
1966 15a94983 2018-12-23 stsp int type1, type2;
1967 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
1969 15a94983 2018-12-23 stsp return err;
1970 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
1972 15a94983 2018-12-23 stsp return err;
1974 15a94983 2018-12-23 stsp if (type1 != type2)
1975 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
1978 15a94983 2018-12-23 stsp if (id1) {
1979 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
1980 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
1981 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1983 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1985 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
1986 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
1987 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1988 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1989 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1991 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
1992 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1993 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1994 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
1995 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
1997 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
1998 48ae06ee 2018-10-18 stsp if (err) {
1999 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2000 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2001 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2002 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2003 48ae06ee 2018-10-18 stsp return err;
2006 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2007 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2008 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2010 5dc9f4bc 2018-08-04 stsp return NULL;
2013 e5a0f69f 2018-08-18 stsp static const struct got_error *
2014 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2016 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2018 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2019 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2020 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2021 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2022 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2023 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2024 e5a0f69f 2018-08-18 stsp return err;
2027 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2028 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2030 a3404814 2018-09-02 stsp const struct got_error *err;
2031 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2032 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2034 a3404814 2018-09-02 stsp if (s->id1) {
2035 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2037 a3404814 2018-09-02 stsp return err;
2039 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2041 a3404814 2018-09-02 stsp return err;
2043 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2044 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2045 a3404814 2018-09-02 stsp err = got_error_from_errno();
2046 a3404814 2018-09-02 stsp free(id_str1);
2047 a3404814 2018-09-02 stsp free(id_str2);
2048 a3404814 2018-09-02 stsp return err;
2050 a3404814 2018-09-02 stsp free(id_str1);
2051 a3404814 2018-09-02 stsp free(id_str2);
2053 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2054 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2058 0cf4efb1 2018-09-29 stsp static const struct got_error *
2059 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2060 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2062 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2063 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2066 e5a0f69f 2018-08-18 stsp switch (ch) {
2068 e5a0f69f 2018-08-18 stsp case KEY_UP:
2069 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
2070 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2072 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2074 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
2075 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2076 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2079 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2080 e5a0f69f 2018-08-18 stsp if (!s->eof)
2081 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2083 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2086 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
2087 e5a0f69f 2018-08-18 stsp char *line;
2088 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
2089 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2090 e5a0f69f 2018-08-18 stsp if (line == NULL)
2095 48ae06ee 2018-10-18 stsp if (s->diff_context > 0) {
2096 48ae06ee 2018-10-18 stsp s->diff_context--;
2097 48ae06ee 2018-10-18 stsp err = create_diff(s);
2101 4a8520aa 2018-10-18 stsp if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2102 48ae06ee 2018-10-18 stsp s->diff_context++;
2103 48ae06ee 2018-10-18 stsp err = create_diff(s);
2110 bcbd79e2 2018-08-19 stsp return err;
2113 4ed7e80c 2018-05-20 stsp static const struct got_error *
2114 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2116 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2117 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2118 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2119 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2120 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2122 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2124 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2125 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2126 eb6600df 2019-01-04 stsp NULL) == -1)
2127 26ed57b2 2018-05-19 stsp err(1, "pledge");
2130 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2131 26ed57b2 2018-05-19 stsp switch (ch) {
2134 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2138 26ed57b2 2018-05-19 stsp argc -= optind;
2139 26ed57b2 2018-05-19 stsp argv += optind;
2141 26ed57b2 2018-05-19 stsp if (argc == 0) {
2142 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2143 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2144 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2145 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2146 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2147 15a94983 2018-12-23 stsp id_str1 = argv[0];
2148 15a94983 2018-12-23 stsp id_str2 = argv[1];
2149 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2150 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2151 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2152 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2153 15a94983 2018-12-23 stsp id_str1 = argv[1];
2154 15a94983 2018-12-23 stsp id_str2 = argv[2];
2156 26ed57b2 2018-05-19 stsp usage_diff();
2158 eb6600df 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
2159 eb6600df 2019-01-04 stsp if (error)
2160 eb6600df 2019-01-04 stsp goto done;
2162 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
2163 26ed57b2 2018-05-19 stsp free(repo_path);
2164 26ed57b2 2018-05-19 stsp if (error)
2165 26ed57b2 2018-05-19 stsp goto done;
2167 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
2168 26ed57b2 2018-05-19 stsp if (error)
2169 26ed57b2 2018-05-19 stsp goto done;
2171 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
2172 26ed57b2 2018-05-19 stsp if (error)
2173 26ed57b2 2018-05-19 stsp goto done;
2175 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2176 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2177 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
2178 ea5e7bb5 2018-08-01 stsp goto done;
2180 15a94983 2018-12-23 stsp error = open_diff_view(view, id1, id2, repo);
2181 5dc9f4bc 2018-08-04 stsp if (error)
2182 5dc9f4bc 2018-08-04 stsp goto done;
2183 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2185 26ed57b2 2018-05-19 stsp got_repo_close(repo);
2186 26ed57b2 2018-05-19 stsp return error;
2189 4ed7e80c 2018-05-20 stsp __dead static void
2190 9f7d7167 2018-04-29 stsp usage_blame(void)
2193 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2194 9f7d7167 2018-04-29 stsp getprogname());
2198 84451b3e 2018-07-10 stsp struct tog_blame_line {
2199 84451b3e 2018-07-10 stsp int annotated;
2200 84451b3e 2018-07-10 stsp struct got_object_id *id;
2203 4ed7e80c 2018-05-20 stsp static const struct got_error *
2204 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2205 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2206 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2207 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2209 84451b3e 2018-07-10 stsp const struct got_error *err;
2210 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2211 84451b3e 2018-07-10 stsp char *line;
2212 84451b3e 2018-07-10 stsp size_t len;
2213 84451b3e 2018-07-10 stsp wchar_t *wline;
2214 b700b5d6 2018-07-10 stsp int width, wlimit;
2215 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2216 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2217 ab089a2a 2018-07-12 stsp char *id_str;
2219 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2221 ab089a2a 2018-07-12 stsp return err;
2223 84451b3e 2018-07-10 stsp rewind(f);
2224 f7d12f7e 2018-08-01 stsp werase(view->window);
2226 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
2227 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
2228 ab089a2a 2018-07-12 stsp free(id_str);
2229 ab089a2a 2018-07-12 stsp return err;
2232 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2233 ab089a2a 2018-07-12 stsp free(line);
2234 2550e4c3 2018-07-13 stsp line = NULL;
2235 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2236 a3404814 2018-09-02 stsp wstandout(view->window);
2237 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2238 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2239 a3404814 2018-09-02 stsp wstandend(view->window);
2240 2550e4c3 2018-07-13 stsp free(wline);
2241 2550e4c3 2018-07-13 stsp wline = NULL;
2242 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2243 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2245 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2246 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2247 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
2248 ab089a2a 2018-07-12 stsp free(id_str);
2249 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
2251 ab089a2a 2018-07-12 stsp free(id_str);
2252 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2253 3f60a8ef 2018-07-10 stsp free(line);
2254 2550e4c3 2018-07-13 stsp line = NULL;
2256 3f60a8ef 2018-07-10 stsp return err;
2257 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2258 2550e4c3 2018-07-13 stsp free(wline);
2259 2550e4c3 2018-07-13 stsp wline = NULL;
2260 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2261 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2264 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2265 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2266 84451b3e 2018-07-10 stsp if (line == NULL) {
2270 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2271 84451b3e 2018-07-10 stsp free(line);
2275 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2276 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2277 84451b3e 2018-07-10 stsp if (err) {
2278 84451b3e 2018-07-10 stsp free(line);
2279 84451b3e 2018-07-10 stsp return err;
2282 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2283 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2285 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2286 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2287 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2288 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2289 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2290 84451b3e 2018-07-10 stsp char *id_str;
2291 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2292 84451b3e 2018-07-10 stsp if (err) {
2293 84451b3e 2018-07-10 stsp free(line);
2294 2550e4c3 2018-07-13 stsp free(wline);
2295 84451b3e 2018-07-10 stsp return err;
2297 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2298 84451b3e 2018-07-10 stsp free(id_str);
2299 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2301 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2302 ee41ec32 2018-07-10 stsp prev_id = NULL;
2305 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2306 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2307 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2310 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2311 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2312 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2313 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2314 84451b3e 2018-07-10 stsp free(line);
2315 2550e4c3 2018-07-13 stsp free(wline);
2316 2550e4c3 2018-07-13 stsp wline = NULL;
2318 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2320 1a57306a 2018-09-02 stsp view_vborder(view);
2322 84451b3e 2018-07-10 stsp return NULL;
2325 84451b3e 2018-07-10 stsp static const struct got_error *
2326 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2328 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2329 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2330 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2331 1a76625f 2018-10-22 stsp int errcode;
2333 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2334 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2335 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2337 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2338 1a76625f 2018-10-22 stsp if (errcode)
2339 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2341 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2342 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2343 d68a0a7d 2018-07-10 stsp goto done;
2346 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2347 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2349 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2350 d68a0a7d 2018-07-10 stsp if (line->annotated)
2351 d68a0a7d 2018-07-10 stsp goto done;
2353 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2354 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2355 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2356 84451b3e 2018-07-10 stsp goto done;
2358 84451b3e 2018-07-10 stsp line->annotated = 1;
2360 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2361 1a76625f 2018-10-22 stsp if (errcode)
2362 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2363 84451b3e 2018-07-10 stsp return err;
2366 84451b3e 2018-07-10 stsp static void *
2367 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2369 18430de3 2018-07-10 stsp const struct got_error *err;
2370 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
2371 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
2372 1a76625f 2018-10-22 stsp int errcode;
2374 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2375 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
2377 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2378 1a76625f 2018-10-22 stsp if (errcode)
2379 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
2381 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
2382 c9beca56 2018-07-22 stsp ta->repo = NULL;
2383 c9beca56 2018-07-22 stsp *ta->complete = 1;
2385 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2386 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2387 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2389 18430de3 2018-07-10 stsp return (void *)err;
2392 245d91c1 2018-07-12 stsp static struct got_object_id *
2393 15a94983 2018-12-23 stsp get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2394 15a94983 2018-12-23 stsp int selected_line)
2396 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2398 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2399 245d91c1 2018-07-12 stsp if (!line->annotated)
2400 245d91c1 2018-07-12 stsp return NULL;
2402 245d91c1 2018-07-12 stsp return line->id;
2405 b880a918 2018-07-10 stsp static const struct got_error *
2406 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2408 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2411 245d91c1 2018-07-12 stsp if (blame->thread) {
2412 1a76625f 2018-10-22 stsp int errcode;
2413 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2414 1a76625f 2018-10-22 stsp if (errcode)
2415 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2416 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
2417 1a76625f 2018-10-22 stsp if (errcode)
2418 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2419 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2420 1a76625f 2018-10-22 stsp if (errcode)
2421 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2422 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2423 245d91c1 2018-07-12 stsp err = NULL;
2424 245d91c1 2018-07-12 stsp blame->thread = NULL;
2426 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2427 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2428 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2430 245d91c1 2018-07-12 stsp if (blame->f) {
2431 245d91c1 2018-07-12 stsp fclose(blame->f);
2432 245d91c1 2018-07-12 stsp blame->f = NULL;
2434 57670559 2018-12-23 stsp if (blame->lines) {
2435 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
2436 57670559 2018-12-23 stsp free(blame->lines[i].id);
2437 57670559 2018-12-23 stsp free(blame->lines);
2438 57670559 2018-12-23 stsp blame->lines = NULL;
2440 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2441 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2443 245d91c1 2018-07-12 stsp return err;
2446 245d91c1 2018-07-12 stsp static const struct got_error *
2447 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2448 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
2449 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
2450 245d91c1 2018-07-12 stsp struct got_repository *repo)
2452 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2453 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2454 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2455 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2456 15a94983 2018-12-23 stsp int obj_type;
2458 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2460 15a94983 2018-12-23 stsp return err;
2461 15a94983 2018-12-23 stsp if (obj_id == NULL)
2462 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
2464 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
2466 84451b3e 2018-07-10 stsp goto done;
2468 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2469 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2470 84451b3e 2018-07-10 stsp goto done;
2473 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2475 a70480e0 2018-06-23 stsp goto done;
2476 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2477 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2478 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2479 84451b3e 2018-07-10 stsp goto done;
2481 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2482 245d91c1 2018-07-12 stsp blame->f, blob);
2484 84451b3e 2018-07-10 stsp goto done;
2486 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2487 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2488 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2489 84451b3e 2018-07-10 stsp goto done;
2492 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2494 bd24772e 2018-07-11 stsp goto done;
2496 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2497 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2498 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2499 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2500 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2501 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2502 245d91c1 2018-07-12 stsp goto done;
2504 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2506 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2507 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2508 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2509 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2510 245d91c1 2018-07-12 stsp *blame_complete = 0;
2514 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2515 27d434c2 2018-09-15 stsp free(obj_id);
2517 245d91c1 2018-07-12 stsp stop_blame(blame);
2518 245d91c1 2018-07-12 stsp return err;
2521 245d91c1 2018-07-12 stsp static const struct got_error *
2522 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2523 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2525 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2526 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2528 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2530 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2532 7cbe629d 2018-08-04 stsp return err;
2534 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2535 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2536 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2537 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2538 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2539 fb2756b9 2018-08-04 stsp s->path = path;
2540 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2541 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2542 fb2756b9 2018-08-04 stsp s->repo = repo;
2543 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2544 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2546 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2547 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2548 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2550 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
2551 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2552 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2553 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2556 e5a0f69f 2018-08-18 stsp static const struct got_error *
2557 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2559 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2560 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2562 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2563 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2565 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2566 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2567 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2568 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2569 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2572 e5a0f69f 2018-08-18 stsp free(s->path);
2574 e5a0f69f 2018-08-18 stsp return err;
2577 7cbe629d 2018-08-04 stsp static const struct got_error *
2578 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2580 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2581 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2582 2b380cc8 2018-10-24 stsp int errcode;
2584 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
2585 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2586 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
2587 2b380cc8 2018-10-24 stsp if (errcode)
2588 2b380cc8 2018-10-24 stsp return got_error_set_errno(errcode);
2591 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2592 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2593 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2594 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2596 669b5ffa 2018-10-07 stsp view_vborder(view);
2597 e5a0f69f 2018-08-18 stsp return err;
2600 e5a0f69f 2018-08-18 stsp static const struct got_error *
2601 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2602 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2604 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2605 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2606 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2607 669b5ffa 2018-10-07 stsp int begin_x = 0;
2609 e5a0f69f 2018-08-18 stsp switch (ch) {
2611 e5a0f69f 2018-08-18 stsp s->done = 1;
2614 e5a0f69f 2018-08-18 stsp case KEY_UP:
2615 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2616 e5a0f69f 2018-08-18 stsp s->selected_line--;
2617 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2618 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2619 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2621 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2622 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2623 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2626 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2627 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2628 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2630 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2633 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2634 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2635 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2636 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2637 e5a0f69f 2018-08-18 stsp s->selected_line++;
2638 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2639 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2640 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2643 e5a0f69f 2018-08-18 stsp case 'p': {
2644 15a94983 2018-12-23 stsp struct got_object_id *id = NULL;
2645 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2646 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2647 15a94983 2018-12-23 stsp if (id == NULL)
2649 15a94983 2018-12-23 stsp if (ch == 'p') {
2650 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2651 15a94983 2018-12-23 stsp struct got_object_qid *pid;
2652 15a94983 2018-12-23 stsp struct got_object_id *blob_id = NULL;
2653 15a94983 2018-12-23 stsp int obj_type;
2654 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit,
2655 15a94983 2018-12-23 stsp s->repo, id);
2658 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
2659 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
2660 15a94983 2018-12-23 stsp if (pid == NULL) {
2661 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2664 15a94983 2018-12-23 stsp /* Check if path history ends here. */
2665 15a94983 2018-12-23 stsp err = got_object_id_by_path(&blob_id, s->repo,
2666 15a94983 2018-12-23 stsp pid->id, s->path);
2667 15a94983 2018-12-23 stsp if (err) {
2668 15a94983 2018-12-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
2669 15a94983 2018-12-23 stsp err = NULL;
2670 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2673 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo,
2675 15a94983 2018-12-23 stsp free(blob_id);
2676 15a94983 2018-12-23 stsp /* Can't blame non-blob type objects. */
2677 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2678 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2681 15a94983 2018-12-23 stsp err = got_object_qid_alloc(&s->blamed_commit,
2683 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2685 15a94983 2018-12-23 stsp if (got_object_id_cmp(id,
2686 15a94983 2018-12-23 stsp s->blamed_commit->id) == 0)
2688 15a94983 2018-12-23 stsp err = got_object_qid_alloc(&s->blamed_commit,
2693 e5a0f69f 2018-08-18 stsp s->done = 1;
2694 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2695 e5a0f69f 2018-08-18 stsp s->done = 0;
2696 e5a0f69f 2018-08-18 stsp if (thread_err)
2698 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2699 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2700 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2701 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2702 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2703 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2708 e5a0f69f 2018-08-18 stsp case 'B': {
2709 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2710 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2711 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2713 e5a0f69f 2018-08-18 stsp s->done = 1;
2714 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2715 e5a0f69f 2018-08-18 stsp s->done = 0;
2716 e5a0f69f 2018-08-18 stsp if (thread_err)
2718 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2719 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2720 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2721 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2722 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2723 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2724 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2725 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2730 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2731 15a94983 2018-12-23 stsp case '\r': {
2732 15a94983 2018-12-23 stsp struct got_object_id *id = NULL;
2733 15a94983 2018-12-23 stsp struct got_object_qid *pid;
2734 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2735 15a94983 2018-12-23 stsp id = get_selected_commit_id(s->blame.lines,
2736 15a94983 2018-12-23 stsp s->first_displayed_line, s->selected_line);
2737 3e9926ea 2019-01-04 stsp if (id == NULL)
2739 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, s->repo, id);
2742 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
2743 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
2744 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
2745 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
2746 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2747 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2748 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2749 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2752 15a94983 2018-12-23 stsp err = open_diff_view(diff_view, pid ? pid->id : NULL,
2753 15a94983 2018-12-23 stsp id, s->repo);
2754 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2755 e5a0f69f 2018-08-18 stsp if (err) {
2756 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2759 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
2760 669b5ffa 2018-10-07 stsp err = view_close_child(view);
2763 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
2764 669b5ffa 2018-10-07 stsp if (err) {
2765 669b5ffa 2018-10-07 stsp view_close(diff_view);
2768 51c68690 2018-11-07 stsp *focus_view = diff_view;
2769 51c68690 2018-11-07 stsp view->child_focussed = 1;
2771 669b5ffa 2018-10-07 stsp *new_view = diff_view;
2776 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2778 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2779 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2780 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2781 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2784 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2785 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2786 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2787 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2789 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2790 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2791 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2793 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2794 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2795 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2796 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2802 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2805 a70480e0 2018-06-23 stsp static const struct got_error *
2806 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2808 a70480e0 2018-06-23 stsp const struct got_error *error;
2809 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2810 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2811 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2812 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2814 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2816 a70480e0 2018-06-23 stsp #ifndef PROFILE
2817 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2818 8e94dd5b 2019-01-04 stsp NULL) == -1)
2819 a70480e0 2018-06-23 stsp err(1, "pledge");
2822 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2823 a70480e0 2018-06-23 stsp switch (ch) {
2825 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2828 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2829 69069811 2018-08-02 stsp if (repo_path == NULL)
2830 69069811 2018-08-02 stsp err(1, "-r option");
2834 a70480e0 2018-06-23 stsp /* NOTREACHED */
2838 a70480e0 2018-06-23 stsp argc -= optind;
2839 a70480e0 2018-06-23 stsp argv += optind;
2841 69069811 2018-08-02 stsp if (argc == 1)
2842 69069811 2018-08-02 stsp path = argv[0];
2844 a70480e0 2018-06-23 stsp usage_blame();
2846 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2847 69069811 2018-08-02 stsp if (cwd == NULL) {
2848 69069811 2018-08-02 stsp error = got_error_from_errno();
2849 69069811 2018-08-02 stsp goto done;
2851 69069811 2018-08-02 stsp if (repo_path == NULL) {
2852 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2853 69069811 2018-08-02 stsp if (repo_path == NULL) {
2854 69069811 2018-08-02 stsp error = got_error_from_errno();
2855 69069811 2018-08-02 stsp goto done;
2859 8e94dd5b 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
2860 8e94dd5b 2019-01-04 stsp if (error)
2861 8e94dd5b 2019-01-04 stsp goto done;
2863 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2864 a70480e0 2018-06-23 stsp if (error != NULL)
2865 92205607 2019-01-04 stsp goto done;
2867 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2868 69069811 2018-08-02 stsp if (error != NULL)
2869 69069811 2018-08-02 stsp goto done;
2871 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2872 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2873 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2874 a70480e0 2018-06-23 stsp if (error != NULL)
2875 66b4983c 2018-06-23 stsp goto done;
2876 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2877 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2879 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
2880 15a94983 2018-12-23 stsp commit_id_str);
2882 a19e88aa 2018-06-23 stsp if (error != NULL)
2883 a19e88aa 2018-06-23 stsp goto done;
2885 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2886 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2887 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2888 e1cd8fed 2018-08-01 stsp goto done;
2890 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2891 7cbe629d 2018-08-04 stsp if (error)
2892 7cbe629d 2018-08-04 stsp goto done;
2893 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2895 69069811 2018-08-02 stsp free(repo_path);
2896 69069811 2018-08-02 stsp free(cwd);
2897 a70480e0 2018-06-23 stsp free(commit_id);
2899 a70480e0 2018-06-23 stsp got_repo_close(repo);
2900 a70480e0 2018-06-23 stsp return error;
2903 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2904 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2905 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2906 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2907 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2908 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2909 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2911 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2912 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2913 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2914 ffd1d5e5 2018-06-23 stsp int width, n;
2916 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2918 f7d12f7e 2018-08-01 stsp werase(view->window);
2920 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2921 ffd1d5e5 2018-06-23 stsp return NULL;
2923 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2925 ffd1d5e5 2018-06-23 stsp return err;
2926 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2927 a3404814 2018-09-02 stsp wstandout(view->window);
2928 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2929 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2930 a3404814 2018-09-02 stsp wstandend(view->window);
2931 2550e4c3 2018-07-13 stsp free(wline);
2932 2550e4c3 2018-07-13 stsp wline = NULL;
2933 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2934 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2935 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2936 ffd1d5e5 2018-06-23 stsp return NULL;
2937 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2939 ce52c690 2018-06-23 stsp return err;
2940 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2941 2550e4c3 2018-07-13 stsp free(wline);
2942 2550e4c3 2018-07-13 stsp wline = NULL;
2943 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2944 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2945 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2946 ffd1d5e5 2018-06-23 stsp return NULL;
2947 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2948 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2949 a1eca9bb 2018-06-23 stsp return NULL;
2951 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2952 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2953 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2954 0cf4efb1 2018-09-29 stsp if (view->focussed)
2955 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2956 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2958 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2959 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
2960 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2961 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2962 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2963 ffd1d5e5 2018-06-23 stsp return NULL;
2967 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2968 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2971 ffd1d5e5 2018-06-23 stsp while (te) {
2972 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2974 1d13200f 2018-07-12 stsp if (show_ids) {
2975 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2977 1d13200f 2018-07-12 stsp return got_error_from_errno();
2979 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2980 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2981 1d13200f 2018-07-12 stsp free(id_str);
2982 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2984 1d13200f 2018-07-12 stsp free(id_str);
2985 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2986 ffd1d5e5 2018-06-23 stsp if (err) {
2987 ffd1d5e5 2018-06-23 stsp free(line);
2990 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2991 0cf4efb1 2018-09-29 stsp if (view->focussed)
2992 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2993 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2995 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2996 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2997 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2998 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
2999 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3000 ffd1d5e5 2018-06-23 stsp free(line);
3001 2550e4c3 2018-07-13 stsp free(wline);
3002 2550e4c3 2018-07-13 stsp wline = NULL;
3004 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3005 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
3006 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3008 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3011 ffd1d5e5 2018-06-23 stsp return err;
3014 ffd1d5e5 2018-06-23 stsp static void
3015 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3016 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
3018 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
3021 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
3024 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3025 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
3026 ffd1d5e5 2018-06-23 stsp if (!isroot)
3027 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3031 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
3032 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
3033 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
3034 ffd1d5e5 2018-06-23 stsp prev = te;
3035 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3037 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
3038 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3040 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3041 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3044 768394f3 2019-01-24 stsp static int
3045 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3046 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
3047 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
3049 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
3050 ffd1d5e5 2018-06-23 stsp int n = 0;
3052 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
3053 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3055 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
3056 768394f3 2019-01-24 stsp last = last_displayed_entry;
3057 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
3058 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
3059 768394f3 2019-01-24 stsp if (last) {
3060 768394f3 2019-01-24 stsp *first_displayed_entry = next;
3061 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
3067 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3068 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
3069 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
3071 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
3072 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
3073 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
3075 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
3076 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
3078 ce52c690 2018-06-23 stsp len += strlen(te->name);
3080 ce52c690 2018-06-23 stsp *path = calloc(1, len);
3081 ffd1d5e5 2018-06-23 stsp if (path == NULL)
3082 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3084 ce52c690 2018-06-23 stsp (*path)[0] = '/';
3085 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
3086 d9765a41 2018-06-23 stsp while (pt) {
3087 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3088 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3089 cb2ebc8a 2018-06-23 stsp goto done;
3091 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
3092 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3093 cb2ebc8a 2018-06-23 stsp goto done;
3095 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3098 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3099 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3100 ce52c690 2018-06-23 stsp goto done;
3104 ce52c690 2018-06-23 stsp if (err) {
3105 ce52c690 2018-06-23 stsp free(*path);
3106 ce52c690 2018-06-23 stsp *path = NULL;
3108 ce52c690 2018-06-23 stsp return err;
3111 ce52c690 2018-06-23 stsp static const struct got_error *
3112 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3113 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3114 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
3116 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3117 ce52c690 2018-06-23 stsp char *path;
3118 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3120 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3122 ce52c690 2018-06-23 stsp return err;
3124 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3125 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3126 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3128 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
3129 e5a0f69f 2018-08-18 stsp if (err) {
3130 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3131 e5a0f69f 2018-08-18 stsp free(path);
3133 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3134 69efd4c4 2018-07-18 stsp return err;
3137 69efd4c4 2018-07-18 stsp static const struct got_error *
3138 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3139 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3140 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
3142 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3143 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3144 69efd4c4 2018-07-18 stsp char *path;
3146 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3147 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3148 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3150 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3152 69efd4c4 2018-07-18 stsp return err;
3154 23721109 2018-10-22 stsp err = open_log_view(log_view, commit_id, repo, path, 0);
3156 e5a0f69f 2018-08-18 stsp view_close(log_view);
3158 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3159 cb2ebc8a 2018-06-23 stsp free(path);
3160 cb2ebc8a 2018-06-23 stsp return err;
3163 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3164 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3165 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
3167 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3168 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3169 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3171 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3173 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3174 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3175 ffd1d5e5 2018-06-23 stsp goto done;
3177 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3178 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
3179 ffd1d5e5 2018-06-23 stsp goto done;
3182 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3183 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3184 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3185 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3186 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3187 6484ec90 2018-09-29 stsp err = got_error_from_errno();
3188 6484ec90 2018-09-29 stsp goto done;
3190 fb2756b9 2018-08-04 stsp s->repo = repo;
3192 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
3193 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
3194 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
3196 ad80ab7b 2018-08-04 stsp free(commit_id_str);
3197 6484ec90 2018-09-29 stsp if (err) {
3198 fb2756b9 2018-08-04 stsp free(s->tree_label);
3199 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3201 ad80ab7b 2018-08-04 stsp return err;
3204 e5a0f69f 2018-08-18 stsp static const struct got_error *
3205 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
3207 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3209 fb2756b9 2018-08-04 stsp free(s->tree_label);
3210 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3211 6484ec90 2018-09-29 stsp free(s->commit_id);
3212 6484ec90 2018-09-29 stsp s->commit_id = NULL;
3213 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
3214 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
3215 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
3216 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
3217 ad80ab7b 2018-08-04 stsp free(parent);
3220 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
3221 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
3222 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
3224 e5a0f69f 2018-08-18 stsp return NULL;
3227 ad80ab7b 2018-08-04 stsp static const struct got_error *
3228 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
3230 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
3231 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3232 e5a0f69f 2018-08-18 stsp char *parent_path;
3234 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
3236 e5a0f69f 2018-08-18 stsp return err;
3238 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
3239 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
3240 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3241 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
3242 e5a0f69f 2018-08-18 stsp free(parent_path);
3244 669b5ffa 2018-10-07 stsp view_vborder(view);
3245 e5a0f69f 2018-08-18 stsp return err;
3248 e5a0f69f 2018-08-18 stsp static const struct got_error *
3249 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3250 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3252 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3253 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
3254 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
3255 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
3257 e5a0f69f 2018-08-18 stsp switch (ch) {
3259 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
3262 669b5ffa 2018-10-07 stsp if (!s->selected_entry)
3264 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
3265 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
3266 669b5ffa 2018-10-07 stsp err = log_tree_entry(&log_view, begin_x,
3267 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
3268 669b5ffa 2018-10-07 stsp s->commit_id, s->repo);
3269 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3270 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3272 669b5ffa 2018-10-07 stsp return err;
3273 669b5ffa 2018-10-07 stsp err = view_set_child(view, log_view);
3274 669b5ffa 2018-10-07 stsp if (err) {
3275 669b5ffa 2018-10-07 stsp view_close(log_view);
3278 669b5ffa 2018-10-07 stsp *focus_view = log_view;
3279 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3281 669b5ffa 2018-10-07 stsp *new_view = log_view;
3284 e5a0f69f 2018-08-18 stsp case KEY_UP:
3285 bf979164 2019-01-24 stsp if (s->selected > 0) {
3286 e5a0f69f 2018-08-18 stsp s->selected--;
3287 bf979164 2019-01-24 stsp if (s->selected == 0)
3290 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3292 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
3293 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
3295 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
3296 67cc7991 2019-01-24 stsp tree_scroll_up(&s->first_displayed_entry,
3297 67cc7991 2019-01-24 stsp MAX(0, view->nlines - 4 - s->selected), s->entries,
3298 67cc7991 2019-01-24 stsp s->tree == s->root);
3299 8a0ead39 2018-11-03 stsp s->selected = 0;
3300 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
3301 67cc7991 2019-01-24 stsp s->first_displayed_entry && s->tree != s->root)
3302 67cc7991 2019-01-24 stsp s->first_displayed_entry = NULL;
3305 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
3306 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
3307 e5a0f69f 2018-08-18 stsp s->selected++;
3310 768394f3 2019-01-24 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3311 768394f3 2019-01-24 stsp == NULL) {
3312 768394f3 2019-01-24 stsp /* can't scroll any further */
3315 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
3316 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
3318 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
3319 768394f3 2019-01-24 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3320 768394f3 2019-01-24 stsp == NULL) {
3321 768394f3 2019-01-24 stsp /* can't scroll any further; move cursor down */
3322 768394f3 2019-01-24 stsp if (s->selected < s->ndisplayed - 1)
3323 768394f3 2019-01-24 stsp s->selected = s->ndisplayed - 1;
3326 768394f3 2019-01-24 stsp nscrolled = tree_scroll_down(&s->first_displayed_entry,
3327 768394f3 2019-01-24 stsp view->nlines, s->last_displayed_entry, s->entries);
3328 768394f3 2019-01-24 stsp if (nscrolled < view->nlines) {
3329 768394f3 2019-01-24 stsp int ndisplayed = 0;
3330 768394f3 2019-01-24 stsp struct got_tree_entry *te;
3331 768394f3 2019-01-24 stsp te = s->first_displayed_entry;
3333 768394f3 2019-01-24 stsp ndisplayed++;
3334 768394f3 2019-01-24 stsp te = SIMPLEQ_NEXT(te, entry);
3335 768394f3 2019-01-24 stsp } while (te);
3336 768394f3 2019-01-24 stsp s->selected = ndisplayed - 1;
3339 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
3340 e5a0f69f 2018-08-18 stsp case '\r':
3341 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
3342 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3343 7837eeac 2018-09-24 stsp case KEY_BACKSPACE:
3344 e5a0f69f 2018-08-18 stsp /* user selected '..' */
3345 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
3347 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
3348 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
3350 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
3351 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
3352 e5a0f69f 2018-08-18 stsp s->entries =
3353 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3354 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
3355 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
3356 e5a0f69f 2018-08-18 stsp s->selected_entry =
3357 e5a0f69f 2018-08-18 stsp parent->selected_entry;
3358 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
3359 e5a0f69f 2018-08-18 stsp free(parent);
3360 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
3361 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3362 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
3363 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
3364 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
3367 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
3368 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
3369 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
3372 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
3373 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
3374 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
3375 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
3376 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
3377 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3378 e5a0f69f 2018-08-18 stsp s->tree = child;
3379 e5a0f69f 2018-08-18 stsp s->entries =
3380 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3381 e5a0f69f 2018-08-18 stsp s->selected = 0;
3382 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3383 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
3384 669b5ffa 2018-10-07 stsp struct tog_view *blame_view;
3385 669b5ffa 2018-10-07 stsp int begin_x = view_is_parent_view(view) ?
3386 669b5ffa 2018-10-07 stsp view_split_begin_x(view->begin_x) : 0;
3388 669b5ffa 2018-10-07 stsp err = blame_tree_entry(&blame_view, begin_x,
3389 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents, s->commit_id,
3393 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3394 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3396 669b5ffa 2018-10-07 stsp return err;
3397 669b5ffa 2018-10-07 stsp err = view_set_child(view, blame_view);
3398 669b5ffa 2018-10-07 stsp if (err) {
3399 669b5ffa 2018-10-07 stsp view_close(blame_view);
3402 669b5ffa 2018-10-07 stsp *focus_view = blame_view;
3403 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3405 669b5ffa 2018-10-07 stsp *new_view = blame_view;
3408 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3409 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3410 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3416 ffd1d5e5 2018-06-23 stsp return err;
3419 ffd1d5e5 2018-06-23 stsp __dead static void
3420 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3423 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3424 ffd1d5e5 2018-06-23 stsp getprogname());
3428 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3429 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3431 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3432 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3433 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3434 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3435 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3436 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3437 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3439 5221c383 2018-08-01 stsp struct tog_view *view;
3441 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3442 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3443 d188b9a6 2019-01-04 stsp NULL) == -1)
3444 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3447 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3448 ffd1d5e5 2018-06-23 stsp switch (ch) {
3450 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3454 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3458 ffd1d5e5 2018-06-23 stsp argc -= optind;
3459 ffd1d5e5 2018-06-23 stsp argv += optind;
3461 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3462 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
3463 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3464 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3465 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3466 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3467 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3468 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3470 ffd1d5e5 2018-06-23 stsp usage_log();
3472 d188b9a6 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
3473 d188b9a6 2019-01-04 stsp if (error)
3474 d188b9a6 2019-01-04 stsp return error;
3476 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3477 ffd1d5e5 2018-06-23 stsp free(repo_path);
3478 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3479 ffd1d5e5 2018-06-23 stsp return error;
3481 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
3482 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3484 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
3485 15a94983 2018-12-23 stsp commit_id_arg);
3486 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3487 ffd1d5e5 2018-06-23 stsp goto done;
3489 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3490 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3491 ffd1d5e5 2018-06-23 stsp goto done;
3493 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
3494 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
3495 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3496 ffd1d5e5 2018-06-23 stsp goto done;
3498 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3499 5221c383 2018-08-01 stsp if (view == NULL) {
3500 5221c383 2018-08-01 stsp error = got_error_from_errno();
3501 5221c383 2018-08-01 stsp goto done;
3503 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3504 ad80ab7b 2018-08-04 stsp if (error)
3505 ad80ab7b 2018-08-04 stsp goto done;
3506 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3508 ffd1d5e5 2018-06-23 stsp free(commit_id);
3509 ffd1d5e5 2018-06-23 stsp if (commit)
3510 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3512 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3514 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3515 ffd1d5e5 2018-06-23 stsp return error;
3518 5c5136c5 2018-05-20 stsp static void
3519 9f7d7167 2018-04-29 stsp init_curses(void)
3521 9f7d7167 2018-04-29 stsp initscr();
3523 fd823528 2018-10-22 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3526 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3527 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3528 1f475ad8 2018-05-10 stsp curs_set(0);
3529 25791caa 2018-10-24 stsp signal(SIGWINCH, tog_sigwinch);
3532 4ed7e80c 2018-05-20 stsp __dead static void
3533 9f7d7167 2018-04-29 stsp usage(void)
3537 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3538 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3539 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3540 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3541 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3546 c2301be8 2018-04-30 stsp static char **
3547 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3549 c2301be8 2018-04-30 stsp char **argv;
3550 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3552 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3553 c2301be8 2018-04-30 stsp if (argv == NULL)
3554 c2301be8 2018-04-30 stsp err(1, "calloc");
3555 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3556 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3557 c2301be8 2018-04-30 stsp err(1, "calloc");
3558 c2301be8 2018-04-30 stsp if (arg1) {
3559 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3560 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3561 c2301be8 2018-04-30 stsp err(1, "calloc");
3564 c2301be8 2018-04-30 stsp return argv;
3568 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3570 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3571 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3572 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3573 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3575 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
3577 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3578 9f7d7167 2018-04-29 stsp switch (ch) {
3580 9f7d7167 2018-04-29 stsp hflag = 1;
3584 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3588 9f7d7167 2018-04-29 stsp argc -= optind;
3589 9f7d7167 2018-04-29 stsp argv += optind;
3590 9f7d7167 2018-04-29 stsp optind = 0;
3591 c2301be8 2018-04-30 stsp optreset = 1;
3593 c2301be8 2018-04-30 stsp if (argc == 0) {
3594 f29d3e89 2018-06-23 stsp if (hflag)
3596 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3597 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3598 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3603 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3604 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3605 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3606 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3607 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3608 9f7d7167 2018-04-29 stsp if (hflag)
3609 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3613 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3614 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3615 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3616 c2301be8 2018-04-30 stsp if (repo_path) {
3617 c2301be8 2018-04-30 stsp struct got_repository *repo;
3618 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3619 c2301be8 2018-04-30 stsp if (error == NULL)
3620 c2301be8 2018-04-30 stsp got_repo_close(repo);
3622 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3623 c2301be8 2018-04-30 stsp if (error) {
3624 f29d3e89 2018-06-23 stsp if (hflag) {
3625 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3626 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3630 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3631 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3632 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3633 ad7de8d9 2018-04-30 stsp free(repo_path);
3636 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3637 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3639 c2301be8 2018-04-30 stsp free(repo_path);
3643 5c5136c5 2018-05-20 stsp init_curses();
3645 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3646 9f7d7167 2018-04-29 stsp if (error)
3647 9f7d7167 2018-04-29 stsp goto done;
3650 c2301be8 2018-04-30 stsp free(cmd_argv);
3651 9f7d7167 2018-04-29 stsp if (error)
3652 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);