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 1dd54920 2019-05-11 stsp #include "got_path.h"
51 b7165be3 2019-02-05 stsp #include "got_worktree.h"
54 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 9f7d7167 2018-04-29 stsp #ifndef nitems
63 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 9f7d7167 2018-04-29 stsp struct tog_cmd {
67 c2301be8 2018-04-30 stsp const char *name;
68 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
69 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
70 c2301be8 2018-04-30 stsp const char *descr;
73 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
74 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
75 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
76 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
77 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
79 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
80 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
81 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
82 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
84 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
85 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
86 9f7d7167 2018-04-29 stsp "show repository history" },
87 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
88 9f7d7167 2018-04-29 stsp "compare files and directories" },
89 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
90 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
91 ffd1d5e5 2018-06-23 stsp { "tree", cmd_tree, usage_tree,
92 ffd1d5e5 2018-06-23 stsp "browse trees in repository" },
95 d6b05b5b 2018-08-04 stsp enum tog_view_type {
96 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
97 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
98 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
99 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
102 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
103 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
104 ba4f502b 2018-08-04 stsp struct got_object_id *id;
105 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
108 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
109 ba4f502b 2018-08-04 stsp struct commit_queue {
110 ba4f502b 2018-08-04 stsp int ncommits;
111 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
114 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
115 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
117 15a087fe 2019-02-21 stsp int first_displayed_line;
118 15a087fe 2019-02-21 stsp int last_displayed_line;
120 15a087fe 2019-02-21 stsp int diff_context;
121 15a087fe 2019-02-21 stsp struct got_repository *repo;
122 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
124 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
125 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
128 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
130 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
131 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
132 1a76625f 2018-10-22 stsp int commits_needed;
133 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
134 1a76625f 2018-10-22 stsp struct commit_queue *commits;
135 1a76625f 2018-10-22 stsp const char *in_repo_path;
136 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
137 1a76625f 2018-10-22 stsp struct got_repository *repo;
138 1a76625f 2018-10-22 stsp int log_complete;
139 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
140 1a76625f 2018-10-22 stsp struct tog_view *view;
141 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
142 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
145 1a76625f 2018-10-22 stsp struct tog_log_view_state {
146 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
147 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
148 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
149 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
150 b01e7d3b 2018-08-04 stsp int selected;
151 b01e7d3b 2018-08-04 stsp char *in_repo_path;
152 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
153 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
154 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
155 1a76625f 2018-10-22 stsp sig_atomic_t quit;
156 1a76625f 2018-10-22 stsp pthread_t thread;
157 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
160 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
161 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
162 e9424729 2018-08-04 stsp int nlines;
164 e9424729 2018-08-04 stsp struct tog_view *view;
165 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
169 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
170 e9424729 2018-08-04 stsp const char *path;
171 e9424729 2018-08-04 stsp struct got_repository *repo;
172 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
173 e9424729 2018-08-04 stsp int *complete;
176 e9424729 2018-08-04 stsp struct tog_blame {
178 e9424729 2018-08-04 stsp size_t filesize;
179 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
180 6fcac457 2018-11-19 stsp int nlines;
181 e9424729 2018-08-04 stsp pthread_t thread;
182 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
183 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
184 e9424729 2018-08-04 stsp const char *path;
187 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
188 7cbe629d 2018-08-04 stsp int first_displayed_line;
189 7cbe629d 2018-08-04 stsp int last_displayed_line;
190 7cbe629d 2018-08-04 stsp int selected_line;
191 7cbe629d 2018-08-04 stsp int blame_complete;
194 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
195 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
196 e5a0f69f 2018-08-18 stsp char *path;
197 7cbe629d 2018-08-04 stsp struct got_repository *repo;
198 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
199 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
200 e9424729 2018-08-04 stsp struct tog_blame blame;
203 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
204 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
205 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
206 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
207 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
208 ad80ab7b 2018-08-04 stsp int selected;
211 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
213 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
214 ad80ab7b 2018-08-04 stsp char *tree_label;
215 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
216 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
217 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
218 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
219 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
220 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
221 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
222 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
223 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
224 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
225 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
229 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
231 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
232 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
233 669b5ffa 2018-10-07 stsp * there is enough screen estate.
235 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
236 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
238 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
239 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
240 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
242 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
243 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
245 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
247 cc3c9aac 2018-08-01 stsp struct tog_view {
248 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
249 26ed57b2 2018-05-19 stsp WINDOW *window;
250 26ed57b2 2018-05-19 stsp PANEL *panel;
251 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
252 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
253 1004088d 2018-09-29 stsp int focussed;
254 669b5ffa 2018-10-07 stsp struct tog_view *parent;
255 669b5ffa 2018-10-07 stsp struct tog_view *child;
256 669b5ffa 2018-10-07 stsp int child_focussed;
258 5dc9f4bc 2018-08-04 stsp /* type-specific state */
259 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
261 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
262 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
263 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
264 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
267 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
268 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
269 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
270 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
273 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
274 fb872ab2 2019-02-21 stsp struct got_object_id *, struct got_object_id *, struct tog_view *,
275 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
276 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
277 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
278 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
279 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
281 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
282 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *,
283 8b473291 2019-02-21 stsp struct got_repository *, const char *, int);
284 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
285 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
286 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
287 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
289 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
290 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *, struct got_repository *);
291 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
292 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
293 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
294 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
296 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
297 8b473291 2019-02-21 stsp struct got_tree_object *, struct got_object_id *,
298 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
299 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
300 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
301 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
302 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
304 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
306 25791caa 2018-10-24 stsp static void
307 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
309 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
312 e5a0f69f 2018-08-18 stsp static const struct got_error *
313 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
315 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
317 669b5ffa 2018-10-07 stsp if (view->child) {
318 669b5ffa 2018-10-07 stsp view_close(view->child);
319 669b5ffa 2018-10-07 stsp view->child = NULL;
321 e5a0f69f 2018-08-18 stsp if (view->close)
322 e5a0f69f 2018-08-18 stsp err = view->close(view);
323 ea5e7bb5 2018-08-01 stsp if (view->panel)
324 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
325 ea5e7bb5 2018-08-01 stsp if (view->window)
326 ea5e7bb5 2018-08-01 stsp delwin(view->window);
327 ea5e7bb5 2018-08-01 stsp free(view);
328 e5a0f69f 2018-08-18 stsp return err;
331 ea5e7bb5 2018-08-01 stsp static struct tog_view *
332 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
333 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
335 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
337 ea5e7bb5 2018-08-01 stsp if (view == NULL)
338 ea5e7bb5 2018-08-01 stsp return NULL;
340 d6b05b5b 2018-08-04 stsp view->type = type;
341 f7d12f7e 2018-08-01 stsp view->lines = LINES;
342 f7d12f7e 2018-08-01 stsp view->cols = COLS;
343 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
344 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
345 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
346 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
347 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
348 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
349 96a765a8 2018-08-04 stsp view_close(view);
350 ea5e7bb5 2018-08-01 stsp return NULL;
352 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
353 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
354 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
355 96a765a8 2018-08-04 stsp view_close(view);
356 ea5e7bb5 2018-08-01 stsp return NULL;
359 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
360 ea5e7bb5 2018-08-01 stsp return view;
364 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
366 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
368 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
371 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
373 5c60c32a 2018-10-18 stsp static const struct got_error *
374 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
376 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
378 5c60c32a 2018-10-18 stsp view->begin_y = 0;
379 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
380 5c60c32a 2018-10-18 stsp view->nlines = LINES;
381 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
382 5c60c32a 2018-10-18 stsp view->lines = LINES;
383 5c60c32a 2018-10-18 stsp view->cols = COLS;
384 5c60c32a 2018-10-18 stsp err = view_resize(view);
386 5c60c32a 2018-10-18 stsp return err;
388 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
389 230a42bd 2019-05-11 jcs return got_error_prefix_errno("mvwin");
391 5c60c32a 2018-10-18 stsp return NULL;
394 5c60c32a 2018-10-18 stsp static const struct got_error *
395 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
397 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
399 5c60c32a 2018-10-18 stsp view->begin_x = 0;
400 5c60c32a 2018-10-18 stsp view->begin_y = 0;
401 5c60c32a 2018-10-18 stsp view->nlines = LINES;
402 5c60c32a 2018-10-18 stsp view->ncols = COLS;
403 5c60c32a 2018-10-18 stsp view->lines = LINES;
404 5c60c32a 2018-10-18 stsp view->cols = COLS;
405 5c60c32a 2018-10-18 stsp err = view_resize(view);
407 5c60c32a 2018-10-18 stsp return err;
409 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
410 230a42bd 2019-05-11 jcs return got_error_prefix_errno("mvwin");
412 5c60c32a 2018-10-18 stsp return NULL;
416 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
418 5c60c32a 2018-10-18 stsp return view->parent == NULL;
421 4d8c2215 2018-08-19 stsp static const struct got_error *
422 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
424 f7d12f7e 2018-08-01 stsp int nlines, ncols;
426 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
427 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
429 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
431 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
432 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
434 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
436 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
437 230a42bd 2019-05-11 jcs return got_error_prefix_errno("wresize");
438 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
439 230a42bd 2019-05-11 jcs return got_error_prefix_errno("replace_panel");
440 25791caa 2018-10-24 stsp wclear(view->window);
442 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
443 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
444 0cf4efb1 2018-09-29 stsp view->lines = LINES;
445 0cf4efb1 2018-09-29 stsp view->cols = COLS;
447 6e3e5d9c 2018-10-18 stsp if (view->child) {
448 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
449 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
450 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
451 5c60c32a 2018-10-18 stsp if (view->child->focussed)
452 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
454 5c60c32a 2018-10-18 stsp show_panel(view->panel);
456 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
457 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
461 5c60c32a 2018-10-18 stsp return NULL;
464 669b5ffa 2018-10-07 stsp static const struct got_error *
465 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
467 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
469 669b5ffa 2018-10-07 stsp if (view->child == NULL)
470 669b5ffa 2018-10-07 stsp return NULL;
472 669b5ffa 2018-10-07 stsp err = view_close(view->child);
473 669b5ffa 2018-10-07 stsp view->child = NULL;
474 669b5ffa 2018-10-07 stsp return err;
477 669b5ffa 2018-10-07 stsp static const struct got_error *
478 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
480 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
482 669b5ffa 2018-10-07 stsp view->child = child;
483 669b5ffa 2018-10-07 stsp child->parent = view;
484 669b5ffa 2018-10-07 stsp return err;
488 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
490 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
494 34bc9ec9 2019-02-22 stsp * Erase all content of the view. Can be used to "flash" the view because
495 34bc9ec9 2019-02-22 stsp * the view loop will redraw it quickly, providing a more subtle visual
496 34bc9ec9 2019-02-22 stsp * effect than curs_flash(3) would provide.
498 25791caa 2018-10-24 stsp static void
499 34bc9ec9 2019-02-22 stsp view_flash(struct tog_view *view)
501 34bc9ec9 2019-02-22 stsp werase(view->window);
502 34bc9ec9 2019-02-22 stsp update_panels();
503 34bc9ec9 2019-02-22 stsp doupdate();
506 34bc9ec9 2019-02-22 stsp static void
507 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
509 25791caa 2018-10-24 stsp int cols, lines;
510 25791caa 2018-10-24 stsp struct winsize size;
512 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
513 25791caa 2018-10-24 stsp cols = 80; /* Default */
514 25791caa 2018-10-24 stsp lines = 24;
516 25791caa 2018-10-24 stsp cols = size.ws_col;
517 25791caa 2018-10-24 stsp lines = size.ws_row;
519 25791caa 2018-10-24 stsp resize_term(lines, cols);
522 0cf4efb1 2018-09-29 stsp static const struct got_error *
523 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
524 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
525 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
527 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
528 669b5ffa 2018-10-07 stsp struct tog_view *v;
529 1a76625f 2018-10-22 stsp int ch, errcode;
531 e5a0f69f 2018-08-18 stsp *new = NULL;
532 e5a0f69f 2018-08-18 stsp *dead = NULL;
533 0cf4efb1 2018-09-29 stsp *focus = NULL;
535 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
536 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
537 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
538 1a76625f 2018-10-22 stsp if (errcode)
539 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
540 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
541 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
542 1a76625f 2018-10-22 stsp if (errcode)
543 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
544 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
546 25791caa 2018-10-24 stsp if (tog_sigwinch_received) {
547 25791caa 2018-10-24 stsp tog_resizeterm();
548 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
549 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
550 25791caa 2018-10-24 stsp err = view_resize(v);
552 25791caa 2018-10-24 stsp return err;
553 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
555 25791caa 2018-10-24 stsp return err;
559 e5a0f69f 2018-08-18 stsp switch (ch) {
563 669b5ffa 2018-10-07 stsp if (view->child) {
564 669b5ffa 2018-10-07 stsp *focus = view->child;
565 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
566 669b5ffa 2018-10-07 stsp } else if (view->parent) {
567 669b5ffa 2018-10-07 stsp *focus = view->parent;
568 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 0;
572 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
573 e5a0f69f 2018-08-18 stsp *dead = view;
579 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
580 669b5ffa 2018-10-07 stsp if (view->child == NULL)
582 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view->child)) {
583 669b5ffa 2018-10-07 stsp *focus = view->child;
584 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
585 669b5ffa 2018-10-07 stsp err = view_fullscreen(view->child);
587 669b5ffa 2018-10-07 stsp err = view_splitscreen(view->child);
590 669b5ffa 2018-10-07 stsp err = view->child->input(new, dead, focus,
591 669b5ffa 2018-10-07 stsp view->child, KEY_RESIZE);
593 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view)) {
594 669b5ffa 2018-10-07 stsp *focus = view;
595 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 1;
596 669b5ffa 2018-10-07 stsp err = view_fullscreen(view);
598 669b5ffa 2018-10-07 stsp err = view_splitscreen(view);
602 669b5ffa 2018-10-07 stsp err = view->input(new, dead, focus, view,
603 669b5ffa 2018-10-07 stsp KEY_RESIZE);
606 0cf4efb1 2018-09-29 stsp case KEY_RESIZE:
609 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
613 e5a0f69f 2018-08-18 stsp return err;
617 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
619 0cf4efb1 2018-09-29 stsp PANEL *panel;
620 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
622 669b5ffa 2018-10-07 stsp if (view->parent)
623 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
625 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
626 0cf4efb1 2018-09-29 stsp if (panel == NULL)
629 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
630 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
631 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
635 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
637 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
638 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
640 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
642 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
645 669b5ffa 2018-10-07 stsp return view->focussed;
648 bcbd79e2 2018-08-19 stsp static const struct got_error *
649 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
651 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
652 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
653 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
654 fd823528 2018-10-22 stsp int fast_refresh = 10;
655 1a76625f 2018-10-22 stsp int done = 0, errcode;
657 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
658 1a76625f 2018-10-22 stsp if (errcode)
659 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
661 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
662 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
664 a81bf10d 2018-09-29 stsp main_view = view;
665 1004088d 2018-09-29 stsp view->focussed = 1;
666 878940b7 2018-09-29 stsp err = view->show(view);
668 0cf4efb1 2018-09-29 stsp return err;
669 0cf4efb1 2018-09-29 stsp update_panels();
670 0cf4efb1 2018-09-29 stsp doupdate();
671 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
672 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
673 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
674 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
676 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
677 e4197bf9 2018-08-18 stsp view, &views);
680 e5a0f69f 2018-08-18 stsp if (dead_view) {
681 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
683 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
684 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
685 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
686 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
687 669b5ffa 2018-10-07 stsp prev = view->parent;
689 669b5ffa 2018-10-07 stsp if (dead_view->parent)
690 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
692 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
694 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
695 a81bf10d 2018-09-29 stsp if (err || dead_view == main_view)
698 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
699 0cf4efb1 2018-09-29 stsp if (focus_view)
700 0cf4efb1 2018-09-29 stsp view = focus_view;
701 669b5ffa 2018-10-07 stsp else if (prev)
702 669b5ffa 2018-10-07 stsp view = prev;
703 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
704 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
705 0cf4efb1 2018-09-29 stsp tog_view_list_head);
707 0cf4efb1 2018-09-29 stsp view = NULL;
708 669b5ffa 2018-10-07 stsp if (view) {
709 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
710 669b5ffa 2018-10-07 stsp focus_view = view->child;
712 669b5ffa 2018-10-07 stsp focus_view = view;
716 bcbd79e2 2018-08-19 stsp if (new_view) {
717 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
718 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
719 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
720 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
722 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
723 86c66b02 2018-10-18 stsp err = view_close(v);
728 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
729 fed7eaa8 2018-10-24 stsp view = new_view;
730 878940b7 2018-09-29 stsp if (focus_view == NULL)
731 878940b7 2018-09-29 stsp focus_view = new_view;
733 0cf4efb1 2018-09-29 stsp if (focus_view) {
734 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
736 0cf4efb1 2018-09-29 stsp view->focussed = 0;
737 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
738 0cf4efb1 2018-09-29 stsp view = focus_view;
739 878940b7 2018-09-29 stsp if (new_view)
740 878940b7 2018-09-29 stsp show_panel(new_view->panel);
741 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
742 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
744 669b5ffa 2018-10-07 stsp if (view) {
745 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
746 758194b5 2018-10-24 stsp view->focussed = 1;
747 758194b5 2018-10-24 stsp show_panel(view->panel);
748 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
749 758194b5 2018-10-24 stsp show_panel(view->child->panel);
750 1a76625f 2018-10-22 stsp focus_view = view;
752 669b5ffa 2018-10-07 stsp if (view->parent) {
753 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
757 669b5ffa 2018-10-07 stsp err = view->show(view);
760 669b5ffa 2018-10-07 stsp if (view->child) {
761 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
765 1a76625f 2018-10-22 stsp update_panels();
766 1a76625f 2018-10-22 stsp doupdate();
770 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
771 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
772 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
773 e5a0f69f 2018-08-18 stsp view_close(view);
776 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
777 1a76625f 2018-10-22 stsp if (errcode)
778 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
780 e5a0f69f 2018-08-18 stsp return err;
783 4ed7e80c 2018-05-20 stsp __dead static void
784 9f7d7167 2018-04-29 stsp usage_log(void)
787 c70c5802 2018-08-01 stsp fprintf(stderr,
788 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
789 9f7d7167 2018-04-29 stsp getprogname());
793 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
794 80ddbec8 2018-04-29 stsp static const struct got_error *
795 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
797 00dfcb92 2018-06-11 stsp char *vis = NULL;
798 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
800 963b370f 2018-05-20 stsp *ws = NULL;
801 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
802 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
803 00dfcb92 2018-06-11 stsp int vislen;
804 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
805 230a42bd 2019-05-11 jcs return got_error_prefix_errno("mbstowcs");
807 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
808 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
810 00dfcb92 2018-06-11 stsp return err;
811 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
812 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
813 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("mbstowcs"); /* give up */
818 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
819 a7f50699 2018-06-11 stsp if (*ws == NULL) {
820 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("calloc");
824 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
825 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("mbstowcs");
830 963b370f 2018-05-20 stsp *ws = NULL;
833 963b370f 2018-05-20 stsp return err;
836 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
837 963b370f 2018-05-20 stsp static const struct got_error *
838 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
840 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
841 963b370f 2018-05-20 stsp int cols = 0;
842 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
843 963b370f 2018-05-20 stsp size_t wlen;
846 963b370f 2018-05-20 stsp *wlinep = NULL;
847 b700b5d6 2018-07-10 stsp *widthp = 0;
849 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
851 963b370f 2018-05-20 stsp return err;
854 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
855 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
856 963b370f 2018-05-20 stsp switch (width) {
862 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
863 b700b5d6 2018-07-10 stsp cols += width;
867 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
868 67ceb59d 2019-03-03 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
872 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("wcwidth");
876 963b370f 2018-05-20 stsp wline[i] = L'\0';
877 b700b5d6 2018-07-10 stsp if (widthp)
878 b700b5d6 2018-07-10 stsp *widthp = cols;
881 963b370f 2018-05-20 stsp free(wline);
883 963b370f 2018-05-20 stsp *wlinep = wline;
884 963b370f 2018-05-20 stsp return err;
887 8b473291 2019-02-21 stsp static const struct got_error*
888 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
889 8b473291 2019-02-21 stsp struct got_object_id *id)
891 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
892 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
894 8b473291 2019-02-21 stsp const char *name;
896 8b473291 2019-02-21 stsp *refs_str = NULL;
898 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
899 8b473291 2019-02-21 stsp if (got_object_id_cmp(re->id, id) != 0)
901 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
902 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
904 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
906 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
908 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
910 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
912 8b473291 2019-02-21 stsp s = *refs_str;
913 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
914 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
915 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
917 8b473291 2019-02-21 stsp *refs_str = NULL;
923 8b473291 2019-02-21 stsp return err;
926 963b370f 2018-05-20 stsp static const struct got_error *
927 5813d178 2019-03-09 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
929 5813d178 2019-03-09 stsp char *smallerthan, *at;
931 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
932 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
933 5813d178 2019-03-09 stsp author = smallerthan + 1;
934 5813d178 2019-03-09 stsp at = strchr(author, '@');
936 5813d178 2019-03-09 stsp *at = '\0';
937 5813d178 2019-03-09 stsp return format_line(wauthor, author_width, author, limit);
940 5813d178 2019-03-09 stsp static const struct got_error *
941 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
942 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
943 5813d178 2019-03-09 stsp int author_display_cols)
945 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
946 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
947 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
948 5813d178 2019-03-09 stsp char *author = NULL;
949 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
950 bb737323 2018-05-20 stsp int author_width, logmsg_width;
951 5813d178 2019-03-09 stsp char *newline, *line = NULL;
952 bb737323 2018-05-20 stsp int col, limit;
953 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
954 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
955 ccb26ccd 2018-11-05 stsp struct tm tm;
956 45d799e2 2018-12-23 stsp time_t committer_time;
958 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
959 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
960 230a42bd 2019-05-11 jcs return got_error_prefix_errno("localtime_r");
961 ccb26ccd 2018-11-05 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
962 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
963 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
965 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
966 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
968 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
969 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
970 b39d25c7 2018-07-10 stsp col = limit + 1;
971 b39d25c7 2018-07-10 stsp if (col > avail)
974 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
975 5813d178 2019-03-09 stsp if (author == NULL) {
976 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
979 5813d178 2019-03-09 stsp err = format_author(&wauthor, &author_width, author, avail - col);
982 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
983 bb737323 2018-05-20 stsp col += author_width;
984 5813d178 2019-03-09 stsp while (col <= avail && author_width < author_display_cols + 2) {
985 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
987 bb737323 2018-05-20 stsp author_width++;
989 9c2eaf34 2018-05-20 stsp if (col > avail)
992 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
993 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
994 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
997 bb737323 2018-05-20 stsp logmsg = logmsg0;
998 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1000 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1001 bb737323 2018-05-20 stsp if (newline)
1002 bb737323 2018-05-20 stsp *newline = '\0';
1003 bb737323 2018-05-20 stsp limit = avail - col;
1004 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1006 bb737323 2018-05-20 stsp goto done;
1007 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1008 bb737323 2018-05-20 stsp col += logmsg_width;
1009 bb737323 2018-05-20 stsp while (col <= avail) {
1010 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1014 80ddbec8 2018-04-29 stsp free(logmsg0);
1015 bb737323 2018-05-20 stsp free(wlogmsg);
1016 5813d178 2019-03-09 stsp free(author);
1017 bb737323 2018-05-20 stsp free(wauthor);
1018 80ddbec8 2018-04-29 stsp free(line);
1019 80ddbec8 2018-04-29 stsp return err;
1022 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1023 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1024 899d86c2 2018-05-10 stsp struct got_object_id *id)
1026 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1028 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1029 80ddbec8 2018-04-29 stsp if (entry == NULL)
1030 899d86c2 2018-05-10 stsp return NULL;
1032 899d86c2 2018-05-10 stsp entry->id = id;
1033 99db9666 2018-05-07 stsp entry->commit = commit;
1034 899d86c2 2018-05-10 stsp return entry;
1037 99db9666 2018-05-07 stsp static void
1038 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1040 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1042 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1043 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1044 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1045 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1046 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1047 99db9666 2018-05-07 stsp free(entry);
1050 99db9666 2018-05-07 stsp static void
1051 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1053 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1054 99db9666 2018-05-07 stsp pop_commit(commits);
1057 c4972b91 2018-05-07 stsp static const struct got_error *
1058 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1059 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
1061 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1062 93e45b7c 2018-09-24 stsp int nqueued = 0;
1065 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1066 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1067 1a76625f 2018-10-22 stsp * while updating the display.
1069 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
1070 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1071 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1072 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1073 1a76625f 2018-10-22 stsp int errcode;
1075 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1076 9ba79e04 2018-06-11 stsp if (err) {
1077 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1079 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
1080 93e45b7c 2018-09-24 stsp minqueue, repo);
1082 ecb28ae0 2018-07-16 stsp return err;
1086 ecb28ae0 2018-07-16 stsp if (id == NULL)
1089 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1092 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1093 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1094 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("alloc_commit_queue_entry");
1098 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1099 1a76625f 2018-10-22 stsp if (errcode) {
1100 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_lock");
1104 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1105 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1106 ecb28ae0 2018-07-16 stsp nqueued++;
1107 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1109 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1110 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1111 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1112 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1115 9ba79e04 2018-06-11 stsp return err;
1118 99db9666 2018-05-07 stsp static const struct got_error *
1119 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1121 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1122 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1124 9ba79e04 2018-06-11 stsp *head_id = NULL;
1126 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1128 99db9666 2018-05-07 stsp return err;
1130 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1131 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1132 9ba79e04 2018-06-11 stsp if (err) {
1133 9ba79e04 2018-06-11 stsp *head_id = NULL;
1134 99db9666 2018-05-07 stsp return err;
1137 9ba79e04 2018-06-11 stsp return NULL;
1140 0553a4e3 2018-04-30 stsp static const struct got_error *
1141 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1142 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1143 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1144 8b473291 2019-02-21 stsp struct got_reflist_head *refs, const char *path, int commits_needed)
1146 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1147 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1148 ecb28ae0 2018-07-16 stsp int ncommits, width;
1149 5813d178 2019-03-09 stsp int author_cols = 10;
1150 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1151 8b473291 2019-02-21 stsp char *refs_str = NULL;
1152 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1154 e0d42f60 2018-07-22 stsp entry = first;
1155 e0d42f60 2018-07-22 stsp ncommits = 0;
1156 e0d42f60 2018-07-22 stsp while (entry) {
1157 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1158 e0d42f60 2018-07-22 stsp *selected = entry;
1161 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1162 e0d42f60 2018-07-22 stsp ncommits++;
1165 1a76625f 2018-10-22 stsp if (*selected) {
1166 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1168 ecb28ae0 2018-07-16 stsp return err;
1169 8b473291 2019-02-21 stsp if (refs) {
1170 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, (*selected)->id);
1172 8b473291 2019-02-21 stsp goto done;
1176 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1177 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1179 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1180 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1181 8b473291 2019-02-21 stsp commits_needed > 0 ? "loading... " :
1182 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1183 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1184 8b473291 2019-02-21 stsp goto done;
1187 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1188 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1189 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1190 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1191 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1192 1a76625f 2018-10-22 stsp header = NULL;
1193 1a76625f 2018-10-22 stsp goto done;
1195 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1196 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1197 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1198 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1199 1a76625f 2018-10-22 stsp header = NULL;
1200 1a76625f 2018-10-22 stsp goto done;
1202 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1204 1a76625f 2018-10-22 stsp goto done;
1206 2814baeb 2018-08-01 stsp werase(view->window);
1208 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1209 a3404814 2018-09-02 stsp wstandout(view->window);
1210 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1211 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1212 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1215 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1216 a3404814 2018-09-02 stsp wstandend(view->window);
1217 ecb28ae0 2018-07-16 stsp free(wline);
1218 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1219 1a76625f 2018-10-22 stsp goto done;
1221 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1222 899d86c2 2018-05-10 stsp entry = first;
1223 5813d178 2019-03-09 stsp ncommits = 0;
1224 5813d178 2019-03-09 stsp while (entry) {
1225 5813d178 2019-03-09 stsp char *author;
1226 5813d178 2019-03-09 stsp wchar_t *wauthor;
1227 5813d178 2019-03-09 stsp int width;
1228 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1230 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1231 5813d178 2019-03-09 stsp if (author == NULL) {
1232 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
1233 5813d178 2019-03-09 stsp goto done;
1235 5813d178 2019-03-09 stsp err = format_author(&wauthor, &width, author, COLS);
1236 5813d178 2019-03-09 stsp if (author_cols < width)
1237 5813d178 2019-03-09 stsp author_cols = width;
1238 5813d178 2019-03-09 stsp free(wauthor);
1239 5813d178 2019-03-09 stsp free(author);
1240 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1243 5813d178 2019-03-09 stsp entry = first;
1244 899d86c2 2018-05-10 stsp *last = first;
1245 867c6645 2018-07-10 stsp ncommits = 0;
1246 899d86c2 2018-05-10 stsp while (entry) {
1247 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1249 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1250 2814baeb 2018-08-01 stsp wstandout(view->window);
1251 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1252 5813d178 2019-03-09 stsp author_cols);
1253 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1254 2814baeb 2018-08-01 stsp wstandend(view->window);
1257 0553a4e3 2018-04-30 stsp ncommits++;
1258 899d86c2 2018-05-10 stsp *last = entry;
1259 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1262 1a57306a 2018-09-02 stsp view_vborder(view);
1264 1a76625f 2018-10-22 stsp free(id_str);
1265 8b473291 2019-02-21 stsp free(refs_str);
1266 1a76625f 2018-10-22 stsp free(ncommits_str);
1267 1a76625f 2018-10-22 stsp free(header);
1268 80ddbec8 2018-04-29 stsp return err;
1271 07b55e75 2018-05-10 stsp static void
1272 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1273 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1274 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1276 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1277 07b55e75 2018-05-10 stsp int nscrolled = 0;
1279 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1280 34bc9ec9 2019-02-22 stsp if (*first_displayed_entry == entry) {
1281 34bc9ec9 2019-02-22 stsp view_flash(view);
1285 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1286 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1287 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1288 07b55e75 2018-05-10 stsp if (entry) {
1289 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1290 07b55e75 2018-05-10 stsp nscrolled++;
1295 aa075928 2018-05-10 stsp static const struct got_error *
1296 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1297 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1299 5e224a3e 2019-02-22 stsp int errcode;
1300 8a42fca8 2019-02-22 stsp int max_wait = 20;
1302 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1304 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1305 5e224a3e 2019-02-22 stsp if (*log_complete)
1308 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1309 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1310 7aafa0d1 2019-02-22 stsp if (errcode)
1311 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1312 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1313 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1314 7aafa0d1 2019-02-22 stsp if (errcode)
1315 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1316 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1317 7aafa0d1 2019-02-22 stsp pthread_yield();
1318 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1319 7aafa0d1 2019-02-22 stsp if (errcode)
1320 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1321 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1323 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1325 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1326 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1327 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1328 5e224a3e 2019-02-22 stsp * with few commits in history.
1330 7aafa0d1 2019-02-22 stsp return NULL;
1334 5e224a3e 2019-02-22 stsp return NULL;
1337 5e224a3e 2019-02-22 stsp static const struct got_error *
1338 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1339 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1340 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1341 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1342 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1344 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1345 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1346 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1348 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1349 5e224a3e 2019-02-22 stsp return NULL;
1351 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1352 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1354 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1355 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1357 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1358 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1359 5e224a3e 2019-02-22 stsp need_commits);
1361 5e224a3e 2019-02-22 stsp return err;
1365 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1366 34bc9ec9 2019-02-22 stsp if (pentry == NULL) {
1367 34bc9ec9 2019-02-22 stsp if (*log_complete)
1368 34bc9ec9 2019-02-22 stsp view_flash(view);
1372 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1374 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1375 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1377 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1378 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1380 dd0a52c1 2018-05-20 stsp return err;
1383 cd0acaa7 2018-05-20 stsp static const struct got_error *
1384 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1385 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1386 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1387 8b473291 2019-02-21 stsp struct got_repository *repo)
1389 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1390 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1391 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1393 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1394 15a94983 2018-12-23 stsp if (diff_view == NULL)
1395 230a42bd 2019-05-11 jcs return got_error_prefix_errno("view_open");
1397 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1398 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1399 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1400 e5a0f69f 2018-08-18 stsp if (err == NULL)
1401 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1402 cd0acaa7 2018-05-20 stsp return err;
1405 80ddbec8 2018-04-29 stsp static const struct got_error *
1406 0cf4efb1 2018-09-29 stsp browse_commit(struct tog_view **new_view, int begin_x,
1407 8b473291 2019-02-21 stsp struct commit_queue_entry *entry, struct got_reflist_head *refs,
1408 8b473291 2019-02-21 stsp struct got_repository *repo)
1410 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1411 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1412 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1414 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1415 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1417 9343a5fb 2018-06-23 stsp return err;
1419 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1420 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1421 230a42bd 2019-05-11 jcs return got_error_prefix_errno("view_open");
1423 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1425 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1427 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1428 1a76625f 2018-10-22 stsp return err;
1431 1a76625f 2018-10-22 stsp static void *
1432 1a76625f 2018-10-22 stsp log_thread(void *arg)
1434 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1435 1a76625f 2018-10-22 stsp int errcode = 0;
1436 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1437 1a76625f 2018-10-22 stsp int done = 0;
1439 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1441 1a76625f 2018-10-22 stsp return (void *)err;
1443 1a76625f 2018-10-22 stsp while (!done && !err) {
1444 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1445 1a76625f 2018-10-22 stsp a->in_repo_path);
1446 1a76625f 2018-10-22 stsp if (err) {
1447 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1448 1a76625f 2018-10-22 stsp return (void *)err;
1449 1a76625f 2018-10-22 stsp err = NULL;
1451 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1452 1a76625f 2018-10-22 stsp a->commits_needed--;
1454 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1455 3abe8080 2019-04-10 stsp if (errcode) {
1456 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1457 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1459 3abe8080 2019-04-10 stsp } else if (*a->quit)
1461 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1462 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1463 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1464 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1468 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1469 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1470 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1471 1a76625f 2018-10-22 stsp &tog_mutex);
1472 1a76625f 2018-10-22 stsp if (errcode)
1473 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1474 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1477 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1478 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1479 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1480 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1482 3abe8080 2019-04-10 stsp a->log_complete = 1;
1483 1a76625f 2018-10-22 stsp return (void *)err;
1486 1a76625f 2018-10-22 stsp static const struct got_error *
1487 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1489 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1490 1a76625f 2018-10-22 stsp int errcode;
1492 1a76625f 2018-10-22 stsp if (s->thread) {
1493 1a76625f 2018-10-22 stsp s->quit = 1;
1494 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1495 1a76625f 2018-10-22 stsp if (errcode)
1496 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1497 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1498 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1499 1a76625f 2018-10-22 stsp if (errcode)
1500 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1501 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1502 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1503 1a76625f 2018-10-22 stsp if (errcode)
1504 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1505 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1506 1a76625f 2018-10-22 stsp if (errcode)
1507 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1508 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1509 1a76625f 2018-10-22 stsp s->thread = NULL;
1512 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1513 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1514 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1516 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1517 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1518 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1521 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1522 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1523 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1526 9343a5fb 2018-06-23 stsp return err;
1529 9343a5fb 2018-06-23 stsp static const struct got_error *
1530 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1532 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1533 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1535 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1536 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1537 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1538 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1539 1a76625f 2018-10-22 stsp free(s->start_id);
1540 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1541 1a76625f 2018-10-22 stsp return err;
1544 1a76625f 2018-10-22 stsp static const struct got_error *
1545 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1546 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
1547 8b473291 2019-02-21 stsp const char *path, int check_disk)
1549 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1550 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1551 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1552 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1553 1a76625f 2018-10-22 stsp int errcode;
1555 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1556 ecb28ae0 2018-07-16 stsp if (err != NULL)
1557 ecb28ae0 2018-07-16 stsp goto done;
1559 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1560 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1561 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1563 8b473291 2019-02-21 stsp s->refs = refs;
1564 fb2756b9 2018-08-04 stsp s->repo = repo;
1565 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1566 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1567 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_object_id_dup");
1568 5036bf37 2018-09-24 stsp goto done;
1571 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1572 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1573 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1575 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1577 1a76625f 2018-10-22 stsp goto done;
1578 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1579 1a76625f 2018-10-22 stsp 0, thread_repo);
1581 1a76625f 2018-10-22 stsp goto done;
1583 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1584 1a76625f 2018-10-22 stsp if (errcode) {
1585 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
1586 1a76625f 2018-10-22 stsp goto done;
1589 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1590 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1591 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1592 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1593 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1594 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1595 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1596 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1597 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1598 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1599 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1602 1a76625f 2018-10-22 stsp close_log_view(view);
1603 ba4f502b 2018-08-04 stsp return err;
1606 e5a0f69f 2018-08-18 stsp static const struct got_error *
1607 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1609 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1611 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1612 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1613 2b380cc8 2018-10-24 stsp &s->thread_args);
1614 2b380cc8 2018-10-24 stsp if (errcode)
1615 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
1618 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1619 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1620 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
1621 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1624 e5a0f69f 2018-08-18 stsp static const struct got_error *
1625 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1626 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1628 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1629 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1630 5036bf37 2018-09-24 stsp char *parent_path;
1631 669b5ffa 2018-10-07 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
1632 669b5ffa 2018-10-07 stsp int begin_x = 0;
1634 e5a0f69f 2018-08-18 stsp switch (ch) {
1636 1a76625f 2018-10-22 stsp s->quit = 1;
1639 e5a0f69f 2018-08-18 stsp case KEY_UP:
1642 2b380cc8 2018-10-24 stsp if (s->first_displayed_entry == NULL)
1644 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1645 e5a0f69f 2018-08-18 stsp s->selected--;
1646 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1648 34bc9ec9 2019-02-22 stsp scroll_up(view, &s->first_displayed_entry, 1,
1649 e5a0f69f 2018-08-18 stsp &s->commits);
1651 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1652 2b380cc8 2018-10-24 stsp if (s->first_displayed_entry == NULL)
1654 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1655 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1656 34bc9ec9 2019-02-22 stsp if (s->selected == 0) {
1657 34bc9ec9 2019-02-22 stsp view_flash(view);
1660 e5a0f69f 2018-08-18 stsp s->selected = 0;
1663 34bc9ec9 2019-02-22 stsp scroll_up(view, &s->first_displayed_entry,
1664 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1667 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1670 2b380cc8 2018-10-24 stsp if (s->first_displayed_entry == NULL)
1672 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1673 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1674 e5a0f69f 2018-08-18 stsp s->selected++;
1677 34bc9ec9 2019-02-22 stsp err = scroll_down(view, &s->first_displayed_entry, 1,
1678 93e45b7c 2018-09-24 stsp &s->last_displayed_entry, &s->commits,
1679 1a76625f 2018-10-22 stsp &s->thread_args.log_complete,
1680 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1681 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1683 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1684 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1685 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1686 2b380cc8 2018-10-24 stsp if (first == NULL)
1688 34bc9ec9 2019-02-22 stsp err = scroll_down(view, &s->first_displayed_entry,
1689 93e45b7c 2018-09-24 stsp view->nlines, &s->last_displayed_entry,
1690 1a76625f 2018-10-22 stsp &s->commits, &s->thread_args.log_complete,
1691 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1692 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1693 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1694 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1695 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1696 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1697 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1698 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1700 e5a0f69f 2018-08-18 stsp err = NULL;
1703 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1704 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1705 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1706 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1707 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1709 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1710 e5a0f69f 2018-08-18 stsp case '\r':
1711 2b380cc8 2018-10-24 stsp if (s->selected_entry == NULL)
1713 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1714 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1715 669b5ffa 2018-10-07 stsp err = open_diff_view_for_commit(&diff_view, begin_x,
1716 fb872ab2 2019-02-21 stsp s->selected_entry->commit, s->selected_entry->id,
1717 8b473291 2019-02-21 stsp view, s->refs, s->repo);
1720 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1721 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1723 669b5ffa 2018-10-07 stsp return err;
1724 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
1725 669b5ffa 2018-10-07 stsp if (err) {
1726 669b5ffa 2018-10-07 stsp view_close(diff_view);
1729 55566b34 2018-11-05 stsp *focus_view = diff_view;
1730 55566b34 2018-11-05 stsp view->child_focussed = 1;
1732 669b5ffa 2018-10-07 stsp *new_view = diff_view;
1735 2b380cc8 2018-10-24 stsp if (s->selected_entry == NULL)
1737 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1738 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1739 669b5ffa 2018-10-07 stsp err = browse_commit(&tree_view, begin_x,
1740 8b473291 2019-02-21 stsp s->selected_entry, s->refs, s->repo);
1743 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1744 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1746 669b5ffa 2018-10-07 stsp return err;
1747 669b5ffa 2018-10-07 stsp err = view_set_child(view, tree_view);
1748 669b5ffa 2018-10-07 stsp if (err) {
1749 669b5ffa 2018-10-07 stsp view_close(tree_view);
1752 669b5ffa 2018-10-07 stsp *focus_view = tree_view;
1753 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1755 669b5ffa 2018-10-07 stsp *new_view = tree_view;
1757 5036bf37 2018-09-24 stsp case KEY_BACKSPACE:
1758 5036bf37 2018-09-24 stsp if (strcmp(s->in_repo_path, "/") == 0)
1760 5036bf37 2018-09-24 stsp parent_path = dirname(s->in_repo_path);
1761 5036bf37 2018-09-24 stsp if (parent_path && strcmp(parent_path, ".") != 0) {
1762 5036bf37 2018-09-24 stsp struct tog_view *lv;
1763 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1765 1a76625f 2018-10-22 stsp return err;
1766 0cf4efb1 2018-09-29 stsp lv = view_open(view->nlines, view->ncols,
1767 0cf4efb1 2018-09-29 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
1768 5036bf37 2018-09-24 stsp if (lv == NULL)
1769 230a42bd 2019-05-11 jcs return got_error_prefix_errno(
1770 230a42bd 2019-05-11 jcs "view_open");
1771 8b473291 2019-02-21 stsp err = open_log_view(lv, s->start_id, s->refs,
1772 8b473291 2019-02-21 stsp s->repo, parent_path, 0);
1774 1a76625f 2018-10-22 stsp return err;;
1775 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1776 669b5ffa 2018-10-07 stsp *new_view = lv;
1778 669b5ffa 2018-10-07 stsp view_set_child(view->parent, lv);
1779 34095730 2018-10-22 stsp *focus_view = lv;
1781 1a76625f 2018-10-22 stsp return NULL;
1788 80ddbec8 2018-04-29 stsp return err;
1791 4ed7e80c 2018-05-20 stsp static const struct got_error *
1792 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
1794 c2db6724 2019-01-04 stsp const struct got_error *error;
1796 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
1797 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("unveil", repo_path);
1799 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
1800 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("unveil", worktree_path);
1802 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
1803 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("unveil", "/tmp");
1805 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
1806 c2db6724 2019-01-04 stsp if (error != NULL)
1807 c2db6724 2019-01-04 stsp return error;
1809 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
1810 230a42bd 2019-05-11 jcs return got_error_prefix_errno("unveil");
1812 c2db6724 2019-01-04 stsp return NULL;
1815 a915003a 2019-02-05 stsp static void
1816 a915003a 2019-02-05 stsp init_curses(void)
1818 a915003a 2019-02-05 stsp initscr();
1820 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
1823 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
1824 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
1825 a915003a 2019-02-05 stsp curs_set(0);
1826 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
1829 c2db6724 2019-01-04 stsp static const struct got_error *
1830 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1832 80ddbec8 2018-04-29 stsp const struct got_error *error;
1833 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1834 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
1835 8b473291 2019-02-21 stsp struct got_reflist_head refs;
1836 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1837 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1838 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1840 04cc582a 2018-08-01 stsp struct tog_view *view;
1842 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
1844 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1845 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1846 c2db6724 2019-01-04 stsp NULL) == -1)
1847 80ddbec8 2018-04-29 stsp err(1, "pledge");
1850 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1851 80ddbec8 2018-04-29 stsp switch (ch) {
1853 80ddbec8 2018-04-29 stsp start_commit = optarg;
1856 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1857 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1858 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1861 17020d27 2019-03-07 stsp usage_log();
1862 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1866 80ddbec8 2018-04-29 stsp argc -= optind;
1867 80ddbec8 2018-04-29 stsp argv += optind;
1869 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1870 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1871 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1872 ecb28ae0 2018-07-16 stsp goto done;
1874 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
1875 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1876 963f97a1 2019-03-18 stsp goto done;
1877 963f97a1 2019-03-18 stsp error = NULL;
1879 963f97a1 2019-03-18 stsp if (argc == 0) {
1880 963f97a1 2019-03-18 stsp path = strdup("");
1881 963f97a1 2019-03-18 stsp if (path == NULL) {
1882 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1883 ecb28ae0 2018-07-16 stsp goto done;
1885 963f97a1 2019-03-18 stsp } else if (argc == 1) {
1886 963f97a1 2019-03-18 stsp if (worktree) {
1887 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1889 963f97a1 2019-03-18 stsp if (error)
1890 963f97a1 2019-03-18 stsp goto done;
1892 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
1893 963f97a1 2019-03-18 stsp if (path == NULL) {
1894 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1895 963f97a1 2019-03-18 stsp goto done;
1899 963f97a1 2019-03-18 stsp usage_log();
1901 963f97a1 2019-03-18 stsp repo_path = worktree ?
1902 963f97a1 2019-03-18 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1903 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
1904 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1905 963f97a1 2019-03-18 stsp goto done;
1908 a915003a 2019-02-05 stsp init_curses();
1910 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1911 80ddbec8 2018-04-29 stsp if (error != NULL)
1912 ecb28ae0 2018-07-16 stsp goto done;
1914 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
1915 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1916 c02c541e 2019-03-29 stsp if (error)
1917 c02c541e 2019-03-29 stsp goto done;
1919 15a94983 2018-12-23 stsp if (start_commit == NULL)
1920 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1922 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&start_id, repo,
1923 15a94983 2018-12-23 stsp start_commit);
1924 80ddbec8 2018-04-29 stsp if (error != NULL)
1925 8b473291 2019-02-21 stsp goto done;
1927 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
1928 8b473291 2019-02-21 stsp if (error)
1929 ecb28ae0 2018-07-16 stsp goto done;
1931 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1932 04cc582a 2018-08-01 stsp if (view == NULL) {
1933 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("view_open");
1934 04cc582a 2018-08-01 stsp goto done;
1936 8b473291 2019-02-21 stsp error = open_log_view(view, start_id, &refs, repo, path, 1);
1937 ba4f502b 2018-08-04 stsp if (error)
1938 ba4f502b 2018-08-04 stsp goto done;
1939 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1941 ecb28ae0 2018-07-16 stsp free(repo_path);
1942 ecb28ae0 2018-07-16 stsp free(cwd);
1943 ecb28ae0 2018-07-16 stsp free(path);
1944 899d86c2 2018-05-10 stsp free(start_id);
1946 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1947 ec142235 2019-03-07 stsp if (worktree)
1948 ec142235 2019-03-07 stsp got_worktree_close(worktree);
1949 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1950 80ddbec8 2018-04-29 stsp return error;
1953 4ed7e80c 2018-05-20 stsp __dead static void
1954 9f7d7167 2018-04-29 stsp usage_diff(void)
1957 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1958 9f7d7167 2018-04-29 stsp getprogname());
1962 b304db33 2018-05-20 stsp static char *
1963 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1965 b304db33 2018-05-20 stsp char *line;
1966 b304db33 2018-05-20 stsp size_t linelen;
1967 b304db33 2018-05-20 stsp size_t lineno;
1968 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1970 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1972 b304db33 2018-05-20 stsp *len = linelen;
1973 b304db33 2018-05-20 stsp return line;
1976 4ed7e80c 2018-05-20 stsp static const struct got_error *
1977 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1978 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1979 a3404814 2018-09-02 stsp char * header)
1981 61e69b96 2018-05-20 stsp const struct got_error *err;
1982 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1983 b304db33 2018-05-20 stsp char *line;
1984 b304db33 2018-05-20 stsp size_t len;
1985 61e69b96 2018-05-20 stsp wchar_t *wline;
1986 e0b650dd 2018-05-20 stsp int width;
1988 26ed57b2 2018-05-19 stsp rewind(f);
1989 f7d12f7e 2018-08-01 stsp werase(view->window);
1991 a3404814 2018-09-02 stsp if (header) {
1992 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1993 a3404814 2018-09-02 stsp if (err) {
1994 a3404814 2018-09-02 stsp return err;
1997 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1998 a3404814 2018-09-02 stsp wstandout(view->window);
1999 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2000 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2001 a3404814 2018-09-02 stsp wstandend(view->window);
2002 a3404814 2018-09-02 stsp if (width < view->ncols)
2003 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2005 a3404814 2018-09-02 stsp if (max_lines <= 1)
2006 a3404814 2018-09-02 stsp return NULL;
2007 a3404814 2018-09-02 stsp max_lines--;
2011 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2012 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2013 26ed57b2 2018-05-19 stsp if (line == NULL) {
2017 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2018 26ed57b2 2018-05-19 stsp free(line);
2022 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2023 61e69b96 2018-05-20 stsp if (err) {
2024 61e69b96 2018-05-20 stsp free(line);
2025 61e69b96 2018-05-20 stsp return err;
2027 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2028 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2029 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2030 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2031 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2032 26ed57b2 2018-05-19 stsp free(line);
2033 2550e4c3 2018-07-13 stsp free(wline);
2034 2550e4c3 2018-07-13 stsp wline = NULL;
2036 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2038 1a57306a 2018-09-02 stsp view_vborder(view);
2040 26ed57b2 2018-05-19 stsp return NULL;
2043 abd2672a 2018-12-23 stsp static char *
2044 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2046 abd2672a 2018-12-23 stsp char *p, *s = ctime_r(time, datebuf);
2047 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2049 abd2672a 2018-12-23 stsp *p = '\0';
2053 4ed7e80c 2018-05-20 stsp static const struct got_error *
2054 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2055 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2057 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2058 abd2672a 2018-12-23 stsp char datebuf[26];
2059 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2060 15a94983 2018-12-23 stsp char *id_str = NULL;
2061 45d799e2 2018-12-23 stsp time_t committer_time;
2062 45d799e2 2018-12-23 stsp const char *author, *committer;
2063 8b473291 2019-02-21 stsp char *refs_str = NULL;
2065 8b473291 2019-02-21 stsp if (refs) {
2066 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, commit_id);
2068 8b473291 2019-02-21 stsp return err;
2071 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2073 abd2672a 2018-12-23 stsp return err;
2075 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2076 15a94983 2018-12-23 stsp if (err) {
2077 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_object_id_str");
2078 15a94983 2018-12-23 stsp goto done;
2081 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2082 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2083 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fprintf");
2084 abd2672a 2018-12-23 stsp goto done;
2086 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2087 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2088 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fprintf");
2089 abd2672a 2018-12-23 stsp goto done;
2091 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2092 abd2672a 2018-12-23 stsp if (fprintf(outfile, "date: %s UTC\n",
2093 45d799e2 2018-12-23 stsp get_datestr(&committer_time, datebuf)) < 0) {
2094 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fprintf");
2095 abd2672a 2018-12-23 stsp goto done;
2097 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2098 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2099 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2100 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2101 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fprintf");
2102 abd2672a 2018-12-23 stsp goto done;
2104 45d799e2 2018-12-23 stsp if (fprintf(outfile, "%s\n",
2105 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(commit)) < 0) {
2106 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fprintf");
2107 abd2672a 2018-12-23 stsp goto done;
2110 abd2672a 2018-12-23 stsp free(id_str);
2111 8b473291 2019-02-21 stsp free(refs_str);
2112 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2113 abd2672a 2018-12-23 stsp return err;
2116 abd2672a 2018-12-23 stsp static const struct got_error *
2117 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2119 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2120 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2121 15a94983 2018-12-23 stsp int obj_type;
2123 511a516b 2018-05-19 stsp f = got_opentemp();
2124 48ae06ee 2018-10-18 stsp if (f == NULL) {
2125 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_opentemp");
2126 48ae06ee 2018-10-18 stsp goto done;
2128 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2129 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fclose");
2130 fb43ecf1 2019-02-11 stsp goto done;
2134 15a94983 2018-12-23 stsp if (s->id1)
2135 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2137 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2139 15a94983 2018-12-23 stsp goto done;
2141 15a94983 2018-12-23 stsp switch (obj_type) {
2142 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2143 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2144 54156555 2018-12-24 stsp s->diff_context, s->repo, f);
2146 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2147 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2148 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
2150 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2151 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2152 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2153 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2155 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2158 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2159 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2160 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2162 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2163 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2164 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2165 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2166 8b473291 2019-02-21 stsp s->repo, f);
2171 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2173 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2174 15a94983 2018-12-23 stsp s->diff_context, s->repo, f);
2178 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2182 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2183 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fflush");
2184 48ae06ee 2018-10-18 stsp return err;
2187 f5215bb9 2019-02-22 stsp static void
2188 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2190 f5215bb9 2019-02-22 stsp werase(view->window);
2191 f5215bb9 2019-02-22 stsp waddstr(view->window, "diffing...");
2192 f5215bb9 2019-02-22 stsp update_panels();
2193 f5215bb9 2019-02-22 stsp doupdate();
2196 48ae06ee 2018-10-18 stsp static const struct got_error *
2197 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2198 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2199 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2201 48ae06ee 2018-10-18 stsp const struct got_error *err;
2203 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2204 15a94983 2018-12-23 stsp int type1, type2;
2205 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2207 15a94983 2018-12-23 stsp return err;
2208 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2210 15a94983 2018-12-23 stsp return err;
2212 15a94983 2018-12-23 stsp if (type1 != type2)
2213 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2216 15a94983 2018-12-23 stsp if (id1) {
2217 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2218 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2219 230a42bd 2019-05-11 jcs return got_error_prefix_errno("got_object_id_dup");
2221 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2223 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2224 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2225 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2226 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2227 230a42bd 2019-05-11 jcs return got_error_prefix_errno("got_object_id_dup");
2229 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2230 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2231 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2232 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2233 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2234 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2235 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2237 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2238 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2239 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2241 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2242 48ae06ee 2018-10-18 stsp if (err) {
2243 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2244 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2245 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2246 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2247 48ae06ee 2018-10-18 stsp return err;
2250 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2251 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2252 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2254 5dc9f4bc 2018-08-04 stsp return NULL;
2257 e5a0f69f 2018-08-18 stsp static const struct got_error *
2258 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2260 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2262 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2263 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2264 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2265 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2266 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2267 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fclose");
2268 e5a0f69f 2018-08-18 stsp return err;
2271 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2272 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2274 a3404814 2018-09-02 stsp const struct got_error *err;
2275 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2276 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2278 a3404814 2018-09-02 stsp if (s->id1) {
2279 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2281 a3404814 2018-09-02 stsp return err;
2283 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2285 a3404814 2018-09-02 stsp return err;
2287 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2288 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2289 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
2290 a3404814 2018-09-02 stsp free(id_str1);
2291 a3404814 2018-09-02 stsp free(id_str2);
2292 a3404814 2018-09-02 stsp return err;
2294 a3404814 2018-09-02 stsp free(id_str1);
2295 a3404814 2018-09-02 stsp free(id_str2);
2297 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2298 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2302 15a087fe 2019-02-21 stsp static const struct got_error *
2303 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2304 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2306 d7a04538 2019-02-21 stsp const struct got_error *err;
2307 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
2308 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
2309 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
2311 15a087fe 2019-02-21 stsp free(s->id2);
2312 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2313 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2314 230a42bd 2019-05-11 jcs return got_error_prefix_errno("got_object_id_dup");
2316 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2318 d7a04538 2019-02-21 stsp return err;
2319 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
2320 15a087fe 2019-02-21 stsp free(s->id1);
2321 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
2322 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2323 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
2324 15a087fe 2019-02-21 stsp return NULL;
2327 0cf4efb1 2018-09-29 stsp static const struct got_error *
2328 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2329 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2331 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2332 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2333 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
2334 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
2337 e5a0f69f 2018-08-18 stsp switch (ch) {
2339 e5a0f69f 2018-08-18 stsp case KEY_UP:
2340 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
2341 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2343 34bc9ec9 2019-02-22 stsp view_flash(view);
2345 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2346 34bc9ec9 2019-02-22 stsp if (s->first_displayed_line == 1) {
2347 34bc9ec9 2019-02-22 stsp view_flash(view);
2351 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
2352 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2353 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2356 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2357 e5a0f69f 2018-08-18 stsp if (!s->eof)
2358 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2360 34bc9ec9 2019-02-22 stsp view_flash(view);
2362 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2364 34bc9ec9 2019-02-22 stsp if (s->eof) {
2365 34bc9ec9 2019-02-22 stsp view_flash(view);
2369 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
2370 e5a0f69f 2018-08-18 stsp char *line;
2371 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
2372 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2373 e5a0f69f 2018-08-18 stsp if (line == NULL)
2378 48ae06ee 2018-10-18 stsp if (s->diff_context > 0) {
2379 48ae06ee 2018-10-18 stsp s->diff_context--;
2380 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2381 48ae06ee 2018-10-18 stsp err = create_diff(s);
2385 4a8520aa 2018-10-18 stsp if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2386 48ae06ee 2018-10-18 stsp s->diff_context++;
2387 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2388 48ae06ee 2018-10-18 stsp err = create_diff(s);
2393 fb872ab2 2019-02-21 stsp if (s->log_view == NULL)
2395 fb872ab2 2019-02-21 stsp ls = &s->log_view->state.log;
2396 fb872ab2 2019-02-21 stsp entry = TAILQ_PREV(ls->selected_entry,
2397 15a087fe 2019-02-21 stsp commit_queue_head, entry);
2398 15a087fe 2019-02-21 stsp if (entry == NULL)
2401 6524637e 2019-02-21 stsp err = input_log_view(NULL, NULL, NULL, s->log_view,
2406 15a087fe 2019-02-21 stsp err = set_selected_commit(s, entry);
2410 15a087fe 2019-02-21 stsp s->first_displayed_line = 1;
2411 15a087fe 2019-02-21 stsp s->last_displayed_line = view->nlines;
2413 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2414 15a087fe 2019-02-21 stsp err = create_diff(s);
2418 fb872ab2 2019-02-21 stsp if (s->log_view == NULL)
2420 fb872ab2 2019-02-21 stsp ls = &s->log_view->state.log;
2422 6e73b0d6 2019-02-22 stsp if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2423 5e224a3e 2019-02-22 stsp ls->thread_args.commits_needed++;
2425 5e224a3e 2019-02-22 stsp /* Display "loading..." in log view. */
2426 5e224a3e 2019-02-22 stsp show_log_view(s->log_view);
2427 5e224a3e 2019-02-22 stsp update_panels();
2428 5e224a3e 2019-02-22 stsp doupdate();
2430 6e73b0d6 2019-02-22 stsp err = trigger_log_thread(1 /* load_all */,
2431 6e73b0d6 2019-02-22 stsp &ls->thread_args.commits_needed,
2432 6e73b0d6 2019-02-22 stsp &ls->thread_args.log_complete,
2433 6e73b0d6 2019-02-22 stsp &ls->thread_args.need_commits);
2437 fb872ab2 2019-02-21 stsp err = input_log_view(NULL, NULL, NULL, s->log_view,
2438 fb872ab2 2019-02-21 stsp KEY_DOWN);
2442 fb872ab2 2019-02-21 stsp entry = TAILQ_NEXT(ls->selected_entry, entry);
2443 15a087fe 2019-02-21 stsp if (entry == NULL)
2446 15a087fe 2019-02-21 stsp err = set_selected_commit(s, entry);
2450 15a087fe 2019-02-21 stsp s->first_displayed_line = 1;
2451 15a087fe 2019-02-21 stsp s->last_displayed_line = view->nlines;
2453 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2454 15a087fe 2019-02-21 stsp err = create_diff(s);
2460 bcbd79e2 2018-08-19 stsp return err;
2463 4ed7e80c 2018-05-20 stsp static const struct got_error *
2464 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2466 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2467 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2468 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2469 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2470 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2471 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2473 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2475 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2477 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2478 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2479 eb6600df 2019-01-04 stsp NULL) == -1)
2480 26ed57b2 2018-05-19 stsp err(1, "pledge");
2483 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2484 26ed57b2 2018-05-19 stsp switch (ch) {
2486 17020d27 2019-03-07 stsp usage_diff();
2487 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2491 26ed57b2 2018-05-19 stsp argc -= optind;
2492 26ed57b2 2018-05-19 stsp argv += optind;
2494 26ed57b2 2018-05-19 stsp if (argc == 0) {
2495 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2496 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2497 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2498 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2499 230a42bd 2019-05-11 jcs return got_error_prefix_errno("getcwd");
2500 15a94983 2018-12-23 stsp id_str1 = argv[0];
2501 15a94983 2018-12-23 stsp id_str2 = argv[1];
2502 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2503 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2504 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2505 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("realpath", argv[0]);
2506 15a94983 2018-12-23 stsp id_str1 = argv[1];
2507 15a94983 2018-12-23 stsp id_str2 = argv[2];
2509 26ed57b2 2018-05-19 stsp usage_diff();
2511 a915003a 2019-02-05 stsp init_curses();
2513 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2514 eb6600df 2019-01-04 stsp if (error)
2515 eb6600df 2019-01-04 stsp goto done;
2517 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
2518 26ed57b2 2018-05-19 stsp if (error)
2519 26ed57b2 2018-05-19 stsp goto done;
2521 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
2522 26ed57b2 2018-05-19 stsp if (error)
2523 26ed57b2 2018-05-19 stsp goto done;
2525 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
2526 26ed57b2 2018-05-19 stsp if (error)
2527 26ed57b2 2018-05-19 stsp goto done;
2529 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2530 8b473291 2019-02-21 stsp if (error)
2531 8b473291 2019-02-21 stsp goto done;
2533 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2534 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2535 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("view_open");
2536 ea5e7bb5 2018-08-01 stsp goto done;
2538 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2539 5dc9f4bc 2018-08-04 stsp if (error)
2540 5dc9f4bc 2018-08-04 stsp goto done;
2541 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2543 c02c541e 2019-03-29 stsp free(repo_path);
2544 26ed57b2 2018-05-19 stsp got_repo_close(repo);
2545 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2546 26ed57b2 2018-05-19 stsp return error;
2549 4ed7e80c 2018-05-20 stsp __dead static void
2550 9f7d7167 2018-04-29 stsp usage_blame(void)
2553 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2554 9f7d7167 2018-04-29 stsp getprogname());
2558 84451b3e 2018-07-10 stsp struct tog_blame_line {
2559 84451b3e 2018-07-10 stsp int annotated;
2560 84451b3e 2018-07-10 stsp struct got_object_id *id;
2563 4ed7e80c 2018-05-20 stsp static const struct got_error *
2564 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2565 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2566 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2567 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2569 84451b3e 2018-07-10 stsp const struct got_error *err;
2570 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2571 84451b3e 2018-07-10 stsp char *line;
2572 84451b3e 2018-07-10 stsp size_t len;
2573 84451b3e 2018-07-10 stsp wchar_t *wline;
2574 b700b5d6 2018-07-10 stsp int width, wlimit;
2575 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2576 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2577 ab089a2a 2018-07-12 stsp char *id_str;
2579 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2581 ab089a2a 2018-07-12 stsp return err;
2583 84451b3e 2018-07-10 stsp rewind(f);
2584 f7d12f7e 2018-08-01 stsp werase(view->window);
2586 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
2587 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
2588 ab089a2a 2018-07-12 stsp free(id_str);
2589 ab089a2a 2018-07-12 stsp return err;
2592 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2593 ab089a2a 2018-07-12 stsp free(line);
2594 2550e4c3 2018-07-13 stsp line = NULL;
2595 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2596 a3404814 2018-09-02 stsp wstandout(view->window);
2597 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2598 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2599 a3404814 2018-09-02 stsp wstandend(view->window);
2600 2550e4c3 2018-07-13 stsp free(wline);
2601 2550e4c3 2018-07-13 stsp wline = NULL;
2602 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2603 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2605 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2606 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2607 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
2608 ab089a2a 2018-07-12 stsp free(id_str);
2609 230a42bd 2019-05-11 jcs return got_error_prefix_errno("asprintf");
2611 ab089a2a 2018-07-12 stsp free(id_str);
2612 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2613 3f60a8ef 2018-07-10 stsp free(line);
2614 2550e4c3 2018-07-13 stsp line = NULL;
2616 3f60a8ef 2018-07-10 stsp return err;
2617 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2618 2550e4c3 2018-07-13 stsp free(wline);
2619 2550e4c3 2018-07-13 stsp wline = NULL;
2620 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2621 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2624 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2625 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2626 84451b3e 2018-07-10 stsp if (line == NULL) {
2630 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2631 84451b3e 2018-07-10 stsp free(line);
2635 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2636 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2637 84451b3e 2018-07-10 stsp if (err) {
2638 84451b3e 2018-07-10 stsp free(line);
2639 84451b3e 2018-07-10 stsp return err;
2642 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2643 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2645 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2646 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2647 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2648 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2649 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2650 84451b3e 2018-07-10 stsp char *id_str;
2651 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2652 84451b3e 2018-07-10 stsp if (err) {
2653 84451b3e 2018-07-10 stsp free(line);
2654 2550e4c3 2018-07-13 stsp free(wline);
2655 84451b3e 2018-07-10 stsp return err;
2657 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2658 84451b3e 2018-07-10 stsp free(id_str);
2659 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2661 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2662 ee41ec32 2018-07-10 stsp prev_id = NULL;
2665 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2666 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2667 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2670 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2671 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2672 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2673 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2674 84451b3e 2018-07-10 stsp free(line);
2675 2550e4c3 2018-07-13 stsp free(wline);
2676 2550e4c3 2018-07-13 stsp wline = NULL;
2678 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2680 1a57306a 2018-09-02 stsp view_vborder(view);
2682 84451b3e 2018-07-10 stsp return NULL;
2685 84451b3e 2018-07-10 stsp static const struct got_error *
2686 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2688 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2689 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2690 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2691 1a76625f 2018-10-22 stsp int errcode;
2693 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2694 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2695 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2697 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2698 1a76625f 2018-10-22 stsp if (errcode)
2699 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
2701 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2702 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2703 d68a0a7d 2018-07-10 stsp goto done;
2706 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2707 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2709 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2710 d68a0a7d 2018-07-10 stsp if (line->annotated)
2711 d68a0a7d 2018-07-10 stsp goto done;
2713 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2714 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2715 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_object_id_dup");
2716 84451b3e 2018-07-10 stsp goto done;
2718 84451b3e 2018-07-10 stsp line->annotated = 1;
2720 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2721 1a76625f 2018-10-22 stsp if (errcode)
2722 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2723 84451b3e 2018-07-10 stsp return err;
2726 84451b3e 2018-07-10 stsp static void *
2727 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2729 18430de3 2018-07-10 stsp const struct got_error *err;
2730 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
2731 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
2732 1a76625f 2018-10-22 stsp int errcode;
2734 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2735 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
2737 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2738 1a76625f 2018-10-22 stsp if (errcode)
2739 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
2740 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2742 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
2743 c9beca56 2018-07-22 stsp ta->repo = NULL;
2744 c9beca56 2018-07-22 stsp *ta->complete = 1;
2746 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2747 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2748 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2750 18430de3 2018-07-10 stsp return (void *)err;
2753 245d91c1 2018-07-12 stsp static struct got_object_id *
2754 15a94983 2018-12-23 stsp get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2755 15a94983 2018-12-23 stsp int selected_line)
2757 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2759 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2760 245d91c1 2018-07-12 stsp if (!line->annotated)
2761 245d91c1 2018-07-12 stsp return NULL;
2763 245d91c1 2018-07-12 stsp return line->id;
2766 b880a918 2018-07-10 stsp static const struct got_error *
2767 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2769 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2772 245d91c1 2018-07-12 stsp if (blame->thread) {
2773 1a76625f 2018-10-22 stsp int errcode;
2774 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2775 1a76625f 2018-10-22 stsp if (errcode)
2776 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2777 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2778 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
2779 1a76625f 2018-10-22 stsp if (errcode)
2780 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2781 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2782 1a76625f 2018-10-22 stsp if (errcode)
2783 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2784 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2785 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2786 245d91c1 2018-07-12 stsp err = NULL;
2787 245d91c1 2018-07-12 stsp blame->thread = NULL;
2789 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2790 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2791 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2793 245d91c1 2018-07-12 stsp if (blame->f) {
2794 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
2795 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fclose");
2796 245d91c1 2018-07-12 stsp blame->f = NULL;
2798 57670559 2018-12-23 stsp if (blame->lines) {
2799 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
2800 57670559 2018-12-23 stsp free(blame->lines[i].id);
2801 57670559 2018-12-23 stsp free(blame->lines);
2802 57670559 2018-12-23 stsp blame->lines = NULL;
2804 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2805 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2807 245d91c1 2018-07-12 stsp return err;
2810 245d91c1 2018-07-12 stsp static const struct got_error *
2811 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2812 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
2813 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
2814 245d91c1 2018-07-12 stsp struct got_repository *repo)
2816 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2817 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2818 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2819 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2820 15a94983 2018-12-23 stsp int obj_type;
2822 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2824 15a94983 2018-12-23 stsp return err;
2825 15a94983 2018-12-23 stsp if (obj_id == NULL)
2826 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
2828 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
2830 84451b3e 2018-07-10 stsp goto done;
2832 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2833 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2834 84451b3e 2018-07-10 stsp goto done;
2837 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2839 a70480e0 2018-06-23 stsp goto done;
2840 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2841 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2842 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_opentemp");
2843 84451b3e 2018-07-10 stsp goto done;
2845 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2846 245d91c1 2018-07-12 stsp blame->f, blob);
2848 84451b3e 2018-07-10 stsp goto done;
2850 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2851 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2852 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("calloc");
2853 84451b3e 2018-07-10 stsp goto done;
2856 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2858 bd24772e 2018-07-11 stsp goto done;
2860 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2861 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2862 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2863 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2864 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2865 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_object_id_dup");
2866 245d91c1 2018-07-12 stsp goto done;
2868 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2870 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2871 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2872 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2873 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2874 245d91c1 2018-07-12 stsp *blame_complete = 0;
2878 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2879 27d434c2 2018-09-15 stsp free(obj_id);
2881 245d91c1 2018-07-12 stsp stop_blame(blame);
2882 245d91c1 2018-07-12 stsp return err;
2885 245d91c1 2018-07-12 stsp static const struct got_error *
2886 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2887 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
2888 8b473291 2019-02-21 stsp struct got_repository *repo)
2890 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2891 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2893 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2895 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2897 7cbe629d 2018-08-04 stsp return err;
2899 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2900 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2901 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2902 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2903 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2904 fb2756b9 2018-08-04 stsp s->path = path;
2905 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2906 230a42bd 2019-05-11 jcs return got_error_prefix_errno("open_blame_view");
2907 fb2756b9 2018-08-04 stsp s->repo = repo;
2908 8b473291 2019-02-21 stsp s->refs = refs;
2909 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2910 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2912 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2913 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2914 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2916 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
2917 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2918 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2919 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2922 e5a0f69f 2018-08-18 stsp static const struct got_error *
2923 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2925 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2926 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2928 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2929 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2931 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2932 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2933 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2934 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2935 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2938 e5a0f69f 2018-08-18 stsp free(s->path);
2940 e5a0f69f 2018-08-18 stsp return err;
2943 7cbe629d 2018-08-04 stsp static const struct got_error *
2944 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2946 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2947 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2948 2b380cc8 2018-10-24 stsp int errcode;
2950 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
2951 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2952 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
2953 2b380cc8 2018-10-24 stsp if (errcode)
2954 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2957 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2958 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2959 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2960 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2962 669b5ffa 2018-10-07 stsp view_vborder(view);
2963 e5a0f69f 2018-08-18 stsp return err;
2966 e5a0f69f 2018-08-18 stsp static const struct got_error *
2967 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2968 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2970 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2971 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2972 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2973 669b5ffa 2018-10-07 stsp int begin_x = 0;
2975 e5a0f69f 2018-08-18 stsp switch (ch) {
2977 e5a0f69f 2018-08-18 stsp s->done = 1;
2980 e5a0f69f 2018-08-18 stsp case KEY_UP:
2981 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2982 e5a0f69f 2018-08-18 stsp s->selected_line--;
2983 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2984 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2985 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2987 34bc9ec9 2019-02-22 stsp view_flash(view);
2989 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2990 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2991 34bc9ec9 2019-02-22 stsp if (s->selected_line == 1) {
2992 34bc9ec9 2019-02-22 stsp view_flash(view);
2995 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2998 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2999 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
3000 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
3002 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
3005 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
3006 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
3007 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
3008 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
3009 e5a0f69f 2018-08-18 stsp s->selected_line++;
3010 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
3011 e5a0f69f 2018-08-18 stsp s->blame.nlines)
3012 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
3014 34bc9ec9 2019-02-22 stsp view_flash(view);
3017 e5a0f69f 2018-08-18 stsp case 'p': {
3018 15a94983 2018-12-23 stsp struct got_object_id *id = NULL;
3019 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
3020 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
3021 15a94983 2018-12-23 stsp if (id == NULL)
3023 15a94983 2018-12-23 stsp if (ch == 'p') {
3024 15a94983 2018-12-23 stsp struct got_commit_object *commit;
3025 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3026 15a94983 2018-12-23 stsp struct got_object_id *blob_id = NULL;
3027 15a94983 2018-12-23 stsp int obj_type;
3028 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit,
3029 15a94983 2018-12-23 stsp s->repo, id);
3032 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3033 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3034 15a94983 2018-12-23 stsp if (pid == NULL) {
3035 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3038 15a94983 2018-12-23 stsp /* Check if path history ends here. */
3039 15a94983 2018-12-23 stsp err = got_object_id_by_path(&blob_id, s->repo,
3040 15a94983 2018-12-23 stsp pid->id, s->path);
3041 15a94983 2018-12-23 stsp if (err) {
3042 15a94983 2018-12-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
3043 15a94983 2018-12-23 stsp err = NULL;
3044 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3047 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo,
3049 15a94983 2018-12-23 stsp free(blob_id);
3050 15a94983 2018-12-23 stsp /* Can't blame non-blob type objects. */
3051 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3052 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3055 15a94983 2018-12-23 stsp err = got_object_qid_alloc(&s->blamed_commit,
3057 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3059 15a94983 2018-12-23 stsp if (got_object_id_cmp(id,
3060 15a94983 2018-12-23 stsp s->blamed_commit->id) == 0)
3062 15a94983 2018-12-23 stsp err = got_object_qid_alloc(&s->blamed_commit,
3067 e5a0f69f 2018-08-18 stsp s->done = 1;
3068 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
3069 e5a0f69f 2018-08-18 stsp s->done = 0;
3070 e5a0f69f 2018-08-18 stsp if (thread_err)
3072 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3073 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
3074 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
3075 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
3076 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
3077 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
3082 e5a0f69f 2018-08-18 stsp case 'B': {
3083 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
3084 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
3085 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
3087 e5a0f69f 2018-08-18 stsp s->done = 1;
3088 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
3089 e5a0f69f 2018-08-18 stsp s->done = 0;
3090 e5a0f69f 2018-08-18 stsp if (thread_err)
3092 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3093 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
3094 e5a0f69f 2018-08-18 stsp s->blamed_commit =
3095 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
3096 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
3097 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
3098 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3099 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3104 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
3105 15a94983 2018-12-23 stsp case '\r': {
3106 15a94983 2018-12-23 stsp struct got_object_id *id = NULL;
3107 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3108 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
3109 15a94983 2018-12-23 stsp id = get_selected_commit_id(s->blame.lines,
3110 15a94983 2018-12-23 stsp s->first_displayed_line, s->selected_line);
3111 3e9926ea 2019-01-04 stsp if (id == NULL)
3113 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, s->repo, id);
3116 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3117 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3118 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
3119 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
3120 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3121 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
3122 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3123 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("view_open");
3126 15a94983 2018-12-23 stsp err = open_diff_view(diff_view, pid ? pid->id : NULL,
3127 8b473291 2019-02-21 stsp id, NULL, s->refs, s->repo);
3128 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3129 e5a0f69f 2018-08-18 stsp if (err) {
3130 e5a0f69f 2018-08-18 stsp view_close(diff_view);
3133 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3134 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3137 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
3138 669b5ffa 2018-10-07 stsp if (err) {
3139 669b5ffa 2018-10-07 stsp view_close(diff_view);
3142 51c68690 2018-11-07 stsp *focus_view = diff_view;
3143 51c68690 2018-11-07 stsp view->child_focussed = 1;
3145 669b5ffa 2018-10-07 stsp *new_view = diff_view;
3150 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
3152 34bc9ec9 2019-02-22 stsp if (s->last_displayed_line >= s->blame.nlines &&
3153 34bc9ec9 2019-02-22 stsp s->selected_line >= MIN(s->blame.nlines,
3154 34bc9ec9 2019-02-22 stsp view->nlines - 2)) {
3155 34bc9ec9 2019-02-22 stsp view_flash(view);
3158 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
3159 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
3160 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
3161 e5a0f69f 2018-08-18 stsp view->nlines - 2);
3164 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
3165 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
3166 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
3167 e5a0f69f 2018-08-18 stsp view->nlines - 2;
3169 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
3170 e5a0f69f 2018-08-18 stsp s->blame.nlines -
3171 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
3173 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3174 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
3175 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
3176 e5a0f69f 2018-08-18 stsp view->nlines - 2);
3182 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
3185 a70480e0 2018-06-23 stsp static const struct got_error *
3186 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
3188 a70480e0 2018-06-23 stsp const struct got_error *error;
3189 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
3190 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3191 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
3192 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3193 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3194 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
3196 e1cd8fed 2018-08-01 stsp struct tog_view *view;
3198 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3200 a70480e0 2018-06-23 stsp #ifndef PROFILE
3201 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3202 8e94dd5b 2019-01-04 stsp NULL) == -1)
3203 a70480e0 2018-06-23 stsp err(1, "pledge");
3206 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3207 a70480e0 2018-06-23 stsp switch (ch) {
3209 a70480e0 2018-06-23 stsp commit_id_str = optarg;
3212 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3213 69069811 2018-08-02 stsp if (repo_path == NULL)
3214 69069811 2018-08-02 stsp err(1, "-r option");
3217 17020d27 2019-03-07 stsp usage_blame();
3218 a70480e0 2018-06-23 stsp /* NOTREACHED */
3222 a70480e0 2018-06-23 stsp argc -= optind;
3223 a70480e0 2018-06-23 stsp argv += optind;
3225 69069811 2018-08-02 stsp if (argc == 1)
3226 69069811 2018-08-02 stsp path = argv[0];
3228 a70480e0 2018-06-23 stsp usage_blame();
3230 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
3231 69069811 2018-08-02 stsp if (cwd == NULL) {
3232 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
3233 69069811 2018-08-02 stsp goto done;
3235 69069811 2018-08-02 stsp if (repo_path == NULL) {
3236 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3237 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3238 69069811 2018-08-02 stsp goto done;
3240 eb41ed75 2019-02-05 stsp error = NULL;
3241 eb41ed75 2019-02-05 stsp if (worktree) {
3242 eb41ed75 2019-02-05 stsp repo_path =
3243 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3244 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
3245 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
3246 eb41ed75 2019-02-05 stsp if (error)
3247 eb41ed75 2019-02-05 stsp goto done;
3249 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
3250 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
3251 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
3252 eb41ed75 2019-02-05 stsp goto done;
3257 a915003a 2019-02-05 stsp init_curses();
3259 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
3260 c02c541e 2019-03-29 stsp if (error != NULL)
3261 8e94dd5b 2019-01-04 stsp goto done;
3263 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3264 c02c541e 2019-03-29 stsp if (error)
3265 92205607 2019-01-04 stsp goto done;
3267 eb41ed75 2019-02-05 stsp if (worktree) {
3268 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3269 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3270 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3271 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3272 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3273 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3274 eb41ed75 2019-02-05 stsp path) == -1) {
3275 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("asprintf");
3276 eb41ed75 2019-02-05 stsp goto done;
3278 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3281 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3283 eb41ed75 2019-02-05 stsp if (error)
3284 69069811 2018-08-02 stsp goto done;
3286 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
3287 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
3288 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3289 a70480e0 2018-06-23 stsp if (error != NULL)
3290 66b4983c 2018-06-23 stsp goto done;
3291 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3292 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
3294 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
3295 15a94983 2018-12-23 stsp commit_id_str);
3297 a19e88aa 2018-06-23 stsp if (error != NULL)
3298 8b473291 2019-02-21 stsp goto done;
3300 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
3301 8b473291 2019-02-21 stsp if (error)
3302 a19e88aa 2018-06-23 stsp goto done;
3304 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3305 e1cd8fed 2018-08-01 stsp if (view == NULL) {
3306 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("view_open");
3307 e1cd8fed 2018-08-01 stsp goto done;
3309 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3310 7cbe629d 2018-08-04 stsp if (error)
3311 7cbe629d 2018-08-04 stsp goto done;
3312 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3314 69069811 2018-08-02 stsp free(repo_path);
3315 69069811 2018-08-02 stsp free(cwd);
3316 a70480e0 2018-06-23 stsp free(commit_id);
3317 eb41ed75 2019-02-05 stsp if (worktree)
3318 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
3320 a70480e0 2018-06-23 stsp got_repo_close(repo);
3321 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3322 a70480e0 2018-06-23 stsp return error;
3325 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3326 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
3327 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
3328 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
3329 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
3330 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
3331 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
3333 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3334 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
3335 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
3336 ffd1d5e5 2018-06-23 stsp int width, n;
3338 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
3340 f7d12f7e 2018-08-01 stsp werase(view->window);
3342 ffd1d5e5 2018-06-23 stsp if (limit == 0)
3343 ffd1d5e5 2018-06-23 stsp return NULL;
3345 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
3347 ffd1d5e5 2018-06-23 stsp return err;
3348 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3349 a3404814 2018-09-02 stsp wstandout(view->window);
3350 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3351 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3352 a3404814 2018-09-02 stsp wstandend(view->window);
3353 2550e4c3 2018-07-13 stsp free(wline);
3354 2550e4c3 2018-07-13 stsp wline = NULL;
3355 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
3356 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3357 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3358 ffd1d5e5 2018-06-23 stsp return NULL;
3359 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
3361 ce52c690 2018-06-23 stsp return err;
3362 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3363 2550e4c3 2018-07-13 stsp free(wline);
3364 2550e4c3 2018-07-13 stsp wline = NULL;
3365 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
3366 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3367 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3368 ffd1d5e5 2018-06-23 stsp return NULL;
3369 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3370 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
3371 a1eca9bb 2018-06-23 stsp return NULL;
3373 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3374 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
3375 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
3376 0cf4efb1 2018-09-29 stsp if (view->focussed)
3377 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3378 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
3380 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
3381 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
3382 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3383 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3384 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3385 ffd1d5e5 2018-06-23 stsp return NULL;
3389 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
3390 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3393 ffd1d5e5 2018-06-23 stsp while (te) {
3394 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
3396 1d13200f 2018-07-12 stsp if (show_ids) {
3397 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
3399 230a42bd 2019-05-11 jcs return got_error_prefix_errno(
3400 230a42bd 2019-05-11 jcs "got_object_id_str");
3402 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3403 d8355ef1 2019-02-10 stsp te->name, S_ISDIR(te->mode) ? "/" :
3404 d8355ef1 2019-02-10 stsp ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3405 1d13200f 2018-07-12 stsp free(id_str);
3406 230a42bd 2019-05-11 jcs return got_error_prefix_errno("asprintf");
3408 1d13200f 2018-07-12 stsp free(id_str);
3409 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
3410 ffd1d5e5 2018-06-23 stsp if (err) {
3411 ffd1d5e5 2018-06-23 stsp free(line);
3414 ffd1d5e5 2018-06-23 stsp if (n == selected) {
3415 0cf4efb1 2018-09-29 stsp if (view->focussed)
3416 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3417 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
3419 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3420 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
3421 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3422 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
3423 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3424 ffd1d5e5 2018-06-23 stsp free(line);
3425 2550e4c3 2018-07-13 stsp free(wline);
3426 2550e4c3 2018-07-13 stsp wline = NULL;
3428 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3429 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
3430 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3432 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3435 ffd1d5e5 2018-06-23 stsp return err;
3438 ffd1d5e5 2018-06-23 stsp static void
3439 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
3440 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
3441 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
3443 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
3446 5e37ffca 2019-02-25 stsp if (*first_displayed_entry == NULL) {
3447 5e37ffca 2019-02-25 stsp view_flash(view);
3451 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3452 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
3453 34bc9ec9 2019-02-22 stsp view_flash(view);
3454 ffd1d5e5 2018-06-23 stsp if (!isroot)
3455 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3459 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
3460 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
3461 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
3462 ffd1d5e5 2018-06-23 stsp prev = te;
3463 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3465 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
3466 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3468 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3469 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3472 768394f3 2019-01-24 stsp static int
3473 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3474 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
3475 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
3477 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
3478 ffd1d5e5 2018-06-23 stsp int n = 0;
3480 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
3481 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3483 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
3484 768394f3 2019-01-24 stsp last = last_displayed_entry;
3485 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
3486 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
3487 768394f3 2019-01-24 stsp if (last) {
3488 768394f3 2019-01-24 stsp *first_displayed_entry = next;
3489 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
3495 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3496 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
3497 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
3499 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
3500 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
3501 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
3503 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
3504 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
3506 ce52c690 2018-06-23 stsp len += strlen(te->name);
3508 ce52c690 2018-06-23 stsp *path = calloc(1, len);
3509 ffd1d5e5 2018-06-23 stsp if (path == NULL)
3510 230a42bd 2019-05-11 jcs return got_error_prefix_errno("calloc");
3512 ce52c690 2018-06-23 stsp (*path)[0] = '/';
3513 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
3514 d9765a41 2018-06-23 stsp while (pt) {
3515 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3516 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3517 cb2ebc8a 2018-06-23 stsp goto done;
3519 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
3520 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3521 cb2ebc8a 2018-06-23 stsp goto done;
3523 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3526 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3527 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3528 ce52c690 2018-06-23 stsp goto done;
3532 ce52c690 2018-06-23 stsp if (err) {
3533 ce52c690 2018-06-23 stsp free(*path);
3534 ce52c690 2018-06-23 stsp *path = NULL;
3536 ce52c690 2018-06-23 stsp return err;
3539 ce52c690 2018-06-23 stsp static const struct got_error *
3540 0cf4efb1 2018-09-29 stsp blame_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 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3546 ce52c690 2018-06-23 stsp char *path;
3547 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3549 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3551 ce52c690 2018-06-23 stsp return err;
3553 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3554 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3555 230a42bd 2019-05-11 jcs return got_error_prefix_errno("view_open");
3557 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
3558 e5a0f69f 2018-08-18 stsp if (err) {
3559 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3560 e5a0f69f 2018-08-18 stsp free(path);
3562 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3563 69efd4c4 2018-07-18 stsp return err;
3566 69efd4c4 2018-07-18 stsp static const struct got_error *
3567 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3568 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3569 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3570 8b473291 2019-02-21 stsp struct got_repository *repo)
3572 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3573 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3574 69efd4c4 2018-07-18 stsp char *path;
3576 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3577 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3578 230a42bd 2019-05-11 jcs return got_error_prefix_errno("view_open");
3580 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3582 69efd4c4 2018-07-18 stsp return err;
3584 8b473291 2019-02-21 stsp err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3586 e5a0f69f 2018-08-18 stsp view_close(log_view);
3588 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3589 cb2ebc8a 2018-06-23 stsp free(path);
3590 cb2ebc8a 2018-06-23 stsp return err;
3593 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3594 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3595 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3596 8b473291 2019-02-21 stsp struct got_repository *repo)
3598 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3599 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3600 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3602 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3604 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3605 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3606 ffd1d5e5 2018-06-23 stsp goto done;
3608 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3609 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
3610 ffd1d5e5 2018-06-23 stsp goto done;
3613 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3614 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3615 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3616 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3617 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3618 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_object_id_dup");
3619 6484ec90 2018-09-29 stsp goto done;
3621 8b473291 2019-02-21 stsp s->refs = refs;
3622 fb2756b9 2018-08-04 stsp s->repo = repo;
3624 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
3625 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
3626 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
3628 ad80ab7b 2018-08-04 stsp free(commit_id_str);
3629 6484ec90 2018-09-29 stsp if (err) {
3630 fb2756b9 2018-08-04 stsp free(s->tree_label);
3631 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3633 ad80ab7b 2018-08-04 stsp return err;
3636 e5a0f69f 2018-08-18 stsp static const struct got_error *
3637 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
3639 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3641 fb2756b9 2018-08-04 stsp free(s->tree_label);
3642 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3643 6484ec90 2018-09-29 stsp free(s->commit_id);
3644 6484ec90 2018-09-29 stsp s->commit_id = NULL;
3645 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
3646 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
3647 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
3648 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
3649 ad80ab7b 2018-08-04 stsp free(parent);
3652 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
3653 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
3654 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
3656 e5a0f69f 2018-08-18 stsp return NULL;
3659 ad80ab7b 2018-08-04 stsp static const struct got_error *
3660 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
3662 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
3663 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3664 e5a0f69f 2018-08-18 stsp char *parent_path;
3666 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
3668 e5a0f69f 2018-08-18 stsp return err;
3670 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
3671 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
3672 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3673 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
3674 e5a0f69f 2018-08-18 stsp free(parent_path);
3676 669b5ffa 2018-10-07 stsp view_vborder(view);
3677 e5a0f69f 2018-08-18 stsp return err;
3680 e5a0f69f 2018-08-18 stsp static const struct got_error *
3681 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3682 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3684 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3685 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
3686 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
3687 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
3689 e5a0f69f 2018-08-18 stsp switch (ch) {
3691 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
3694 669b5ffa 2018-10-07 stsp if (!s->selected_entry)
3696 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
3697 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
3698 669b5ffa 2018-10-07 stsp err = log_tree_entry(&log_view, begin_x,
3699 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
3700 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
3701 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3702 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3704 669b5ffa 2018-10-07 stsp return err;
3705 669b5ffa 2018-10-07 stsp err = view_set_child(view, log_view);
3706 669b5ffa 2018-10-07 stsp if (err) {
3707 669b5ffa 2018-10-07 stsp view_close(log_view);
3710 669b5ffa 2018-10-07 stsp *focus_view = log_view;
3711 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3713 669b5ffa 2018-10-07 stsp *new_view = log_view;
3716 e5a0f69f 2018-08-18 stsp case KEY_UP:
3717 bf979164 2019-01-24 stsp if (s->selected > 0) {
3718 e5a0f69f 2018-08-18 stsp s->selected--;
3719 bf979164 2019-01-24 stsp if (s->selected == 0)
3722 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3724 34bc9ec9 2019-02-22 stsp tree_scroll_up(view, &s->first_displayed_entry, 1,
3725 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
3727 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
3728 34bc9ec9 2019-02-22 stsp tree_scroll_up(view, &s->first_displayed_entry,
3729 67cc7991 2019-01-24 stsp MAX(0, view->nlines - 4 - s->selected), s->entries,
3730 67cc7991 2019-01-24 stsp s->tree == s->root);
3731 8a0ead39 2018-11-03 stsp s->selected = 0;
3732 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
3733 67cc7991 2019-01-24 stsp s->first_displayed_entry && s->tree != s->root)
3734 67cc7991 2019-01-24 stsp s->first_displayed_entry = NULL;
3737 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
3738 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
3739 e5a0f69f 2018-08-18 stsp s->selected++;
3742 768394f3 2019-01-24 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3743 768394f3 2019-01-24 stsp == NULL) {
3744 768394f3 2019-01-24 stsp /* can't scroll any further */
3745 34bc9ec9 2019-02-22 stsp view_flash(view);
3748 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
3749 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
3751 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
3752 768394f3 2019-01-24 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3753 768394f3 2019-01-24 stsp == NULL) {
3754 768394f3 2019-01-24 stsp /* can't scroll any further; move cursor down */
3755 768394f3 2019-01-24 stsp if (s->selected < s->ndisplayed - 1)
3756 768394f3 2019-01-24 stsp s->selected = s->ndisplayed - 1;
3758 34bc9ec9 2019-02-22 stsp view_flash(view);
3761 768394f3 2019-01-24 stsp nscrolled = tree_scroll_down(&s->first_displayed_entry,
3762 768394f3 2019-01-24 stsp view->nlines, s->last_displayed_entry, s->entries);
3763 768394f3 2019-01-24 stsp if (nscrolled < view->nlines) {
3764 768394f3 2019-01-24 stsp int ndisplayed = 0;
3765 768394f3 2019-01-24 stsp struct got_tree_entry *te;
3766 768394f3 2019-01-24 stsp te = s->first_displayed_entry;
3768 768394f3 2019-01-24 stsp ndisplayed++;
3769 768394f3 2019-01-24 stsp te = SIMPLEQ_NEXT(te, entry);
3770 768394f3 2019-01-24 stsp } while (te);
3771 768394f3 2019-01-24 stsp s->selected = ndisplayed - 1;
3774 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
3775 e5a0f69f 2018-08-18 stsp case '\r':
3776 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
3777 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3778 7837eeac 2018-09-24 stsp case KEY_BACKSPACE:
3779 e5a0f69f 2018-08-18 stsp /* user selected '..' */
3780 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
3782 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
3783 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
3785 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
3786 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
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->first_displayed_entry =
3790 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
3791 e5a0f69f 2018-08-18 stsp s->selected_entry =
3792 e5a0f69f 2018-08-18 stsp parent->selected_entry;
3793 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
3794 e5a0f69f 2018-08-18 stsp free(parent);
3795 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
3796 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3797 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
3798 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
3799 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
3802 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
3803 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
3804 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("calloc");
3807 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
3808 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
3809 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
3810 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
3811 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
3812 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3813 e5a0f69f 2018-08-18 stsp s->tree = child;
3814 e5a0f69f 2018-08-18 stsp s->entries =
3815 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3816 e5a0f69f 2018-08-18 stsp s->selected = 0;
3817 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3818 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
3819 669b5ffa 2018-10-07 stsp struct tog_view *blame_view;
3820 669b5ffa 2018-10-07 stsp int begin_x = view_is_parent_view(view) ?
3821 669b5ffa 2018-10-07 stsp view_split_begin_x(view->begin_x) : 0;
3823 669b5ffa 2018-10-07 stsp err = blame_tree_entry(&blame_view, begin_x,
3824 8b473291 2019-02-21 stsp s->selected_entry, &s->parents,
3825 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
3828 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3829 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3831 669b5ffa 2018-10-07 stsp return err;
3832 669b5ffa 2018-10-07 stsp err = view_set_child(view, blame_view);
3833 669b5ffa 2018-10-07 stsp if (err) {
3834 669b5ffa 2018-10-07 stsp view_close(blame_view);
3837 669b5ffa 2018-10-07 stsp *focus_view = blame_view;
3838 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3840 669b5ffa 2018-10-07 stsp *new_view = blame_view;
3843 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3844 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3845 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3851 ffd1d5e5 2018-06-23 stsp return err;
3854 ffd1d5e5 2018-06-23 stsp __dead static void
3855 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3858 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3859 ffd1d5e5 2018-06-23 stsp getprogname());
3863 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3864 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3866 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3867 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3868 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3869 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3870 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3871 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3872 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3873 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3875 5221c383 2018-08-01 stsp struct tog_view *view;
3877 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3879 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3880 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3881 d188b9a6 2019-01-04 stsp NULL) == -1)
3882 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3885 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3886 ffd1d5e5 2018-06-23 stsp switch (ch) {
3888 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3891 17020d27 2019-03-07 stsp usage_tree();
3892 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3896 ffd1d5e5 2018-06-23 stsp argc -= optind;
3897 ffd1d5e5 2018-06-23 stsp argv += optind;
3899 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3900 52185f70 2019-02-05 stsp struct got_worktree *worktree;
3901 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
3902 52185f70 2019-02-05 stsp if (cwd == NULL)
3903 230a42bd 2019-05-11 jcs return got_error_prefix_errno("getcwd");
3904 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3905 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3906 52185f70 2019-02-05 stsp goto done;
3907 52185f70 2019-02-05 stsp if (worktree) {
3908 52185f70 2019-02-05 stsp free(cwd);
3909 52185f70 2019-02-05 stsp repo_path =
3910 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3911 52185f70 2019-02-05 stsp got_worktree_close(worktree);
3913 52185f70 2019-02-05 stsp repo_path = cwd;
3914 52185f70 2019-02-05 stsp if (repo_path == NULL) {
3915 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
3916 52185f70 2019-02-05 stsp goto done;
3918 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3919 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3920 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3921 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("realpath", argv[0]);
3923 ffd1d5e5 2018-06-23 stsp usage_log();
3925 a915003a 2019-02-05 stsp init_curses();
3927 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
3928 c02c541e 2019-03-29 stsp if (error != NULL)
3929 52185f70 2019-02-05 stsp goto done;
3931 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3932 c02c541e 2019-03-29 stsp if (error)
3933 52185f70 2019-02-05 stsp goto done;
3935 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
3936 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3938 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
3939 15a94983 2018-12-23 stsp commit_id_arg);
3940 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3941 ffd1d5e5 2018-06-23 stsp goto done;
3943 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3944 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3945 ffd1d5e5 2018-06-23 stsp goto done;
3947 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
3948 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
3949 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3950 8b473291 2019-02-21 stsp goto done;
3952 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
3953 8b473291 2019-02-21 stsp if (error)
3954 ffd1d5e5 2018-06-23 stsp goto done;
3956 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3957 5221c383 2018-08-01 stsp if (view == NULL) {
3958 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("view_open");
3959 5221c383 2018-08-01 stsp goto done;
3961 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
3962 ad80ab7b 2018-08-04 stsp if (error)
3963 ad80ab7b 2018-08-04 stsp goto done;
3964 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3966 52185f70 2019-02-05 stsp free(repo_path);
3967 ffd1d5e5 2018-06-23 stsp free(commit_id);
3968 ffd1d5e5 2018-06-23 stsp if (commit)
3969 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3971 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3973 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3974 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3975 ffd1d5e5 2018-06-23 stsp return error;
3978 4ed7e80c 2018-05-20 stsp __dead static void
3979 9f7d7167 2018-04-29 stsp usage(void)
3983 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3984 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3985 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3986 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3987 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3992 c2301be8 2018-04-30 stsp static char **
3993 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3995 c2301be8 2018-04-30 stsp char **argv;
3996 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3998 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3999 c2301be8 2018-04-30 stsp if (argv == NULL)
4000 c2301be8 2018-04-30 stsp err(1, "calloc");
4001 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
4002 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
4003 c2301be8 2018-04-30 stsp err(1, "calloc");
4004 c2301be8 2018-04-30 stsp if (arg1) {
4005 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
4006 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
4007 c2301be8 2018-04-30 stsp err(1, "calloc");
4010 c2301be8 2018-04-30 stsp return argv;
4014 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
4016 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
4017 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
4018 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
4019 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
4021 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
4023 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
4024 9f7d7167 2018-04-29 stsp switch (ch) {
4026 9f7d7167 2018-04-29 stsp hflag = 1;
4030 9f7d7167 2018-04-29 stsp /* NOTREACHED */
4034 9f7d7167 2018-04-29 stsp argc -= optind;
4035 9f7d7167 2018-04-29 stsp argv += optind;
4036 9f7d7167 2018-04-29 stsp optind = 0;
4037 c2301be8 2018-04-30 stsp optreset = 1;
4039 c2301be8 2018-04-30 stsp if (argc == 0) {
4040 f29d3e89 2018-06-23 stsp if (hflag)
4042 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
4043 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
4044 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
4049 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
4050 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
4051 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
4052 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
4053 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
4054 9f7d7167 2018-04-29 stsp if (hflag)
4055 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
4059 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
4060 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
4061 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
4062 c2301be8 2018-04-30 stsp if (repo_path) {
4063 c2301be8 2018-04-30 stsp struct got_repository *repo;
4064 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
4065 c2301be8 2018-04-30 stsp if (error == NULL)
4066 c2301be8 2018-04-30 stsp got_repo_close(repo);
4068 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("realpath",
4070 c2301be8 2018-04-30 stsp if (error) {
4071 f29d3e89 2018-06-23 stsp if (hflag) {
4072 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
4073 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
4077 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
4078 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
4079 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
4080 ad7de8d9 2018-04-30 stsp free(repo_path);
4083 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
4084 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
4086 c2301be8 2018-04-30 stsp free(repo_path);
4090 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4091 9f7d7167 2018-04-29 stsp if (error)
4092 9f7d7167 2018-04-29 stsp goto done;
4095 c2301be8 2018-04-30 stsp free(cmd_argv);
4096 9f7d7167 2018-04-29 stsp if (error)
4097 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);