2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
21 31120ada 2018-04-30 stsp #include <errno.h>
22 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
23 9f7d7167 2018-04-29 stsp #include <curses.h>
24 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
25 9f7d7167 2018-04-29 stsp #include <panel.h>
26 9f7d7167 2018-04-29 stsp #include <locale.h>
27 9f7d7167 2018-04-29 stsp #include <stdlib.h>
28 26ed57b2 2018-05-19 stsp #include <stdio.h>
29 9f7d7167 2018-04-29 stsp #include <getopt.h>
30 9f7d7167 2018-04-29 stsp #include <string.h>
31 9f7d7167 2018-04-29 stsp #include <err.h>
32 80ddbec8 2018-04-29 stsp #include <unistd.h>
33 26ed57b2 2018-05-19 stsp #include <util.h>
34 26ed57b2 2018-05-19 stsp #include <limits.h>
35 61e69b96 2018-05-20 stsp #include <wchar.h>
36 788c352e 2018-06-16 stsp #include <time.h>
37 84451b3e 2018-07-10 stsp #include <pthread.h>
38 5036bf37 2018-09-24 stsp #include <libgen.h>
40 9f7d7167 2018-04-29 stsp #include "got_error.h"
41 80ddbec8 2018-04-29 stsp #include "got_object.h"
42 80ddbec8 2018-04-29 stsp #include "got_reference.h"
43 80ddbec8 2018-04-29 stsp #include "got_repository.h"
44 80ddbec8 2018-04-29 stsp #include "got_diff.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
47 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
48 a70480e0 2018-06-23 stsp #include "got_blame.h"
49 c2db6724 2019-01-04 stsp #include "got_privsep.h"
50 b7165be3 2019-02-05 stsp #include "got_worktree.h"
53 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
61 9f7d7167 2018-04-29 stsp #ifndef nitems
62 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
65 9f7d7167 2018-04-29 stsp struct tog_cmd {
66 c2301be8 2018-04-30 stsp const char *name;
67 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
68 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
69 c2301be8 2018-04-30 stsp const char *descr;
72 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
73 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
74 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
75 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
76 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
78 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
79 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
80 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
81 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
83 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
84 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
85 9f7d7167 2018-04-29 stsp "show repository history" },
86 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
87 9f7d7167 2018-04-29 stsp "compare files and directories" },
88 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
89 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
90 ffd1d5e5 2018-06-23 stsp { "tree", cmd_tree, usage_tree,
91 ffd1d5e5 2018-06-23 stsp "browse trees in repository" },
94 d6b05b5b 2018-08-04 stsp enum tog_view_type {
95 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
96 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
97 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
98 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
101 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
102 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
103 ba4f502b 2018-08-04 stsp struct got_object_id *id;
104 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
107 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
108 ba4f502b 2018-08-04 stsp struct commit_queue {
109 ba4f502b 2018-08-04 stsp int ncommits;
110 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
113 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
114 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
116 15a087fe 2019-02-21 stsp int first_displayed_line;
117 15a087fe 2019-02-21 stsp int last_displayed_line;
119 15a087fe 2019-02-21 stsp int diff_context;
120 15a087fe 2019-02-21 stsp struct got_repository *repo;
121 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
123 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
124 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
127 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
129 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
130 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
131 1a76625f 2018-10-22 stsp int commits_needed;
132 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
133 1a76625f 2018-10-22 stsp struct commit_queue *commits;
134 1a76625f 2018-10-22 stsp const char *in_repo_path;
135 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
136 1a76625f 2018-10-22 stsp struct got_repository *repo;
137 1a76625f 2018-10-22 stsp int log_complete;
138 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
139 1a76625f 2018-10-22 stsp struct tog_view *view;
140 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
141 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
144 1a76625f 2018-10-22 stsp struct tog_log_view_state {
145 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
146 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
147 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
148 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
149 b01e7d3b 2018-08-04 stsp int selected;
150 b01e7d3b 2018-08-04 stsp char *in_repo_path;
151 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
152 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
153 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
154 1a76625f 2018-10-22 stsp sig_atomic_t quit;
155 1a76625f 2018-10-22 stsp pthread_t thread;
156 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
159 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
160 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
161 e9424729 2018-08-04 stsp int nlines;
163 e9424729 2018-08-04 stsp struct tog_view *view;
164 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
168 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
169 e9424729 2018-08-04 stsp const char *path;
170 e9424729 2018-08-04 stsp struct got_repository *repo;
171 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
172 e9424729 2018-08-04 stsp int *complete;
175 e9424729 2018-08-04 stsp struct tog_blame {
177 e9424729 2018-08-04 stsp size_t filesize;
178 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
179 6fcac457 2018-11-19 stsp int nlines;
180 e9424729 2018-08-04 stsp pthread_t thread;
181 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
182 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
183 e9424729 2018-08-04 stsp const char *path;
186 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
187 7cbe629d 2018-08-04 stsp int first_displayed_line;
188 7cbe629d 2018-08-04 stsp int last_displayed_line;
189 7cbe629d 2018-08-04 stsp int selected_line;
190 7cbe629d 2018-08-04 stsp int blame_complete;
193 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
194 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
195 e5a0f69f 2018-08-18 stsp char *path;
196 7cbe629d 2018-08-04 stsp struct got_repository *repo;
197 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
198 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
199 e9424729 2018-08-04 stsp struct tog_blame blame;
202 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
203 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
204 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
205 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
206 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
207 ad80ab7b 2018-08-04 stsp int selected;
210 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
212 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
213 ad80ab7b 2018-08-04 stsp char *tree_label;
214 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
215 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
216 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
217 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
218 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
219 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
220 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
221 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
222 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
223 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
224 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
228 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
230 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
231 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
232 669b5ffa 2018-10-07 stsp * there is enough screen estate.
234 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
235 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
237 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
238 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
239 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
241 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
242 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
244 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
246 cc3c9aac 2018-08-01 stsp struct tog_view {
247 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
248 26ed57b2 2018-05-19 stsp WINDOW *window;
249 26ed57b2 2018-05-19 stsp PANEL *panel;
250 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
251 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
252 1004088d 2018-09-29 stsp int focussed;
253 669b5ffa 2018-10-07 stsp struct tog_view *parent;
254 669b5ffa 2018-10-07 stsp struct tog_view *child;
255 669b5ffa 2018-10-07 stsp int child_focussed;
257 5dc9f4bc 2018-08-04 stsp /* type-specific state */
258 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
260 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
261 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
262 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
263 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
266 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
267 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
268 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
269 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
272 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
273 fb872ab2 2019-02-21 stsp struct got_object_id *, struct got_object_id *, struct tog_view *,
274 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
275 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
276 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
277 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
278 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
280 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
281 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *,
282 8b473291 2019-02-21 stsp struct got_repository *, const char *, int);
283 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
284 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
285 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
286 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
288 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
289 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *, struct got_repository *);
290 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
291 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
292 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
293 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
295 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
296 8b473291 2019-02-21 stsp struct got_tree_object *, struct got_object_id *,
297 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
298 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
299 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
300 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
301 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
303 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
305 25791caa 2018-10-24 stsp static void
306 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
308 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
311 e5a0f69f 2018-08-18 stsp static const struct got_error *
312 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
314 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
316 669b5ffa 2018-10-07 stsp if (view->child) {
317 669b5ffa 2018-10-07 stsp view_close(view->child);
318 669b5ffa 2018-10-07 stsp view->child = NULL;
320 e5a0f69f 2018-08-18 stsp if (view->close)
321 e5a0f69f 2018-08-18 stsp err = view->close(view);
322 ea5e7bb5 2018-08-01 stsp if (view->panel)
323 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
324 ea5e7bb5 2018-08-01 stsp if (view->window)
325 ea5e7bb5 2018-08-01 stsp delwin(view->window);
326 ea5e7bb5 2018-08-01 stsp free(view);
327 e5a0f69f 2018-08-18 stsp return err;
330 ea5e7bb5 2018-08-01 stsp static struct tog_view *
331 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
332 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
334 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
336 ea5e7bb5 2018-08-01 stsp if (view == NULL)
337 ea5e7bb5 2018-08-01 stsp return NULL;
339 d6b05b5b 2018-08-04 stsp view->type = type;
340 f7d12f7e 2018-08-01 stsp view->lines = LINES;
341 f7d12f7e 2018-08-01 stsp view->cols = COLS;
342 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
343 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
344 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
345 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
346 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
347 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
348 96a765a8 2018-08-04 stsp view_close(view);
349 ea5e7bb5 2018-08-01 stsp return NULL;
351 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
352 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
353 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
354 96a765a8 2018-08-04 stsp view_close(view);
355 ea5e7bb5 2018-08-01 stsp return NULL;
358 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
359 ea5e7bb5 2018-08-01 stsp return view;
363 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
365 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
367 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
370 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
372 5c60c32a 2018-10-18 stsp static const struct got_error *
373 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
375 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
377 5c60c32a 2018-10-18 stsp view->begin_y = 0;
378 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
379 5c60c32a 2018-10-18 stsp view->nlines = LINES;
380 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
381 5c60c32a 2018-10-18 stsp view->lines = LINES;
382 5c60c32a 2018-10-18 stsp view->cols = COLS;
383 5c60c32a 2018-10-18 stsp err = view_resize(view);
385 5c60c32a 2018-10-18 stsp return err;
387 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
388 5c60c32a 2018-10-18 stsp return got_error_from_errno();
390 5c60c32a 2018-10-18 stsp return NULL;
393 5c60c32a 2018-10-18 stsp static const struct got_error *
394 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
396 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
398 5c60c32a 2018-10-18 stsp view->begin_x = 0;
399 5c60c32a 2018-10-18 stsp view->begin_y = 0;
400 5c60c32a 2018-10-18 stsp view->nlines = LINES;
401 5c60c32a 2018-10-18 stsp view->ncols = COLS;
402 5c60c32a 2018-10-18 stsp view->lines = LINES;
403 5c60c32a 2018-10-18 stsp view->cols = COLS;
404 5c60c32a 2018-10-18 stsp err = view_resize(view);
406 5c60c32a 2018-10-18 stsp return err;
408 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
409 5c60c32a 2018-10-18 stsp return got_error_from_errno();
411 5c60c32a 2018-10-18 stsp return NULL;
415 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
417 5c60c32a 2018-10-18 stsp return view->parent == NULL;
420 4d8c2215 2018-08-19 stsp static const struct got_error *
421 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
423 f7d12f7e 2018-08-01 stsp int nlines, ncols;
425 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
426 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
428 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
430 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
431 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
433 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
435 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
436 0cf4efb1 2018-09-29 stsp return got_error_from_errno();
437 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
438 a6d7eb8d 2018-10-24 stsp return got_error_from_errno();
439 25791caa 2018-10-24 stsp wclear(view->window);
441 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
442 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
443 0cf4efb1 2018-09-29 stsp view->lines = LINES;
444 0cf4efb1 2018-09-29 stsp view->cols = COLS;
446 6e3e5d9c 2018-10-18 stsp if (view->child) {
447 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
448 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
449 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
450 5c60c32a 2018-10-18 stsp if (view->child->focussed)
451 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
453 5c60c32a 2018-10-18 stsp show_panel(view->panel);
455 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
456 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
460 5c60c32a 2018-10-18 stsp return NULL;
463 669b5ffa 2018-10-07 stsp static const struct got_error *
464 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
466 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
468 669b5ffa 2018-10-07 stsp if (view->child == NULL)
469 669b5ffa 2018-10-07 stsp return NULL;
471 669b5ffa 2018-10-07 stsp err = view_close(view->child);
472 669b5ffa 2018-10-07 stsp view->child = NULL;
473 669b5ffa 2018-10-07 stsp return err;
476 669b5ffa 2018-10-07 stsp static const struct got_error *
477 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
479 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
481 669b5ffa 2018-10-07 stsp view->child = child;
482 669b5ffa 2018-10-07 stsp child->parent = view;
483 669b5ffa 2018-10-07 stsp return err;
487 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
489 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
493 34bc9ec9 2019-02-22 stsp * Erase all content of the view. Can be used to "flash" the view because
494 34bc9ec9 2019-02-22 stsp * the view loop will redraw it quickly, providing a more subtle visual
495 34bc9ec9 2019-02-22 stsp * effect than curs_flash(3) would provide.
497 25791caa 2018-10-24 stsp static void
498 34bc9ec9 2019-02-22 stsp view_flash(struct tog_view *view)
500 34bc9ec9 2019-02-22 stsp werase(view->window);
501 34bc9ec9 2019-02-22 stsp update_panels();
502 34bc9ec9 2019-02-22 stsp doupdate();
505 34bc9ec9 2019-02-22 stsp static void
506 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
508 25791caa 2018-10-24 stsp int cols, lines;
509 25791caa 2018-10-24 stsp struct winsize size;
511 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
512 25791caa 2018-10-24 stsp cols = 80; /* Default */
513 25791caa 2018-10-24 stsp lines = 24;
515 25791caa 2018-10-24 stsp cols = size.ws_col;
516 25791caa 2018-10-24 stsp lines = size.ws_row;
518 25791caa 2018-10-24 stsp resize_term(lines, cols);
521 0cf4efb1 2018-09-29 stsp static const struct got_error *
522 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
523 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
524 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
526 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
527 669b5ffa 2018-10-07 stsp struct tog_view *v;
528 1a76625f 2018-10-22 stsp int ch, errcode;
530 e5a0f69f 2018-08-18 stsp *new = NULL;
531 e5a0f69f 2018-08-18 stsp *dead = NULL;
532 0cf4efb1 2018-09-29 stsp *focus = NULL;
534 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
535 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
536 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
537 1a76625f 2018-10-22 stsp if (errcode)
538 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
539 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
540 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
541 1a76625f 2018-10-22 stsp if (errcode)
542 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
543 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
545 25791caa 2018-10-24 stsp if (tog_sigwinch_received) {
546 25791caa 2018-10-24 stsp tog_resizeterm();
547 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
548 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
549 25791caa 2018-10-24 stsp err = view_resize(v);
551 25791caa 2018-10-24 stsp return err;
552 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
554 25791caa 2018-10-24 stsp return err;
558 e5a0f69f 2018-08-18 stsp switch (ch) {
562 669b5ffa 2018-10-07 stsp if (view->child) {
563 669b5ffa 2018-10-07 stsp *focus = view->child;
564 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
565 669b5ffa 2018-10-07 stsp } else if (view->parent) {
566 669b5ffa 2018-10-07 stsp *focus = view->parent;
567 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 0;
571 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
572 e5a0f69f 2018-08-18 stsp *dead = view;
578 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
579 669b5ffa 2018-10-07 stsp if (view->child == NULL)
581 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view->child)) {
582 669b5ffa 2018-10-07 stsp *focus = view->child;
583 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
584 669b5ffa 2018-10-07 stsp err = view_fullscreen(view->child);
586 669b5ffa 2018-10-07 stsp err = view_splitscreen(view->child);
589 669b5ffa 2018-10-07 stsp err = view->child->input(new, dead, focus,
590 669b5ffa 2018-10-07 stsp view->child, KEY_RESIZE);
592 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view)) {
593 669b5ffa 2018-10-07 stsp *focus = view;
594 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 1;
595 669b5ffa 2018-10-07 stsp err = view_fullscreen(view);
597 669b5ffa 2018-10-07 stsp err = view_splitscreen(view);
601 669b5ffa 2018-10-07 stsp err = view->input(new, dead, focus, view,
602 669b5ffa 2018-10-07 stsp KEY_RESIZE);
605 0cf4efb1 2018-09-29 stsp case KEY_RESIZE:
608 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
612 e5a0f69f 2018-08-18 stsp return err;
616 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
618 0cf4efb1 2018-09-29 stsp PANEL *panel;
619 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
621 669b5ffa 2018-10-07 stsp if (view->parent)
622 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
624 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
625 0cf4efb1 2018-09-29 stsp if (panel == NULL)
628 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
629 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
630 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
634 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
636 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
637 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
639 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
641 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
644 669b5ffa 2018-10-07 stsp return view->focussed;
647 bcbd79e2 2018-08-19 stsp static const struct got_error *
648 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
650 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
651 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
652 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
653 fd823528 2018-10-22 stsp int fast_refresh = 10;
654 1a76625f 2018-10-22 stsp int done = 0, errcode;
656 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
657 1a76625f 2018-10-22 stsp if (errcode)
658 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
660 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
661 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
663 a81bf10d 2018-09-29 stsp main_view = view;
664 1004088d 2018-09-29 stsp view->focussed = 1;
665 878940b7 2018-09-29 stsp err = view->show(view);
667 0cf4efb1 2018-09-29 stsp return err;
668 0cf4efb1 2018-09-29 stsp update_panels();
669 0cf4efb1 2018-09-29 stsp doupdate();
670 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
671 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
672 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
673 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
675 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
676 e4197bf9 2018-08-18 stsp view, &views);
679 e5a0f69f 2018-08-18 stsp if (dead_view) {
680 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
682 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
683 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
684 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
685 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
686 669b5ffa 2018-10-07 stsp prev = view->parent;
688 669b5ffa 2018-10-07 stsp if (dead_view->parent)
689 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
691 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
693 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
694 a81bf10d 2018-09-29 stsp if (err || dead_view == main_view)
697 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
698 0cf4efb1 2018-09-29 stsp if (focus_view)
699 0cf4efb1 2018-09-29 stsp view = focus_view;
700 669b5ffa 2018-10-07 stsp else if (prev)
701 669b5ffa 2018-10-07 stsp view = prev;
702 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
703 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
704 0cf4efb1 2018-09-29 stsp tog_view_list_head);
706 0cf4efb1 2018-09-29 stsp view = NULL;
707 669b5ffa 2018-10-07 stsp if (view) {
708 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
709 669b5ffa 2018-10-07 stsp focus_view = view->child;
711 669b5ffa 2018-10-07 stsp focus_view = view;
715 bcbd79e2 2018-08-19 stsp if (new_view) {
716 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
717 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
718 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
719 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
721 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
722 86c66b02 2018-10-18 stsp err = view_close(v);
727 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
728 fed7eaa8 2018-10-24 stsp view = new_view;
729 878940b7 2018-09-29 stsp if (focus_view == NULL)
730 878940b7 2018-09-29 stsp focus_view = new_view;
732 0cf4efb1 2018-09-29 stsp if (focus_view) {
733 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
735 0cf4efb1 2018-09-29 stsp view->focussed = 0;
736 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
737 0cf4efb1 2018-09-29 stsp view = focus_view;
738 878940b7 2018-09-29 stsp if (new_view)
739 878940b7 2018-09-29 stsp show_panel(new_view->panel);
740 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
741 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
743 669b5ffa 2018-10-07 stsp if (view) {
744 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
745 758194b5 2018-10-24 stsp view->focussed = 1;
746 758194b5 2018-10-24 stsp show_panel(view->panel);
747 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
748 758194b5 2018-10-24 stsp show_panel(view->child->panel);
749 1a76625f 2018-10-22 stsp focus_view = view;
751 669b5ffa 2018-10-07 stsp if (view->parent) {
752 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
756 669b5ffa 2018-10-07 stsp err = view->show(view);
759 669b5ffa 2018-10-07 stsp if (view->child) {
760 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
764 1a76625f 2018-10-22 stsp update_panels();
765 1a76625f 2018-10-22 stsp doupdate();
769 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
770 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
771 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
772 e5a0f69f 2018-08-18 stsp view_close(view);
775 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
776 1a76625f 2018-10-22 stsp if (errcode)
777 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
779 e5a0f69f 2018-08-18 stsp return err;
782 4ed7e80c 2018-05-20 stsp __dead static void
783 9f7d7167 2018-04-29 stsp usage_log(void)
786 c70c5802 2018-08-01 stsp fprintf(stderr,
787 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
788 9f7d7167 2018-04-29 stsp getprogname());
792 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
793 80ddbec8 2018-04-29 stsp static const struct got_error *
794 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
796 00dfcb92 2018-06-11 stsp char *vis = NULL;
797 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
799 963b370f 2018-05-20 stsp *ws = NULL;
800 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
801 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
802 00dfcb92 2018-06-11 stsp int vislen;
803 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
804 00dfcb92 2018-06-11 stsp return got_error_from_errno();
806 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
807 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
809 00dfcb92 2018-06-11 stsp return err;
810 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
811 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
812 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
817 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
818 a7f50699 2018-06-11 stsp if (*ws == NULL) {
819 a7f50699 2018-06-11 stsp err = got_error_from_errno();
823 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
824 963b370f 2018-05-20 stsp err = got_error_from_errno();
829 963b370f 2018-05-20 stsp *ws = NULL;
832 963b370f 2018-05-20 stsp return err;
835 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
836 963b370f 2018-05-20 stsp static const struct got_error *
837 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
839 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
840 963b370f 2018-05-20 stsp int cols = 0;
841 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
842 963b370f 2018-05-20 stsp size_t wlen;
845 963b370f 2018-05-20 stsp *wlinep = NULL;
846 b700b5d6 2018-07-10 stsp *widthp = 0;
848 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
850 963b370f 2018-05-20 stsp return err;
853 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
854 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
855 963b370f 2018-05-20 stsp switch (width) {
861 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
862 b700b5d6 2018-07-10 stsp cols += width;
866 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
867 67ceb59d 2019-03-03 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
871 963b370f 2018-05-20 stsp err = got_error_from_errno();
875 963b370f 2018-05-20 stsp wline[i] = L'\0';
876 b700b5d6 2018-07-10 stsp if (widthp)
877 b700b5d6 2018-07-10 stsp *widthp = cols;
880 963b370f 2018-05-20 stsp free(wline);
882 963b370f 2018-05-20 stsp *wlinep = wline;
883 963b370f 2018-05-20 stsp return err;
886 8b473291 2019-02-21 stsp static const struct got_error*
887 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
888 8b473291 2019-02-21 stsp struct got_object_id *id)
890 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
891 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
893 8b473291 2019-02-21 stsp const char *name;
895 8b473291 2019-02-21 stsp *refs_str = NULL;
897 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
898 8b473291 2019-02-21 stsp if (got_object_id_cmp(re->id, id) != 0)
900 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
901 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
903 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
905 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
907 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
909 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
911 8b473291 2019-02-21 stsp s = *refs_str;
912 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
913 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
914 8b473291 2019-02-21 stsp err = got_error_from_errno();
916 8b473291 2019-02-21 stsp *refs_str = NULL;
922 8b473291 2019-02-21 stsp return err;
925 963b370f 2018-05-20 stsp static const struct got_error *
926 5813d178 2019-03-09 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
928 5813d178 2019-03-09 stsp char *smallerthan, *at;
930 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
931 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
932 5813d178 2019-03-09 stsp author = smallerthan + 1;
933 5813d178 2019-03-09 stsp at = strchr(author, '@');
935 5813d178 2019-03-09 stsp *at = '\0';
936 5813d178 2019-03-09 stsp return format_line(wauthor, author_width, author, limit);
939 5813d178 2019-03-09 stsp static const struct got_error *
940 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
941 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
942 5813d178 2019-03-09 stsp int author_display_cols)
944 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
945 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
946 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
947 5813d178 2019-03-09 stsp char *author = NULL;
948 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
949 bb737323 2018-05-20 stsp int author_width, logmsg_width;
950 5813d178 2019-03-09 stsp char *newline, *line = NULL;
951 bb737323 2018-05-20 stsp int col, limit;
952 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
953 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
954 ccb26ccd 2018-11-05 stsp struct tm tm;
955 45d799e2 2018-12-23 stsp time_t committer_time;
957 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
958 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
959 ccb26ccd 2018-11-05 stsp return got_error_from_errno();
960 ccb26ccd 2018-11-05 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
961 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
962 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
964 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
965 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
967 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
968 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
969 b39d25c7 2018-07-10 stsp col = limit + 1;
970 b39d25c7 2018-07-10 stsp if (col > avail)
973 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
974 5813d178 2019-03-09 stsp if (author == NULL) {
975 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
978 5813d178 2019-03-09 stsp err = format_author(&wauthor, &author_width, author, avail - col);
981 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
982 bb737323 2018-05-20 stsp col += author_width;
983 5813d178 2019-03-09 stsp while (col <= avail && author_width < author_display_cols + 2) {
984 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
986 bb737323 2018-05-20 stsp author_width++;
988 9c2eaf34 2018-05-20 stsp if (col > avail)
991 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
992 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
993 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
996 bb737323 2018-05-20 stsp logmsg = logmsg0;
997 bb737323 2018-05-20 stsp while (*logmsg == '\n')
999 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1000 bb737323 2018-05-20 stsp if (newline)
1001 bb737323 2018-05-20 stsp *newline = '\0';
1002 bb737323 2018-05-20 stsp limit = avail - col;
1003 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1005 bb737323 2018-05-20 stsp goto done;
1006 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1007 bb737323 2018-05-20 stsp col += logmsg_width;
1008 bb737323 2018-05-20 stsp while (col <= avail) {
1009 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1013 80ddbec8 2018-04-29 stsp free(logmsg0);
1014 bb737323 2018-05-20 stsp free(wlogmsg);
1015 5813d178 2019-03-09 stsp free(author);
1016 bb737323 2018-05-20 stsp free(wauthor);
1017 80ddbec8 2018-04-29 stsp free(line);
1018 80ddbec8 2018-04-29 stsp return err;
1021 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1022 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1023 899d86c2 2018-05-10 stsp struct got_object_id *id)
1025 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1027 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1028 80ddbec8 2018-04-29 stsp if (entry == NULL)
1029 899d86c2 2018-05-10 stsp return NULL;
1031 899d86c2 2018-05-10 stsp entry->id = id;
1032 99db9666 2018-05-07 stsp entry->commit = commit;
1033 899d86c2 2018-05-10 stsp return entry;
1036 99db9666 2018-05-07 stsp static void
1037 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1039 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1041 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1042 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1043 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1044 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1045 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1046 99db9666 2018-05-07 stsp free(entry);
1049 99db9666 2018-05-07 stsp static void
1050 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1052 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1053 99db9666 2018-05-07 stsp pop_commit(commits);
1056 c4972b91 2018-05-07 stsp static const struct got_error *
1057 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1058 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
1060 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1061 93e45b7c 2018-09-24 stsp int nqueued = 0;
1064 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1065 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1066 1a76625f 2018-10-22 stsp * while updating the display.
1068 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
1069 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1070 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1071 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1072 1a76625f 2018-10-22 stsp int errcode;
1074 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1075 9ba79e04 2018-06-11 stsp if (err) {
1076 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1078 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
1079 93e45b7c 2018-09-24 stsp minqueue, repo);
1081 ecb28ae0 2018-07-16 stsp return err;
1085 ecb28ae0 2018-07-16 stsp if (id == NULL)
1088 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1091 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1092 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1093 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
1097 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1098 1a76625f 2018-10-22 stsp if (errcode) {
1099 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1103 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1104 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1105 ecb28ae0 2018-07-16 stsp nqueued++;
1106 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1108 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1109 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1110 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1113 9ba79e04 2018-06-11 stsp return err;
1116 99db9666 2018-05-07 stsp static const struct got_error *
1117 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1119 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1120 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1122 9ba79e04 2018-06-11 stsp *head_id = NULL;
1124 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1126 99db9666 2018-05-07 stsp return err;
1128 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1129 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1130 9ba79e04 2018-06-11 stsp if (err) {
1131 9ba79e04 2018-06-11 stsp *head_id = NULL;
1132 99db9666 2018-05-07 stsp return err;
1135 9ba79e04 2018-06-11 stsp return NULL;
1138 0553a4e3 2018-04-30 stsp static const struct got_error *
1139 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1140 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1141 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1142 8b473291 2019-02-21 stsp struct got_reflist_head *refs, const char *path, int commits_needed)
1144 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1145 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1146 ecb28ae0 2018-07-16 stsp int ncommits, width;
1147 5813d178 2019-03-09 stsp int author_cols = 10;
1148 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1149 8b473291 2019-02-21 stsp char *refs_str = NULL;
1150 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1152 e0d42f60 2018-07-22 stsp entry = first;
1153 e0d42f60 2018-07-22 stsp ncommits = 0;
1154 e0d42f60 2018-07-22 stsp while (entry) {
1155 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1156 e0d42f60 2018-07-22 stsp *selected = entry;
1159 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1160 e0d42f60 2018-07-22 stsp ncommits++;
1163 1a76625f 2018-10-22 stsp if (*selected) {
1164 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1166 ecb28ae0 2018-07-16 stsp return err;
1167 8b473291 2019-02-21 stsp if (refs) {
1168 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, (*selected)->id);
1170 8b473291 2019-02-21 stsp goto done;
1174 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1175 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1177 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1178 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1179 8b473291 2019-02-21 stsp commits_needed > 0 ? "loading... " :
1180 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1181 8b473291 2019-02-21 stsp err = got_error_from_errno();
1182 8b473291 2019-02-21 stsp goto done;
1185 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1186 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1187 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1188 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1189 1a76625f 2018-10-22 stsp err = got_error_from_errno();
1190 1a76625f 2018-10-22 stsp header = NULL;
1191 1a76625f 2018-10-22 stsp goto done;
1193 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1194 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1195 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1196 1a76625f 2018-10-22 stsp err = got_error_from_errno();
1197 1a76625f 2018-10-22 stsp header = NULL;
1198 1a76625f 2018-10-22 stsp goto done;
1200 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1202 1a76625f 2018-10-22 stsp goto done;
1204 2814baeb 2018-08-01 stsp werase(view->window);
1206 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1207 a3404814 2018-09-02 stsp wstandout(view->window);
1208 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1209 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1210 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1213 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1214 a3404814 2018-09-02 stsp wstandend(view->window);
1215 ecb28ae0 2018-07-16 stsp free(wline);
1216 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1217 1a76625f 2018-10-22 stsp goto done;
1219 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1220 899d86c2 2018-05-10 stsp entry = first;
1221 5813d178 2019-03-09 stsp ncommits = 0;
1222 5813d178 2019-03-09 stsp while (entry) {
1223 5813d178 2019-03-09 stsp char *author;
1224 5813d178 2019-03-09 stsp wchar_t *wauthor;
1225 5813d178 2019-03-09 stsp int width;
1226 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1228 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1229 5813d178 2019-03-09 stsp if (author == NULL) {
1230 5813d178 2019-03-09 stsp err = got_error_from_errno();
1231 5813d178 2019-03-09 stsp goto done;
1233 5813d178 2019-03-09 stsp err = format_author(&wauthor, &width, author, COLS);
1234 5813d178 2019-03-09 stsp if (author_cols < width)
1235 5813d178 2019-03-09 stsp author_cols = width;
1236 5813d178 2019-03-09 stsp free(wauthor);
1237 5813d178 2019-03-09 stsp free(author);
1238 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1241 5813d178 2019-03-09 stsp entry = first;
1242 899d86c2 2018-05-10 stsp *last = first;
1243 867c6645 2018-07-10 stsp ncommits = 0;
1244 899d86c2 2018-05-10 stsp while (entry) {
1245 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1247 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1248 2814baeb 2018-08-01 stsp wstandout(view->window);
1249 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1250 5813d178 2019-03-09 stsp author_cols);
1251 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1252 2814baeb 2018-08-01 stsp wstandend(view->window);
1255 0553a4e3 2018-04-30 stsp ncommits++;
1256 899d86c2 2018-05-10 stsp *last = entry;
1257 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1260 1a57306a 2018-09-02 stsp view_vborder(view);
1262 1a76625f 2018-10-22 stsp free(id_str);
1263 8b473291 2019-02-21 stsp free(refs_str);
1264 1a76625f 2018-10-22 stsp free(ncommits_str);
1265 1a76625f 2018-10-22 stsp free(header);
1266 80ddbec8 2018-04-29 stsp return err;
1269 07b55e75 2018-05-10 stsp static void
1270 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1271 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1272 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1274 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1275 07b55e75 2018-05-10 stsp int nscrolled = 0;
1277 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1278 34bc9ec9 2019-02-22 stsp if (*first_displayed_entry == entry) {
1279 34bc9ec9 2019-02-22 stsp view_flash(view);
1283 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1284 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1285 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1286 07b55e75 2018-05-10 stsp if (entry) {
1287 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1288 07b55e75 2018-05-10 stsp nscrolled++;
1293 aa075928 2018-05-10 stsp static const struct got_error *
1294 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1295 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1297 5e224a3e 2019-02-22 stsp int errcode;
1298 8a42fca8 2019-02-22 stsp int max_wait = 20;
1300 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1302 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1303 5e224a3e 2019-02-22 stsp if (*log_complete)
1306 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1307 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1308 7aafa0d1 2019-02-22 stsp if (errcode)
1309 7aafa0d1 2019-02-22 stsp return got_error_set_errno(errcode);
1310 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1311 7aafa0d1 2019-02-22 stsp if (errcode)
1312 7aafa0d1 2019-02-22 stsp return got_error_set_errno(errcode);
1313 7aafa0d1 2019-02-22 stsp pthread_yield();
1314 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1315 7aafa0d1 2019-02-22 stsp if (errcode)
1316 7aafa0d1 2019-02-22 stsp return got_error_set_errno(errcode);
1318 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1320 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1321 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1322 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1323 5e224a3e 2019-02-22 stsp * with few commits in history.
1325 7aafa0d1 2019-02-22 stsp return NULL;
1329 5e224a3e 2019-02-22 stsp return NULL;
1332 5e224a3e 2019-02-22 stsp static const struct got_error *
1333 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1334 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1335 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1336 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1337 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1339 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1340 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1341 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1343 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1344 5e224a3e 2019-02-22 stsp return NULL;
1346 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1347 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1349 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1350 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1352 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1353 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1354 5e224a3e 2019-02-22 stsp need_commits);
1356 5e224a3e 2019-02-22 stsp return err;
1360 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1361 34bc9ec9 2019-02-22 stsp if (pentry == NULL) {
1362 34bc9ec9 2019-02-22 stsp if (*log_complete)
1363 34bc9ec9 2019-02-22 stsp view_flash(view);
1367 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1369 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1370 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1372 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1373 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1375 dd0a52c1 2018-05-20 stsp return err;
1378 cd0acaa7 2018-05-20 stsp static const struct got_error *
1379 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1380 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1381 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1382 8b473291 2019-02-21 stsp struct got_repository *repo)
1384 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1385 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1386 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1388 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1389 15a94983 2018-12-23 stsp if (diff_view == NULL)
1390 15a94983 2018-12-23 stsp return got_error_from_errno();
1392 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1393 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1394 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1395 e5a0f69f 2018-08-18 stsp if (err == NULL)
1396 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1397 cd0acaa7 2018-05-20 stsp return err;
1400 80ddbec8 2018-04-29 stsp static const struct got_error *
1401 0cf4efb1 2018-09-29 stsp browse_commit(struct tog_view **new_view, int begin_x,
1402 8b473291 2019-02-21 stsp struct commit_queue_entry *entry, struct got_reflist_head *refs,
1403 8b473291 2019-02-21 stsp struct got_repository *repo)
1405 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1406 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1407 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1409 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1410 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1412 9343a5fb 2018-06-23 stsp return err;
1414 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1415 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1416 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1418 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1420 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1422 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1423 1a76625f 2018-10-22 stsp return err;
1426 1a76625f 2018-10-22 stsp static void *
1427 1a76625f 2018-10-22 stsp log_thread(void *arg)
1429 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1430 1a76625f 2018-10-22 stsp int errcode = 0;
1431 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1432 1a76625f 2018-10-22 stsp int done = 0;
1434 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1436 1a76625f 2018-10-22 stsp return (void *)err;
1438 1a76625f 2018-10-22 stsp while (!done && !err) {
1439 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1440 1a76625f 2018-10-22 stsp a->in_repo_path);
1441 1a76625f 2018-10-22 stsp if (err) {
1442 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1443 1a76625f 2018-10-22 stsp return (void *)err;
1444 1a76625f 2018-10-22 stsp err = NULL;
1446 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1447 1a76625f 2018-10-22 stsp a->commits_needed--;
1449 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1450 1a76625f 2018-10-22 stsp if (errcode)
1451 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
1454 1a76625f 2018-10-22 stsp a->log_complete = 1;
1455 1a76625f 2018-10-22 stsp else if (*a->quit) {
1457 1a76625f 2018-10-22 stsp a->log_complete = 1;
1458 1a76625f 2018-10-22 stsp } else if (*a->first_displayed_entry == NULL) {
1459 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1460 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1461 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1465 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1466 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1467 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1468 1a76625f 2018-10-22 stsp &tog_mutex);
1469 1a76625f 2018-10-22 stsp if (errcode)
1470 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1473 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1474 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1475 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1477 1a76625f 2018-10-22 stsp return (void *)err;
1480 1a76625f 2018-10-22 stsp static const struct got_error *
1481 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1483 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1484 1a76625f 2018-10-22 stsp int errcode;
1486 1a76625f 2018-10-22 stsp if (s->thread) {
1487 1a76625f 2018-10-22 stsp s->quit = 1;
1488 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1489 1a76625f 2018-10-22 stsp if (errcode)
1490 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1491 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1492 1a76625f 2018-10-22 stsp if (errcode)
1493 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1494 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1495 1a76625f 2018-10-22 stsp if (errcode)
1496 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1497 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1498 1a76625f 2018-10-22 stsp if (errcode)
1499 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1500 1a76625f 2018-10-22 stsp s->thread = NULL;
1503 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1504 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1505 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1507 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1508 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1509 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1512 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1513 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1514 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1517 9343a5fb 2018-06-23 stsp return err;
1520 9343a5fb 2018-06-23 stsp static const struct got_error *
1521 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1523 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1524 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1526 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1527 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1528 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1529 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1530 1a76625f 2018-10-22 stsp free(s->start_id);
1531 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1532 1a76625f 2018-10-22 stsp return err;
1535 1a76625f 2018-10-22 stsp static const struct got_error *
1536 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1537 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
1538 8b473291 2019-02-21 stsp const char *path, int check_disk)
1540 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1541 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1542 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1543 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1544 1a76625f 2018-10-22 stsp int errcode;
1546 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1547 ecb28ae0 2018-07-16 stsp if (err != NULL)
1548 ecb28ae0 2018-07-16 stsp goto done;
1550 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1551 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1552 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1554 8b473291 2019-02-21 stsp s->refs = refs;
1555 fb2756b9 2018-08-04 stsp s->repo = repo;
1556 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1557 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1558 5036bf37 2018-09-24 stsp err = got_error_from_errno();
1559 5036bf37 2018-09-24 stsp goto done;
1562 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1563 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1564 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1566 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1568 1a76625f 2018-10-22 stsp goto done;
1569 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1570 1a76625f 2018-10-22 stsp 0, thread_repo);
1572 1a76625f 2018-10-22 stsp goto done;
1574 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1575 1a76625f 2018-10-22 stsp if (errcode) {
1576 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1577 1a76625f 2018-10-22 stsp goto done;
1580 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1581 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1582 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1583 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1584 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1585 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1586 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1587 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1588 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1589 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1590 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1593 1a76625f 2018-10-22 stsp close_log_view(view);
1594 ba4f502b 2018-08-04 stsp return err;
1597 e5a0f69f 2018-08-18 stsp static const struct got_error *
1598 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1600 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1602 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1603 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1604 2b380cc8 2018-10-24 stsp &s->thread_args);
1605 2b380cc8 2018-10-24 stsp if (errcode)
1606 2b380cc8 2018-10-24 stsp return got_error_set_errno(errcode);
1609 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1610 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1611 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
1612 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1615 e5a0f69f 2018-08-18 stsp static const struct got_error *
1616 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1617 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1619 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1620 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1621 5036bf37 2018-09-24 stsp char *parent_path;
1622 669b5ffa 2018-10-07 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
1623 669b5ffa 2018-10-07 stsp int begin_x = 0;
1625 e5a0f69f 2018-08-18 stsp switch (ch) {
1627 1a76625f 2018-10-22 stsp s->quit = 1;
1630 e5a0f69f 2018-08-18 stsp case KEY_UP:
1633 2b380cc8 2018-10-24 stsp if (s->first_displayed_entry == NULL)
1635 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1636 e5a0f69f 2018-08-18 stsp s->selected--;
1637 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1639 34bc9ec9 2019-02-22 stsp scroll_up(view, &s->first_displayed_entry, 1,
1640 e5a0f69f 2018-08-18 stsp &s->commits);
1642 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1643 2b380cc8 2018-10-24 stsp if (s->first_displayed_entry == NULL)
1645 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1646 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1647 34bc9ec9 2019-02-22 stsp if (s->selected == 0) {
1648 34bc9ec9 2019-02-22 stsp view_flash(view);
1651 e5a0f69f 2018-08-18 stsp s->selected = 0;
1654 34bc9ec9 2019-02-22 stsp scroll_up(view, &s->first_displayed_entry,
1655 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1658 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1661 2b380cc8 2018-10-24 stsp if (s->first_displayed_entry == NULL)
1663 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1664 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1665 e5a0f69f 2018-08-18 stsp s->selected++;
1668 34bc9ec9 2019-02-22 stsp err = scroll_down(view, &s->first_displayed_entry, 1,
1669 93e45b7c 2018-09-24 stsp &s->last_displayed_entry, &s->commits,
1670 1a76625f 2018-10-22 stsp &s->thread_args.log_complete,
1671 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1672 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1674 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1675 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1676 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1677 2b380cc8 2018-10-24 stsp if (first == NULL)
1679 34bc9ec9 2019-02-22 stsp err = scroll_down(view, &s->first_displayed_entry,
1680 93e45b7c 2018-09-24 stsp view->nlines, &s->last_displayed_entry,
1681 1a76625f 2018-10-22 stsp &s->commits, &s->thread_args.log_complete,
1682 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1683 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1684 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1685 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1686 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1687 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1688 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1689 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1691 e5a0f69f 2018-08-18 stsp err = NULL;
1694 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1695 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1696 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1697 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1698 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1700 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1701 e5a0f69f 2018-08-18 stsp case '\r':
1702 2b380cc8 2018-10-24 stsp if (s->selected_entry == NULL)
1704 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1705 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1706 669b5ffa 2018-10-07 stsp err = open_diff_view_for_commit(&diff_view, begin_x,
1707 fb872ab2 2019-02-21 stsp s->selected_entry->commit, s->selected_entry->id,
1708 8b473291 2019-02-21 stsp view, s->refs, s->repo);
1711 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1712 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1714 669b5ffa 2018-10-07 stsp return err;
1715 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
1716 669b5ffa 2018-10-07 stsp if (err) {
1717 669b5ffa 2018-10-07 stsp view_close(diff_view);
1720 55566b34 2018-11-05 stsp *focus_view = diff_view;
1721 55566b34 2018-11-05 stsp view->child_focussed = 1;
1723 669b5ffa 2018-10-07 stsp *new_view = diff_view;
1726 2b380cc8 2018-10-24 stsp if (s->selected_entry == NULL)
1728 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1729 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1730 669b5ffa 2018-10-07 stsp err = browse_commit(&tree_view, begin_x,
1731 8b473291 2019-02-21 stsp s->selected_entry, s->refs, s->repo);
1734 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1735 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1737 669b5ffa 2018-10-07 stsp return err;
1738 669b5ffa 2018-10-07 stsp err = view_set_child(view, tree_view);
1739 669b5ffa 2018-10-07 stsp if (err) {
1740 669b5ffa 2018-10-07 stsp view_close(tree_view);
1743 669b5ffa 2018-10-07 stsp *focus_view = tree_view;
1744 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1746 669b5ffa 2018-10-07 stsp *new_view = tree_view;
1748 5036bf37 2018-09-24 stsp case KEY_BACKSPACE:
1749 5036bf37 2018-09-24 stsp if (strcmp(s->in_repo_path, "/") == 0)
1751 5036bf37 2018-09-24 stsp parent_path = dirname(s->in_repo_path);
1752 5036bf37 2018-09-24 stsp if (parent_path && strcmp(parent_path, ".") != 0) {
1753 5036bf37 2018-09-24 stsp struct tog_view *lv;
1754 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1756 1a76625f 2018-10-22 stsp return err;
1757 0cf4efb1 2018-09-29 stsp lv = view_open(view->nlines, view->ncols,
1758 0cf4efb1 2018-09-29 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
1759 5036bf37 2018-09-24 stsp if (lv == NULL)
1760 5036bf37 2018-09-24 stsp return got_error_from_errno();
1761 8b473291 2019-02-21 stsp err = open_log_view(lv, s->start_id, s->refs,
1762 8b473291 2019-02-21 stsp s->repo, parent_path, 0);
1764 1a76625f 2018-10-22 stsp return err;;
1765 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1766 669b5ffa 2018-10-07 stsp *new_view = lv;
1768 669b5ffa 2018-10-07 stsp view_set_child(view->parent, lv);
1769 34095730 2018-10-22 stsp *focus_view = lv;
1771 1a76625f 2018-10-22 stsp return NULL;
1778 80ddbec8 2018-04-29 stsp return err;
1781 4ed7e80c 2018-05-20 stsp static const struct got_error *
1782 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
1784 c2db6724 2019-01-04 stsp const struct got_error *error;
1786 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
1787 c2db6724 2019-01-04 stsp return got_error_from_errno();
1789 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
1790 c2db6724 2019-01-04 stsp return got_error_from_errno();
1792 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
1793 c2db6724 2019-01-04 stsp return got_error_from_errno();
1795 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
1796 c2db6724 2019-01-04 stsp if (error != NULL)
1797 c2db6724 2019-01-04 stsp return error;
1799 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
1800 c2db6724 2019-01-04 stsp return got_error_from_errno();
1802 c2db6724 2019-01-04 stsp return NULL;
1805 a915003a 2019-02-05 stsp static void
1806 a915003a 2019-02-05 stsp init_curses(void)
1808 a915003a 2019-02-05 stsp initscr();
1810 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
1813 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
1814 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
1815 a915003a 2019-02-05 stsp curs_set(0);
1816 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
1819 c2db6724 2019-01-04 stsp static const struct got_error *
1820 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1822 80ddbec8 2018-04-29 stsp const struct got_error *error;
1823 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1824 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
1825 8b473291 2019-02-21 stsp struct got_reflist_head refs;
1826 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1827 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1828 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1830 04cc582a 2018-08-01 stsp struct tog_view *view;
1832 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1833 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1834 c2db6724 2019-01-04 stsp NULL) == -1)
1835 80ddbec8 2018-04-29 stsp err(1, "pledge");
1838 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1839 80ddbec8 2018-04-29 stsp switch (ch) {
1841 80ddbec8 2018-04-29 stsp start_commit = optarg;
1844 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1845 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1846 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1849 17020d27 2019-03-07 stsp usage_log();
1850 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1854 80ddbec8 2018-04-29 stsp argc -= optind;
1855 80ddbec8 2018-04-29 stsp argv += optind;
1857 ecb28ae0 2018-07-16 stsp if (argc == 0)
1858 ecb28ae0 2018-07-16 stsp path = strdup("");
1859 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1860 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1862 80ddbec8 2018-04-29 stsp usage_log();
1863 ecb28ae0 2018-07-16 stsp if (path == NULL)
1864 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1866 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1867 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1868 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1869 ecb28ae0 2018-07-16 stsp goto done;
1871 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1872 b7165be3 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1873 b7165be3 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1874 b7165be3 2019-02-05 stsp goto done;
1875 b7165be3 2019-02-05 stsp if (worktree) {
1876 b7165be3 2019-02-05 stsp repo_path =
1877 b7165be3 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1879 b7165be3 2019-02-05 stsp repo_path = strdup(cwd);
1880 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1881 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1882 ecb28ae0 2018-07-16 stsp goto done;
1886 a915003a 2019-02-05 stsp init_curses();
1888 ec142235 2019-03-07 stsp error = apply_unveil(repo_path,
1889 ec142235 2019-03-07 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1890 c2db6724 2019-01-04 stsp if (error)
1891 c2db6724 2019-01-04 stsp goto done;
1893 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1894 80ddbec8 2018-04-29 stsp if (error != NULL)
1895 ecb28ae0 2018-07-16 stsp goto done;
1897 15a94983 2018-12-23 stsp if (start_commit == NULL)
1898 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1900 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&start_id, repo,
1901 15a94983 2018-12-23 stsp start_commit);
1902 80ddbec8 2018-04-29 stsp if (error != NULL)
1903 8b473291 2019-02-21 stsp goto done;
1905 8b473291 2019-02-21 stsp SIMPLEQ_INIT(&refs);
1906 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
1907 8b473291 2019-02-21 stsp if (error)
1908 ecb28ae0 2018-07-16 stsp goto done;
1910 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1911 04cc582a 2018-08-01 stsp if (view == NULL) {
1912 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1913 04cc582a 2018-08-01 stsp goto done;
1915 8b473291 2019-02-21 stsp error = open_log_view(view, start_id, &refs, repo, path, 1);
1916 ba4f502b 2018-08-04 stsp if (error)
1917 ba4f502b 2018-08-04 stsp goto done;
1918 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1920 ecb28ae0 2018-07-16 stsp free(repo_path);
1921 ecb28ae0 2018-07-16 stsp free(cwd);
1922 ecb28ae0 2018-07-16 stsp free(path);
1923 899d86c2 2018-05-10 stsp free(start_id);
1925 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1926 ec142235 2019-03-07 stsp if (worktree)
1927 ec142235 2019-03-07 stsp got_worktree_close(worktree);
1928 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1929 80ddbec8 2018-04-29 stsp return error;
1932 4ed7e80c 2018-05-20 stsp __dead static void
1933 9f7d7167 2018-04-29 stsp usage_diff(void)
1936 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1937 9f7d7167 2018-04-29 stsp getprogname());
1941 b304db33 2018-05-20 stsp static char *
1942 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1944 b304db33 2018-05-20 stsp char *line;
1945 b304db33 2018-05-20 stsp size_t linelen;
1946 b304db33 2018-05-20 stsp size_t lineno;
1947 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1949 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1951 b304db33 2018-05-20 stsp *len = linelen;
1952 b304db33 2018-05-20 stsp return line;
1955 4ed7e80c 2018-05-20 stsp static const struct got_error *
1956 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1957 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1958 a3404814 2018-09-02 stsp char * header)
1960 61e69b96 2018-05-20 stsp const struct got_error *err;
1961 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1962 b304db33 2018-05-20 stsp char *line;
1963 b304db33 2018-05-20 stsp size_t len;
1964 61e69b96 2018-05-20 stsp wchar_t *wline;
1965 e0b650dd 2018-05-20 stsp int width;
1967 26ed57b2 2018-05-19 stsp rewind(f);
1968 f7d12f7e 2018-08-01 stsp werase(view->window);
1970 a3404814 2018-09-02 stsp if (header) {
1971 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1972 a3404814 2018-09-02 stsp if (err) {
1973 a3404814 2018-09-02 stsp return err;
1976 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1977 a3404814 2018-09-02 stsp wstandout(view->window);
1978 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1979 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1980 a3404814 2018-09-02 stsp wstandend(view->window);
1981 a3404814 2018-09-02 stsp if (width < view->ncols)
1982 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1984 a3404814 2018-09-02 stsp if (max_lines <= 1)
1985 a3404814 2018-09-02 stsp return NULL;
1986 a3404814 2018-09-02 stsp max_lines--;
1990 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1991 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1992 26ed57b2 2018-05-19 stsp if (line == NULL) {
1996 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1997 26ed57b2 2018-05-19 stsp free(line);
2001 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2002 61e69b96 2018-05-20 stsp if (err) {
2003 61e69b96 2018-05-20 stsp free(line);
2004 61e69b96 2018-05-20 stsp return err;
2006 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2007 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2008 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2009 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2010 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2011 26ed57b2 2018-05-19 stsp free(line);
2012 2550e4c3 2018-07-13 stsp free(wline);
2013 2550e4c3 2018-07-13 stsp wline = NULL;
2015 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2017 1a57306a 2018-09-02 stsp view_vborder(view);
2019 26ed57b2 2018-05-19 stsp return NULL;
2022 abd2672a 2018-12-23 stsp static char *
2023 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2025 abd2672a 2018-12-23 stsp char *p, *s = ctime_r(time, datebuf);
2026 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2028 abd2672a 2018-12-23 stsp *p = '\0';
2032 4ed7e80c 2018-05-20 stsp static const struct got_error *
2033 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2034 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2036 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2037 abd2672a 2018-12-23 stsp char datebuf[26];
2038 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2039 15a94983 2018-12-23 stsp char *id_str = NULL;
2040 45d799e2 2018-12-23 stsp time_t committer_time;
2041 45d799e2 2018-12-23 stsp const char *author, *committer;
2042 8b473291 2019-02-21 stsp char *refs_str = NULL;
2044 8b473291 2019-02-21 stsp if (refs) {
2045 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, commit_id);
2047 8b473291 2019-02-21 stsp return err;
2050 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2052 abd2672a 2018-12-23 stsp return err;
2054 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2055 15a94983 2018-12-23 stsp if (err) {
2056 15a94983 2018-12-23 stsp err = got_error_from_errno();
2057 15a94983 2018-12-23 stsp goto done;
2060 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2061 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2062 abd2672a 2018-12-23 stsp err = got_error_from_errno();
2063 abd2672a 2018-12-23 stsp goto done;
2065 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2066 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2067 abd2672a 2018-12-23 stsp err = got_error_from_errno();
2068 abd2672a 2018-12-23 stsp goto done;
2070 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2071 abd2672a 2018-12-23 stsp if (fprintf(outfile, "date: %s UTC\n",
2072 45d799e2 2018-12-23 stsp get_datestr(&committer_time, datebuf)) < 0) {
2073 abd2672a 2018-12-23 stsp err = got_error_from_errno();
2074 abd2672a 2018-12-23 stsp goto done;
2076 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2077 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2078 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2079 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2080 abd2672a 2018-12-23 stsp err = got_error_from_errno();
2081 abd2672a 2018-12-23 stsp goto done;
2083 45d799e2 2018-12-23 stsp if (fprintf(outfile, "%s\n",
2084 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(commit)) < 0) {
2085 abd2672a 2018-12-23 stsp err = got_error_from_errno();
2086 abd2672a 2018-12-23 stsp goto done;
2089 abd2672a 2018-12-23 stsp free(id_str);
2090 8b473291 2019-02-21 stsp free(refs_str);
2091 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2092 abd2672a 2018-12-23 stsp return err;
2095 abd2672a 2018-12-23 stsp static const struct got_error *
2096 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2098 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2099 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2100 15a94983 2018-12-23 stsp int obj_type;
2102 511a516b 2018-05-19 stsp f = got_opentemp();
2103 48ae06ee 2018-10-18 stsp if (f == NULL) {
2104 48ae06ee 2018-10-18 stsp err = got_error_from_errno();
2105 48ae06ee 2018-10-18 stsp goto done;
2107 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2108 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
2109 fb43ecf1 2019-02-11 stsp goto done;
2113 15a94983 2018-12-23 stsp if (s->id1)
2114 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2116 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2118 15a94983 2018-12-23 stsp goto done;
2120 15a94983 2018-12-23 stsp switch (obj_type) {
2121 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2122 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2123 54156555 2018-12-24 stsp s->diff_context, s->repo, f);
2125 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2126 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2127 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
2129 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2130 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2131 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2132 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2134 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2137 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2138 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2139 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2141 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2142 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2143 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2144 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2145 8b473291 2019-02-21 stsp s->repo, f);
2150 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2152 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2153 15a94983 2018-12-23 stsp s->diff_context, s->repo, f);
2157 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2161 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2162 cbe7f848 2019-02-11 stsp err = got_error_from_errno();
2163 48ae06ee 2018-10-18 stsp return err;
2166 f5215bb9 2019-02-22 stsp static void
2167 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2169 f5215bb9 2019-02-22 stsp werase(view->window);
2170 f5215bb9 2019-02-22 stsp waddstr(view->window, "diffing...");
2171 f5215bb9 2019-02-22 stsp update_panels();
2172 f5215bb9 2019-02-22 stsp doupdate();
2175 48ae06ee 2018-10-18 stsp static const struct got_error *
2176 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2177 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2178 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2180 48ae06ee 2018-10-18 stsp const struct got_error *err;
2182 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2183 15a94983 2018-12-23 stsp int type1, type2;
2184 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2186 15a94983 2018-12-23 stsp return err;
2187 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2189 15a94983 2018-12-23 stsp return err;
2191 15a94983 2018-12-23 stsp if (type1 != type2)
2192 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2195 15a94983 2018-12-23 stsp if (id1) {
2196 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2197 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2198 48ae06ee 2018-10-18 stsp return got_error_from_errno();
2200 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2202 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2203 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2204 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2205 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2206 48ae06ee 2018-10-18 stsp return got_error_from_errno();
2208 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2209 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2210 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2211 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2212 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2213 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2214 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2216 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2217 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2218 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2220 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2221 48ae06ee 2018-10-18 stsp if (err) {
2222 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2223 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2224 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2225 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2226 48ae06ee 2018-10-18 stsp return err;
2229 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2230 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2231 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2233 5dc9f4bc 2018-08-04 stsp return NULL;
2236 e5a0f69f 2018-08-18 stsp static const struct got_error *
2237 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2239 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2241 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2242 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2243 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2244 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2245 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2246 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2247 e5a0f69f 2018-08-18 stsp return err;
2250 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2251 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2253 a3404814 2018-09-02 stsp const struct got_error *err;
2254 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2255 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2257 a3404814 2018-09-02 stsp if (s->id1) {
2258 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2260 a3404814 2018-09-02 stsp return err;
2262 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2264 a3404814 2018-09-02 stsp return err;
2266 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2267 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2268 a3404814 2018-09-02 stsp err = got_error_from_errno();
2269 a3404814 2018-09-02 stsp free(id_str1);
2270 a3404814 2018-09-02 stsp free(id_str2);
2271 a3404814 2018-09-02 stsp return err;
2273 a3404814 2018-09-02 stsp free(id_str1);
2274 a3404814 2018-09-02 stsp free(id_str2);
2276 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2277 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2281 15a087fe 2019-02-21 stsp static const struct got_error *
2282 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2283 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2285 d7a04538 2019-02-21 stsp const struct got_error *err;
2286 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
2287 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
2288 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
2290 15a087fe 2019-02-21 stsp free(s->id2);
2291 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2292 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2293 15a087fe 2019-02-21 stsp return got_error_from_errno();
2295 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2297 d7a04538 2019-02-21 stsp return err;
2298 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
2299 15a087fe 2019-02-21 stsp free(s->id1);
2300 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
2301 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2302 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
2303 15a087fe 2019-02-21 stsp return NULL;
2306 0cf4efb1 2018-09-29 stsp static const struct got_error *
2307 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2308 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2310 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2311 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2312 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
2313 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
2316 e5a0f69f 2018-08-18 stsp switch (ch) {
2318 e5a0f69f 2018-08-18 stsp case KEY_UP:
2319 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
2320 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2322 34bc9ec9 2019-02-22 stsp view_flash(view);
2324 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2325 34bc9ec9 2019-02-22 stsp if (s->first_displayed_line == 1) {
2326 34bc9ec9 2019-02-22 stsp view_flash(view);
2330 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
2331 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2332 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2335 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2336 e5a0f69f 2018-08-18 stsp if (!s->eof)
2337 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2339 34bc9ec9 2019-02-22 stsp view_flash(view);
2341 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2343 34bc9ec9 2019-02-22 stsp if (s->eof) {
2344 34bc9ec9 2019-02-22 stsp view_flash(view);
2348 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
2349 e5a0f69f 2018-08-18 stsp char *line;
2350 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
2351 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2352 e5a0f69f 2018-08-18 stsp if (line == NULL)
2357 48ae06ee 2018-10-18 stsp if (s->diff_context > 0) {
2358 48ae06ee 2018-10-18 stsp s->diff_context--;
2359 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2360 48ae06ee 2018-10-18 stsp err = create_diff(s);
2364 4a8520aa 2018-10-18 stsp if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2365 48ae06ee 2018-10-18 stsp s->diff_context++;
2366 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2367 48ae06ee 2018-10-18 stsp err = create_diff(s);
2372 fb872ab2 2019-02-21 stsp if (s->log_view == NULL)
2374 fb872ab2 2019-02-21 stsp ls = &s->log_view->state.log;
2375 fb872ab2 2019-02-21 stsp entry = TAILQ_PREV(ls->selected_entry,
2376 15a087fe 2019-02-21 stsp commit_queue_head, entry);
2377 15a087fe 2019-02-21 stsp if (entry == NULL)
2380 6524637e 2019-02-21 stsp err = input_log_view(NULL, NULL, NULL, s->log_view,
2385 15a087fe 2019-02-21 stsp err = set_selected_commit(s, entry);
2389 15a087fe 2019-02-21 stsp s->first_displayed_line = 1;
2390 15a087fe 2019-02-21 stsp s->last_displayed_line = view->nlines;
2392 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2393 15a087fe 2019-02-21 stsp err = create_diff(s);
2397 fb872ab2 2019-02-21 stsp if (s->log_view == NULL)
2399 fb872ab2 2019-02-21 stsp ls = &s->log_view->state.log;
2401 6e73b0d6 2019-02-22 stsp if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2402 5e224a3e 2019-02-22 stsp ls->thread_args.commits_needed++;
2404 5e224a3e 2019-02-22 stsp /* Display "loading..." in log view. */
2405 5e224a3e 2019-02-22 stsp show_log_view(s->log_view);
2406 5e224a3e 2019-02-22 stsp update_panels();
2407 5e224a3e 2019-02-22 stsp doupdate();
2409 6e73b0d6 2019-02-22 stsp err = trigger_log_thread(1 /* load_all */,
2410 6e73b0d6 2019-02-22 stsp &ls->thread_args.commits_needed,
2411 6e73b0d6 2019-02-22 stsp &ls->thread_args.log_complete,
2412 6e73b0d6 2019-02-22 stsp &ls->thread_args.need_commits);
2416 fb872ab2 2019-02-21 stsp err = input_log_view(NULL, NULL, NULL, s->log_view,
2417 fb872ab2 2019-02-21 stsp KEY_DOWN);
2421 fb872ab2 2019-02-21 stsp entry = TAILQ_NEXT(ls->selected_entry, entry);
2422 15a087fe 2019-02-21 stsp if (entry == NULL)
2425 15a087fe 2019-02-21 stsp err = set_selected_commit(s, entry);
2429 15a087fe 2019-02-21 stsp s->first_displayed_line = 1;
2430 15a087fe 2019-02-21 stsp s->last_displayed_line = view->nlines;
2432 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2433 15a087fe 2019-02-21 stsp err = create_diff(s);
2439 bcbd79e2 2018-08-19 stsp return err;
2442 4ed7e80c 2018-05-20 stsp static const struct got_error *
2443 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2445 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2446 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2447 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2448 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2449 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2450 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2452 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2454 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2455 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2456 eb6600df 2019-01-04 stsp NULL) == -1)
2457 26ed57b2 2018-05-19 stsp err(1, "pledge");
2460 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2461 26ed57b2 2018-05-19 stsp switch (ch) {
2463 17020d27 2019-03-07 stsp usage_diff();
2464 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2468 26ed57b2 2018-05-19 stsp argc -= optind;
2469 26ed57b2 2018-05-19 stsp argv += optind;
2471 26ed57b2 2018-05-19 stsp if (argc == 0) {
2472 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2473 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2474 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2475 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2476 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2477 15a94983 2018-12-23 stsp id_str1 = argv[0];
2478 15a94983 2018-12-23 stsp id_str2 = argv[1];
2479 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2480 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2481 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2482 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2483 15a94983 2018-12-23 stsp id_str1 = argv[1];
2484 15a94983 2018-12-23 stsp id_str2 = argv[2];
2486 26ed57b2 2018-05-19 stsp usage_diff();
2488 a915003a 2019-02-05 stsp init_curses();
2490 eb6600df 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
2491 eb6600df 2019-01-04 stsp if (error)
2492 eb6600df 2019-01-04 stsp goto done;
2494 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
2495 26ed57b2 2018-05-19 stsp free(repo_path);
2496 26ed57b2 2018-05-19 stsp if (error)
2497 26ed57b2 2018-05-19 stsp goto done;
2499 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
2500 26ed57b2 2018-05-19 stsp if (error)
2501 26ed57b2 2018-05-19 stsp goto done;
2503 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
2504 26ed57b2 2018-05-19 stsp if (error)
2505 26ed57b2 2018-05-19 stsp goto done;
2507 8b473291 2019-02-21 stsp SIMPLEQ_INIT(&refs);
2508 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2509 8b473291 2019-02-21 stsp if (error)
2510 8b473291 2019-02-21 stsp goto done;
2512 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2513 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2514 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
2515 ea5e7bb5 2018-08-01 stsp goto done;
2517 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2518 5dc9f4bc 2018-08-04 stsp if (error)
2519 5dc9f4bc 2018-08-04 stsp goto done;
2520 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2522 26ed57b2 2018-05-19 stsp got_repo_close(repo);
2523 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2524 26ed57b2 2018-05-19 stsp return error;
2527 4ed7e80c 2018-05-20 stsp __dead static void
2528 9f7d7167 2018-04-29 stsp usage_blame(void)
2531 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2532 9f7d7167 2018-04-29 stsp getprogname());
2536 84451b3e 2018-07-10 stsp struct tog_blame_line {
2537 84451b3e 2018-07-10 stsp int annotated;
2538 84451b3e 2018-07-10 stsp struct got_object_id *id;
2541 4ed7e80c 2018-05-20 stsp static const struct got_error *
2542 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2543 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2544 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2545 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2547 84451b3e 2018-07-10 stsp const struct got_error *err;
2548 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2549 84451b3e 2018-07-10 stsp char *line;
2550 84451b3e 2018-07-10 stsp size_t len;
2551 84451b3e 2018-07-10 stsp wchar_t *wline;
2552 b700b5d6 2018-07-10 stsp int width, wlimit;
2553 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2554 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2555 ab089a2a 2018-07-12 stsp char *id_str;
2557 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2559 ab089a2a 2018-07-12 stsp return err;
2561 84451b3e 2018-07-10 stsp rewind(f);
2562 f7d12f7e 2018-08-01 stsp werase(view->window);
2564 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
2565 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
2566 ab089a2a 2018-07-12 stsp free(id_str);
2567 ab089a2a 2018-07-12 stsp return err;
2570 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2571 ab089a2a 2018-07-12 stsp free(line);
2572 2550e4c3 2018-07-13 stsp line = NULL;
2573 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2574 a3404814 2018-09-02 stsp wstandout(view->window);
2575 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2576 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2577 a3404814 2018-09-02 stsp wstandend(view->window);
2578 2550e4c3 2018-07-13 stsp free(wline);
2579 2550e4c3 2018-07-13 stsp wline = NULL;
2580 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2581 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2583 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2584 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2585 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
2586 ab089a2a 2018-07-12 stsp free(id_str);
2587 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
2589 ab089a2a 2018-07-12 stsp free(id_str);
2590 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2591 3f60a8ef 2018-07-10 stsp free(line);
2592 2550e4c3 2018-07-13 stsp line = NULL;
2594 3f60a8ef 2018-07-10 stsp return err;
2595 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2596 2550e4c3 2018-07-13 stsp free(wline);
2597 2550e4c3 2018-07-13 stsp wline = NULL;
2598 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2599 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2602 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2603 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2604 84451b3e 2018-07-10 stsp if (line == NULL) {
2608 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2609 84451b3e 2018-07-10 stsp free(line);
2613 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2614 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2615 84451b3e 2018-07-10 stsp if (err) {
2616 84451b3e 2018-07-10 stsp free(line);
2617 84451b3e 2018-07-10 stsp return err;
2620 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2621 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2623 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2624 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2625 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2626 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2627 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2628 84451b3e 2018-07-10 stsp char *id_str;
2629 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2630 84451b3e 2018-07-10 stsp if (err) {
2631 84451b3e 2018-07-10 stsp free(line);
2632 2550e4c3 2018-07-13 stsp free(wline);
2633 84451b3e 2018-07-10 stsp return err;
2635 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2636 84451b3e 2018-07-10 stsp free(id_str);
2637 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2639 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2640 ee41ec32 2018-07-10 stsp prev_id = NULL;
2643 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2644 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2645 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2648 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2649 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2650 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2651 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2652 84451b3e 2018-07-10 stsp free(line);
2653 2550e4c3 2018-07-13 stsp free(wline);
2654 2550e4c3 2018-07-13 stsp wline = NULL;
2656 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2658 1a57306a 2018-09-02 stsp view_vborder(view);
2660 84451b3e 2018-07-10 stsp return NULL;
2663 84451b3e 2018-07-10 stsp static const struct got_error *
2664 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2666 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2667 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2668 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2669 1a76625f 2018-10-22 stsp int errcode;
2671 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2672 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2673 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2675 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2676 1a76625f 2018-10-22 stsp if (errcode)
2677 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2679 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2680 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2681 d68a0a7d 2018-07-10 stsp goto done;
2684 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2685 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2687 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2688 d68a0a7d 2018-07-10 stsp if (line->annotated)
2689 d68a0a7d 2018-07-10 stsp goto done;
2691 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2692 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2693 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2694 84451b3e 2018-07-10 stsp goto done;
2696 84451b3e 2018-07-10 stsp line->annotated = 1;
2698 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2699 1a76625f 2018-10-22 stsp if (errcode)
2700 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2701 84451b3e 2018-07-10 stsp return err;
2704 84451b3e 2018-07-10 stsp static void *
2705 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2707 18430de3 2018-07-10 stsp const struct got_error *err;
2708 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
2709 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
2710 1a76625f 2018-10-22 stsp int errcode;
2712 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2713 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
2715 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2716 1a76625f 2018-10-22 stsp if (errcode)
2717 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
2719 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
2720 c9beca56 2018-07-22 stsp ta->repo = NULL;
2721 c9beca56 2018-07-22 stsp *ta->complete = 1;
2723 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2724 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2725 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2727 18430de3 2018-07-10 stsp return (void *)err;
2730 245d91c1 2018-07-12 stsp static struct got_object_id *
2731 15a94983 2018-12-23 stsp get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2732 15a94983 2018-12-23 stsp int selected_line)
2734 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2736 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2737 245d91c1 2018-07-12 stsp if (!line->annotated)
2738 245d91c1 2018-07-12 stsp return NULL;
2740 245d91c1 2018-07-12 stsp return line->id;
2743 b880a918 2018-07-10 stsp static const struct got_error *
2744 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2746 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2749 245d91c1 2018-07-12 stsp if (blame->thread) {
2750 1a76625f 2018-10-22 stsp int errcode;
2751 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2752 1a76625f 2018-10-22 stsp if (errcode)
2753 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2754 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
2755 1a76625f 2018-10-22 stsp if (errcode)
2756 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2757 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2758 1a76625f 2018-10-22 stsp if (errcode)
2759 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2760 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2761 245d91c1 2018-07-12 stsp err = NULL;
2762 245d91c1 2018-07-12 stsp blame->thread = NULL;
2764 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2765 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2766 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2768 245d91c1 2018-07-12 stsp if (blame->f) {
2769 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
2770 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
2771 245d91c1 2018-07-12 stsp blame->f = NULL;
2773 57670559 2018-12-23 stsp if (blame->lines) {
2774 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
2775 57670559 2018-12-23 stsp free(blame->lines[i].id);
2776 57670559 2018-12-23 stsp free(blame->lines);
2777 57670559 2018-12-23 stsp blame->lines = NULL;
2779 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2780 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2782 245d91c1 2018-07-12 stsp return err;
2785 245d91c1 2018-07-12 stsp static const struct got_error *
2786 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2787 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
2788 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
2789 245d91c1 2018-07-12 stsp struct got_repository *repo)
2791 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2792 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2793 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2794 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2795 15a94983 2018-12-23 stsp int obj_type;
2797 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2799 15a94983 2018-12-23 stsp return err;
2800 15a94983 2018-12-23 stsp if (obj_id == NULL)
2801 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
2803 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
2805 84451b3e 2018-07-10 stsp goto done;
2807 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2808 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2809 84451b3e 2018-07-10 stsp goto done;
2812 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2814 a70480e0 2018-06-23 stsp goto done;
2815 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2816 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2817 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2818 84451b3e 2018-07-10 stsp goto done;
2820 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2821 245d91c1 2018-07-12 stsp blame->f, blob);
2823 84451b3e 2018-07-10 stsp goto done;
2825 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2826 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2827 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2828 84451b3e 2018-07-10 stsp goto done;
2831 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2833 bd24772e 2018-07-11 stsp goto done;
2835 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2836 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2837 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2838 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2839 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2840 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2841 245d91c1 2018-07-12 stsp goto done;
2843 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2845 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2846 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2847 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2848 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2849 245d91c1 2018-07-12 stsp *blame_complete = 0;
2853 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2854 27d434c2 2018-09-15 stsp free(obj_id);
2856 245d91c1 2018-07-12 stsp stop_blame(blame);
2857 245d91c1 2018-07-12 stsp return err;
2860 245d91c1 2018-07-12 stsp static const struct got_error *
2861 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2862 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
2863 8b473291 2019-02-21 stsp struct got_repository *repo)
2865 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2866 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2868 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2870 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2872 7cbe629d 2018-08-04 stsp return err;
2874 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2875 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2876 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2877 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2878 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2879 fb2756b9 2018-08-04 stsp s->path = path;
2880 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2881 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2882 fb2756b9 2018-08-04 stsp s->repo = repo;
2883 8b473291 2019-02-21 stsp s->refs = refs;
2884 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2885 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2887 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2888 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2889 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2891 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
2892 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2893 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2894 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2897 e5a0f69f 2018-08-18 stsp static const struct got_error *
2898 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2900 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2901 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2903 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2904 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2906 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2907 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2908 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2909 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2910 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2913 e5a0f69f 2018-08-18 stsp free(s->path);
2915 e5a0f69f 2018-08-18 stsp return err;
2918 7cbe629d 2018-08-04 stsp static const struct got_error *
2919 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2921 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2922 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2923 2b380cc8 2018-10-24 stsp int errcode;
2925 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
2926 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2927 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
2928 2b380cc8 2018-10-24 stsp if (errcode)
2929 2b380cc8 2018-10-24 stsp return got_error_set_errno(errcode);
2932 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2933 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2934 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2935 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2937 669b5ffa 2018-10-07 stsp view_vborder(view);
2938 e5a0f69f 2018-08-18 stsp return err;
2941 e5a0f69f 2018-08-18 stsp static const struct got_error *
2942 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2943 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2945 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2946 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2947 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2948 669b5ffa 2018-10-07 stsp int begin_x = 0;
2950 e5a0f69f 2018-08-18 stsp switch (ch) {
2952 e5a0f69f 2018-08-18 stsp s->done = 1;
2955 e5a0f69f 2018-08-18 stsp case KEY_UP:
2956 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2957 e5a0f69f 2018-08-18 stsp s->selected_line--;
2958 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2959 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2960 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2962 34bc9ec9 2019-02-22 stsp view_flash(view);
2964 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2965 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2966 34bc9ec9 2019-02-22 stsp if (s->selected_line == 1) {
2967 34bc9ec9 2019-02-22 stsp view_flash(view);
2970 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2973 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2974 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2975 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2977 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2980 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2981 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2982 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2983 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2984 e5a0f69f 2018-08-18 stsp s->selected_line++;
2985 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2986 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2987 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2989 34bc9ec9 2019-02-22 stsp view_flash(view);
2992 e5a0f69f 2018-08-18 stsp case 'p': {
2993 15a94983 2018-12-23 stsp struct got_object_id *id = NULL;
2994 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2995 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2996 15a94983 2018-12-23 stsp if (id == NULL)
2998 15a94983 2018-12-23 stsp if (ch == 'p') {
2999 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3000 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3001 15a94983 2018-12-23 stsp struct got_object_id *blob_id = NULL;
3002 15a94983 2018-12-23 stsp int obj_type;
3003 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit,
3004 15a94983 2018-12-23 stsp s->repo, id);
3007 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3008 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3009 15a94983 2018-12-23 stsp if (pid == NULL) {
3010 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3013 15a94983 2018-12-23 stsp /* Check if path history ends here. */
3014 15a94983 2018-12-23 stsp err = got_object_id_by_path(&blob_id, s->repo,
3015 15a94983 2018-12-23 stsp pid->id, s->path);
3016 15a94983 2018-12-23 stsp if (err) {
3017 15a94983 2018-12-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
3018 15a94983 2018-12-23 stsp err = NULL;
3019 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3022 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo,
3024 15a94983 2018-12-23 stsp free(blob_id);
3025 15a94983 2018-12-23 stsp /* Can't blame non-blob type objects. */
3026 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3027 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3030 15a94983 2018-12-23 stsp err = got_object_qid_alloc(&s->blamed_commit,
3032 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3034 15a94983 2018-12-23 stsp if (got_object_id_cmp(id,
3035 15a94983 2018-12-23 stsp s->blamed_commit->id) == 0)
3037 15a94983 2018-12-23 stsp err = got_object_qid_alloc(&s->blamed_commit,
3042 e5a0f69f 2018-08-18 stsp s->done = 1;
3043 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
3044 e5a0f69f 2018-08-18 stsp s->done = 0;
3045 e5a0f69f 2018-08-18 stsp if (thread_err)
3047 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3048 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
3049 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
3050 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
3051 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
3052 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
3057 e5a0f69f 2018-08-18 stsp case 'B': {
3058 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
3059 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
3060 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
3062 e5a0f69f 2018-08-18 stsp s->done = 1;
3063 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
3064 e5a0f69f 2018-08-18 stsp s->done = 0;
3065 e5a0f69f 2018-08-18 stsp if (thread_err)
3067 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3068 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
3069 e5a0f69f 2018-08-18 stsp s->blamed_commit =
3070 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
3071 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
3072 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
3073 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3074 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3079 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
3080 15a94983 2018-12-23 stsp case '\r': {
3081 15a94983 2018-12-23 stsp struct got_object_id *id = NULL;
3082 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3083 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
3084 15a94983 2018-12-23 stsp id = get_selected_commit_id(s->blame.lines,
3085 15a94983 2018-12-23 stsp s->first_displayed_line, s->selected_line);
3086 3e9926ea 2019-01-04 stsp if (id == NULL)
3088 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, s->repo, id);
3091 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3092 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3093 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
3094 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
3095 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3096 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
3097 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3098 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
3101 15a94983 2018-12-23 stsp err = open_diff_view(diff_view, pid ? pid->id : NULL,
3102 8b473291 2019-02-21 stsp id, NULL, s->refs, s->repo);
3103 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3104 e5a0f69f 2018-08-18 stsp if (err) {
3105 e5a0f69f 2018-08-18 stsp view_close(diff_view);
3108 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3109 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3112 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
3113 669b5ffa 2018-10-07 stsp if (err) {
3114 669b5ffa 2018-10-07 stsp view_close(diff_view);
3117 51c68690 2018-11-07 stsp *focus_view = diff_view;
3118 51c68690 2018-11-07 stsp view->child_focussed = 1;
3120 669b5ffa 2018-10-07 stsp *new_view = diff_view;
3125 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
3127 34bc9ec9 2019-02-22 stsp if (s->last_displayed_line >= s->blame.nlines &&
3128 34bc9ec9 2019-02-22 stsp s->selected_line >= MIN(s->blame.nlines,
3129 34bc9ec9 2019-02-22 stsp view->nlines - 2)) {
3130 34bc9ec9 2019-02-22 stsp view_flash(view);
3133 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
3134 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
3135 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
3136 e5a0f69f 2018-08-18 stsp view->nlines - 2);
3139 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
3140 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
3141 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
3142 e5a0f69f 2018-08-18 stsp view->nlines - 2;
3144 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
3145 e5a0f69f 2018-08-18 stsp s->blame.nlines -
3146 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
3148 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3149 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
3150 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
3151 e5a0f69f 2018-08-18 stsp view->nlines - 2);
3157 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
3160 a70480e0 2018-06-23 stsp static const struct got_error *
3161 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
3163 a70480e0 2018-06-23 stsp const struct got_error *error;
3164 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
3165 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3166 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
3167 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3168 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3169 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
3171 e1cd8fed 2018-08-01 stsp struct tog_view *view;
3173 a70480e0 2018-06-23 stsp #ifndef PROFILE
3174 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3175 8e94dd5b 2019-01-04 stsp NULL) == -1)
3176 a70480e0 2018-06-23 stsp err(1, "pledge");
3179 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3180 a70480e0 2018-06-23 stsp switch (ch) {
3182 a70480e0 2018-06-23 stsp commit_id_str = optarg;
3185 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3186 69069811 2018-08-02 stsp if (repo_path == NULL)
3187 69069811 2018-08-02 stsp err(1, "-r option");
3190 17020d27 2019-03-07 stsp usage_blame();
3191 a70480e0 2018-06-23 stsp /* NOTREACHED */
3195 a70480e0 2018-06-23 stsp argc -= optind;
3196 a70480e0 2018-06-23 stsp argv += optind;
3198 69069811 2018-08-02 stsp if (argc == 1)
3199 69069811 2018-08-02 stsp path = argv[0];
3201 a70480e0 2018-06-23 stsp usage_blame();
3203 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
3204 69069811 2018-08-02 stsp if (cwd == NULL) {
3205 69069811 2018-08-02 stsp error = got_error_from_errno();
3206 69069811 2018-08-02 stsp goto done;
3208 69069811 2018-08-02 stsp if (repo_path == NULL) {
3209 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3210 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3211 69069811 2018-08-02 stsp goto done;
3213 eb41ed75 2019-02-05 stsp error = NULL;
3214 eb41ed75 2019-02-05 stsp if (worktree) {
3215 eb41ed75 2019-02-05 stsp repo_path =
3216 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3217 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
3218 eb41ed75 2019-02-05 stsp error = got_error_from_errno();
3219 eb41ed75 2019-02-05 stsp if (error)
3220 eb41ed75 2019-02-05 stsp goto done;
3222 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
3223 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
3224 eb41ed75 2019-02-05 stsp error = got_error_from_errno();
3225 eb41ed75 2019-02-05 stsp goto done;
3230 a915003a 2019-02-05 stsp init_curses();
3232 8e94dd5b 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
3233 8e94dd5b 2019-01-04 stsp if (error)
3234 8e94dd5b 2019-01-04 stsp goto done;
3236 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
3237 a70480e0 2018-06-23 stsp if (error != NULL)
3238 92205607 2019-01-04 stsp goto done;
3240 eb41ed75 2019-02-05 stsp if (worktree) {
3241 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3242 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3243 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3244 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3245 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3246 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3247 eb41ed75 2019-02-05 stsp path) == -1) {
3248 eb41ed75 2019-02-05 stsp error = got_error_from_errno();
3249 eb41ed75 2019-02-05 stsp goto done;
3251 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3254 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3256 eb41ed75 2019-02-05 stsp if (error)
3257 69069811 2018-08-02 stsp goto done;
3259 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
3260 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
3261 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3262 a70480e0 2018-06-23 stsp if (error != NULL)
3263 66b4983c 2018-06-23 stsp goto done;
3264 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3265 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
3267 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
3268 15a94983 2018-12-23 stsp commit_id_str);
3270 a19e88aa 2018-06-23 stsp if (error != NULL)
3271 8b473291 2019-02-21 stsp goto done;
3273 8b473291 2019-02-21 stsp SIMPLEQ_INIT(&refs);
3274 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
3275 8b473291 2019-02-21 stsp if (error)
3276 a19e88aa 2018-06-23 stsp goto done;
3278 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3279 e1cd8fed 2018-08-01 stsp if (view == NULL) {
3280 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
3281 e1cd8fed 2018-08-01 stsp goto done;
3283 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3284 7cbe629d 2018-08-04 stsp if (error)
3285 7cbe629d 2018-08-04 stsp goto done;
3286 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3288 69069811 2018-08-02 stsp free(repo_path);
3289 69069811 2018-08-02 stsp free(cwd);
3290 a70480e0 2018-06-23 stsp free(commit_id);
3291 eb41ed75 2019-02-05 stsp if (worktree)
3292 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
3294 a70480e0 2018-06-23 stsp got_repo_close(repo);
3295 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3296 a70480e0 2018-06-23 stsp return error;
3299 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3300 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
3301 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
3302 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
3303 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
3304 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
3305 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
3307 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3308 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
3309 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
3310 ffd1d5e5 2018-06-23 stsp int width, n;
3312 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
3314 f7d12f7e 2018-08-01 stsp werase(view->window);
3316 ffd1d5e5 2018-06-23 stsp if (limit == 0)
3317 ffd1d5e5 2018-06-23 stsp return NULL;
3319 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
3321 ffd1d5e5 2018-06-23 stsp return err;
3322 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3323 a3404814 2018-09-02 stsp wstandout(view->window);
3324 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3325 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3326 a3404814 2018-09-02 stsp wstandend(view->window);
3327 2550e4c3 2018-07-13 stsp free(wline);
3328 2550e4c3 2018-07-13 stsp wline = NULL;
3329 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
3330 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3331 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3332 ffd1d5e5 2018-06-23 stsp return NULL;
3333 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
3335 ce52c690 2018-06-23 stsp return err;
3336 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3337 2550e4c3 2018-07-13 stsp free(wline);
3338 2550e4c3 2018-07-13 stsp wline = NULL;
3339 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
3340 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3341 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3342 ffd1d5e5 2018-06-23 stsp return NULL;
3343 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3344 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
3345 a1eca9bb 2018-06-23 stsp return NULL;
3347 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3348 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
3349 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
3350 0cf4efb1 2018-09-29 stsp if (view->focussed)
3351 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3352 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
3354 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
3355 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
3356 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3357 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3358 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3359 ffd1d5e5 2018-06-23 stsp return NULL;
3363 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
3364 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3367 ffd1d5e5 2018-06-23 stsp while (te) {
3368 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
3370 1d13200f 2018-07-12 stsp if (show_ids) {
3371 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
3373 1d13200f 2018-07-12 stsp return got_error_from_errno();
3375 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3376 d8355ef1 2019-02-10 stsp te->name, S_ISDIR(te->mode) ? "/" :
3377 d8355ef1 2019-02-10 stsp ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3378 1d13200f 2018-07-12 stsp free(id_str);
3379 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3381 1d13200f 2018-07-12 stsp free(id_str);
3382 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
3383 ffd1d5e5 2018-06-23 stsp if (err) {
3384 ffd1d5e5 2018-06-23 stsp free(line);
3387 ffd1d5e5 2018-06-23 stsp if (n == selected) {
3388 0cf4efb1 2018-09-29 stsp if (view->focussed)
3389 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3390 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
3392 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3393 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
3394 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3395 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
3396 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3397 ffd1d5e5 2018-06-23 stsp free(line);
3398 2550e4c3 2018-07-13 stsp free(wline);
3399 2550e4c3 2018-07-13 stsp wline = NULL;
3401 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3402 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
3403 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3405 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3408 ffd1d5e5 2018-06-23 stsp return err;
3411 ffd1d5e5 2018-06-23 stsp static void
3412 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
3413 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
3414 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
3416 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
3419 5e37ffca 2019-02-25 stsp if (*first_displayed_entry == NULL) {
3420 5e37ffca 2019-02-25 stsp view_flash(view);
3424 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3425 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
3426 34bc9ec9 2019-02-22 stsp view_flash(view);
3427 ffd1d5e5 2018-06-23 stsp if (!isroot)
3428 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3432 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
3433 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
3434 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
3435 ffd1d5e5 2018-06-23 stsp prev = te;
3436 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3438 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
3439 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3441 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3442 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3445 768394f3 2019-01-24 stsp static int
3446 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3447 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
3448 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
3450 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
3451 ffd1d5e5 2018-06-23 stsp int n = 0;
3453 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
3454 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3456 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
3457 768394f3 2019-01-24 stsp last = last_displayed_entry;
3458 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
3459 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
3460 768394f3 2019-01-24 stsp if (last) {
3461 768394f3 2019-01-24 stsp *first_displayed_entry = next;
3462 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
3468 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3469 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
3470 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
3472 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
3473 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
3474 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
3476 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
3477 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
3479 ce52c690 2018-06-23 stsp len += strlen(te->name);
3481 ce52c690 2018-06-23 stsp *path = calloc(1, len);
3482 ffd1d5e5 2018-06-23 stsp if (path == NULL)
3483 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3485 ce52c690 2018-06-23 stsp (*path)[0] = '/';
3486 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
3487 d9765a41 2018-06-23 stsp while (pt) {
3488 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3489 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3490 cb2ebc8a 2018-06-23 stsp goto done;
3492 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
3493 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3494 cb2ebc8a 2018-06-23 stsp goto done;
3496 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3499 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3500 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3501 ce52c690 2018-06-23 stsp goto done;
3505 ce52c690 2018-06-23 stsp if (err) {
3506 ce52c690 2018-06-23 stsp free(*path);
3507 ce52c690 2018-06-23 stsp *path = NULL;
3509 ce52c690 2018-06-23 stsp return err;
3512 ce52c690 2018-06-23 stsp static const struct got_error *
3513 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3514 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3515 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3516 8b473291 2019-02-21 stsp struct got_repository *repo)
3518 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3519 ce52c690 2018-06-23 stsp char *path;
3520 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3522 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3524 ce52c690 2018-06-23 stsp return err;
3526 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3527 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3528 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3530 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
3531 e5a0f69f 2018-08-18 stsp if (err) {
3532 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3533 e5a0f69f 2018-08-18 stsp free(path);
3535 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3536 69efd4c4 2018-07-18 stsp return err;
3539 69efd4c4 2018-07-18 stsp static const struct got_error *
3540 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3541 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3542 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3543 8b473291 2019-02-21 stsp struct got_repository *repo)
3545 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3546 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3547 69efd4c4 2018-07-18 stsp char *path;
3549 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3550 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3551 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3553 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3555 69efd4c4 2018-07-18 stsp return err;
3557 8b473291 2019-02-21 stsp err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3559 e5a0f69f 2018-08-18 stsp view_close(log_view);
3561 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3562 cb2ebc8a 2018-06-23 stsp free(path);
3563 cb2ebc8a 2018-06-23 stsp return err;
3566 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3567 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3568 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3569 8b473291 2019-02-21 stsp struct got_repository *repo)
3571 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3572 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3573 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3575 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3577 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3578 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3579 ffd1d5e5 2018-06-23 stsp goto done;
3581 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3582 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
3583 ffd1d5e5 2018-06-23 stsp goto done;
3586 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3587 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3588 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3589 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3590 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3591 6484ec90 2018-09-29 stsp err = got_error_from_errno();
3592 6484ec90 2018-09-29 stsp goto done;
3594 8b473291 2019-02-21 stsp s->refs = refs;
3595 fb2756b9 2018-08-04 stsp s->repo = repo;
3597 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
3598 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
3599 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
3601 ad80ab7b 2018-08-04 stsp free(commit_id_str);
3602 6484ec90 2018-09-29 stsp if (err) {
3603 fb2756b9 2018-08-04 stsp free(s->tree_label);
3604 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3606 ad80ab7b 2018-08-04 stsp return err;
3609 e5a0f69f 2018-08-18 stsp static const struct got_error *
3610 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
3612 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3614 fb2756b9 2018-08-04 stsp free(s->tree_label);
3615 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3616 6484ec90 2018-09-29 stsp free(s->commit_id);
3617 6484ec90 2018-09-29 stsp s->commit_id = NULL;
3618 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
3619 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
3620 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
3621 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
3622 ad80ab7b 2018-08-04 stsp free(parent);
3625 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
3626 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
3627 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
3629 e5a0f69f 2018-08-18 stsp return NULL;
3632 ad80ab7b 2018-08-04 stsp static const struct got_error *
3633 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
3635 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
3636 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3637 e5a0f69f 2018-08-18 stsp char *parent_path;
3639 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
3641 e5a0f69f 2018-08-18 stsp return err;
3643 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
3644 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
3645 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3646 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
3647 e5a0f69f 2018-08-18 stsp free(parent_path);
3649 669b5ffa 2018-10-07 stsp view_vborder(view);
3650 e5a0f69f 2018-08-18 stsp return err;
3653 e5a0f69f 2018-08-18 stsp static const struct got_error *
3654 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3655 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3657 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3658 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
3659 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
3660 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
3662 e5a0f69f 2018-08-18 stsp switch (ch) {
3664 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
3667 669b5ffa 2018-10-07 stsp if (!s->selected_entry)
3669 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
3670 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
3671 669b5ffa 2018-10-07 stsp err = log_tree_entry(&log_view, begin_x,
3672 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
3673 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
3674 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3675 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3677 669b5ffa 2018-10-07 stsp return err;
3678 669b5ffa 2018-10-07 stsp err = view_set_child(view, log_view);
3679 669b5ffa 2018-10-07 stsp if (err) {
3680 669b5ffa 2018-10-07 stsp view_close(log_view);
3683 669b5ffa 2018-10-07 stsp *focus_view = log_view;
3684 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3686 669b5ffa 2018-10-07 stsp *new_view = log_view;
3689 e5a0f69f 2018-08-18 stsp case KEY_UP:
3690 bf979164 2019-01-24 stsp if (s->selected > 0) {
3691 e5a0f69f 2018-08-18 stsp s->selected--;
3692 bf979164 2019-01-24 stsp if (s->selected == 0)
3695 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3697 34bc9ec9 2019-02-22 stsp tree_scroll_up(view, &s->first_displayed_entry, 1,
3698 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
3700 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
3701 34bc9ec9 2019-02-22 stsp tree_scroll_up(view, &s->first_displayed_entry,
3702 67cc7991 2019-01-24 stsp MAX(0, view->nlines - 4 - s->selected), s->entries,
3703 67cc7991 2019-01-24 stsp s->tree == s->root);
3704 8a0ead39 2018-11-03 stsp s->selected = 0;
3705 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
3706 67cc7991 2019-01-24 stsp s->first_displayed_entry && s->tree != s->root)
3707 67cc7991 2019-01-24 stsp s->first_displayed_entry = NULL;
3710 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
3711 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
3712 e5a0f69f 2018-08-18 stsp s->selected++;
3715 768394f3 2019-01-24 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3716 768394f3 2019-01-24 stsp == NULL) {
3717 768394f3 2019-01-24 stsp /* can't scroll any further */
3718 34bc9ec9 2019-02-22 stsp view_flash(view);
3721 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
3722 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
3724 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
3725 768394f3 2019-01-24 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3726 768394f3 2019-01-24 stsp == NULL) {
3727 768394f3 2019-01-24 stsp /* can't scroll any further; move cursor down */
3728 768394f3 2019-01-24 stsp if (s->selected < s->ndisplayed - 1)
3729 768394f3 2019-01-24 stsp s->selected = s->ndisplayed - 1;
3731 34bc9ec9 2019-02-22 stsp view_flash(view);
3734 768394f3 2019-01-24 stsp nscrolled = tree_scroll_down(&s->first_displayed_entry,
3735 768394f3 2019-01-24 stsp view->nlines, s->last_displayed_entry, s->entries);
3736 768394f3 2019-01-24 stsp if (nscrolled < view->nlines) {
3737 768394f3 2019-01-24 stsp int ndisplayed = 0;
3738 768394f3 2019-01-24 stsp struct got_tree_entry *te;
3739 768394f3 2019-01-24 stsp te = s->first_displayed_entry;
3741 768394f3 2019-01-24 stsp ndisplayed++;
3742 768394f3 2019-01-24 stsp te = SIMPLEQ_NEXT(te, entry);
3743 768394f3 2019-01-24 stsp } while (te);
3744 768394f3 2019-01-24 stsp s->selected = ndisplayed - 1;
3747 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
3748 e5a0f69f 2018-08-18 stsp case '\r':
3749 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
3750 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3751 7837eeac 2018-09-24 stsp case KEY_BACKSPACE:
3752 e5a0f69f 2018-08-18 stsp /* user selected '..' */
3753 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
3755 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
3756 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
3758 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
3759 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
3760 e5a0f69f 2018-08-18 stsp s->entries =
3761 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3762 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
3763 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
3764 e5a0f69f 2018-08-18 stsp s->selected_entry =
3765 e5a0f69f 2018-08-18 stsp parent->selected_entry;
3766 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
3767 e5a0f69f 2018-08-18 stsp free(parent);
3768 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
3769 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3770 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
3771 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
3772 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
3775 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
3776 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
3777 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
3780 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
3781 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
3782 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
3783 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
3784 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
3785 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3786 e5a0f69f 2018-08-18 stsp s->tree = child;
3787 e5a0f69f 2018-08-18 stsp s->entries =
3788 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3789 e5a0f69f 2018-08-18 stsp s->selected = 0;
3790 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3791 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
3792 669b5ffa 2018-10-07 stsp struct tog_view *blame_view;
3793 669b5ffa 2018-10-07 stsp int begin_x = view_is_parent_view(view) ?
3794 669b5ffa 2018-10-07 stsp view_split_begin_x(view->begin_x) : 0;
3796 669b5ffa 2018-10-07 stsp err = blame_tree_entry(&blame_view, begin_x,
3797 8b473291 2019-02-21 stsp s->selected_entry, &s->parents,
3798 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
3801 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3802 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3804 669b5ffa 2018-10-07 stsp return err;
3805 669b5ffa 2018-10-07 stsp err = view_set_child(view, blame_view);
3806 669b5ffa 2018-10-07 stsp if (err) {
3807 669b5ffa 2018-10-07 stsp view_close(blame_view);
3810 669b5ffa 2018-10-07 stsp *focus_view = blame_view;
3811 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3813 669b5ffa 2018-10-07 stsp *new_view = blame_view;
3816 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3817 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3818 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3824 ffd1d5e5 2018-06-23 stsp return err;
3827 ffd1d5e5 2018-06-23 stsp __dead static void
3828 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3831 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3832 ffd1d5e5 2018-06-23 stsp getprogname());
3836 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3837 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3839 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3840 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3841 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3842 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3843 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3844 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3845 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3846 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3848 5221c383 2018-08-01 stsp struct tog_view *view;
3850 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3851 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3852 d188b9a6 2019-01-04 stsp NULL) == -1)
3853 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3856 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3857 ffd1d5e5 2018-06-23 stsp switch (ch) {
3859 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3862 17020d27 2019-03-07 stsp usage_tree();
3863 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3867 ffd1d5e5 2018-06-23 stsp argc -= optind;
3868 ffd1d5e5 2018-06-23 stsp argv += optind;
3870 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3871 52185f70 2019-02-05 stsp struct got_worktree *worktree;
3872 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
3873 52185f70 2019-02-05 stsp if (cwd == NULL)
3874 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3875 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3876 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3877 52185f70 2019-02-05 stsp goto done;
3878 52185f70 2019-02-05 stsp if (worktree) {
3879 52185f70 2019-02-05 stsp free(cwd);
3880 52185f70 2019-02-05 stsp repo_path =
3881 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3882 52185f70 2019-02-05 stsp got_worktree_close(worktree);
3884 52185f70 2019-02-05 stsp repo_path = cwd;
3885 52185f70 2019-02-05 stsp if (repo_path == NULL) {
3886 52185f70 2019-02-05 stsp error = got_error_from_errno();
3887 52185f70 2019-02-05 stsp goto done;
3889 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3890 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3891 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3892 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3894 ffd1d5e5 2018-06-23 stsp usage_log();
3896 a915003a 2019-02-05 stsp init_curses();
3898 d188b9a6 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
3899 d188b9a6 2019-01-04 stsp if (error)
3900 52185f70 2019-02-05 stsp goto done;
3902 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3903 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3904 52185f70 2019-02-05 stsp goto done;
3906 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
3907 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3909 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
3910 15a94983 2018-12-23 stsp commit_id_arg);
3911 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3912 ffd1d5e5 2018-06-23 stsp goto done;
3914 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3915 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3916 ffd1d5e5 2018-06-23 stsp goto done;
3918 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
3919 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
3920 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3921 8b473291 2019-02-21 stsp goto done;
3923 8b473291 2019-02-21 stsp SIMPLEQ_INIT(&refs);
3924 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
3925 8b473291 2019-02-21 stsp if (error)
3926 ffd1d5e5 2018-06-23 stsp goto done;
3928 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3929 5221c383 2018-08-01 stsp if (view == NULL) {
3930 5221c383 2018-08-01 stsp error = got_error_from_errno();
3931 5221c383 2018-08-01 stsp goto done;
3933 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
3934 ad80ab7b 2018-08-04 stsp if (error)
3935 ad80ab7b 2018-08-04 stsp goto done;
3936 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3938 52185f70 2019-02-05 stsp free(repo_path);
3939 ffd1d5e5 2018-06-23 stsp free(commit_id);
3940 ffd1d5e5 2018-06-23 stsp if (commit)
3941 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3943 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3945 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3946 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3947 ffd1d5e5 2018-06-23 stsp return error;
3950 4ed7e80c 2018-05-20 stsp __dead static void
3951 9f7d7167 2018-04-29 stsp usage(void)
3955 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3956 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3957 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3958 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3959 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3964 c2301be8 2018-04-30 stsp static char **
3965 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3967 c2301be8 2018-04-30 stsp char **argv;
3968 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3970 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3971 c2301be8 2018-04-30 stsp if (argv == NULL)
3972 c2301be8 2018-04-30 stsp err(1, "calloc");
3973 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3974 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3975 c2301be8 2018-04-30 stsp err(1, "calloc");
3976 c2301be8 2018-04-30 stsp if (arg1) {
3977 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3978 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3979 c2301be8 2018-04-30 stsp err(1, "calloc");
3982 c2301be8 2018-04-30 stsp return argv;
3986 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3988 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3989 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3990 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3991 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3993 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
3995 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3996 9f7d7167 2018-04-29 stsp switch (ch) {
3998 9f7d7167 2018-04-29 stsp hflag = 1;
4002 9f7d7167 2018-04-29 stsp /* NOTREACHED */
4006 9f7d7167 2018-04-29 stsp argc -= optind;
4007 9f7d7167 2018-04-29 stsp argv += optind;
4008 9f7d7167 2018-04-29 stsp optind = 0;
4009 c2301be8 2018-04-30 stsp optreset = 1;
4011 c2301be8 2018-04-30 stsp if (argc == 0) {
4012 f29d3e89 2018-06-23 stsp if (hflag)
4014 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
4015 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
4016 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
4021 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
4022 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
4023 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
4024 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
4025 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
4026 9f7d7167 2018-04-29 stsp if (hflag)
4027 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
4031 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
4032 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
4033 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
4034 c2301be8 2018-04-30 stsp if (repo_path) {
4035 c2301be8 2018-04-30 stsp struct got_repository *repo;
4036 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
4037 c2301be8 2018-04-30 stsp if (error == NULL)
4038 c2301be8 2018-04-30 stsp got_repo_close(repo);
4040 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
4041 c2301be8 2018-04-30 stsp if (error) {
4042 f29d3e89 2018-06-23 stsp if (hflag) {
4043 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
4044 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
4048 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
4049 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
4050 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
4051 ad7de8d9 2018-04-30 stsp free(repo_path);
4054 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
4055 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
4057 c2301be8 2018-04-30 stsp free(repo_path);
4061 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4062 9f7d7167 2018-04-29 stsp if (error)
4063 9f7d7167 2018-04-29 stsp goto done;
4066 c2301be8 2018-04-30 stsp free(cmd_argv);
4067 9f7d7167 2018-04-29 stsp if (error)
4068 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);