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:
1294 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1295 e5a0f69f 2018-08-18 stsp s->selected--;
1296 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1298 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1299 e5a0f69f 2018-08-18 stsp &s->commits);
1301 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1302 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1303 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1304 e5a0f69f 2018-08-18 stsp s->selected = 0;
1307 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1308 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1311 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1313 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1314 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1315 e5a0f69f 2018-08-18 stsp s->selected++;
1318 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1319 93e45b7c 2018-09-24 stsp &s->last_displayed_entry, &s->commits,
1320 e5a0f69f 2018-08-18 stsp s->graph, s->repo, s->in_repo_path);
1321 e5a0f69f 2018-08-18 stsp if (err) {
1322 4d7951a5 2018-08-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1324 4d7951a5 2018-08-04 stsp err = NULL;
1327 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1328 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1329 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1330 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1331 93e45b7c 2018-09-24 stsp view->nlines, &s->last_displayed_entry,
1332 e5a0f69f 2018-08-18 stsp &s->commits, s->graph, s->repo,
1333 e5a0f69f 2018-08-18 stsp s->in_repo_path);
1334 93e45b7c 2018-09-24 stsp if (err && err->code != GOT_ERR_ITER_COMPLETED)
1336 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1337 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1338 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1339 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1340 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1341 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1343 e5a0f69f 2018-08-18 stsp err = NULL;
1346 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1347 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1348 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1349 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1350 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1352 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1353 e5a0f69f 2018-08-18 stsp case '\r':
1354 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1355 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1356 669b5ffa 2018-10-07 stsp err = open_diff_view_for_commit(&diff_view, begin_x,
1357 2a4718d3 2018-09-29 stsp s->selected_entry->id, s->selected_entry->commit,
1361 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1362 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1364 669b5ffa 2018-10-07 stsp return err;
1365 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
1366 669b5ffa 2018-10-07 stsp if (err) {
1367 669b5ffa 2018-10-07 stsp view_close(diff_view);
1370 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(diff_view)) {
1371 669b5ffa 2018-10-07 stsp *focus_view = diff_view;
1372 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1375 669b5ffa 2018-10-07 stsp *new_view = diff_view;
1378 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1379 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1380 669b5ffa 2018-10-07 stsp err = browse_commit(&tree_view, begin_x,
1381 0cf4efb1 2018-09-29 stsp s->selected_entry, s->repo);
1382 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1383 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1385 669b5ffa 2018-10-07 stsp return err;
1386 669b5ffa 2018-10-07 stsp err = view_set_child(view, tree_view);
1387 669b5ffa 2018-10-07 stsp if (err) {
1388 669b5ffa 2018-10-07 stsp view_close(tree_view);
1391 669b5ffa 2018-10-07 stsp *focus_view = tree_view;
1392 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1394 669b5ffa 2018-10-07 stsp *new_view = tree_view;
1396 5036bf37 2018-09-24 stsp case KEY_BACKSPACE:
1397 5036bf37 2018-09-24 stsp if (strcmp(s->in_repo_path, "/") == 0)
1399 5036bf37 2018-09-24 stsp parent_path = dirname(s->in_repo_path);
1400 5036bf37 2018-09-24 stsp if (parent_path && strcmp(parent_path, ".") != 0) {
1401 5036bf37 2018-09-24 stsp struct tog_view *lv;
1402 0cf4efb1 2018-09-29 stsp lv = view_open(view->nlines, view->ncols,
1403 0cf4efb1 2018-09-29 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
1404 5036bf37 2018-09-24 stsp if (lv == NULL)
1405 5036bf37 2018-09-24 stsp return got_error_from_errno();
1406 5036bf37 2018-09-24 stsp err = open_log_view(lv, s->start_id, s->repo,
1407 5036bf37 2018-09-24 stsp parent_path);
1410 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1411 669b5ffa 2018-10-07 stsp *new_view = lv;
1413 669b5ffa 2018-10-07 stsp view_set_child(view->parent, lv);
1414 669b5ffa 2018-10-07 stsp *dead_view = view;
1422 80ddbec8 2018-04-29 stsp return err;
1425 4ed7e80c 2018-05-20 stsp static const struct got_error *
1426 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1428 80ddbec8 2018-04-29 stsp const struct got_error *error;
1429 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1430 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1431 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1432 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1434 04cc582a 2018-08-01 stsp struct tog_view *view;
1436 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1437 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1439 80ddbec8 2018-04-29 stsp err(1, "pledge");
1442 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1443 80ddbec8 2018-04-29 stsp switch (ch) {
1445 80ddbec8 2018-04-29 stsp start_commit = optarg;
1448 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1449 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1450 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1454 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1458 80ddbec8 2018-04-29 stsp argc -= optind;
1459 80ddbec8 2018-04-29 stsp argv += optind;
1461 ecb28ae0 2018-07-16 stsp if (argc == 0)
1462 ecb28ae0 2018-07-16 stsp path = strdup("");
1463 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1464 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1466 80ddbec8 2018-04-29 stsp usage_log();
1467 ecb28ae0 2018-07-16 stsp if (path == NULL)
1468 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1470 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1471 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1472 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1473 ecb28ae0 2018-07-16 stsp goto done;
1475 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1476 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1477 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1478 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1479 ecb28ae0 2018-07-16 stsp goto done;
1483 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1484 80ddbec8 2018-04-29 stsp if (error != NULL)
1485 ecb28ae0 2018-07-16 stsp goto done;
1487 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1488 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1489 80ddbec8 2018-04-29 stsp if (error != NULL)
1490 ecb28ae0 2018-07-16 stsp goto done;
1492 80ddbec8 2018-04-29 stsp struct got_object *obj;
1493 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1494 80ddbec8 2018-04-29 stsp if (error == NULL) {
1495 6402fb3c 2018-09-15 stsp start_id = got_object_id_dup(got_object_get_id(obj));
1496 899d86c2 2018-05-10 stsp if (start_id == NULL)
1497 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1498 ecb28ae0 2018-07-16 stsp goto done;
1501 80ddbec8 2018-04-29 stsp if (error != NULL)
1502 ecb28ae0 2018-07-16 stsp goto done;
1504 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1505 04cc582a 2018-08-01 stsp if (view == NULL) {
1506 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1507 04cc582a 2018-08-01 stsp goto done;
1509 ba4f502b 2018-08-04 stsp error = open_log_view(view, start_id, repo, path);
1510 ba4f502b 2018-08-04 stsp if (error)
1511 ba4f502b 2018-08-04 stsp goto done;
1512 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1514 ecb28ae0 2018-07-16 stsp free(repo_path);
1515 ecb28ae0 2018-07-16 stsp free(cwd);
1516 ecb28ae0 2018-07-16 stsp free(path);
1517 899d86c2 2018-05-10 stsp free(start_id);
1519 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1520 80ddbec8 2018-04-29 stsp return error;
1523 4ed7e80c 2018-05-20 stsp __dead static void
1524 9f7d7167 2018-04-29 stsp usage_diff(void)
1527 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1528 9f7d7167 2018-04-29 stsp getprogname());
1532 b304db33 2018-05-20 stsp static char *
1533 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1535 b304db33 2018-05-20 stsp char *line;
1536 b304db33 2018-05-20 stsp size_t linelen;
1537 b304db33 2018-05-20 stsp size_t lineno;
1538 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1540 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1542 b304db33 2018-05-20 stsp *len = linelen;
1543 b304db33 2018-05-20 stsp return line;
1546 4ed7e80c 2018-05-20 stsp static const struct got_error *
1547 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1548 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1549 a3404814 2018-09-02 stsp char * header)
1551 61e69b96 2018-05-20 stsp const struct got_error *err;
1552 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1553 b304db33 2018-05-20 stsp char *line;
1554 b304db33 2018-05-20 stsp size_t len;
1555 61e69b96 2018-05-20 stsp wchar_t *wline;
1556 e0b650dd 2018-05-20 stsp int width;
1558 26ed57b2 2018-05-19 stsp rewind(f);
1559 f7d12f7e 2018-08-01 stsp werase(view->window);
1561 a3404814 2018-09-02 stsp if (header) {
1562 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1563 a3404814 2018-09-02 stsp if (err) {
1564 a3404814 2018-09-02 stsp return err;
1567 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1568 a3404814 2018-09-02 stsp wstandout(view->window);
1569 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1570 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1571 a3404814 2018-09-02 stsp wstandend(view->window);
1572 a3404814 2018-09-02 stsp if (width < view->ncols)
1573 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1575 a3404814 2018-09-02 stsp if (max_lines <= 1)
1576 a3404814 2018-09-02 stsp return NULL;
1577 a3404814 2018-09-02 stsp max_lines--;
1581 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1582 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1583 26ed57b2 2018-05-19 stsp if (line == NULL) {
1587 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1588 26ed57b2 2018-05-19 stsp free(line);
1592 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1593 61e69b96 2018-05-20 stsp if (err) {
1594 61e69b96 2018-05-20 stsp free(line);
1595 61e69b96 2018-05-20 stsp return err;
1597 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1598 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1599 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1600 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1601 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1602 26ed57b2 2018-05-19 stsp free(line);
1603 2550e4c3 2018-07-13 stsp free(wline);
1604 2550e4c3 2018-07-13 stsp wline = NULL;
1606 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1608 1a57306a 2018-09-02 stsp view_vborder(view);
1610 26ed57b2 2018-05-19 stsp return NULL;
1613 4ed7e80c 2018-05-20 stsp static const struct got_error *
1614 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
1616 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
1617 48ae06ee 2018-10-18 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1618 48ae06ee 2018-10-18 stsp FILE *f = NULL;
1620 48ae06ee 2018-10-18 stsp if (s->id1) {
1621 48ae06ee 2018-10-18 stsp err = got_object_open(&obj1, s->repo, s->id1);
1623 48ae06ee 2018-10-18 stsp return err;
1626 48ae06ee 2018-10-18 stsp err = got_object_open(&obj2, s->repo, s->id2);
1628 48ae06ee 2018-10-18 stsp goto done;
1630 511a516b 2018-05-19 stsp f = got_opentemp();
1631 48ae06ee 2018-10-18 stsp if (f == NULL) {
1632 48ae06ee 2018-10-18 stsp err = got_error_from_errno();
1633 48ae06ee 2018-10-18 stsp goto done;
1636 48ae06ee 2018-10-18 stsp fclose(s->f);
1639 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1640 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1641 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1642 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1644 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1645 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_trees(obj1, obj2, "", "",
1646 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1648 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1649 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1650 48ae06ee 2018-10-18 stsp s->repo, f);
1653 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1658 48ae06ee 2018-10-18 stsp got_object_close(obj1);
1659 48ae06ee 2018-10-18 stsp got_object_close(obj2);
1661 48ae06ee 2018-10-18 stsp fflush(f);
1662 48ae06ee 2018-10-18 stsp return err;
1665 48ae06ee 2018-10-18 stsp static const struct got_error *
1666 48ae06ee 2018-10-18 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1667 48ae06ee 2018-10-18 stsp struct got_object *obj2, struct got_repository *repo)
1669 48ae06ee 2018-10-18 stsp const struct got_error *err;
1671 48ae06ee 2018-10-18 stsp if (obj1 != NULL && obj2 != NULL &&
1672 48ae06ee 2018-10-18 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1673 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
1675 48ae06ee 2018-10-18 stsp if (obj1) {
1676 48ae06ee 2018-10-18 stsp struct got_object_id *id1;
1677 48ae06ee 2018-10-18 stsp id1 = got_object_id_dup(got_object_get_id(obj1));
1678 48ae06ee 2018-10-18 stsp if (id1 == NULL)
1679 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1680 48ae06ee 2018-10-18 stsp view->state.diff.id1 = id1;
1682 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1684 48ae06ee 2018-10-18 stsp view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1685 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
1686 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1687 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1688 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1690 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
1691 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1692 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1693 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
1694 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
1696 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
1697 48ae06ee 2018-10-18 stsp if (err) {
1698 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1699 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1700 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
1701 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
1702 48ae06ee 2018-10-18 stsp return err;
1705 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
1706 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
1707 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
1709 5dc9f4bc 2018-08-04 stsp return NULL;
1712 e5a0f69f 2018-08-18 stsp static const struct got_error *
1713 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1715 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1717 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1718 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1719 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
1720 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
1721 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1722 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1723 e5a0f69f 2018-08-18 stsp return err;
1726 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1727 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1729 a3404814 2018-09-02 stsp const struct got_error *err;
1730 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1731 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
1733 a3404814 2018-09-02 stsp if (s->id1) {
1734 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
1736 a3404814 2018-09-02 stsp return err;
1738 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
1740 a3404814 2018-09-02 stsp return err;
1742 a3404814 2018-09-02 stsp if (asprintf(&header, "diff: %s %s",
1743 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1744 a3404814 2018-09-02 stsp err = got_error_from_errno();
1745 a3404814 2018-09-02 stsp free(id_str1);
1746 a3404814 2018-09-02 stsp free(id_str2);
1747 a3404814 2018-09-02 stsp return err;
1749 a3404814 2018-09-02 stsp free(id_str1);
1750 a3404814 2018-09-02 stsp free(id_str2);
1752 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
1753 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
1757 0cf4efb1 2018-09-29 stsp static const struct got_error *
1758 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1759 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1761 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1762 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
1765 e5a0f69f 2018-08-18 stsp switch (ch) {
1767 e5a0f69f 2018-08-18 stsp case KEY_UP:
1768 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
1769 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1771 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1773 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
1774 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
1775 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1778 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1779 e5a0f69f 2018-08-18 stsp if (!s->eof)
1780 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1782 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
1785 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
1786 e5a0f69f 2018-08-18 stsp char *line;
1787 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
1788 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1789 e5a0f69f 2018-08-18 stsp if (line == NULL)
1794 48ae06ee 2018-10-18 stsp if (s->diff_context > 0) {
1795 48ae06ee 2018-10-18 stsp s->diff_context--;
1796 48ae06ee 2018-10-18 stsp err = create_diff(s);
1800 48ae06ee 2018-10-18 stsp if (s->diff_context < INT_MAX) {
1801 48ae06ee 2018-10-18 stsp s->diff_context++;
1802 48ae06ee 2018-10-18 stsp err = create_diff(s);
1809 bcbd79e2 2018-08-19 stsp return err;
1812 4ed7e80c 2018-05-20 stsp static const struct got_error *
1813 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1815 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1816 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1817 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1818 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1819 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1821 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1823 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1824 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1826 26ed57b2 2018-05-19 stsp err(1, "pledge");
1829 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1830 26ed57b2 2018-05-19 stsp switch (ch) {
1833 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1837 26ed57b2 2018-05-19 stsp argc -= optind;
1838 26ed57b2 2018-05-19 stsp argv += optind;
1840 26ed57b2 2018-05-19 stsp if (argc == 0) {
1841 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1842 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1843 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1844 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1845 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1846 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1847 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1848 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1849 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1850 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1851 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1852 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1853 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1855 26ed57b2 2018-05-19 stsp usage_diff();
1857 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1858 26ed57b2 2018-05-19 stsp free(repo_path);
1859 26ed57b2 2018-05-19 stsp if (error)
1860 26ed57b2 2018-05-19 stsp goto done;
1862 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1863 26ed57b2 2018-05-19 stsp if (error)
1864 26ed57b2 2018-05-19 stsp goto done;
1866 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1867 26ed57b2 2018-05-19 stsp if (error)
1868 26ed57b2 2018-05-19 stsp goto done;
1870 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
1871 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1872 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1873 ea5e7bb5 2018-08-01 stsp goto done;
1875 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
1876 5dc9f4bc 2018-08-04 stsp if (error)
1877 5dc9f4bc 2018-08-04 stsp goto done;
1878 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1880 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1882 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1884 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1885 26ed57b2 2018-05-19 stsp return error;
1888 4ed7e80c 2018-05-20 stsp __dead static void
1889 9f7d7167 2018-04-29 stsp usage_blame(void)
1892 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1893 9f7d7167 2018-04-29 stsp getprogname());
1897 84451b3e 2018-07-10 stsp struct tog_blame_line {
1898 84451b3e 2018-07-10 stsp int annotated;
1899 84451b3e 2018-07-10 stsp struct got_object_id *id;
1902 4ed7e80c 2018-05-20 stsp static const struct got_error *
1903 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1904 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1905 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1906 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1908 84451b3e 2018-07-10 stsp const struct got_error *err;
1909 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1910 84451b3e 2018-07-10 stsp char *line;
1911 84451b3e 2018-07-10 stsp size_t len;
1912 84451b3e 2018-07-10 stsp wchar_t *wline;
1913 b700b5d6 2018-07-10 stsp int width, wlimit;
1914 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1915 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1916 ab089a2a 2018-07-12 stsp char *id_str;
1918 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1920 ab089a2a 2018-07-12 stsp return err;
1922 84451b3e 2018-07-10 stsp rewind(f);
1923 f7d12f7e 2018-08-01 stsp werase(view->window);
1925 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1926 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1927 ab089a2a 2018-07-12 stsp free(id_str);
1928 ab089a2a 2018-07-12 stsp return err;
1931 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1932 ab089a2a 2018-07-12 stsp free(line);
1933 2550e4c3 2018-07-13 stsp line = NULL;
1934 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1935 a3404814 2018-09-02 stsp wstandout(view->window);
1936 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1937 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1938 a3404814 2018-09-02 stsp wstandend(view->window);
1939 2550e4c3 2018-07-13 stsp free(wline);
1940 2550e4c3 2018-07-13 stsp wline = NULL;
1941 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1942 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1944 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1945 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1946 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1947 ab089a2a 2018-07-12 stsp free(id_str);
1948 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1950 ab089a2a 2018-07-12 stsp free(id_str);
1951 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1952 3f60a8ef 2018-07-10 stsp free(line);
1953 2550e4c3 2018-07-13 stsp line = NULL;
1955 3f60a8ef 2018-07-10 stsp return err;
1956 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1957 2550e4c3 2018-07-13 stsp free(wline);
1958 2550e4c3 2018-07-13 stsp wline = NULL;
1959 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1960 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1963 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1964 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1965 84451b3e 2018-07-10 stsp if (line == NULL) {
1969 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1970 84451b3e 2018-07-10 stsp free(line);
1974 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1975 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1976 84451b3e 2018-07-10 stsp if (err) {
1977 84451b3e 2018-07-10 stsp free(line);
1978 84451b3e 2018-07-10 stsp return err;
1981 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
1982 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1984 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1985 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1986 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1987 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1988 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1989 84451b3e 2018-07-10 stsp char *id_str;
1990 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1991 84451b3e 2018-07-10 stsp if (err) {
1992 84451b3e 2018-07-10 stsp free(line);
1993 2550e4c3 2018-07-13 stsp free(wline);
1994 84451b3e 2018-07-10 stsp return err;
1996 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1997 84451b3e 2018-07-10 stsp free(id_str);
1998 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2000 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2001 ee41ec32 2018-07-10 stsp prev_id = NULL;
2004 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2005 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2006 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2009 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2010 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2011 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2012 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2013 84451b3e 2018-07-10 stsp free(line);
2014 2550e4c3 2018-07-13 stsp free(wline);
2015 2550e4c3 2018-07-13 stsp wline = NULL;
2017 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2019 1a57306a 2018-09-02 stsp view_vborder(view);
2021 84451b3e 2018-07-10 stsp return NULL;
2024 84451b3e 2018-07-10 stsp static const struct got_error *
2025 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2027 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2028 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2029 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2031 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2032 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2033 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2035 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
2036 84451b3e 2018-07-10 stsp return got_error_from_errno();
2038 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2039 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2040 d68a0a7d 2018-07-10 stsp goto done;
2043 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2044 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2046 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2047 d68a0a7d 2018-07-10 stsp if (line->annotated)
2048 d68a0a7d 2018-07-10 stsp goto done;
2050 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2051 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2052 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2053 84451b3e 2018-07-10 stsp goto done;
2055 84451b3e 2018-07-10 stsp line->annotated = 1;
2057 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
2058 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
2059 e5a0f69f 2018-08-18 stsp a->last_displayed_line, a->eof, a->view->nlines);
2061 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
2062 84451b3e 2018-07-10 stsp return got_error_from_errno();
2063 84451b3e 2018-07-10 stsp return err;
2066 84451b3e 2018-07-10 stsp static void *
2067 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2069 18430de3 2018-07-10 stsp const struct got_error *err;
2070 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
2071 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
2073 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2074 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
2076 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
2077 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
2079 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
2080 c9beca56 2018-07-22 stsp ta->repo = NULL;
2081 c9beca56 2018-07-22 stsp *ta->complete = 1;
2083 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
2084 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
2085 e5a0f69f 2018-08-18 stsp a->first_displayed_line, a->last_displayed_line, a->eof,
2086 f7d12f7e 2018-08-01 stsp a->view->nlines);
2088 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
2089 18430de3 2018-07-10 stsp err = got_error_from_errno();
2091 18430de3 2018-07-10 stsp return (void *)err;
2094 245d91c1 2018-07-12 stsp static struct got_object_id *
2095 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
2096 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
2098 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2100 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2101 245d91c1 2018-07-12 stsp if (!line->annotated)
2102 245d91c1 2018-07-12 stsp return NULL;
2104 245d91c1 2018-07-12 stsp return line->id;
2107 84451b3e 2018-07-10 stsp static const struct got_error *
2108 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
2109 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
2110 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
2112 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
2113 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
2114 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
2115 b880a918 2018-07-10 stsp struct got_object_qid *pid;
2117 b880a918 2018-07-10 stsp *pobj = NULL;
2118 b880a918 2018-07-10 stsp *obj = NULL;
2120 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
2121 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
2122 245d91c1 2018-07-12 stsp if (selected_id == NULL)
2123 b880a918 2018-07-10 stsp return NULL;
2125 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
2127 b880a918 2018-07-10 stsp goto done;
2129 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
2131 b880a918 2018-07-10 stsp goto done;
2133 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
2134 b880a918 2018-07-10 stsp if (pid) {
2135 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
2137 b880a918 2018-07-10 stsp goto done;
2140 b880a918 2018-07-10 stsp if (commit)
2141 b880a918 2018-07-10 stsp got_object_commit_close(commit);
2142 b880a918 2018-07-10 stsp return err;
2145 b880a918 2018-07-10 stsp static const struct got_error *
2146 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2148 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2151 245d91c1 2018-07-12 stsp if (blame->thread) {
2152 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
2153 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2154 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2155 245d91c1 2018-07-12 stsp err = NULL;
2156 245d91c1 2018-07-12 stsp blame->thread = NULL;
2158 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2159 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2160 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2162 245d91c1 2018-07-12 stsp if (blame->f) {
2163 245d91c1 2018-07-12 stsp fclose(blame->f);
2164 245d91c1 2018-07-12 stsp blame->f = NULL;
2166 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
2167 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
2168 245d91c1 2018-07-12 stsp free(blame->lines);
2169 245d91c1 2018-07-12 stsp blame->lines = NULL;
2170 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2171 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2173 245d91c1 2018-07-12 stsp return err;
2176 245d91c1 2018-07-12 stsp static const struct got_error *
2177 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2178 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
2179 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
2180 e5a0f69f 2018-08-18 stsp int *selected_line, int *done, int *eof, const char *path,
2181 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
2182 245d91c1 2018-07-12 stsp struct got_repository *repo)
2184 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2185 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2186 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2187 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2188 27d434c2 2018-09-15 stsp struct got_object *obj = NULL;
2190 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2192 27d434c2 2018-09-15 stsp goto done;
2194 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
2196 84451b3e 2018-07-10 stsp goto done;
2198 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2199 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2200 84451b3e 2018-07-10 stsp goto done;
2203 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
2205 a70480e0 2018-06-23 stsp goto done;
2206 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2207 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2208 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2209 84451b3e 2018-07-10 stsp goto done;
2211 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2212 245d91c1 2018-07-12 stsp blame->f, blob);
2214 84451b3e 2018-07-10 stsp goto done;
2216 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2217 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2218 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2219 84451b3e 2018-07-10 stsp goto done;
2222 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2224 bd24772e 2018-07-11 stsp goto done;
2226 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2227 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2228 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2229 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
2230 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2231 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2232 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2233 245d91c1 2018-07-12 stsp goto done;
2235 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
2236 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
2237 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
2238 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
2239 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
2240 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2241 e5a0f69f 2018-08-18 stsp blame->cb_args.eof = eof;
2243 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2244 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2245 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2246 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2247 245d91c1 2018-07-12 stsp *blame_complete = 0;
2249 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
2250 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
2251 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2252 245d91c1 2018-07-12 stsp goto done;
2257 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2258 27d434c2 2018-09-15 stsp free(obj_id);
2260 245d91c1 2018-07-12 stsp got_object_close(obj);
2262 245d91c1 2018-07-12 stsp stop_blame(blame);
2263 245d91c1 2018-07-12 stsp return err;
2266 245d91c1 2018-07-12 stsp static const struct got_error *
2267 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2268 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2270 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2271 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2273 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2275 fb2756b9 2018-08-04 stsp if (pthread_mutex_init(&s->mutex, NULL) != 0)
2276 7cbe629d 2018-08-04 stsp return got_error_from_errno();
2278 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2280 7cbe629d 2018-08-04 stsp return err;
2282 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2283 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2284 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2285 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2286 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2287 fb2756b9 2018-08-04 stsp s->path = path;
2288 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2289 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2290 fb2756b9 2018-08-04 stsp s->repo = repo;
2291 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2292 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2294 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2295 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2296 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2298 e5a0f69f 2018-08-18 stsp return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2299 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2300 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2301 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2304 e5a0f69f 2018-08-18 stsp static const struct got_error *
2305 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2307 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2308 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2310 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2311 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2313 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2314 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2315 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2316 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2317 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2320 e5a0f69f 2018-08-18 stsp free(s->path);
2322 e5a0f69f 2018-08-18 stsp return err;
2325 7cbe629d 2018-08-04 stsp static const struct got_error *
2326 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2328 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2329 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2331 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0)
2332 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2334 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2335 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2336 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2337 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2339 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2340 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2342 669b5ffa 2018-10-07 stsp view_vborder(view);
2343 e5a0f69f 2018-08-18 stsp return err;
2346 e5a0f69f 2018-08-18 stsp static const struct got_error *
2347 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2348 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2350 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2351 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
2352 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2353 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2354 669b5ffa 2018-10-07 stsp int begin_x = 0;
2356 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2357 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2358 e5a0f69f 2018-08-18 stsp goto done;
2361 e5a0f69f 2018-08-18 stsp switch (ch) {
2363 e5a0f69f 2018-08-18 stsp s->done = 1;
2364 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2365 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2366 e5a0f69f 2018-08-18 stsp goto done;
2368 e5a0f69f 2018-08-18 stsp return stop_blame(&s->blame);
2370 e5a0f69f 2018-08-18 stsp case KEY_UP:
2371 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2372 e5a0f69f 2018-08-18 stsp s->selected_line--;
2373 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2374 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2375 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2377 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2378 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2379 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2382 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2383 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2384 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2386 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2389 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2390 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2391 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2392 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2393 e5a0f69f 2018-08-18 stsp s->selected_line++;
2394 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2395 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2396 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2399 e5a0f69f 2018-08-18 stsp case 'p': {
2400 e5a0f69f 2018-08-18 stsp struct got_object_id *id;
2401 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2402 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2403 e5a0f69f 2018-08-18 stsp if (id == NULL || got_object_id_cmp(id,
2404 e5a0f69f 2018-08-18 stsp s->blamed_commit->id) == 0)
2406 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2407 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2408 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2411 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2413 e5a0f69f 2018-08-18 stsp if (ch == 'p' && pobj == NULL)
2415 e5a0f69f 2018-08-18 stsp s->done = 1;
2416 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2417 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2418 e5a0f69f 2018-08-18 stsp goto done;
2420 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2421 e5a0f69f 2018-08-18 stsp s->done = 0;
2422 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2423 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2424 e5a0f69f 2018-08-18 stsp goto done;
2426 e5a0f69f 2018-08-18 stsp if (thread_err)
2428 e5a0f69f 2018-08-18 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
2429 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2430 e5a0f69f 2018-08-18 stsp obj = NULL;
2431 e5a0f69f 2018-08-18 stsp if (pobj) {
2432 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2433 e5a0f69f 2018-08-18 stsp pobj = NULL;
2435 6402fb3c 2018-09-15 stsp err = got_object_qid_alloc(&s->blamed_commit, id);
2437 e5a0f69f 2018-08-18 stsp goto done;
2438 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2439 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2440 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2441 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2442 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2443 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2444 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2445 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2450 e5a0f69f 2018-08-18 stsp case 'B': {
2451 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2452 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2453 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2455 e5a0f69f 2018-08-18 stsp s->done = 1;
2456 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2457 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2458 e5a0f69f 2018-08-18 stsp goto done;
2460 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2461 e5a0f69f 2018-08-18 stsp s->done = 0;
2462 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2463 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2464 e5a0f69f 2018-08-18 stsp goto done;
2466 e5a0f69f 2018-08-18 stsp if (thread_err)
2468 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2469 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2470 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2471 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2472 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2473 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2474 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2475 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2476 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2477 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2482 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2483 e5a0f69f 2018-08-18 stsp case '\r':
2484 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2485 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2486 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2489 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2492 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
2493 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
2494 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2495 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2496 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2499 669b5ffa 2018-10-07 stsp err = open_diff_view(diff_view, pobj, obj, s->repo);
2500 e5a0f69f 2018-08-18 stsp if (err) {
2501 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2504 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
2505 669b5ffa 2018-10-07 stsp err = view_close_child(view);
2507 669b5ffa 2018-10-07 stsp return err;
2508 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
2509 669b5ffa 2018-10-07 stsp if (err) {
2510 669b5ffa 2018-10-07 stsp view_close(diff_view);
2513 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(diff_view)) {
2514 669b5ffa 2018-10-07 stsp *focus_view = diff_view;
2515 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
2518 669b5ffa 2018-10-07 stsp *new_view = diff_view;
2519 e5a0f69f 2018-08-18 stsp if (pobj) {
2520 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2521 e5a0f69f 2018-08-18 stsp pobj = NULL;
2523 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2524 e5a0f69f 2018-08-18 stsp obj = NULL;
2528 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2530 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2531 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2532 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2533 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2536 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2537 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2538 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2539 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2541 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2542 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2543 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2545 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2546 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2547 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2548 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2555 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0)
2556 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2559 b880a918 2018-07-10 stsp got_object_close(pobj);
2560 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2563 a70480e0 2018-06-23 stsp static const struct got_error *
2564 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2566 a70480e0 2018-06-23 stsp const struct got_error *error;
2567 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2568 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2569 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2570 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2572 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2574 a70480e0 2018-06-23 stsp #ifndef PROFILE
2575 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2577 a70480e0 2018-06-23 stsp err(1, "pledge");
2580 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2581 a70480e0 2018-06-23 stsp switch (ch) {
2583 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2586 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2587 69069811 2018-08-02 stsp if (repo_path == NULL)
2588 69069811 2018-08-02 stsp err(1, "-r option");
2592 a70480e0 2018-06-23 stsp /* NOTREACHED */
2596 a70480e0 2018-06-23 stsp argc -= optind;
2597 a70480e0 2018-06-23 stsp argv += optind;
2599 69069811 2018-08-02 stsp if (argc == 1)
2600 69069811 2018-08-02 stsp path = argv[0];
2602 a70480e0 2018-06-23 stsp usage_blame();
2604 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2605 69069811 2018-08-02 stsp if (cwd == NULL) {
2606 69069811 2018-08-02 stsp error = got_error_from_errno();
2607 69069811 2018-08-02 stsp goto done;
2609 69069811 2018-08-02 stsp if (repo_path == NULL) {
2610 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2611 69069811 2018-08-02 stsp if (repo_path == NULL) {
2612 69069811 2018-08-02 stsp error = got_error_from_errno();
2613 69069811 2018-08-02 stsp goto done;
2618 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2619 a70480e0 2018-06-23 stsp if (error != NULL)
2620 66b4983c 2018-06-23 stsp return error;
2622 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2623 69069811 2018-08-02 stsp if (error != NULL)
2624 69069811 2018-08-02 stsp goto done;
2626 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2627 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2628 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2629 a70480e0 2018-06-23 stsp if (error != NULL)
2630 66b4983c 2018-06-23 stsp goto done;
2631 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2632 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2634 a70480e0 2018-06-23 stsp struct got_object *obj;
2635 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2636 a70480e0 2018-06-23 stsp if (error != NULL)
2637 66b4983c 2018-06-23 stsp goto done;
2638 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
2639 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2640 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2641 a19e88aa 2018-06-23 stsp got_object_close(obj);
2643 a19e88aa 2018-06-23 stsp if (error != NULL)
2644 a19e88aa 2018-06-23 stsp goto done;
2646 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2647 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2648 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2649 e1cd8fed 2018-08-01 stsp goto done;
2651 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2652 7cbe629d 2018-08-04 stsp if (error)
2653 7cbe629d 2018-08-04 stsp goto done;
2654 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2656 69069811 2018-08-02 stsp free(repo_path);
2657 69069811 2018-08-02 stsp free(cwd);
2658 a70480e0 2018-06-23 stsp free(commit_id);
2660 a70480e0 2018-06-23 stsp got_repo_close(repo);
2661 a70480e0 2018-06-23 stsp return error;
2664 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2665 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2666 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2667 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2668 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2669 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2670 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2672 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2673 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2674 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2675 ffd1d5e5 2018-06-23 stsp int width, n;
2677 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2679 f7d12f7e 2018-08-01 stsp werase(view->window);
2681 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2682 ffd1d5e5 2018-06-23 stsp return NULL;
2684 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2686 ffd1d5e5 2018-06-23 stsp return err;
2687 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2688 a3404814 2018-09-02 stsp wstandout(view->window);
2689 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2690 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2691 a3404814 2018-09-02 stsp wstandend(view->window);
2692 2550e4c3 2018-07-13 stsp free(wline);
2693 2550e4c3 2018-07-13 stsp wline = NULL;
2694 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2695 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2696 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2697 ffd1d5e5 2018-06-23 stsp return NULL;
2698 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2700 ce52c690 2018-06-23 stsp return err;
2701 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2702 2550e4c3 2018-07-13 stsp free(wline);
2703 2550e4c3 2018-07-13 stsp wline = NULL;
2704 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2705 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2706 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2707 ffd1d5e5 2018-06-23 stsp return NULL;
2708 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2709 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2710 a1eca9bb 2018-06-23 stsp return NULL;
2712 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2713 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2714 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2715 0cf4efb1 2018-09-29 stsp if (view->focussed)
2716 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2717 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2719 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2720 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
2721 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2722 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2723 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2724 ffd1d5e5 2018-06-23 stsp return NULL;
2728 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2729 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2732 ffd1d5e5 2018-06-23 stsp while (te) {
2733 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2735 1d13200f 2018-07-12 stsp if (show_ids) {
2736 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2738 1d13200f 2018-07-12 stsp return got_error_from_errno();
2740 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2741 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2742 1d13200f 2018-07-12 stsp free(id_str);
2743 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2745 1d13200f 2018-07-12 stsp free(id_str);
2746 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2747 ffd1d5e5 2018-06-23 stsp if (err) {
2748 ffd1d5e5 2018-06-23 stsp free(line);
2751 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2752 0cf4efb1 2018-09-29 stsp if (view->focussed)
2753 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2754 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2756 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2757 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2758 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2759 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
2760 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2761 ffd1d5e5 2018-06-23 stsp free(line);
2762 2550e4c3 2018-07-13 stsp free(wline);
2763 2550e4c3 2018-07-13 stsp wline = NULL;
2765 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2766 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2767 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2769 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2772 ffd1d5e5 2018-06-23 stsp return err;
2775 ffd1d5e5 2018-06-23 stsp static void
2776 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2777 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2779 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2782 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2785 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2786 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2787 ffd1d5e5 2018-06-23 stsp if (!isroot)
2788 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2792 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2793 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2794 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2795 ffd1d5e5 2018-06-23 stsp prev = te;
2796 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2798 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2799 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2801 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2802 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2805 ffd1d5e5 2018-06-23 stsp static void
2806 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2807 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2808 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2810 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2811 ffd1d5e5 2018-06-23 stsp int n = 0;
2813 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2816 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2817 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2819 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2820 ffd1d5e5 2018-06-23 stsp while (next) {
2821 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2822 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2824 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2828 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2829 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2830 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2832 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2833 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2834 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2836 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2837 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2839 ce52c690 2018-06-23 stsp len += strlen(te->name);
2841 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2842 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2843 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2845 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2846 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2847 d9765a41 2018-06-23 stsp while (pt) {
2848 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2849 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2850 cb2ebc8a 2018-06-23 stsp goto done;
2852 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2853 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2854 cb2ebc8a 2018-06-23 stsp goto done;
2856 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2859 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2860 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2861 ce52c690 2018-06-23 stsp goto done;
2865 ce52c690 2018-06-23 stsp if (err) {
2866 ce52c690 2018-06-23 stsp free(*path);
2867 ce52c690 2018-06-23 stsp *path = NULL;
2869 ce52c690 2018-06-23 stsp return err;
2872 ce52c690 2018-06-23 stsp static const struct got_error *
2873 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
2874 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2875 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2877 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2878 ce52c690 2018-06-23 stsp char *path;
2879 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
2881 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2883 ce52c690 2018-06-23 stsp return err;
2885 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
2886 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
2887 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2889 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
2890 e5a0f69f 2018-08-18 stsp if (err) {
2891 e5a0f69f 2018-08-18 stsp view_close(blame_view);
2892 e5a0f69f 2018-08-18 stsp free(path);
2894 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
2895 69efd4c4 2018-07-18 stsp return err;
2898 69efd4c4 2018-07-18 stsp static const struct got_error *
2899 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
2900 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2901 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2903 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
2904 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2905 69efd4c4 2018-07-18 stsp char *path;
2907 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
2908 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
2909 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2911 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2913 69efd4c4 2018-07-18 stsp return err;
2915 e5a0f69f 2018-08-18 stsp err = open_log_view(log_view, commit_id, repo, path);
2917 e5a0f69f 2018-08-18 stsp view_close(log_view);
2919 e5a0f69f 2018-08-18 stsp *new_view = log_view;
2920 cb2ebc8a 2018-06-23 stsp free(path);
2921 cb2ebc8a 2018-06-23 stsp return err;
2924 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2925 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
2926 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2928 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2929 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
2930 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2932 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
2934 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2935 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2936 ffd1d5e5 2018-06-23 stsp goto done;
2938 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2939 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2940 ffd1d5e5 2018-06-23 stsp goto done;
2943 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
2944 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
2945 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2946 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
2947 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
2948 6484ec90 2018-09-29 stsp err = got_error_from_errno();
2949 6484ec90 2018-09-29 stsp goto done;
2951 fb2756b9 2018-08-04 stsp s->repo = repo;
2953 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
2954 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
2955 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
2957 ad80ab7b 2018-08-04 stsp free(commit_id_str);
2958 6484ec90 2018-09-29 stsp if (err) {
2959 fb2756b9 2018-08-04 stsp free(s->tree_label);
2960 6484ec90 2018-09-29 stsp s->tree_label = NULL;
2962 ad80ab7b 2018-08-04 stsp return err;
2965 e5a0f69f 2018-08-18 stsp static const struct got_error *
2966 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
2968 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2970 fb2756b9 2018-08-04 stsp free(s->tree_label);
2971 6484ec90 2018-09-29 stsp s->tree_label = NULL;
2972 6484ec90 2018-09-29 stsp free(s->commit_id);
2973 6484ec90 2018-09-29 stsp s->commit_id = NULL;
2974 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
2975 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
2976 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
2977 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
2978 ad80ab7b 2018-08-04 stsp free(parent);
2981 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
2982 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
2983 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
2985 e5a0f69f 2018-08-18 stsp return NULL;
2988 ad80ab7b 2018-08-04 stsp static const struct got_error *
2989 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
2991 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
2992 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2993 e5a0f69f 2018-08-18 stsp char *parent_path;
2995 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
2997 e5a0f69f 2018-08-18 stsp return err;
2999 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
3000 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
3001 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3002 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
3003 e5a0f69f 2018-08-18 stsp free(parent_path);
3005 669b5ffa 2018-10-07 stsp view_vborder(view);
3006 e5a0f69f 2018-08-18 stsp return err;
3009 e5a0f69f 2018-08-18 stsp static const struct got_error *
3010 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3011 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3013 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3014 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
3015 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
3016 669b5ffa 2018-10-07 stsp int begin_x = 0;
3018 e5a0f69f 2018-08-18 stsp switch (ch) {
3020 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
3023 669b5ffa 2018-10-07 stsp if (!s->selected_entry)
3025 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
3026 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
3027 669b5ffa 2018-10-07 stsp err = log_tree_entry(&log_view, begin_x,
3028 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
3029 669b5ffa 2018-10-07 stsp s->commit_id, s->repo);
3030 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3031 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3033 669b5ffa 2018-10-07 stsp return err;
3034 669b5ffa 2018-10-07 stsp err = view_set_child(view, log_view);
3035 669b5ffa 2018-10-07 stsp if (err) {
3036 669b5ffa 2018-10-07 stsp view_close(log_view);
3039 669b5ffa 2018-10-07 stsp *focus_view = log_view;
3040 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3042 669b5ffa 2018-10-07 stsp *new_view = log_view;
3045 e5a0f69f 2018-08-18 stsp case KEY_UP:
3046 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3047 e5a0f69f 2018-08-18 stsp s->selected--;
3048 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3050 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
3051 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
3053 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
3054 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
3055 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
3056 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
3057 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3058 e5a0f69f 2018-08-18 stsp s->selected = 0;
3061 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
3062 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
3063 e5a0f69f 2018-08-18 stsp s->tree == s->root);
3066 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
3067 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
3068 e5a0f69f 2018-08-18 stsp s->selected++;
3071 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
3072 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
3074 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
3075 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
3076 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
3077 e5a0f69f 2018-08-18 stsp s->entries);
3078 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
3081 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
3082 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
3083 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3085 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
3086 e5a0f69f 2018-08-18 stsp case '\r':
3087 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
3088 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3089 7837eeac 2018-09-24 stsp case KEY_BACKSPACE:
3090 e5a0f69f 2018-08-18 stsp /* user selected '..' */
3091 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
3093 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
3094 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
3096 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
3097 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
3098 e5a0f69f 2018-08-18 stsp s->entries =
3099 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3100 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
3101 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
3102 e5a0f69f 2018-08-18 stsp s->selected_entry =
3103 e5a0f69f 2018-08-18 stsp parent->selected_entry;
3104 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
3105 e5a0f69f 2018-08-18 stsp free(parent);
3106 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
3107 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3108 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
3109 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
3110 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
3113 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
3114 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
3115 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
3118 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
3119 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
3120 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
3121 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
3122 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
3123 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3124 e5a0f69f 2018-08-18 stsp s->tree = child;
3125 e5a0f69f 2018-08-18 stsp s->entries =
3126 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3127 e5a0f69f 2018-08-18 stsp s->selected = 0;
3128 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3129 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
3130 669b5ffa 2018-10-07 stsp struct tog_view *blame_view;
3131 669b5ffa 2018-10-07 stsp int begin_x = view_is_parent_view(view) ?
3132 669b5ffa 2018-10-07 stsp view_split_begin_x(view->begin_x) : 0;
3134 669b5ffa 2018-10-07 stsp err = blame_tree_entry(&blame_view, begin_x,
3135 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents, s->commit_id,
3139 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3140 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3142 669b5ffa 2018-10-07 stsp return err;
3143 669b5ffa 2018-10-07 stsp err = view_set_child(view, blame_view);
3144 669b5ffa 2018-10-07 stsp if (err) {
3145 669b5ffa 2018-10-07 stsp view_close(blame_view);
3148 669b5ffa 2018-10-07 stsp *focus_view = blame_view;
3149 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3151 669b5ffa 2018-10-07 stsp *new_view = blame_view;
3154 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3155 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3156 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3162 ffd1d5e5 2018-06-23 stsp return err;
3165 ffd1d5e5 2018-06-23 stsp __dead static void
3166 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3169 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3170 ffd1d5e5 2018-06-23 stsp getprogname());
3174 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3175 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3177 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3178 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3179 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3180 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3181 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3182 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3183 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3185 5221c383 2018-08-01 stsp struct tog_view *view;
3187 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3188 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3190 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3193 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3194 ffd1d5e5 2018-06-23 stsp switch (ch) {
3196 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3200 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3204 ffd1d5e5 2018-06-23 stsp argc -= optind;
3205 ffd1d5e5 2018-06-23 stsp argv += optind;
3207 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3208 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
3209 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3210 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3211 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3212 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3213 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3214 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3216 ffd1d5e5 2018-06-23 stsp usage_log();
3218 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3219 ffd1d5e5 2018-06-23 stsp free(repo_path);
3220 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3221 ffd1d5e5 2018-06-23 stsp return error;
3223 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
3224 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3225 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3226 ffd1d5e5 2018-06-23 stsp goto done;
3228 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
3229 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3230 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
3231 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
3232 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
3233 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
3236 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3237 ffd1d5e5 2018-06-23 stsp goto done;
3239 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3240 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3241 ffd1d5e5 2018-06-23 stsp goto done;
3243 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3244 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3245 ffd1d5e5 2018-06-23 stsp goto done;
3247 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3248 5221c383 2018-08-01 stsp if (view == NULL) {
3249 5221c383 2018-08-01 stsp error = got_error_from_errno();
3250 5221c383 2018-08-01 stsp goto done;
3252 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3253 ad80ab7b 2018-08-04 stsp if (error)
3254 ad80ab7b 2018-08-04 stsp goto done;
3255 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3257 ffd1d5e5 2018-06-23 stsp free(commit_id);
3258 ffd1d5e5 2018-06-23 stsp if (commit)
3259 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3261 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3263 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3264 ffd1d5e5 2018-06-23 stsp return error;
3266 5c5136c5 2018-05-20 stsp static void
3267 9f7d7167 2018-04-29 stsp init_curses(void)
3269 9f7d7167 2018-04-29 stsp initscr();
3273 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3274 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3275 1f475ad8 2018-05-10 stsp curs_set(0);
3278 4ed7e80c 2018-05-20 stsp __dead static void
3279 9f7d7167 2018-04-29 stsp usage(void)
3283 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3284 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3285 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3286 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3287 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3292 c2301be8 2018-04-30 stsp static char **
3293 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3295 c2301be8 2018-04-30 stsp char **argv;
3296 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3298 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3299 c2301be8 2018-04-30 stsp if (argv == NULL)
3300 c2301be8 2018-04-30 stsp err(1, "calloc");
3301 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3302 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3303 c2301be8 2018-04-30 stsp err(1, "calloc");
3304 c2301be8 2018-04-30 stsp if (arg1) {
3305 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3306 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3307 c2301be8 2018-04-30 stsp err(1, "calloc");
3310 c2301be8 2018-04-30 stsp return argv;
3314 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3316 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3317 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3318 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3319 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3321 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
3323 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3324 9f7d7167 2018-04-29 stsp switch (ch) {
3326 9f7d7167 2018-04-29 stsp hflag = 1;
3330 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3334 9f7d7167 2018-04-29 stsp argc -= optind;
3335 9f7d7167 2018-04-29 stsp argv += optind;
3336 9f7d7167 2018-04-29 stsp optind = 0;
3337 c2301be8 2018-04-30 stsp optreset = 1;
3339 c2301be8 2018-04-30 stsp if (argc == 0) {
3340 f29d3e89 2018-06-23 stsp if (hflag)
3342 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3343 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3344 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3349 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3350 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3351 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3352 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3353 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3354 9f7d7167 2018-04-29 stsp if (hflag)
3355 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3359 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3360 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3361 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3362 c2301be8 2018-04-30 stsp if (repo_path) {
3363 c2301be8 2018-04-30 stsp struct got_repository *repo;
3364 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3365 c2301be8 2018-04-30 stsp if (error == NULL)
3366 c2301be8 2018-04-30 stsp got_repo_close(repo);
3368 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3369 c2301be8 2018-04-30 stsp if (error) {
3370 f29d3e89 2018-06-23 stsp if (hflag) {
3371 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3372 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3376 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3377 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3378 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3379 ad7de8d9 2018-04-30 stsp free(repo_path);
3382 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3383 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3385 c2301be8 2018-04-30 stsp free(repo_path);
3389 5c5136c5 2018-05-20 stsp init_curses();
3391 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3392 9f7d7167 2018-04-29 stsp if (error)
3393 9f7d7167 2018-04-29 stsp goto done;
3396 c2301be8 2018-04-30 stsp free(cmd_argv);
3397 9f7d7167 2018-04-29 stsp if (error)
3398 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);