2 9f7d7167 2018-04-29 stsp * Copyright (c) 2018 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>
20 31120ada 2018-04-30 stsp #include <errno.h>
21 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
22 9f7d7167 2018-04-29 stsp #include <curses.h>
23 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
24 9f7d7167 2018-04-29 stsp #include <panel.h>
25 9f7d7167 2018-04-29 stsp #include <locale.h>
26 9f7d7167 2018-04-29 stsp #include <stdlib.h>
27 26ed57b2 2018-05-19 stsp #include <stdio.h>
28 9f7d7167 2018-04-29 stsp #include <getopt.h>
29 9f7d7167 2018-04-29 stsp #include <string.h>
30 9f7d7167 2018-04-29 stsp #include <err.h>
31 80ddbec8 2018-04-29 stsp #include <unistd.h>
32 26ed57b2 2018-05-19 stsp #include <util.h>
33 26ed57b2 2018-05-19 stsp #include <limits.h>
34 61e69b96 2018-05-20 stsp #include <wchar.h>
35 788c352e 2018-06-16 stsp #include <time.h>
36 84451b3e 2018-07-10 stsp #include <pthread.h>
37 5036bf37 2018-09-24 stsp #include <libgen.h>
39 9f7d7167 2018-04-29 stsp #include "got_error.h"
40 80ddbec8 2018-04-29 stsp #include "got_object.h"
41 80ddbec8 2018-04-29 stsp #include "got_reference.h"
42 80ddbec8 2018-04-29 stsp #include "got_repository.h"
43 80ddbec8 2018-04-29 stsp #include "got_diff.h"
44 511a516b 2018-05-19 stsp #include "got_opentemp.h"
45 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
46 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
47 a70480e0 2018-06-23 stsp #include "got_blame.h"
50 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 9f7d7167 2018-04-29 stsp #ifndef nitems
54 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 9f7d7167 2018-04-29 stsp struct tog_cmd {
58 c2301be8 2018-04-30 stsp const char *name;
59 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
60 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
61 c2301be8 2018-04-30 stsp const char *descr;
64 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
65 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
66 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
67 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
68 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
70 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
71 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
72 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
73 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
75 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
76 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
77 9f7d7167 2018-04-29 stsp "show repository history" },
78 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
79 9f7d7167 2018-04-29 stsp "compare files and directories" },
80 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
81 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
82 ffd1d5e5 2018-06-23 stsp { "tree", cmd_tree, usage_tree,
83 ffd1d5e5 2018-06-23 stsp "browse trees in repository" },
86 d6b05b5b 2018-08-04 stsp enum tog_view_type {
87 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
88 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
89 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
90 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
93 ad80ab7b 2018-08-04 stsp struct tog_diff_view_state {
94 a3404814 2018-09-02 stsp struct got_object_id *id1, *id2;
96 ad80ab7b 2018-08-04 stsp int first_displayed_line;
97 ad80ab7b 2018-08-04 stsp int last_displayed_line;
101 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
102 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
103 ba4f502b 2018-08-04 stsp struct got_object_id *id;
104 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
106 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
107 ba4f502b 2018-08-04 stsp struct commit_queue {
108 ba4f502b 2018-08-04 stsp int ncommits;
109 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
112 b01e7d3b 2018-08-04 stsp struct tog_log_view_state {
113 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
114 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
115 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
116 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
117 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
118 b01e7d3b 2018-08-04 stsp int selected;
119 b01e7d3b 2018-08-04 stsp char *in_repo_path;
120 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
121 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
124 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
125 e9424729 2018-08-04 stsp pthread_mutex_t *mutex;
126 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
127 e9424729 2018-08-04 stsp int nlines;
129 e9424729 2018-08-04 stsp struct tog_view *view;
130 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
132 e9424729 2018-08-04 stsp const char *path;
133 e9424729 2018-08-04 stsp int *first_displayed_line;
134 e9424729 2018-08-04 stsp int *last_displayed_line;
135 e9424729 2018-08-04 stsp int *selected_line;
140 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
141 e9424729 2018-08-04 stsp const char *path;
142 e9424729 2018-08-04 stsp struct got_repository *repo;
143 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
144 e9424729 2018-08-04 stsp int *complete;
147 e9424729 2018-08-04 stsp struct tog_blame {
149 e9424729 2018-08-04 stsp size_t filesize;
150 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
151 e9424729 2018-08-04 stsp size_t nlines;
152 e9424729 2018-08-04 stsp pthread_t thread;
153 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
154 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
155 e9424729 2018-08-04 stsp const char *path;
158 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
159 7cbe629d 2018-08-04 stsp int first_displayed_line;
160 7cbe629d 2018-08-04 stsp int last_displayed_line;
161 7cbe629d 2018-08-04 stsp int selected_line;
162 7cbe629d 2018-08-04 stsp int blame_complete;
165 7cbe629d 2018-08-04 stsp pthread_mutex_t mutex;
166 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
167 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
168 e5a0f69f 2018-08-18 stsp char *path;
169 7cbe629d 2018-08-04 stsp struct got_repository *repo;
170 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
171 e9424729 2018-08-04 stsp struct tog_blame blame;
174 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
175 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
176 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
177 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
178 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
179 ad80ab7b 2018-08-04 stsp int selected;
182 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
184 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
185 ad80ab7b 2018-08-04 stsp char *tree_label;
186 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
187 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
188 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
189 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
190 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
191 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
192 ad80ab7b 2018-08-04 stsp int nentries, ndisplayed, selected, show_ids;
193 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
194 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
195 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
199 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
201 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
202 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
203 669b5ffa 2018-10-07 stsp * there is enough screen estate.
205 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
206 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
208 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
209 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
210 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
212 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
213 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
215 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
217 cc3c9aac 2018-08-01 stsp struct tog_view {
218 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
219 26ed57b2 2018-05-19 stsp WINDOW *window;
220 26ed57b2 2018-05-19 stsp PANEL *panel;
221 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
222 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
223 1004088d 2018-09-29 stsp int focussed;
224 669b5ffa 2018-10-07 stsp struct tog_view *parent;
225 669b5ffa 2018-10-07 stsp struct tog_view *child;
226 669b5ffa 2018-10-07 stsp int child_focussed;
228 5dc9f4bc 2018-08-04 stsp /* type-specific state */
229 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
231 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
232 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
233 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
234 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
237 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
238 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
239 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
240 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
243 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
244 ba4f502b 2018-08-04 stsp struct got_object *, struct got_object *, struct got_repository *);
245 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
246 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
247 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
248 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
250 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
251 ba4f502b 2018-08-04 stsp struct got_object_id *, struct got_repository *, const char *);
252 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
253 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
254 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
255 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
257 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
258 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
259 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
260 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
261 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
262 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
264 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
265 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
266 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
267 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
268 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
269 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
271 e5a0f69f 2018-08-18 stsp static const struct got_error *
272 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
274 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
276 669b5ffa 2018-10-07 stsp if (view->child) {
277 669b5ffa 2018-10-07 stsp view_close(view->child);
278 669b5ffa 2018-10-07 stsp view->child = NULL;
280 e5a0f69f 2018-08-18 stsp if (view->close)
281 e5a0f69f 2018-08-18 stsp err = view->close(view);
282 ea5e7bb5 2018-08-01 stsp if (view->panel)
283 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
284 ea5e7bb5 2018-08-01 stsp if (view->window)
285 ea5e7bb5 2018-08-01 stsp delwin(view->window);
286 ea5e7bb5 2018-08-01 stsp free(view);
287 e5a0f69f 2018-08-18 stsp return err;
290 ea5e7bb5 2018-08-01 stsp static struct tog_view *
291 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
292 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
294 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
296 ea5e7bb5 2018-08-01 stsp if (view == NULL)
297 ea5e7bb5 2018-08-01 stsp return NULL;
299 d6b05b5b 2018-08-04 stsp view->type = type;
300 f7d12f7e 2018-08-01 stsp view->lines = LINES;
301 f7d12f7e 2018-08-01 stsp view->cols = COLS;
302 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
303 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
304 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
305 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
306 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
307 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
308 96a765a8 2018-08-04 stsp view_close(view);
309 ea5e7bb5 2018-08-01 stsp return NULL;
311 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
312 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
313 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
314 96a765a8 2018-08-04 stsp view_close(view);
315 ea5e7bb5 2018-08-01 stsp return NULL;
318 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
319 ea5e7bb5 2018-08-01 stsp return view;
323 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
325 0cf4efb1 2018-09-29 stsp if (begin_x > 0)
327 0cf4efb1 2018-09-29 stsp return (COLS >= 120 ? COLS/2 : 0);
330 4d8c2215 2018-08-19 stsp static const struct got_error *
331 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
333 f7d12f7e 2018-08-01 stsp int nlines, ncols;
335 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
336 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
338 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
340 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
341 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
343 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
345 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
346 0cf4efb1 2018-09-29 stsp return got_error_from_errno();
347 0cf4efb1 2018-09-29 stsp replace_panel(view->panel, view->window);
349 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
350 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
351 0cf4efb1 2018-09-29 stsp view->lines = LINES;
352 0cf4efb1 2018-09-29 stsp view->cols = COLS;
354 0cf4efb1 2018-09-29 stsp return NULL;
358 669b5ffa 2018-10-07 stsp view_is_parent_view(struct tog_view *view)
360 669b5ffa 2018-10-07 stsp return view->parent == NULL;
363 669b5ffa 2018-10-07 stsp static const struct got_error *
364 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
366 669b5ffa 2018-10-07 stsp const struct got_error *err;
368 669b5ffa 2018-10-07 stsp if (view->child == NULL)
369 669b5ffa 2018-10-07 stsp return NULL;
371 669b5ffa 2018-10-07 stsp err = view_close(view->child);
372 669b5ffa 2018-10-07 stsp view->child = NULL;
373 669b5ffa 2018-10-07 stsp return err;
376 669b5ffa 2018-10-07 stsp static const struct got_error *
377 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
379 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
381 669b5ffa 2018-10-07 stsp view->child = child;
382 669b5ffa 2018-10-07 stsp child->parent = view;
383 669b5ffa 2018-10-07 stsp return err;
387 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
389 669b5ffa 2018-10-07 stsp return !view_is_parent_view(view) && view->begin_x > 0;
392 0cf4efb1 2018-09-29 stsp static const struct got_error *
393 0cf4efb1 2018-09-29 stsp view_splitscreen(struct tog_view *view)
395 0cf4efb1 2018-09-29 stsp const struct got_error *err = NULL;
397 0cf4efb1 2018-09-29 stsp view->begin_y = 0;
398 0cf4efb1 2018-09-29 stsp view->begin_x = view_split_begin_x(0);
399 0cf4efb1 2018-09-29 stsp view->nlines = LINES;
400 0cf4efb1 2018-09-29 stsp view->ncols = COLS - view->begin_x;
401 0cf4efb1 2018-09-29 stsp view->lines = LINES;
402 0cf4efb1 2018-09-29 stsp view->cols = COLS;
403 0cf4efb1 2018-09-29 stsp err = view_resize(view);
405 0cf4efb1 2018-09-29 stsp return err;
407 0cf4efb1 2018-09-29 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
408 0cf4efb1 2018-09-29 stsp return got_error_from_errno();
410 f7d12f7e 2018-08-01 stsp return NULL;
413 e5a0f69f 2018-08-18 stsp static const struct got_error *
414 0cf4efb1 2018-09-29 stsp view_fullscreen(struct tog_view *view)
416 0cf4efb1 2018-09-29 stsp const struct got_error *err = NULL;
418 0cf4efb1 2018-09-29 stsp view->begin_x = 0;
419 0cf4efb1 2018-09-29 stsp view->begin_y = 0;
420 0cf4efb1 2018-09-29 stsp view->nlines = LINES;
421 0cf4efb1 2018-09-29 stsp view->ncols = COLS;
422 0cf4efb1 2018-09-29 stsp view->lines = LINES;
423 0cf4efb1 2018-09-29 stsp view->cols = COLS;
424 0cf4efb1 2018-09-29 stsp err = view_resize(view);
426 0cf4efb1 2018-09-29 stsp return err;
428 0cf4efb1 2018-09-29 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
429 0cf4efb1 2018-09-29 stsp return got_error_from_errno();
431 0cf4efb1 2018-09-29 stsp return NULL;
434 0cf4efb1 2018-09-29 stsp static const struct got_error *
435 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
436 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
437 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
439 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
440 669b5ffa 2018-10-07 stsp struct tog_view *v;
443 e5a0f69f 2018-08-18 stsp *new = NULL;
444 e5a0f69f 2018-08-18 stsp *dead = NULL;
445 0cf4efb1 2018-09-29 stsp *focus = NULL;
447 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
448 e5a0f69f 2018-08-18 stsp ch = wgetch(view->window);
449 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
450 e5a0f69f 2018-08-18 stsp switch (ch) {
454 669b5ffa 2018-10-07 stsp if (view->child) {
455 669b5ffa 2018-10-07 stsp *focus = view->child;
456 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
457 669b5ffa 2018-10-07 stsp } else if (view->parent) {
458 669b5ffa 2018-10-07 stsp *focus = view->parent;
459 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 0;
463 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
464 e5a0f69f 2018-08-18 stsp *dead = view;
470 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
471 669b5ffa 2018-10-07 stsp if (view->child == NULL)
473 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view->child)) {
474 669b5ffa 2018-10-07 stsp *focus = view->child;
475 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
476 669b5ffa 2018-10-07 stsp err = view_fullscreen(view->child);
478 669b5ffa 2018-10-07 stsp err = view_splitscreen(view->child);
481 669b5ffa 2018-10-07 stsp err = view->child->input(new, dead, focus,
482 669b5ffa 2018-10-07 stsp view->child, KEY_RESIZE);
484 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view)) {
485 669b5ffa 2018-10-07 stsp *focus = view;
486 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 1;
487 669b5ffa 2018-10-07 stsp err = view_fullscreen(view);
489 669b5ffa 2018-10-07 stsp err = view_splitscreen(view);
493 669b5ffa 2018-10-07 stsp err = view->input(new, dead, focus, view,
494 669b5ffa 2018-10-07 stsp KEY_RESIZE);
497 0cf4efb1 2018-09-29 stsp case KEY_RESIZE:
498 0cf4efb1 2018-09-29 stsp TAILQ_FOREACH(v, views, entry) {
499 0cf4efb1 2018-09-29 stsp err = view_resize(v);
501 0cf4efb1 2018-09-29 stsp return err;
502 878940b7 2018-09-29 stsp err = v->input(new, dead, focus, v, ch);
506 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
510 e5a0f69f 2018-08-18 stsp return err;
514 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
516 0cf4efb1 2018-09-29 stsp PANEL *panel;
517 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
519 669b5ffa 2018-10-07 stsp if (view->parent)
520 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
522 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
523 0cf4efb1 2018-09-29 stsp if (panel == NULL)
526 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
527 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
528 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
532 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
534 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
535 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
537 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
539 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
542 669b5ffa 2018-10-07 stsp return view->focussed;
545 bcbd79e2 2018-08-19 stsp static const struct got_error *
546 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
548 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
549 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
550 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
551 e4197bf9 2018-08-18 stsp int done = 0;
553 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
554 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
556 a81bf10d 2018-09-29 stsp main_view = view;
557 1004088d 2018-09-29 stsp view->focussed = 1;
558 878940b7 2018-09-29 stsp err = view->show(view);
560 0cf4efb1 2018-09-29 stsp return err;
561 0cf4efb1 2018-09-29 stsp update_panels();
562 0cf4efb1 2018-09-29 stsp doupdate();
563 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
564 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
565 e4197bf9 2018-08-18 stsp view, &views);
568 e5a0f69f 2018-08-18 stsp if (dead_view) {
569 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
571 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
572 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
573 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
575 669b5ffa 2018-10-07 stsp prev = view->parent;
577 669b5ffa 2018-10-07 stsp if (dead_view->parent)
578 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
580 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
582 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
583 a81bf10d 2018-09-29 stsp if (err || dead_view == main_view)
586 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
587 0cf4efb1 2018-09-29 stsp if (focus_view)
588 0cf4efb1 2018-09-29 stsp view = focus_view;
589 669b5ffa 2018-10-07 stsp else if (prev)
590 669b5ffa 2018-10-07 stsp view = prev;
591 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
592 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
593 0cf4efb1 2018-09-29 stsp tog_view_list_head);
595 0cf4efb1 2018-09-29 stsp view = NULL;
596 669b5ffa 2018-10-07 stsp if (view) {
597 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
598 669b5ffa 2018-10-07 stsp focus_view = view->child;
600 669b5ffa 2018-10-07 stsp focus_view = view;
604 bcbd79e2 2018-08-19 stsp if (new_view) {
605 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
606 878940b7 2018-09-29 stsp if (focus_view == NULL)
607 878940b7 2018-09-29 stsp focus_view = new_view;
609 0cf4efb1 2018-09-29 stsp if (focus_view) {
610 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
612 0cf4efb1 2018-09-29 stsp view->focussed = 0;
613 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
614 0cf4efb1 2018-09-29 stsp view = focus_view;
615 878940b7 2018-09-29 stsp if (new_view)
616 878940b7 2018-09-29 stsp show_panel(new_view->panel);
617 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
618 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
620 669b5ffa 2018-10-07 stsp if (view) {
621 669b5ffa 2018-10-07 stsp if (view->parent) {
622 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
624 669b5ffa 2018-10-07 stsp return err;
626 669b5ffa 2018-10-07 stsp err = view->show(view);
628 878940b7 2018-09-29 stsp return err;
629 669b5ffa 2018-10-07 stsp if (view->child) {
630 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
632 669b5ffa 2018-10-07 stsp return err;
635 0cf4efb1 2018-09-29 stsp update_panels();
636 0cf4efb1 2018-09-29 stsp doupdate();
639 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
640 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
641 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
642 e5a0f69f 2018-08-18 stsp view_close(view);
644 e5a0f69f 2018-08-18 stsp return err;
647 4ed7e80c 2018-05-20 stsp __dead static void
648 9f7d7167 2018-04-29 stsp usage_log(void)
651 c70c5802 2018-08-01 stsp fprintf(stderr,
652 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
653 9f7d7167 2018-04-29 stsp getprogname());
657 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
658 80ddbec8 2018-04-29 stsp static const struct got_error *
659 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
661 00dfcb92 2018-06-11 stsp char *vis = NULL;
662 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
664 963b370f 2018-05-20 stsp *ws = NULL;
665 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
666 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
667 00dfcb92 2018-06-11 stsp int vislen;
668 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
669 00dfcb92 2018-06-11 stsp return got_error_from_errno();
671 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
672 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
674 00dfcb92 2018-06-11 stsp return err;
675 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
676 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
677 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
682 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
683 a7f50699 2018-06-11 stsp if (*ws == NULL) {
684 a7f50699 2018-06-11 stsp err = got_error_from_errno();
688 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
689 963b370f 2018-05-20 stsp err = got_error_from_errno();
694 963b370f 2018-05-20 stsp *ws = NULL;
697 963b370f 2018-05-20 stsp return err;
700 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
701 963b370f 2018-05-20 stsp static const struct got_error *
702 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
704 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
705 963b370f 2018-05-20 stsp int cols = 0;
706 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
707 963b370f 2018-05-20 stsp size_t wlen;
710 963b370f 2018-05-20 stsp *wlinep = NULL;
711 b700b5d6 2018-07-10 stsp *widthp = 0;
713 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
715 963b370f 2018-05-20 stsp return err;
718 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
719 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
720 963b370f 2018-05-20 stsp switch (width) {
726 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
727 b700b5d6 2018-07-10 stsp cols += width;
731 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
732 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
736 963b370f 2018-05-20 stsp err = got_error_from_errno();
740 963b370f 2018-05-20 stsp wline[i] = L'\0';
741 b700b5d6 2018-07-10 stsp if (widthp)
742 b700b5d6 2018-07-10 stsp *widthp = cols;
745 963b370f 2018-05-20 stsp free(wline);
747 963b370f 2018-05-20 stsp *wlinep = wline;
748 963b370f 2018-05-20 stsp return err;
751 963b370f 2018-05-20 stsp static const struct got_error *
752 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
753 2814baeb 2018-08-01 stsp struct got_object_id *id)
755 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
756 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
757 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
758 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
759 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
760 bb737323 2018-05-20 stsp int author_width, logmsg_width;
761 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
762 80ddbec8 2018-04-29 stsp char *line = NULL;
763 bb737323 2018-05-20 stsp int col, limit;
764 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
765 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
766 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
768 c70c5802 2018-08-01 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
769 c70c5802 2018-08-01 stsp &commit->tm_committer) >= sizeof(datebuf))
770 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
772 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
773 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
775 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
776 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
777 b39d25c7 2018-07-10 stsp col = limit + 1;
778 b39d25c7 2018-07-10 stsp if (col > avail)
781 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
782 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
783 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
786 6d9fbc00 2018-04-29 stsp author = author0;
787 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
788 6d9fbc00 2018-04-29 stsp if (smallerthan)
789 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
791 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
793 6d9fbc00 2018-04-29 stsp *at = '\0';
795 b39d25c7 2018-07-10 stsp limit = avail - col;
796 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
799 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
800 bb737323 2018-05-20 stsp col += author_width;
801 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
802 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
804 bb737323 2018-05-20 stsp author_width++;
806 9c2eaf34 2018-05-20 stsp if (col > avail)
809 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
810 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
811 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
814 bb737323 2018-05-20 stsp logmsg = logmsg0;
815 bb737323 2018-05-20 stsp while (*logmsg == '\n')
817 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
818 bb737323 2018-05-20 stsp if (newline)
819 bb737323 2018-05-20 stsp *newline = '\0';
820 bb737323 2018-05-20 stsp limit = avail - col;
821 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
824 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
825 bb737323 2018-05-20 stsp col += logmsg_width;
826 bb737323 2018-05-20 stsp while (col <= avail) {
827 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
831 80ddbec8 2018-04-29 stsp free(logmsg0);
832 bb737323 2018-05-20 stsp free(wlogmsg);
833 6d9fbc00 2018-04-29 stsp free(author0);
834 bb737323 2018-05-20 stsp free(wauthor);
835 80ddbec8 2018-04-29 stsp free(line);
836 80ddbec8 2018-04-29 stsp return err;
839 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
840 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
841 899d86c2 2018-05-10 stsp struct got_object_id *id)
843 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
845 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
846 80ddbec8 2018-04-29 stsp if (entry == NULL)
847 899d86c2 2018-05-10 stsp return NULL;
849 899d86c2 2018-05-10 stsp entry->id = id;
850 99db9666 2018-05-07 stsp entry->commit = commit;
851 899d86c2 2018-05-10 stsp return entry;
854 99db9666 2018-05-07 stsp static void
855 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
857 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
859 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
860 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
861 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
862 ecb28ae0 2018-07-16 stsp commits->ncommits--;
863 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
864 99db9666 2018-05-07 stsp free(entry);
867 99db9666 2018-05-07 stsp static void
868 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
870 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
871 99db9666 2018-05-07 stsp pop_commit(commits);
874 c4972b91 2018-05-07 stsp static const struct got_error *
875 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
876 93e45b7c 2018-09-24 stsp struct got_object_id *start_id, int minqueue,
877 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
879 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
880 93e45b7c 2018-09-24 stsp int nqueued = 0;
882 93e45b7c 2018-09-24 stsp if (start_id) {
883 93e45b7c 2018-09-24 stsp err = got_commit_graph_iter_start(graph, start_id, repo);
885 9ba79e04 2018-06-11 stsp return err;
888 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
889 93e45b7c 2018-09-24 stsp struct got_object_id *id;
890 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
891 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
893 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
895 93e45b7c 2018-09-24 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
896 9ba79e04 2018-06-11 stsp err = NULL;
899 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
901 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
902 93e45b7c 2018-09-24 stsp minqueue, repo);
904 ecb28ae0 2018-07-16 stsp return err;
908 ecb28ae0 2018-07-16 stsp if (id == NULL)
911 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
914 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
915 9ba79e04 2018-06-11 stsp if (entry == NULL) {
916 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
920 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
922 ecb28ae0 2018-07-16 stsp commits->ncommits++;
925 9ba79e04 2018-06-11 stsp return err;
928 99db9666 2018-05-07 stsp static const struct got_error *
929 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
930 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
931 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph, struct got_repository *repo,
932 ecb28ae0 2018-07-16 stsp const char *path)
934 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
936 9ba79e04 2018-06-11 stsp *pentry = NULL;
938 93e45b7c 2018-09-24 stsp err = queue_commits(graph, commits, NULL, 1, repo, path);
940 9ba79e04 2018-06-11 stsp return err;
942 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
943 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
944 9ba79e04 2018-06-11 stsp return NULL;
947 899d86c2 2018-05-10 stsp static const struct got_error *
948 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
950 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
951 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
953 9ba79e04 2018-06-11 stsp *head_id = NULL;
955 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
957 99db9666 2018-05-07 stsp return err;
959 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
960 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
962 9ba79e04 2018-06-11 stsp *head_id = NULL;
963 99db9666 2018-05-07 stsp return err;
966 9ba79e04 2018-06-11 stsp return NULL;
969 0553a4e3 2018-04-30 stsp static const struct got_error *
970 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
971 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
972 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
973 a7f40148 2018-07-18 stsp struct got_commit_graph *graph, struct got_repository *repo,
974 a7f40148 2018-07-18 stsp const char *path)
976 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
977 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
978 ecb28ae0 2018-07-16 stsp int ncommits, width;
979 867c6645 2018-07-10 stsp char *id_str, *header;
980 ecb28ae0 2018-07-16 stsp wchar_t *wline;
982 e0d42f60 2018-07-22 stsp entry = first;
983 e0d42f60 2018-07-22 stsp ncommits = 0;
984 e0d42f60 2018-07-22 stsp while (entry) {
985 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
986 e0d42f60 2018-07-22 stsp *selected = entry;
989 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
990 e0d42f60 2018-07-22 stsp ncommits++;
993 867c6645 2018-07-10 stsp err = got_object_id_str(&id_str, (*selected)->id);
995 867c6645 2018-07-10 stsp return err;
997 c3ba6f36 2018-08-01 stsp if (path && strcmp(path, "/") != 0) {
998 ecb28ae0 2018-07-16 stsp if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
999 ecb28ae0 2018-07-16 stsp err = got_error_from_errno();
1000 ecb28ae0 2018-07-16 stsp free(id_str);
1001 ecb28ae0 2018-07-16 stsp return err;
1003 ecb28ae0 2018-07-16 stsp } else if (asprintf(&header, "commit: %s", id_str) == -1) {
1004 867c6645 2018-07-10 stsp err = got_error_from_errno();
1005 867c6645 2018-07-10 stsp free(id_str);
1006 867c6645 2018-07-10 stsp return err;
1008 ecb28ae0 2018-07-16 stsp free(id_str);
1009 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, header, view->ncols);
1010 ecb28ae0 2018-07-16 stsp if (err) {
1011 ecb28ae0 2018-07-16 stsp free(header);
1012 ecb28ae0 2018-07-16 stsp return err;
1014 ecb28ae0 2018-07-16 stsp free(header);
1016 2814baeb 2018-08-01 stsp werase(view->window);
1018 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1019 a3404814 2018-09-02 stsp wstandout(view->window);
1020 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1021 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1022 a3404814 2018-09-02 stsp wstandend(view->window);
1023 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1024 2814baeb 2018-08-01 stsp waddch(view->window, '\n');
1025 ecb28ae0 2018-07-16 stsp free(wline);
1026 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1027 ecb28ae0 2018-07-16 stsp return NULL;
1029 899d86c2 2018-05-10 stsp entry = first;
1030 899d86c2 2018-05-10 stsp *last = first;
1031 867c6645 2018-07-10 stsp ncommits = 0;
1032 899d86c2 2018-05-10 stsp while (entry) {
1033 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1035 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1036 2814baeb 2018-08-01 stsp wstandout(view->window);
1037 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
1038 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1039 2814baeb 2018-08-01 stsp wstandend(view->window);
1042 0553a4e3 2018-04-30 stsp ncommits++;
1043 899d86c2 2018-05-10 stsp *last = entry;
1044 a7f40148 2018-07-18 stsp if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
1045 93e45b7c 2018-09-24 stsp err = queue_commits(graph, commits, NULL, 1,
1046 93e45b7c 2018-09-24 stsp repo, path);
1047 a7f40148 2018-07-18 stsp if (err) {
1048 a7f40148 2018-07-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1049 a7f40148 2018-07-18 stsp return err;
1050 a7f40148 2018-07-18 stsp err = NULL;
1053 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1056 1a57306a 2018-09-02 stsp view_vborder(view);
1058 80ddbec8 2018-04-29 stsp return err;
1061 07b55e75 2018-05-10 stsp static void
1062 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1063 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1065 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1066 07b55e75 2018-05-10 stsp int nscrolled = 0;
1068 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1069 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
1072 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1073 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1074 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1075 07b55e75 2018-05-10 stsp if (entry) {
1076 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1077 07b55e75 2018-05-10 stsp nscrolled++;
1082 aa075928 2018-05-10 stsp static const struct got_error *
1083 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1084 93e45b7c 2018-09-24 stsp struct commit_queue_entry **last_displayed_entry,
1085 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
1086 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
1088 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
1089 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
1090 aa075928 2018-05-10 stsp int nscrolled = 0;
1093 93e45b7c 2018-09-24 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1094 aa075928 2018-05-10 stsp if (pentry == NULL) {
1095 93e45b7c 2018-09-24 stsp err = fetch_next_commit(&pentry, *last_displayed_entry,
1096 ecb28ae0 2018-07-16 stsp commits, graph, repo, path);
1097 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
1100 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1102 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1103 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1105 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1106 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1108 dd0a52c1 2018-05-20 stsp return err;
1111 cd0acaa7 2018-05-20 stsp static const struct got_error *
1112 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1113 0cf4efb1 2018-09-29 stsp struct got_object_id *commit_id, struct got_commit_object *commit,
1114 2a4718d3 2018-09-29 stsp struct got_repository *repo)
1116 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1117 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1118 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1119 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1121 2a4718d3 2018-09-29 stsp err = got_object_open(&obj2, repo, commit_id);
1123 cd0acaa7 2018-05-20 stsp return err;
1125 2a4718d3 2018-09-29 stsp parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1126 9ba79e04 2018-06-11 stsp if (parent_id) {
1127 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
1129 cd0acaa7 2018-05-20 stsp goto done;
1132 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1133 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
1134 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1135 ea5e7bb5 2018-08-01 stsp goto done;
1138 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, obj1, obj2, repo);
1139 e5a0f69f 2018-08-18 stsp if (err == NULL)
1140 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1143 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
1145 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
1146 cd0acaa7 2018-05-20 stsp return err;
1149 80ddbec8 2018-04-29 stsp static const struct got_error *
1150 0cf4efb1 2018-09-29 stsp browse_commit(struct tog_view **new_view, int begin_x,
1151 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1153 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1154 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1155 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1157 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1159 9343a5fb 2018-06-23 stsp return err;
1161 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1162 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1163 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1165 e5a0f69f 2018-08-18 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1167 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1169 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1170 9343a5fb 2018-06-23 stsp return err;
1173 9343a5fb 2018-06-23 stsp static const struct got_error *
1174 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1175 04cc582a 2018-08-01 stsp struct got_repository *repo, const char *path)
1177 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1178 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1180 fb2756b9 2018-08-04 stsp err = got_repo_map_path(&s->in_repo_path, repo, path);
1181 ecb28ae0 2018-07-16 stsp if (err != NULL)
1182 ecb28ae0 2018-07-16 stsp goto done;
1184 93e45b7c 2018-09-24 stsp err = got_commit_graph_open(&s->graph, start_id, s->in_repo_path,
1187 9ba79e04 2018-06-11 stsp goto done;
1188 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1189 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1190 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1193 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
1194 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
1195 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
1196 9ba79e04 2018-06-11 stsp * updating the display.
1198 93e45b7c 2018-09-24 stsp err = queue_commits(s->graph, &s->commits, start_id, view->nlines,
1199 fb2756b9 2018-08-04 stsp repo, s->in_repo_path);
1200 55198a88 2018-07-16 stsp if (err) {
1201 55198a88 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1202 55198a88 2018-07-16 stsp goto done;
1203 55198a88 2018-07-16 stsp err = NULL;
1206 93e45b7c 2018-09-24 stsp s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
1207 1bfa490b 2018-08-18 stsp s->selected_entry = s->first_displayed_entry;
1208 fb2756b9 2018-08-04 stsp s->repo = repo;
1209 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1210 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1211 5036bf37 2018-09-24 stsp err = got_error_from_errno();
1212 5036bf37 2018-09-24 stsp goto done;
1215 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1216 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1217 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1219 ba4f502b 2018-08-04 stsp return err;
1222 e5a0f69f 2018-08-18 stsp static const struct got_error *
1223 e5a0f69f 2018-08-18 stsp close_log_view(struct tog_view *view)
1225 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1227 fb2756b9 2018-08-04 stsp if (s->graph)
1228 fb2756b9 2018-08-04 stsp got_commit_graph_close(s->graph);
1229 fb2756b9 2018-08-04 stsp free_commits(&s->commits);
1230 fb2756b9 2018-08-04 stsp free(s->in_repo_path);
1231 5036bf37 2018-09-24 stsp free(s->start_id);
1232 e5a0f69f 2018-08-18 stsp return NULL;
1235 ba4f502b 2018-08-04 stsp static const struct got_error *
1236 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1238 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1239 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1241 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1242 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1243 e5a0f69f 2018-08-18 stsp &s->commits, s->selected, view->nlines, s->graph,
1244 e5a0f69f 2018-08-18 stsp s->repo, s->in_repo_path);
1246 bcbd79e2 2018-08-19 stsp return err;
1249 e5a0f69f 2018-08-18 stsp static const struct got_error *
1250 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1251 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1253 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1254 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1255 5036bf37 2018-09-24 stsp char *parent_path;
1256 669b5ffa 2018-10-07 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
1257 669b5ffa 2018-10-07 stsp int begin_x = 0;
1259 e5a0f69f 2018-08-18 stsp switch (ch) {
1261 e5a0f69f 2018-08-18 stsp case KEY_UP:
1263 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1264 e5a0f69f 2018-08-18 stsp s->selected--;
1265 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1267 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1268 e5a0f69f 2018-08-18 stsp &s->commits);
1270 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1271 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1272 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1273 e5a0f69f 2018-08-18 stsp s->selected = 0;
1276 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1277 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1280 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1282 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1283 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1284 e5a0f69f 2018-08-18 stsp s->selected++;
1287 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1288 93e45b7c 2018-09-24 stsp &s->last_displayed_entry, &s->commits,
1289 e5a0f69f 2018-08-18 stsp s->graph, s->repo, s->in_repo_path);
1290 e5a0f69f 2018-08-18 stsp if (err) {
1291 4d7951a5 2018-08-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1293 4d7951a5 2018-08-04 stsp err = NULL;
1296 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1297 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1298 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1299 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1300 93e45b7c 2018-09-24 stsp view->nlines, &s->last_displayed_entry,
1301 e5a0f69f 2018-08-18 stsp &s->commits, s->graph, s->repo,
1302 e5a0f69f 2018-08-18 stsp s->in_repo_path);
1303 93e45b7c 2018-09-24 stsp if (err && err->code != GOT_ERR_ITER_COMPLETED)
1305 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1306 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1307 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1308 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1309 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1310 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1312 e5a0f69f 2018-08-18 stsp err = NULL;
1315 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1316 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1317 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1318 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1319 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1321 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1322 e5a0f69f 2018-08-18 stsp case '\r':
1323 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1324 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1325 669b5ffa 2018-10-07 stsp err = open_diff_view_for_commit(&diff_view, begin_x,
1326 2a4718d3 2018-09-29 stsp s->selected_entry->id, s->selected_entry->commit,
1330 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1331 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1333 669b5ffa 2018-10-07 stsp return err;
1334 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
1335 669b5ffa 2018-10-07 stsp if (err) {
1336 669b5ffa 2018-10-07 stsp view_close(diff_view);
1339 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(diff_view)) {
1340 669b5ffa 2018-10-07 stsp *focus_view = diff_view;
1341 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1344 669b5ffa 2018-10-07 stsp *new_view = diff_view;
1347 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1348 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1349 669b5ffa 2018-10-07 stsp err = browse_commit(&tree_view, begin_x,
1350 0cf4efb1 2018-09-29 stsp s->selected_entry, s->repo);
1351 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1352 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1354 669b5ffa 2018-10-07 stsp return err;
1355 669b5ffa 2018-10-07 stsp err = view_set_child(view, tree_view);
1356 669b5ffa 2018-10-07 stsp if (err) {
1357 669b5ffa 2018-10-07 stsp view_close(tree_view);
1360 669b5ffa 2018-10-07 stsp *focus_view = tree_view;
1361 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1363 669b5ffa 2018-10-07 stsp *new_view = tree_view;
1365 5036bf37 2018-09-24 stsp case KEY_BACKSPACE:
1366 5036bf37 2018-09-24 stsp if (strcmp(s->in_repo_path, "/") == 0)
1368 5036bf37 2018-09-24 stsp parent_path = dirname(s->in_repo_path);
1369 5036bf37 2018-09-24 stsp if (parent_path && strcmp(parent_path, ".") != 0) {
1370 5036bf37 2018-09-24 stsp struct tog_view *lv;
1371 0cf4efb1 2018-09-29 stsp lv = view_open(view->nlines, view->ncols,
1372 0cf4efb1 2018-09-29 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
1373 5036bf37 2018-09-24 stsp if (lv == NULL)
1374 5036bf37 2018-09-24 stsp return got_error_from_errno();
1375 5036bf37 2018-09-24 stsp err = open_log_view(lv, s->start_id, s->repo,
1376 5036bf37 2018-09-24 stsp parent_path);
1379 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1380 669b5ffa 2018-10-07 stsp *new_view = lv;
1382 669b5ffa 2018-10-07 stsp view_set_child(view->parent, lv);
1383 669b5ffa 2018-10-07 stsp *dead_view = view;
1391 80ddbec8 2018-04-29 stsp return err;
1394 4ed7e80c 2018-05-20 stsp static const struct got_error *
1395 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1397 80ddbec8 2018-04-29 stsp const struct got_error *error;
1398 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1399 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1400 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1401 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1403 04cc582a 2018-08-01 stsp struct tog_view *view;
1405 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1406 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1408 80ddbec8 2018-04-29 stsp err(1, "pledge");
1411 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1412 80ddbec8 2018-04-29 stsp switch (ch) {
1414 80ddbec8 2018-04-29 stsp start_commit = optarg;
1417 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1418 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1419 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1423 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1427 80ddbec8 2018-04-29 stsp argc -= optind;
1428 80ddbec8 2018-04-29 stsp argv += optind;
1430 ecb28ae0 2018-07-16 stsp if (argc == 0)
1431 ecb28ae0 2018-07-16 stsp path = strdup("");
1432 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1433 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1435 80ddbec8 2018-04-29 stsp usage_log();
1436 ecb28ae0 2018-07-16 stsp if (path == NULL)
1437 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1439 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1440 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1441 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1442 ecb28ae0 2018-07-16 stsp goto done;
1444 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1445 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1446 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1447 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1448 ecb28ae0 2018-07-16 stsp goto done;
1452 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1453 80ddbec8 2018-04-29 stsp if (error != NULL)
1454 ecb28ae0 2018-07-16 stsp goto done;
1456 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1457 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1458 80ddbec8 2018-04-29 stsp if (error != NULL)
1459 ecb28ae0 2018-07-16 stsp goto done;
1461 80ddbec8 2018-04-29 stsp struct got_object *obj;
1462 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1463 80ddbec8 2018-04-29 stsp if (error == NULL) {
1464 6402fb3c 2018-09-15 stsp start_id = got_object_id_dup(got_object_get_id(obj));
1465 899d86c2 2018-05-10 stsp if (start_id == NULL)
1466 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1467 ecb28ae0 2018-07-16 stsp goto done;
1470 80ddbec8 2018-04-29 stsp if (error != NULL)
1471 ecb28ae0 2018-07-16 stsp goto done;
1473 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1474 04cc582a 2018-08-01 stsp if (view == NULL) {
1475 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1476 04cc582a 2018-08-01 stsp goto done;
1478 ba4f502b 2018-08-04 stsp error = open_log_view(view, start_id, repo, path);
1479 ba4f502b 2018-08-04 stsp if (error)
1480 ba4f502b 2018-08-04 stsp goto done;
1481 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1483 ecb28ae0 2018-07-16 stsp free(repo_path);
1484 ecb28ae0 2018-07-16 stsp free(cwd);
1485 ecb28ae0 2018-07-16 stsp free(path);
1486 899d86c2 2018-05-10 stsp free(start_id);
1488 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1489 80ddbec8 2018-04-29 stsp return error;
1492 4ed7e80c 2018-05-20 stsp __dead static void
1493 9f7d7167 2018-04-29 stsp usage_diff(void)
1496 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1497 9f7d7167 2018-04-29 stsp getprogname());
1501 b304db33 2018-05-20 stsp static char *
1502 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1504 b304db33 2018-05-20 stsp char *line;
1505 b304db33 2018-05-20 stsp size_t linelen;
1506 b304db33 2018-05-20 stsp size_t lineno;
1507 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1509 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1511 b304db33 2018-05-20 stsp *len = linelen;
1512 b304db33 2018-05-20 stsp return line;
1515 4ed7e80c 2018-05-20 stsp static const struct got_error *
1516 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1517 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1518 a3404814 2018-09-02 stsp char * header)
1520 61e69b96 2018-05-20 stsp const struct got_error *err;
1521 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1522 b304db33 2018-05-20 stsp char *line;
1523 b304db33 2018-05-20 stsp size_t len;
1524 61e69b96 2018-05-20 stsp wchar_t *wline;
1525 e0b650dd 2018-05-20 stsp int width;
1527 26ed57b2 2018-05-19 stsp rewind(f);
1528 f7d12f7e 2018-08-01 stsp werase(view->window);
1530 a3404814 2018-09-02 stsp if (header) {
1531 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1532 a3404814 2018-09-02 stsp if (err) {
1533 a3404814 2018-09-02 stsp return err;
1536 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1537 a3404814 2018-09-02 stsp wstandout(view->window);
1538 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1539 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1540 a3404814 2018-09-02 stsp wstandend(view->window);
1541 a3404814 2018-09-02 stsp if (width < view->ncols)
1542 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1544 a3404814 2018-09-02 stsp if (max_lines <= 1)
1545 a3404814 2018-09-02 stsp return NULL;
1546 a3404814 2018-09-02 stsp max_lines--;
1550 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1551 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1552 26ed57b2 2018-05-19 stsp if (line == NULL) {
1556 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1557 26ed57b2 2018-05-19 stsp free(line);
1561 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1562 61e69b96 2018-05-20 stsp if (err) {
1563 61e69b96 2018-05-20 stsp free(line);
1564 61e69b96 2018-05-20 stsp return err;
1566 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1567 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1568 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1569 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1570 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1571 26ed57b2 2018-05-19 stsp free(line);
1572 2550e4c3 2018-07-13 stsp free(wline);
1573 2550e4c3 2018-07-13 stsp wline = NULL;
1575 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1577 1a57306a 2018-09-02 stsp view_vborder(view);
1579 26ed57b2 2018-05-19 stsp return NULL;
1582 4ed7e80c 2018-05-20 stsp static const struct got_error *
1583 5dc9f4bc 2018-08-04 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1584 ea5e7bb5 2018-08-01 stsp struct got_object *obj2, struct got_repository *repo)
1586 26ed57b2 2018-05-19 stsp const struct got_error *err;
1589 cd0acaa7 2018-05-20 stsp if (obj1 != NULL && obj2 != NULL &&
1590 cd0acaa7 2018-05-20 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1591 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1593 511a516b 2018-05-19 stsp f = got_opentemp();
1594 26ed57b2 2018-05-19 stsp if (f == NULL)
1595 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1597 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1598 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1599 f6861a81 2018-09-13 stsp err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1602 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1603 f6861a81 2018-09-13 stsp err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
1605 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1606 11528a82 2018-05-19 stsp err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1609 26ed57b2 2018-05-19 stsp return got_error(GOT_ERR_OBJ_TYPE);
1612 26ed57b2 2018-05-19 stsp fflush(f);
1614 a3404814 2018-09-02 stsp view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1615 a3404814 2018-09-02 stsp view->state.diff.id2 = got_object_get_id(obj2);
1616 5dc9f4bc 2018-08-04 stsp view->state.diff.f = f;
1617 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1618 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1620 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
1621 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
1622 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
1624 5dc9f4bc 2018-08-04 stsp return NULL;
1627 e5a0f69f 2018-08-18 stsp static const struct got_error *
1628 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1630 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1632 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1633 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1634 e5a0f69f 2018-08-18 stsp return err;
1637 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1638 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1640 a3404814 2018-09-02 stsp const struct got_error *err;
1641 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1642 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
1644 a3404814 2018-09-02 stsp if (s->id1) {
1645 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
1647 a3404814 2018-09-02 stsp return err;
1649 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
1651 a3404814 2018-09-02 stsp return err;
1653 a3404814 2018-09-02 stsp if (asprintf(&header, "diff: %s %s",
1654 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1655 a3404814 2018-09-02 stsp err = got_error_from_errno();
1656 a3404814 2018-09-02 stsp free(id_str1);
1657 a3404814 2018-09-02 stsp free(id_str2);
1658 a3404814 2018-09-02 stsp return err;
1660 a3404814 2018-09-02 stsp free(id_str1);
1661 a3404814 2018-09-02 stsp free(id_str2);
1663 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
1664 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
1668 0cf4efb1 2018-09-29 stsp static const struct got_error *
1669 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1670 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1672 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1673 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
1676 e5a0f69f 2018-08-18 stsp switch (ch) {
1678 e5a0f69f 2018-08-18 stsp case KEY_UP:
1679 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
1680 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1682 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1684 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
1685 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
1686 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1689 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1690 e5a0f69f 2018-08-18 stsp if (!s->eof)
1691 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1693 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
1696 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
1697 e5a0f69f 2018-08-18 stsp char *line;
1698 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
1699 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1700 e5a0f69f 2018-08-18 stsp if (line == NULL)
1708 bcbd79e2 2018-08-19 stsp return err;
1711 4ed7e80c 2018-05-20 stsp static const struct got_error *
1712 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1714 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1715 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1716 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1717 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1718 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1720 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1722 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1723 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1725 26ed57b2 2018-05-19 stsp err(1, "pledge");
1728 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1729 26ed57b2 2018-05-19 stsp switch (ch) {
1732 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1736 26ed57b2 2018-05-19 stsp argc -= optind;
1737 26ed57b2 2018-05-19 stsp argv += optind;
1739 26ed57b2 2018-05-19 stsp if (argc == 0) {
1740 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1741 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1742 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1743 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1744 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1745 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1746 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1747 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1748 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1749 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1750 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1751 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1752 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1754 26ed57b2 2018-05-19 stsp usage_diff();
1756 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1757 26ed57b2 2018-05-19 stsp free(repo_path);
1758 26ed57b2 2018-05-19 stsp if (error)
1759 26ed57b2 2018-05-19 stsp goto done;
1761 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1762 26ed57b2 2018-05-19 stsp if (error)
1763 26ed57b2 2018-05-19 stsp goto done;
1765 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1766 26ed57b2 2018-05-19 stsp if (error)
1767 26ed57b2 2018-05-19 stsp goto done;
1769 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
1770 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1771 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1772 ea5e7bb5 2018-08-01 stsp goto done;
1774 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
1775 5dc9f4bc 2018-08-04 stsp if (error)
1776 5dc9f4bc 2018-08-04 stsp goto done;
1777 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1779 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1781 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1783 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1784 26ed57b2 2018-05-19 stsp return error;
1787 4ed7e80c 2018-05-20 stsp __dead static void
1788 9f7d7167 2018-04-29 stsp usage_blame(void)
1791 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1792 9f7d7167 2018-04-29 stsp getprogname());
1796 84451b3e 2018-07-10 stsp struct tog_blame_line {
1797 84451b3e 2018-07-10 stsp int annotated;
1798 84451b3e 2018-07-10 stsp struct got_object_id *id;
1801 4ed7e80c 2018-05-20 stsp static const struct got_error *
1802 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1803 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1804 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1805 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1807 84451b3e 2018-07-10 stsp const struct got_error *err;
1808 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1809 84451b3e 2018-07-10 stsp char *line;
1810 84451b3e 2018-07-10 stsp size_t len;
1811 84451b3e 2018-07-10 stsp wchar_t *wline;
1812 b700b5d6 2018-07-10 stsp int width, wlimit;
1813 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1814 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1815 ab089a2a 2018-07-12 stsp char *id_str;
1817 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1819 ab089a2a 2018-07-12 stsp return err;
1821 84451b3e 2018-07-10 stsp rewind(f);
1822 f7d12f7e 2018-08-01 stsp werase(view->window);
1824 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1825 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1826 ab089a2a 2018-07-12 stsp free(id_str);
1827 ab089a2a 2018-07-12 stsp return err;
1830 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1831 ab089a2a 2018-07-12 stsp free(line);
1832 2550e4c3 2018-07-13 stsp line = NULL;
1833 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1834 a3404814 2018-09-02 stsp wstandout(view->window);
1835 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1836 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1837 a3404814 2018-09-02 stsp wstandend(view->window);
1838 2550e4c3 2018-07-13 stsp free(wline);
1839 2550e4c3 2018-07-13 stsp wline = NULL;
1840 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1841 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1843 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1844 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1845 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1846 ab089a2a 2018-07-12 stsp free(id_str);
1847 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1849 ab089a2a 2018-07-12 stsp free(id_str);
1850 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1851 3f60a8ef 2018-07-10 stsp free(line);
1852 2550e4c3 2018-07-13 stsp line = NULL;
1854 3f60a8ef 2018-07-10 stsp return err;
1855 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1856 2550e4c3 2018-07-13 stsp free(wline);
1857 2550e4c3 2018-07-13 stsp wline = NULL;
1858 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1859 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1862 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1863 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1864 84451b3e 2018-07-10 stsp if (line == NULL) {
1868 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1869 84451b3e 2018-07-10 stsp free(line);
1873 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1874 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1875 84451b3e 2018-07-10 stsp if (err) {
1876 84451b3e 2018-07-10 stsp free(line);
1877 84451b3e 2018-07-10 stsp return err;
1880 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
1881 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1883 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1884 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1885 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1886 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1887 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1888 84451b3e 2018-07-10 stsp char *id_str;
1889 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1890 84451b3e 2018-07-10 stsp if (err) {
1891 84451b3e 2018-07-10 stsp free(line);
1892 2550e4c3 2018-07-13 stsp free(wline);
1893 84451b3e 2018-07-10 stsp return err;
1895 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1896 84451b3e 2018-07-10 stsp free(id_str);
1897 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1899 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
1900 ee41ec32 2018-07-10 stsp prev_id = NULL;
1903 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1904 b700b5d6 2018-07-10 stsp while (width < wlimit) {
1905 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
1908 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
1909 f7d12f7e 2018-08-01 stsp wstandend(view->window);
1910 84451b3e 2018-07-10 stsp if (++nprinted == 1)
1911 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
1912 84451b3e 2018-07-10 stsp free(line);
1913 2550e4c3 2018-07-13 stsp free(wline);
1914 2550e4c3 2018-07-13 stsp wline = NULL;
1916 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
1918 1a57306a 2018-09-02 stsp view_vborder(view);
1920 84451b3e 2018-07-10 stsp return NULL;
1923 84451b3e 2018-07-10 stsp static const struct got_error *
1924 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1926 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
1927 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
1928 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
1930 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
1931 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
1932 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
1934 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1935 84451b3e 2018-07-10 stsp return got_error_from_errno();
1937 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
1938 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
1939 d68a0a7d 2018-07-10 stsp goto done;
1942 d68a0a7d 2018-07-10 stsp if (lineno == -1)
1943 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
1945 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
1946 d68a0a7d 2018-07-10 stsp if (line->annotated)
1947 d68a0a7d 2018-07-10 stsp goto done;
1949 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
1950 84451b3e 2018-07-10 stsp if (line->id == NULL) {
1951 84451b3e 2018-07-10 stsp err = got_error_from_errno();
1952 84451b3e 2018-07-10 stsp goto done;
1954 84451b3e 2018-07-10 stsp line->annotated = 1;
1956 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1957 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1958 e5a0f69f 2018-08-18 stsp a->last_displayed_line, a->eof, a->view->nlines);
1960 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
1961 84451b3e 2018-07-10 stsp return got_error_from_errno();
1962 84451b3e 2018-07-10 stsp return err;
1965 84451b3e 2018-07-10 stsp static void *
1966 84451b3e 2018-07-10 stsp blame_thread(void *arg)
1968 18430de3 2018-07-10 stsp const struct got_error *err;
1969 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
1970 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
1972 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1973 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
1975 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
1976 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
1978 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
1979 c9beca56 2018-07-22 stsp ta->repo = NULL;
1980 c9beca56 2018-07-22 stsp *ta->complete = 1;
1982 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
1983 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
1984 e5a0f69f 2018-08-18 stsp a->first_displayed_line, a->last_displayed_line, a->eof,
1985 f7d12f7e 2018-08-01 stsp a->view->nlines);
1987 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1988 18430de3 2018-07-10 stsp err = got_error_from_errno();
1990 18430de3 2018-07-10 stsp return (void *)err;
1993 245d91c1 2018-07-12 stsp static struct got_object_id *
1994 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
1995 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
1997 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
1999 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2000 245d91c1 2018-07-12 stsp if (!line->annotated)
2001 245d91c1 2018-07-12 stsp return NULL;
2003 245d91c1 2018-07-12 stsp return line->id;
2006 84451b3e 2018-07-10 stsp static const struct got_error *
2007 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
2008 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
2009 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
2011 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
2012 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
2013 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
2014 b880a918 2018-07-10 stsp struct got_object_qid *pid;
2016 b880a918 2018-07-10 stsp *pobj = NULL;
2017 b880a918 2018-07-10 stsp *obj = NULL;
2019 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
2020 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
2021 245d91c1 2018-07-12 stsp if (selected_id == NULL)
2022 b880a918 2018-07-10 stsp return NULL;
2024 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
2026 b880a918 2018-07-10 stsp goto done;
2028 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
2030 b880a918 2018-07-10 stsp goto done;
2032 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
2033 b880a918 2018-07-10 stsp if (pid) {
2034 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
2036 b880a918 2018-07-10 stsp goto done;
2039 b880a918 2018-07-10 stsp if (commit)
2040 b880a918 2018-07-10 stsp got_object_commit_close(commit);
2041 b880a918 2018-07-10 stsp return err;
2044 b880a918 2018-07-10 stsp static const struct got_error *
2045 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2047 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2050 245d91c1 2018-07-12 stsp if (blame->thread) {
2051 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
2052 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2053 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2054 245d91c1 2018-07-12 stsp err = NULL;
2055 245d91c1 2018-07-12 stsp blame->thread = NULL;
2057 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2058 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2059 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2061 245d91c1 2018-07-12 stsp if (blame->f) {
2062 245d91c1 2018-07-12 stsp fclose(blame->f);
2063 245d91c1 2018-07-12 stsp blame->f = NULL;
2065 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
2066 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
2067 245d91c1 2018-07-12 stsp free(blame->lines);
2068 245d91c1 2018-07-12 stsp blame->lines = NULL;
2069 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2070 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2072 245d91c1 2018-07-12 stsp return err;
2075 245d91c1 2018-07-12 stsp static const struct got_error *
2076 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2077 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
2078 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
2079 e5a0f69f 2018-08-18 stsp int *selected_line, int *done, int *eof, const char *path,
2080 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
2081 245d91c1 2018-07-12 stsp struct got_repository *repo)
2083 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2084 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2085 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2086 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2087 27d434c2 2018-09-15 stsp struct got_object *obj = NULL;
2089 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2091 27d434c2 2018-09-15 stsp goto done;
2093 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
2095 84451b3e 2018-07-10 stsp goto done;
2097 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2098 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2099 84451b3e 2018-07-10 stsp goto done;
2102 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
2104 a70480e0 2018-06-23 stsp goto done;
2105 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2106 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2107 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2108 84451b3e 2018-07-10 stsp goto done;
2110 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2111 245d91c1 2018-07-12 stsp blame->f, blob);
2113 84451b3e 2018-07-10 stsp goto done;
2115 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2116 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2117 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2118 84451b3e 2018-07-10 stsp goto done;
2121 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2123 bd24772e 2018-07-11 stsp goto done;
2125 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2126 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2127 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2128 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
2129 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2130 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2131 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2132 245d91c1 2018-07-12 stsp goto done;
2134 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
2135 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
2136 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
2137 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
2138 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
2139 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2140 e5a0f69f 2018-08-18 stsp blame->cb_args.eof = eof;
2142 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2143 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2144 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2145 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2146 245d91c1 2018-07-12 stsp *blame_complete = 0;
2148 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
2149 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
2150 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2151 245d91c1 2018-07-12 stsp goto done;
2156 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2157 27d434c2 2018-09-15 stsp free(obj_id);
2159 245d91c1 2018-07-12 stsp got_object_close(obj);
2161 245d91c1 2018-07-12 stsp stop_blame(blame);
2162 245d91c1 2018-07-12 stsp return err;
2165 245d91c1 2018-07-12 stsp static const struct got_error *
2166 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2167 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2169 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2170 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2172 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2174 fb2756b9 2018-08-04 stsp if (pthread_mutex_init(&s->mutex, NULL) != 0)
2175 7cbe629d 2018-08-04 stsp return got_error_from_errno();
2177 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2179 7cbe629d 2018-08-04 stsp return err;
2181 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2182 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2183 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2184 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2185 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2186 fb2756b9 2018-08-04 stsp s->path = path;
2187 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2188 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2189 fb2756b9 2018-08-04 stsp s->repo = repo;
2190 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2191 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2193 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2194 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2195 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2197 e5a0f69f 2018-08-18 stsp return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2198 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2199 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2200 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2203 e5a0f69f 2018-08-18 stsp static const struct got_error *
2204 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2206 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2207 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2209 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2210 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2212 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2213 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2214 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2215 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2216 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2219 e5a0f69f 2018-08-18 stsp free(s->path);
2221 e5a0f69f 2018-08-18 stsp return err;
2224 7cbe629d 2018-08-04 stsp static const struct got_error *
2225 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2227 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2228 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2230 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0)
2231 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2233 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2234 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2235 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2236 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2238 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2239 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2241 669b5ffa 2018-10-07 stsp view_vborder(view);
2242 e5a0f69f 2018-08-18 stsp return err;
2245 e5a0f69f 2018-08-18 stsp static const struct got_error *
2246 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2247 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2249 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2250 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
2251 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2252 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2253 669b5ffa 2018-10-07 stsp int begin_x = 0;
2255 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2256 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2257 e5a0f69f 2018-08-18 stsp goto done;
2260 e5a0f69f 2018-08-18 stsp switch (ch) {
2262 e5a0f69f 2018-08-18 stsp s->done = 1;
2263 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2264 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2265 e5a0f69f 2018-08-18 stsp goto done;
2267 e5a0f69f 2018-08-18 stsp return stop_blame(&s->blame);
2269 e5a0f69f 2018-08-18 stsp case KEY_UP:
2270 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2271 e5a0f69f 2018-08-18 stsp s->selected_line--;
2272 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2273 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2274 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2276 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2277 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2278 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2281 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2282 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2283 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2285 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2288 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2289 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2290 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2291 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2292 e5a0f69f 2018-08-18 stsp s->selected_line++;
2293 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2294 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2295 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2298 e5a0f69f 2018-08-18 stsp case 'p': {
2299 e5a0f69f 2018-08-18 stsp struct got_object_id *id;
2300 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2301 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2302 e5a0f69f 2018-08-18 stsp if (id == NULL || got_object_id_cmp(id,
2303 e5a0f69f 2018-08-18 stsp s->blamed_commit->id) == 0)
2305 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2306 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2307 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2310 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2312 e5a0f69f 2018-08-18 stsp if (ch == 'p' && pobj == NULL)
2314 e5a0f69f 2018-08-18 stsp s->done = 1;
2315 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2316 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2317 e5a0f69f 2018-08-18 stsp goto done;
2319 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2320 e5a0f69f 2018-08-18 stsp s->done = 0;
2321 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2322 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2323 e5a0f69f 2018-08-18 stsp goto done;
2325 e5a0f69f 2018-08-18 stsp if (thread_err)
2327 e5a0f69f 2018-08-18 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
2328 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2329 e5a0f69f 2018-08-18 stsp obj = NULL;
2330 e5a0f69f 2018-08-18 stsp if (pobj) {
2331 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2332 e5a0f69f 2018-08-18 stsp pobj = NULL;
2334 6402fb3c 2018-09-15 stsp err = got_object_qid_alloc(&s->blamed_commit, id);
2336 e5a0f69f 2018-08-18 stsp goto done;
2337 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2338 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2339 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2340 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2341 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2342 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2343 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2344 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2349 e5a0f69f 2018-08-18 stsp case 'B': {
2350 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2351 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2352 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2354 e5a0f69f 2018-08-18 stsp s->done = 1;
2355 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2356 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2357 e5a0f69f 2018-08-18 stsp goto done;
2359 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2360 e5a0f69f 2018-08-18 stsp s->done = 0;
2361 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2362 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2363 e5a0f69f 2018-08-18 stsp goto done;
2365 e5a0f69f 2018-08-18 stsp if (thread_err)
2367 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2368 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2369 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2370 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2371 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2372 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2373 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2374 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2375 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2376 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2381 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2382 e5a0f69f 2018-08-18 stsp case '\r':
2383 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2384 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2385 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2388 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2391 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
2392 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
2393 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2394 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2395 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2398 669b5ffa 2018-10-07 stsp err = open_diff_view(diff_view, pobj, obj, s->repo);
2399 e5a0f69f 2018-08-18 stsp if (err) {
2400 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2403 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
2404 669b5ffa 2018-10-07 stsp err = view_close_child(view);
2406 669b5ffa 2018-10-07 stsp return err;
2407 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
2408 669b5ffa 2018-10-07 stsp if (err) {
2409 669b5ffa 2018-10-07 stsp view_close(diff_view);
2412 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(diff_view)) {
2413 669b5ffa 2018-10-07 stsp *focus_view = diff_view;
2414 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
2417 669b5ffa 2018-10-07 stsp *new_view = diff_view;
2418 e5a0f69f 2018-08-18 stsp if (pobj) {
2419 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2420 e5a0f69f 2018-08-18 stsp pobj = NULL;
2422 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2423 e5a0f69f 2018-08-18 stsp obj = NULL;
2427 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2429 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2430 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2431 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2432 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2435 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2436 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2437 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2438 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2440 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2441 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2442 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2444 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2445 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2446 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2447 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2454 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0)
2455 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2458 b880a918 2018-07-10 stsp got_object_close(pobj);
2459 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2462 a70480e0 2018-06-23 stsp static const struct got_error *
2463 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2465 a70480e0 2018-06-23 stsp const struct got_error *error;
2466 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2467 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2468 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2469 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2471 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2473 a70480e0 2018-06-23 stsp #ifndef PROFILE
2474 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2476 a70480e0 2018-06-23 stsp err(1, "pledge");
2479 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2480 a70480e0 2018-06-23 stsp switch (ch) {
2482 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2485 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2486 69069811 2018-08-02 stsp if (repo_path == NULL)
2487 69069811 2018-08-02 stsp err(1, "-r option");
2491 a70480e0 2018-06-23 stsp /* NOTREACHED */
2495 a70480e0 2018-06-23 stsp argc -= optind;
2496 a70480e0 2018-06-23 stsp argv += optind;
2498 69069811 2018-08-02 stsp if (argc == 1)
2499 69069811 2018-08-02 stsp path = argv[0];
2501 a70480e0 2018-06-23 stsp usage_blame();
2503 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2504 69069811 2018-08-02 stsp if (cwd == NULL) {
2505 69069811 2018-08-02 stsp error = got_error_from_errno();
2506 69069811 2018-08-02 stsp goto done;
2508 69069811 2018-08-02 stsp if (repo_path == NULL) {
2509 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2510 69069811 2018-08-02 stsp if (repo_path == NULL) {
2511 69069811 2018-08-02 stsp error = got_error_from_errno();
2512 69069811 2018-08-02 stsp goto done;
2517 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2518 a70480e0 2018-06-23 stsp if (error != NULL)
2519 66b4983c 2018-06-23 stsp return error;
2521 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2522 69069811 2018-08-02 stsp if (error != NULL)
2523 69069811 2018-08-02 stsp goto done;
2525 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2526 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2527 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2528 a70480e0 2018-06-23 stsp if (error != NULL)
2529 66b4983c 2018-06-23 stsp goto done;
2530 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2531 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2533 a70480e0 2018-06-23 stsp struct got_object *obj;
2534 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2535 a70480e0 2018-06-23 stsp if (error != NULL)
2536 66b4983c 2018-06-23 stsp goto done;
2537 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
2538 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2539 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2540 a19e88aa 2018-06-23 stsp got_object_close(obj);
2542 a19e88aa 2018-06-23 stsp if (error != NULL)
2543 a19e88aa 2018-06-23 stsp goto done;
2545 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2546 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2547 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2548 e1cd8fed 2018-08-01 stsp goto done;
2550 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2551 7cbe629d 2018-08-04 stsp if (error)
2552 7cbe629d 2018-08-04 stsp goto done;
2553 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2555 69069811 2018-08-02 stsp free(repo_path);
2556 69069811 2018-08-02 stsp free(cwd);
2557 a70480e0 2018-06-23 stsp free(commit_id);
2559 a70480e0 2018-06-23 stsp got_repo_close(repo);
2560 a70480e0 2018-06-23 stsp return error;
2563 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2564 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2565 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2566 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2567 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2568 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2569 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2571 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2572 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2573 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2574 ffd1d5e5 2018-06-23 stsp int width, n;
2576 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2578 f7d12f7e 2018-08-01 stsp werase(view->window);
2580 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2581 ffd1d5e5 2018-06-23 stsp return NULL;
2583 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2585 ffd1d5e5 2018-06-23 stsp return err;
2586 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2587 a3404814 2018-09-02 stsp wstandout(view->window);
2588 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2589 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2590 a3404814 2018-09-02 stsp wstandend(view->window);
2591 2550e4c3 2018-07-13 stsp free(wline);
2592 2550e4c3 2018-07-13 stsp wline = NULL;
2593 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2594 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2595 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2596 ffd1d5e5 2018-06-23 stsp return NULL;
2597 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2599 ce52c690 2018-06-23 stsp return err;
2600 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2601 2550e4c3 2018-07-13 stsp free(wline);
2602 2550e4c3 2018-07-13 stsp wline = NULL;
2603 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2604 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2605 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2606 ffd1d5e5 2018-06-23 stsp return NULL;
2607 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2608 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2609 a1eca9bb 2018-06-23 stsp return NULL;
2611 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2612 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2613 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2614 0cf4efb1 2018-09-29 stsp if (view->focussed)
2615 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2616 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2618 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2619 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
2620 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2621 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2622 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2623 ffd1d5e5 2018-06-23 stsp return NULL;
2627 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2628 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2631 ffd1d5e5 2018-06-23 stsp while (te) {
2632 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2634 1d13200f 2018-07-12 stsp if (show_ids) {
2635 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2637 1d13200f 2018-07-12 stsp return got_error_from_errno();
2639 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2640 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2641 1d13200f 2018-07-12 stsp free(id_str);
2642 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2644 1d13200f 2018-07-12 stsp free(id_str);
2645 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2646 ffd1d5e5 2018-06-23 stsp if (err) {
2647 ffd1d5e5 2018-06-23 stsp free(line);
2650 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2651 0cf4efb1 2018-09-29 stsp if (view->focussed)
2652 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2653 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2655 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2656 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2657 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2658 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
2659 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2660 ffd1d5e5 2018-06-23 stsp free(line);
2661 2550e4c3 2018-07-13 stsp free(wline);
2662 2550e4c3 2018-07-13 stsp wline = NULL;
2664 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2665 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2666 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2668 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2671 ffd1d5e5 2018-06-23 stsp return err;
2674 ffd1d5e5 2018-06-23 stsp static void
2675 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2676 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2678 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2681 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2684 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2685 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2686 ffd1d5e5 2018-06-23 stsp if (!isroot)
2687 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2691 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2692 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2693 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2694 ffd1d5e5 2018-06-23 stsp prev = te;
2695 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2697 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2698 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2700 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2701 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2704 ffd1d5e5 2018-06-23 stsp static void
2705 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2706 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2707 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2709 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2710 ffd1d5e5 2018-06-23 stsp int n = 0;
2712 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2715 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2716 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2718 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2719 ffd1d5e5 2018-06-23 stsp while (next) {
2720 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2721 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2723 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2727 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2728 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2729 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2731 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2732 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2733 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2735 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2736 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2738 ce52c690 2018-06-23 stsp len += strlen(te->name);
2740 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2741 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2742 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2744 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2745 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2746 d9765a41 2018-06-23 stsp while (pt) {
2747 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2748 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2749 cb2ebc8a 2018-06-23 stsp goto done;
2751 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2752 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2753 cb2ebc8a 2018-06-23 stsp goto done;
2755 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2758 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2759 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2760 ce52c690 2018-06-23 stsp goto done;
2764 ce52c690 2018-06-23 stsp if (err) {
2765 ce52c690 2018-06-23 stsp free(*path);
2766 ce52c690 2018-06-23 stsp *path = NULL;
2768 ce52c690 2018-06-23 stsp return err;
2771 ce52c690 2018-06-23 stsp static const struct got_error *
2772 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
2773 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2774 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2776 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2777 ce52c690 2018-06-23 stsp char *path;
2778 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
2780 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2782 ce52c690 2018-06-23 stsp return err;
2784 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
2785 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
2786 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2788 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
2789 e5a0f69f 2018-08-18 stsp if (err) {
2790 e5a0f69f 2018-08-18 stsp view_close(blame_view);
2791 e5a0f69f 2018-08-18 stsp free(path);
2793 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
2794 69efd4c4 2018-07-18 stsp return err;
2797 69efd4c4 2018-07-18 stsp static const struct got_error *
2798 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
2799 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2800 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2802 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
2803 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2804 69efd4c4 2018-07-18 stsp char *path;
2806 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
2807 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
2808 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2810 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2812 69efd4c4 2018-07-18 stsp return err;
2814 e5a0f69f 2018-08-18 stsp err = open_log_view(log_view, commit_id, repo, path);
2816 e5a0f69f 2018-08-18 stsp view_close(log_view);
2818 e5a0f69f 2018-08-18 stsp *new_view = log_view;
2819 cb2ebc8a 2018-06-23 stsp free(path);
2820 cb2ebc8a 2018-06-23 stsp return err;
2823 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2824 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
2825 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2827 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2828 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
2829 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2831 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
2833 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2834 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2835 ffd1d5e5 2018-06-23 stsp goto done;
2837 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2838 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2839 ffd1d5e5 2018-06-23 stsp goto done;
2842 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
2843 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
2844 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2845 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
2846 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
2847 6484ec90 2018-09-29 stsp err = got_error_from_errno();
2848 6484ec90 2018-09-29 stsp goto done;
2850 fb2756b9 2018-08-04 stsp s->repo = repo;
2852 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
2853 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
2854 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
2856 ad80ab7b 2018-08-04 stsp free(commit_id_str);
2857 6484ec90 2018-09-29 stsp if (err) {
2858 fb2756b9 2018-08-04 stsp free(s->tree_label);
2859 6484ec90 2018-09-29 stsp s->tree_label = NULL;
2861 ad80ab7b 2018-08-04 stsp return err;
2864 e5a0f69f 2018-08-18 stsp static const struct got_error *
2865 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
2867 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2869 fb2756b9 2018-08-04 stsp free(s->tree_label);
2870 6484ec90 2018-09-29 stsp s->tree_label = NULL;
2871 6484ec90 2018-09-29 stsp free(s->commit_id);
2872 6484ec90 2018-09-29 stsp s->commit_id = NULL;
2873 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
2874 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
2875 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
2876 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
2877 ad80ab7b 2018-08-04 stsp free(parent);
2880 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
2881 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
2882 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
2884 e5a0f69f 2018-08-18 stsp return NULL;
2887 ad80ab7b 2018-08-04 stsp static const struct got_error *
2888 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
2890 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
2891 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2892 e5a0f69f 2018-08-18 stsp char *parent_path;
2894 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
2896 e5a0f69f 2018-08-18 stsp return err;
2898 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
2899 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
2900 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2901 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
2902 e5a0f69f 2018-08-18 stsp free(parent_path);
2904 669b5ffa 2018-10-07 stsp view_vborder(view);
2905 e5a0f69f 2018-08-18 stsp return err;
2908 e5a0f69f 2018-08-18 stsp static const struct got_error *
2909 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2910 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2912 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2913 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
2914 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
2915 669b5ffa 2018-10-07 stsp int begin_x = 0;
2917 e5a0f69f 2018-08-18 stsp switch (ch) {
2919 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
2922 669b5ffa 2018-10-07 stsp if (!s->selected_entry)
2924 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
2925 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
2926 669b5ffa 2018-10-07 stsp err = log_tree_entry(&log_view, begin_x,
2927 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
2928 669b5ffa 2018-10-07 stsp s->commit_id, s->repo);
2929 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
2930 669b5ffa 2018-10-07 stsp err = view_close_child(view);
2932 669b5ffa 2018-10-07 stsp return err;
2933 669b5ffa 2018-10-07 stsp err = view_set_child(view, log_view);
2934 669b5ffa 2018-10-07 stsp if (err) {
2935 669b5ffa 2018-10-07 stsp view_close(log_view);
2938 669b5ffa 2018-10-07 stsp *focus_view = log_view;
2939 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
2941 669b5ffa 2018-10-07 stsp *new_view = log_view;
2944 e5a0f69f 2018-08-18 stsp case KEY_UP:
2945 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2946 e5a0f69f 2018-08-18 stsp s->selected--;
2947 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
2949 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
2950 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
2952 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2953 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
2954 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
2955 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
2956 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
2957 e5a0f69f 2018-08-18 stsp s->selected = 0;
2960 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
2961 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
2962 e5a0f69f 2018-08-18 stsp s->tree == s->root);
2965 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2966 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
2967 e5a0f69f 2018-08-18 stsp s->selected++;
2970 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
2971 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
2973 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2974 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
2975 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
2976 e5a0f69f 2018-08-18 stsp s->entries);
2977 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
2980 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
2981 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
2982 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
2984 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2985 e5a0f69f 2018-08-18 stsp case '\r':
2986 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
2987 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
2988 7837eeac 2018-09-24 stsp case KEY_BACKSPACE:
2989 e5a0f69f 2018-08-18 stsp /* user selected '..' */
2990 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
2992 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
2993 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
2995 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
2996 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
2997 e5a0f69f 2018-08-18 stsp s->entries =
2998 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
2999 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
3000 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
3001 e5a0f69f 2018-08-18 stsp s->selected_entry =
3002 e5a0f69f 2018-08-18 stsp parent->selected_entry;
3003 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
3004 e5a0f69f 2018-08-18 stsp free(parent);
3005 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
3006 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3007 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
3008 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
3009 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
3012 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
3013 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
3014 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
3017 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
3018 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
3019 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
3020 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
3021 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
3022 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3023 e5a0f69f 2018-08-18 stsp s->tree = child;
3024 e5a0f69f 2018-08-18 stsp s->entries =
3025 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3026 e5a0f69f 2018-08-18 stsp s->selected = 0;
3027 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3028 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
3029 669b5ffa 2018-10-07 stsp struct tog_view *blame_view;
3030 669b5ffa 2018-10-07 stsp int begin_x = view_is_parent_view(view) ?
3031 669b5ffa 2018-10-07 stsp view_split_begin_x(view->begin_x) : 0;
3033 669b5ffa 2018-10-07 stsp err = blame_tree_entry(&blame_view, begin_x,
3034 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents, s->commit_id,
3038 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3039 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3041 669b5ffa 2018-10-07 stsp return err;
3042 669b5ffa 2018-10-07 stsp err = view_set_child(view, blame_view);
3043 669b5ffa 2018-10-07 stsp if (err) {
3044 669b5ffa 2018-10-07 stsp view_close(blame_view);
3047 669b5ffa 2018-10-07 stsp *focus_view = blame_view;
3048 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3050 669b5ffa 2018-10-07 stsp *new_view = blame_view;
3053 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3054 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3055 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3061 ffd1d5e5 2018-06-23 stsp return err;
3064 ffd1d5e5 2018-06-23 stsp __dead static void
3065 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3068 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3069 ffd1d5e5 2018-06-23 stsp getprogname());
3073 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3074 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3076 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3077 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3078 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3079 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3080 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3081 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3082 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3084 5221c383 2018-08-01 stsp struct tog_view *view;
3086 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3087 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3089 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3092 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3093 ffd1d5e5 2018-06-23 stsp switch (ch) {
3095 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3099 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3103 ffd1d5e5 2018-06-23 stsp argc -= optind;
3104 ffd1d5e5 2018-06-23 stsp argv += optind;
3106 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3107 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
3108 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3109 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3110 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3111 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3112 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3113 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3115 ffd1d5e5 2018-06-23 stsp usage_log();
3117 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3118 ffd1d5e5 2018-06-23 stsp free(repo_path);
3119 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3120 ffd1d5e5 2018-06-23 stsp return error;
3122 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
3123 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3124 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3125 ffd1d5e5 2018-06-23 stsp goto done;
3127 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
3128 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3129 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
3130 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
3131 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
3132 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
3135 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3136 ffd1d5e5 2018-06-23 stsp goto done;
3138 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3139 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3140 ffd1d5e5 2018-06-23 stsp goto done;
3142 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3143 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3144 ffd1d5e5 2018-06-23 stsp goto done;
3146 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3147 5221c383 2018-08-01 stsp if (view == NULL) {
3148 5221c383 2018-08-01 stsp error = got_error_from_errno();
3149 5221c383 2018-08-01 stsp goto done;
3151 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3152 ad80ab7b 2018-08-04 stsp if (error)
3153 ad80ab7b 2018-08-04 stsp goto done;
3154 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3156 ffd1d5e5 2018-06-23 stsp free(commit_id);
3157 ffd1d5e5 2018-06-23 stsp if (commit)
3158 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3160 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3162 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3163 ffd1d5e5 2018-06-23 stsp return error;
3165 5c5136c5 2018-05-20 stsp static void
3166 9f7d7167 2018-04-29 stsp init_curses(void)
3168 9f7d7167 2018-04-29 stsp initscr();
3172 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3173 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3174 1f475ad8 2018-05-10 stsp curs_set(0);
3177 4ed7e80c 2018-05-20 stsp __dead static void
3178 9f7d7167 2018-04-29 stsp usage(void)
3182 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3183 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3184 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3185 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3186 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3191 c2301be8 2018-04-30 stsp static char **
3192 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3194 c2301be8 2018-04-30 stsp char **argv;
3195 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3197 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3198 c2301be8 2018-04-30 stsp if (argv == NULL)
3199 c2301be8 2018-04-30 stsp err(1, "calloc");
3200 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3201 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3202 c2301be8 2018-04-30 stsp err(1, "calloc");
3203 c2301be8 2018-04-30 stsp if (arg1) {
3204 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3205 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3206 c2301be8 2018-04-30 stsp err(1, "calloc");
3209 c2301be8 2018-04-30 stsp return argv;
3213 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3215 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3216 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3217 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3218 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3220 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
3222 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3223 9f7d7167 2018-04-29 stsp switch (ch) {
3225 9f7d7167 2018-04-29 stsp hflag = 1;
3229 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3233 9f7d7167 2018-04-29 stsp argc -= optind;
3234 9f7d7167 2018-04-29 stsp argv += optind;
3235 9f7d7167 2018-04-29 stsp optind = 0;
3236 c2301be8 2018-04-30 stsp optreset = 1;
3238 c2301be8 2018-04-30 stsp if (argc == 0) {
3239 f29d3e89 2018-06-23 stsp if (hflag)
3241 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3242 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3243 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3248 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3249 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3250 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3251 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3252 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3253 9f7d7167 2018-04-29 stsp if (hflag)
3254 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3258 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3259 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3260 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3261 c2301be8 2018-04-30 stsp if (repo_path) {
3262 c2301be8 2018-04-30 stsp struct got_repository *repo;
3263 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3264 c2301be8 2018-04-30 stsp if (error == NULL)
3265 c2301be8 2018-04-30 stsp got_repo_close(repo);
3267 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3268 c2301be8 2018-04-30 stsp if (error) {
3269 f29d3e89 2018-06-23 stsp if (hflag) {
3270 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3271 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3275 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3276 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3277 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3278 ad7de8d9 2018-04-30 stsp free(repo_path);
3281 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3282 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3284 c2301be8 2018-04-30 stsp free(repo_path);
3288 5c5136c5 2018-05-20 stsp init_curses();
3290 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3291 9f7d7167 2018-04-29 stsp if (error)
3292 9f7d7167 2018-04-29 stsp goto done;
3295 c2301be8 2018-04-30 stsp free(cmd_argv);
3296 9f7d7167 2018-04-29 stsp if (error)
3297 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);