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))
54 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
58 9f7d7167 2018-04-29 stsp #ifndef nitems
59 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 9f7d7167 2018-04-29 stsp struct tog_cmd {
63 c2301be8 2018-04-30 stsp const char *name;
64 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
65 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
66 c2301be8 2018-04-30 stsp const char *descr;
69 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
70 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
71 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
72 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
73 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
75 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
76 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
77 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
78 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
80 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
81 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
82 9f7d7167 2018-04-29 stsp "show repository history" },
83 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
84 9f7d7167 2018-04-29 stsp "compare files and directories" },
85 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
86 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
87 ffd1d5e5 2018-06-23 stsp { "tree", cmd_tree, usage_tree,
88 ffd1d5e5 2018-06-23 stsp "browse trees in repository" },
91 d6b05b5b 2018-08-04 stsp enum tog_view_type {
92 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
93 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
94 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
95 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
98 ad80ab7b 2018-08-04 stsp struct tog_diff_view_state {
99 a3404814 2018-09-02 stsp struct got_object_id *id1, *id2;
101 ad80ab7b 2018-08-04 stsp int first_displayed_line;
102 ad80ab7b 2018-08-04 stsp int last_displayed_line;
104 48ae06ee 2018-10-18 stsp int diff_context;
105 48ae06ee 2018-10-18 stsp struct got_repository *repo;
108 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
109 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
110 ba4f502b 2018-08-04 stsp struct got_object_id *id;
111 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
114 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
115 ba4f502b 2018-08-04 stsp struct commit_queue {
116 ba4f502b 2018-08-04 stsp int ncommits;
117 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
120 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
122 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
123 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
124 1a76625f 2018-10-22 stsp int commits_needed;
125 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
126 1a76625f 2018-10-22 stsp struct commit_queue *commits;
127 1a76625f 2018-10-22 stsp const char *in_repo_path;
128 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
129 1a76625f 2018-10-22 stsp struct got_repository *repo;
130 1a76625f 2018-10-22 stsp int log_complete;
131 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
132 1a76625f 2018-10-22 stsp struct tog_view *view;
133 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
134 1a76625f 2018-10-22 stsp struct commit_queue_entry **last_displayed_entry;
135 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
136 1a76625f 2018-10-22 stsp int *selected;
139 1a76625f 2018-10-22 stsp struct tog_log_view_state {
140 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
141 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
142 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
143 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
144 b01e7d3b 2018-08-04 stsp int selected;
145 b01e7d3b 2018-08-04 stsp char *in_repo_path;
146 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
147 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
148 1a76625f 2018-10-22 stsp sig_atomic_t quit;
149 1a76625f 2018-10-22 stsp pthread_t thread;
150 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
153 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
154 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
155 e9424729 2018-08-04 stsp int nlines;
157 e9424729 2018-08-04 stsp struct tog_view *view;
158 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
160 e9424729 2018-08-04 stsp const char *path;
161 e9424729 2018-08-04 stsp int *first_displayed_line;
162 e9424729 2018-08-04 stsp int *last_displayed_line;
163 e9424729 2018-08-04 stsp int *selected_line;
168 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
169 e9424729 2018-08-04 stsp const char *path;
170 e9424729 2018-08-04 stsp struct got_repository *repo;
171 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
172 e9424729 2018-08-04 stsp int *complete;
175 e9424729 2018-08-04 stsp struct tog_blame {
177 e9424729 2018-08-04 stsp size_t filesize;
178 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
179 e9424729 2018-08-04 stsp size_t nlines;
180 e9424729 2018-08-04 stsp pthread_t thread;
181 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
182 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
183 e9424729 2018-08-04 stsp const char *path;
186 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
187 7cbe629d 2018-08-04 stsp int first_displayed_line;
188 7cbe629d 2018-08-04 stsp int last_displayed_line;
189 7cbe629d 2018-08-04 stsp int selected_line;
190 7cbe629d 2018-08-04 stsp int blame_complete;
193 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
194 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
195 e5a0f69f 2018-08-18 stsp char *path;
196 7cbe629d 2018-08-04 stsp struct got_repository *repo;
197 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
198 e9424729 2018-08-04 stsp struct tog_blame blame;
201 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
202 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
203 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
204 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
205 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
206 ad80ab7b 2018-08-04 stsp int selected;
209 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
211 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
212 ad80ab7b 2018-08-04 stsp char *tree_label;
213 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
214 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
215 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
216 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
217 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
218 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
219 ad80ab7b 2018-08-04 stsp int nentries, ndisplayed, selected, show_ids;
220 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
221 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
222 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
226 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
228 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
229 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
230 669b5ffa 2018-10-07 stsp * there is enough screen estate.
232 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
233 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
235 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
236 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
237 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
239 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
240 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
242 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
244 cc3c9aac 2018-08-01 stsp struct tog_view {
245 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
246 26ed57b2 2018-05-19 stsp WINDOW *window;
247 26ed57b2 2018-05-19 stsp PANEL *panel;
248 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
249 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
250 1004088d 2018-09-29 stsp int focussed;
251 669b5ffa 2018-10-07 stsp struct tog_view *parent;
252 669b5ffa 2018-10-07 stsp struct tog_view *child;
253 669b5ffa 2018-10-07 stsp int child_focussed;
255 5dc9f4bc 2018-08-04 stsp /* type-specific state */
256 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
258 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
259 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
260 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
261 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
264 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
265 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
266 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
267 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
270 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
271 ba4f502b 2018-08-04 stsp struct got_object *, struct got_object *, struct got_repository *);
272 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
273 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
274 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
275 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
277 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
278 23721109 2018-10-22 stsp struct got_object_id *, struct got_repository *, const char *, int);
279 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
280 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
281 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
282 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
284 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
285 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
286 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
287 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
288 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
289 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
291 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
292 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
293 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
294 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
295 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
296 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
298 e5a0f69f 2018-08-18 stsp static const struct got_error *
299 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
301 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
303 669b5ffa 2018-10-07 stsp if (view->child) {
304 669b5ffa 2018-10-07 stsp view_close(view->child);
305 669b5ffa 2018-10-07 stsp view->child = NULL;
307 e5a0f69f 2018-08-18 stsp if (view->close)
308 e5a0f69f 2018-08-18 stsp err = view->close(view);
309 ea5e7bb5 2018-08-01 stsp if (view->panel)
310 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
311 ea5e7bb5 2018-08-01 stsp if (view->window)
312 ea5e7bb5 2018-08-01 stsp delwin(view->window);
313 ea5e7bb5 2018-08-01 stsp free(view);
314 e5a0f69f 2018-08-18 stsp return err;
317 ea5e7bb5 2018-08-01 stsp static struct tog_view *
318 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
319 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
321 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
323 ea5e7bb5 2018-08-01 stsp if (view == NULL)
324 ea5e7bb5 2018-08-01 stsp return NULL;
326 d6b05b5b 2018-08-04 stsp view->type = type;
327 f7d12f7e 2018-08-01 stsp view->lines = LINES;
328 f7d12f7e 2018-08-01 stsp view->cols = COLS;
329 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
330 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
331 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
332 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
333 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
334 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
335 96a765a8 2018-08-04 stsp view_close(view);
336 ea5e7bb5 2018-08-01 stsp return NULL;
338 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
339 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
340 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
341 96a765a8 2018-08-04 stsp view_close(view);
342 ea5e7bb5 2018-08-01 stsp return NULL;
345 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
346 ea5e7bb5 2018-08-01 stsp return view;
350 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
352 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
354 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
357 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
359 5c60c32a 2018-10-18 stsp static const struct got_error *
360 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
362 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
364 5c60c32a 2018-10-18 stsp view->begin_y = 0;
365 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
366 5c60c32a 2018-10-18 stsp view->nlines = LINES;
367 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
368 5c60c32a 2018-10-18 stsp view->lines = LINES;
369 5c60c32a 2018-10-18 stsp view->cols = COLS;
370 5c60c32a 2018-10-18 stsp err = view_resize(view);
372 5c60c32a 2018-10-18 stsp return err;
374 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
375 5c60c32a 2018-10-18 stsp return got_error_from_errno();
377 5c60c32a 2018-10-18 stsp return NULL;
380 5c60c32a 2018-10-18 stsp static const struct got_error *
381 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
383 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
385 5c60c32a 2018-10-18 stsp view->begin_x = 0;
386 5c60c32a 2018-10-18 stsp view->begin_y = 0;
387 5c60c32a 2018-10-18 stsp view->nlines = LINES;
388 5c60c32a 2018-10-18 stsp view->ncols = COLS;
389 5c60c32a 2018-10-18 stsp view->lines = LINES;
390 5c60c32a 2018-10-18 stsp view->cols = COLS;
391 5c60c32a 2018-10-18 stsp err = view_resize(view);
393 5c60c32a 2018-10-18 stsp return err;
395 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
396 5c60c32a 2018-10-18 stsp return got_error_from_errno();
398 5c60c32a 2018-10-18 stsp return NULL;
402 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
404 5c60c32a 2018-10-18 stsp return view->parent == NULL;
407 4d8c2215 2018-08-19 stsp static const struct got_error *
408 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
410 f7d12f7e 2018-08-01 stsp int nlines, ncols;
412 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
413 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
415 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
417 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
418 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
420 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
422 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
423 0cf4efb1 2018-09-29 stsp return got_error_from_errno();
424 0cf4efb1 2018-09-29 stsp replace_panel(view->panel, view->window);
426 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
427 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
428 0cf4efb1 2018-09-29 stsp view->lines = LINES;
429 0cf4efb1 2018-09-29 stsp view->cols = COLS;
431 6e3e5d9c 2018-10-18 stsp if (view->child) {
432 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
433 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
434 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
435 5c60c32a 2018-10-18 stsp if (view->child->focussed)
436 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
438 5c60c32a 2018-10-18 stsp show_panel(view->panel);
440 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
441 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
445 5c60c32a 2018-10-18 stsp return NULL;
448 669b5ffa 2018-10-07 stsp static const struct got_error *
449 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
451 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
453 669b5ffa 2018-10-07 stsp if (view->child == NULL)
454 669b5ffa 2018-10-07 stsp return NULL;
456 669b5ffa 2018-10-07 stsp err = view_close(view->child);
457 669b5ffa 2018-10-07 stsp view->child = NULL;
458 669b5ffa 2018-10-07 stsp return err;
461 669b5ffa 2018-10-07 stsp static const struct got_error *
462 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
464 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
466 669b5ffa 2018-10-07 stsp view->child = child;
467 669b5ffa 2018-10-07 stsp child->parent = view;
468 669b5ffa 2018-10-07 stsp return err;
472 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
474 669b5ffa 2018-10-07 stsp return !view_is_parent_view(view) && view->begin_x > 0;
477 0cf4efb1 2018-09-29 stsp static const struct got_error *
478 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
479 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
480 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
482 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
483 669b5ffa 2018-10-07 stsp struct tog_view *v;
484 1a76625f 2018-10-22 stsp int ch, errcode;
486 e5a0f69f 2018-08-18 stsp *new = NULL;
487 e5a0f69f 2018-08-18 stsp *dead = NULL;
488 0cf4efb1 2018-09-29 stsp *focus = NULL;
490 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
491 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
492 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
493 1a76625f 2018-10-22 stsp if (errcode)
494 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
495 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
496 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
497 1a76625f 2018-10-22 stsp if (errcode)
498 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
499 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
500 e5a0f69f 2018-08-18 stsp switch (ch) {
504 669b5ffa 2018-10-07 stsp if (view->child) {
505 669b5ffa 2018-10-07 stsp *focus = view->child;
506 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
507 669b5ffa 2018-10-07 stsp } else if (view->parent) {
508 669b5ffa 2018-10-07 stsp *focus = view->parent;
509 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 0;
513 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
514 e5a0f69f 2018-08-18 stsp *dead = view;
520 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
521 669b5ffa 2018-10-07 stsp if (view->child == NULL)
523 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view->child)) {
524 669b5ffa 2018-10-07 stsp *focus = view->child;
525 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
526 669b5ffa 2018-10-07 stsp err = view_fullscreen(view->child);
528 669b5ffa 2018-10-07 stsp err = view_splitscreen(view->child);
531 669b5ffa 2018-10-07 stsp err = view->child->input(new, dead, focus,
532 669b5ffa 2018-10-07 stsp view->child, KEY_RESIZE);
534 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view)) {
535 669b5ffa 2018-10-07 stsp *focus = view;
536 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 1;
537 669b5ffa 2018-10-07 stsp err = view_fullscreen(view);
539 669b5ffa 2018-10-07 stsp err = view_splitscreen(view);
543 669b5ffa 2018-10-07 stsp err = view->input(new, dead, focus, view,
544 669b5ffa 2018-10-07 stsp KEY_RESIZE);
547 0cf4efb1 2018-09-29 stsp case KEY_RESIZE:
548 0cf4efb1 2018-09-29 stsp TAILQ_FOREACH(v, views, entry) {
549 0cf4efb1 2018-09-29 stsp err = view_resize(v);
551 0cf4efb1 2018-09-29 stsp return err;
552 878940b7 2018-09-29 stsp err = v->input(new, dead, focus, v, ch);
554 876c7f55 2018-10-22 stsp return err;
558 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
562 e5a0f69f 2018-08-18 stsp return err;
566 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
568 0cf4efb1 2018-09-29 stsp PANEL *panel;
569 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
571 669b5ffa 2018-10-07 stsp if (view->parent)
572 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
574 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
575 0cf4efb1 2018-09-29 stsp if (panel == NULL)
578 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
579 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
580 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
584 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
586 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
587 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
589 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
591 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
594 669b5ffa 2018-10-07 stsp return view->focussed;
597 bcbd79e2 2018-08-19 stsp static const struct got_error *
598 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
600 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
601 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
602 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
603 fd823528 2018-10-22 stsp int fast_refresh = 10;
604 1a76625f 2018-10-22 stsp int done = 0, errcode;
606 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
607 1a76625f 2018-10-22 stsp if (errcode)
608 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
610 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
611 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
613 a81bf10d 2018-09-29 stsp main_view = view;
614 1004088d 2018-09-29 stsp view->focussed = 1;
615 878940b7 2018-09-29 stsp err = view->show(view);
617 0cf4efb1 2018-09-29 stsp return err;
618 0cf4efb1 2018-09-29 stsp update_panels();
619 0cf4efb1 2018-09-29 stsp doupdate();
620 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
621 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
622 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
623 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
625 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
626 e4197bf9 2018-08-18 stsp view, &views);
629 e5a0f69f 2018-08-18 stsp if (dead_view) {
630 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
632 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
633 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
634 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
636 669b5ffa 2018-10-07 stsp prev = view->parent;
638 669b5ffa 2018-10-07 stsp if (dead_view->parent)
639 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
641 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
643 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
644 a81bf10d 2018-09-29 stsp if (err || dead_view == main_view)
647 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
648 0cf4efb1 2018-09-29 stsp if (focus_view)
649 0cf4efb1 2018-09-29 stsp view = focus_view;
650 669b5ffa 2018-10-07 stsp else if (prev)
651 669b5ffa 2018-10-07 stsp view = prev;
652 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
653 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
654 0cf4efb1 2018-09-29 stsp tog_view_list_head);
656 0cf4efb1 2018-09-29 stsp view = NULL;
657 669b5ffa 2018-10-07 stsp if (view) {
658 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
659 669b5ffa 2018-10-07 stsp focus_view = view->child;
661 669b5ffa 2018-10-07 stsp focus_view = view;
665 bcbd79e2 2018-08-19 stsp if (new_view) {
666 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
667 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
668 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
669 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
671 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
672 86c66b02 2018-10-18 stsp err = view_close(v);
675 86c66b02 2018-10-18 stsp if (v == view)
676 86c66b02 2018-10-18 stsp view = new_view;
679 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
680 878940b7 2018-09-29 stsp if (focus_view == NULL)
681 878940b7 2018-09-29 stsp focus_view = new_view;
683 0cf4efb1 2018-09-29 stsp if (focus_view) {
684 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
686 0cf4efb1 2018-09-29 stsp view->focussed = 0;
687 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
688 0cf4efb1 2018-09-29 stsp view = focus_view;
689 878940b7 2018-09-29 stsp if (new_view)
690 878940b7 2018-09-29 stsp show_panel(new_view->panel);
691 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
692 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
694 669b5ffa 2018-10-07 stsp if (view) {
695 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
696 758194b5 2018-10-24 stsp view->focussed = 1;
697 758194b5 2018-10-24 stsp show_panel(view->panel);
698 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
699 758194b5 2018-10-24 stsp show_panel(view->child->panel);
700 1a76625f 2018-10-22 stsp focus_view = view;
702 669b5ffa 2018-10-07 stsp if (view->parent) {
703 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
707 669b5ffa 2018-10-07 stsp err = view->show(view);
710 669b5ffa 2018-10-07 stsp if (view->child) {
711 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
715 1a76625f 2018-10-22 stsp update_panels();
716 1a76625f 2018-10-22 stsp doupdate();
720 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
721 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
722 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
723 e5a0f69f 2018-08-18 stsp view_close(view);
726 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
727 1a76625f 2018-10-22 stsp if (errcode)
728 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
730 e5a0f69f 2018-08-18 stsp return err;
733 4ed7e80c 2018-05-20 stsp __dead static void
734 9f7d7167 2018-04-29 stsp usage_log(void)
737 c70c5802 2018-08-01 stsp fprintf(stderr,
738 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
739 9f7d7167 2018-04-29 stsp getprogname());
743 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
744 80ddbec8 2018-04-29 stsp static const struct got_error *
745 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
747 00dfcb92 2018-06-11 stsp char *vis = NULL;
748 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
750 963b370f 2018-05-20 stsp *ws = NULL;
751 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
752 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
753 00dfcb92 2018-06-11 stsp int vislen;
754 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
755 00dfcb92 2018-06-11 stsp return got_error_from_errno();
757 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
758 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
760 00dfcb92 2018-06-11 stsp return err;
761 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
762 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
763 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
768 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
769 a7f50699 2018-06-11 stsp if (*ws == NULL) {
770 a7f50699 2018-06-11 stsp err = got_error_from_errno();
774 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
775 963b370f 2018-05-20 stsp err = got_error_from_errno();
780 963b370f 2018-05-20 stsp *ws = NULL;
783 963b370f 2018-05-20 stsp return err;
786 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
787 963b370f 2018-05-20 stsp static const struct got_error *
788 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
790 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
791 963b370f 2018-05-20 stsp int cols = 0;
792 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
793 963b370f 2018-05-20 stsp size_t wlen;
796 963b370f 2018-05-20 stsp *wlinep = NULL;
797 b700b5d6 2018-07-10 stsp *widthp = 0;
799 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
801 963b370f 2018-05-20 stsp return err;
804 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
805 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
806 963b370f 2018-05-20 stsp switch (width) {
812 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
813 b700b5d6 2018-07-10 stsp cols += width;
817 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
818 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
822 963b370f 2018-05-20 stsp err = got_error_from_errno();
826 963b370f 2018-05-20 stsp wline[i] = L'\0';
827 b700b5d6 2018-07-10 stsp if (widthp)
828 b700b5d6 2018-07-10 stsp *widthp = cols;
831 963b370f 2018-05-20 stsp free(wline);
833 963b370f 2018-05-20 stsp *wlinep = wline;
834 963b370f 2018-05-20 stsp return err;
837 963b370f 2018-05-20 stsp static const struct got_error *
838 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
839 2814baeb 2018-08-01 stsp struct got_object_id *id)
841 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
842 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
843 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
844 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
845 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
846 bb737323 2018-05-20 stsp int author_width, logmsg_width;
847 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
848 80ddbec8 2018-04-29 stsp char *line = NULL;
849 bb737323 2018-05-20 stsp int col, limit;
850 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
851 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
852 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
854 c70c5802 2018-08-01 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
855 c70c5802 2018-08-01 stsp &commit->tm_committer) >= sizeof(datebuf))
856 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
858 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
859 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
861 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
862 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
863 b39d25c7 2018-07-10 stsp col = limit + 1;
864 b39d25c7 2018-07-10 stsp if (col > avail)
867 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
868 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
869 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
872 6d9fbc00 2018-04-29 stsp author = author0;
873 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
874 6d9fbc00 2018-04-29 stsp if (smallerthan)
875 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
877 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
879 6d9fbc00 2018-04-29 stsp *at = '\0';
881 b39d25c7 2018-07-10 stsp limit = avail - col;
882 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
885 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
886 bb737323 2018-05-20 stsp col += author_width;
887 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
888 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
890 bb737323 2018-05-20 stsp author_width++;
892 9c2eaf34 2018-05-20 stsp if (col > avail)
895 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
896 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
897 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
900 bb737323 2018-05-20 stsp logmsg = logmsg0;
901 bb737323 2018-05-20 stsp while (*logmsg == '\n')
903 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
904 bb737323 2018-05-20 stsp if (newline)
905 bb737323 2018-05-20 stsp *newline = '\0';
906 bb737323 2018-05-20 stsp limit = avail - col;
907 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
910 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
911 bb737323 2018-05-20 stsp col += logmsg_width;
912 bb737323 2018-05-20 stsp while (col <= avail) {
913 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
917 80ddbec8 2018-04-29 stsp free(logmsg0);
918 bb737323 2018-05-20 stsp free(wlogmsg);
919 6d9fbc00 2018-04-29 stsp free(author0);
920 bb737323 2018-05-20 stsp free(wauthor);
921 80ddbec8 2018-04-29 stsp free(line);
922 80ddbec8 2018-04-29 stsp return err;
925 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
926 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
927 899d86c2 2018-05-10 stsp struct got_object_id *id)
929 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
931 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
932 80ddbec8 2018-04-29 stsp if (entry == NULL)
933 899d86c2 2018-05-10 stsp return NULL;
935 899d86c2 2018-05-10 stsp entry->id = id;
936 99db9666 2018-05-07 stsp entry->commit = commit;
937 899d86c2 2018-05-10 stsp return entry;
940 99db9666 2018-05-07 stsp static void
941 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
943 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
945 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
946 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
947 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
948 ecb28ae0 2018-07-16 stsp commits->ncommits--;
949 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
950 99db9666 2018-05-07 stsp free(entry);
953 99db9666 2018-05-07 stsp static void
954 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
956 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
957 99db9666 2018-05-07 stsp pop_commit(commits);
960 c4972b91 2018-05-07 stsp static const struct got_error *
961 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
962 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
964 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
965 93e45b7c 2018-09-24 stsp int nqueued = 0;
968 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
969 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
970 1a76625f 2018-10-22 stsp * while updating the display.
972 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
973 93e45b7c 2018-09-24 stsp struct got_object_id *id;
974 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
975 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
976 1a76625f 2018-10-22 stsp int errcode;
978 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
980 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
982 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
983 93e45b7c 2018-09-24 stsp minqueue, repo);
985 ecb28ae0 2018-07-16 stsp return err;
989 ecb28ae0 2018-07-16 stsp if (id == NULL)
992 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
995 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
996 9ba79e04 2018-06-11 stsp if (entry == NULL) {
997 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
1001 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1002 1a76625f 2018-10-22 stsp if (errcode) {
1003 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1007 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1008 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1009 ecb28ae0 2018-07-16 stsp nqueued++;
1010 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1012 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1013 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1014 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1017 9ba79e04 2018-06-11 stsp return err;
1020 99db9666 2018-05-07 stsp static const struct got_error *
1021 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1023 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1024 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1026 9ba79e04 2018-06-11 stsp *head_id = NULL;
1028 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1030 99db9666 2018-05-07 stsp return err;
1032 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1033 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1034 9ba79e04 2018-06-11 stsp if (err) {
1035 9ba79e04 2018-06-11 stsp *head_id = NULL;
1036 99db9666 2018-05-07 stsp return err;
1039 9ba79e04 2018-06-11 stsp return NULL;
1042 0553a4e3 2018-04-30 stsp static const struct got_error *
1043 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1044 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1045 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1046 1a76625f 2018-10-22 stsp const char *path, int commits_needed)
1048 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1049 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1050 ecb28ae0 2018-07-16 stsp int ncommits, width;
1051 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1052 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1054 e0d42f60 2018-07-22 stsp entry = first;
1055 e0d42f60 2018-07-22 stsp ncommits = 0;
1056 e0d42f60 2018-07-22 stsp while (entry) {
1057 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1058 e0d42f60 2018-07-22 stsp *selected = entry;
1061 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1062 e0d42f60 2018-07-22 stsp ncommits++;
1065 1a76625f 2018-10-22 stsp if (*selected) {
1066 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1068 ecb28ae0 2018-07-16 stsp return err;
1071 1a76625f 2018-10-22 stsp if (asprintf(&ncommits_str, " [%d/%d]%s ",
1072 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1073 1a76625f 2018-10-22 stsp commits_needed == 0 ? "" : " loading...") == -1)
1074 1a76625f 2018-10-22 stsp return got_error_from_errno();
1076 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1077 1a76625f 2018-10-22 stsp if (asprintf(&header, "commit: %s %s%s",
1078 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1079 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1080 1a76625f 2018-10-22 stsp err = got_error_from_errno();
1081 1a76625f 2018-10-22 stsp header = NULL;
1082 1a76625f 2018-10-22 stsp goto done;
1084 1a76625f 2018-10-22 stsp } else if (asprintf(&header, "commit: %s%s",
1085 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1086 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1087 1a76625f 2018-10-22 stsp err = got_error_from_errno();
1088 1a76625f 2018-10-22 stsp header = NULL;
1089 1a76625f 2018-10-22 stsp goto done;
1091 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1093 1a76625f 2018-10-22 stsp goto done;
1095 2814baeb 2018-08-01 stsp werase(view->window);
1097 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1098 a3404814 2018-09-02 stsp wstandout(view->window);
1099 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1100 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1101 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1104 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1105 a3404814 2018-09-02 stsp wstandend(view->window);
1106 ecb28ae0 2018-07-16 stsp free(wline);
1107 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1108 1a76625f 2018-10-22 stsp goto done;
1110 899d86c2 2018-05-10 stsp entry = first;
1111 899d86c2 2018-05-10 stsp *last = first;
1112 867c6645 2018-07-10 stsp ncommits = 0;
1113 899d86c2 2018-05-10 stsp while (entry) {
1114 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1116 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1117 2814baeb 2018-08-01 stsp wstandout(view->window);
1118 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
1119 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1120 2814baeb 2018-08-01 stsp wstandend(view->window);
1123 0553a4e3 2018-04-30 stsp ncommits++;
1124 899d86c2 2018-05-10 stsp *last = entry;
1125 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1128 1a57306a 2018-09-02 stsp view_vborder(view);
1130 1a76625f 2018-10-22 stsp free(id_str);
1131 1a76625f 2018-10-22 stsp free(ncommits_str);
1132 1a76625f 2018-10-22 stsp free(header);
1133 80ddbec8 2018-04-29 stsp return err;
1136 07b55e75 2018-05-10 stsp static void
1137 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1138 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1140 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1141 07b55e75 2018-05-10 stsp int nscrolled = 0;
1143 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1144 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
1147 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1148 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1149 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1150 07b55e75 2018-05-10 stsp if (entry) {
1151 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1152 07b55e75 2018-05-10 stsp nscrolled++;
1157 aa075928 2018-05-10 stsp static const struct got_error *
1158 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1159 93e45b7c 2018-09-24 stsp struct commit_queue_entry **last_displayed_entry,
1160 1a76625f 2018-10-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1161 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1163 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
1164 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
1165 aa075928 2018-05-10 stsp int nscrolled = 0;
1167 1a76625f 2018-10-22 stsp if (*last_displayed_entry == NULL)
1168 1a76625f 2018-10-22 stsp return NULL;
1171 93e45b7c 2018-09-24 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1172 aa075928 2018-05-10 stsp if (pentry == NULL) {
1173 1a76625f 2018-10-22 stsp int errcode;
1174 1a76625f 2018-10-22 stsp if (*log_complete)
1175 1a76625f 2018-10-22 stsp return NULL;
1176 1a76625f 2018-10-22 stsp *commits_needed = maxscroll + 20;
1177 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(need_commits);
1178 1a76625f 2018-10-22 stsp if (errcode)
1179 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1180 1a76625f 2018-10-22 stsp return NULL;
1182 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1184 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1185 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1187 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1188 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1190 dd0a52c1 2018-05-20 stsp return err;
1193 cd0acaa7 2018-05-20 stsp static const struct got_error *
1194 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1195 0cf4efb1 2018-09-29 stsp struct got_object_id *commit_id, struct got_commit_object *commit,
1196 2a4718d3 2018-09-29 stsp struct got_repository *repo)
1198 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1199 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1200 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1201 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1203 2a4718d3 2018-09-29 stsp err = got_object_open(&obj2, repo, commit_id);
1205 cd0acaa7 2018-05-20 stsp return err;
1207 2a4718d3 2018-09-29 stsp parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1208 9ba79e04 2018-06-11 stsp if (parent_id) {
1209 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
1211 cd0acaa7 2018-05-20 stsp goto done;
1214 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1215 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
1216 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1217 ea5e7bb5 2018-08-01 stsp goto done;
1220 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, obj1, obj2, repo);
1221 e5a0f69f 2018-08-18 stsp if (err == NULL)
1222 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1225 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
1227 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
1228 cd0acaa7 2018-05-20 stsp return err;
1231 80ddbec8 2018-04-29 stsp static const struct got_error *
1232 0cf4efb1 2018-09-29 stsp browse_commit(struct tog_view **new_view, int begin_x,
1233 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1235 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1236 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1237 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1239 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1241 9343a5fb 2018-06-23 stsp return err;
1243 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1244 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1245 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1247 e5a0f69f 2018-08-18 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1249 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1251 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1252 1a76625f 2018-10-22 stsp return err;
1255 1a76625f 2018-10-22 stsp static void *
1256 1a76625f 2018-10-22 stsp log_thread(void *arg)
1258 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1259 1a76625f 2018-10-22 stsp int errcode = 0;
1260 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1261 1a76625f 2018-10-22 stsp int done = 0;
1263 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1265 1a76625f 2018-10-22 stsp return (void *)err;
1267 1a76625f 2018-10-22 stsp while (!done && !err) {
1268 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1269 1a76625f 2018-10-22 stsp a->in_repo_path);
1270 1a76625f 2018-10-22 stsp if (err) {
1271 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1272 1a76625f 2018-10-22 stsp return (void *)err;
1273 1a76625f 2018-10-22 stsp err = NULL;
1275 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1276 1a76625f 2018-10-22 stsp a->commits_needed--;
1278 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1279 1a76625f 2018-10-22 stsp if (errcode)
1280 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
1283 1a76625f 2018-10-22 stsp a->log_complete = 1;
1284 1a76625f 2018-10-22 stsp else if (*a->quit) {
1286 1a76625f 2018-10-22 stsp a->log_complete = 1;
1287 1a76625f 2018-10-22 stsp } else if (*a->first_displayed_entry == NULL) {
1288 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1289 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1290 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1293 1a76625f 2018-10-22 stsp err = draw_commits(a->view, a->last_displayed_entry,
1294 1a76625f 2018-10-22 stsp a->selected_entry, *a->first_displayed_entry,
1295 1a76625f 2018-10-22 stsp a->commits, *a->selected, a->view->nlines,
1296 1a76625f 2018-10-22 stsp a->in_repo_path, a->commits_needed);
1299 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1300 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1301 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1302 1a76625f 2018-10-22 stsp &tog_mutex);
1303 1a76625f 2018-10-22 stsp if (errcode)
1304 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1307 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1308 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1309 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1311 1a76625f 2018-10-22 stsp return (void *)err;
1314 1a76625f 2018-10-22 stsp static const struct got_error *
1315 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1317 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1318 1a76625f 2018-10-22 stsp int errcode;
1320 1a76625f 2018-10-22 stsp if (s->thread) {
1321 1a76625f 2018-10-22 stsp s->quit = 1;
1322 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1323 1a76625f 2018-10-22 stsp if (errcode)
1324 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1325 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1326 1a76625f 2018-10-22 stsp if (errcode)
1327 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1328 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1329 1a76625f 2018-10-22 stsp if (errcode)
1330 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1331 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1332 1a76625f 2018-10-22 stsp if (errcode)
1333 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1334 1a76625f 2018-10-22 stsp s->thread = NULL;
1337 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1338 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1339 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1341 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1342 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1343 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1346 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1347 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1348 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1351 9343a5fb 2018-06-23 stsp return err;
1354 9343a5fb 2018-06-23 stsp static const struct got_error *
1355 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1357 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1358 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1360 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1361 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1362 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1363 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1364 1a76625f 2018-10-22 stsp free(s->start_id);
1365 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1366 1a76625f 2018-10-22 stsp return err;
1369 1a76625f 2018-10-22 stsp static const struct got_error *
1370 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1371 23721109 2018-10-22 stsp struct got_repository *repo, const char *path, int check_disk)
1373 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1374 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1375 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1376 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1377 1a76625f 2018-10-22 stsp int errcode;
1379 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1380 ecb28ae0 2018-07-16 stsp if (err != NULL)
1381 ecb28ae0 2018-07-16 stsp goto done;
1383 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1384 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1385 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1387 fb2756b9 2018-08-04 stsp s->repo = repo;
1388 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1389 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1390 5036bf37 2018-09-24 stsp err = got_error_from_errno();
1391 5036bf37 2018-09-24 stsp goto done;
1394 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1395 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1396 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1398 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1400 1a76625f 2018-10-22 stsp goto done;
1401 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1402 1a76625f 2018-10-22 stsp 0, thread_repo);
1404 1a76625f 2018-10-22 stsp goto done;
1406 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1407 1a76625f 2018-10-22 stsp if (errcode) {
1408 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1409 1a76625f 2018-10-22 stsp goto done;
1412 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1413 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1414 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1415 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1416 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1417 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1418 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1419 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1420 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1421 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1422 1a76625f 2018-10-22 stsp s->thread_args.last_displayed_entry = &s->last_displayed_entry;
1423 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1424 1a76625f 2018-10-22 stsp s->thread_args.selected = &s->selected;
1426 1a76625f 2018-10-22 stsp errcode = pthread_create(&s->thread, NULL, log_thread,
1427 1a76625f 2018-10-22 stsp &s->thread_args);
1428 1a76625f 2018-10-22 stsp if (errcode) {
1429 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1430 1a76625f 2018-10-22 stsp goto done;
1435 1a76625f 2018-10-22 stsp close_log_view(view);
1436 ba4f502b 2018-08-04 stsp return err;
1439 e5a0f69f 2018-08-18 stsp static const struct got_error *
1440 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1442 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1444 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1445 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1446 1a76625f 2018-10-22 stsp &s->commits, s->selected, view->nlines,
1447 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1450 e5a0f69f 2018-08-18 stsp static const struct got_error *
1451 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1452 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1454 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1455 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1456 5036bf37 2018-09-24 stsp char *parent_path;
1457 669b5ffa 2018-10-07 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
1458 669b5ffa 2018-10-07 stsp int begin_x = 0;
1460 e5a0f69f 2018-08-18 stsp switch (ch) {
1462 1a76625f 2018-10-22 stsp s->quit = 1;
1465 e5a0f69f 2018-08-18 stsp case KEY_UP:
1466 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1467 e5a0f69f 2018-08-18 stsp s->selected--;
1468 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1470 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1471 e5a0f69f 2018-08-18 stsp &s->commits);
1473 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1474 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1475 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1476 e5a0f69f 2018-08-18 stsp s->selected = 0;
1479 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1480 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1483 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1484 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1485 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1486 e5a0f69f 2018-08-18 stsp s->selected++;
1489 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1490 93e45b7c 2018-09-24 stsp &s->last_displayed_entry, &s->commits,
1491 1a76625f 2018-10-22 stsp &s->thread_args.log_complete,
1492 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1493 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1495 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1496 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1497 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1498 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1499 93e45b7c 2018-09-24 stsp view->nlines, &s->last_displayed_entry,
1500 1a76625f 2018-10-22 stsp &s->commits, &s->thread_args.log_complete,
1501 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1502 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1503 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1504 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1505 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1506 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1507 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1508 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1510 e5a0f69f 2018-08-18 stsp err = NULL;
1513 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1514 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1515 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1516 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1517 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1519 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1520 e5a0f69f 2018-08-18 stsp case '\r':
1521 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1522 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1523 669b5ffa 2018-10-07 stsp err = open_diff_view_for_commit(&diff_view, begin_x,
1524 2a4718d3 2018-09-29 stsp s->selected_entry->id, s->selected_entry->commit,
1528 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1529 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1531 669b5ffa 2018-10-07 stsp return err;
1532 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
1533 669b5ffa 2018-10-07 stsp if (err) {
1534 669b5ffa 2018-10-07 stsp view_close(diff_view);
1537 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(diff_view)) {
1538 669b5ffa 2018-10-07 stsp *focus_view = diff_view;
1539 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1542 669b5ffa 2018-10-07 stsp *new_view = diff_view;
1545 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1546 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1547 669b5ffa 2018-10-07 stsp err = browse_commit(&tree_view, begin_x,
1548 0cf4efb1 2018-09-29 stsp s->selected_entry, s->repo);
1549 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1550 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1552 669b5ffa 2018-10-07 stsp return err;
1553 669b5ffa 2018-10-07 stsp err = view_set_child(view, tree_view);
1554 669b5ffa 2018-10-07 stsp if (err) {
1555 669b5ffa 2018-10-07 stsp view_close(tree_view);
1558 669b5ffa 2018-10-07 stsp *focus_view = tree_view;
1559 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1561 669b5ffa 2018-10-07 stsp *new_view = tree_view;
1563 5036bf37 2018-09-24 stsp case KEY_BACKSPACE:
1564 5036bf37 2018-09-24 stsp if (strcmp(s->in_repo_path, "/") == 0)
1566 5036bf37 2018-09-24 stsp parent_path = dirname(s->in_repo_path);
1567 5036bf37 2018-09-24 stsp if (parent_path && strcmp(parent_path, ".") != 0) {
1568 5036bf37 2018-09-24 stsp struct tog_view *lv;
1569 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1571 1a76625f 2018-10-22 stsp return err;
1572 0cf4efb1 2018-09-29 stsp lv = view_open(view->nlines, view->ncols,
1573 0cf4efb1 2018-09-29 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
1574 5036bf37 2018-09-24 stsp if (lv == NULL)
1575 5036bf37 2018-09-24 stsp return got_error_from_errno();
1576 5036bf37 2018-09-24 stsp err = open_log_view(lv, s->start_id, s->repo,
1577 23721109 2018-10-22 stsp parent_path, 0);
1579 1a76625f 2018-10-22 stsp return err;;
1580 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1581 669b5ffa 2018-10-07 stsp *new_view = lv;
1583 669b5ffa 2018-10-07 stsp view_set_child(view->parent, lv);
1584 34095730 2018-10-22 stsp *focus_view = lv;
1586 1a76625f 2018-10-22 stsp return NULL;
1593 80ddbec8 2018-04-29 stsp return err;
1596 4ed7e80c 2018-05-20 stsp static const struct got_error *
1597 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1599 80ddbec8 2018-04-29 stsp const struct got_error *error;
1600 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1601 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1602 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1603 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1605 04cc582a 2018-08-01 stsp struct tog_view *view;
1607 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1608 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1610 80ddbec8 2018-04-29 stsp err(1, "pledge");
1613 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1614 80ddbec8 2018-04-29 stsp switch (ch) {
1616 80ddbec8 2018-04-29 stsp start_commit = optarg;
1619 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1620 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1621 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1625 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1629 80ddbec8 2018-04-29 stsp argc -= optind;
1630 80ddbec8 2018-04-29 stsp argv += optind;
1632 ecb28ae0 2018-07-16 stsp if (argc == 0)
1633 ecb28ae0 2018-07-16 stsp path = strdup("");
1634 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1635 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1637 80ddbec8 2018-04-29 stsp usage_log();
1638 ecb28ae0 2018-07-16 stsp if (path == NULL)
1639 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1641 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1642 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1643 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1644 ecb28ae0 2018-07-16 stsp goto done;
1646 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1647 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1648 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1649 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1650 ecb28ae0 2018-07-16 stsp goto done;
1654 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1655 80ddbec8 2018-04-29 stsp if (error != NULL)
1656 ecb28ae0 2018-07-16 stsp goto done;
1658 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1659 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1660 80ddbec8 2018-04-29 stsp if (error != NULL)
1661 ecb28ae0 2018-07-16 stsp goto done;
1663 80ddbec8 2018-04-29 stsp struct got_object *obj;
1664 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1665 80ddbec8 2018-04-29 stsp if (error == NULL) {
1666 6402fb3c 2018-09-15 stsp start_id = got_object_id_dup(got_object_get_id(obj));
1667 899d86c2 2018-05-10 stsp if (start_id == NULL)
1668 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1669 ecb28ae0 2018-07-16 stsp goto done;
1672 80ddbec8 2018-04-29 stsp if (error != NULL)
1673 ecb28ae0 2018-07-16 stsp goto done;
1675 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1676 04cc582a 2018-08-01 stsp if (view == NULL) {
1677 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1678 04cc582a 2018-08-01 stsp goto done;
1680 23721109 2018-10-22 stsp error = open_log_view(view, start_id, repo, path, 1);
1681 ba4f502b 2018-08-04 stsp if (error)
1682 ba4f502b 2018-08-04 stsp goto done;
1683 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1685 ecb28ae0 2018-07-16 stsp free(repo_path);
1686 ecb28ae0 2018-07-16 stsp free(cwd);
1687 ecb28ae0 2018-07-16 stsp free(path);
1688 899d86c2 2018-05-10 stsp free(start_id);
1690 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1691 80ddbec8 2018-04-29 stsp return error;
1694 4ed7e80c 2018-05-20 stsp __dead static void
1695 9f7d7167 2018-04-29 stsp usage_diff(void)
1698 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1699 9f7d7167 2018-04-29 stsp getprogname());
1703 b304db33 2018-05-20 stsp static char *
1704 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1706 b304db33 2018-05-20 stsp char *line;
1707 b304db33 2018-05-20 stsp size_t linelen;
1708 b304db33 2018-05-20 stsp size_t lineno;
1709 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1711 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1713 b304db33 2018-05-20 stsp *len = linelen;
1714 b304db33 2018-05-20 stsp return line;
1717 4ed7e80c 2018-05-20 stsp static const struct got_error *
1718 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1719 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1720 a3404814 2018-09-02 stsp char * header)
1722 61e69b96 2018-05-20 stsp const struct got_error *err;
1723 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1724 b304db33 2018-05-20 stsp char *line;
1725 b304db33 2018-05-20 stsp size_t len;
1726 61e69b96 2018-05-20 stsp wchar_t *wline;
1727 e0b650dd 2018-05-20 stsp int width;
1729 26ed57b2 2018-05-19 stsp rewind(f);
1730 f7d12f7e 2018-08-01 stsp werase(view->window);
1732 a3404814 2018-09-02 stsp if (header) {
1733 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1734 a3404814 2018-09-02 stsp if (err) {
1735 a3404814 2018-09-02 stsp return err;
1738 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1739 a3404814 2018-09-02 stsp wstandout(view->window);
1740 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1741 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1742 a3404814 2018-09-02 stsp wstandend(view->window);
1743 a3404814 2018-09-02 stsp if (width < view->ncols)
1744 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1746 a3404814 2018-09-02 stsp if (max_lines <= 1)
1747 a3404814 2018-09-02 stsp return NULL;
1748 a3404814 2018-09-02 stsp max_lines--;
1752 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1753 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1754 26ed57b2 2018-05-19 stsp if (line == NULL) {
1758 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1759 26ed57b2 2018-05-19 stsp free(line);
1763 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1764 61e69b96 2018-05-20 stsp if (err) {
1765 61e69b96 2018-05-20 stsp free(line);
1766 61e69b96 2018-05-20 stsp return err;
1768 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1769 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1770 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1771 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1772 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1773 26ed57b2 2018-05-19 stsp free(line);
1774 2550e4c3 2018-07-13 stsp free(wline);
1775 2550e4c3 2018-07-13 stsp wline = NULL;
1777 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1779 1a57306a 2018-09-02 stsp view_vborder(view);
1781 26ed57b2 2018-05-19 stsp return NULL;
1784 4ed7e80c 2018-05-20 stsp static const struct got_error *
1785 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
1787 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
1788 48ae06ee 2018-10-18 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1789 48ae06ee 2018-10-18 stsp FILE *f = NULL;
1791 48ae06ee 2018-10-18 stsp if (s->id1) {
1792 48ae06ee 2018-10-18 stsp err = got_object_open(&obj1, s->repo, s->id1);
1794 48ae06ee 2018-10-18 stsp return err;
1797 48ae06ee 2018-10-18 stsp err = got_object_open(&obj2, s->repo, s->id2);
1799 48ae06ee 2018-10-18 stsp goto done;
1801 511a516b 2018-05-19 stsp f = got_opentemp();
1802 48ae06ee 2018-10-18 stsp if (f == NULL) {
1803 48ae06ee 2018-10-18 stsp err = got_error_from_errno();
1804 48ae06ee 2018-10-18 stsp goto done;
1807 48ae06ee 2018-10-18 stsp fclose(s->f);
1810 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1811 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1812 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1813 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1815 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1816 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_trees(obj1, obj2, "", "",
1817 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1819 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1820 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1821 48ae06ee 2018-10-18 stsp s->repo, f);
1824 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1829 48ae06ee 2018-10-18 stsp got_object_close(obj1);
1830 48ae06ee 2018-10-18 stsp got_object_close(obj2);
1832 48ae06ee 2018-10-18 stsp fflush(f);
1833 48ae06ee 2018-10-18 stsp return err;
1836 48ae06ee 2018-10-18 stsp static const struct got_error *
1837 48ae06ee 2018-10-18 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1838 48ae06ee 2018-10-18 stsp struct got_object *obj2, struct got_repository *repo)
1840 48ae06ee 2018-10-18 stsp const struct got_error *err;
1842 48ae06ee 2018-10-18 stsp if (obj1 != NULL && obj2 != NULL &&
1843 48ae06ee 2018-10-18 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1844 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
1846 48ae06ee 2018-10-18 stsp if (obj1) {
1847 48ae06ee 2018-10-18 stsp struct got_object_id *id1;
1848 48ae06ee 2018-10-18 stsp id1 = got_object_id_dup(got_object_get_id(obj1));
1849 48ae06ee 2018-10-18 stsp if (id1 == NULL)
1850 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1851 48ae06ee 2018-10-18 stsp view->state.diff.id1 = id1;
1853 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1855 48ae06ee 2018-10-18 stsp view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1856 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
1857 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1858 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1859 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1861 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
1862 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1863 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1864 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
1865 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
1867 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
1868 48ae06ee 2018-10-18 stsp if (err) {
1869 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1870 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1871 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
1872 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
1873 48ae06ee 2018-10-18 stsp return err;
1876 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
1877 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
1878 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
1880 5dc9f4bc 2018-08-04 stsp return NULL;
1883 e5a0f69f 2018-08-18 stsp static const struct got_error *
1884 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1886 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1888 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1889 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1890 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
1891 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
1892 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1893 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1894 e5a0f69f 2018-08-18 stsp return err;
1897 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1898 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1900 a3404814 2018-09-02 stsp const struct got_error *err;
1901 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1902 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
1904 a3404814 2018-09-02 stsp if (s->id1) {
1905 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
1907 a3404814 2018-09-02 stsp return err;
1909 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
1911 a3404814 2018-09-02 stsp return err;
1913 a3404814 2018-09-02 stsp if (asprintf(&header, "diff: %s %s",
1914 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1915 a3404814 2018-09-02 stsp err = got_error_from_errno();
1916 a3404814 2018-09-02 stsp free(id_str1);
1917 a3404814 2018-09-02 stsp free(id_str2);
1918 a3404814 2018-09-02 stsp return err;
1920 a3404814 2018-09-02 stsp free(id_str1);
1921 a3404814 2018-09-02 stsp free(id_str2);
1923 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
1924 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
1928 0cf4efb1 2018-09-29 stsp static const struct got_error *
1929 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1930 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1932 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1933 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
1936 e5a0f69f 2018-08-18 stsp switch (ch) {
1938 e5a0f69f 2018-08-18 stsp case KEY_UP:
1939 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
1940 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1942 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1944 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
1945 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
1946 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1949 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1950 e5a0f69f 2018-08-18 stsp if (!s->eof)
1951 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1953 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
1956 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
1957 e5a0f69f 2018-08-18 stsp char *line;
1958 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
1959 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1960 e5a0f69f 2018-08-18 stsp if (line == NULL)
1965 48ae06ee 2018-10-18 stsp if (s->diff_context > 0) {
1966 48ae06ee 2018-10-18 stsp s->diff_context--;
1967 48ae06ee 2018-10-18 stsp err = create_diff(s);
1971 4a8520aa 2018-10-18 stsp if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
1972 48ae06ee 2018-10-18 stsp s->diff_context++;
1973 48ae06ee 2018-10-18 stsp err = create_diff(s);
1980 bcbd79e2 2018-08-19 stsp return err;
1983 4ed7e80c 2018-05-20 stsp static const struct got_error *
1984 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1986 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1987 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1988 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1989 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1990 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1992 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1994 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1995 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1997 26ed57b2 2018-05-19 stsp err(1, "pledge");
2000 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2001 26ed57b2 2018-05-19 stsp switch (ch) {
2004 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2008 26ed57b2 2018-05-19 stsp argc -= optind;
2009 26ed57b2 2018-05-19 stsp argv += optind;
2011 26ed57b2 2018-05-19 stsp if (argc == 0) {
2012 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2013 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2014 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2015 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2016 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2017 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
2018 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
2019 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2020 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2021 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2022 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2023 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
2024 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
2026 26ed57b2 2018-05-19 stsp usage_diff();
2028 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
2029 26ed57b2 2018-05-19 stsp free(repo_path);
2030 26ed57b2 2018-05-19 stsp if (error)
2031 26ed57b2 2018-05-19 stsp goto done;
2033 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
2034 26ed57b2 2018-05-19 stsp if (error)
2035 26ed57b2 2018-05-19 stsp goto done;
2037 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
2038 26ed57b2 2018-05-19 stsp if (error)
2039 26ed57b2 2018-05-19 stsp goto done;
2041 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2042 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2043 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
2044 ea5e7bb5 2018-08-01 stsp goto done;
2046 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
2047 5dc9f4bc 2018-08-04 stsp if (error)
2048 5dc9f4bc 2018-08-04 stsp goto done;
2049 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2051 26ed57b2 2018-05-19 stsp got_repo_close(repo);
2053 26ed57b2 2018-05-19 stsp got_object_close(obj1);
2055 26ed57b2 2018-05-19 stsp got_object_close(obj2);
2056 26ed57b2 2018-05-19 stsp return error;
2059 4ed7e80c 2018-05-20 stsp __dead static void
2060 9f7d7167 2018-04-29 stsp usage_blame(void)
2063 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2064 9f7d7167 2018-04-29 stsp getprogname());
2068 84451b3e 2018-07-10 stsp struct tog_blame_line {
2069 84451b3e 2018-07-10 stsp int annotated;
2070 84451b3e 2018-07-10 stsp struct got_object_id *id;
2073 4ed7e80c 2018-05-20 stsp static const struct got_error *
2074 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2075 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2076 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2077 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2079 84451b3e 2018-07-10 stsp const struct got_error *err;
2080 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2081 84451b3e 2018-07-10 stsp char *line;
2082 84451b3e 2018-07-10 stsp size_t len;
2083 84451b3e 2018-07-10 stsp wchar_t *wline;
2084 b700b5d6 2018-07-10 stsp int width, wlimit;
2085 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2086 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2087 ab089a2a 2018-07-12 stsp char *id_str;
2089 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2091 ab089a2a 2018-07-12 stsp return err;
2093 84451b3e 2018-07-10 stsp rewind(f);
2094 f7d12f7e 2018-08-01 stsp werase(view->window);
2096 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
2097 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
2098 ab089a2a 2018-07-12 stsp free(id_str);
2099 ab089a2a 2018-07-12 stsp return err;
2102 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2103 ab089a2a 2018-07-12 stsp free(line);
2104 2550e4c3 2018-07-13 stsp line = NULL;
2105 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2106 a3404814 2018-09-02 stsp wstandout(view->window);
2107 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2108 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2109 a3404814 2018-09-02 stsp wstandend(view->window);
2110 2550e4c3 2018-07-13 stsp free(wline);
2111 2550e4c3 2018-07-13 stsp wline = NULL;
2112 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2113 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2115 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2116 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2117 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
2118 ab089a2a 2018-07-12 stsp free(id_str);
2119 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
2121 ab089a2a 2018-07-12 stsp free(id_str);
2122 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2123 3f60a8ef 2018-07-10 stsp free(line);
2124 2550e4c3 2018-07-13 stsp line = NULL;
2126 3f60a8ef 2018-07-10 stsp return err;
2127 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2128 2550e4c3 2018-07-13 stsp free(wline);
2129 2550e4c3 2018-07-13 stsp wline = NULL;
2130 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2131 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2134 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2135 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2136 84451b3e 2018-07-10 stsp if (line == NULL) {
2140 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2141 84451b3e 2018-07-10 stsp free(line);
2145 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2146 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2147 84451b3e 2018-07-10 stsp if (err) {
2148 84451b3e 2018-07-10 stsp free(line);
2149 84451b3e 2018-07-10 stsp return err;
2152 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2153 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2155 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2156 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2157 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2158 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2159 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2160 84451b3e 2018-07-10 stsp char *id_str;
2161 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2162 84451b3e 2018-07-10 stsp if (err) {
2163 84451b3e 2018-07-10 stsp free(line);
2164 2550e4c3 2018-07-13 stsp free(wline);
2165 84451b3e 2018-07-10 stsp return err;
2167 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2168 84451b3e 2018-07-10 stsp free(id_str);
2169 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2171 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2172 ee41ec32 2018-07-10 stsp prev_id = NULL;
2175 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2176 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2177 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2180 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2181 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2182 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2183 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2184 84451b3e 2018-07-10 stsp free(line);
2185 2550e4c3 2018-07-13 stsp free(wline);
2186 2550e4c3 2018-07-13 stsp wline = NULL;
2188 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2190 1a57306a 2018-09-02 stsp view_vborder(view);
2192 84451b3e 2018-07-10 stsp return NULL;
2195 84451b3e 2018-07-10 stsp static const struct got_error *
2196 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2198 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2199 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2200 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2201 1a76625f 2018-10-22 stsp int errcode;
2203 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2204 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2205 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2207 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2208 1a76625f 2018-10-22 stsp if (errcode)
2209 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2211 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2212 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2213 d68a0a7d 2018-07-10 stsp goto done;
2216 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2217 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2219 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2220 d68a0a7d 2018-07-10 stsp if (line->annotated)
2221 d68a0a7d 2018-07-10 stsp goto done;
2223 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2224 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2225 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2226 84451b3e 2018-07-10 stsp goto done;
2228 84451b3e 2018-07-10 stsp line->annotated = 1;
2230 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
2231 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
2232 e5a0f69f 2018-08-18 stsp a->last_displayed_line, a->eof, a->view->nlines);
2234 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2235 1a76625f 2018-10-22 stsp if (errcode)
2236 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2237 84451b3e 2018-07-10 stsp return err;
2240 84451b3e 2018-07-10 stsp static void *
2241 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2243 18430de3 2018-07-10 stsp const struct got_error *err;
2244 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
2245 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
2246 1a76625f 2018-10-22 stsp int errcode;
2248 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2249 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
2251 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2252 1a76625f 2018-10-22 stsp if (errcode)
2253 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
2255 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
2256 c9beca56 2018-07-22 stsp ta->repo = NULL;
2257 c9beca56 2018-07-22 stsp *ta->complete = 1;
2258 1a76625f 2018-10-22 stsp if (!err) {
2259 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
2260 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
2261 e5a0f69f 2018-08-18 stsp a->first_displayed_line, a->last_displayed_line, a->eof,
2262 f7d12f7e 2018-08-01 stsp a->view->nlines);
2265 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2266 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2267 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2269 18430de3 2018-07-10 stsp return (void *)err;
2272 245d91c1 2018-07-12 stsp static struct got_object_id *
2273 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
2274 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
2276 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2278 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2279 245d91c1 2018-07-12 stsp if (!line->annotated)
2280 245d91c1 2018-07-12 stsp return NULL;
2282 245d91c1 2018-07-12 stsp return line->id;
2285 84451b3e 2018-07-10 stsp static const struct got_error *
2286 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
2287 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
2288 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
2290 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
2291 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
2292 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
2293 b880a918 2018-07-10 stsp struct got_object_qid *pid;
2295 b880a918 2018-07-10 stsp *pobj = NULL;
2296 b880a918 2018-07-10 stsp *obj = NULL;
2298 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
2299 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
2300 245d91c1 2018-07-12 stsp if (selected_id == NULL)
2301 b880a918 2018-07-10 stsp return NULL;
2303 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
2305 b880a918 2018-07-10 stsp goto done;
2307 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
2309 b880a918 2018-07-10 stsp goto done;
2311 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
2312 b880a918 2018-07-10 stsp if (pid) {
2313 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
2315 b880a918 2018-07-10 stsp goto done;
2318 b880a918 2018-07-10 stsp if (commit)
2319 b880a918 2018-07-10 stsp got_object_commit_close(commit);
2320 b880a918 2018-07-10 stsp return err;
2323 b880a918 2018-07-10 stsp static const struct got_error *
2324 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2326 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2329 245d91c1 2018-07-12 stsp if (blame->thread) {
2330 1a76625f 2018-10-22 stsp int errcode;
2331 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2332 1a76625f 2018-10-22 stsp if (errcode)
2333 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2334 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
2335 1a76625f 2018-10-22 stsp if (errcode)
2336 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2337 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2338 1a76625f 2018-10-22 stsp if (errcode)
2339 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2340 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2341 245d91c1 2018-07-12 stsp err = NULL;
2342 245d91c1 2018-07-12 stsp blame->thread = NULL;
2344 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2345 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2346 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2348 245d91c1 2018-07-12 stsp if (blame->f) {
2349 245d91c1 2018-07-12 stsp fclose(blame->f);
2350 245d91c1 2018-07-12 stsp blame->f = NULL;
2352 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
2353 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
2354 245d91c1 2018-07-12 stsp free(blame->lines);
2355 245d91c1 2018-07-12 stsp blame->lines = NULL;
2356 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2357 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2359 245d91c1 2018-07-12 stsp return err;
2362 245d91c1 2018-07-12 stsp static const struct got_error *
2363 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2364 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
2365 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
2366 245d91c1 2018-07-12 stsp struct got_repository *repo)
2368 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2369 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2370 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2371 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2372 27d434c2 2018-09-15 stsp struct got_object *obj = NULL;
2373 1a76625f 2018-10-22 stsp int errcode;
2375 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2377 27d434c2 2018-09-15 stsp goto done;
2379 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
2381 84451b3e 2018-07-10 stsp goto done;
2383 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2384 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2385 84451b3e 2018-07-10 stsp goto done;
2388 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
2390 a70480e0 2018-06-23 stsp goto done;
2391 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2392 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2393 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2394 84451b3e 2018-07-10 stsp goto done;
2396 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2397 245d91c1 2018-07-12 stsp blame->f, blob);
2399 84451b3e 2018-07-10 stsp goto done;
2401 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2402 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2403 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2404 84451b3e 2018-07-10 stsp goto done;
2407 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2409 bd24772e 2018-07-11 stsp goto done;
2411 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2412 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2413 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2414 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2415 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2416 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2417 245d91c1 2018-07-12 stsp goto done;
2419 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
2420 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
2421 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
2422 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
2423 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
2424 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2425 e5a0f69f 2018-08-18 stsp blame->cb_args.eof = eof;
2427 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2428 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2429 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2430 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2431 245d91c1 2018-07-12 stsp *blame_complete = 0;
2433 1a76625f 2018-10-22 stsp errcode = pthread_create(&blame->thread, NULL, blame_thread,
2434 1a76625f 2018-10-22 stsp &blame->thread_args);
2435 1a76625f 2018-10-22 stsp if (errcode) {
2436 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2437 245d91c1 2018-07-12 stsp goto done;
2442 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2443 27d434c2 2018-09-15 stsp free(obj_id);
2445 245d91c1 2018-07-12 stsp got_object_close(obj);
2447 245d91c1 2018-07-12 stsp stop_blame(blame);
2448 245d91c1 2018-07-12 stsp return err;
2451 245d91c1 2018-07-12 stsp static const struct got_error *
2452 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2453 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2455 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2456 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2458 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2460 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2462 7cbe629d 2018-08-04 stsp return err;
2464 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2465 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2466 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2467 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2468 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2469 fb2756b9 2018-08-04 stsp s->path = path;
2470 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2471 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2472 fb2756b9 2018-08-04 stsp s->repo = repo;
2473 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2474 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2476 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2477 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2478 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2480 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
2481 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2482 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2483 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2486 e5a0f69f 2018-08-18 stsp static const struct got_error *
2487 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2489 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2490 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2492 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2493 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2495 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2496 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2497 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2498 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2499 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2502 e5a0f69f 2018-08-18 stsp free(s->path);
2504 e5a0f69f 2018-08-18 stsp return err;
2507 7cbe629d 2018-08-04 stsp static const struct got_error *
2508 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2510 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2511 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2513 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2514 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2515 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2516 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2518 669b5ffa 2018-10-07 stsp view_vborder(view);
2519 e5a0f69f 2018-08-18 stsp return err;
2522 e5a0f69f 2018-08-18 stsp static const struct got_error *
2523 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2524 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2526 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2527 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
2528 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2529 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2530 669b5ffa 2018-10-07 stsp int begin_x = 0;
2532 e5a0f69f 2018-08-18 stsp switch (ch) {
2534 e5a0f69f 2018-08-18 stsp s->done = 1;
2537 e5a0f69f 2018-08-18 stsp case KEY_UP:
2538 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2539 e5a0f69f 2018-08-18 stsp s->selected_line--;
2540 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2541 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2542 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2544 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2545 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2546 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2549 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2550 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2551 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2553 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2556 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2557 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2558 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2559 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2560 e5a0f69f 2018-08-18 stsp s->selected_line++;
2561 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2562 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2563 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2566 e5a0f69f 2018-08-18 stsp case 'p': {
2567 e5a0f69f 2018-08-18 stsp struct got_object_id *id;
2568 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2569 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2570 e5a0f69f 2018-08-18 stsp if (id == NULL || got_object_id_cmp(id,
2571 e5a0f69f 2018-08-18 stsp s->blamed_commit->id) == 0)
2573 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2574 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2575 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2578 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2580 e5a0f69f 2018-08-18 stsp if (ch == 'p' && pobj == NULL)
2582 e5a0f69f 2018-08-18 stsp s->done = 1;
2583 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2584 e5a0f69f 2018-08-18 stsp s->done = 0;
2585 e5a0f69f 2018-08-18 stsp if (thread_err)
2587 e5a0f69f 2018-08-18 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
2588 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2589 e5a0f69f 2018-08-18 stsp obj = NULL;
2590 e5a0f69f 2018-08-18 stsp if (pobj) {
2591 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2592 e5a0f69f 2018-08-18 stsp pobj = NULL;
2594 6402fb3c 2018-09-15 stsp err = got_object_qid_alloc(&s->blamed_commit, id);
2596 e5a0f69f 2018-08-18 stsp goto done;
2597 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2598 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2599 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2600 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2601 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2602 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2607 e5a0f69f 2018-08-18 stsp case 'B': {
2608 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2609 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2610 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2612 e5a0f69f 2018-08-18 stsp s->done = 1;
2613 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2614 e5a0f69f 2018-08-18 stsp s->done = 0;
2615 e5a0f69f 2018-08-18 stsp if (thread_err)
2617 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2618 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2619 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2620 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2621 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2622 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2623 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2624 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2629 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2630 e5a0f69f 2018-08-18 stsp case '\r':
2631 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2632 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2633 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2636 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2639 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
2640 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
2641 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2642 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2643 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2646 669b5ffa 2018-10-07 stsp err = open_diff_view(diff_view, pobj, obj, s->repo);
2647 e5a0f69f 2018-08-18 stsp if (err) {
2648 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2651 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
2652 669b5ffa 2018-10-07 stsp err = view_close_child(view);
2654 669b5ffa 2018-10-07 stsp return err;
2655 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
2656 669b5ffa 2018-10-07 stsp if (err) {
2657 669b5ffa 2018-10-07 stsp view_close(diff_view);
2660 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(diff_view)) {
2661 669b5ffa 2018-10-07 stsp *focus_view = diff_view;
2662 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
2665 669b5ffa 2018-10-07 stsp *new_view = diff_view;
2666 e5a0f69f 2018-08-18 stsp if (pobj) {
2667 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2668 e5a0f69f 2018-08-18 stsp pobj = NULL;
2670 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2671 e5a0f69f 2018-08-18 stsp obj = NULL;
2675 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2677 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2678 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2679 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2680 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2683 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2684 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2685 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2686 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2688 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2689 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2690 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2692 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2693 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2694 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2695 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2703 b880a918 2018-07-10 stsp got_object_close(pobj);
2704 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2707 a70480e0 2018-06-23 stsp static const struct got_error *
2708 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2710 a70480e0 2018-06-23 stsp const struct got_error *error;
2711 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2712 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2713 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2714 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2716 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2718 a70480e0 2018-06-23 stsp #ifndef PROFILE
2719 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2721 a70480e0 2018-06-23 stsp err(1, "pledge");
2724 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2725 a70480e0 2018-06-23 stsp switch (ch) {
2727 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2730 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2731 69069811 2018-08-02 stsp if (repo_path == NULL)
2732 69069811 2018-08-02 stsp err(1, "-r option");
2736 a70480e0 2018-06-23 stsp /* NOTREACHED */
2740 a70480e0 2018-06-23 stsp argc -= optind;
2741 a70480e0 2018-06-23 stsp argv += optind;
2743 69069811 2018-08-02 stsp if (argc == 1)
2744 69069811 2018-08-02 stsp path = argv[0];
2746 a70480e0 2018-06-23 stsp usage_blame();
2748 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2749 69069811 2018-08-02 stsp if (cwd == NULL) {
2750 69069811 2018-08-02 stsp error = got_error_from_errno();
2751 69069811 2018-08-02 stsp goto done;
2753 69069811 2018-08-02 stsp if (repo_path == NULL) {
2754 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2755 69069811 2018-08-02 stsp if (repo_path == NULL) {
2756 69069811 2018-08-02 stsp error = got_error_from_errno();
2757 69069811 2018-08-02 stsp goto done;
2762 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2763 a70480e0 2018-06-23 stsp if (error != NULL)
2764 66b4983c 2018-06-23 stsp return error;
2766 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2767 69069811 2018-08-02 stsp if (error != NULL)
2768 69069811 2018-08-02 stsp goto done;
2770 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2771 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2772 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2773 a70480e0 2018-06-23 stsp if (error != NULL)
2774 66b4983c 2018-06-23 stsp goto done;
2775 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2776 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2778 a70480e0 2018-06-23 stsp struct got_object *obj;
2779 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2780 a70480e0 2018-06-23 stsp if (error != NULL)
2781 66b4983c 2018-06-23 stsp goto done;
2782 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
2783 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2784 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2785 a19e88aa 2018-06-23 stsp got_object_close(obj);
2787 a19e88aa 2018-06-23 stsp if (error != NULL)
2788 a19e88aa 2018-06-23 stsp goto done;
2790 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2791 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2792 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2793 e1cd8fed 2018-08-01 stsp goto done;
2795 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2796 7cbe629d 2018-08-04 stsp if (error)
2797 7cbe629d 2018-08-04 stsp goto done;
2798 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2800 69069811 2018-08-02 stsp free(repo_path);
2801 69069811 2018-08-02 stsp free(cwd);
2802 a70480e0 2018-06-23 stsp free(commit_id);
2804 a70480e0 2018-06-23 stsp got_repo_close(repo);
2805 a70480e0 2018-06-23 stsp return error;
2808 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2809 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2810 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2811 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2812 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2813 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2814 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2816 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2817 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2818 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2819 ffd1d5e5 2018-06-23 stsp int width, n;
2821 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2823 f7d12f7e 2018-08-01 stsp werase(view->window);
2825 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2826 ffd1d5e5 2018-06-23 stsp return NULL;
2828 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2830 ffd1d5e5 2018-06-23 stsp return err;
2831 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2832 a3404814 2018-09-02 stsp wstandout(view->window);
2833 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2834 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2835 a3404814 2018-09-02 stsp wstandend(view->window);
2836 2550e4c3 2018-07-13 stsp free(wline);
2837 2550e4c3 2018-07-13 stsp wline = NULL;
2838 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2839 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2840 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2841 ffd1d5e5 2018-06-23 stsp return NULL;
2842 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2844 ce52c690 2018-06-23 stsp return err;
2845 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2846 2550e4c3 2018-07-13 stsp free(wline);
2847 2550e4c3 2018-07-13 stsp wline = NULL;
2848 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2849 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2850 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2851 ffd1d5e5 2018-06-23 stsp return NULL;
2852 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2853 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2854 a1eca9bb 2018-06-23 stsp return NULL;
2856 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2857 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2858 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2859 0cf4efb1 2018-09-29 stsp if (view->focussed)
2860 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2861 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2863 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2864 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
2865 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2866 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2867 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2868 ffd1d5e5 2018-06-23 stsp return NULL;
2872 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2873 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2876 ffd1d5e5 2018-06-23 stsp while (te) {
2877 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2879 1d13200f 2018-07-12 stsp if (show_ids) {
2880 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2882 1d13200f 2018-07-12 stsp return got_error_from_errno();
2884 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2885 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2886 1d13200f 2018-07-12 stsp free(id_str);
2887 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2889 1d13200f 2018-07-12 stsp free(id_str);
2890 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2891 ffd1d5e5 2018-06-23 stsp if (err) {
2892 ffd1d5e5 2018-06-23 stsp free(line);
2895 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2896 0cf4efb1 2018-09-29 stsp if (view->focussed)
2897 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2898 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2900 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2901 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2902 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2903 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
2904 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2905 ffd1d5e5 2018-06-23 stsp free(line);
2906 2550e4c3 2018-07-13 stsp free(wline);
2907 2550e4c3 2018-07-13 stsp wline = NULL;
2909 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2910 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2911 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2913 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2916 ffd1d5e5 2018-06-23 stsp return err;
2919 ffd1d5e5 2018-06-23 stsp static void
2920 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2921 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2923 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2926 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2929 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2930 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2931 ffd1d5e5 2018-06-23 stsp if (!isroot)
2932 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2936 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2937 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2938 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2939 ffd1d5e5 2018-06-23 stsp prev = te;
2940 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2942 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2943 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2945 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2946 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2949 ffd1d5e5 2018-06-23 stsp static void
2950 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2951 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2952 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2954 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2955 ffd1d5e5 2018-06-23 stsp int n = 0;
2957 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2960 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2961 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2963 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2964 ffd1d5e5 2018-06-23 stsp while (next) {
2965 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2966 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2968 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2972 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2973 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2974 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2976 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2977 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2978 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2980 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2981 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2983 ce52c690 2018-06-23 stsp len += strlen(te->name);
2985 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2986 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2987 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2989 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2990 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2991 d9765a41 2018-06-23 stsp while (pt) {
2992 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2993 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2994 cb2ebc8a 2018-06-23 stsp goto done;
2996 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2997 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2998 cb2ebc8a 2018-06-23 stsp goto done;
3000 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3003 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3004 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3005 ce52c690 2018-06-23 stsp goto done;
3009 ce52c690 2018-06-23 stsp if (err) {
3010 ce52c690 2018-06-23 stsp free(*path);
3011 ce52c690 2018-06-23 stsp *path = NULL;
3013 ce52c690 2018-06-23 stsp return err;
3016 ce52c690 2018-06-23 stsp static const struct got_error *
3017 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3018 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3019 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
3021 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3022 ce52c690 2018-06-23 stsp char *path;
3023 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3025 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3027 ce52c690 2018-06-23 stsp return err;
3029 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3030 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3031 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3033 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
3034 e5a0f69f 2018-08-18 stsp if (err) {
3035 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3036 e5a0f69f 2018-08-18 stsp free(path);
3038 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3039 69efd4c4 2018-07-18 stsp return err;
3042 69efd4c4 2018-07-18 stsp static const struct got_error *
3043 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3044 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3045 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
3047 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3048 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3049 69efd4c4 2018-07-18 stsp char *path;
3051 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3052 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3053 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3055 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3057 69efd4c4 2018-07-18 stsp return err;
3059 23721109 2018-10-22 stsp err = open_log_view(log_view, commit_id, repo, path, 0);
3061 e5a0f69f 2018-08-18 stsp view_close(log_view);
3063 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3064 cb2ebc8a 2018-06-23 stsp free(path);
3065 cb2ebc8a 2018-06-23 stsp return err;
3068 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3069 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3070 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
3072 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3073 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3074 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3076 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3078 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3079 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3080 ffd1d5e5 2018-06-23 stsp goto done;
3082 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3083 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
3084 ffd1d5e5 2018-06-23 stsp goto done;
3087 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3088 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3089 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3090 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3091 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3092 6484ec90 2018-09-29 stsp err = got_error_from_errno();
3093 6484ec90 2018-09-29 stsp goto done;
3095 fb2756b9 2018-08-04 stsp s->repo = repo;
3097 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
3098 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
3099 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
3101 ad80ab7b 2018-08-04 stsp free(commit_id_str);
3102 6484ec90 2018-09-29 stsp if (err) {
3103 fb2756b9 2018-08-04 stsp free(s->tree_label);
3104 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3106 ad80ab7b 2018-08-04 stsp return err;
3109 e5a0f69f 2018-08-18 stsp static const struct got_error *
3110 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
3112 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3114 fb2756b9 2018-08-04 stsp free(s->tree_label);
3115 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3116 6484ec90 2018-09-29 stsp free(s->commit_id);
3117 6484ec90 2018-09-29 stsp s->commit_id = NULL;
3118 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
3119 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
3120 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
3121 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
3122 ad80ab7b 2018-08-04 stsp free(parent);
3125 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
3126 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
3127 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
3129 e5a0f69f 2018-08-18 stsp return NULL;
3132 ad80ab7b 2018-08-04 stsp static const struct got_error *
3133 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
3135 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
3136 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3137 e5a0f69f 2018-08-18 stsp char *parent_path;
3139 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
3141 e5a0f69f 2018-08-18 stsp return err;
3143 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
3144 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
3145 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3146 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
3147 e5a0f69f 2018-08-18 stsp free(parent_path);
3149 669b5ffa 2018-10-07 stsp view_vborder(view);
3150 e5a0f69f 2018-08-18 stsp return err;
3153 e5a0f69f 2018-08-18 stsp static const struct got_error *
3154 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3155 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3157 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3158 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
3159 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
3160 669b5ffa 2018-10-07 stsp int begin_x = 0;
3162 e5a0f69f 2018-08-18 stsp switch (ch) {
3164 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
3167 669b5ffa 2018-10-07 stsp if (!s->selected_entry)
3169 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
3170 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
3171 669b5ffa 2018-10-07 stsp err = log_tree_entry(&log_view, begin_x,
3172 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
3173 669b5ffa 2018-10-07 stsp s->commit_id, s->repo);
3174 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3175 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3177 669b5ffa 2018-10-07 stsp return err;
3178 669b5ffa 2018-10-07 stsp err = view_set_child(view, log_view);
3179 669b5ffa 2018-10-07 stsp if (err) {
3180 669b5ffa 2018-10-07 stsp view_close(log_view);
3183 669b5ffa 2018-10-07 stsp *focus_view = log_view;
3184 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3186 669b5ffa 2018-10-07 stsp *new_view = log_view;
3189 e5a0f69f 2018-08-18 stsp case KEY_UP:
3190 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3191 e5a0f69f 2018-08-18 stsp s->selected--;
3192 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3194 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
3195 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
3197 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
3198 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
3199 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
3200 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
3201 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3202 e5a0f69f 2018-08-18 stsp s->selected = 0;
3205 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
3206 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
3207 e5a0f69f 2018-08-18 stsp s->tree == s->root);
3210 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
3211 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
3212 e5a0f69f 2018-08-18 stsp s->selected++;
3215 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
3216 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
3218 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
3219 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
3220 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
3221 e5a0f69f 2018-08-18 stsp s->entries);
3222 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
3225 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
3226 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
3227 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3229 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
3230 e5a0f69f 2018-08-18 stsp case '\r':
3231 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
3232 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3233 7837eeac 2018-09-24 stsp case KEY_BACKSPACE:
3234 e5a0f69f 2018-08-18 stsp /* user selected '..' */
3235 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
3237 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
3238 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
3240 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
3241 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
3242 e5a0f69f 2018-08-18 stsp s->entries =
3243 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3244 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
3245 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
3246 e5a0f69f 2018-08-18 stsp s->selected_entry =
3247 e5a0f69f 2018-08-18 stsp parent->selected_entry;
3248 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
3249 e5a0f69f 2018-08-18 stsp free(parent);
3250 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
3251 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3252 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
3253 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
3254 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
3257 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
3258 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
3259 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
3262 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
3263 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
3264 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
3265 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
3266 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
3267 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3268 e5a0f69f 2018-08-18 stsp s->tree = child;
3269 e5a0f69f 2018-08-18 stsp s->entries =
3270 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3271 e5a0f69f 2018-08-18 stsp s->selected = 0;
3272 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3273 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
3274 669b5ffa 2018-10-07 stsp struct tog_view *blame_view;
3275 669b5ffa 2018-10-07 stsp int begin_x = view_is_parent_view(view) ?
3276 669b5ffa 2018-10-07 stsp view_split_begin_x(view->begin_x) : 0;
3278 669b5ffa 2018-10-07 stsp err = blame_tree_entry(&blame_view, begin_x,
3279 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents, s->commit_id,
3283 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3284 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3286 669b5ffa 2018-10-07 stsp return err;
3287 669b5ffa 2018-10-07 stsp err = view_set_child(view, blame_view);
3288 669b5ffa 2018-10-07 stsp if (err) {
3289 669b5ffa 2018-10-07 stsp view_close(blame_view);
3292 669b5ffa 2018-10-07 stsp *focus_view = blame_view;
3293 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3295 669b5ffa 2018-10-07 stsp *new_view = blame_view;
3298 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3299 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3300 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3306 ffd1d5e5 2018-06-23 stsp return err;
3309 ffd1d5e5 2018-06-23 stsp __dead static void
3310 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3313 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3314 ffd1d5e5 2018-06-23 stsp getprogname());
3318 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3319 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3321 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3322 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3323 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3324 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3325 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3326 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3327 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3329 5221c383 2018-08-01 stsp struct tog_view *view;
3331 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3332 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3334 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3337 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3338 ffd1d5e5 2018-06-23 stsp switch (ch) {
3340 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3344 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3348 ffd1d5e5 2018-06-23 stsp argc -= optind;
3349 ffd1d5e5 2018-06-23 stsp argv += optind;
3351 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3352 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
3353 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3354 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3355 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3356 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3357 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3358 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3360 ffd1d5e5 2018-06-23 stsp usage_log();
3362 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3363 ffd1d5e5 2018-06-23 stsp free(repo_path);
3364 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3365 ffd1d5e5 2018-06-23 stsp return error;
3367 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
3368 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3369 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3370 ffd1d5e5 2018-06-23 stsp goto done;
3372 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
3373 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3374 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
3375 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
3376 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
3377 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
3380 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3381 ffd1d5e5 2018-06-23 stsp goto done;
3383 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3384 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3385 ffd1d5e5 2018-06-23 stsp goto done;
3387 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3388 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3389 ffd1d5e5 2018-06-23 stsp goto done;
3391 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3392 5221c383 2018-08-01 stsp if (view == NULL) {
3393 5221c383 2018-08-01 stsp error = got_error_from_errno();
3394 5221c383 2018-08-01 stsp goto done;
3396 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3397 ad80ab7b 2018-08-04 stsp if (error)
3398 ad80ab7b 2018-08-04 stsp goto done;
3399 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3401 ffd1d5e5 2018-06-23 stsp free(commit_id);
3402 ffd1d5e5 2018-06-23 stsp if (commit)
3403 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3405 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3407 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3408 ffd1d5e5 2018-06-23 stsp return error;
3410 5c5136c5 2018-05-20 stsp static void
3411 9f7d7167 2018-04-29 stsp init_curses(void)
3413 9f7d7167 2018-04-29 stsp initscr();
3415 fd823528 2018-10-22 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3418 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3419 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3420 1f475ad8 2018-05-10 stsp curs_set(0);
3423 4ed7e80c 2018-05-20 stsp __dead static void
3424 9f7d7167 2018-04-29 stsp usage(void)
3428 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3429 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3430 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3431 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3432 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3437 c2301be8 2018-04-30 stsp static char **
3438 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3440 c2301be8 2018-04-30 stsp char **argv;
3441 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3443 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3444 c2301be8 2018-04-30 stsp if (argv == NULL)
3445 c2301be8 2018-04-30 stsp err(1, "calloc");
3446 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3447 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3448 c2301be8 2018-04-30 stsp err(1, "calloc");
3449 c2301be8 2018-04-30 stsp if (arg1) {
3450 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3451 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3452 c2301be8 2018-04-30 stsp err(1, "calloc");
3455 c2301be8 2018-04-30 stsp return argv;
3459 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3461 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3462 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3463 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3464 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3466 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
3468 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3469 9f7d7167 2018-04-29 stsp switch (ch) {
3471 9f7d7167 2018-04-29 stsp hflag = 1;
3475 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3479 9f7d7167 2018-04-29 stsp argc -= optind;
3480 9f7d7167 2018-04-29 stsp argv += optind;
3481 9f7d7167 2018-04-29 stsp optind = 0;
3482 c2301be8 2018-04-30 stsp optreset = 1;
3484 c2301be8 2018-04-30 stsp if (argc == 0) {
3485 f29d3e89 2018-06-23 stsp if (hflag)
3487 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3488 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3489 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3494 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3495 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3496 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3497 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3498 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3499 9f7d7167 2018-04-29 stsp if (hflag)
3500 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3504 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3505 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3506 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3507 c2301be8 2018-04-30 stsp if (repo_path) {
3508 c2301be8 2018-04-30 stsp struct got_repository *repo;
3509 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3510 c2301be8 2018-04-30 stsp if (error == NULL)
3511 c2301be8 2018-04-30 stsp got_repo_close(repo);
3513 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3514 c2301be8 2018-04-30 stsp if (error) {
3515 f29d3e89 2018-06-23 stsp if (hflag) {
3516 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3517 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3521 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3522 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3523 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3524 ad7de8d9 2018-04-30 stsp free(repo_path);
3527 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3528 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3530 c2301be8 2018-04-30 stsp free(repo_path);
3534 5c5136c5 2018-05-20 stsp init_curses();
3536 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3537 9f7d7167 2018-04-29 stsp if (error)
3538 9f7d7167 2018-04-29 stsp goto done;
3541 c2301be8 2018-04-30 stsp free(cmd_argv);
3542 9f7d7167 2018-04-29 stsp if (error)
3543 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);