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;
99 48ae06ee 2018-10-18 stsp int diff_context;
100 48ae06ee 2018-10-18 stsp struct got_repository *repo;
103 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
104 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
105 ba4f502b 2018-08-04 stsp struct got_object_id *id;
106 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
108 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
109 ba4f502b 2018-08-04 stsp struct commit_queue {
110 ba4f502b 2018-08-04 stsp int ncommits;
111 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
114 b01e7d3b 2018-08-04 stsp struct tog_log_view_state {
115 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
116 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
117 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
118 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
119 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
120 b01e7d3b 2018-08-04 stsp int selected;
121 b01e7d3b 2018-08-04 stsp char *in_repo_path;
122 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
123 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
126 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
127 e9424729 2018-08-04 stsp pthread_mutex_t *mutex;
128 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
129 e9424729 2018-08-04 stsp int nlines;
131 e9424729 2018-08-04 stsp struct tog_view *view;
132 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
134 e9424729 2018-08-04 stsp const char *path;
135 e9424729 2018-08-04 stsp int *first_displayed_line;
136 e9424729 2018-08-04 stsp int *last_displayed_line;
137 e9424729 2018-08-04 stsp int *selected_line;
142 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
143 e9424729 2018-08-04 stsp const char *path;
144 e9424729 2018-08-04 stsp struct got_repository *repo;
145 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
146 e9424729 2018-08-04 stsp int *complete;
149 e9424729 2018-08-04 stsp struct tog_blame {
151 e9424729 2018-08-04 stsp size_t filesize;
152 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
153 e9424729 2018-08-04 stsp size_t nlines;
154 e9424729 2018-08-04 stsp pthread_t thread;
155 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
156 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
157 e9424729 2018-08-04 stsp const char *path;
160 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
161 7cbe629d 2018-08-04 stsp int first_displayed_line;
162 7cbe629d 2018-08-04 stsp int last_displayed_line;
163 7cbe629d 2018-08-04 stsp int selected_line;
164 7cbe629d 2018-08-04 stsp int blame_complete;
167 7cbe629d 2018-08-04 stsp pthread_mutex_t mutex;
168 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
169 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
170 e5a0f69f 2018-08-18 stsp char *path;
171 7cbe629d 2018-08-04 stsp struct got_repository *repo;
172 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
173 e9424729 2018-08-04 stsp struct tog_blame blame;
176 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
177 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
178 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
179 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
180 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
181 ad80ab7b 2018-08-04 stsp int selected;
184 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
186 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
187 ad80ab7b 2018-08-04 stsp char *tree_label;
188 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
189 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
190 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
191 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
192 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
193 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
194 ad80ab7b 2018-08-04 stsp int nentries, ndisplayed, selected, show_ids;
195 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
196 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
197 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
201 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
203 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
204 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
205 669b5ffa 2018-10-07 stsp * there is enough screen estate.
207 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
208 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
210 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
211 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
212 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
214 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
215 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
217 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
219 cc3c9aac 2018-08-01 stsp struct tog_view {
220 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
221 26ed57b2 2018-05-19 stsp WINDOW *window;
222 26ed57b2 2018-05-19 stsp PANEL *panel;
223 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
224 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
225 1004088d 2018-09-29 stsp int focussed;
226 669b5ffa 2018-10-07 stsp struct tog_view *parent;
227 669b5ffa 2018-10-07 stsp struct tog_view *child;
228 669b5ffa 2018-10-07 stsp int child_focussed;
230 5dc9f4bc 2018-08-04 stsp /* type-specific state */
231 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
233 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
234 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
235 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
236 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
239 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
240 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
241 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
242 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
245 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
246 ba4f502b 2018-08-04 stsp struct got_object *, struct got_object *, struct got_repository *);
247 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
248 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
249 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
250 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
252 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
253 ba4f502b 2018-08-04 stsp struct got_object_id *, struct got_repository *, const char *);
254 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
255 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
256 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
257 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
259 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
260 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
261 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
262 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
263 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
264 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
266 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
267 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
268 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
269 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
270 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
271 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
273 e5a0f69f 2018-08-18 stsp static const struct got_error *
274 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
276 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
278 669b5ffa 2018-10-07 stsp if (view->child) {
279 669b5ffa 2018-10-07 stsp view_close(view->child);
280 669b5ffa 2018-10-07 stsp view->child = NULL;
282 e5a0f69f 2018-08-18 stsp if (view->close)
283 e5a0f69f 2018-08-18 stsp err = view->close(view);
284 ea5e7bb5 2018-08-01 stsp if (view->panel)
285 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
286 ea5e7bb5 2018-08-01 stsp if (view->window)
287 ea5e7bb5 2018-08-01 stsp delwin(view->window);
288 ea5e7bb5 2018-08-01 stsp free(view);
289 e5a0f69f 2018-08-18 stsp return err;
292 ea5e7bb5 2018-08-01 stsp static struct tog_view *
293 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
294 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
296 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
298 ea5e7bb5 2018-08-01 stsp if (view == NULL)
299 ea5e7bb5 2018-08-01 stsp return NULL;
301 d6b05b5b 2018-08-04 stsp view->type = type;
302 f7d12f7e 2018-08-01 stsp view->lines = LINES;
303 f7d12f7e 2018-08-01 stsp view->cols = COLS;
304 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
305 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
306 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
307 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
308 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
309 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
310 96a765a8 2018-08-04 stsp view_close(view);
311 ea5e7bb5 2018-08-01 stsp return NULL;
313 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
314 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
315 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
316 96a765a8 2018-08-04 stsp view_close(view);
317 ea5e7bb5 2018-08-01 stsp return NULL;
320 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
321 ea5e7bb5 2018-08-01 stsp return view;
325 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
327 0cf4efb1 2018-09-29 stsp if (begin_x > 0)
329 0cf4efb1 2018-09-29 stsp return (COLS >= 120 ? COLS/2 : 0);
332 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
334 5c60c32a 2018-10-18 stsp static const struct got_error *
335 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
337 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
339 5c60c32a 2018-10-18 stsp view->begin_y = 0;
340 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
341 5c60c32a 2018-10-18 stsp view->nlines = LINES;
342 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
343 5c60c32a 2018-10-18 stsp view->lines = LINES;
344 5c60c32a 2018-10-18 stsp view->cols = COLS;
345 5c60c32a 2018-10-18 stsp err = view_resize(view);
347 5c60c32a 2018-10-18 stsp return err;
349 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
350 5c60c32a 2018-10-18 stsp return got_error_from_errno();
352 5c60c32a 2018-10-18 stsp return NULL;
355 5c60c32a 2018-10-18 stsp static const struct got_error *
356 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
358 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
360 5c60c32a 2018-10-18 stsp view->begin_x = 0;
361 5c60c32a 2018-10-18 stsp view->begin_y = 0;
362 5c60c32a 2018-10-18 stsp view->nlines = LINES;
363 5c60c32a 2018-10-18 stsp view->ncols = COLS;
364 5c60c32a 2018-10-18 stsp view->lines = LINES;
365 5c60c32a 2018-10-18 stsp view->cols = COLS;
366 5c60c32a 2018-10-18 stsp err = view_resize(view);
368 5c60c32a 2018-10-18 stsp return err;
370 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
371 5c60c32a 2018-10-18 stsp return got_error_from_errno();
373 5c60c32a 2018-10-18 stsp return NULL;
377 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
379 5c60c32a 2018-10-18 stsp return view->parent == NULL;
382 4d8c2215 2018-08-19 stsp static const struct got_error *
383 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
385 f7d12f7e 2018-08-01 stsp int nlines, ncols;
387 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
388 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
390 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
392 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
393 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
395 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
397 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
398 0cf4efb1 2018-09-29 stsp return got_error_from_errno();
399 0cf4efb1 2018-09-29 stsp replace_panel(view->panel, view->window);
401 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
402 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
403 0cf4efb1 2018-09-29 stsp view->lines = LINES;
404 0cf4efb1 2018-09-29 stsp view->cols = COLS;
406 5c60c32a 2018-10-18 stsp if (view_is_parent_view(view)) {
407 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
408 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
409 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
410 5c60c32a 2018-10-18 stsp if (view->child->focussed)
411 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
413 5c60c32a 2018-10-18 stsp show_panel(view->panel);
415 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
416 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
420 5c60c32a 2018-10-18 stsp return NULL;
423 669b5ffa 2018-10-07 stsp static const struct got_error *
424 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
426 669b5ffa 2018-10-07 stsp const struct got_error *err;
428 669b5ffa 2018-10-07 stsp if (view->child == NULL)
429 669b5ffa 2018-10-07 stsp return NULL;
431 669b5ffa 2018-10-07 stsp err = view_close(view->child);
432 669b5ffa 2018-10-07 stsp view->child = NULL;
433 669b5ffa 2018-10-07 stsp return err;
436 669b5ffa 2018-10-07 stsp static const struct got_error *
437 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
439 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
441 669b5ffa 2018-10-07 stsp view->child = child;
442 669b5ffa 2018-10-07 stsp child->parent = view;
443 669b5ffa 2018-10-07 stsp return err;
447 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
449 669b5ffa 2018-10-07 stsp return !view_is_parent_view(view) && view->begin_x > 0;
452 0cf4efb1 2018-09-29 stsp static const struct got_error *
453 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
454 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
455 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
457 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
458 669b5ffa 2018-10-07 stsp struct tog_view *v;
461 e5a0f69f 2018-08-18 stsp *new = NULL;
462 e5a0f69f 2018-08-18 stsp *dead = NULL;
463 0cf4efb1 2018-09-29 stsp *focus = NULL;
465 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
466 e5a0f69f 2018-08-18 stsp ch = wgetch(view->window);
467 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
468 e5a0f69f 2018-08-18 stsp switch (ch) {
472 669b5ffa 2018-10-07 stsp if (view->child) {
473 669b5ffa 2018-10-07 stsp *focus = view->child;
474 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
475 669b5ffa 2018-10-07 stsp } else if (view->parent) {
476 669b5ffa 2018-10-07 stsp *focus = view->parent;
477 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 0;
481 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
482 e5a0f69f 2018-08-18 stsp *dead = view;
488 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
489 669b5ffa 2018-10-07 stsp if (view->child == NULL)
491 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view->child)) {
492 669b5ffa 2018-10-07 stsp *focus = view->child;
493 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
494 669b5ffa 2018-10-07 stsp err = view_fullscreen(view->child);
496 669b5ffa 2018-10-07 stsp err = view_splitscreen(view->child);
499 669b5ffa 2018-10-07 stsp err = view->child->input(new, dead, focus,
500 669b5ffa 2018-10-07 stsp view->child, KEY_RESIZE);
502 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view)) {
503 669b5ffa 2018-10-07 stsp *focus = view;
504 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 1;
505 669b5ffa 2018-10-07 stsp err = view_fullscreen(view);
507 669b5ffa 2018-10-07 stsp err = view_splitscreen(view);
511 669b5ffa 2018-10-07 stsp err = view->input(new, dead, focus, view,
512 669b5ffa 2018-10-07 stsp KEY_RESIZE);
515 0cf4efb1 2018-09-29 stsp case KEY_RESIZE:
516 0cf4efb1 2018-09-29 stsp TAILQ_FOREACH(v, views, entry) {
517 0cf4efb1 2018-09-29 stsp err = view_resize(v);
519 0cf4efb1 2018-09-29 stsp return err;
520 878940b7 2018-09-29 stsp err = v->input(new, dead, focus, v, ch);
524 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
528 e5a0f69f 2018-08-18 stsp return err;
532 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
534 0cf4efb1 2018-09-29 stsp PANEL *panel;
535 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
537 669b5ffa 2018-10-07 stsp if (view->parent)
538 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
540 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
541 0cf4efb1 2018-09-29 stsp if (panel == NULL)
544 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
545 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
546 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
550 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
552 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
553 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
555 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
557 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
560 669b5ffa 2018-10-07 stsp return view->focussed;
563 bcbd79e2 2018-08-19 stsp static const struct got_error *
564 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
566 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
567 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
568 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
569 e4197bf9 2018-08-18 stsp int done = 0;
571 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
572 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
574 a81bf10d 2018-09-29 stsp main_view = view;
575 1004088d 2018-09-29 stsp view->focussed = 1;
576 878940b7 2018-09-29 stsp err = view->show(view);
578 0cf4efb1 2018-09-29 stsp return err;
579 0cf4efb1 2018-09-29 stsp update_panels();
580 0cf4efb1 2018-09-29 stsp doupdate();
581 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
582 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
583 e4197bf9 2018-08-18 stsp view, &views);
586 e5a0f69f 2018-08-18 stsp if (dead_view) {
587 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
589 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
590 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
591 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
593 669b5ffa 2018-10-07 stsp prev = view->parent;
595 669b5ffa 2018-10-07 stsp if (dead_view->parent)
596 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
598 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
600 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
601 a81bf10d 2018-09-29 stsp if (err || dead_view == main_view)
604 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
605 0cf4efb1 2018-09-29 stsp if (focus_view)
606 0cf4efb1 2018-09-29 stsp view = focus_view;
607 669b5ffa 2018-10-07 stsp else if (prev)
608 669b5ffa 2018-10-07 stsp view = prev;
609 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
610 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
611 0cf4efb1 2018-09-29 stsp tog_view_list_head);
613 0cf4efb1 2018-09-29 stsp view = NULL;
614 669b5ffa 2018-10-07 stsp if (view) {
615 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
616 669b5ffa 2018-10-07 stsp focus_view = view->child;
618 669b5ffa 2018-10-07 stsp focus_view = view;
622 bcbd79e2 2018-08-19 stsp if (new_view) {
623 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
624 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
625 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
626 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
628 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
629 86c66b02 2018-10-18 stsp err = view_close(v);
632 86c66b02 2018-10-18 stsp if (v == view)
633 86c66b02 2018-10-18 stsp view = new_view;
636 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
637 878940b7 2018-09-29 stsp if (focus_view == NULL)
638 878940b7 2018-09-29 stsp focus_view = new_view;
640 0cf4efb1 2018-09-29 stsp if (focus_view) {
641 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
643 0cf4efb1 2018-09-29 stsp view->focussed = 0;
644 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
645 0cf4efb1 2018-09-29 stsp view = focus_view;
646 878940b7 2018-09-29 stsp if (new_view)
647 878940b7 2018-09-29 stsp show_panel(new_view->panel);
648 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
649 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
651 669b5ffa 2018-10-07 stsp if (view) {
652 669b5ffa 2018-10-07 stsp if (view->parent) {
653 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
655 669b5ffa 2018-10-07 stsp return err;
657 669b5ffa 2018-10-07 stsp err = view->show(view);
659 878940b7 2018-09-29 stsp return err;
660 669b5ffa 2018-10-07 stsp if (view->child) {
661 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
663 669b5ffa 2018-10-07 stsp return err;
666 0cf4efb1 2018-09-29 stsp update_panels();
667 0cf4efb1 2018-09-29 stsp doupdate();
670 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
671 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
672 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
673 e5a0f69f 2018-08-18 stsp view_close(view);
675 e5a0f69f 2018-08-18 stsp return err;
678 4ed7e80c 2018-05-20 stsp __dead static void
679 9f7d7167 2018-04-29 stsp usage_log(void)
682 c70c5802 2018-08-01 stsp fprintf(stderr,
683 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
684 9f7d7167 2018-04-29 stsp getprogname());
688 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
689 80ddbec8 2018-04-29 stsp static const struct got_error *
690 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
692 00dfcb92 2018-06-11 stsp char *vis = NULL;
693 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
695 963b370f 2018-05-20 stsp *ws = NULL;
696 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
697 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
698 00dfcb92 2018-06-11 stsp int vislen;
699 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
700 00dfcb92 2018-06-11 stsp return got_error_from_errno();
702 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
703 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
705 00dfcb92 2018-06-11 stsp return err;
706 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
707 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
708 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
713 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
714 a7f50699 2018-06-11 stsp if (*ws == NULL) {
715 a7f50699 2018-06-11 stsp err = got_error_from_errno();
719 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
720 963b370f 2018-05-20 stsp err = got_error_from_errno();
725 963b370f 2018-05-20 stsp *ws = NULL;
728 963b370f 2018-05-20 stsp return err;
731 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
732 963b370f 2018-05-20 stsp static const struct got_error *
733 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
735 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
736 963b370f 2018-05-20 stsp int cols = 0;
737 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
738 963b370f 2018-05-20 stsp size_t wlen;
741 963b370f 2018-05-20 stsp *wlinep = NULL;
742 b700b5d6 2018-07-10 stsp *widthp = 0;
744 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
746 963b370f 2018-05-20 stsp return err;
749 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
750 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
751 963b370f 2018-05-20 stsp switch (width) {
757 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
758 b700b5d6 2018-07-10 stsp cols += width;
762 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
763 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
767 963b370f 2018-05-20 stsp err = got_error_from_errno();
771 963b370f 2018-05-20 stsp wline[i] = L'\0';
772 b700b5d6 2018-07-10 stsp if (widthp)
773 b700b5d6 2018-07-10 stsp *widthp = cols;
776 963b370f 2018-05-20 stsp free(wline);
778 963b370f 2018-05-20 stsp *wlinep = wline;
779 963b370f 2018-05-20 stsp return err;
782 963b370f 2018-05-20 stsp static const struct got_error *
783 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
784 2814baeb 2018-08-01 stsp struct got_object_id *id)
786 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
787 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
788 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
789 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
790 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
791 bb737323 2018-05-20 stsp int author_width, logmsg_width;
792 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
793 80ddbec8 2018-04-29 stsp char *line = NULL;
794 bb737323 2018-05-20 stsp int col, limit;
795 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
796 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
797 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
799 c70c5802 2018-08-01 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
800 c70c5802 2018-08-01 stsp &commit->tm_committer) >= sizeof(datebuf))
801 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
803 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
804 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
806 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
807 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
808 b39d25c7 2018-07-10 stsp col = limit + 1;
809 b39d25c7 2018-07-10 stsp if (col > avail)
812 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
813 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
814 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
817 6d9fbc00 2018-04-29 stsp author = author0;
818 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
819 6d9fbc00 2018-04-29 stsp if (smallerthan)
820 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
822 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
824 6d9fbc00 2018-04-29 stsp *at = '\0';
826 b39d25c7 2018-07-10 stsp limit = avail - col;
827 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
830 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
831 bb737323 2018-05-20 stsp col += author_width;
832 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
833 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
835 bb737323 2018-05-20 stsp author_width++;
837 9c2eaf34 2018-05-20 stsp if (col > avail)
840 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
841 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
842 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
845 bb737323 2018-05-20 stsp logmsg = logmsg0;
846 bb737323 2018-05-20 stsp while (*logmsg == '\n')
848 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
849 bb737323 2018-05-20 stsp if (newline)
850 bb737323 2018-05-20 stsp *newline = '\0';
851 bb737323 2018-05-20 stsp limit = avail - col;
852 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
855 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
856 bb737323 2018-05-20 stsp col += logmsg_width;
857 bb737323 2018-05-20 stsp while (col <= avail) {
858 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
862 80ddbec8 2018-04-29 stsp free(logmsg0);
863 bb737323 2018-05-20 stsp free(wlogmsg);
864 6d9fbc00 2018-04-29 stsp free(author0);
865 bb737323 2018-05-20 stsp free(wauthor);
866 80ddbec8 2018-04-29 stsp free(line);
867 80ddbec8 2018-04-29 stsp return err;
870 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
871 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
872 899d86c2 2018-05-10 stsp struct got_object_id *id)
874 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
876 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
877 80ddbec8 2018-04-29 stsp if (entry == NULL)
878 899d86c2 2018-05-10 stsp return NULL;
880 899d86c2 2018-05-10 stsp entry->id = id;
881 99db9666 2018-05-07 stsp entry->commit = commit;
882 899d86c2 2018-05-10 stsp return entry;
885 99db9666 2018-05-07 stsp static void
886 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
888 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
890 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
891 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
892 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
893 ecb28ae0 2018-07-16 stsp commits->ncommits--;
894 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
895 99db9666 2018-05-07 stsp free(entry);
898 99db9666 2018-05-07 stsp static void
899 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
901 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
902 99db9666 2018-05-07 stsp pop_commit(commits);
905 c4972b91 2018-05-07 stsp static const struct got_error *
906 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
907 93e45b7c 2018-09-24 stsp struct got_object_id *start_id, int minqueue,
908 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
910 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
911 93e45b7c 2018-09-24 stsp int nqueued = 0;
913 93e45b7c 2018-09-24 stsp if (start_id) {
914 93e45b7c 2018-09-24 stsp err = got_commit_graph_iter_start(graph, start_id, repo);
916 9ba79e04 2018-06-11 stsp return err;
919 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
920 93e45b7c 2018-09-24 stsp struct got_object_id *id;
921 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
922 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
924 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
926 93e45b7c 2018-09-24 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
927 9ba79e04 2018-06-11 stsp err = NULL;
930 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
932 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
933 93e45b7c 2018-09-24 stsp minqueue, repo);
935 ecb28ae0 2018-07-16 stsp return err;
939 ecb28ae0 2018-07-16 stsp if (id == NULL)
942 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
945 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
946 9ba79e04 2018-06-11 stsp if (entry == NULL) {
947 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
951 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
953 ecb28ae0 2018-07-16 stsp commits->ncommits++;
956 9ba79e04 2018-06-11 stsp return err;
959 99db9666 2018-05-07 stsp static const struct got_error *
960 9ba79e04 2018-06-11 stsp fetch_next_commit(struct commit_queue_entry **pentry,
961 9ba79e04 2018-06-11 stsp struct commit_queue_entry *entry, struct commit_queue *commits,
962 ecb28ae0 2018-07-16 stsp struct got_commit_graph *graph, struct got_repository *repo,
963 ecb28ae0 2018-07-16 stsp const char *path)
965 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
967 9ba79e04 2018-06-11 stsp *pentry = NULL;
969 93e45b7c 2018-09-24 stsp err = queue_commits(graph, commits, NULL, 1, repo, path);
971 9ba79e04 2018-06-11 stsp return err;
973 9ba79e04 2018-06-11 stsp /* Next entry to display should now be available. */
974 9ba79e04 2018-06-11 stsp *pentry = TAILQ_NEXT(entry, entry);
975 9ba79e04 2018-06-11 stsp return NULL;
978 899d86c2 2018-05-10 stsp static const struct got_error *
979 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
981 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
982 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
984 9ba79e04 2018-06-11 stsp *head_id = NULL;
986 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
988 99db9666 2018-05-07 stsp return err;
990 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
991 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
993 9ba79e04 2018-06-11 stsp *head_id = NULL;
994 99db9666 2018-05-07 stsp return err;
997 9ba79e04 2018-06-11 stsp return NULL;
1000 0553a4e3 2018-04-30 stsp static const struct got_error *
1001 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1002 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1003 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1004 a7f40148 2018-07-18 stsp struct got_commit_graph *graph, struct got_repository *repo,
1005 a7f40148 2018-07-18 stsp const char *path)
1007 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1008 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1009 ecb28ae0 2018-07-16 stsp int ncommits, width;
1010 867c6645 2018-07-10 stsp char *id_str, *header;
1011 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1013 e0d42f60 2018-07-22 stsp entry = first;
1014 e0d42f60 2018-07-22 stsp ncommits = 0;
1015 e0d42f60 2018-07-22 stsp while (entry) {
1016 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1017 e0d42f60 2018-07-22 stsp *selected = entry;
1020 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1021 e0d42f60 2018-07-22 stsp ncommits++;
1024 867c6645 2018-07-10 stsp err = got_object_id_str(&id_str, (*selected)->id);
1026 867c6645 2018-07-10 stsp return err;
1028 c3ba6f36 2018-08-01 stsp if (path && strcmp(path, "/") != 0) {
1029 ecb28ae0 2018-07-16 stsp if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
1030 ecb28ae0 2018-07-16 stsp err = got_error_from_errno();
1031 ecb28ae0 2018-07-16 stsp free(id_str);
1032 ecb28ae0 2018-07-16 stsp return err;
1034 ecb28ae0 2018-07-16 stsp } else if (asprintf(&header, "commit: %s", id_str) == -1) {
1035 867c6645 2018-07-10 stsp err = got_error_from_errno();
1036 867c6645 2018-07-10 stsp free(id_str);
1037 867c6645 2018-07-10 stsp return err;
1039 ecb28ae0 2018-07-16 stsp free(id_str);
1040 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, header, view->ncols);
1041 ecb28ae0 2018-07-16 stsp if (err) {
1042 ecb28ae0 2018-07-16 stsp free(header);
1043 ecb28ae0 2018-07-16 stsp return err;
1045 ecb28ae0 2018-07-16 stsp free(header);
1047 2814baeb 2018-08-01 stsp werase(view->window);
1049 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1050 a3404814 2018-09-02 stsp wstandout(view->window);
1051 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1052 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1053 a3404814 2018-09-02 stsp wstandend(view->window);
1054 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1055 2814baeb 2018-08-01 stsp waddch(view->window, '\n');
1056 ecb28ae0 2018-07-16 stsp free(wline);
1057 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1058 ecb28ae0 2018-07-16 stsp return NULL;
1060 899d86c2 2018-05-10 stsp entry = first;
1061 899d86c2 2018-05-10 stsp *last = first;
1062 867c6645 2018-07-10 stsp ncommits = 0;
1063 899d86c2 2018-05-10 stsp while (entry) {
1064 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1066 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1067 2814baeb 2018-08-01 stsp wstandout(view->window);
1068 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
1069 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1070 2814baeb 2018-08-01 stsp wstandend(view->window);
1073 0553a4e3 2018-04-30 stsp ncommits++;
1074 899d86c2 2018-05-10 stsp *last = entry;
1075 a7f40148 2018-07-18 stsp if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
1076 93e45b7c 2018-09-24 stsp err = queue_commits(graph, commits, NULL, 1,
1077 93e45b7c 2018-09-24 stsp repo, path);
1078 a7f40148 2018-07-18 stsp if (err) {
1079 a7f40148 2018-07-18 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1080 a7f40148 2018-07-18 stsp return err;
1081 a7f40148 2018-07-18 stsp err = NULL;
1084 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1087 1a57306a 2018-09-02 stsp view_vborder(view);
1089 80ddbec8 2018-04-29 stsp return err;
1092 07b55e75 2018-05-10 stsp static void
1093 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1094 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1096 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1097 07b55e75 2018-05-10 stsp int nscrolled = 0;
1099 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1100 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
1103 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1104 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1105 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1106 07b55e75 2018-05-10 stsp if (entry) {
1107 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1108 07b55e75 2018-05-10 stsp nscrolled++;
1113 aa075928 2018-05-10 stsp static const struct got_error *
1114 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1115 93e45b7c 2018-09-24 stsp struct commit_queue_entry **last_displayed_entry,
1116 9ba79e04 2018-06-11 stsp struct commit_queue *commits, struct got_commit_graph *graph,
1117 ecb28ae0 2018-07-16 stsp struct got_repository *repo, const char *path)
1119 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
1120 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
1121 aa075928 2018-05-10 stsp int nscrolled = 0;
1124 93e45b7c 2018-09-24 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1125 aa075928 2018-05-10 stsp if (pentry == NULL) {
1126 93e45b7c 2018-09-24 stsp err = fetch_next_commit(&pentry, *last_displayed_entry,
1127 ecb28ae0 2018-07-16 stsp commits, graph, repo, path);
1128 9a6bf2a5 2018-05-20 stsp if (err || pentry == NULL)
1131 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1133 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1134 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1136 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1137 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1139 dd0a52c1 2018-05-20 stsp return err;
1142 cd0acaa7 2018-05-20 stsp static const struct got_error *
1143 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1144 0cf4efb1 2018-09-29 stsp struct got_object_id *commit_id, struct got_commit_object *commit,
1145 2a4718d3 2018-09-29 stsp struct got_repository *repo)
1147 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1148 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1149 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1150 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1152 2a4718d3 2018-09-29 stsp err = got_object_open(&obj2, repo, commit_id);
1154 cd0acaa7 2018-05-20 stsp return err;
1156 2a4718d3 2018-09-29 stsp parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1157 9ba79e04 2018-06-11 stsp if (parent_id) {
1158 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
1160 cd0acaa7 2018-05-20 stsp goto done;
1163 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1164 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
1165 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1166 ea5e7bb5 2018-08-01 stsp goto done;
1169 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, obj1, obj2, repo);
1170 e5a0f69f 2018-08-18 stsp if (err == NULL)
1171 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1174 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
1176 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
1177 cd0acaa7 2018-05-20 stsp return err;
1180 80ddbec8 2018-04-29 stsp static const struct got_error *
1181 0cf4efb1 2018-09-29 stsp browse_commit(struct tog_view **new_view, int begin_x,
1182 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1184 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1185 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1186 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1188 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1190 9343a5fb 2018-06-23 stsp return err;
1192 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1193 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1194 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1196 e5a0f69f 2018-08-18 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1198 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1200 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1201 9343a5fb 2018-06-23 stsp return err;
1204 9343a5fb 2018-06-23 stsp static const struct got_error *
1205 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1206 04cc582a 2018-08-01 stsp struct got_repository *repo, const char *path)
1208 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1209 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1211 fb2756b9 2018-08-04 stsp err = got_repo_map_path(&s->in_repo_path, repo, path);
1212 ecb28ae0 2018-07-16 stsp if (err != NULL)
1213 ecb28ae0 2018-07-16 stsp goto done;
1215 93e45b7c 2018-09-24 stsp err = got_commit_graph_open(&s->graph, start_id, s->in_repo_path,
1218 9ba79e04 2018-06-11 stsp goto done;
1219 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1220 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1221 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1224 9ba79e04 2018-06-11 stsp * Open the initial batch of commits, sorted in commit graph order.
1225 9ba79e04 2018-06-11 stsp * We keep all commits open throughout the lifetime of the log view
1226 9ba79e04 2018-06-11 stsp * in order to avoid having to re-fetch commits from disk while
1227 9ba79e04 2018-06-11 stsp * updating the display.
1229 93e45b7c 2018-09-24 stsp err = queue_commits(s->graph, &s->commits, start_id, view->nlines,
1230 fb2756b9 2018-08-04 stsp repo, s->in_repo_path);
1231 55198a88 2018-07-16 stsp if (err) {
1232 55198a88 2018-07-16 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1233 55198a88 2018-07-16 stsp goto done;
1234 55198a88 2018-07-16 stsp err = NULL;
1237 93e45b7c 2018-09-24 stsp s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
1238 1bfa490b 2018-08-18 stsp s->selected_entry = s->first_displayed_entry;
1239 fb2756b9 2018-08-04 stsp s->repo = repo;
1240 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1241 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1242 5036bf37 2018-09-24 stsp err = got_error_from_errno();
1243 5036bf37 2018-09-24 stsp goto done;
1246 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1247 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1248 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1250 ba4f502b 2018-08-04 stsp return err;
1253 e5a0f69f 2018-08-18 stsp static const struct got_error *
1254 e5a0f69f 2018-08-18 stsp close_log_view(struct tog_view *view)
1256 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1258 fb2756b9 2018-08-04 stsp if (s->graph)
1259 fb2756b9 2018-08-04 stsp got_commit_graph_close(s->graph);
1260 fb2756b9 2018-08-04 stsp free_commits(&s->commits);
1261 fb2756b9 2018-08-04 stsp free(s->in_repo_path);
1262 5036bf37 2018-09-24 stsp free(s->start_id);
1263 e5a0f69f 2018-08-18 stsp return NULL;
1266 ba4f502b 2018-08-04 stsp static const struct got_error *
1267 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1269 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1270 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1272 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1273 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1274 e5a0f69f 2018-08-18 stsp &s->commits, s->selected, view->nlines, s->graph,
1275 e5a0f69f 2018-08-18 stsp s->repo, s->in_repo_path);
1277 bcbd79e2 2018-08-19 stsp return err;
1280 e5a0f69f 2018-08-18 stsp static const struct got_error *
1281 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1282 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1284 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1285 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1286 5036bf37 2018-09-24 stsp char *parent_path;
1287 669b5ffa 2018-10-07 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
1288 669b5ffa 2018-10-07 stsp int begin_x = 0;
1290 e5a0f69f 2018-08-18 stsp switch (ch) {
1292 e5a0f69f 2018-08-18 stsp case KEY_UP:
1293 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1294 e5a0f69f 2018-08-18 stsp s->selected--;
1295 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1297 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1298 e5a0f69f 2018-08-18 stsp &s->commits);
1300 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1301 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1302 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1303 e5a0f69f 2018-08-18 stsp s->selected = 0;
1306 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1307 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1310 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1311 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1312 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1313 e5a0f69f 2018-08-18 stsp s->selected++;
1316 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1317 93e45b7c 2018-09-24 stsp &s->last_displayed_entry, &s->commits,
1318 e5a0f69f 2018-08-18 stsp s->graph, s->repo, s->in_repo_path);
1319 e5a0f69f 2018-08-18 stsp if (err) {
1320 4d7951a5 2018-08-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1322 4d7951a5 2018-08-04 stsp err = NULL;
1325 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1326 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1327 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1328 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1329 93e45b7c 2018-09-24 stsp view->nlines, &s->last_displayed_entry,
1330 e5a0f69f 2018-08-18 stsp &s->commits, s->graph, s->repo,
1331 e5a0f69f 2018-08-18 stsp s->in_repo_path);
1332 93e45b7c 2018-09-24 stsp if (err && err->code != GOT_ERR_ITER_COMPLETED)
1334 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1335 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1336 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1337 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1338 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1339 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1341 e5a0f69f 2018-08-18 stsp err = NULL;
1344 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1345 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1346 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1347 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1348 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1350 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1351 e5a0f69f 2018-08-18 stsp case '\r':
1352 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1353 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1354 669b5ffa 2018-10-07 stsp err = open_diff_view_for_commit(&diff_view, begin_x,
1355 2a4718d3 2018-09-29 stsp s->selected_entry->id, s->selected_entry->commit,
1359 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1360 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1362 669b5ffa 2018-10-07 stsp return err;
1363 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
1364 669b5ffa 2018-10-07 stsp if (err) {
1365 669b5ffa 2018-10-07 stsp view_close(diff_view);
1368 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(diff_view)) {
1369 669b5ffa 2018-10-07 stsp *focus_view = diff_view;
1370 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1373 669b5ffa 2018-10-07 stsp *new_view = diff_view;
1376 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1377 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1378 669b5ffa 2018-10-07 stsp err = browse_commit(&tree_view, begin_x,
1379 0cf4efb1 2018-09-29 stsp s->selected_entry, s->repo);
1380 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1381 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1383 669b5ffa 2018-10-07 stsp return err;
1384 669b5ffa 2018-10-07 stsp err = view_set_child(view, tree_view);
1385 669b5ffa 2018-10-07 stsp if (err) {
1386 669b5ffa 2018-10-07 stsp view_close(tree_view);
1389 669b5ffa 2018-10-07 stsp *focus_view = tree_view;
1390 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1392 669b5ffa 2018-10-07 stsp *new_view = tree_view;
1394 5036bf37 2018-09-24 stsp case KEY_BACKSPACE:
1395 5036bf37 2018-09-24 stsp if (strcmp(s->in_repo_path, "/") == 0)
1397 5036bf37 2018-09-24 stsp parent_path = dirname(s->in_repo_path);
1398 5036bf37 2018-09-24 stsp if (parent_path && strcmp(parent_path, ".") != 0) {
1399 5036bf37 2018-09-24 stsp struct tog_view *lv;
1400 0cf4efb1 2018-09-29 stsp lv = view_open(view->nlines, view->ncols,
1401 0cf4efb1 2018-09-29 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
1402 5036bf37 2018-09-24 stsp if (lv == NULL)
1403 5036bf37 2018-09-24 stsp return got_error_from_errno();
1404 5036bf37 2018-09-24 stsp err = open_log_view(lv, s->start_id, s->repo,
1405 5036bf37 2018-09-24 stsp parent_path);
1408 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1409 669b5ffa 2018-10-07 stsp *new_view = lv;
1411 669b5ffa 2018-10-07 stsp view_set_child(view->parent, lv);
1412 669b5ffa 2018-10-07 stsp *dead_view = view;
1420 80ddbec8 2018-04-29 stsp return err;
1423 4ed7e80c 2018-05-20 stsp static const struct got_error *
1424 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1426 80ddbec8 2018-04-29 stsp const struct got_error *error;
1427 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1428 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1429 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1430 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1432 04cc582a 2018-08-01 stsp struct tog_view *view;
1434 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1435 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1437 80ddbec8 2018-04-29 stsp err(1, "pledge");
1440 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1441 80ddbec8 2018-04-29 stsp switch (ch) {
1443 80ddbec8 2018-04-29 stsp start_commit = optarg;
1446 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1447 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1448 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1452 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1456 80ddbec8 2018-04-29 stsp argc -= optind;
1457 80ddbec8 2018-04-29 stsp argv += optind;
1459 ecb28ae0 2018-07-16 stsp if (argc == 0)
1460 ecb28ae0 2018-07-16 stsp path = strdup("");
1461 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1462 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1464 80ddbec8 2018-04-29 stsp usage_log();
1465 ecb28ae0 2018-07-16 stsp if (path == NULL)
1466 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1468 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1469 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1470 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1471 ecb28ae0 2018-07-16 stsp goto done;
1473 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1474 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1475 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1476 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1477 ecb28ae0 2018-07-16 stsp goto done;
1481 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1482 80ddbec8 2018-04-29 stsp if (error != NULL)
1483 ecb28ae0 2018-07-16 stsp goto done;
1485 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1486 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1487 80ddbec8 2018-04-29 stsp if (error != NULL)
1488 ecb28ae0 2018-07-16 stsp goto done;
1490 80ddbec8 2018-04-29 stsp struct got_object *obj;
1491 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1492 80ddbec8 2018-04-29 stsp if (error == NULL) {
1493 6402fb3c 2018-09-15 stsp start_id = got_object_id_dup(got_object_get_id(obj));
1494 899d86c2 2018-05-10 stsp if (start_id == NULL)
1495 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1496 ecb28ae0 2018-07-16 stsp goto done;
1499 80ddbec8 2018-04-29 stsp if (error != NULL)
1500 ecb28ae0 2018-07-16 stsp goto done;
1502 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1503 04cc582a 2018-08-01 stsp if (view == NULL) {
1504 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1505 04cc582a 2018-08-01 stsp goto done;
1507 ba4f502b 2018-08-04 stsp error = open_log_view(view, start_id, repo, path);
1508 ba4f502b 2018-08-04 stsp if (error)
1509 ba4f502b 2018-08-04 stsp goto done;
1510 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1512 ecb28ae0 2018-07-16 stsp free(repo_path);
1513 ecb28ae0 2018-07-16 stsp free(cwd);
1514 ecb28ae0 2018-07-16 stsp free(path);
1515 899d86c2 2018-05-10 stsp free(start_id);
1517 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1518 80ddbec8 2018-04-29 stsp return error;
1521 4ed7e80c 2018-05-20 stsp __dead static void
1522 9f7d7167 2018-04-29 stsp usage_diff(void)
1525 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1526 9f7d7167 2018-04-29 stsp getprogname());
1530 b304db33 2018-05-20 stsp static char *
1531 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1533 b304db33 2018-05-20 stsp char *line;
1534 b304db33 2018-05-20 stsp size_t linelen;
1535 b304db33 2018-05-20 stsp size_t lineno;
1536 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1538 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1540 b304db33 2018-05-20 stsp *len = linelen;
1541 b304db33 2018-05-20 stsp return line;
1544 4ed7e80c 2018-05-20 stsp static const struct got_error *
1545 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1546 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1547 a3404814 2018-09-02 stsp char * header)
1549 61e69b96 2018-05-20 stsp const struct got_error *err;
1550 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1551 b304db33 2018-05-20 stsp char *line;
1552 b304db33 2018-05-20 stsp size_t len;
1553 61e69b96 2018-05-20 stsp wchar_t *wline;
1554 e0b650dd 2018-05-20 stsp int width;
1556 26ed57b2 2018-05-19 stsp rewind(f);
1557 f7d12f7e 2018-08-01 stsp werase(view->window);
1559 a3404814 2018-09-02 stsp if (header) {
1560 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1561 a3404814 2018-09-02 stsp if (err) {
1562 a3404814 2018-09-02 stsp return err;
1565 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1566 a3404814 2018-09-02 stsp wstandout(view->window);
1567 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1568 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1569 a3404814 2018-09-02 stsp wstandend(view->window);
1570 a3404814 2018-09-02 stsp if (width < view->ncols)
1571 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1573 a3404814 2018-09-02 stsp if (max_lines <= 1)
1574 a3404814 2018-09-02 stsp return NULL;
1575 a3404814 2018-09-02 stsp max_lines--;
1579 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1580 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1581 26ed57b2 2018-05-19 stsp if (line == NULL) {
1585 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1586 26ed57b2 2018-05-19 stsp free(line);
1590 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1591 61e69b96 2018-05-20 stsp if (err) {
1592 61e69b96 2018-05-20 stsp free(line);
1593 61e69b96 2018-05-20 stsp return err;
1595 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1596 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1597 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1598 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1599 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1600 26ed57b2 2018-05-19 stsp free(line);
1601 2550e4c3 2018-07-13 stsp free(wline);
1602 2550e4c3 2018-07-13 stsp wline = NULL;
1604 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1606 1a57306a 2018-09-02 stsp view_vborder(view);
1608 26ed57b2 2018-05-19 stsp return NULL;
1611 4ed7e80c 2018-05-20 stsp static const struct got_error *
1612 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
1614 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
1615 48ae06ee 2018-10-18 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1616 48ae06ee 2018-10-18 stsp FILE *f = NULL;
1618 48ae06ee 2018-10-18 stsp if (s->id1) {
1619 48ae06ee 2018-10-18 stsp err = got_object_open(&obj1, s->repo, s->id1);
1621 48ae06ee 2018-10-18 stsp return err;
1624 48ae06ee 2018-10-18 stsp err = got_object_open(&obj2, s->repo, s->id2);
1626 48ae06ee 2018-10-18 stsp goto done;
1628 511a516b 2018-05-19 stsp f = got_opentemp();
1629 48ae06ee 2018-10-18 stsp if (f == NULL) {
1630 48ae06ee 2018-10-18 stsp err = got_error_from_errno();
1631 48ae06ee 2018-10-18 stsp goto done;
1634 48ae06ee 2018-10-18 stsp fclose(s->f);
1637 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1638 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1639 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1640 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1642 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1643 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_trees(obj1, obj2, "", "",
1644 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1646 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1647 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1648 48ae06ee 2018-10-18 stsp s->repo, f);
1651 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1656 48ae06ee 2018-10-18 stsp got_object_close(obj1);
1657 48ae06ee 2018-10-18 stsp got_object_close(obj2);
1659 48ae06ee 2018-10-18 stsp fflush(f);
1660 48ae06ee 2018-10-18 stsp return err;
1663 48ae06ee 2018-10-18 stsp static const struct got_error *
1664 48ae06ee 2018-10-18 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1665 48ae06ee 2018-10-18 stsp struct got_object *obj2, struct got_repository *repo)
1667 48ae06ee 2018-10-18 stsp const struct got_error *err;
1669 48ae06ee 2018-10-18 stsp if (obj1 != NULL && obj2 != NULL &&
1670 48ae06ee 2018-10-18 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1671 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
1673 48ae06ee 2018-10-18 stsp if (obj1) {
1674 48ae06ee 2018-10-18 stsp struct got_object_id *id1;
1675 48ae06ee 2018-10-18 stsp id1 = got_object_id_dup(got_object_get_id(obj1));
1676 48ae06ee 2018-10-18 stsp if (id1 == NULL)
1677 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1678 48ae06ee 2018-10-18 stsp view->state.diff.id1 = id1;
1680 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1682 48ae06ee 2018-10-18 stsp view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1683 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
1684 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1685 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1686 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1688 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
1689 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1690 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1691 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
1692 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
1694 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
1695 48ae06ee 2018-10-18 stsp if (err) {
1696 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1697 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1698 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
1699 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
1700 48ae06ee 2018-10-18 stsp return err;
1703 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
1704 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
1705 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
1707 5dc9f4bc 2018-08-04 stsp return NULL;
1710 e5a0f69f 2018-08-18 stsp static const struct got_error *
1711 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1713 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1715 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1716 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1717 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
1718 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
1719 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1720 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1721 e5a0f69f 2018-08-18 stsp return err;
1724 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1725 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1727 a3404814 2018-09-02 stsp const struct got_error *err;
1728 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1729 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
1731 a3404814 2018-09-02 stsp if (s->id1) {
1732 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
1734 a3404814 2018-09-02 stsp return err;
1736 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
1738 a3404814 2018-09-02 stsp return err;
1740 a3404814 2018-09-02 stsp if (asprintf(&header, "diff: %s %s",
1741 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1742 a3404814 2018-09-02 stsp err = got_error_from_errno();
1743 a3404814 2018-09-02 stsp free(id_str1);
1744 a3404814 2018-09-02 stsp free(id_str2);
1745 a3404814 2018-09-02 stsp return err;
1747 a3404814 2018-09-02 stsp free(id_str1);
1748 a3404814 2018-09-02 stsp free(id_str2);
1750 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
1751 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
1755 0cf4efb1 2018-09-29 stsp static const struct got_error *
1756 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1757 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1759 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1760 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
1763 e5a0f69f 2018-08-18 stsp switch (ch) {
1765 e5a0f69f 2018-08-18 stsp case KEY_UP:
1766 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
1767 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1769 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1771 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
1772 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
1773 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1776 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1777 e5a0f69f 2018-08-18 stsp if (!s->eof)
1778 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1780 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
1783 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
1784 e5a0f69f 2018-08-18 stsp char *line;
1785 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
1786 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1787 e5a0f69f 2018-08-18 stsp if (line == NULL)
1792 48ae06ee 2018-10-18 stsp if (s->diff_context > 0) {
1793 48ae06ee 2018-10-18 stsp s->diff_context--;
1794 48ae06ee 2018-10-18 stsp err = create_diff(s);
1798 48ae06ee 2018-10-18 stsp if (s->diff_context < INT_MAX) {
1799 48ae06ee 2018-10-18 stsp s->diff_context++;
1800 48ae06ee 2018-10-18 stsp err = create_diff(s);
1807 bcbd79e2 2018-08-19 stsp return err;
1810 4ed7e80c 2018-05-20 stsp static const struct got_error *
1811 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1813 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1814 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1815 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1816 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1817 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1819 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1821 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1822 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1824 26ed57b2 2018-05-19 stsp err(1, "pledge");
1827 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1828 26ed57b2 2018-05-19 stsp switch (ch) {
1831 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1835 26ed57b2 2018-05-19 stsp argc -= optind;
1836 26ed57b2 2018-05-19 stsp argv += optind;
1838 26ed57b2 2018-05-19 stsp if (argc == 0) {
1839 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1840 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1841 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1842 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1843 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1844 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1845 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1846 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1847 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1848 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1849 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1850 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1851 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1853 26ed57b2 2018-05-19 stsp usage_diff();
1855 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1856 26ed57b2 2018-05-19 stsp free(repo_path);
1857 26ed57b2 2018-05-19 stsp if (error)
1858 26ed57b2 2018-05-19 stsp goto done;
1860 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1861 26ed57b2 2018-05-19 stsp if (error)
1862 26ed57b2 2018-05-19 stsp goto done;
1864 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1865 26ed57b2 2018-05-19 stsp if (error)
1866 26ed57b2 2018-05-19 stsp goto done;
1868 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
1869 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1870 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1871 ea5e7bb5 2018-08-01 stsp goto done;
1873 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
1874 5dc9f4bc 2018-08-04 stsp if (error)
1875 5dc9f4bc 2018-08-04 stsp goto done;
1876 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1878 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1880 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1882 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1883 26ed57b2 2018-05-19 stsp return error;
1886 4ed7e80c 2018-05-20 stsp __dead static void
1887 9f7d7167 2018-04-29 stsp usage_blame(void)
1890 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1891 9f7d7167 2018-04-29 stsp getprogname());
1895 84451b3e 2018-07-10 stsp struct tog_blame_line {
1896 84451b3e 2018-07-10 stsp int annotated;
1897 84451b3e 2018-07-10 stsp struct got_object_id *id;
1900 4ed7e80c 2018-05-20 stsp static const struct got_error *
1901 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1902 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1903 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1904 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1906 84451b3e 2018-07-10 stsp const struct got_error *err;
1907 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1908 84451b3e 2018-07-10 stsp char *line;
1909 84451b3e 2018-07-10 stsp size_t len;
1910 84451b3e 2018-07-10 stsp wchar_t *wline;
1911 b700b5d6 2018-07-10 stsp int width, wlimit;
1912 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1913 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1914 ab089a2a 2018-07-12 stsp char *id_str;
1916 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1918 ab089a2a 2018-07-12 stsp return err;
1920 84451b3e 2018-07-10 stsp rewind(f);
1921 f7d12f7e 2018-08-01 stsp werase(view->window);
1923 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1924 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1925 ab089a2a 2018-07-12 stsp free(id_str);
1926 ab089a2a 2018-07-12 stsp return err;
1929 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1930 ab089a2a 2018-07-12 stsp free(line);
1931 2550e4c3 2018-07-13 stsp line = NULL;
1932 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1933 a3404814 2018-09-02 stsp wstandout(view->window);
1934 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1935 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1936 a3404814 2018-09-02 stsp wstandend(view->window);
1937 2550e4c3 2018-07-13 stsp free(wline);
1938 2550e4c3 2018-07-13 stsp wline = NULL;
1939 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1940 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1942 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1943 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1944 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1945 ab089a2a 2018-07-12 stsp free(id_str);
1946 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1948 ab089a2a 2018-07-12 stsp free(id_str);
1949 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1950 3f60a8ef 2018-07-10 stsp free(line);
1951 2550e4c3 2018-07-13 stsp line = NULL;
1953 3f60a8ef 2018-07-10 stsp return err;
1954 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1955 2550e4c3 2018-07-13 stsp free(wline);
1956 2550e4c3 2018-07-13 stsp wline = NULL;
1957 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1958 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1961 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1962 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1963 84451b3e 2018-07-10 stsp if (line == NULL) {
1967 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1968 84451b3e 2018-07-10 stsp free(line);
1972 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1973 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1974 84451b3e 2018-07-10 stsp if (err) {
1975 84451b3e 2018-07-10 stsp free(line);
1976 84451b3e 2018-07-10 stsp return err;
1979 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
1980 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1982 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1983 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1984 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1985 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1986 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1987 84451b3e 2018-07-10 stsp char *id_str;
1988 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1989 84451b3e 2018-07-10 stsp if (err) {
1990 84451b3e 2018-07-10 stsp free(line);
1991 2550e4c3 2018-07-13 stsp free(wline);
1992 84451b3e 2018-07-10 stsp return err;
1994 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1995 84451b3e 2018-07-10 stsp free(id_str);
1996 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1998 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
1999 ee41ec32 2018-07-10 stsp prev_id = NULL;
2002 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2003 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2004 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2007 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2008 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2009 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2010 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2011 84451b3e 2018-07-10 stsp free(line);
2012 2550e4c3 2018-07-13 stsp free(wline);
2013 2550e4c3 2018-07-13 stsp wline = NULL;
2015 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2017 1a57306a 2018-09-02 stsp view_vborder(view);
2019 84451b3e 2018-07-10 stsp return NULL;
2022 84451b3e 2018-07-10 stsp static const struct got_error *
2023 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2025 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2026 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2027 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2029 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2030 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2031 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2033 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
2034 84451b3e 2018-07-10 stsp return got_error_from_errno();
2036 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2037 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2038 d68a0a7d 2018-07-10 stsp goto done;
2041 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2042 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2044 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2045 d68a0a7d 2018-07-10 stsp if (line->annotated)
2046 d68a0a7d 2018-07-10 stsp goto done;
2048 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2049 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2050 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2051 84451b3e 2018-07-10 stsp goto done;
2053 84451b3e 2018-07-10 stsp line->annotated = 1;
2055 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
2056 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
2057 e5a0f69f 2018-08-18 stsp a->last_displayed_line, a->eof, a->view->nlines);
2059 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
2060 84451b3e 2018-07-10 stsp return got_error_from_errno();
2061 84451b3e 2018-07-10 stsp return err;
2064 84451b3e 2018-07-10 stsp static void *
2065 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2067 18430de3 2018-07-10 stsp const struct got_error *err;
2068 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
2069 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
2071 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2072 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
2074 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
2075 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
2077 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
2078 c9beca56 2018-07-22 stsp ta->repo = NULL;
2079 c9beca56 2018-07-22 stsp *ta->complete = 1;
2081 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
2082 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
2083 e5a0f69f 2018-08-18 stsp a->first_displayed_line, a->last_displayed_line, a->eof,
2084 f7d12f7e 2018-08-01 stsp a->view->nlines);
2086 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
2087 18430de3 2018-07-10 stsp err = got_error_from_errno();
2089 18430de3 2018-07-10 stsp return (void *)err;
2092 245d91c1 2018-07-12 stsp static struct got_object_id *
2093 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
2094 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
2096 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2098 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2099 245d91c1 2018-07-12 stsp if (!line->annotated)
2100 245d91c1 2018-07-12 stsp return NULL;
2102 245d91c1 2018-07-12 stsp return line->id;
2105 84451b3e 2018-07-10 stsp static const struct got_error *
2106 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
2107 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
2108 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
2110 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
2111 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
2112 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
2113 b880a918 2018-07-10 stsp struct got_object_qid *pid;
2115 b880a918 2018-07-10 stsp *pobj = NULL;
2116 b880a918 2018-07-10 stsp *obj = NULL;
2118 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
2119 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
2120 245d91c1 2018-07-12 stsp if (selected_id == NULL)
2121 b880a918 2018-07-10 stsp return NULL;
2123 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
2125 b880a918 2018-07-10 stsp goto done;
2127 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
2129 b880a918 2018-07-10 stsp goto done;
2131 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
2132 b880a918 2018-07-10 stsp if (pid) {
2133 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
2135 b880a918 2018-07-10 stsp goto done;
2138 b880a918 2018-07-10 stsp if (commit)
2139 b880a918 2018-07-10 stsp got_object_commit_close(commit);
2140 b880a918 2018-07-10 stsp return err;
2143 b880a918 2018-07-10 stsp static const struct got_error *
2144 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2146 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2149 245d91c1 2018-07-12 stsp if (blame->thread) {
2150 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
2151 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2152 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2153 245d91c1 2018-07-12 stsp err = NULL;
2154 245d91c1 2018-07-12 stsp blame->thread = NULL;
2156 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2157 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2158 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2160 245d91c1 2018-07-12 stsp if (blame->f) {
2161 245d91c1 2018-07-12 stsp fclose(blame->f);
2162 245d91c1 2018-07-12 stsp blame->f = NULL;
2164 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
2165 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
2166 245d91c1 2018-07-12 stsp free(blame->lines);
2167 245d91c1 2018-07-12 stsp blame->lines = NULL;
2168 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2169 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2171 245d91c1 2018-07-12 stsp return err;
2174 245d91c1 2018-07-12 stsp static const struct got_error *
2175 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2176 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
2177 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
2178 e5a0f69f 2018-08-18 stsp int *selected_line, int *done, int *eof, const char *path,
2179 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
2180 245d91c1 2018-07-12 stsp struct got_repository *repo)
2182 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2183 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2184 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2185 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2186 27d434c2 2018-09-15 stsp struct got_object *obj = NULL;
2188 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2190 27d434c2 2018-09-15 stsp goto done;
2192 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
2194 84451b3e 2018-07-10 stsp goto done;
2196 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2197 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2198 84451b3e 2018-07-10 stsp goto done;
2201 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
2203 a70480e0 2018-06-23 stsp goto done;
2204 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2205 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2206 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2207 84451b3e 2018-07-10 stsp goto done;
2209 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2210 245d91c1 2018-07-12 stsp blame->f, blob);
2212 84451b3e 2018-07-10 stsp goto done;
2214 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2215 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2216 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2217 84451b3e 2018-07-10 stsp goto done;
2220 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2222 bd24772e 2018-07-11 stsp goto done;
2224 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2225 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2226 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2227 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
2228 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2229 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2230 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2231 245d91c1 2018-07-12 stsp goto done;
2233 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
2234 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
2235 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
2236 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
2237 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
2238 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2239 e5a0f69f 2018-08-18 stsp blame->cb_args.eof = eof;
2241 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2242 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2243 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2244 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2245 245d91c1 2018-07-12 stsp *blame_complete = 0;
2247 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
2248 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
2249 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2250 245d91c1 2018-07-12 stsp goto done;
2255 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2256 27d434c2 2018-09-15 stsp free(obj_id);
2258 245d91c1 2018-07-12 stsp got_object_close(obj);
2260 245d91c1 2018-07-12 stsp stop_blame(blame);
2261 245d91c1 2018-07-12 stsp return err;
2264 245d91c1 2018-07-12 stsp static const struct got_error *
2265 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2266 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2268 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2269 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2271 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2273 fb2756b9 2018-08-04 stsp if (pthread_mutex_init(&s->mutex, NULL) != 0)
2274 7cbe629d 2018-08-04 stsp return got_error_from_errno();
2276 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2278 7cbe629d 2018-08-04 stsp return err;
2280 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2281 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2282 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2283 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2284 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2285 fb2756b9 2018-08-04 stsp s->path = path;
2286 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2287 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2288 fb2756b9 2018-08-04 stsp s->repo = repo;
2289 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2290 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2292 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2293 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2294 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2296 e5a0f69f 2018-08-18 stsp return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2297 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2298 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2299 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2302 e5a0f69f 2018-08-18 stsp static const struct got_error *
2303 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2305 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2306 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2308 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2309 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2311 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2312 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2313 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2314 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2315 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2318 e5a0f69f 2018-08-18 stsp free(s->path);
2320 e5a0f69f 2018-08-18 stsp return err;
2323 7cbe629d 2018-08-04 stsp static const struct got_error *
2324 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2326 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2327 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2329 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0)
2330 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2332 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2333 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2334 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2335 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2337 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2338 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2340 669b5ffa 2018-10-07 stsp view_vborder(view);
2341 e5a0f69f 2018-08-18 stsp return err;
2344 e5a0f69f 2018-08-18 stsp static const struct got_error *
2345 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2346 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2348 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2349 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
2350 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2351 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2352 669b5ffa 2018-10-07 stsp int begin_x = 0;
2354 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2355 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2356 e5a0f69f 2018-08-18 stsp goto done;
2359 e5a0f69f 2018-08-18 stsp switch (ch) {
2361 e5a0f69f 2018-08-18 stsp s->done = 1;
2362 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2363 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2364 e5a0f69f 2018-08-18 stsp goto done;
2366 e5a0f69f 2018-08-18 stsp return stop_blame(&s->blame);
2368 e5a0f69f 2018-08-18 stsp case KEY_UP:
2369 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2370 e5a0f69f 2018-08-18 stsp s->selected_line--;
2371 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2372 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2373 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2375 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2376 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2377 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2380 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2381 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2382 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2384 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2387 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2388 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2389 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2390 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2391 e5a0f69f 2018-08-18 stsp s->selected_line++;
2392 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2393 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2394 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2397 e5a0f69f 2018-08-18 stsp case 'p': {
2398 e5a0f69f 2018-08-18 stsp struct got_object_id *id;
2399 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2400 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2401 e5a0f69f 2018-08-18 stsp if (id == NULL || got_object_id_cmp(id,
2402 e5a0f69f 2018-08-18 stsp s->blamed_commit->id) == 0)
2404 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2405 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2406 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2409 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2411 e5a0f69f 2018-08-18 stsp if (ch == 'p' && pobj == NULL)
2413 e5a0f69f 2018-08-18 stsp s->done = 1;
2414 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2415 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2416 e5a0f69f 2018-08-18 stsp goto done;
2418 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2419 e5a0f69f 2018-08-18 stsp s->done = 0;
2420 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2421 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2422 e5a0f69f 2018-08-18 stsp goto done;
2424 e5a0f69f 2018-08-18 stsp if (thread_err)
2426 e5a0f69f 2018-08-18 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
2427 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2428 e5a0f69f 2018-08-18 stsp obj = NULL;
2429 e5a0f69f 2018-08-18 stsp if (pobj) {
2430 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2431 e5a0f69f 2018-08-18 stsp pobj = NULL;
2433 6402fb3c 2018-09-15 stsp err = got_object_qid_alloc(&s->blamed_commit, id);
2435 e5a0f69f 2018-08-18 stsp goto done;
2436 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2437 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2438 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2439 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2440 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2441 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2442 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2443 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2448 e5a0f69f 2018-08-18 stsp case 'B': {
2449 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2450 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2451 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2453 e5a0f69f 2018-08-18 stsp s->done = 1;
2454 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2455 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2456 e5a0f69f 2018-08-18 stsp goto done;
2458 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2459 e5a0f69f 2018-08-18 stsp s->done = 0;
2460 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2461 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2462 e5a0f69f 2018-08-18 stsp goto done;
2464 e5a0f69f 2018-08-18 stsp if (thread_err)
2466 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2467 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2468 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2469 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2470 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2471 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2472 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2473 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2474 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2475 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2480 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2481 e5a0f69f 2018-08-18 stsp case '\r':
2482 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2483 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2484 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2487 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2490 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
2491 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
2492 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2493 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2494 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2497 669b5ffa 2018-10-07 stsp err = open_diff_view(diff_view, pobj, obj, s->repo);
2498 e5a0f69f 2018-08-18 stsp if (err) {
2499 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2502 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
2503 669b5ffa 2018-10-07 stsp err = view_close_child(view);
2505 669b5ffa 2018-10-07 stsp return err;
2506 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
2507 669b5ffa 2018-10-07 stsp if (err) {
2508 669b5ffa 2018-10-07 stsp view_close(diff_view);
2511 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(diff_view)) {
2512 669b5ffa 2018-10-07 stsp *focus_view = diff_view;
2513 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
2516 669b5ffa 2018-10-07 stsp *new_view = diff_view;
2517 e5a0f69f 2018-08-18 stsp if (pobj) {
2518 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2519 e5a0f69f 2018-08-18 stsp pobj = NULL;
2521 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2522 e5a0f69f 2018-08-18 stsp obj = NULL;
2526 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2528 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2529 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2530 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2531 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2534 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2535 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2536 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2537 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2539 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2540 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2541 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2543 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2544 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2545 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2546 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2553 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0)
2554 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2557 b880a918 2018-07-10 stsp got_object_close(pobj);
2558 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2561 a70480e0 2018-06-23 stsp static const struct got_error *
2562 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2564 a70480e0 2018-06-23 stsp const struct got_error *error;
2565 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2566 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2567 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2568 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2570 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2572 a70480e0 2018-06-23 stsp #ifndef PROFILE
2573 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2575 a70480e0 2018-06-23 stsp err(1, "pledge");
2578 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2579 a70480e0 2018-06-23 stsp switch (ch) {
2581 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2584 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2585 69069811 2018-08-02 stsp if (repo_path == NULL)
2586 69069811 2018-08-02 stsp err(1, "-r option");
2590 a70480e0 2018-06-23 stsp /* NOTREACHED */
2594 a70480e0 2018-06-23 stsp argc -= optind;
2595 a70480e0 2018-06-23 stsp argv += optind;
2597 69069811 2018-08-02 stsp if (argc == 1)
2598 69069811 2018-08-02 stsp path = argv[0];
2600 a70480e0 2018-06-23 stsp usage_blame();
2602 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2603 69069811 2018-08-02 stsp if (cwd == NULL) {
2604 69069811 2018-08-02 stsp error = got_error_from_errno();
2605 69069811 2018-08-02 stsp goto done;
2607 69069811 2018-08-02 stsp if (repo_path == NULL) {
2608 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2609 69069811 2018-08-02 stsp if (repo_path == NULL) {
2610 69069811 2018-08-02 stsp error = got_error_from_errno();
2611 69069811 2018-08-02 stsp goto done;
2616 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2617 a70480e0 2018-06-23 stsp if (error != NULL)
2618 66b4983c 2018-06-23 stsp return error;
2620 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2621 69069811 2018-08-02 stsp if (error != NULL)
2622 69069811 2018-08-02 stsp goto done;
2624 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2625 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2626 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2627 a70480e0 2018-06-23 stsp if (error != NULL)
2628 66b4983c 2018-06-23 stsp goto done;
2629 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2630 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2632 a70480e0 2018-06-23 stsp struct got_object *obj;
2633 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2634 a70480e0 2018-06-23 stsp if (error != NULL)
2635 66b4983c 2018-06-23 stsp goto done;
2636 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
2637 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2638 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2639 a19e88aa 2018-06-23 stsp got_object_close(obj);
2641 a19e88aa 2018-06-23 stsp if (error != NULL)
2642 a19e88aa 2018-06-23 stsp goto done;
2644 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2645 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2646 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2647 e1cd8fed 2018-08-01 stsp goto done;
2649 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2650 7cbe629d 2018-08-04 stsp if (error)
2651 7cbe629d 2018-08-04 stsp goto done;
2652 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2654 69069811 2018-08-02 stsp free(repo_path);
2655 69069811 2018-08-02 stsp free(cwd);
2656 a70480e0 2018-06-23 stsp free(commit_id);
2658 a70480e0 2018-06-23 stsp got_repo_close(repo);
2659 a70480e0 2018-06-23 stsp return error;
2662 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2663 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2664 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2665 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2666 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2667 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2668 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2670 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2671 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2672 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2673 ffd1d5e5 2018-06-23 stsp int width, n;
2675 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2677 f7d12f7e 2018-08-01 stsp werase(view->window);
2679 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2680 ffd1d5e5 2018-06-23 stsp return NULL;
2682 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2684 ffd1d5e5 2018-06-23 stsp return err;
2685 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2686 a3404814 2018-09-02 stsp wstandout(view->window);
2687 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2688 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2689 a3404814 2018-09-02 stsp wstandend(view->window);
2690 2550e4c3 2018-07-13 stsp free(wline);
2691 2550e4c3 2018-07-13 stsp wline = NULL;
2692 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2693 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2694 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2695 ffd1d5e5 2018-06-23 stsp return NULL;
2696 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2698 ce52c690 2018-06-23 stsp return err;
2699 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2700 2550e4c3 2018-07-13 stsp free(wline);
2701 2550e4c3 2018-07-13 stsp wline = NULL;
2702 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2703 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2704 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2705 ffd1d5e5 2018-06-23 stsp return NULL;
2706 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2707 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2708 a1eca9bb 2018-06-23 stsp return NULL;
2710 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2711 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2712 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2713 0cf4efb1 2018-09-29 stsp if (view->focussed)
2714 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2715 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2717 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2718 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
2719 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2720 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2721 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2722 ffd1d5e5 2018-06-23 stsp return NULL;
2726 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2727 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2730 ffd1d5e5 2018-06-23 stsp while (te) {
2731 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2733 1d13200f 2018-07-12 stsp if (show_ids) {
2734 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2736 1d13200f 2018-07-12 stsp return got_error_from_errno();
2738 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2739 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2740 1d13200f 2018-07-12 stsp free(id_str);
2741 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2743 1d13200f 2018-07-12 stsp free(id_str);
2744 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2745 ffd1d5e5 2018-06-23 stsp if (err) {
2746 ffd1d5e5 2018-06-23 stsp free(line);
2749 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2750 0cf4efb1 2018-09-29 stsp if (view->focussed)
2751 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2752 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2754 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2755 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2756 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2757 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
2758 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2759 ffd1d5e5 2018-06-23 stsp free(line);
2760 2550e4c3 2018-07-13 stsp free(wline);
2761 2550e4c3 2018-07-13 stsp wline = NULL;
2763 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2764 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2765 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2767 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2770 ffd1d5e5 2018-06-23 stsp return err;
2773 ffd1d5e5 2018-06-23 stsp static void
2774 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2775 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2777 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2780 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2783 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2784 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2785 ffd1d5e5 2018-06-23 stsp if (!isroot)
2786 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2790 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2791 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2792 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2793 ffd1d5e5 2018-06-23 stsp prev = te;
2794 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2796 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2797 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2799 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2800 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2803 ffd1d5e5 2018-06-23 stsp static void
2804 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2805 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2806 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2808 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2809 ffd1d5e5 2018-06-23 stsp int n = 0;
2811 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2814 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2815 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2817 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2818 ffd1d5e5 2018-06-23 stsp while (next) {
2819 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2820 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2822 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2826 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2827 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2828 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2830 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2831 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2832 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2834 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2835 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2837 ce52c690 2018-06-23 stsp len += strlen(te->name);
2839 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2840 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2841 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2843 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2844 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2845 d9765a41 2018-06-23 stsp while (pt) {
2846 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2847 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2848 cb2ebc8a 2018-06-23 stsp goto done;
2850 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2851 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2852 cb2ebc8a 2018-06-23 stsp goto done;
2854 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2857 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2858 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2859 ce52c690 2018-06-23 stsp goto done;
2863 ce52c690 2018-06-23 stsp if (err) {
2864 ce52c690 2018-06-23 stsp free(*path);
2865 ce52c690 2018-06-23 stsp *path = NULL;
2867 ce52c690 2018-06-23 stsp return err;
2870 ce52c690 2018-06-23 stsp static const struct got_error *
2871 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
2872 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2873 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2875 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2876 ce52c690 2018-06-23 stsp char *path;
2877 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
2879 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2881 ce52c690 2018-06-23 stsp return err;
2883 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
2884 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
2885 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2887 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
2888 e5a0f69f 2018-08-18 stsp if (err) {
2889 e5a0f69f 2018-08-18 stsp view_close(blame_view);
2890 e5a0f69f 2018-08-18 stsp free(path);
2892 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
2893 69efd4c4 2018-07-18 stsp return err;
2896 69efd4c4 2018-07-18 stsp static const struct got_error *
2897 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
2898 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2899 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2901 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
2902 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2903 69efd4c4 2018-07-18 stsp char *path;
2905 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
2906 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
2907 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2909 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2911 69efd4c4 2018-07-18 stsp return err;
2913 e5a0f69f 2018-08-18 stsp err = open_log_view(log_view, commit_id, repo, path);
2915 e5a0f69f 2018-08-18 stsp view_close(log_view);
2917 e5a0f69f 2018-08-18 stsp *new_view = log_view;
2918 cb2ebc8a 2018-06-23 stsp free(path);
2919 cb2ebc8a 2018-06-23 stsp return err;
2922 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2923 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
2924 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2926 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2927 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
2928 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2930 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
2932 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2933 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2934 ffd1d5e5 2018-06-23 stsp goto done;
2936 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2937 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2938 ffd1d5e5 2018-06-23 stsp goto done;
2941 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
2942 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
2943 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2944 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
2945 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
2946 6484ec90 2018-09-29 stsp err = got_error_from_errno();
2947 6484ec90 2018-09-29 stsp goto done;
2949 fb2756b9 2018-08-04 stsp s->repo = repo;
2951 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
2952 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
2953 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
2955 ad80ab7b 2018-08-04 stsp free(commit_id_str);
2956 6484ec90 2018-09-29 stsp if (err) {
2957 fb2756b9 2018-08-04 stsp free(s->tree_label);
2958 6484ec90 2018-09-29 stsp s->tree_label = NULL;
2960 ad80ab7b 2018-08-04 stsp return err;
2963 e5a0f69f 2018-08-18 stsp static const struct got_error *
2964 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
2966 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2968 fb2756b9 2018-08-04 stsp free(s->tree_label);
2969 6484ec90 2018-09-29 stsp s->tree_label = NULL;
2970 6484ec90 2018-09-29 stsp free(s->commit_id);
2971 6484ec90 2018-09-29 stsp s->commit_id = NULL;
2972 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
2973 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
2974 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
2975 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
2976 ad80ab7b 2018-08-04 stsp free(parent);
2979 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
2980 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
2981 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
2983 e5a0f69f 2018-08-18 stsp return NULL;
2986 ad80ab7b 2018-08-04 stsp static const struct got_error *
2987 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
2989 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
2990 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2991 e5a0f69f 2018-08-18 stsp char *parent_path;
2993 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
2995 e5a0f69f 2018-08-18 stsp return err;
2997 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
2998 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
2999 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3000 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
3001 e5a0f69f 2018-08-18 stsp free(parent_path);
3003 669b5ffa 2018-10-07 stsp view_vborder(view);
3004 e5a0f69f 2018-08-18 stsp return err;
3007 e5a0f69f 2018-08-18 stsp static const struct got_error *
3008 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3009 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3011 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3012 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
3013 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
3014 669b5ffa 2018-10-07 stsp int begin_x = 0;
3016 e5a0f69f 2018-08-18 stsp switch (ch) {
3018 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
3021 669b5ffa 2018-10-07 stsp if (!s->selected_entry)
3023 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
3024 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
3025 669b5ffa 2018-10-07 stsp err = log_tree_entry(&log_view, begin_x,
3026 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
3027 669b5ffa 2018-10-07 stsp s->commit_id, s->repo);
3028 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3029 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3031 669b5ffa 2018-10-07 stsp return err;
3032 669b5ffa 2018-10-07 stsp err = view_set_child(view, log_view);
3033 669b5ffa 2018-10-07 stsp if (err) {
3034 669b5ffa 2018-10-07 stsp view_close(log_view);
3037 669b5ffa 2018-10-07 stsp *focus_view = log_view;
3038 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3040 669b5ffa 2018-10-07 stsp *new_view = log_view;
3043 e5a0f69f 2018-08-18 stsp case KEY_UP:
3044 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3045 e5a0f69f 2018-08-18 stsp s->selected--;
3046 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3048 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
3049 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
3051 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
3052 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
3053 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
3054 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
3055 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3056 e5a0f69f 2018-08-18 stsp s->selected = 0;
3059 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
3060 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
3061 e5a0f69f 2018-08-18 stsp s->tree == s->root);
3064 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
3065 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
3066 e5a0f69f 2018-08-18 stsp s->selected++;
3069 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
3070 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
3072 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
3073 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
3074 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
3075 e5a0f69f 2018-08-18 stsp s->entries);
3076 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
3079 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
3080 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
3081 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3083 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
3084 e5a0f69f 2018-08-18 stsp case '\r':
3085 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
3086 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3087 7837eeac 2018-09-24 stsp case KEY_BACKSPACE:
3088 e5a0f69f 2018-08-18 stsp /* user selected '..' */
3089 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
3091 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
3092 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
3094 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
3095 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
3096 e5a0f69f 2018-08-18 stsp s->entries =
3097 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3098 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
3099 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
3100 e5a0f69f 2018-08-18 stsp s->selected_entry =
3101 e5a0f69f 2018-08-18 stsp parent->selected_entry;
3102 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
3103 e5a0f69f 2018-08-18 stsp free(parent);
3104 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
3105 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3106 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
3107 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
3108 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
3111 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
3112 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
3113 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
3116 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
3117 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
3118 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
3119 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
3120 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
3121 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3122 e5a0f69f 2018-08-18 stsp s->tree = child;
3123 e5a0f69f 2018-08-18 stsp s->entries =
3124 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3125 e5a0f69f 2018-08-18 stsp s->selected = 0;
3126 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3127 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
3128 669b5ffa 2018-10-07 stsp struct tog_view *blame_view;
3129 669b5ffa 2018-10-07 stsp int begin_x = view_is_parent_view(view) ?
3130 669b5ffa 2018-10-07 stsp view_split_begin_x(view->begin_x) : 0;
3132 669b5ffa 2018-10-07 stsp err = blame_tree_entry(&blame_view, begin_x,
3133 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents, s->commit_id,
3137 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3138 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3140 669b5ffa 2018-10-07 stsp return err;
3141 669b5ffa 2018-10-07 stsp err = view_set_child(view, blame_view);
3142 669b5ffa 2018-10-07 stsp if (err) {
3143 669b5ffa 2018-10-07 stsp view_close(blame_view);
3146 669b5ffa 2018-10-07 stsp *focus_view = blame_view;
3147 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3149 669b5ffa 2018-10-07 stsp *new_view = blame_view;
3152 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3153 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3154 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3160 ffd1d5e5 2018-06-23 stsp return err;
3163 ffd1d5e5 2018-06-23 stsp __dead static void
3164 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3167 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3168 ffd1d5e5 2018-06-23 stsp getprogname());
3172 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3173 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3175 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3176 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3177 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3178 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3179 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3180 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3181 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3183 5221c383 2018-08-01 stsp struct tog_view *view;
3185 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3186 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3188 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3191 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3192 ffd1d5e5 2018-06-23 stsp switch (ch) {
3194 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3198 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3202 ffd1d5e5 2018-06-23 stsp argc -= optind;
3203 ffd1d5e5 2018-06-23 stsp argv += optind;
3205 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3206 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
3207 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3208 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3209 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3210 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3211 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3212 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3214 ffd1d5e5 2018-06-23 stsp usage_log();
3216 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3217 ffd1d5e5 2018-06-23 stsp free(repo_path);
3218 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3219 ffd1d5e5 2018-06-23 stsp return error;
3221 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
3222 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3223 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3224 ffd1d5e5 2018-06-23 stsp goto done;
3226 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
3227 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3228 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
3229 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
3230 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
3231 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
3234 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3235 ffd1d5e5 2018-06-23 stsp goto done;
3237 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3238 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3239 ffd1d5e5 2018-06-23 stsp goto done;
3241 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3242 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3243 ffd1d5e5 2018-06-23 stsp goto done;
3245 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3246 5221c383 2018-08-01 stsp if (view == NULL) {
3247 5221c383 2018-08-01 stsp error = got_error_from_errno();
3248 5221c383 2018-08-01 stsp goto done;
3250 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3251 ad80ab7b 2018-08-04 stsp if (error)
3252 ad80ab7b 2018-08-04 stsp goto done;
3253 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3255 ffd1d5e5 2018-06-23 stsp free(commit_id);
3256 ffd1d5e5 2018-06-23 stsp if (commit)
3257 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3259 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3261 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3262 ffd1d5e5 2018-06-23 stsp return error;
3264 5c5136c5 2018-05-20 stsp static void
3265 9f7d7167 2018-04-29 stsp init_curses(void)
3267 9f7d7167 2018-04-29 stsp initscr();
3271 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3272 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3273 1f475ad8 2018-05-10 stsp curs_set(0);
3276 4ed7e80c 2018-05-20 stsp __dead static void
3277 9f7d7167 2018-04-29 stsp usage(void)
3281 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3282 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3283 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3284 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3285 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3290 c2301be8 2018-04-30 stsp static char **
3291 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3293 c2301be8 2018-04-30 stsp char **argv;
3294 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3296 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3297 c2301be8 2018-04-30 stsp if (argv == NULL)
3298 c2301be8 2018-04-30 stsp err(1, "calloc");
3299 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3300 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3301 c2301be8 2018-04-30 stsp err(1, "calloc");
3302 c2301be8 2018-04-30 stsp if (arg1) {
3303 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3304 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3305 c2301be8 2018-04-30 stsp err(1, "calloc");
3308 c2301be8 2018-04-30 stsp return argv;
3312 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3314 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3315 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3316 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3317 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3319 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
3321 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3322 9f7d7167 2018-04-29 stsp switch (ch) {
3324 9f7d7167 2018-04-29 stsp hflag = 1;
3328 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3332 9f7d7167 2018-04-29 stsp argc -= optind;
3333 9f7d7167 2018-04-29 stsp argv += optind;
3334 9f7d7167 2018-04-29 stsp optind = 0;
3335 c2301be8 2018-04-30 stsp optreset = 1;
3337 c2301be8 2018-04-30 stsp if (argc == 0) {
3338 f29d3e89 2018-06-23 stsp if (hflag)
3340 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3341 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3342 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3347 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3348 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3349 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3350 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3351 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3352 9f7d7167 2018-04-29 stsp if (hflag)
3353 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3357 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3358 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3359 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3360 c2301be8 2018-04-30 stsp if (repo_path) {
3361 c2301be8 2018-04-30 stsp struct got_repository *repo;
3362 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3363 c2301be8 2018-04-30 stsp if (error == NULL)
3364 c2301be8 2018-04-30 stsp got_repo_close(repo);
3366 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3367 c2301be8 2018-04-30 stsp if (error) {
3368 f29d3e89 2018-06-23 stsp if (hflag) {
3369 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3370 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3374 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3375 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3376 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3377 ad7de8d9 2018-04-30 stsp free(repo_path);
3380 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3381 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3383 c2301be8 2018-04-30 stsp free(repo_path);
3387 5c5136c5 2018-05-20 stsp init_curses();
3389 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3390 9f7d7167 2018-04-29 stsp if (error)
3391 9f7d7167 2018-04-29 stsp goto done;
3394 c2301be8 2018-04-30 stsp free(cmd_argv);
3395 9f7d7167 2018-04-29 stsp if (error)
3396 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);