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;
109 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
110 ba4f502b 2018-08-04 stsp struct commit_queue {
111 ba4f502b 2018-08-04 stsp int ncommits;
112 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
115 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
117 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
118 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
119 1a76625f 2018-10-22 stsp int commits_needed;
120 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
121 1a76625f 2018-10-22 stsp struct commit_queue *commits;
122 1a76625f 2018-10-22 stsp const char *in_repo_path;
123 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
124 1a76625f 2018-10-22 stsp struct got_repository *repo;
125 1a76625f 2018-10-22 stsp int log_complete;
126 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
127 1a76625f 2018-10-22 stsp struct tog_view *view;
128 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
129 1a76625f 2018-10-22 stsp struct commit_queue_entry **last_displayed_entry;
130 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
131 1a76625f 2018-10-22 stsp int *selected;
134 1a76625f 2018-10-22 stsp struct tog_log_view_state {
135 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
136 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
137 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
138 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
139 b01e7d3b 2018-08-04 stsp int selected;
140 b01e7d3b 2018-08-04 stsp char *in_repo_path;
141 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
142 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
143 1a76625f 2018-10-22 stsp sig_atomic_t quit;
144 1a76625f 2018-10-22 stsp pthread_t thread;
145 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
148 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
149 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
150 e9424729 2018-08-04 stsp int nlines;
152 e9424729 2018-08-04 stsp struct tog_view *view;
153 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
155 e9424729 2018-08-04 stsp const char *path;
156 e9424729 2018-08-04 stsp int *first_displayed_line;
157 e9424729 2018-08-04 stsp int *last_displayed_line;
158 e9424729 2018-08-04 stsp int *selected_line;
163 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
164 e9424729 2018-08-04 stsp const char *path;
165 e9424729 2018-08-04 stsp struct got_repository *repo;
166 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
167 e9424729 2018-08-04 stsp int *complete;
170 e9424729 2018-08-04 stsp struct tog_blame {
172 e9424729 2018-08-04 stsp size_t filesize;
173 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
174 e9424729 2018-08-04 stsp size_t nlines;
175 e9424729 2018-08-04 stsp pthread_t thread;
176 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
177 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
178 e9424729 2018-08-04 stsp const char *path;
181 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
182 7cbe629d 2018-08-04 stsp int first_displayed_line;
183 7cbe629d 2018-08-04 stsp int last_displayed_line;
184 7cbe629d 2018-08-04 stsp int selected_line;
185 7cbe629d 2018-08-04 stsp int blame_complete;
188 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
189 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
190 e5a0f69f 2018-08-18 stsp char *path;
191 7cbe629d 2018-08-04 stsp struct got_repository *repo;
192 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
193 e9424729 2018-08-04 stsp struct tog_blame blame;
196 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
197 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
198 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
199 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
200 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
201 ad80ab7b 2018-08-04 stsp int selected;
204 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
206 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
207 ad80ab7b 2018-08-04 stsp char *tree_label;
208 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
209 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
210 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
211 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
212 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
213 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
214 ad80ab7b 2018-08-04 stsp int nentries, ndisplayed, selected, show_ids;
215 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
216 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
217 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
221 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
223 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
224 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
225 669b5ffa 2018-10-07 stsp * there is enough screen estate.
227 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
228 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
230 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
231 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
232 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
234 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
235 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
237 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
239 cc3c9aac 2018-08-01 stsp struct tog_view {
240 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
241 26ed57b2 2018-05-19 stsp WINDOW *window;
242 26ed57b2 2018-05-19 stsp PANEL *panel;
243 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
244 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
245 1004088d 2018-09-29 stsp int focussed;
246 669b5ffa 2018-10-07 stsp struct tog_view *parent;
247 669b5ffa 2018-10-07 stsp struct tog_view *child;
248 669b5ffa 2018-10-07 stsp int child_focussed;
250 5dc9f4bc 2018-08-04 stsp /* type-specific state */
251 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
253 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
254 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
255 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
256 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
259 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
260 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
261 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
262 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
265 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
266 ba4f502b 2018-08-04 stsp struct got_object *, struct got_object *, struct got_repository *);
267 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
268 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
269 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
270 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
272 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
273 23721109 2018-10-22 stsp struct got_object_id *, struct got_repository *, const char *, int);
274 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
275 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
276 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
277 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
279 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
280 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
281 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
282 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
283 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
284 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
286 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
287 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
288 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
289 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
290 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
291 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
293 e5a0f69f 2018-08-18 stsp static const struct got_error *
294 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
296 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
298 669b5ffa 2018-10-07 stsp if (view->child) {
299 669b5ffa 2018-10-07 stsp view_close(view->child);
300 669b5ffa 2018-10-07 stsp view->child = NULL;
302 e5a0f69f 2018-08-18 stsp if (view->close)
303 e5a0f69f 2018-08-18 stsp err = view->close(view);
304 ea5e7bb5 2018-08-01 stsp if (view->panel)
305 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
306 ea5e7bb5 2018-08-01 stsp if (view->window)
307 ea5e7bb5 2018-08-01 stsp delwin(view->window);
308 ea5e7bb5 2018-08-01 stsp free(view);
309 e5a0f69f 2018-08-18 stsp return err;
312 ea5e7bb5 2018-08-01 stsp static struct tog_view *
313 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
314 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
316 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
318 ea5e7bb5 2018-08-01 stsp if (view == NULL)
319 ea5e7bb5 2018-08-01 stsp return NULL;
321 d6b05b5b 2018-08-04 stsp view->type = type;
322 f7d12f7e 2018-08-01 stsp view->lines = LINES;
323 f7d12f7e 2018-08-01 stsp view->cols = COLS;
324 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
325 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
326 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
327 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
328 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
329 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
330 96a765a8 2018-08-04 stsp view_close(view);
331 ea5e7bb5 2018-08-01 stsp return NULL;
333 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
334 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
335 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
336 96a765a8 2018-08-04 stsp view_close(view);
337 ea5e7bb5 2018-08-01 stsp return NULL;
340 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
341 ea5e7bb5 2018-08-01 stsp return view;
345 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
347 0cf4efb1 2018-09-29 stsp if (begin_x > 0)
349 0cf4efb1 2018-09-29 stsp return (COLS >= 120 ? COLS/2 : 0);
352 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
354 5c60c32a 2018-10-18 stsp static const struct got_error *
355 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
357 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
359 5c60c32a 2018-10-18 stsp view->begin_y = 0;
360 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
361 5c60c32a 2018-10-18 stsp view->nlines = LINES;
362 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
363 5c60c32a 2018-10-18 stsp view->lines = LINES;
364 5c60c32a 2018-10-18 stsp view->cols = COLS;
365 5c60c32a 2018-10-18 stsp err = view_resize(view);
367 5c60c32a 2018-10-18 stsp return err;
369 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
370 5c60c32a 2018-10-18 stsp return got_error_from_errno();
372 5c60c32a 2018-10-18 stsp return NULL;
375 5c60c32a 2018-10-18 stsp static const struct got_error *
376 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
378 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
380 5c60c32a 2018-10-18 stsp view->begin_x = 0;
381 5c60c32a 2018-10-18 stsp view->begin_y = 0;
382 5c60c32a 2018-10-18 stsp view->nlines = LINES;
383 5c60c32a 2018-10-18 stsp view->ncols = COLS;
384 5c60c32a 2018-10-18 stsp view->lines = LINES;
385 5c60c32a 2018-10-18 stsp view->cols = COLS;
386 5c60c32a 2018-10-18 stsp err = view_resize(view);
388 5c60c32a 2018-10-18 stsp return err;
390 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
391 5c60c32a 2018-10-18 stsp return got_error_from_errno();
393 5c60c32a 2018-10-18 stsp return NULL;
397 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
399 5c60c32a 2018-10-18 stsp return view->parent == NULL;
402 4d8c2215 2018-08-19 stsp static const struct got_error *
403 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
405 f7d12f7e 2018-08-01 stsp int nlines, ncols;
407 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
408 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
410 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
412 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
413 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
415 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
417 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
418 0cf4efb1 2018-09-29 stsp return got_error_from_errno();
419 0cf4efb1 2018-09-29 stsp replace_panel(view->panel, view->window);
421 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
422 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
423 0cf4efb1 2018-09-29 stsp view->lines = LINES;
424 0cf4efb1 2018-09-29 stsp view->cols = COLS;
426 6e3e5d9c 2018-10-18 stsp if (view->child) {
427 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
428 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
429 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
430 5c60c32a 2018-10-18 stsp if (view->child->focussed)
431 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
433 5c60c32a 2018-10-18 stsp show_panel(view->panel);
435 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
436 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
440 5c60c32a 2018-10-18 stsp return NULL;
443 669b5ffa 2018-10-07 stsp static const struct got_error *
444 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
446 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
448 669b5ffa 2018-10-07 stsp if (view->child == NULL)
449 669b5ffa 2018-10-07 stsp return NULL;
451 669b5ffa 2018-10-07 stsp err = view_close(view->child);
452 669b5ffa 2018-10-07 stsp view->child = NULL;
453 669b5ffa 2018-10-07 stsp return err;
456 669b5ffa 2018-10-07 stsp static const struct got_error *
457 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
459 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
461 669b5ffa 2018-10-07 stsp view->child = child;
462 669b5ffa 2018-10-07 stsp child->parent = view;
463 669b5ffa 2018-10-07 stsp return err;
467 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
469 669b5ffa 2018-10-07 stsp return !view_is_parent_view(view) && view->begin_x > 0;
472 0cf4efb1 2018-09-29 stsp static const struct got_error *
473 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
474 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
475 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
477 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
478 669b5ffa 2018-10-07 stsp struct tog_view *v;
479 1a76625f 2018-10-22 stsp int ch, errcode;
481 e5a0f69f 2018-08-18 stsp *new = NULL;
482 e5a0f69f 2018-08-18 stsp *dead = NULL;
483 0cf4efb1 2018-09-29 stsp *focus = NULL;
485 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
486 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
487 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
488 1a76625f 2018-10-22 stsp if (errcode)
489 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
490 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
491 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
492 1a76625f 2018-10-22 stsp if (errcode)
493 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
494 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
495 e5a0f69f 2018-08-18 stsp switch (ch) {
499 669b5ffa 2018-10-07 stsp if (view->child) {
500 669b5ffa 2018-10-07 stsp *focus = view->child;
501 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
502 669b5ffa 2018-10-07 stsp } else if (view->parent) {
503 669b5ffa 2018-10-07 stsp *focus = view->parent;
504 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 0;
508 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
509 e5a0f69f 2018-08-18 stsp *dead = view;
515 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
516 669b5ffa 2018-10-07 stsp if (view->child == NULL)
518 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view->child)) {
519 669b5ffa 2018-10-07 stsp *focus = view->child;
520 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
521 669b5ffa 2018-10-07 stsp err = view_fullscreen(view->child);
523 669b5ffa 2018-10-07 stsp err = view_splitscreen(view->child);
526 669b5ffa 2018-10-07 stsp err = view->child->input(new, dead, focus,
527 669b5ffa 2018-10-07 stsp view->child, KEY_RESIZE);
529 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view)) {
530 669b5ffa 2018-10-07 stsp *focus = view;
531 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 1;
532 669b5ffa 2018-10-07 stsp err = view_fullscreen(view);
534 669b5ffa 2018-10-07 stsp err = view_splitscreen(view);
538 669b5ffa 2018-10-07 stsp err = view->input(new, dead, focus, view,
539 669b5ffa 2018-10-07 stsp KEY_RESIZE);
542 0cf4efb1 2018-09-29 stsp case KEY_RESIZE:
543 0cf4efb1 2018-09-29 stsp TAILQ_FOREACH(v, views, entry) {
544 0cf4efb1 2018-09-29 stsp err = view_resize(v);
546 0cf4efb1 2018-09-29 stsp return err;
547 878940b7 2018-09-29 stsp err = v->input(new, dead, focus, v, ch);
549 876c7f55 2018-10-22 stsp return err;
553 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
557 e5a0f69f 2018-08-18 stsp return err;
561 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
563 0cf4efb1 2018-09-29 stsp PANEL *panel;
564 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
566 669b5ffa 2018-10-07 stsp if (view->parent)
567 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
569 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
570 0cf4efb1 2018-09-29 stsp if (panel == NULL)
573 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
574 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
575 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
579 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
581 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
582 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
584 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
586 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
589 669b5ffa 2018-10-07 stsp return view->focussed;
592 bcbd79e2 2018-08-19 stsp static const struct got_error *
593 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
595 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
596 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
597 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
598 fd823528 2018-10-22 stsp int fast_refresh = 10;
599 1a76625f 2018-10-22 stsp int done = 0, errcode;
601 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
602 1a76625f 2018-10-22 stsp if (errcode)
603 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
605 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
606 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
608 a81bf10d 2018-09-29 stsp main_view = view;
609 1004088d 2018-09-29 stsp view->focussed = 1;
610 878940b7 2018-09-29 stsp err = view->show(view);
612 0cf4efb1 2018-09-29 stsp return err;
613 0cf4efb1 2018-09-29 stsp update_panels();
614 0cf4efb1 2018-09-29 stsp doupdate();
615 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
616 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
617 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
618 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
620 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
621 e4197bf9 2018-08-18 stsp view, &views);
624 e5a0f69f 2018-08-18 stsp if (dead_view) {
625 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
627 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
628 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
629 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
631 669b5ffa 2018-10-07 stsp prev = view->parent;
633 669b5ffa 2018-10-07 stsp if (dead_view->parent)
634 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
636 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
638 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
639 a81bf10d 2018-09-29 stsp if (err || dead_view == main_view)
642 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
643 0cf4efb1 2018-09-29 stsp if (focus_view)
644 0cf4efb1 2018-09-29 stsp view = focus_view;
645 669b5ffa 2018-10-07 stsp else if (prev)
646 669b5ffa 2018-10-07 stsp view = prev;
647 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
648 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
649 0cf4efb1 2018-09-29 stsp tog_view_list_head);
651 0cf4efb1 2018-09-29 stsp view = NULL;
652 669b5ffa 2018-10-07 stsp if (view) {
653 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
654 669b5ffa 2018-10-07 stsp focus_view = view->child;
656 669b5ffa 2018-10-07 stsp focus_view = view;
660 bcbd79e2 2018-08-19 stsp if (new_view) {
661 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
662 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
663 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
664 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
666 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
667 86c66b02 2018-10-18 stsp err = view_close(v);
670 86c66b02 2018-10-18 stsp if (v == view)
671 86c66b02 2018-10-18 stsp view = new_view;
674 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
675 878940b7 2018-09-29 stsp if (focus_view == NULL)
676 878940b7 2018-09-29 stsp focus_view = new_view;
678 0cf4efb1 2018-09-29 stsp if (focus_view) {
679 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
681 0cf4efb1 2018-09-29 stsp view->focussed = 0;
682 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
683 0cf4efb1 2018-09-29 stsp view = focus_view;
684 878940b7 2018-09-29 stsp if (new_view)
685 878940b7 2018-09-29 stsp show_panel(new_view->panel);
686 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
687 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
689 669b5ffa 2018-10-07 stsp if (view) {
690 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
691 1a76625f 2018-10-22 stsp focus_view = view;
692 1a76625f 2018-10-22 stsp focus_view->focussed = 1;
694 669b5ffa 2018-10-07 stsp if (view->parent) {
695 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
699 669b5ffa 2018-10-07 stsp err = view->show(view);
702 669b5ffa 2018-10-07 stsp if (view->child) {
703 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
707 1a76625f 2018-10-22 stsp update_panels();
708 1a76625f 2018-10-22 stsp doupdate();
712 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
713 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
714 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
715 e5a0f69f 2018-08-18 stsp view_close(view);
718 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
719 1a76625f 2018-10-22 stsp if (errcode)
720 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
722 e5a0f69f 2018-08-18 stsp return err;
725 4ed7e80c 2018-05-20 stsp __dead static void
726 9f7d7167 2018-04-29 stsp usage_log(void)
729 c70c5802 2018-08-01 stsp fprintf(stderr,
730 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
731 9f7d7167 2018-04-29 stsp getprogname());
735 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
736 80ddbec8 2018-04-29 stsp static const struct got_error *
737 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
739 00dfcb92 2018-06-11 stsp char *vis = NULL;
740 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
742 963b370f 2018-05-20 stsp *ws = NULL;
743 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
744 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
745 00dfcb92 2018-06-11 stsp int vislen;
746 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
747 00dfcb92 2018-06-11 stsp return got_error_from_errno();
749 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
750 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
752 00dfcb92 2018-06-11 stsp return err;
753 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
754 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
755 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
760 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
761 a7f50699 2018-06-11 stsp if (*ws == NULL) {
762 a7f50699 2018-06-11 stsp err = got_error_from_errno();
766 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
767 963b370f 2018-05-20 stsp err = got_error_from_errno();
772 963b370f 2018-05-20 stsp *ws = NULL;
775 963b370f 2018-05-20 stsp return err;
778 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
779 963b370f 2018-05-20 stsp static const struct got_error *
780 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
782 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
783 963b370f 2018-05-20 stsp int cols = 0;
784 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
785 963b370f 2018-05-20 stsp size_t wlen;
788 963b370f 2018-05-20 stsp *wlinep = NULL;
789 b700b5d6 2018-07-10 stsp *widthp = 0;
791 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
793 963b370f 2018-05-20 stsp return err;
796 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
797 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
798 963b370f 2018-05-20 stsp switch (width) {
804 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
805 b700b5d6 2018-07-10 stsp cols += width;
809 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
810 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
814 963b370f 2018-05-20 stsp err = got_error_from_errno();
818 963b370f 2018-05-20 stsp wline[i] = L'\0';
819 b700b5d6 2018-07-10 stsp if (widthp)
820 b700b5d6 2018-07-10 stsp *widthp = cols;
823 963b370f 2018-05-20 stsp free(wline);
825 963b370f 2018-05-20 stsp *wlinep = wline;
826 963b370f 2018-05-20 stsp return err;
829 963b370f 2018-05-20 stsp static const struct got_error *
830 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
831 2814baeb 2018-08-01 stsp struct got_object_id *id)
833 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
834 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
835 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
836 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
837 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
838 bb737323 2018-05-20 stsp int author_width, logmsg_width;
839 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
840 80ddbec8 2018-04-29 stsp char *line = NULL;
841 bb737323 2018-05-20 stsp int col, limit;
842 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
843 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
844 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
846 c70c5802 2018-08-01 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
847 c70c5802 2018-08-01 stsp &commit->tm_committer) >= sizeof(datebuf))
848 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
850 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
851 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
853 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
854 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
855 b39d25c7 2018-07-10 stsp col = limit + 1;
856 b39d25c7 2018-07-10 stsp if (col > avail)
859 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
860 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
861 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
864 6d9fbc00 2018-04-29 stsp author = author0;
865 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
866 6d9fbc00 2018-04-29 stsp if (smallerthan)
867 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
869 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
871 6d9fbc00 2018-04-29 stsp *at = '\0';
873 b39d25c7 2018-07-10 stsp limit = avail - col;
874 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
877 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
878 bb737323 2018-05-20 stsp col += author_width;
879 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
880 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
882 bb737323 2018-05-20 stsp author_width++;
884 9c2eaf34 2018-05-20 stsp if (col > avail)
887 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
888 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
889 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
892 bb737323 2018-05-20 stsp logmsg = logmsg0;
893 bb737323 2018-05-20 stsp while (*logmsg == '\n')
895 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
896 bb737323 2018-05-20 stsp if (newline)
897 bb737323 2018-05-20 stsp *newline = '\0';
898 bb737323 2018-05-20 stsp limit = avail - col;
899 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
902 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
903 bb737323 2018-05-20 stsp col += logmsg_width;
904 bb737323 2018-05-20 stsp while (col <= avail) {
905 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
909 80ddbec8 2018-04-29 stsp free(logmsg0);
910 bb737323 2018-05-20 stsp free(wlogmsg);
911 6d9fbc00 2018-04-29 stsp free(author0);
912 bb737323 2018-05-20 stsp free(wauthor);
913 80ddbec8 2018-04-29 stsp free(line);
914 80ddbec8 2018-04-29 stsp return err;
917 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
918 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
919 899d86c2 2018-05-10 stsp struct got_object_id *id)
921 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
923 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
924 80ddbec8 2018-04-29 stsp if (entry == NULL)
925 899d86c2 2018-05-10 stsp return NULL;
927 899d86c2 2018-05-10 stsp entry->id = id;
928 99db9666 2018-05-07 stsp entry->commit = commit;
929 899d86c2 2018-05-10 stsp return entry;
932 99db9666 2018-05-07 stsp static void
933 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
935 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
937 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
938 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
939 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
940 ecb28ae0 2018-07-16 stsp commits->ncommits--;
941 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
942 99db9666 2018-05-07 stsp free(entry);
945 99db9666 2018-05-07 stsp static void
946 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
948 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
949 99db9666 2018-05-07 stsp pop_commit(commits);
952 c4972b91 2018-05-07 stsp static const struct got_error *
953 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
954 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
956 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
957 93e45b7c 2018-09-24 stsp int nqueued = 0;
960 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
961 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
962 1a76625f 2018-10-22 stsp * while updating the display.
964 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
965 93e45b7c 2018-09-24 stsp struct got_object_id *id;
966 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
967 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
968 1a76625f 2018-10-22 stsp int errcode;
970 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
972 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
974 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
975 93e45b7c 2018-09-24 stsp minqueue, repo);
977 ecb28ae0 2018-07-16 stsp return err;
981 ecb28ae0 2018-07-16 stsp if (id == NULL)
984 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
987 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
988 9ba79e04 2018-06-11 stsp if (entry == NULL) {
989 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
993 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
994 1a76625f 2018-10-22 stsp if (errcode) {
995 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
999 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1000 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1001 ecb28ae0 2018-07-16 stsp nqueued++;
1002 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1004 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1005 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1006 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1009 9ba79e04 2018-06-11 stsp return err;
1012 99db9666 2018-05-07 stsp static const struct got_error *
1013 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1015 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1016 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1018 9ba79e04 2018-06-11 stsp *head_id = NULL;
1020 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1022 99db9666 2018-05-07 stsp return err;
1024 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1025 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1026 9ba79e04 2018-06-11 stsp if (err) {
1027 9ba79e04 2018-06-11 stsp *head_id = NULL;
1028 99db9666 2018-05-07 stsp return err;
1031 9ba79e04 2018-06-11 stsp return NULL;
1034 0553a4e3 2018-04-30 stsp static const struct got_error *
1035 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1036 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1037 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1038 1a76625f 2018-10-22 stsp const char *path, int commits_needed)
1040 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1041 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1042 ecb28ae0 2018-07-16 stsp int ncommits, width;
1043 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1044 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1046 e0d42f60 2018-07-22 stsp entry = first;
1047 e0d42f60 2018-07-22 stsp ncommits = 0;
1048 e0d42f60 2018-07-22 stsp while (entry) {
1049 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1050 e0d42f60 2018-07-22 stsp *selected = entry;
1053 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1054 e0d42f60 2018-07-22 stsp ncommits++;
1057 1a76625f 2018-10-22 stsp if (*selected) {
1058 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1060 ecb28ae0 2018-07-16 stsp return err;
1063 1a76625f 2018-10-22 stsp if (asprintf(&ncommits_str, " [%d/%d]%s ",
1064 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1065 1a76625f 2018-10-22 stsp commits_needed == 0 ? "" : " loading...") == -1)
1066 1a76625f 2018-10-22 stsp return got_error_from_errno();
1068 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1069 1a76625f 2018-10-22 stsp if (asprintf(&header, "commit: %s %s%s",
1070 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1071 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1072 1a76625f 2018-10-22 stsp err = got_error_from_errno();
1073 1a76625f 2018-10-22 stsp header = NULL;
1074 1a76625f 2018-10-22 stsp goto done;
1076 1a76625f 2018-10-22 stsp } else if (asprintf(&header, "commit: %s%s",
1077 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1078 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1079 1a76625f 2018-10-22 stsp err = got_error_from_errno();
1080 1a76625f 2018-10-22 stsp header = NULL;
1081 1a76625f 2018-10-22 stsp goto done;
1083 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1085 1a76625f 2018-10-22 stsp goto done;
1087 2814baeb 2018-08-01 stsp werase(view->window);
1089 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1090 a3404814 2018-09-02 stsp wstandout(view->window);
1091 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1092 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1093 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1096 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1097 a3404814 2018-09-02 stsp wstandend(view->window);
1098 ecb28ae0 2018-07-16 stsp free(wline);
1099 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1100 1a76625f 2018-10-22 stsp goto done;
1102 899d86c2 2018-05-10 stsp entry = first;
1103 899d86c2 2018-05-10 stsp *last = first;
1104 867c6645 2018-07-10 stsp ncommits = 0;
1105 899d86c2 2018-05-10 stsp while (entry) {
1106 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1108 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1109 2814baeb 2018-08-01 stsp wstandout(view->window);
1110 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
1111 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1112 2814baeb 2018-08-01 stsp wstandend(view->window);
1115 0553a4e3 2018-04-30 stsp ncommits++;
1116 899d86c2 2018-05-10 stsp *last = entry;
1117 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1120 1a57306a 2018-09-02 stsp view_vborder(view);
1122 1a76625f 2018-10-22 stsp free(id_str);
1123 1a76625f 2018-10-22 stsp free(ncommits_str);
1124 1a76625f 2018-10-22 stsp free(header);
1125 80ddbec8 2018-04-29 stsp return err;
1128 07b55e75 2018-05-10 stsp static void
1129 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1130 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1132 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1133 07b55e75 2018-05-10 stsp int nscrolled = 0;
1135 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1136 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
1139 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1140 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1141 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1142 07b55e75 2018-05-10 stsp if (entry) {
1143 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1144 07b55e75 2018-05-10 stsp nscrolled++;
1149 aa075928 2018-05-10 stsp static const struct got_error *
1150 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1151 93e45b7c 2018-09-24 stsp struct commit_queue_entry **last_displayed_entry,
1152 1a76625f 2018-10-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1153 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1155 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
1156 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
1157 aa075928 2018-05-10 stsp int nscrolled = 0;
1159 1a76625f 2018-10-22 stsp if (*last_displayed_entry == NULL)
1160 1a76625f 2018-10-22 stsp return NULL;
1163 93e45b7c 2018-09-24 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1164 aa075928 2018-05-10 stsp if (pentry == NULL) {
1165 1a76625f 2018-10-22 stsp int errcode;
1166 1a76625f 2018-10-22 stsp if (*log_complete)
1167 1a76625f 2018-10-22 stsp return NULL;
1168 1a76625f 2018-10-22 stsp *commits_needed = maxscroll + 20;
1169 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(need_commits);
1170 1a76625f 2018-10-22 stsp if (errcode)
1171 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1172 1a76625f 2018-10-22 stsp return NULL;
1174 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1176 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1177 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1179 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1180 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1182 dd0a52c1 2018-05-20 stsp return err;
1185 cd0acaa7 2018-05-20 stsp static const struct got_error *
1186 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1187 0cf4efb1 2018-09-29 stsp struct got_object_id *commit_id, struct got_commit_object *commit,
1188 2a4718d3 2018-09-29 stsp struct got_repository *repo)
1190 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1191 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1192 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1193 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1195 2a4718d3 2018-09-29 stsp err = got_object_open(&obj2, repo, commit_id);
1197 cd0acaa7 2018-05-20 stsp return err;
1199 2a4718d3 2018-09-29 stsp parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1200 9ba79e04 2018-06-11 stsp if (parent_id) {
1201 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
1203 cd0acaa7 2018-05-20 stsp goto done;
1206 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1207 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
1208 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1209 ea5e7bb5 2018-08-01 stsp goto done;
1212 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, obj1, obj2, repo);
1213 e5a0f69f 2018-08-18 stsp if (err == NULL)
1214 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1217 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
1219 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
1220 cd0acaa7 2018-05-20 stsp return err;
1223 80ddbec8 2018-04-29 stsp static const struct got_error *
1224 0cf4efb1 2018-09-29 stsp browse_commit(struct tog_view **new_view, int begin_x,
1225 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1227 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1228 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1229 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1231 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1233 9343a5fb 2018-06-23 stsp return err;
1235 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1236 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1237 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1239 e5a0f69f 2018-08-18 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1241 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1243 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1244 1a76625f 2018-10-22 stsp return err;
1247 1a76625f 2018-10-22 stsp static void *
1248 1a76625f 2018-10-22 stsp log_thread(void *arg)
1250 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1251 1a76625f 2018-10-22 stsp int errcode = 0;
1252 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1253 1a76625f 2018-10-22 stsp int done = 0;
1255 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1257 1a76625f 2018-10-22 stsp return (void *)err;
1259 1a76625f 2018-10-22 stsp while (!done && !err) {
1260 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1261 1a76625f 2018-10-22 stsp a->in_repo_path);
1262 1a76625f 2018-10-22 stsp if (err) {
1263 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1264 1a76625f 2018-10-22 stsp return (void *)err;
1265 1a76625f 2018-10-22 stsp err = NULL;
1267 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1268 1a76625f 2018-10-22 stsp a->commits_needed--;
1270 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1271 1a76625f 2018-10-22 stsp if (errcode)
1272 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
1275 1a76625f 2018-10-22 stsp a->log_complete = 1;
1276 1a76625f 2018-10-22 stsp else if (*a->quit) {
1278 1a76625f 2018-10-22 stsp a->log_complete = 1;
1279 1a76625f 2018-10-22 stsp } else if (*a->first_displayed_entry == NULL) {
1280 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1281 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1282 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1285 1a76625f 2018-10-22 stsp err = draw_commits(a->view, a->last_displayed_entry,
1286 1a76625f 2018-10-22 stsp a->selected_entry, *a->first_displayed_entry,
1287 1a76625f 2018-10-22 stsp a->commits, *a->selected, a->view->nlines,
1288 1a76625f 2018-10-22 stsp a->in_repo_path, a->commits_needed);
1291 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1292 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1293 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1294 1a76625f 2018-10-22 stsp &tog_mutex);
1295 1a76625f 2018-10-22 stsp if (errcode)
1296 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1299 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1300 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1301 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1303 1a76625f 2018-10-22 stsp return (void *)err;
1306 1a76625f 2018-10-22 stsp static const struct got_error *
1307 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1309 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1310 1a76625f 2018-10-22 stsp int errcode;
1312 1a76625f 2018-10-22 stsp if (s->thread) {
1313 1a76625f 2018-10-22 stsp s->quit = 1;
1314 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1315 1a76625f 2018-10-22 stsp if (errcode)
1316 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1317 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1318 1a76625f 2018-10-22 stsp if (errcode)
1319 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1320 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1321 1a76625f 2018-10-22 stsp if (errcode)
1322 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1323 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1324 1a76625f 2018-10-22 stsp if (errcode)
1325 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1326 1a76625f 2018-10-22 stsp s->thread = NULL;
1329 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1330 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1331 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1333 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1334 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1335 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1338 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1339 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1340 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1343 9343a5fb 2018-06-23 stsp return err;
1346 9343a5fb 2018-06-23 stsp static const struct got_error *
1347 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1349 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1350 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1352 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1353 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1354 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1355 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1356 1a76625f 2018-10-22 stsp free(s->start_id);
1357 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1358 1a76625f 2018-10-22 stsp return err;
1361 1a76625f 2018-10-22 stsp static const struct got_error *
1362 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1363 23721109 2018-10-22 stsp struct got_repository *repo, const char *path, int check_disk)
1365 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1366 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1367 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1368 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1369 1a76625f 2018-10-22 stsp int errcode;
1371 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1372 ecb28ae0 2018-07-16 stsp if (err != NULL)
1373 ecb28ae0 2018-07-16 stsp goto done;
1375 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1376 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1377 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1379 fb2756b9 2018-08-04 stsp s->repo = repo;
1380 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1381 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1382 5036bf37 2018-09-24 stsp err = got_error_from_errno();
1383 5036bf37 2018-09-24 stsp goto done;
1386 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1387 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1388 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1390 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1392 1a76625f 2018-10-22 stsp goto done;
1393 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1394 1a76625f 2018-10-22 stsp 0, thread_repo);
1396 1a76625f 2018-10-22 stsp goto done;
1398 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1399 1a76625f 2018-10-22 stsp if (errcode) {
1400 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1401 1a76625f 2018-10-22 stsp goto done;
1404 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1405 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1406 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1407 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1408 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1409 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1410 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1411 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1412 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1413 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1414 1a76625f 2018-10-22 stsp s->thread_args.last_displayed_entry = &s->last_displayed_entry;
1415 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1416 1a76625f 2018-10-22 stsp s->thread_args.selected = &s->selected;
1418 1a76625f 2018-10-22 stsp errcode = pthread_create(&s->thread, NULL, log_thread,
1419 1a76625f 2018-10-22 stsp &s->thread_args);
1420 1a76625f 2018-10-22 stsp if (errcode) {
1421 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1422 1a76625f 2018-10-22 stsp goto done;
1427 1a76625f 2018-10-22 stsp close_log_view(view);
1428 ba4f502b 2018-08-04 stsp return err;
1431 e5a0f69f 2018-08-18 stsp static const struct got_error *
1432 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1434 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1436 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1437 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1438 1a76625f 2018-10-22 stsp &s->commits, s->selected, view->nlines,
1439 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1442 e5a0f69f 2018-08-18 stsp static const struct got_error *
1443 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1444 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1446 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1447 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1448 5036bf37 2018-09-24 stsp char *parent_path;
1449 669b5ffa 2018-10-07 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
1450 669b5ffa 2018-10-07 stsp int begin_x = 0;
1452 e5a0f69f 2018-08-18 stsp switch (ch) {
1454 1a76625f 2018-10-22 stsp s->quit = 1;
1457 e5a0f69f 2018-08-18 stsp case KEY_UP:
1458 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1459 e5a0f69f 2018-08-18 stsp s->selected--;
1460 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1462 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1463 e5a0f69f 2018-08-18 stsp &s->commits);
1465 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1466 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1467 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1468 e5a0f69f 2018-08-18 stsp s->selected = 0;
1471 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1472 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1475 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1476 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1477 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1478 e5a0f69f 2018-08-18 stsp s->selected++;
1481 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1482 93e45b7c 2018-09-24 stsp &s->last_displayed_entry, &s->commits,
1483 1a76625f 2018-10-22 stsp &s->thread_args.log_complete,
1484 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1485 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1487 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1488 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1489 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1490 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1491 93e45b7c 2018-09-24 stsp view->nlines, &s->last_displayed_entry,
1492 1a76625f 2018-10-22 stsp &s->commits, &s->thread_args.log_complete,
1493 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1494 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1495 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1496 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1497 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1498 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1499 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1500 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1502 e5a0f69f 2018-08-18 stsp err = NULL;
1505 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1506 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1507 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1508 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1509 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1511 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1512 e5a0f69f 2018-08-18 stsp case '\r':
1513 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1514 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1515 669b5ffa 2018-10-07 stsp err = open_diff_view_for_commit(&diff_view, begin_x,
1516 2a4718d3 2018-09-29 stsp s->selected_entry->id, s->selected_entry->commit,
1520 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1521 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1523 669b5ffa 2018-10-07 stsp return err;
1524 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
1525 669b5ffa 2018-10-07 stsp if (err) {
1526 669b5ffa 2018-10-07 stsp view_close(diff_view);
1529 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(diff_view)) {
1530 669b5ffa 2018-10-07 stsp *focus_view = diff_view;
1531 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1534 669b5ffa 2018-10-07 stsp *new_view = diff_view;
1537 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1538 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1539 669b5ffa 2018-10-07 stsp err = browse_commit(&tree_view, begin_x,
1540 0cf4efb1 2018-09-29 stsp s->selected_entry, s->repo);
1541 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1542 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1544 669b5ffa 2018-10-07 stsp return err;
1545 669b5ffa 2018-10-07 stsp err = view_set_child(view, tree_view);
1546 669b5ffa 2018-10-07 stsp if (err) {
1547 669b5ffa 2018-10-07 stsp view_close(tree_view);
1550 669b5ffa 2018-10-07 stsp *focus_view = tree_view;
1551 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1553 669b5ffa 2018-10-07 stsp *new_view = tree_view;
1555 5036bf37 2018-09-24 stsp case KEY_BACKSPACE:
1556 5036bf37 2018-09-24 stsp if (strcmp(s->in_repo_path, "/") == 0)
1558 5036bf37 2018-09-24 stsp parent_path = dirname(s->in_repo_path);
1559 5036bf37 2018-09-24 stsp if (parent_path && strcmp(parent_path, ".") != 0) {
1560 5036bf37 2018-09-24 stsp struct tog_view *lv;
1561 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1563 1a76625f 2018-10-22 stsp return err;
1564 0cf4efb1 2018-09-29 stsp lv = view_open(view->nlines, view->ncols,
1565 0cf4efb1 2018-09-29 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
1566 5036bf37 2018-09-24 stsp if (lv == NULL)
1567 5036bf37 2018-09-24 stsp return got_error_from_errno();
1568 5036bf37 2018-09-24 stsp err = open_log_view(lv, s->start_id, s->repo,
1569 23721109 2018-10-22 stsp parent_path, 0);
1571 1a76625f 2018-10-22 stsp return err;;
1572 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1573 669b5ffa 2018-10-07 stsp *new_view = lv;
1575 669b5ffa 2018-10-07 stsp view_set_child(view->parent, lv);
1576 669b5ffa 2018-10-07 stsp *dead_view = view;
1578 1a76625f 2018-10-22 stsp return NULL;
1585 80ddbec8 2018-04-29 stsp return err;
1588 4ed7e80c 2018-05-20 stsp static const struct got_error *
1589 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1591 80ddbec8 2018-04-29 stsp const struct got_error *error;
1592 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1593 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1594 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1595 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1597 04cc582a 2018-08-01 stsp struct tog_view *view;
1599 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1600 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1602 80ddbec8 2018-04-29 stsp err(1, "pledge");
1605 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1606 80ddbec8 2018-04-29 stsp switch (ch) {
1608 80ddbec8 2018-04-29 stsp start_commit = optarg;
1611 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1612 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1613 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1617 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1621 80ddbec8 2018-04-29 stsp argc -= optind;
1622 80ddbec8 2018-04-29 stsp argv += optind;
1624 ecb28ae0 2018-07-16 stsp if (argc == 0)
1625 ecb28ae0 2018-07-16 stsp path = strdup("");
1626 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1627 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1629 80ddbec8 2018-04-29 stsp usage_log();
1630 ecb28ae0 2018-07-16 stsp if (path == NULL)
1631 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1633 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1634 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1635 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1636 ecb28ae0 2018-07-16 stsp goto done;
1638 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1639 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1640 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1641 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1642 ecb28ae0 2018-07-16 stsp goto done;
1646 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1647 80ddbec8 2018-04-29 stsp if (error != NULL)
1648 ecb28ae0 2018-07-16 stsp goto done;
1650 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1651 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1652 80ddbec8 2018-04-29 stsp if (error != NULL)
1653 ecb28ae0 2018-07-16 stsp goto done;
1655 80ddbec8 2018-04-29 stsp struct got_object *obj;
1656 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1657 80ddbec8 2018-04-29 stsp if (error == NULL) {
1658 6402fb3c 2018-09-15 stsp start_id = got_object_id_dup(got_object_get_id(obj));
1659 899d86c2 2018-05-10 stsp if (start_id == NULL)
1660 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1661 ecb28ae0 2018-07-16 stsp goto done;
1664 80ddbec8 2018-04-29 stsp if (error != NULL)
1665 ecb28ae0 2018-07-16 stsp goto done;
1667 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1668 04cc582a 2018-08-01 stsp if (view == NULL) {
1669 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1670 04cc582a 2018-08-01 stsp goto done;
1672 23721109 2018-10-22 stsp error = open_log_view(view, start_id, repo, path, 1);
1673 ba4f502b 2018-08-04 stsp if (error)
1674 ba4f502b 2018-08-04 stsp goto done;
1675 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1677 ecb28ae0 2018-07-16 stsp free(repo_path);
1678 ecb28ae0 2018-07-16 stsp free(cwd);
1679 ecb28ae0 2018-07-16 stsp free(path);
1680 899d86c2 2018-05-10 stsp free(start_id);
1682 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1683 80ddbec8 2018-04-29 stsp return error;
1686 4ed7e80c 2018-05-20 stsp __dead static void
1687 9f7d7167 2018-04-29 stsp usage_diff(void)
1690 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1691 9f7d7167 2018-04-29 stsp getprogname());
1695 b304db33 2018-05-20 stsp static char *
1696 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1698 b304db33 2018-05-20 stsp char *line;
1699 b304db33 2018-05-20 stsp size_t linelen;
1700 b304db33 2018-05-20 stsp size_t lineno;
1701 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1703 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1705 b304db33 2018-05-20 stsp *len = linelen;
1706 b304db33 2018-05-20 stsp return line;
1709 4ed7e80c 2018-05-20 stsp static const struct got_error *
1710 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1711 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1712 a3404814 2018-09-02 stsp char * header)
1714 61e69b96 2018-05-20 stsp const struct got_error *err;
1715 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1716 b304db33 2018-05-20 stsp char *line;
1717 b304db33 2018-05-20 stsp size_t len;
1718 61e69b96 2018-05-20 stsp wchar_t *wline;
1719 e0b650dd 2018-05-20 stsp int width;
1721 26ed57b2 2018-05-19 stsp rewind(f);
1722 f7d12f7e 2018-08-01 stsp werase(view->window);
1724 a3404814 2018-09-02 stsp if (header) {
1725 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1726 a3404814 2018-09-02 stsp if (err) {
1727 a3404814 2018-09-02 stsp return err;
1730 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1731 a3404814 2018-09-02 stsp wstandout(view->window);
1732 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1733 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1734 a3404814 2018-09-02 stsp wstandend(view->window);
1735 a3404814 2018-09-02 stsp if (width < view->ncols)
1736 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1738 a3404814 2018-09-02 stsp if (max_lines <= 1)
1739 a3404814 2018-09-02 stsp return NULL;
1740 a3404814 2018-09-02 stsp max_lines--;
1744 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1745 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1746 26ed57b2 2018-05-19 stsp if (line == NULL) {
1750 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1751 26ed57b2 2018-05-19 stsp free(line);
1755 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1756 61e69b96 2018-05-20 stsp if (err) {
1757 61e69b96 2018-05-20 stsp free(line);
1758 61e69b96 2018-05-20 stsp return err;
1760 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1761 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1762 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1763 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1764 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1765 26ed57b2 2018-05-19 stsp free(line);
1766 2550e4c3 2018-07-13 stsp free(wline);
1767 2550e4c3 2018-07-13 stsp wline = NULL;
1769 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1771 1a57306a 2018-09-02 stsp view_vborder(view);
1773 26ed57b2 2018-05-19 stsp return NULL;
1776 4ed7e80c 2018-05-20 stsp static const struct got_error *
1777 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
1779 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
1780 48ae06ee 2018-10-18 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1781 48ae06ee 2018-10-18 stsp FILE *f = NULL;
1783 48ae06ee 2018-10-18 stsp if (s->id1) {
1784 48ae06ee 2018-10-18 stsp err = got_object_open(&obj1, s->repo, s->id1);
1786 48ae06ee 2018-10-18 stsp return err;
1789 48ae06ee 2018-10-18 stsp err = got_object_open(&obj2, s->repo, s->id2);
1791 48ae06ee 2018-10-18 stsp goto done;
1793 511a516b 2018-05-19 stsp f = got_opentemp();
1794 48ae06ee 2018-10-18 stsp if (f == NULL) {
1795 48ae06ee 2018-10-18 stsp err = got_error_from_errno();
1796 48ae06ee 2018-10-18 stsp goto done;
1799 48ae06ee 2018-10-18 stsp fclose(s->f);
1802 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1803 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1804 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1805 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1807 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1808 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_trees(obj1, obj2, "", "",
1809 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1811 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1812 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1813 48ae06ee 2018-10-18 stsp s->repo, f);
1816 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1821 48ae06ee 2018-10-18 stsp got_object_close(obj1);
1822 48ae06ee 2018-10-18 stsp got_object_close(obj2);
1824 48ae06ee 2018-10-18 stsp fflush(f);
1825 48ae06ee 2018-10-18 stsp return err;
1828 48ae06ee 2018-10-18 stsp static const struct got_error *
1829 48ae06ee 2018-10-18 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1830 48ae06ee 2018-10-18 stsp struct got_object *obj2, struct got_repository *repo)
1832 48ae06ee 2018-10-18 stsp const struct got_error *err;
1834 48ae06ee 2018-10-18 stsp if (obj1 != NULL && obj2 != NULL &&
1835 48ae06ee 2018-10-18 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1836 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
1838 48ae06ee 2018-10-18 stsp if (obj1) {
1839 48ae06ee 2018-10-18 stsp struct got_object_id *id1;
1840 48ae06ee 2018-10-18 stsp id1 = got_object_id_dup(got_object_get_id(obj1));
1841 48ae06ee 2018-10-18 stsp if (id1 == NULL)
1842 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1843 48ae06ee 2018-10-18 stsp view->state.diff.id1 = id1;
1845 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1847 48ae06ee 2018-10-18 stsp view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1848 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
1849 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1850 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1851 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1853 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
1854 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1855 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1856 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
1857 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
1859 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
1860 48ae06ee 2018-10-18 stsp if (err) {
1861 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1862 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1863 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
1864 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
1865 48ae06ee 2018-10-18 stsp return err;
1868 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
1869 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
1870 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
1872 5dc9f4bc 2018-08-04 stsp return NULL;
1875 e5a0f69f 2018-08-18 stsp static const struct got_error *
1876 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1878 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1880 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1881 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1882 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
1883 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
1884 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1885 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1886 e5a0f69f 2018-08-18 stsp return err;
1889 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1890 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1892 a3404814 2018-09-02 stsp const struct got_error *err;
1893 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1894 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
1896 a3404814 2018-09-02 stsp if (s->id1) {
1897 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
1899 a3404814 2018-09-02 stsp return err;
1901 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
1903 a3404814 2018-09-02 stsp return err;
1905 a3404814 2018-09-02 stsp if (asprintf(&header, "diff: %s %s",
1906 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1907 a3404814 2018-09-02 stsp err = got_error_from_errno();
1908 a3404814 2018-09-02 stsp free(id_str1);
1909 a3404814 2018-09-02 stsp free(id_str2);
1910 a3404814 2018-09-02 stsp return err;
1912 a3404814 2018-09-02 stsp free(id_str1);
1913 a3404814 2018-09-02 stsp free(id_str2);
1915 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
1916 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
1920 0cf4efb1 2018-09-29 stsp static const struct got_error *
1921 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1922 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1924 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1925 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
1928 e5a0f69f 2018-08-18 stsp switch (ch) {
1930 e5a0f69f 2018-08-18 stsp case KEY_UP:
1931 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
1932 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1934 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1936 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
1937 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
1938 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1941 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1942 e5a0f69f 2018-08-18 stsp if (!s->eof)
1943 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1945 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
1948 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
1949 e5a0f69f 2018-08-18 stsp char *line;
1950 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
1951 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1952 e5a0f69f 2018-08-18 stsp if (line == NULL)
1957 48ae06ee 2018-10-18 stsp if (s->diff_context > 0) {
1958 48ae06ee 2018-10-18 stsp s->diff_context--;
1959 48ae06ee 2018-10-18 stsp err = create_diff(s);
1963 4a8520aa 2018-10-18 stsp if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
1964 48ae06ee 2018-10-18 stsp s->diff_context++;
1965 48ae06ee 2018-10-18 stsp err = create_diff(s);
1972 bcbd79e2 2018-08-19 stsp return err;
1975 4ed7e80c 2018-05-20 stsp static const struct got_error *
1976 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1978 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1979 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1980 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1981 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1982 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1984 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1986 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1987 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1989 26ed57b2 2018-05-19 stsp err(1, "pledge");
1992 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1993 26ed57b2 2018-05-19 stsp switch (ch) {
1996 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2000 26ed57b2 2018-05-19 stsp argc -= optind;
2001 26ed57b2 2018-05-19 stsp argv += optind;
2003 26ed57b2 2018-05-19 stsp if (argc == 0) {
2004 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2005 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2006 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2007 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2008 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2009 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
2010 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
2011 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2012 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2013 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2014 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2015 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
2016 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
2018 26ed57b2 2018-05-19 stsp usage_diff();
2020 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
2021 26ed57b2 2018-05-19 stsp free(repo_path);
2022 26ed57b2 2018-05-19 stsp if (error)
2023 26ed57b2 2018-05-19 stsp goto done;
2025 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
2026 26ed57b2 2018-05-19 stsp if (error)
2027 26ed57b2 2018-05-19 stsp goto done;
2029 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
2030 26ed57b2 2018-05-19 stsp if (error)
2031 26ed57b2 2018-05-19 stsp goto done;
2033 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2034 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2035 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
2036 ea5e7bb5 2018-08-01 stsp goto done;
2038 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
2039 5dc9f4bc 2018-08-04 stsp if (error)
2040 5dc9f4bc 2018-08-04 stsp goto done;
2041 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2043 26ed57b2 2018-05-19 stsp got_repo_close(repo);
2045 26ed57b2 2018-05-19 stsp got_object_close(obj1);
2047 26ed57b2 2018-05-19 stsp got_object_close(obj2);
2048 26ed57b2 2018-05-19 stsp return error;
2051 4ed7e80c 2018-05-20 stsp __dead static void
2052 9f7d7167 2018-04-29 stsp usage_blame(void)
2055 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2056 9f7d7167 2018-04-29 stsp getprogname());
2060 84451b3e 2018-07-10 stsp struct tog_blame_line {
2061 84451b3e 2018-07-10 stsp int annotated;
2062 84451b3e 2018-07-10 stsp struct got_object_id *id;
2065 4ed7e80c 2018-05-20 stsp static const struct got_error *
2066 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2067 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2068 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2069 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2071 84451b3e 2018-07-10 stsp const struct got_error *err;
2072 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2073 84451b3e 2018-07-10 stsp char *line;
2074 84451b3e 2018-07-10 stsp size_t len;
2075 84451b3e 2018-07-10 stsp wchar_t *wline;
2076 b700b5d6 2018-07-10 stsp int width, wlimit;
2077 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2078 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2079 ab089a2a 2018-07-12 stsp char *id_str;
2081 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2083 ab089a2a 2018-07-12 stsp return err;
2085 84451b3e 2018-07-10 stsp rewind(f);
2086 f7d12f7e 2018-08-01 stsp werase(view->window);
2088 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
2089 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
2090 ab089a2a 2018-07-12 stsp free(id_str);
2091 ab089a2a 2018-07-12 stsp return err;
2094 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2095 ab089a2a 2018-07-12 stsp free(line);
2096 2550e4c3 2018-07-13 stsp line = NULL;
2097 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2098 a3404814 2018-09-02 stsp wstandout(view->window);
2099 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2100 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2101 a3404814 2018-09-02 stsp wstandend(view->window);
2102 2550e4c3 2018-07-13 stsp free(wline);
2103 2550e4c3 2018-07-13 stsp wline = NULL;
2104 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2105 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2107 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2108 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2109 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
2110 ab089a2a 2018-07-12 stsp free(id_str);
2111 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
2113 ab089a2a 2018-07-12 stsp free(id_str);
2114 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2115 3f60a8ef 2018-07-10 stsp free(line);
2116 2550e4c3 2018-07-13 stsp line = NULL;
2118 3f60a8ef 2018-07-10 stsp return err;
2119 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2120 2550e4c3 2018-07-13 stsp free(wline);
2121 2550e4c3 2018-07-13 stsp wline = NULL;
2122 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2123 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2126 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2127 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2128 84451b3e 2018-07-10 stsp if (line == NULL) {
2132 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2133 84451b3e 2018-07-10 stsp free(line);
2137 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2138 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2139 84451b3e 2018-07-10 stsp if (err) {
2140 84451b3e 2018-07-10 stsp free(line);
2141 84451b3e 2018-07-10 stsp return err;
2144 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2145 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2147 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2148 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2149 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2150 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2151 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2152 84451b3e 2018-07-10 stsp char *id_str;
2153 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2154 84451b3e 2018-07-10 stsp if (err) {
2155 84451b3e 2018-07-10 stsp free(line);
2156 2550e4c3 2018-07-13 stsp free(wline);
2157 84451b3e 2018-07-10 stsp return err;
2159 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2160 84451b3e 2018-07-10 stsp free(id_str);
2161 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2163 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2164 ee41ec32 2018-07-10 stsp prev_id = NULL;
2167 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2168 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2169 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2172 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2173 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2174 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2175 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2176 84451b3e 2018-07-10 stsp free(line);
2177 2550e4c3 2018-07-13 stsp free(wline);
2178 2550e4c3 2018-07-13 stsp wline = NULL;
2180 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2182 1a57306a 2018-09-02 stsp view_vborder(view);
2184 84451b3e 2018-07-10 stsp return NULL;
2187 84451b3e 2018-07-10 stsp static const struct got_error *
2188 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2190 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2191 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2192 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2193 1a76625f 2018-10-22 stsp int errcode;
2195 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2196 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2197 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2199 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2200 1a76625f 2018-10-22 stsp if (errcode)
2201 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2203 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2204 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2205 d68a0a7d 2018-07-10 stsp goto done;
2208 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2209 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2211 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2212 d68a0a7d 2018-07-10 stsp if (line->annotated)
2213 d68a0a7d 2018-07-10 stsp goto done;
2215 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2216 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2217 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2218 84451b3e 2018-07-10 stsp goto done;
2220 84451b3e 2018-07-10 stsp line->annotated = 1;
2222 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
2223 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
2224 e5a0f69f 2018-08-18 stsp a->last_displayed_line, a->eof, a->view->nlines);
2226 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2227 1a76625f 2018-10-22 stsp if (errcode)
2228 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2229 84451b3e 2018-07-10 stsp return err;
2232 84451b3e 2018-07-10 stsp static void *
2233 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2235 18430de3 2018-07-10 stsp const struct got_error *err;
2236 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
2237 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
2238 1a76625f 2018-10-22 stsp int errcode;
2240 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2241 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
2243 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2244 1a76625f 2018-10-22 stsp if (errcode)
2245 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
2247 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
2248 c9beca56 2018-07-22 stsp ta->repo = NULL;
2249 c9beca56 2018-07-22 stsp *ta->complete = 1;
2250 1a76625f 2018-10-22 stsp if (!err) {
2251 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
2252 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
2253 e5a0f69f 2018-08-18 stsp a->first_displayed_line, a->last_displayed_line, a->eof,
2254 f7d12f7e 2018-08-01 stsp a->view->nlines);
2257 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2258 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2259 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2261 18430de3 2018-07-10 stsp return (void *)err;
2264 245d91c1 2018-07-12 stsp static struct got_object_id *
2265 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
2266 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
2268 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2270 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2271 245d91c1 2018-07-12 stsp if (!line->annotated)
2272 245d91c1 2018-07-12 stsp return NULL;
2274 245d91c1 2018-07-12 stsp return line->id;
2277 84451b3e 2018-07-10 stsp static const struct got_error *
2278 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
2279 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
2280 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
2282 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
2283 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
2284 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
2285 b880a918 2018-07-10 stsp struct got_object_qid *pid;
2287 b880a918 2018-07-10 stsp *pobj = NULL;
2288 b880a918 2018-07-10 stsp *obj = NULL;
2290 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
2291 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
2292 245d91c1 2018-07-12 stsp if (selected_id == NULL)
2293 b880a918 2018-07-10 stsp return NULL;
2295 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
2297 b880a918 2018-07-10 stsp goto done;
2299 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
2301 b880a918 2018-07-10 stsp goto done;
2303 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
2304 b880a918 2018-07-10 stsp if (pid) {
2305 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
2307 b880a918 2018-07-10 stsp goto done;
2310 b880a918 2018-07-10 stsp if (commit)
2311 b880a918 2018-07-10 stsp got_object_commit_close(commit);
2312 b880a918 2018-07-10 stsp return err;
2315 b880a918 2018-07-10 stsp static const struct got_error *
2316 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2318 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2321 245d91c1 2018-07-12 stsp if (blame->thread) {
2322 1a76625f 2018-10-22 stsp int errcode;
2323 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2324 1a76625f 2018-10-22 stsp if (errcode)
2325 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2326 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
2327 1a76625f 2018-10-22 stsp if (errcode)
2328 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2329 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2330 1a76625f 2018-10-22 stsp if (errcode)
2331 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2332 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2333 245d91c1 2018-07-12 stsp err = NULL;
2334 245d91c1 2018-07-12 stsp blame->thread = NULL;
2336 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2337 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2338 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2340 245d91c1 2018-07-12 stsp if (blame->f) {
2341 245d91c1 2018-07-12 stsp fclose(blame->f);
2342 245d91c1 2018-07-12 stsp blame->f = NULL;
2344 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
2345 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
2346 245d91c1 2018-07-12 stsp free(blame->lines);
2347 245d91c1 2018-07-12 stsp blame->lines = NULL;
2348 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2349 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2351 245d91c1 2018-07-12 stsp return err;
2354 245d91c1 2018-07-12 stsp static const struct got_error *
2355 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2356 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
2357 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
2358 245d91c1 2018-07-12 stsp struct got_repository *repo)
2360 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2361 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2362 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2363 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2364 27d434c2 2018-09-15 stsp struct got_object *obj = NULL;
2365 1a76625f 2018-10-22 stsp int errcode;
2367 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2369 27d434c2 2018-09-15 stsp goto done;
2371 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
2373 84451b3e 2018-07-10 stsp goto done;
2375 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2376 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2377 84451b3e 2018-07-10 stsp goto done;
2380 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
2382 a70480e0 2018-06-23 stsp goto done;
2383 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2384 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2385 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2386 84451b3e 2018-07-10 stsp goto done;
2388 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2389 245d91c1 2018-07-12 stsp blame->f, blob);
2391 84451b3e 2018-07-10 stsp goto done;
2393 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2394 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2395 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2396 84451b3e 2018-07-10 stsp goto done;
2399 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2401 bd24772e 2018-07-11 stsp goto done;
2403 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2404 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2405 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2406 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2407 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2408 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2409 245d91c1 2018-07-12 stsp goto done;
2411 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
2412 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
2413 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
2414 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
2415 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
2416 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2417 e5a0f69f 2018-08-18 stsp blame->cb_args.eof = eof;
2419 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2420 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2421 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2422 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2423 245d91c1 2018-07-12 stsp *blame_complete = 0;
2425 1a76625f 2018-10-22 stsp errcode = pthread_create(&blame->thread, NULL, blame_thread,
2426 1a76625f 2018-10-22 stsp &blame->thread_args);
2427 1a76625f 2018-10-22 stsp if (errcode) {
2428 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2429 245d91c1 2018-07-12 stsp goto done;
2434 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2435 27d434c2 2018-09-15 stsp free(obj_id);
2437 245d91c1 2018-07-12 stsp got_object_close(obj);
2439 245d91c1 2018-07-12 stsp stop_blame(blame);
2440 245d91c1 2018-07-12 stsp return err;
2443 245d91c1 2018-07-12 stsp static const struct got_error *
2444 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2445 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2447 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2448 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2450 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2452 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2454 7cbe629d 2018-08-04 stsp return err;
2456 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2457 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2458 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2459 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2460 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2461 fb2756b9 2018-08-04 stsp s->path = path;
2462 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2463 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2464 fb2756b9 2018-08-04 stsp s->repo = repo;
2465 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2466 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2468 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2469 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2470 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2472 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
2473 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2474 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2475 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2478 e5a0f69f 2018-08-18 stsp static const struct got_error *
2479 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2481 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2482 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2484 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2485 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2487 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2488 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2489 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2490 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2491 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2494 e5a0f69f 2018-08-18 stsp free(s->path);
2496 e5a0f69f 2018-08-18 stsp return err;
2499 7cbe629d 2018-08-04 stsp static const struct got_error *
2500 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2502 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2503 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2505 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2506 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2507 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2508 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2510 669b5ffa 2018-10-07 stsp view_vborder(view);
2511 e5a0f69f 2018-08-18 stsp return err;
2514 e5a0f69f 2018-08-18 stsp static const struct got_error *
2515 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2516 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2518 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2519 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
2520 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2521 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2522 669b5ffa 2018-10-07 stsp int begin_x = 0;
2524 e5a0f69f 2018-08-18 stsp switch (ch) {
2526 e5a0f69f 2018-08-18 stsp s->done = 1;
2529 e5a0f69f 2018-08-18 stsp case KEY_UP:
2530 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2531 e5a0f69f 2018-08-18 stsp s->selected_line--;
2532 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2533 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2534 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2536 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2537 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2538 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2541 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2542 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2543 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2545 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2548 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2549 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2550 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2551 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2552 e5a0f69f 2018-08-18 stsp s->selected_line++;
2553 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2554 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2555 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2558 e5a0f69f 2018-08-18 stsp case 'p': {
2559 e5a0f69f 2018-08-18 stsp struct got_object_id *id;
2560 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2561 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2562 e5a0f69f 2018-08-18 stsp if (id == NULL || got_object_id_cmp(id,
2563 e5a0f69f 2018-08-18 stsp s->blamed_commit->id) == 0)
2565 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2566 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2567 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2570 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2572 e5a0f69f 2018-08-18 stsp if (ch == 'p' && pobj == NULL)
2574 e5a0f69f 2018-08-18 stsp s->done = 1;
2575 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2576 e5a0f69f 2018-08-18 stsp s->done = 0;
2577 e5a0f69f 2018-08-18 stsp if (thread_err)
2579 e5a0f69f 2018-08-18 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
2580 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2581 e5a0f69f 2018-08-18 stsp obj = NULL;
2582 e5a0f69f 2018-08-18 stsp if (pobj) {
2583 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2584 e5a0f69f 2018-08-18 stsp pobj = NULL;
2586 6402fb3c 2018-09-15 stsp err = got_object_qid_alloc(&s->blamed_commit, id);
2588 e5a0f69f 2018-08-18 stsp goto done;
2589 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2590 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2591 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2592 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2593 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2594 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2599 e5a0f69f 2018-08-18 stsp case 'B': {
2600 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2601 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2602 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2604 e5a0f69f 2018-08-18 stsp s->done = 1;
2605 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2606 e5a0f69f 2018-08-18 stsp s->done = 0;
2607 e5a0f69f 2018-08-18 stsp if (thread_err)
2609 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2610 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2611 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2612 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2613 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2614 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2615 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2616 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2621 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2622 e5a0f69f 2018-08-18 stsp case '\r':
2623 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2624 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2625 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2628 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2631 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
2632 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
2633 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2634 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2635 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2638 669b5ffa 2018-10-07 stsp err = open_diff_view(diff_view, pobj, obj, s->repo);
2639 e5a0f69f 2018-08-18 stsp if (err) {
2640 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2643 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
2644 669b5ffa 2018-10-07 stsp err = view_close_child(view);
2646 669b5ffa 2018-10-07 stsp return err;
2647 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
2648 669b5ffa 2018-10-07 stsp if (err) {
2649 669b5ffa 2018-10-07 stsp view_close(diff_view);
2652 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(diff_view)) {
2653 669b5ffa 2018-10-07 stsp *focus_view = diff_view;
2654 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
2657 669b5ffa 2018-10-07 stsp *new_view = diff_view;
2658 e5a0f69f 2018-08-18 stsp if (pobj) {
2659 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2660 e5a0f69f 2018-08-18 stsp pobj = NULL;
2662 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2663 e5a0f69f 2018-08-18 stsp obj = NULL;
2667 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2669 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2670 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2671 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2672 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2675 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2676 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2677 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2678 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2680 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2681 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2682 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2684 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2685 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2686 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2687 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2695 b880a918 2018-07-10 stsp got_object_close(pobj);
2696 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2699 a70480e0 2018-06-23 stsp static const struct got_error *
2700 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2702 a70480e0 2018-06-23 stsp const struct got_error *error;
2703 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2704 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2705 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2706 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2708 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2710 a70480e0 2018-06-23 stsp #ifndef PROFILE
2711 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2713 a70480e0 2018-06-23 stsp err(1, "pledge");
2716 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2717 a70480e0 2018-06-23 stsp switch (ch) {
2719 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2722 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2723 69069811 2018-08-02 stsp if (repo_path == NULL)
2724 69069811 2018-08-02 stsp err(1, "-r option");
2728 a70480e0 2018-06-23 stsp /* NOTREACHED */
2732 a70480e0 2018-06-23 stsp argc -= optind;
2733 a70480e0 2018-06-23 stsp argv += optind;
2735 69069811 2018-08-02 stsp if (argc == 1)
2736 69069811 2018-08-02 stsp path = argv[0];
2738 a70480e0 2018-06-23 stsp usage_blame();
2740 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2741 69069811 2018-08-02 stsp if (cwd == NULL) {
2742 69069811 2018-08-02 stsp error = got_error_from_errno();
2743 69069811 2018-08-02 stsp goto done;
2745 69069811 2018-08-02 stsp if (repo_path == NULL) {
2746 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2747 69069811 2018-08-02 stsp if (repo_path == NULL) {
2748 69069811 2018-08-02 stsp error = got_error_from_errno();
2749 69069811 2018-08-02 stsp goto done;
2754 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2755 a70480e0 2018-06-23 stsp if (error != NULL)
2756 66b4983c 2018-06-23 stsp return error;
2758 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2759 69069811 2018-08-02 stsp if (error != NULL)
2760 69069811 2018-08-02 stsp goto done;
2762 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2763 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2764 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2765 a70480e0 2018-06-23 stsp if (error != NULL)
2766 66b4983c 2018-06-23 stsp goto done;
2767 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2768 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2770 a70480e0 2018-06-23 stsp struct got_object *obj;
2771 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2772 a70480e0 2018-06-23 stsp if (error != NULL)
2773 66b4983c 2018-06-23 stsp goto done;
2774 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
2775 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2776 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2777 a19e88aa 2018-06-23 stsp got_object_close(obj);
2779 a19e88aa 2018-06-23 stsp if (error != NULL)
2780 a19e88aa 2018-06-23 stsp goto done;
2782 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2783 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2784 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2785 e1cd8fed 2018-08-01 stsp goto done;
2787 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2788 7cbe629d 2018-08-04 stsp if (error)
2789 7cbe629d 2018-08-04 stsp goto done;
2790 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2792 69069811 2018-08-02 stsp free(repo_path);
2793 69069811 2018-08-02 stsp free(cwd);
2794 a70480e0 2018-06-23 stsp free(commit_id);
2796 a70480e0 2018-06-23 stsp got_repo_close(repo);
2797 a70480e0 2018-06-23 stsp return error;
2800 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2801 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2802 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2803 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2804 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2805 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2806 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2808 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2809 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2810 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2811 ffd1d5e5 2018-06-23 stsp int width, n;
2813 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2815 f7d12f7e 2018-08-01 stsp werase(view->window);
2817 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2818 ffd1d5e5 2018-06-23 stsp return NULL;
2820 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2822 ffd1d5e5 2018-06-23 stsp return err;
2823 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2824 a3404814 2018-09-02 stsp wstandout(view->window);
2825 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2826 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2827 a3404814 2018-09-02 stsp wstandend(view->window);
2828 2550e4c3 2018-07-13 stsp free(wline);
2829 2550e4c3 2018-07-13 stsp wline = NULL;
2830 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2831 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2832 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2833 ffd1d5e5 2018-06-23 stsp return NULL;
2834 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2836 ce52c690 2018-06-23 stsp return err;
2837 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2838 2550e4c3 2018-07-13 stsp free(wline);
2839 2550e4c3 2018-07-13 stsp wline = NULL;
2840 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2841 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2842 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2843 ffd1d5e5 2018-06-23 stsp return NULL;
2844 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2845 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2846 a1eca9bb 2018-06-23 stsp return NULL;
2848 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2849 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2850 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2851 0cf4efb1 2018-09-29 stsp if (view->focussed)
2852 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2853 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2855 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2856 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
2857 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2858 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2859 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2860 ffd1d5e5 2018-06-23 stsp return NULL;
2864 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2865 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2868 ffd1d5e5 2018-06-23 stsp while (te) {
2869 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2871 1d13200f 2018-07-12 stsp if (show_ids) {
2872 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2874 1d13200f 2018-07-12 stsp return got_error_from_errno();
2876 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2877 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2878 1d13200f 2018-07-12 stsp free(id_str);
2879 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2881 1d13200f 2018-07-12 stsp free(id_str);
2882 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2883 ffd1d5e5 2018-06-23 stsp if (err) {
2884 ffd1d5e5 2018-06-23 stsp free(line);
2887 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2888 0cf4efb1 2018-09-29 stsp if (view->focussed)
2889 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2890 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2892 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2893 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2894 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2895 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
2896 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2897 ffd1d5e5 2018-06-23 stsp free(line);
2898 2550e4c3 2018-07-13 stsp free(wline);
2899 2550e4c3 2018-07-13 stsp wline = NULL;
2901 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2902 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2903 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2905 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2908 ffd1d5e5 2018-06-23 stsp return err;
2911 ffd1d5e5 2018-06-23 stsp static void
2912 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2913 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2915 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2918 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2921 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2922 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2923 ffd1d5e5 2018-06-23 stsp if (!isroot)
2924 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2928 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2929 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2930 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2931 ffd1d5e5 2018-06-23 stsp prev = te;
2932 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2934 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2935 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2937 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2938 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2941 ffd1d5e5 2018-06-23 stsp static void
2942 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2943 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2944 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2946 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2947 ffd1d5e5 2018-06-23 stsp int n = 0;
2949 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2952 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2953 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2955 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2956 ffd1d5e5 2018-06-23 stsp while (next) {
2957 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2958 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2960 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2964 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2965 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2966 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2968 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2969 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2970 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2972 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2973 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2975 ce52c690 2018-06-23 stsp len += strlen(te->name);
2977 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2978 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2979 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2981 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2982 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2983 d9765a41 2018-06-23 stsp while (pt) {
2984 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2985 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2986 cb2ebc8a 2018-06-23 stsp goto done;
2988 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2989 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2990 cb2ebc8a 2018-06-23 stsp goto done;
2992 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2995 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2996 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2997 ce52c690 2018-06-23 stsp goto done;
3001 ce52c690 2018-06-23 stsp if (err) {
3002 ce52c690 2018-06-23 stsp free(*path);
3003 ce52c690 2018-06-23 stsp *path = NULL;
3005 ce52c690 2018-06-23 stsp return err;
3008 ce52c690 2018-06-23 stsp static const struct got_error *
3009 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3010 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3011 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
3013 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3014 ce52c690 2018-06-23 stsp char *path;
3015 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3017 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3019 ce52c690 2018-06-23 stsp return err;
3021 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3022 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3023 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3025 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
3026 e5a0f69f 2018-08-18 stsp if (err) {
3027 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3028 e5a0f69f 2018-08-18 stsp free(path);
3030 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3031 69efd4c4 2018-07-18 stsp return err;
3034 69efd4c4 2018-07-18 stsp static const struct got_error *
3035 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3036 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3037 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
3039 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3040 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3041 69efd4c4 2018-07-18 stsp char *path;
3043 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3044 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3045 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3047 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3049 69efd4c4 2018-07-18 stsp return err;
3051 23721109 2018-10-22 stsp err = open_log_view(log_view, commit_id, repo, path, 0);
3053 e5a0f69f 2018-08-18 stsp view_close(log_view);
3055 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3056 cb2ebc8a 2018-06-23 stsp free(path);
3057 cb2ebc8a 2018-06-23 stsp return err;
3060 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3061 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3062 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
3064 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3065 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3066 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3068 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3070 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3071 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3072 ffd1d5e5 2018-06-23 stsp goto done;
3074 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3075 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
3076 ffd1d5e5 2018-06-23 stsp goto done;
3079 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3080 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3081 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3082 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3083 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3084 6484ec90 2018-09-29 stsp err = got_error_from_errno();
3085 6484ec90 2018-09-29 stsp goto done;
3087 fb2756b9 2018-08-04 stsp s->repo = repo;
3089 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
3090 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
3091 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
3093 ad80ab7b 2018-08-04 stsp free(commit_id_str);
3094 6484ec90 2018-09-29 stsp if (err) {
3095 fb2756b9 2018-08-04 stsp free(s->tree_label);
3096 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3098 ad80ab7b 2018-08-04 stsp return err;
3101 e5a0f69f 2018-08-18 stsp static const struct got_error *
3102 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
3104 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3106 fb2756b9 2018-08-04 stsp free(s->tree_label);
3107 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3108 6484ec90 2018-09-29 stsp free(s->commit_id);
3109 6484ec90 2018-09-29 stsp s->commit_id = NULL;
3110 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
3111 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
3112 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
3113 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
3114 ad80ab7b 2018-08-04 stsp free(parent);
3117 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
3118 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
3119 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
3121 e5a0f69f 2018-08-18 stsp return NULL;
3124 ad80ab7b 2018-08-04 stsp static const struct got_error *
3125 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
3127 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
3128 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3129 e5a0f69f 2018-08-18 stsp char *parent_path;
3131 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
3133 e5a0f69f 2018-08-18 stsp return err;
3135 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
3136 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
3137 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3138 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
3139 e5a0f69f 2018-08-18 stsp free(parent_path);
3141 669b5ffa 2018-10-07 stsp view_vborder(view);
3142 e5a0f69f 2018-08-18 stsp return err;
3145 e5a0f69f 2018-08-18 stsp static const struct got_error *
3146 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3147 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3149 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3150 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
3151 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
3152 669b5ffa 2018-10-07 stsp int begin_x = 0;
3154 e5a0f69f 2018-08-18 stsp switch (ch) {
3156 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
3159 669b5ffa 2018-10-07 stsp if (!s->selected_entry)
3161 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
3162 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
3163 669b5ffa 2018-10-07 stsp err = log_tree_entry(&log_view, begin_x,
3164 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
3165 669b5ffa 2018-10-07 stsp s->commit_id, s->repo);
3166 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3167 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3169 669b5ffa 2018-10-07 stsp return err;
3170 669b5ffa 2018-10-07 stsp err = view_set_child(view, log_view);
3171 669b5ffa 2018-10-07 stsp if (err) {
3172 669b5ffa 2018-10-07 stsp view_close(log_view);
3175 669b5ffa 2018-10-07 stsp *focus_view = log_view;
3176 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3178 669b5ffa 2018-10-07 stsp *new_view = log_view;
3181 e5a0f69f 2018-08-18 stsp case KEY_UP:
3182 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3183 e5a0f69f 2018-08-18 stsp s->selected--;
3184 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3186 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
3187 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
3189 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
3190 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
3191 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
3192 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
3193 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3194 e5a0f69f 2018-08-18 stsp s->selected = 0;
3197 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
3198 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
3199 e5a0f69f 2018-08-18 stsp s->tree == s->root);
3202 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
3203 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
3204 e5a0f69f 2018-08-18 stsp s->selected++;
3207 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
3208 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
3210 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
3211 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
3212 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
3213 e5a0f69f 2018-08-18 stsp s->entries);
3214 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
3217 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
3218 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
3219 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3221 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
3222 e5a0f69f 2018-08-18 stsp case '\r':
3223 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
3224 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3225 7837eeac 2018-09-24 stsp case KEY_BACKSPACE:
3226 e5a0f69f 2018-08-18 stsp /* user selected '..' */
3227 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
3229 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
3230 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
3232 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
3233 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
3234 e5a0f69f 2018-08-18 stsp s->entries =
3235 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3236 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
3237 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
3238 e5a0f69f 2018-08-18 stsp s->selected_entry =
3239 e5a0f69f 2018-08-18 stsp parent->selected_entry;
3240 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
3241 e5a0f69f 2018-08-18 stsp free(parent);
3242 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
3243 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3244 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
3245 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
3246 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
3249 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
3250 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
3251 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
3254 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
3255 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
3256 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
3257 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
3258 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
3259 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3260 e5a0f69f 2018-08-18 stsp s->tree = child;
3261 e5a0f69f 2018-08-18 stsp s->entries =
3262 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3263 e5a0f69f 2018-08-18 stsp s->selected = 0;
3264 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3265 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
3266 669b5ffa 2018-10-07 stsp struct tog_view *blame_view;
3267 669b5ffa 2018-10-07 stsp int begin_x = view_is_parent_view(view) ?
3268 669b5ffa 2018-10-07 stsp view_split_begin_x(view->begin_x) : 0;
3270 669b5ffa 2018-10-07 stsp err = blame_tree_entry(&blame_view, begin_x,
3271 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents, s->commit_id,
3275 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3276 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3278 669b5ffa 2018-10-07 stsp return err;
3279 669b5ffa 2018-10-07 stsp err = view_set_child(view, blame_view);
3280 669b5ffa 2018-10-07 stsp if (err) {
3281 669b5ffa 2018-10-07 stsp view_close(blame_view);
3284 669b5ffa 2018-10-07 stsp *focus_view = blame_view;
3285 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3287 669b5ffa 2018-10-07 stsp *new_view = blame_view;
3290 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3291 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3292 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3298 ffd1d5e5 2018-06-23 stsp return err;
3301 ffd1d5e5 2018-06-23 stsp __dead static void
3302 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3305 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3306 ffd1d5e5 2018-06-23 stsp getprogname());
3310 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3311 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3313 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3314 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3315 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3316 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3317 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3318 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3319 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3321 5221c383 2018-08-01 stsp struct tog_view *view;
3323 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3324 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3326 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3329 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3330 ffd1d5e5 2018-06-23 stsp switch (ch) {
3332 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3336 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3340 ffd1d5e5 2018-06-23 stsp argc -= optind;
3341 ffd1d5e5 2018-06-23 stsp argv += optind;
3343 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3344 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
3345 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3346 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3347 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3348 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3349 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3350 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3352 ffd1d5e5 2018-06-23 stsp usage_log();
3354 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3355 ffd1d5e5 2018-06-23 stsp free(repo_path);
3356 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3357 ffd1d5e5 2018-06-23 stsp return error;
3359 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
3360 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3361 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3362 ffd1d5e5 2018-06-23 stsp goto done;
3364 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
3365 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3366 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
3367 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
3368 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
3369 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
3372 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3373 ffd1d5e5 2018-06-23 stsp goto done;
3375 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3376 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3377 ffd1d5e5 2018-06-23 stsp goto done;
3379 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3380 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3381 ffd1d5e5 2018-06-23 stsp goto done;
3383 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3384 5221c383 2018-08-01 stsp if (view == NULL) {
3385 5221c383 2018-08-01 stsp error = got_error_from_errno();
3386 5221c383 2018-08-01 stsp goto done;
3388 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3389 ad80ab7b 2018-08-04 stsp if (error)
3390 ad80ab7b 2018-08-04 stsp goto done;
3391 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3393 ffd1d5e5 2018-06-23 stsp free(commit_id);
3394 ffd1d5e5 2018-06-23 stsp if (commit)
3395 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3397 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3399 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3400 ffd1d5e5 2018-06-23 stsp return error;
3402 5c5136c5 2018-05-20 stsp static void
3403 9f7d7167 2018-04-29 stsp init_curses(void)
3405 9f7d7167 2018-04-29 stsp initscr();
3407 fd823528 2018-10-22 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3410 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3411 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3412 1f475ad8 2018-05-10 stsp curs_set(0);
3415 4ed7e80c 2018-05-20 stsp __dead static void
3416 9f7d7167 2018-04-29 stsp usage(void)
3420 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3421 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3422 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3423 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3424 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3429 c2301be8 2018-04-30 stsp static char **
3430 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3432 c2301be8 2018-04-30 stsp char **argv;
3433 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3435 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3436 c2301be8 2018-04-30 stsp if (argv == NULL)
3437 c2301be8 2018-04-30 stsp err(1, "calloc");
3438 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3439 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3440 c2301be8 2018-04-30 stsp err(1, "calloc");
3441 c2301be8 2018-04-30 stsp if (arg1) {
3442 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3443 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3444 c2301be8 2018-04-30 stsp err(1, "calloc");
3447 c2301be8 2018-04-30 stsp return argv;
3451 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3453 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3454 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3455 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3456 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3458 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
3460 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3461 9f7d7167 2018-04-29 stsp switch (ch) {
3463 9f7d7167 2018-04-29 stsp hflag = 1;
3467 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3471 9f7d7167 2018-04-29 stsp argc -= optind;
3472 9f7d7167 2018-04-29 stsp argv += optind;
3473 9f7d7167 2018-04-29 stsp optind = 0;
3474 c2301be8 2018-04-30 stsp optreset = 1;
3476 c2301be8 2018-04-30 stsp if (argc == 0) {
3477 f29d3e89 2018-06-23 stsp if (hflag)
3479 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3480 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3481 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3486 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3487 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3488 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3489 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3490 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3491 9f7d7167 2018-04-29 stsp if (hflag)
3492 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3496 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3497 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3498 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3499 c2301be8 2018-04-30 stsp if (repo_path) {
3500 c2301be8 2018-04-30 stsp struct got_repository *repo;
3501 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3502 c2301be8 2018-04-30 stsp if (error == NULL)
3503 c2301be8 2018-04-30 stsp got_repo_close(repo);
3505 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3506 c2301be8 2018-04-30 stsp if (error) {
3507 f29d3e89 2018-06-23 stsp if (hflag) {
3508 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3509 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3513 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3514 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3515 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3516 ad7de8d9 2018-04-30 stsp free(repo_path);
3519 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3520 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3522 c2301be8 2018-04-30 stsp free(repo_path);
3526 5c5136c5 2018-05-20 stsp init_curses();
3528 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3529 9f7d7167 2018-04-29 stsp if (error)
3530 9f7d7167 2018-04-29 stsp goto done;
3533 c2301be8 2018-04-30 stsp free(cmd_argv);
3534 9f7d7167 2018-04-29 stsp if (error)
3535 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);