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))
61 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
63 9f7d7167 2018-04-29 stsp #ifndef nitems
64 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 9f7d7167 2018-04-29 stsp struct tog_cmd {
68 c2301be8 2018-04-30 stsp const char *name;
69 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
70 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
71 c2301be8 2018-04-30 stsp const char *descr;
74 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
75 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
76 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
77 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
78 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
80 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
81 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
82 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
83 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
85 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
86 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
87 9f7d7167 2018-04-29 stsp "show repository history" },
88 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
89 9f7d7167 2018-04-29 stsp "compare files and directories" },
90 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
91 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
92 ffd1d5e5 2018-06-23 stsp { "tree", cmd_tree, usage_tree,
93 ffd1d5e5 2018-06-23 stsp "browse trees in repository" },
96 d6b05b5b 2018-08-04 stsp enum tog_view_type {
97 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
98 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
99 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
100 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
103 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
104 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
105 ba4f502b 2018-08-04 stsp struct got_object_id *id;
106 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
109 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
110 ba4f502b 2018-08-04 stsp struct commit_queue {
111 ba4f502b 2018-08-04 stsp int ncommits;
112 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
115 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
116 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
118 15a087fe 2019-02-21 stsp int first_displayed_line;
119 15a087fe 2019-02-21 stsp int last_displayed_line;
121 15a087fe 2019-02-21 stsp int diff_context;
122 15a087fe 2019-02-21 stsp struct got_repository *repo;
123 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
125 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
126 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
129 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
131 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
132 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
133 1a76625f 2018-10-22 stsp int commits_needed;
134 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
135 1a76625f 2018-10-22 stsp struct commit_queue *commits;
136 1a76625f 2018-10-22 stsp const char *in_repo_path;
137 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
138 1a76625f 2018-10-22 stsp struct got_repository *repo;
139 1a76625f 2018-10-22 stsp int log_complete;
140 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
141 1a76625f 2018-10-22 stsp struct tog_view *view;
142 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
143 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
146 1a76625f 2018-10-22 stsp struct tog_log_view_state {
147 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
148 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
149 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
150 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
151 b01e7d3b 2018-08-04 stsp int selected;
152 b01e7d3b 2018-08-04 stsp char *in_repo_path;
153 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
154 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
155 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
156 1a76625f 2018-10-22 stsp sig_atomic_t quit;
157 1a76625f 2018-10-22 stsp pthread_t thread;
158 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
161 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
162 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
163 e9424729 2018-08-04 stsp int nlines;
165 e9424729 2018-08-04 stsp struct tog_view *view;
166 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
170 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
171 e9424729 2018-08-04 stsp const char *path;
172 e9424729 2018-08-04 stsp struct got_repository *repo;
173 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
174 e9424729 2018-08-04 stsp int *complete;
177 e9424729 2018-08-04 stsp struct tog_blame {
179 e9424729 2018-08-04 stsp size_t filesize;
180 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
181 6fcac457 2018-11-19 stsp int nlines;
182 e9424729 2018-08-04 stsp pthread_t thread;
183 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
184 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
185 e9424729 2018-08-04 stsp const char *path;
188 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
189 7cbe629d 2018-08-04 stsp int first_displayed_line;
190 7cbe629d 2018-08-04 stsp int last_displayed_line;
191 7cbe629d 2018-08-04 stsp int selected_line;
192 7cbe629d 2018-08-04 stsp int blame_complete;
195 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
196 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
197 e5a0f69f 2018-08-18 stsp char *path;
198 7cbe629d 2018-08-04 stsp struct got_repository *repo;
199 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
200 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
201 e9424729 2018-08-04 stsp struct tog_blame blame;
204 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
205 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
206 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
207 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
208 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
209 ad80ab7b 2018-08-04 stsp int selected;
212 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
214 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
215 ad80ab7b 2018-08-04 stsp char *tree_label;
216 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
217 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
218 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
219 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
220 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
221 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
222 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
223 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
224 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
225 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
226 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
230 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
232 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
233 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
234 669b5ffa 2018-10-07 stsp * there is enough screen estate.
236 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
237 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
239 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
240 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
241 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
243 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
244 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
246 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
248 cc3c9aac 2018-08-01 stsp struct tog_view {
249 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
250 26ed57b2 2018-05-19 stsp WINDOW *window;
251 26ed57b2 2018-05-19 stsp PANEL *panel;
252 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
253 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
254 1004088d 2018-09-29 stsp int focussed;
255 669b5ffa 2018-10-07 stsp struct tog_view *parent;
256 669b5ffa 2018-10-07 stsp struct tog_view *child;
257 669b5ffa 2018-10-07 stsp int child_focussed;
259 5dc9f4bc 2018-08-04 stsp /* type-specific state */
260 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
262 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
263 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
264 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
265 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
268 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
269 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
270 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
271 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
274 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
275 fb872ab2 2019-02-21 stsp struct got_object_id *, struct got_object_id *, struct tog_view *,
276 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
277 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
278 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
279 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
280 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
282 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
283 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *,
284 8b473291 2019-02-21 stsp struct got_repository *, const char *, int);
285 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
286 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
287 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
288 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
290 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
291 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *, struct got_repository *);
292 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
293 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
294 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
295 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
297 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
298 8b473291 2019-02-21 stsp struct got_tree_object *, struct got_object_id *,
299 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
300 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
301 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
302 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
303 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
305 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
307 25791caa 2018-10-24 stsp static void
308 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
310 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
313 e5a0f69f 2018-08-18 stsp static const struct got_error *
314 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
316 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
318 669b5ffa 2018-10-07 stsp if (view->child) {
319 669b5ffa 2018-10-07 stsp view_close(view->child);
320 669b5ffa 2018-10-07 stsp view->child = NULL;
322 e5a0f69f 2018-08-18 stsp if (view->close)
323 e5a0f69f 2018-08-18 stsp err = view->close(view);
324 ea5e7bb5 2018-08-01 stsp if (view->panel)
325 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
326 ea5e7bb5 2018-08-01 stsp if (view->window)
327 ea5e7bb5 2018-08-01 stsp delwin(view->window);
328 ea5e7bb5 2018-08-01 stsp free(view);
329 e5a0f69f 2018-08-18 stsp return err;
332 ea5e7bb5 2018-08-01 stsp static struct tog_view *
333 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
334 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
336 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
338 ea5e7bb5 2018-08-01 stsp if (view == NULL)
339 ea5e7bb5 2018-08-01 stsp return NULL;
341 d6b05b5b 2018-08-04 stsp view->type = type;
342 f7d12f7e 2018-08-01 stsp view->lines = LINES;
343 f7d12f7e 2018-08-01 stsp view->cols = COLS;
344 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
345 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
346 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
347 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
348 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
349 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
350 96a765a8 2018-08-04 stsp view_close(view);
351 ea5e7bb5 2018-08-01 stsp return NULL;
353 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
354 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
355 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
356 96a765a8 2018-08-04 stsp view_close(view);
357 ea5e7bb5 2018-08-01 stsp return NULL;
360 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
361 ea5e7bb5 2018-08-01 stsp return view;
365 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
367 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
369 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
372 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
374 5c60c32a 2018-10-18 stsp static const struct got_error *
375 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
377 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
379 5c60c32a 2018-10-18 stsp view->begin_y = 0;
380 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
381 5c60c32a 2018-10-18 stsp view->nlines = LINES;
382 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
383 5c60c32a 2018-10-18 stsp view->lines = LINES;
384 5c60c32a 2018-10-18 stsp view->cols = COLS;
385 5c60c32a 2018-10-18 stsp err = view_resize(view);
387 5c60c32a 2018-10-18 stsp return err;
389 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
390 230a42bd 2019-05-11 jcs return got_error_prefix_errno("mvwin");
392 5c60c32a 2018-10-18 stsp return NULL;
395 5c60c32a 2018-10-18 stsp static const struct got_error *
396 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
398 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
400 5c60c32a 2018-10-18 stsp view->begin_x = 0;
401 5c60c32a 2018-10-18 stsp view->begin_y = 0;
402 5c60c32a 2018-10-18 stsp view->nlines = LINES;
403 5c60c32a 2018-10-18 stsp view->ncols = COLS;
404 5c60c32a 2018-10-18 stsp view->lines = LINES;
405 5c60c32a 2018-10-18 stsp view->cols = COLS;
406 5c60c32a 2018-10-18 stsp err = view_resize(view);
408 5c60c32a 2018-10-18 stsp return err;
410 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
411 230a42bd 2019-05-11 jcs return got_error_prefix_errno("mvwin");
413 5c60c32a 2018-10-18 stsp return NULL;
417 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
419 5c60c32a 2018-10-18 stsp return view->parent == NULL;
422 4d8c2215 2018-08-19 stsp static const struct got_error *
423 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
425 f7d12f7e 2018-08-01 stsp int nlines, ncols;
427 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
428 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
430 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
432 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
433 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
435 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
437 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
438 230a42bd 2019-05-11 jcs return got_error_prefix_errno("wresize");
439 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
440 230a42bd 2019-05-11 jcs return got_error_prefix_errno("replace_panel");
441 25791caa 2018-10-24 stsp wclear(view->window);
443 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
444 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
445 0cf4efb1 2018-09-29 stsp view->lines = LINES;
446 0cf4efb1 2018-09-29 stsp view->cols = COLS;
448 6e3e5d9c 2018-10-18 stsp if (view->child) {
449 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
450 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
451 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
452 5c60c32a 2018-10-18 stsp if (view->child->focussed)
453 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
455 5c60c32a 2018-10-18 stsp show_panel(view->panel);
457 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
458 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
462 5c60c32a 2018-10-18 stsp return NULL;
465 669b5ffa 2018-10-07 stsp static const struct got_error *
466 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
468 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
470 669b5ffa 2018-10-07 stsp if (view->child == NULL)
471 669b5ffa 2018-10-07 stsp return NULL;
473 669b5ffa 2018-10-07 stsp err = view_close(view->child);
474 669b5ffa 2018-10-07 stsp view->child = NULL;
475 669b5ffa 2018-10-07 stsp return err;
478 669b5ffa 2018-10-07 stsp static const struct got_error *
479 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
481 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
483 669b5ffa 2018-10-07 stsp view->child = child;
484 669b5ffa 2018-10-07 stsp child->parent = view;
485 669b5ffa 2018-10-07 stsp return err;
489 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
491 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
495 34bc9ec9 2019-02-22 stsp * Erase all content of the view. Can be used to "flash" the view because
496 34bc9ec9 2019-02-22 stsp * the view loop will redraw it quickly, providing a more subtle visual
497 34bc9ec9 2019-02-22 stsp * effect than curs_flash(3) would provide.
499 25791caa 2018-10-24 stsp static void
500 34bc9ec9 2019-02-22 stsp view_flash(struct tog_view *view)
502 34bc9ec9 2019-02-22 stsp werase(view->window);
503 34bc9ec9 2019-02-22 stsp update_panels();
504 34bc9ec9 2019-02-22 stsp doupdate();
507 34bc9ec9 2019-02-22 stsp static void
508 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
510 25791caa 2018-10-24 stsp int cols, lines;
511 25791caa 2018-10-24 stsp struct winsize size;
513 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
514 25791caa 2018-10-24 stsp cols = 80; /* Default */
515 25791caa 2018-10-24 stsp lines = 24;
517 25791caa 2018-10-24 stsp cols = size.ws_col;
518 25791caa 2018-10-24 stsp lines = size.ws_row;
520 25791caa 2018-10-24 stsp resize_term(lines, cols);
523 0cf4efb1 2018-09-29 stsp static const struct got_error *
524 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
525 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
526 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
528 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
529 669b5ffa 2018-10-07 stsp struct tog_view *v;
530 1a76625f 2018-10-22 stsp int ch, errcode;
532 e5a0f69f 2018-08-18 stsp *new = NULL;
533 e5a0f69f 2018-08-18 stsp *dead = NULL;
534 0cf4efb1 2018-09-29 stsp *focus = NULL;
536 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
537 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
538 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
539 1a76625f 2018-10-22 stsp if (errcode)
540 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
541 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
542 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
543 1a76625f 2018-10-22 stsp if (errcode)
544 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
545 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
547 25791caa 2018-10-24 stsp if (tog_sigwinch_received) {
548 25791caa 2018-10-24 stsp tog_resizeterm();
549 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
550 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
551 25791caa 2018-10-24 stsp err = view_resize(v);
553 25791caa 2018-10-24 stsp return err;
554 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
556 25791caa 2018-10-24 stsp return err;
560 e5a0f69f 2018-08-18 stsp switch (ch) {
564 1e37a5c2 2019-05-12 jcs if (view->child) {
565 1e37a5c2 2019-05-12 jcs *focus = view->child;
566 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
567 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
568 1e37a5c2 2019-05-12 jcs *focus = view->parent;
569 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
573 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
574 1e37a5c2 2019-05-12 jcs *dead = view;
580 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
581 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
583 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
584 669b5ffa 2018-10-07 stsp *focus = view->child;
585 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
586 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
588 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
591 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
592 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
594 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
595 1e37a5c2 2019-05-12 jcs *focus = view;
596 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
597 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
599 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
603 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
604 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
607 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
610 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
614 e5a0f69f 2018-08-18 stsp return err;
618 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
620 0cf4efb1 2018-09-29 stsp PANEL *panel;
621 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
623 669b5ffa 2018-10-07 stsp if (view->parent)
624 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
626 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
627 0cf4efb1 2018-09-29 stsp if (panel == NULL)
630 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
631 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
632 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
636 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
638 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
639 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
641 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
643 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
646 669b5ffa 2018-10-07 stsp return view->focussed;
649 bcbd79e2 2018-08-19 stsp static const struct got_error *
650 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
652 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
653 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
654 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
655 fd823528 2018-10-22 stsp int fast_refresh = 10;
656 1a76625f 2018-10-22 stsp int done = 0, errcode;
658 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
659 1a76625f 2018-10-22 stsp if (errcode)
660 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
662 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
663 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
665 a81bf10d 2018-09-29 stsp main_view = view;
666 1004088d 2018-09-29 stsp view->focussed = 1;
667 878940b7 2018-09-29 stsp err = view->show(view);
669 0cf4efb1 2018-09-29 stsp return err;
670 0cf4efb1 2018-09-29 stsp update_panels();
671 0cf4efb1 2018-09-29 stsp doupdate();
672 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
673 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
674 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
675 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
677 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
678 e4197bf9 2018-08-18 stsp view, &views);
681 e5a0f69f 2018-08-18 stsp if (dead_view) {
682 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
684 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
685 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
686 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
687 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
688 669b5ffa 2018-10-07 stsp prev = view->parent;
690 669b5ffa 2018-10-07 stsp if (dead_view->parent)
691 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
693 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
695 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
696 a81bf10d 2018-09-29 stsp if (err || dead_view == main_view)
699 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
700 0cf4efb1 2018-09-29 stsp if (focus_view)
701 0cf4efb1 2018-09-29 stsp view = focus_view;
702 669b5ffa 2018-10-07 stsp else if (prev)
703 669b5ffa 2018-10-07 stsp view = prev;
704 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
705 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
706 0cf4efb1 2018-09-29 stsp tog_view_list_head);
708 0cf4efb1 2018-09-29 stsp view = NULL;
709 669b5ffa 2018-10-07 stsp if (view) {
710 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
711 669b5ffa 2018-10-07 stsp focus_view = view->child;
713 669b5ffa 2018-10-07 stsp focus_view = view;
717 bcbd79e2 2018-08-19 stsp if (new_view) {
718 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
719 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
720 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
721 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
723 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
724 86c66b02 2018-10-18 stsp err = view_close(v);
729 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
730 fed7eaa8 2018-10-24 stsp view = new_view;
731 878940b7 2018-09-29 stsp if (focus_view == NULL)
732 878940b7 2018-09-29 stsp focus_view = new_view;
734 0cf4efb1 2018-09-29 stsp if (focus_view) {
735 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
737 0cf4efb1 2018-09-29 stsp view->focussed = 0;
738 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
739 0cf4efb1 2018-09-29 stsp view = focus_view;
740 878940b7 2018-09-29 stsp if (new_view)
741 878940b7 2018-09-29 stsp show_panel(new_view->panel);
742 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
743 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
745 669b5ffa 2018-10-07 stsp if (view) {
746 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
747 758194b5 2018-10-24 stsp view->focussed = 1;
748 758194b5 2018-10-24 stsp show_panel(view->panel);
749 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
750 758194b5 2018-10-24 stsp show_panel(view->child->panel);
751 1a76625f 2018-10-22 stsp focus_view = view;
753 669b5ffa 2018-10-07 stsp if (view->parent) {
754 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
758 669b5ffa 2018-10-07 stsp err = view->show(view);
761 669b5ffa 2018-10-07 stsp if (view->child) {
762 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
766 1a76625f 2018-10-22 stsp update_panels();
767 1a76625f 2018-10-22 stsp doupdate();
771 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
772 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
773 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
774 e5a0f69f 2018-08-18 stsp view_close(view);
777 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
778 1a76625f 2018-10-22 stsp if (errcode)
779 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
781 e5a0f69f 2018-08-18 stsp return err;
784 4ed7e80c 2018-05-20 stsp __dead static void
785 9f7d7167 2018-04-29 stsp usage_log(void)
788 c70c5802 2018-08-01 stsp fprintf(stderr,
789 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
790 9f7d7167 2018-04-29 stsp getprogname());
794 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
795 80ddbec8 2018-04-29 stsp static const struct got_error *
796 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
798 00dfcb92 2018-06-11 stsp char *vis = NULL;
799 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
801 963b370f 2018-05-20 stsp *ws = NULL;
802 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
803 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
804 00dfcb92 2018-06-11 stsp int vislen;
805 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
806 230a42bd 2019-05-11 jcs return got_error_prefix_errno("mbstowcs");
808 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
809 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
811 00dfcb92 2018-06-11 stsp return err;
812 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
813 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
814 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("mbstowcs"); /* give up */
819 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
820 a7f50699 2018-06-11 stsp if (*ws == NULL) {
821 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("calloc");
825 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
826 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("mbstowcs");
831 963b370f 2018-05-20 stsp *ws = NULL;
834 963b370f 2018-05-20 stsp return err;
837 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
838 963b370f 2018-05-20 stsp static const struct got_error *
839 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
841 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
842 963b370f 2018-05-20 stsp int cols = 0;
843 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
844 963b370f 2018-05-20 stsp size_t wlen;
847 963b370f 2018-05-20 stsp *wlinep = NULL;
848 b700b5d6 2018-07-10 stsp *widthp = 0;
850 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
852 963b370f 2018-05-20 stsp return err;
855 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
856 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
857 963b370f 2018-05-20 stsp switch (width) {
863 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
864 b700b5d6 2018-07-10 stsp cols += width;
868 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
869 67ceb59d 2019-03-03 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
873 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("wcwidth");
877 963b370f 2018-05-20 stsp wline[i] = L'\0';
878 b700b5d6 2018-07-10 stsp if (widthp)
879 b700b5d6 2018-07-10 stsp *widthp = cols;
882 963b370f 2018-05-20 stsp free(wline);
884 963b370f 2018-05-20 stsp *wlinep = wline;
885 963b370f 2018-05-20 stsp return err;
888 8b473291 2019-02-21 stsp static const struct got_error*
889 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
890 8b473291 2019-02-21 stsp struct got_object_id *id)
892 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
893 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
895 8b473291 2019-02-21 stsp const char *name;
897 8b473291 2019-02-21 stsp *refs_str = NULL;
899 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
900 8b473291 2019-02-21 stsp if (got_object_id_cmp(re->id, id) != 0)
902 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
903 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
905 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
907 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
909 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
911 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
913 8b473291 2019-02-21 stsp s = *refs_str;
914 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
915 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
916 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
918 8b473291 2019-02-21 stsp *refs_str = NULL;
924 8b473291 2019-02-21 stsp return err;
927 963b370f 2018-05-20 stsp static const struct got_error *
928 5813d178 2019-03-09 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
930 5813d178 2019-03-09 stsp char *smallerthan, *at;
932 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
933 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
934 5813d178 2019-03-09 stsp author = smallerthan + 1;
935 5813d178 2019-03-09 stsp at = strchr(author, '@');
937 5813d178 2019-03-09 stsp *at = '\0';
938 5813d178 2019-03-09 stsp return format_line(wauthor, author_width, author, limit);
941 5813d178 2019-03-09 stsp static const struct got_error *
942 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
943 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
944 5813d178 2019-03-09 stsp int author_display_cols)
946 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
947 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
948 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
949 5813d178 2019-03-09 stsp char *author = NULL;
950 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
951 bb737323 2018-05-20 stsp int author_width, logmsg_width;
952 5813d178 2019-03-09 stsp char *newline, *line = NULL;
953 bb737323 2018-05-20 stsp int col, limit;
954 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
955 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
956 ccb26ccd 2018-11-05 stsp struct tm tm;
957 45d799e2 2018-12-23 stsp time_t committer_time;
959 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
960 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
961 230a42bd 2019-05-11 jcs return got_error_prefix_errno("localtime_r");
962 ccb26ccd 2018-11-05 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
963 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
964 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
966 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
967 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
969 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
970 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
971 b39d25c7 2018-07-10 stsp col = limit + 1;
972 b39d25c7 2018-07-10 stsp if (col > avail)
975 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
976 5813d178 2019-03-09 stsp if (author == NULL) {
977 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
980 5813d178 2019-03-09 stsp err = format_author(&wauthor, &author_width, author, avail - col);
983 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
984 bb737323 2018-05-20 stsp col += author_width;
985 5813d178 2019-03-09 stsp while (col <= avail && author_width < author_display_cols + 2) {
986 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
988 bb737323 2018-05-20 stsp author_width++;
990 9c2eaf34 2018-05-20 stsp if (col > avail)
993 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
994 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
995 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
998 bb737323 2018-05-20 stsp logmsg = logmsg0;
999 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1001 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1002 bb737323 2018-05-20 stsp if (newline)
1003 bb737323 2018-05-20 stsp *newline = '\0';
1004 bb737323 2018-05-20 stsp limit = avail - col;
1005 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1007 bb737323 2018-05-20 stsp goto done;
1008 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1009 bb737323 2018-05-20 stsp col += logmsg_width;
1010 bb737323 2018-05-20 stsp while (col <= avail) {
1011 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1015 80ddbec8 2018-04-29 stsp free(logmsg0);
1016 bb737323 2018-05-20 stsp free(wlogmsg);
1017 5813d178 2019-03-09 stsp free(author);
1018 bb737323 2018-05-20 stsp free(wauthor);
1019 80ddbec8 2018-04-29 stsp free(line);
1020 80ddbec8 2018-04-29 stsp return err;
1023 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1024 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1025 899d86c2 2018-05-10 stsp struct got_object_id *id)
1027 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1029 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1030 80ddbec8 2018-04-29 stsp if (entry == NULL)
1031 899d86c2 2018-05-10 stsp return NULL;
1033 899d86c2 2018-05-10 stsp entry->id = id;
1034 99db9666 2018-05-07 stsp entry->commit = commit;
1035 899d86c2 2018-05-10 stsp return entry;
1038 99db9666 2018-05-07 stsp static void
1039 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1041 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1043 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1044 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1045 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1046 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1047 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1048 99db9666 2018-05-07 stsp free(entry);
1051 99db9666 2018-05-07 stsp static void
1052 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1054 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1055 99db9666 2018-05-07 stsp pop_commit(commits);
1058 c4972b91 2018-05-07 stsp static const struct got_error *
1059 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1060 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
1062 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1063 93e45b7c 2018-09-24 stsp int nqueued = 0;
1066 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1067 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1068 1a76625f 2018-10-22 stsp * while updating the display.
1070 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
1071 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1072 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1073 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1074 1a76625f 2018-10-22 stsp int errcode;
1076 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1077 9ba79e04 2018-06-11 stsp if (err) {
1078 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1080 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
1081 93e45b7c 2018-09-24 stsp minqueue, repo);
1083 ecb28ae0 2018-07-16 stsp return err;
1087 ecb28ae0 2018-07-16 stsp if (id == NULL)
1090 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1093 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1094 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1095 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("alloc_commit_queue_entry");
1099 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1100 1a76625f 2018-10-22 stsp if (errcode) {
1101 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_lock");
1105 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1106 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1107 ecb28ae0 2018-07-16 stsp nqueued++;
1108 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1110 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1111 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1112 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1113 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1116 9ba79e04 2018-06-11 stsp return err;
1119 99db9666 2018-05-07 stsp static const struct got_error *
1120 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1122 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1123 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1125 9ba79e04 2018-06-11 stsp *head_id = NULL;
1127 2f17228e 2019-05-12 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1129 99db9666 2018-05-07 stsp return err;
1131 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1132 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1133 9ba79e04 2018-06-11 stsp if (err) {
1134 9ba79e04 2018-06-11 stsp *head_id = NULL;
1135 99db9666 2018-05-07 stsp return err;
1138 9ba79e04 2018-06-11 stsp return NULL;
1141 0553a4e3 2018-04-30 stsp static const struct got_error *
1142 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1143 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1144 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1145 8b473291 2019-02-21 stsp struct got_reflist_head *refs, const char *path, int commits_needed)
1147 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1148 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1149 ecb28ae0 2018-07-16 stsp int ncommits, width;
1150 5813d178 2019-03-09 stsp int author_cols = 10;
1151 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1152 8b473291 2019-02-21 stsp char *refs_str = NULL;
1153 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1155 e0d42f60 2018-07-22 stsp entry = first;
1156 e0d42f60 2018-07-22 stsp ncommits = 0;
1157 e0d42f60 2018-07-22 stsp while (entry) {
1158 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1159 e0d42f60 2018-07-22 stsp *selected = entry;
1162 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1163 e0d42f60 2018-07-22 stsp ncommits++;
1166 1a76625f 2018-10-22 stsp if (*selected) {
1167 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1169 ecb28ae0 2018-07-16 stsp return err;
1170 8b473291 2019-02-21 stsp if (refs) {
1171 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, (*selected)->id);
1173 8b473291 2019-02-21 stsp goto done;
1177 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1178 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1180 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1181 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1182 8b473291 2019-02-21 stsp commits_needed > 0 ? "loading... " :
1183 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1184 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1185 8b473291 2019-02-21 stsp goto done;
1188 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1189 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1190 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1191 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1192 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1193 1a76625f 2018-10-22 stsp header = NULL;
1194 1a76625f 2018-10-22 stsp goto done;
1196 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1197 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1198 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1199 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
1200 1a76625f 2018-10-22 stsp header = NULL;
1201 1a76625f 2018-10-22 stsp goto done;
1203 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1205 1a76625f 2018-10-22 stsp goto done;
1207 2814baeb 2018-08-01 stsp werase(view->window);
1209 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1210 a3404814 2018-09-02 stsp wstandout(view->window);
1211 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1212 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1213 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1216 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1217 a3404814 2018-09-02 stsp wstandend(view->window);
1218 ecb28ae0 2018-07-16 stsp free(wline);
1219 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1220 1a76625f 2018-10-22 stsp goto done;
1222 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1223 899d86c2 2018-05-10 stsp entry = first;
1224 5813d178 2019-03-09 stsp ncommits = 0;
1225 5813d178 2019-03-09 stsp while (entry) {
1226 5813d178 2019-03-09 stsp char *author;
1227 5813d178 2019-03-09 stsp wchar_t *wauthor;
1228 5813d178 2019-03-09 stsp int width;
1229 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1231 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1232 5813d178 2019-03-09 stsp if (author == NULL) {
1233 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("strdup");
1234 5813d178 2019-03-09 stsp goto done;
1236 5813d178 2019-03-09 stsp err = format_author(&wauthor, &width, author, COLS);
1237 5813d178 2019-03-09 stsp if (author_cols < width)
1238 5813d178 2019-03-09 stsp author_cols = width;
1239 5813d178 2019-03-09 stsp free(wauthor);
1240 5813d178 2019-03-09 stsp free(author);
1241 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1244 5813d178 2019-03-09 stsp entry = first;
1245 899d86c2 2018-05-10 stsp *last = first;
1246 867c6645 2018-07-10 stsp ncommits = 0;
1247 899d86c2 2018-05-10 stsp while (entry) {
1248 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1250 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1251 2814baeb 2018-08-01 stsp wstandout(view->window);
1252 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1253 5813d178 2019-03-09 stsp author_cols);
1254 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1255 2814baeb 2018-08-01 stsp wstandend(view->window);
1258 0553a4e3 2018-04-30 stsp ncommits++;
1259 899d86c2 2018-05-10 stsp *last = entry;
1260 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1263 1a57306a 2018-09-02 stsp view_vborder(view);
1265 1a76625f 2018-10-22 stsp free(id_str);
1266 8b473291 2019-02-21 stsp free(refs_str);
1267 1a76625f 2018-10-22 stsp free(ncommits_str);
1268 1a76625f 2018-10-22 stsp free(header);
1269 80ddbec8 2018-04-29 stsp return err;
1272 07b55e75 2018-05-10 stsp static void
1273 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1274 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1275 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1277 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1278 07b55e75 2018-05-10 stsp int nscrolled = 0;
1280 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1281 34bc9ec9 2019-02-22 stsp if (*first_displayed_entry == entry) {
1282 34bc9ec9 2019-02-22 stsp view_flash(view);
1286 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1287 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1288 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1289 07b55e75 2018-05-10 stsp if (entry) {
1290 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1291 07b55e75 2018-05-10 stsp nscrolled++;
1296 aa075928 2018-05-10 stsp static const struct got_error *
1297 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1298 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1300 5e224a3e 2019-02-22 stsp int errcode;
1301 8a42fca8 2019-02-22 stsp int max_wait = 20;
1303 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1305 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1306 5e224a3e 2019-02-22 stsp if (*log_complete)
1309 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1310 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1311 7aafa0d1 2019-02-22 stsp if (errcode)
1312 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1313 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1314 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1315 7aafa0d1 2019-02-22 stsp if (errcode)
1316 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1317 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1318 7aafa0d1 2019-02-22 stsp pthread_yield();
1319 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1320 7aafa0d1 2019-02-22 stsp if (errcode)
1321 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1322 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1324 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1326 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1327 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1328 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1329 5e224a3e 2019-02-22 stsp * with few commits in history.
1331 7aafa0d1 2019-02-22 stsp return NULL;
1335 5e224a3e 2019-02-22 stsp return NULL;
1338 5e224a3e 2019-02-22 stsp static const struct got_error *
1339 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1340 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1341 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1342 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1343 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1345 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1346 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1347 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1349 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1350 5e224a3e 2019-02-22 stsp return NULL;
1352 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1353 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1355 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1356 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1358 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1359 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1360 5e224a3e 2019-02-22 stsp need_commits);
1362 5e224a3e 2019-02-22 stsp return err;
1366 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1367 34bc9ec9 2019-02-22 stsp if (pentry == NULL) {
1368 34bc9ec9 2019-02-22 stsp if (*log_complete)
1369 34bc9ec9 2019-02-22 stsp view_flash(view);
1373 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1375 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1376 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1378 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1379 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1381 dd0a52c1 2018-05-20 stsp return err;
1384 cd0acaa7 2018-05-20 stsp static const struct got_error *
1385 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1386 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1387 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1388 8b473291 2019-02-21 stsp struct got_repository *repo)
1390 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1391 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1392 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1394 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1395 15a94983 2018-12-23 stsp if (diff_view == NULL)
1396 230a42bd 2019-05-11 jcs return got_error_prefix_errno("view_open");
1398 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1399 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1400 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1401 e5a0f69f 2018-08-18 stsp if (err == NULL)
1402 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1403 cd0acaa7 2018-05-20 stsp return err;
1406 80ddbec8 2018-04-29 stsp static const struct got_error *
1407 0cf4efb1 2018-09-29 stsp browse_commit(struct tog_view **new_view, int begin_x,
1408 8b473291 2019-02-21 stsp struct commit_queue_entry *entry, struct got_reflist_head *refs,
1409 8b473291 2019-02-21 stsp struct got_repository *repo)
1411 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1412 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1413 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1415 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1416 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1418 9343a5fb 2018-06-23 stsp return err;
1420 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1421 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1422 230a42bd 2019-05-11 jcs return got_error_prefix_errno("view_open");
1424 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1426 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1428 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1429 1a76625f 2018-10-22 stsp return err;
1432 1a76625f 2018-10-22 stsp static void *
1433 1a76625f 2018-10-22 stsp log_thread(void *arg)
1435 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1436 1a76625f 2018-10-22 stsp int errcode = 0;
1437 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1438 1a76625f 2018-10-22 stsp int done = 0;
1440 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1442 1a76625f 2018-10-22 stsp return (void *)err;
1444 1a76625f 2018-10-22 stsp while (!done && !err) {
1445 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1446 1a76625f 2018-10-22 stsp a->in_repo_path);
1447 1a76625f 2018-10-22 stsp if (err) {
1448 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1449 1a76625f 2018-10-22 stsp return (void *)err;
1450 1a76625f 2018-10-22 stsp err = NULL;
1452 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1453 1a76625f 2018-10-22 stsp a->commits_needed--;
1455 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1456 3abe8080 2019-04-10 stsp if (errcode) {
1457 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1458 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1460 3abe8080 2019-04-10 stsp } else if (*a->quit)
1462 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1463 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1464 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1465 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1469 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1470 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1471 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1472 1a76625f 2018-10-22 stsp &tog_mutex);
1473 1a76625f 2018-10-22 stsp if (errcode)
1474 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1475 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1478 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1479 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1480 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1481 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1483 3abe8080 2019-04-10 stsp a->log_complete = 1;
1484 1a76625f 2018-10-22 stsp return (void *)err;
1487 1a76625f 2018-10-22 stsp static const struct got_error *
1488 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1490 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1491 1a76625f 2018-10-22 stsp int errcode;
1493 1a76625f 2018-10-22 stsp if (s->thread) {
1494 1a76625f 2018-10-22 stsp s->quit = 1;
1495 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1496 1a76625f 2018-10-22 stsp if (errcode)
1497 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1498 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1499 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1500 1a76625f 2018-10-22 stsp if (errcode)
1501 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1502 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1503 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1504 1a76625f 2018-10-22 stsp if (errcode)
1505 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1506 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1507 1a76625f 2018-10-22 stsp if (errcode)
1508 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1509 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1510 1a76625f 2018-10-22 stsp s->thread = NULL;
1513 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1514 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1515 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1517 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1518 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1519 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1522 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1523 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1524 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1527 9343a5fb 2018-06-23 stsp return err;
1530 9343a5fb 2018-06-23 stsp static const struct got_error *
1531 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1533 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1534 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1536 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1537 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1538 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1539 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1540 1a76625f 2018-10-22 stsp free(s->start_id);
1541 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1542 1a76625f 2018-10-22 stsp return err;
1545 1a76625f 2018-10-22 stsp static const struct got_error *
1546 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1547 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
1548 8b473291 2019-02-21 stsp const char *path, int check_disk)
1550 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1551 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1552 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1553 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1554 1a76625f 2018-10-22 stsp int errcode;
1556 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1557 ecb28ae0 2018-07-16 stsp if (err != NULL)
1558 ecb28ae0 2018-07-16 stsp goto done;
1560 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1561 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1562 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1564 8b473291 2019-02-21 stsp s->refs = refs;
1565 fb2756b9 2018-08-04 stsp s->repo = repo;
1566 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1567 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1568 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_object_id_dup");
1569 5036bf37 2018-09-24 stsp goto done;
1572 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1573 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1574 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1576 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1578 1a76625f 2018-10-22 stsp goto done;
1579 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1580 1a76625f 2018-10-22 stsp 0, thread_repo);
1582 1a76625f 2018-10-22 stsp goto done;
1584 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1585 1a76625f 2018-10-22 stsp if (errcode) {
1586 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
1587 1a76625f 2018-10-22 stsp goto done;
1590 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1591 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1592 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1593 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1594 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1595 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1596 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1597 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1598 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1599 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1600 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1603 1a76625f 2018-10-22 stsp close_log_view(view);
1604 ba4f502b 2018-08-04 stsp return err;
1607 e5a0f69f 2018-08-18 stsp static const struct got_error *
1608 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1610 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1612 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1613 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1614 2b380cc8 2018-10-24 stsp &s->thread_args);
1615 2b380cc8 2018-10-24 stsp if (errcode)
1616 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
1619 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1620 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1621 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
1622 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1625 e5a0f69f 2018-08-18 stsp static const struct got_error *
1626 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1627 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1629 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1630 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1631 5036bf37 2018-09-24 stsp char *parent_path;
1632 669b5ffa 2018-10-07 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
1633 669b5ffa 2018-10-07 stsp int begin_x = 0;
1635 e5a0f69f 2018-08-18 stsp switch (ch) {
1637 1e37a5c2 2019-05-12 jcs s->quit = 1;
1640 1e37a5c2 2019-05-12 jcs case KEY_UP:
1643 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1645 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
1646 1e37a5c2 2019-05-12 jcs s->selected--;
1647 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
1649 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry, 1,
1650 1e37a5c2 2019-05-12 jcs &s->commits);
1652 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
1653 a4292ac5 2019-05-12 jcs case CTRL('b'):
1654 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1656 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
1657 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
1658 1e37a5c2 2019-05-12 jcs if (s->selected == 0) {
1659 1e37a5c2 2019-05-12 jcs view_flash(view);
1662 1e37a5c2 2019-05-12 jcs s->selected = 0;
1665 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
1666 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
1669 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
1672 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1674 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
1675 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1676 1e37a5c2 2019-05-12 jcs s->selected++;
1679 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
1680 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
1681 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
1682 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1683 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1685 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
1686 a4292ac5 2019-05-12 jcs case CTRL('f'): {
1687 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
1688 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
1689 1e37a5c2 2019-05-12 jcs if (first == NULL)
1691 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
1692 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
1693 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
1694 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1695 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1696 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
1697 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
1698 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1699 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
1700 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
1701 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
1703 1e37a5c2 2019-05-12 jcs err = NULL;
1706 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1707 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
1708 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
1709 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
1710 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
1712 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
1715 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
1717 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
1718 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
1719 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
1720 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
1721 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
1724 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1725 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
1727 1e37a5c2 2019-05-12 jcs return err;
1728 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
1730 1e37a5c2 2019-05-12 jcs view_close(diff_view);
1733 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
1734 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
1736 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
1739 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
1741 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
1742 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
1743 1e37a5c2 2019-05-12 jcs err = browse_commit(&tree_view, begin_x,
1744 1e37a5c2 2019-05-12 jcs s->selected_entry, s->refs, s->repo);
1747 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1748 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
1750 1e37a5c2 2019-05-12 jcs return err;
1751 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
1753 1e37a5c2 2019-05-12 jcs view_close(tree_view);
1756 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
1757 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
1759 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
1761 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
1762 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
1764 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
1765 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
1766 1e37a5c2 2019-05-12 jcs struct tog_view *lv;
1767 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
1769 1e37a5c2 2019-05-12 jcs return err;
1770 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
1771 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
1772 1e37a5c2 2019-05-12 jcs if (lv == NULL)
1773 1e37a5c2 2019-05-12 jcs return got_error_prefix_errno(
1774 1e37a5c2 2019-05-12 jcs "view_open");
1775 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
1776 1e37a5c2 2019-05-12 jcs s->repo, parent_path, 0);
1778 1e37a5c2 2019-05-12 jcs return err;;
1779 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
1780 1e37a5c2 2019-05-12 jcs *new_view = lv;
1782 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
1783 1e37a5c2 2019-05-12 jcs *focus_view = lv;
1785 1e37a5c2 2019-05-12 jcs return NULL;
1792 80ddbec8 2018-04-29 stsp return err;
1795 4ed7e80c 2018-05-20 stsp static const struct got_error *
1796 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
1798 c2db6724 2019-01-04 stsp const struct got_error *error;
1800 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
1801 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("unveil", repo_path);
1803 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
1804 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("unveil", worktree_path);
1806 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
1807 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("unveil", "/tmp");
1809 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
1810 c2db6724 2019-01-04 stsp if (error != NULL)
1811 c2db6724 2019-01-04 stsp return error;
1813 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
1814 230a42bd 2019-05-11 jcs return got_error_prefix_errno("unveil");
1816 c2db6724 2019-01-04 stsp return NULL;
1819 a915003a 2019-02-05 stsp static void
1820 a915003a 2019-02-05 stsp init_curses(void)
1822 a915003a 2019-02-05 stsp initscr();
1824 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
1827 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
1828 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
1829 a915003a 2019-02-05 stsp curs_set(0);
1830 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
1833 c2db6724 2019-01-04 stsp static const struct got_error *
1834 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1836 80ddbec8 2018-04-29 stsp const struct got_error *error;
1837 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1838 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
1839 8b473291 2019-02-21 stsp struct got_reflist_head refs;
1840 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1841 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1842 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1844 04cc582a 2018-08-01 stsp struct tog_view *view;
1846 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
1848 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1849 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1850 c2db6724 2019-01-04 stsp NULL) == -1)
1851 80ddbec8 2018-04-29 stsp err(1, "pledge");
1854 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1855 80ddbec8 2018-04-29 stsp switch (ch) {
1857 80ddbec8 2018-04-29 stsp start_commit = optarg;
1860 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1861 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1862 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1865 17020d27 2019-03-07 stsp usage_log();
1866 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1870 80ddbec8 2018-04-29 stsp argc -= optind;
1871 80ddbec8 2018-04-29 stsp argv += optind;
1873 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1874 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1875 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
1876 ecb28ae0 2018-07-16 stsp goto done;
1878 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
1879 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1880 963f97a1 2019-03-18 stsp goto done;
1881 963f97a1 2019-03-18 stsp error = NULL;
1883 963f97a1 2019-03-18 stsp if (argc == 0) {
1884 963f97a1 2019-03-18 stsp path = strdup("");
1885 963f97a1 2019-03-18 stsp if (path == NULL) {
1886 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1887 ecb28ae0 2018-07-16 stsp goto done;
1889 963f97a1 2019-03-18 stsp } else if (argc == 1) {
1890 963f97a1 2019-03-18 stsp if (worktree) {
1891 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1893 963f97a1 2019-03-18 stsp if (error)
1894 963f97a1 2019-03-18 stsp goto done;
1896 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
1897 963f97a1 2019-03-18 stsp if (path == NULL) {
1898 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1899 963f97a1 2019-03-18 stsp goto done;
1903 963f97a1 2019-03-18 stsp usage_log();
1905 963f97a1 2019-03-18 stsp repo_path = worktree ?
1906 963f97a1 2019-03-18 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1907 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
1908 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
1909 963f97a1 2019-03-18 stsp goto done;
1912 a915003a 2019-02-05 stsp init_curses();
1914 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1915 80ddbec8 2018-04-29 stsp if (error != NULL)
1916 ecb28ae0 2018-07-16 stsp goto done;
1918 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
1919 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1920 c02c541e 2019-03-29 stsp if (error)
1921 c02c541e 2019-03-29 stsp goto done;
1923 15a94983 2018-12-23 stsp if (start_commit == NULL)
1924 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1926 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&start_id, repo,
1927 15a94983 2018-12-23 stsp start_commit);
1928 80ddbec8 2018-04-29 stsp if (error != NULL)
1929 8b473291 2019-02-21 stsp goto done;
1931 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
1932 8b473291 2019-02-21 stsp if (error)
1933 ecb28ae0 2018-07-16 stsp goto done;
1935 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1936 04cc582a 2018-08-01 stsp if (view == NULL) {
1937 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("view_open");
1938 04cc582a 2018-08-01 stsp goto done;
1940 8b473291 2019-02-21 stsp error = open_log_view(view, start_id, &refs, repo, path, 1);
1941 ba4f502b 2018-08-04 stsp if (error)
1942 ba4f502b 2018-08-04 stsp goto done;
1943 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1945 ecb28ae0 2018-07-16 stsp free(repo_path);
1946 ecb28ae0 2018-07-16 stsp free(cwd);
1947 ecb28ae0 2018-07-16 stsp free(path);
1948 899d86c2 2018-05-10 stsp free(start_id);
1950 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1951 ec142235 2019-03-07 stsp if (worktree)
1952 ec142235 2019-03-07 stsp got_worktree_close(worktree);
1953 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1954 80ddbec8 2018-04-29 stsp return error;
1957 4ed7e80c 2018-05-20 stsp __dead static void
1958 9f7d7167 2018-04-29 stsp usage_diff(void)
1961 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1962 9f7d7167 2018-04-29 stsp getprogname());
1966 b304db33 2018-05-20 stsp static char *
1967 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1969 b304db33 2018-05-20 stsp char *line;
1970 b304db33 2018-05-20 stsp size_t linelen;
1971 b304db33 2018-05-20 stsp size_t lineno;
1972 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1974 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1976 b304db33 2018-05-20 stsp *len = linelen;
1977 b304db33 2018-05-20 stsp return line;
1980 4ed7e80c 2018-05-20 stsp static const struct got_error *
1981 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1982 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1983 a3404814 2018-09-02 stsp char * header)
1985 61e69b96 2018-05-20 stsp const struct got_error *err;
1986 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1987 b304db33 2018-05-20 stsp char *line;
1988 b304db33 2018-05-20 stsp size_t len;
1989 61e69b96 2018-05-20 stsp wchar_t *wline;
1990 e0b650dd 2018-05-20 stsp int width;
1992 26ed57b2 2018-05-19 stsp rewind(f);
1993 f7d12f7e 2018-08-01 stsp werase(view->window);
1995 a3404814 2018-09-02 stsp if (header) {
1996 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1997 a3404814 2018-09-02 stsp if (err) {
1998 a3404814 2018-09-02 stsp return err;
2001 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2002 a3404814 2018-09-02 stsp wstandout(view->window);
2003 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2004 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2005 a3404814 2018-09-02 stsp wstandend(view->window);
2006 a3404814 2018-09-02 stsp if (width < view->ncols)
2007 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2009 a3404814 2018-09-02 stsp if (max_lines <= 1)
2010 a3404814 2018-09-02 stsp return NULL;
2011 a3404814 2018-09-02 stsp max_lines--;
2015 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2016 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2017 26ed57b2 2018-05-19 stsp if (line == NULL) {
2021 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2022 26ed57b2 2018-05-19 stsp free(line);
2026 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2027 61e69b96 2018-05-20 stsp if (err) {
2028 61e69b96 2018-05-20 stsp free(line);
2029 61e69b96 2018-05-20 stsp return err;
2031 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2032 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2033 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2034 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2035 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2036 26ed57b2 2018-05-19 stsp free(line);
2037 2550e4c3 2018-07-13 stsp free(wline);
2038 2550e4c3 2018-07-13 stsp wline = NULL;
2040 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2042 1a57306a 2018-09-02 stsp view_vborder(view);
2044 26ed57b2 2018-05-19 stsp return NULL;
2047 abd2672a 2018-12-23 stsp static char *
2048 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2050 abd2672a 2018-12-23 stsp char *p, *s = ctime_r(time, datebuf);
2051 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2053 abd2672a 2018-12-23 stsp *p = '\0';
2057 4ed7e80c 2018-05-20 stsp static const struct got_error *
2058 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2059 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2061 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2062 abd2672a 2018-12-23 stsp char datebuf[26];
2063 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2064 15a94983 2018-12-23 stsp char *id_str = NULL;
2065 45d799e2 2018-12-23 stsp time_t committer_time;
2066 45d799e2 2018-12-23 stsp const char *author, *committer;
2067 8b473291 2019-02-21 stsp char *refs_str = NULL;
2069 8b473291 2019-02-21 stsp if (refs) {
2070 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, commit_id);
2072 8b473291 2019-02-21 stsp return err;
2075 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2077 abd2672a 2018-12-23 stsp return err;
2079 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2080 15a94983 2018-12-23 stsp if (err) {
2081 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_object_id_str");
2082 15a94983 2018-12-23 stsp goto done;
2085 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2086 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2087 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fprintf");
2088 abd2672a 2018-12-23 stsp goto done;
2090 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2091 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2092 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fprintf");
2093 abd2672a 2018-12-23 stsp goto done;
2095 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2096 abd2672a 2018-12-23 stsp if (fprintf(outfile, "date: %s UTC\n",
2097 45d799e2 2018-12-23 stsp get_datestr(&committer_time, datebuf)) < 0) {
2098 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fprintf");
2099 abd2672a 2018-12-23 stsp goto done;
2101 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2102 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2103 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2104 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2105 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fprintf");
2106 abd2672a 2018-12-23 stsp goto done;
2108 45d799e2 2018-12-23 stsp if (fprintf(outfile, "%s\n",
2109 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(commit)) < 0) {
2110 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fprintf");
2111 abd2672a 2018-12-23 stsp goto done;
2114 abd2672a 2018-12-23 stsp free(id_str);
2115 8b473291 2019-02-21 stsp free(refs_str);
2116 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2117 abd2672a 2018-12-23 stsp return err;
2120 abd2672a 2018-12-23 stsp static const struct got_error *
2121 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2123 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2124 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2125 15a94983 2018-12-23 stsp int obj_type;
2127 511a516b 2018-05-19 stsp f = got_opentemp();
2128 48ae06ee 2018-10-18 stsp if (f == NULL) {
2129 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_opentemp");
2130 48ae06ee 2018-10-18 stsp goto done;
2132 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2133 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fclose");
2134 fb43ecf1 2019-02-11 stsp goto done;
2138 15a94983 2018-12-23 stsp if (s->id1)
2139 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2141 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2143 15a94983 2018-12-23 stsp goto done;
2145 15a94983 2018-12-23 stsp switch (obj_type) {
2146 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2147 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2148 54156555 2018-12-24 stsp s->diff_context, s->repo, f);
2150 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2151 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2152 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
2154 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2155 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2156 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2157 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2159 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2162 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2163 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2164 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2166 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2167 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2168 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2169 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2170 8b473291 2019-02-21 stsp s->repo, f);
2175 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2177 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2178 15a94983 2018-12-23 stsp s->diff_context, s->repo, f);
2182 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2186 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2187 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fflush");
2188 48ae06ee 2018-10-18 stsp return err;
2191 f5215bb9 2019-02-22 stsp static void
2192 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2194 f5215bb9 2019-02-22 stsp werase(view->window);
2195 f5215bb9 2019-02-22 stsp waddstr(view->window, "diffing...");
2196 f5215bb9 2019-02-22 stsp update_panels();
2197 f5215bb9 2019-02-22 stsp doupdate();
2200 48ae06ee 2018-10-18 stsp static const struct got_error *
2201 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2202 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2203 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2205 48ae06ee 2018-10-18 stsp const struct got_error *err;
2207 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2208 15a94983 2018-12-23 stsp int type1, type2;
2209 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2211 15a94983 2018-12-23 stsp return err;
2212 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2214 15a94983 2018-12-23 stsp return err;
2216 15a94983 2018-12-23 stsp if (type1 != type2)
2217 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2220 15a94983 2018-12-23 stsp if (id1) {
2221 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2222 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2223 230a42bd 2019-05-11 jcs return got_error_prefix_errno("got_object_id_dup");
2225 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2227 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2228 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2229 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2230 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2231 230a42bd 2019-05-11 jcs return got_error_prefix_errno("got_object_id_dup");
2233 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2234 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2235 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2236 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2237 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2238 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2239 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2241 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2242 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2243 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2245 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2246 48ae06ee 2018-10-18 stsp if (err) {
2247 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2248 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2249 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2250 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2251 48ae06ee 2018-10-18 stsp return err;
2254 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2255 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2256 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2258 5dc9f4bc 2018-08-04 stsp return NULL;
2261 e5a0f69f 2018-08-18 stsp static const struct got_error *
2262 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2264 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2266 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2267 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2268 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2269 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2270 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2271 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fclose");
2272 e5a0f69f 2018-08-18 stsp return err;
2275 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2276 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2278 a3404814 2018-09-02 stsp const struct got_error *err;
2279 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2280 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2282 a3404814 2018-09-02 stsp if (s->id1) {
2283 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2285 a3404814 2018-09-02 stsp return err;
2287 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2289 a3404814 2018-09-02 stsp return err;
2291 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2292 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2293 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
2294 a3404814 2018-09-02 stsp free(id_str1);
2295 a3404814 2018-09-02 stsp free(id_str2);
2296 a3404814 2018-09-02 stsp return err;
2298 a3404814 2018-09-02 stsp free(id_str1);
2299 a3404814 2018-09-02 stsp free(id_str2);
2301 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2302 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2306 15a087fe 2019-02-21 stsp static const struct got_error *
2307 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2308 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2310 d7a04538 2019-02-21 stsp const struct got_error *err;
2311 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
2312 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
2313 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
2315 15a087fe 2019-02-21 stsp free(s->id2);
2316 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2317 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2318 230a42bd 2019-05-11 jcs return got_error_prefix_errno("got_object_id_dup");
2320 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2322 d7a04538 2019-02-21 stsp return err;
2323 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
2324 15a087fe 2019-02-21 stsp free(s->id1);
2325 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
2326 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2327 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
2328 15a087fe 2019-02-21 stsp return NULL;
2331 0cf4efb1 2018-09-29 stsp static const struct got_error *
2332 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2333 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2335 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2336 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2337 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
2338 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
2341 e5a0f69f 2018-08-18 stsp switch (ch) {
2343 1e37a5c2 2019-05-12 jcs case KEY_UP:
2344 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
2345 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2347 1e37a5c2 2019-05-12 jcs view_flash(view);
2349 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2350 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
2351 1e37a5c2 2019-05-12 jcs view_flash(view);
2355 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
2356 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
2357 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2360 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2361 1e37a5c2 2019-05-12 jcs if (!s->eof)
2362 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2364 1e37a5c2 2019-05-12 jcs view_flash(view);
2366 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
2368 1e37a5c2 2019-05-12 jcs if (s->eof) {
2369 1e37a5c2 2019-05-12 jcs view_flash(view);
2373 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
2374 1e37a5c2 2019-05-12 jcs char *line;
2375 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
2376 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2377 1e37a5c2 2019-05-12 jcs if (line == NULL)
2382 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
2383 1e37a5c2 2019-05-12 jcs s->diff_context--;
2384 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2385 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2389 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2390 1e37a5c2 2019-05-12 jcs s->diff_context++;
2391 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2392 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2397 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2399 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2400 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
2401 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
2402 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2405 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2410 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2414 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2415 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2417 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2418 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2422 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2424 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2426 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2427 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
2429 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
2430 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
2431 1e37a5c2 2019-05-12 jcs update_panels();
2432 1e37a5c2 2019-05-12 jcs doupdate();
2434 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
2435 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
2436 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
2437 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
2441 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2446 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
2447 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2450 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2454 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2455 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2457 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2458 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2464 bcbd79e2 2018-08-19 stsp return err;
2467 4ed7e80c 2018-05-20 stsp static const struct got_error *
2468 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2470 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2471 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2472 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2473 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2474 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2475 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2477 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2479 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2481 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2482 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2483 eb6600df 2019-01-04 stsp NULL) == -1)
2484 26ed57b2 2018-05-19 stsp err(1, "pledge");
2487 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2488 26ed57b2 2018-05-19 stsp switch (ch) {
2490 17020d27 2019-03-07 stsp usage_diff();
2491 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2495 26ed57b2 2018-05-19 stsp argc -= optind;
2496 26ed57b2 2018-05-19 stsp argv += optind;
2498 26ed57b2 2018-05-19 stsp if (argc == 0) {
2499 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2500 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2501 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2502 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2503 230a42bd 2019-05-11 jcs return got_error_prefix_errno("getcwd");
2504 15a94983 2018-12-23 stsp id_str1 = argv[0];
2505 15a94983 2018-12-23 stsp id_str2 = argv[1];
2506 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2507 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2508 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2509 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("realpath", argv[0]);
2510 15a94983 2018-12-23 stsp id_str1 = argv[1];
2511 15a94983 2018-12-23 stsp id_str2 = argv[2];
2513 26ed57b2 2018-05-19 stsp usage_diff();
2515 a915003a 2019-02-05 stsp init_curses();
2517 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2518 eb6600df 2019-01-04 stsp if (error)
2519 eb6600df 2019-01-04 stsp goto done;
2521 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
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(&id1, repo, id_str1);
2526 26ed57b2 2018-05-19 stsp if (error)
2527 26ed57b2 2018-05-19 stsp goto done;
2529 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
2530 26ed57b2 2018-05-19 stsp if (error)
2531 26ed57b2 2018-05-19 stsp goto done;
2533 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2534 8b473291 2019-02-21 stsp if (error)
2535 8b473291 2019-02-21 stsp goto done;
2537 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2538 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2539 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("view_open");
2540 ea5e7bb5 2018-08-01 stsp goto done;
2542 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2543 5dc9f4bc 2018-08-04 stsp if (error)
2544 5dc9f4bc 2018-08-04 stsp goto done;
2545 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2547 c02c541e 2019-03-29 stsp free(repo_path);
2548 26ed57b2 2018-05-19 stsp got_repo_close(repo);
2549 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2550 26ed57b2 2018-05-19 stsp return error;
2553 4ed7e80c 2018-05-20 stsp __dead static void
2554 9f7d7167 2018-04-29 stsp usage_blame(void)
2557 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2558 9f7d7167 2018-04-29 stsp getprogname());
2562 84451b3e 2018-07-10 stsp struct tog_blame_line {
2563 84451b3e 2018-07-10 stsp int annotated;
2564 84451b3e 2018-07-10 stsp struct got_object_id *id;
2567 4ed7e80c 2018-05-20 stsp static const struct got_error *
2568 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2569 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2570 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2571 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2573 84451b3e 2018-07-10 stsp const struct got_error *err;
2574 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2575 84451b3e 2018-07-10 stsp char *line;
2576 84451b3e 2018-07-10 stsp size_t len;
2577 84451b3e 2018-07-10 stsp wchar_t *wline;
2578 b700b5d6 2018-07-10 stsp int width, wlimit;
2579 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2580 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2581 ab089a2a 2018-07-12 stsp char *id_str;
2583 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2585 ab089a2a 2018-07-12 stsp return err;
2587 84451b3e 2018-07-10 stsp rewind(f);
2588 f7d12f7e 2018-08-01 stsp werase(view->window);
2590 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
2591 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
2592 ab089a2a 2018-07-12 stsp free(id_str);
2593 ab089a2a 2018-07-12 stsp return err;
2596 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2597 ab089a2a 2018-07-12 stsp free(line);
2598 2550e4c3 2018-07-13 stsp line = NULL;
2599 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2600 a3404814 2018-09-02 stsp wstandout(view->window);
2601 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2602 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2603 a3404814 2018-09-02 stsp wstandend(view->window);
2604 2550e4c3 2018-07-13 stsp free(wline);
2605 2550e4c3 2018-07-13 stsp wline = NULL;
2606 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2607 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2609 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2610 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2611 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
2612 ab089a2a 2018-07-12 stsp free(id_str);
2613 230a42bd 2019-05-11 jcs return got_error_prefix_errno("asprintf");
2615 ab089a2a 2018-07-12 stsp free(id_str);
2616 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2617 3f60a8ef 2018-07-10 stsp free(line);
2618 2550e4c3 2018-07-13 stsp line = NULL;
2620 3f60a8ef 2018-07-10 stsp return err;
2621 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2622 2550e4c3 2018-07-13 stsp free(wline);
2623 2550e4c3 2018-07-13 stsp wline = NULL;
2624 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2625 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2628 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2629 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2630 84451b3e 2018-07-10 stsp if (line == NULL) {
2634 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2635 84451b3e 2018-07-10 stsp free(line);
2639 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2640 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2641 84451b3e 2018-07-10 stsp if (err) {
2642 84451b3e 2018-07-10 stsp free(line);
2643 84451b3e 2018-07-10 stsp return err;
2646 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2647 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2649 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2650 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2651 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2652 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2653 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2654 84451b3e 2018-07-10 stsp char *id_str;
2655 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2656 84451b3e 2018-07-10 stsp if (err) {
2657 84451b3e 2018-07-10 stsp free(line);
2658 2550e4c3 2018-07-13 stsp free(wline);
2659 84451b3e 2018-07-10 stsp return err;
2661 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2662 84451b3e 2018-07-10 stsp free(id_str);
2663 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2665 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2666 ee41ec32 2018-07-10 stsp prev_id = NULL;
2669 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2670 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2671 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2674 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2675 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2676 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2677 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2678 84451b3e 2018-07-10 stsp free(line);
2679 2550e4c3 2018-07-13 stsp free(wline);
2680 2550e4c3 2018-07-13 stsp wline = NULL;
2682 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2684 1a57306a 2018-09-02 stsp view_vborder(view);
2686 84451b3e 2018-07-10 stsp return NULL;
2689 84451b3e 2018-07-10 stsp static const struct got_error *
2690 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2692 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2693 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2694 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2695 1a76625f 2018-10-22 stsp int errcode;
2697 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2698 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2699 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2701 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2702 1a76625f 2018-10-22 stsp if (errcode)
2703 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
2705 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2706 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2707 d68a0a7d 2018-07-10 stsp goto done;
2710 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2711 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2713 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2714 d68a0a7d 2018-07-10 stsp if (line->annotated)
2715 d68a0a7d 2018-07-10 stsp goto done;
2717 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2718 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2719 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_object_id_dup");
2720 84451b3e 2018-07-10 stsp goto done;
2722 84451b3e 2018-07-10 stsp line->annotated = 1;
2724 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2725 1a76625f 2018-10-22 stsp if (errcode)
2726 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2727 84451b3e 2018-07-10 stsp return err;
2730 84451b3e 2018-07-10 stsp static void *
2731 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2733 18430de3 2018-07-10 stsp const struct got_error *err;
2734 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
2735 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
2736 1a76625f 2018-10-22 stsp int errcode;
2738 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2739 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
2741 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2742 1a76625f 2018-10-22 stsp if (errcode)
2743 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
2744 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2746 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
2747 c9beca56 2018-07-22 stsp ta->repo = NULL;
2748 c9beca56 2018-07-22 stsp *ta->complete = 1;
2750 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2751 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2752 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2754 18430de3 2018-07-10 stsp return (void *)err;
2757 245d91c1 2018-07-12 stsp static struct got_object_id *
2758 15a94983 2018-12-23 stsp get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2759 15a94983 2018-12-23 stsp int selected_line)
2761 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2763 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2764 245d91c1 2018-07-12 stsp if (!line->annotated)
2765 245d91c1 2018-07-12 stsp return NULL;
2767 245d91c1 2018-07-12 stsp return line->id;
2770 b880a918 2018-07-10 stsp static const struct got_error *
2771 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2773 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2776 245d91c1 2018-07-12 stsp if (blame->thread) {
2777 1a76625f 2018-10-22 stsp int errcode;
2778 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2779 1a76625f 2018-10-22 stsp if (errcode)
2780 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2781 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
2782 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
2783 1a76625f 2018-10-22 stsp if (errcode)
2784 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
2785 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2786 1a76625f 2018-10-22 stsp if (errcode)
2787 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
2788 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
2789 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2790 245d91c1 2018-07-12 stsp err = NULL;
2791 245d91c1 2018-07-12 stsp blame->thread = NULL;
2793 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2794 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2795 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2797 245d91c1 2018-07-12 stsp if (blame->f) {
2798 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
2799 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("fclose");
2800 245d91c1 2018-07-12 stsp blame->f = NULL;
2802 57670559 2018-12-23 stsp if (blame->lines) {
2803 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
2804 57670559 2018-12-23 stsp free(blame->lines[i].id);
2805 57670559 2018-12-23 stsp free(blame->lines);
2806 57670559 2018-12-23 stsp blame->lines = NULL;
2808 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2809 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2811 245d91c1 2018-07-12 stsp return err;
2814 245d91c1 2018-07-12 stsp static const struct got_error *
2815 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2816 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
2817 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
2818 245d91c1 2018-07-12 stsp struct got_repository *repo)
2820 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2821 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2822 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2823 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2824 15a94983 2018-12-23 stsp int obj_type;
2826 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2828 15a94983 2018-12-23 stsp return err;
2829 15a94983 2018-12-23 stsp if (obj_id == NULL)
2830 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
2832 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
2834 84451b3e 2018-07-10 stsp goto done;
2836 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2837 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2838 84451b3e 2018-07-10 stsp goto done;
2841 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2843 a70480e0 2018-06-23 stsp goto done;
2844 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2845 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2846 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_opentemp");
2847 84451b3e 2018-07-10 stsp goto done;
2849 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2850 245d91c1 2018-07-12 stsp blame->f, blob);
2852 84451b3e 2018-07-10 stsp goto done;
2854 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2855 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2856 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("calloc");
2857 84451b3e 2018-07-10 stsp goto done;
2860 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2862 bd24772e 2018-07-11 stsp goto done;
2864 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2865 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2866 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2867 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2868 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2869 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_object_id_dup");
2870 245d91c1 2018-07-12 stsp goto done;
2872 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2874 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2875 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2876 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2877 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2878 245d91c1 2018-07-12 stsp *blame_complete = 0;
2882 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2883 27d434c2 2018-09-15 stsp free(obj_id);
2885 245d91c1 2018-07-12 stsp stop_blame(blame);
2886 245d91c1 2018-07-12 stsp return err;
2889 245d91c1 2018-07-12 stsp static const struct got_error *
2890 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2891 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
2892 8b473291 2019-02-21 stsp struct got_repository *repo)
2894 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2895 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2897 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2899 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2901 7cbe629d 2018-08-04 stsp return err;
2903 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2904 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2905 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2906 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2907 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2908 fb2756b9 2018-08-04 stsp s->path = path;
2909 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2910 230a42bd 2019-05-11 jcs return got_error_prefix_errno("open_blame_view");
2911 fb2756b9 2018-08-04 stsp s->repo = repo;
2912 8b473291 2019-02-21 stsp s->refs = refs;
2913 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2914 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2916 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2917 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2918 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2920 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
2921 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2922 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2923 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2926 e5a0f69f 2018-08-18 stsp static const struct got_error *
2927 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2929 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2930 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2932 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2933 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2935 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2936 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2937 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2938 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2939 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2942 e5a0f69f 2018-08-18 stsp free(s->path);
2944 e5a0f69f 2018-08-18 stsp return err;
2947 7cbe629d 2018-08-04 stsp static const struct got_error *
2948 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2950 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2951 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2952 2b380cc8 2018-10-24 stsp int errcode;
2954 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
2955 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2956 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
2957 2b380cc8 2018-10-24 stsp if (errcode)
2958 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2961 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2962 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2963 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2964 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2966 669b5ffa 2018-10-07 stsp view_vborder(view);
2967 e5a0f69f 2018-08-18 stsp return err;
2970 e5a0f69f 2018-08-18 stsp static const struct got_error *
2971 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2972 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2974 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2975 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2976 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2977 669b5ffa 2018-10-07 stsp int begin_x = 0;
2979 e5a0f69f 2018-08-18 stsp switch (ch) {
2981 1e37a5c2 2019-05-12 jcs s->done = 1;
2984 1e37a5c2 2019-05-12 jcs case KEY_UP:
2985 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
2986 1e37a5c2 2019-05-12 jcs s->selected_line--;
2987 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
2988 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
2989 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2991 1e37a5c2 2019-05-12 jcs view_flash(view);
2993 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2994 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
2995 1e37a5c2 2019-05-12 jcs if (s->selected_line == 1) {
2996 34bc9ec9 2019-02-22 stsp view_flash(view);
2999 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3002 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3003 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3004 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3006 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3009 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3010 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3011 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3012 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3013 1e37a5c2 2019-05-12 jcs s->selected_line++;
3014 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3015 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3016 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3018 1e37a5c2 2019-05-12 jcs view_flash(view);
3021 1e37a5c2 2019-05-12 jcs case 'p': {
3022 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3023 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3024 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3025 1e37a5c2 2019-05-12 jcs if (id == NULL)
3027 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3028 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3029 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3030 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3031 1e37a5c2 2019-05-12 jcs int obj_type;
3032 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3033 1e37a5c2 2019-05-12 jcs s->repo, id);
3036 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3037 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3038 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3039 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3042 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3043 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3044 1e37a5c2 2019-05-12 jcs pid->id, s->path);
3045 e5a0f69f 2018-08-18 stsp if (err) {
3046 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
3047 1e37a5c2 2019-05-12 jcs err = NULL;
3048 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3051 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
3053 1e37a5c2 2019-05-12 jcs free(blob_id);
3054 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
3055 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
3056 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3059 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3061 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3063 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
3064 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
3066 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3071 1e37a5c2 2019-05-12 jcs s->done = 1;
3072 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3073 1e37a5c2 2019-05-12 jcs s->done = 0;
3074 1e37a5c2 2019-05-12 jcs if (thread_err)
3076 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3077 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
3078 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3079 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3080 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
3081 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
3086 1e37a5c2 2019-05-12 jcs case 'B': {
3087 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
3088 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
3089 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
3091 1e37a5c2 2019-05-12 jcs s->done = 1;
3092 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3093 1e37a5c2 2019-05-12 jcs s->done = 0;
3094 1e37a5c2 2019-05-12 jcs if (thread_err)
3096 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3097 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
3098 1e37a5c2 2019-05-12 jcs s->blamed_commit =
3099 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
3100 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3101 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3102 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
3103 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
3108 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3109 1e37a5c2 2019-05-12 jcs case '\r': {
3110 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3111 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
3112 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
3113 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3114 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3115 1e37a5c2 2019-05-12 jcs if (id == NULL)
3117 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
3120 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
3121 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
3122 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3123 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3124 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3125 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
3126 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3127 1e37a5c2 2019-05-12 jcs err = got_error_prefix_errno("view_open");
3130 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
3131 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
3132 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3134 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3137 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3138 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3141 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
3143 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3146 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
3147 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
3149 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3154 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3156 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3157 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
3158 1e37a5c2 2019-05-12 jcs view->nlines - 2)) {
3159 1e37a5c2 2019-05-12 jcs view_flash(view);
3162 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3163 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
3164 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3165 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3168 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
3169 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
3170 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
3171 1e37a5c2 2019-05-12 jcs view->nlines - 2;
3173 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
3174 1e37a5c2 2019-05-12 jcs s->blame.nlines -
3175 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
3177 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3178 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
3179 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3180 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3186 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
3189 a70480e0 2018-06-23 stsp static const struct got_error *
3190 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
3192 a70480e0 2018-06-23 stsp const struct got_error *error;
3193 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
3194 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3195 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
3196 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3197 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3198 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
3200 e1cd8fed 2018-08-01 stsp struct tog_view *view;
3202 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3204 a70480e0 2018-06-23 stsp #ifndef PROFILE
3205 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3206 8e94dd5b 2019-01-04 stsp NULL) == -1)
3207 a70480e0 2018-06-23 stsp err(1, "pledge");
3210 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3211 a70480e0 2018-06-23 stsp switch (ch) {
3213 a70480e0 2018-06-23 stsp commit_id_str = optarg;
3216 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3217 69069811 2018-08-02 stsp if (repo_path == NULL)
3218 69069811 2018-08-02 stsp err(1, "-r option");
3221 17020d27 2019-03-07 stsp usage_blame();
3222 a70480e0 2018-06-23 stsp /* NOTREACHED */
3226 a70480e0 2018-06-23 stsp argc -= optind;
3227 a70480e0 2018-06-23 stsp argv += optind;
3229 69069811 2018-08-02 stsp if (argc == 1)
3230 69069811 2018-08-02 stsp path = argv[0];
3232 a70480e0 2018-06-23 stsp usage_blame();
3234 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
3235 69069811 2018-08-02 stsp if (cwd == NULL) {
3236 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("getcwd");
3237 69069811 2018-08-02 stsp goto done;
3239 69069811 2018-08-02 stsp if (repo_path == NULL) {
3240 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3241 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3242 69069811 2018-08-02 stsp goto done;
3244 eb41ed75 2019-02-05 stsp error = NULL;
3245 eb41ed75 2019-02-05 stsp if (worktree) {
3246 eb41ed75 2019-02-05 stsp repo_path =
3247 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3248 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
3249 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
3250 eb41ed75 2019-02-05 stsp if (error)
3251 eb41ed75 2019-02-05 stsp goto done;
3253 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
3254 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
3255 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
3256 eb41ed75 2019-02-05 stsp goto done;
3261 a915003a 2019-02-05 stsp init_curses();
3263 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
3264 c02c541e 2019-03-29 stsp if (error != NULL)
3265 8e94dd5b 2019-01-04 stsp goto done;
3267 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3268 c02c541e 2019-03-29 stsp if (error)
3269 92205607 2019-01-04 stsp goto done;
3271 eb41ed75 2019-02-05 stsp if (worktree) {
3272 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3273 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3274 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3275 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3276 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3277 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3278 eb41ed75 2019-02-05 stsp path) == -1) {
3279 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("asprintf");
3280 eb41ed75 2019-02-05 stsp goto done;
3282 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3285 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3287 eb41ed75 2019-02-05 stsp if (error)
3288 69069811 2018-08-02 stsp goto done;
3290 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
3291 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
3292 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3293 a70480e0 2018-06-23 stsp if (error != NULL)
3294 66b4983c 2018-06-23 stsp goto done;
3295 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3296 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
3298 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
3299 15a94983 2018-12-23 stsp commit_id_str);
3301 a19e88aa 2018-06-23 stsp if (error != NULL)
3302 8b473291 2019-02-21 stsp goto done;
3304 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
3305 8b473291 2019-02-21 stsp if (error)
3306 a19e88aa 2018-06-23 stsp goto done;
3308 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3309 e1cd8fed 2018-08-01 stsp if (view == NULL) {
3310 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("view_open");
3311 e1cd8fed 2018-08-01 stsp goto done;
3313 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3314 7cbe629d 2018-08-04 stsp if (error)
3315 7cbe629d 2018-08-04 stsp goto done;
3316 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3318 69069811 2018-08-02 stsp free(repo_path);
3319 69069811 2018-08-02 stsp free(cwd);
3320 a70480e0 2018-06-23 stsp free(commit_id);
3321 eb41ed75 2019-02-05 stsp if (worktree)
3322 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
3324 a70480e0 2018-06-23 stsp got_repo_close(repo);
3325 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3326 a70480e0 2018-06-23 stsp return error;
3329 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3330 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
3331 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
3332 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
3333 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
3334 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
3335 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
3337 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3338 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
3339 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
3340 ffd1d5e5 2018-06-23 stsp int width, n;
3342 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
3344 f7d12f7e 2018-08-01 stsp werase(view->window);
3346 ffd1d5e5 2018-06-23 stsp if (limit == 0)
3347 ffd1d5e5 2018-06-23 stsp return NULL;
3349 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
3351 ffd1d5e5 2018-06-23 stsp return err;
3352 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3353 a3404814 2018-09-02 stsp wstandout(view->window);
3354 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3355 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3356 a3404814 2018-09-02 stsp wstandend(view->window);
3357 2550e4c3 2018-07-13 stsp free(wline);
3358 2550e4c3 2018-07-13 stsp wline = NULL;
3359 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
3360 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3361 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3362 ffd1d5e5 2018-06-23 stsp return NULL;
3363 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
3365 ce52c690 2018-06-23 stsp return err;
3366 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3367 2550e4c3 2018-07-13 stsp free(wline);
3368 2550e4c3 2018-07-13 stsp wline = NULL;
3369 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
3370 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3371 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3372 ffd1d5e5 2018-06-23 stsp return NULL;
3373 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3374 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
3375 a1eca9bb 2018-06-23 stsp return NULL;
3377 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3378 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
3379 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
3380 0cf4efb1 2018-09-29 stsp if (view->focussed)
3381 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3382 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
3384 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
3385 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
3386 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3387 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3388 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3389 ffd1d5e5 2018-06-23 stsp return NULL;
3393 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
3394 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3397 ffd1d5e5 2018-06-23 stsp while (te) {
3398 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
3400 1d13200f 2018-07-12 stsp if (show_ids) {
3401 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
3403 230a42bd 2019-05-11 jcs return got_error_prefix_errno(
3404 230a42bd 2019-05-11 jcs "got_object_id_str");
3406 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3407 d8355ef1 2019-02-10 stsp te->name, S_ISDIR(te->mode) ? "/" :
3408 d8355ef1 2019-02-10 stsp ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3409 1d13200f 2018-07-12 stsp free(id_str);
3410 230a42bd 2019-05-11 jcs return got_error_prefix_errno("asprintf");
3412 1d13200f 2018-07-12 stsp free(id_str);
3413 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
3414 ffd1d5e5 2018-06-23 stsp if (err) {
3415 ffd1d5e5 2018-06-23 stsp free(line);
3418 ffd1d5e5 2018-06-23 stsp if (n == selected) {
3419 0cf4efb1 2018-09-29 stsp if (view->focussed)
3420 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3421 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
3423 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3424 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
3425 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3426 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
3427 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3428 ffd1d5e5 2018-06-23 stsp free(line);
3429 2550e4c3 2018-07-13 stsp free(wline);
3430 2550e4c3 2018-07-13 stsp wline = NULL;
3432 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3433 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
3434 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3436 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3439 ffd1d5e5 2018-06-23 stsp return err;
3442 ffd1d5e5 2018-06-23 stsp static void
3443 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
3444 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
3445 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
3447 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
3450 5e37ffca 2019-02-25 stsp if (*first_displayed_entry == NULL) {
3451 5e37ffca 2019-02-25 stsp view_flash(view);
3455 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3456 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
3457 34bc9ec9 2019-02-22 stsp view_flash(view);
3458 ffd1d5e5 2018-06-23 stsp if (!isroot)
3459 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3463 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
3464 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
3465 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
3466 ffd1d5e5 2018-06-23 stsp prev = te;
3467 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3469 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
3470 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3472 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3473 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3476 768394f3 2019-01-24 stsp static int
3477 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3478 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
3479 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
3481 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
3482 ffd1d5e5 2018-06-23 stsp int n = 0;
3484 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
3485 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3487 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
3488 768394f3 2019-01-24 stsp last = last_displayed_entry;
3489 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
3490 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
3491 768394f3 2019-01-24 stsp if (last) {
3492 768394f3 2019-01-24 stsp *first_displayed_entry = next;
3493 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
3499 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3500 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
3501 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
3503 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
3504 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
3505 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
3507 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
3508 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
3510 ce52c690 2018-06-23 stsp len += strlen(te->name);
3512 ce52c690 2018-06-23 stsp *path = calloc(1, len);
3513 ffd1d5e5 2018-06-23 stsp if (path == NULL)
3514 230a42bd 2019-05-11 jcs return got_error_prefix_errno("calloc");
3516 ce52c690 2018-06-23 stsp (*path)[0] = '/';
3517 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
3518 d9765a41 2018-06-23 stsp while (pt) {
3519 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3520 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3521 cb2ebc8a 2018-06-23 stsp goto done;
3523 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
3524 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3525 cb2ebc8a 2018-06-23 stsp goto done;
3527 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3530 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3531 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3532 ce52c690 2018-06-23 stsp goto done;
3536 ce52c690 2018-06-23 stsp if (err) {
3537 ce52c690 2018-06-23 stsp free(*path);
3538 ce52c690 2018-06-23 stsp *path = NULL;
3540 ce52c690 2018-06-23 stsp return err;
3543 ce52c690 2018-06-23 stsp static const struct got_error *
3544 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3545 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3546 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3547 8b473291 2019-02-21 stsp struct got_repository *repo)
3549 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3550 ce52c690 2018-06-23 stsp char *path;
3551 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3553 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3555 ce52c690 2018-06-23 stsp return err;
3557 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3558 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3559 230a42bd 2019-05-11 jcs return got_error_prefix_errno("view_open");
3561 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
3562 e5a0f69f 2018-08-18 stsp if (err) {
3563 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3564 e5a0f69f 2018-08-18 stsp free(path);
3566 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3567 69efd4c4 2018-07-18 stsp return err;
3570 69efd4c4 2018-07-18 stsp static const struct got_error *
3571 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3572 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3573 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3574 8b473291 2019-02-21 stsp struct got_repository *repo)
3576 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3577 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3578 69efd4c4 2018-07-18 stsp char *path;
3580 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3581 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3582 230a42bd 2019-05-11 jcs return got_error_prefix_errno("view_open");
3584 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3586 69efd4c4 2018-07-18 stsp return err;
3588 8b473291 2019-02-21 stsp err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3590 e5a0f69f 2018-08-18 stsp view_close(log_view);
3592 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3593 cb2ebc8a 2018-06-23 stsp free(path);
3594 cb2ebc8a 2018-06-23 stsp return err;
3597 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3598 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3599 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3600 8b473291 2019-02-21 stsp struct got_repository *repo)
3602 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3603 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3604 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3606 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3608 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3609 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3610 ffd1d5e5 2018-06-23 stsp goto done;
3612 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3613 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("asprintf");
3614 ffd1d5e5 2018-06-23 stsp goto done;
3617 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3618 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3619 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3620 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3621 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3622 230a42bd 2019-05-11 jcs err = got_error_prefix_errno("got_object_id_dup");
3623 6484ec90 2018-09-29 stsp goto done;
3625 8b473291 2019-02-21 stsp s->refs = refs;
3626 fb2756b9 2018-08-04 stsp s->repo = repo;
3628 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
3629 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
3630 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
3632 ad80ab7b 2018-08-04 stsp free(commit_id_str);
3633 6484ec90 2018-09-29 stsp if (err) {
3634 fb2756b9 2018-08-04 stsp free(s->tree_label);
3635 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3637 ad80ab7b 2018-08-04 stsp return err;
3640 e5a0f69f 2018-08-18 stsp static const struct got_error *
3641 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
3643 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3645 fb2756b9 2018-08-04 stsp free(s->tree_label);
3646 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3647 6484ec90 2018-09-29 stsp free(s->commit_id);
3648 6484ec90 2018-09-29 stsp s->commit_id = NULL;
3649 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
3650 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
3651 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
3652 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
3653 ad80ab7b 2018-08-04 stsp free(parent);
3656 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
3657 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
3658 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
3660 e5a0f69f 2018-08-18 stsp return NULL;
3663 ad80ab7b 2018-08-04 stsp static const struct got_error *
3664 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
3666 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
3667 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3668 e5a0f69f 2018-08-18 stsp char *parent_path;
3670 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
3672 e5a0f69f 2018-08-18 stsp return err;
3674 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
3675 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
3676 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3677 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
3678 e5a0f69f 2018-08-18 stsp free(parent_path);
3680 669b5ffa 2018-10-07 stsp view_vborder(view);
3681 e5a0f69f 2018-08-18 stsp return err;
3684 e5a0f69f 2018-08-18 stsp static const struct got_error *
3685 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3686 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3688 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3689 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
3690 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
3691 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
3693 e5a0f69f 2018-08-18 stsp switch (ch) {
3695 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
3698 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
3700 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3701 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3702 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
3703 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
3704 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
3705 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3706 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3708 1e37a5c2 2019-05-12 jcs return err;
3709 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
3711 1e37a5c2 2019-05-12 jcs view_close(log_view);
3714 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
3715 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
3717 1e37a5c2 2019-05-12 jcs *new_view = log_view;
3720 1e37a5c2 2019-05-12 jcs case KEY_UP:
3721 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
3722 1e37a5c2 2019-05-12 jcs s->selected--;
3723 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
3726 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
3728 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
3729 1e37a5c2 2019-05-12 jcs s->entries, s->tree == s->root);
3731 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3732 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
3733 1e37a5c2 2019-05-12 jcs MAX(0, view->nlines - 4 - s->selected), s->entries,
3734 1e37a5c2 2019-05-12 jcs s->tree == s->root);
3735 1e37a5c2 2019-05-12 jcs s->selected = 0;
3736 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_FIRST(&s->entries->head) ==
3737 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
3738 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
3741 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3742 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
3743 1e37a5c2 2019-05-12 jcs s->selected++;
3746 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3748 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
3749 1e37a5c2 2019-05-12 jcs view_flash(view);
3752 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
3753 1e37a5c2 2019-05-12 jcs s->last_displayed_entry, s->entries);
3755 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3756 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3758 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
3759 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
3760 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
3762 1e37a5c2 2019-05-12 jcs view_flash(view);
3765 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
3766 1e37a5c2 2019-05-12 jcs view->nlines, s->last_displayed_entry, s->entries);
3767 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
3768 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
3769 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
3770 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
3772 1e37a5c2 2019-05-12 jcs ndisplayed++;
3773 1e37a5c2 2019-05-12 jcs te = SIMPLEQ_NEXT(te, entry);
3774 1e37a5c2 2019-05-12 jcs } while (te);
3775 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
3778 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3780 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL) {
3781 1e37a5c2 2019-05-12 jcs struct tog_parent_tree *parent;
3782 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
3783 1e37a5c2 2019-05-12 jcs /* user selected '..' */
3784 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
3786 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
3787 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
3789 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
3790 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
3791 1e37a5c2 2019-05-12 jcs s->entries =
3792 1e37a5c2 2019-05-12 jcs got_object_tree_get_entries(s->tree);
3793 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
3794 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
3795 1e37a5c2 2019-05-12 jcs s->selected_entry =
3796 1e37a5c2 2019-05-12 jcs parent->selected_entry;
3797 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
3798 1e37a5c2 2019-05-12 jcs free(parent);
3799 1e37a5c2 2019-05-12 jcs } else if (S_ISDIR(s->selected_entry->mode)) {
3800 1e37a5c2 2019-05-12 jcs struct tog_parent_tree *parent;
3801 1e37a5c2 2019-05-12 jcs struct got_tree_object *child;
3802 1e37a5c2 2019-05-12 jcs err = got_object_open_as_tree(&child,
3803 1e37a5c2 2019-05-12 jcs s->repo, s->selected_entry->id);
3806 1e37a5c2 2019-05-12 jcs parent = calloc(1, sizeof(*parent));
3807 1e37a5c2 2019-05-12 jcs if (parent == NULL) {
3808 1e37a5c2 2019-05-12 jcs err = got_error_prefix_errno("calloc");
3811 1e37a5c2 2019-05-12 jcs parent->tree = s->tree;
3812 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry =
3813 1e37a5c2 2019-05-12 jcs s->first_displayed_entry;
3814 1e37a5c2 2019-05-12 jcs parent->selected_entry = s->selected_entry;
3815 1e37a5c2 2019-05-12 jcs parent->selected = s->selected;
3816 1e37a5c2 2019-05-12 jcs TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3817 1e37a5c2 2019-05-12 jcs s->tree = child;
3818 1e37a5c2 2019-05-12 jcs s->entries =
3819 1e37a5c2 2019-05-12 jcs got_object_tree_get_entries(s->tree);
3820 1e37a5c2 2019-05-12 jcs s->selected = 0;
3821 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
3822 1e37a5c2 2019-05-12 jcs } else if (S_ISREG(s->selected_entry->mode)) {
3823 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
3824 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
3825 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
3827 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
3828 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
3829 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
3832 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3833 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3835 669b5ffa 2018-10-07 stsp return err;
3836 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
3837 669b5ffa 2018-10-07 stsp if (err) {
3838 1e37a5c2 2019-05-12 jcs view_close(blame_view);
3841 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
3842 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3844 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
3847 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3848 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
3849 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
3855 ffd1d5e5 2018-06-23 stsp return err;
3858 ffd1d5e5 2018-06-23 stsp __dead static void
3859 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3862 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3863 ffd1d5e5 2018-06-23 stsp getprogname());
3867 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3868 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3870 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3871 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3872 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3873 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3874 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3875 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3876 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3877 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3879 5221c383 2018-08-01 stsp struct tog_view *view;
3881 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3883 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3884 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3885 d188b9a6 2019-01-04 stsp NULL) == -1)
3886 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3889 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3890 ffd1d5e5 2018-06-23 stsp switch (ch) {
3892 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3895 17020d27 2019-03-07 stsp usage_tree();
3896 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3900 ffd1d5e5 2018-06-23 stsp argc -= optind;
3901 ffd1d5e5 2018-06-23 stsp argv += optind;
3903 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3904 52185f70 2019-02-05 stsp struct got_worktree *worktree;
3905 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
3906 52185f70 2019-02-05 stsp if (cwd == NULL)
3907 230a42bd 2019-05-11 jcs return got_error_prefix_errno("getcwd");
3908 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3909 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3910 52185f70 2019-02-05 stsp goto done;
3911 52185f70 2019-02-05 stsp if (worktree) {
3912 52185f70 2019-02-05 stsp free(cwd);
3913 52185f70 2019-02-05 stsp repo_path =
3914 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3915 52185f70 2019-02-05 stsp got_worktree_close(worktree);
3917 52185f70 2019-02-05 stsp repo_path = cwd;
3918 52185f70 2019-02-05 stsp if (repo_path == NULL) {
3919 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("strdup");
3920 52185f70 2019-02-05 stsp goto done;
3922 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3923 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3924 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3925 230a42bd 2019-05-11 jcs return got_error_prefix_errno2("realpath", argv[0]);
3927 ffd1d5e5 2018-06-23 stsp usage_log();
3929 a915003a 2019-02-05 stsp init_curses();
3931 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
3932 c02c541e 2019-03-29 stsp if (error != NULL)
3933 52185f70 2019-02-05 stsp goto done;
3935 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3936 c02c541e 2019-03-29 stsp if (error)
3937 52185f70 2019-02-05 stsp goto done;
3939 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
3940 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3942 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
3943 15a94983 2018-12-23 stsp commit_id_arg);
3944 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3945 ffd1d5e5 2018-06-23 stsp goto done;
3947 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3948 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3949 ffd1d5e5 2018-06-23 stsp goto done;
3951 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
3952 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
3953 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3954 8b473291 2019-02-21 stsp goto done;
3956 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
3957 8b473291 2019-02-21 stsp if (error)
3958 ffd1d5e5 2018-06-23 stsp goto done;
3960 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3961 5221c383 2018-08-01 stsp if (view == NULL) {
3962 230a42bd 2019-05-11 jcs error = got_error_prefix_errno("view_open");
3963 5221c383 2018-08-01 stsp goto done;
3965 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
3966 ad80ab7b 2018-08-04 stsp if (error)
3967 ad80ab7b 2018-08-04 stsp goto done;
3968 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3970 52185f70 2019-02-05 stsp free(repo_path);
3971 ffd1d5e5 2018-06-23 stsp free(commit_id);
3972 ffd1d5e5 2018-06-23 stsp if (commit)
3973 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3975 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3977 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3978 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3979 ffd1d5e5 2018-06-23 stsp return error;
3982 4ed7e80c 2018-05-20 stsp __dead static void
3983 9f7d7167 2018-04-29 stsp usage(void)
3987 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3988 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3989 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3990 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3991 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3996 c2301be8 2018-04-30 stsp static char **
3997 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3999 c2301be8 2018-04-30 stsp char **argv;
4000 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
4002 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
4003 c2301be8 2018-04-30 stsp if (argv == NULL)
4004 c2301be8 2018-04-30 stsp err(1, "calloc");
4005 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
4006 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
4007 c2301be8 2018-04-30 stsp err(1, "calloc");
4008 c2301be8 2018-04-30 stsp if (arg1) {
4009 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
4010 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
4011 c2301be8 2018-04-30 stsp err(1, "calloc");
4014 c2301be8 2018-04-30 stsp return argv;
4018 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
4020 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
4021 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
4022 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
4023 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
4025 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
4027 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
4028 9f7d7167 2018-04-29 stsp switch (ch) {
4030 9f7d7167 2018-04-29 stsp hflag = 1;
4034 9f7d7167 2018-04-29 stsp /* NOTREACHED */
4038 9f7d7167 2018-04-29 stsp argc -= optind;
4039 9f7d7167 2018-04-29 stsp argv += optind;
4040 9f7d7167 2018-04-29 stsp optind = 0;
4041 c2301be8 2018-04-30 stsp optreset = 1;
4043 c2301be8 2018-04-30 stsp if (argc == 0) {
4044 f29d3e89 2018-06-23 stsp if (hflag)
4046 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
4047 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
4048 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
4053 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
4054 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
4055 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
4056 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
4057 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
4058 9f7d7167 2018-04-29 stsp if (hflag)
4059 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
4063 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
4064 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
4065 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
4066 c2301be8 2018-04-30 stsp if (repo_path) {
4067 c2301be8 2018-04-30 stsp struct got_repository *repo;
4068 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
4069 c2301be8 2018-04-30 stsp if (error == NULL)
4070 c2301be8 2018-04-30 stsp got_repo_close(repo);
4072 230a42bd 2019-05-11 jcs error = got_error_prefix_errno2("realpath",
4074 c2301be8 2018-04-30 stsp if (error) {
4075 f29d3e89 2018-06-23 stsp if (hflag) {
4076 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
4077 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
4081 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
4082 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
4083 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
4084 ad7de8d9 2018-04-30 stsp free(repo_path);
4087 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
4088 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
4090 c2301be8 2018-04-30 stsp free(repo_path);
4094 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4095 9f7d7167 2018-04-29 stsp if (error)
4096 9f7d7167 2018-04-29 stsp goto done;
4099 c2301be8 2018-04-30 stsp free(cmd_argv);
4100 9f7d7167 2018-04-29 stsp if (error)
4101 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);