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>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
21 31120ada 2018-04-30 stsp #include <errno.h>
22 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
23 9f7d7167 2018-04-29 stsp #include <curses.h>
24 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
25 9f7d7167 2018-04-29 stsp #include <panel.h>
26 9f7d7167 2018-04-29 stsp #include <locale.h>
27 9f7d7167 2018-04-29 stsp #include <stdlib.h>
28 26ed57b2 2018-05-19 stsp #include <stdio.h>
29 9f7d7167 2018-04-29 stsp #include <getopt.h>
30 9f7d7167 2018-04-29 stsp #include <string.h>
31 9f7d7167 2018-04-29 stsp #include <err.h>
32 80ddbec8 2018-04-29 stsp #include <unistd.h>
33 26ed57b2 2018-05-19 stsp #include <util.h>
34 26ed57b2 2018-05-19 stsp #include <limits.h>
35 61e69b96 2018-05-20 stsp #include <wchar.h>
36 788c352e 2018-06-16 stsp #include <time.h>
37 84451b3e 2018-07-10 stsp #include <pthread.h>
38 5036bf37 2018-09-24 stsp #include <libgen.h>
40 9f7d7167 2018-04-29 stsp #include "got_error.h"
41 80ddbec8 2018-04-29 stsp #include "got_object.h"
42 80ddbec8 2018-04-29 stsp #include "got_reference.h"
43 80ddbec8 2018-04-29 stsp #include "got_repository.h"
44 80ddbec8 2018-04-29 stsp #include "got_diff.h"
45 511a516b 2018-05-19 stsp #include "got_opentemp.h"
46 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
47 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
48 a70480e0 2018-06-23 stsp #include "got_blame.h"
51 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
59 9f7d7167 2018-04-29 stsp #ifndef nitems
60 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 9f7d7167 2018-04-29 stsp struct tog_cmd {
64 c2301be8 2018-04-30 stsp const char *name;
65 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
66 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
67 c2301be8 2018-04-30 stsp const char *descr;
70 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
71 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
72 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
73 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
74 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
76 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
77 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
78 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
79 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
81 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
82 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
83 9f7d7167 2018-04-29 stsp "show repository history" },
84 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
85 9f7d7167 2018-04-29 stsp "compare files and directories" },
86 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
87 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
88 ffd1d5e5 2018-06-23 stsp { "tree", cmd_tree, usage_tree,
89 ffd1d5e5 2018-06-23 stsp "browse trees in repository" },
92 d6b05b5b 2018-08-04 stsp enum tog_view_type {
93 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
94 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
95 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
96 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
99 ad80ab7b 2018-08-04 stsp struct tog_diff_view_state {
100 a3404814 2018-09-02 stsp struct got_object_id *id1, *id2;
102 ad80ab7b 2018-08-04 stsp int first_displayed_line;
103 ad80ab7b 2018-08-04 stsp int last_displayed_line;
105 48ae06ee 2018-10-18 stsp int diff_context;
106 48ae06ee 2018-10-18 stsp struct got_repository *repo;
109 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
110 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
111 ba4f502b 2018-08-04 stsp struct got_object_id *id;
112 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
115 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
116 ba4f502b 2018-08-04 stsp struct commit_queue {
117 ba4f502b 2018-08-04 stsp int ncommits;
118 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
121 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
123 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
124 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
125 1a76625f 2018-10-22 stsp int commits_needed;
126 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
127 1a76625f 2018-10-22 stsp struct commit_queue *commits;
128 1a76625f 2018-10-22 stsp const char *in_repo_path;
129 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
130 1a76625f 2018-10-22 stsp struct got_repository *repo;
131 1a76625f 2018-10-22 stsp int log_complete;
132 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
133 1a76625f 2018-10-22 stsp struct tog_view *view;
134 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
135 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
138 1a76625f 2018-10-22 stsp struct tog_log_view_state {
139 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
140 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
141 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
142 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
143 b01e7d3b 2018-08-04 stsp int selected;
144 b01e7d3b 2018-08-04 stsp char *in_repo_path;
145 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
146 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
147 1a76625f 2018-10-22 stsp sig_atomic_t quit;
148 1a76625f 2018-10-22 stsp pthread_t thread;
149 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
152 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
153 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
154 e9424729 2018-08-04 stsp int nlines;
156 e9424729 2018-08-04 stsp struct tog_view *view;
157 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
161 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
162 e9424729 2018-08-04 stsp const char *path;
163 e9424729 2018-08-04 stsp struct got_repository *repo;
164 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
165 e9424729 2018-08-04 stsp int *complete;
168 e9424729 2018-08-04 stsp struct tog_blame {
170 e9424729 2018-08-04 stsp size_t filesize;
171 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
172 6fcac457 2018-11-19 stsp int nlines;
173 e9424729 2018-08-04 stsp pthread_t thread;
174 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
175 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
176 e9424729 2018-08-04 stsp const char *path;
179 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
180 7cbe629d 2018-08-04 stsp int first_displayed_line;
181 7cbe629d 2018-08-04 stsp int last_displayed_line;
182 7cbe629d 2018-08-04 stsp int selected_line;
183 7cbe629d 2018-08-04 stsp int blame_complete;
186 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
187 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
188 e5a0f69f 2018-08-18 stsp char *path;
189 7cbe629d 2018-08-04 stsp struct got_repository *repo;
190 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
191 e9424729 2018-08-04 stsp struct tog_blame blame;
194 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
195 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
196 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
197 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
198 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
199 ad80ab7b 2018-08-04 stsp int selected;
202 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
204 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
205 ad80ab7b 2018-08-04 stsp char *tree_label;
206 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
207 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
208 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
209 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
210 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
211 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
212 ad80ab7b 2018-08-04 stsp int nentries, ndisplayed, selected, show_ids;
213 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
214 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
215 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
219 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
221 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
222 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
223 669b5ffa 2018-10-07 stsp * there is enough screen estate.
225 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
226 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
228 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
229 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
230 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
232 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
233 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
235 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
237 cc3c9aac 2018-08-01 stsp struct tog_view {
238 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
239 26ed57b2 2018-05-19 stsp WINDOW *window;
240 26ed57b2 2018-05-19 stsp PANEL *panel;
241 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
242 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
243 1004088d 2018-09-29 stsp int focussed;
244 669b5ffa 2018-10-07 stsp struct tog_view *parent;
245 669b5ffa 2018-10-07 stsp struct tog_view *child;
246 669b5ffa 2018-10-07 stsp int child_focussed;
248 5dc9f4bc 2018-08-04 stsp /* type-specific state */
249 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
251 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
252 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
253 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
254 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
257 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
258 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
259 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
260 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
263 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
264 15a94983 2018-12-23 stsp struct got_object_id *, struct got_object_id *, struct got_repository *);
265 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
266 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
267 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
268 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
270 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
271 23721109 2018-10-22 stsp struct got_object_id *, struct got_repository *, const char *, int);
272 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
273 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_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_log_view(struct tog_view *);
277 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
278 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
279 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
280 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_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_blame_view(struct tog_view *);
284 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
285 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
286 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
287 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_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_tree_view(struct tog_view *);
291 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
293 25791caa 2018-10-24 stsp static void
294 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
296 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
299 e5a0f69f 2018-08-18 stsp static const struct got_error *
300 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
302 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
304 669b5ffa 2018-10-07 stsp if (view->child) {
305 669b5ffa 2018-10-07 stsp view_close(view->child);
306 669b5ffa 2018-10-07 stsp view->child = NULL;
308 e5a0f69f 2018-08-18 stsp if (view->close)
309 e5a0f69f 2018-08-18 stsp err = view->close(view);
310 ea5e7bb5 2018-08-01 stsp if (view->panel)
311 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
312 ea5e7bb5 2018-08-01 stsp if (view->window)
313 ea5e7bb5 2018-08-01 stsp delwin(view->window);
314 ea5e7bb5 2018-08-01 stsp free(view);
315 e5a0f69f 2018-08-18 stsp return err;
318 ea5e7bb5 2018-08-01 stsp static struct tog_view *
319 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
320 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
322 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
324 ea5e7bb5 2018-08-01 stsp if (view == NULL)
325 ea5e7bb5 2018-08-01 stsp return NULL;
327 d6b05b5b 2018-08-04 stsp view->type = type;
328 f7d12f7e 2018-08-01 stsp view->lines = LINES;
329 f7d12f7e 2018-08-01 stsp view->cols = COLS;
330 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
331 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
332 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
333 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
334 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
335 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
336 96a765a8 2018-08-04 stsp view_close(view);
337 ea5e7bb5 2018-08-01 stsp return NULL;
339 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
340 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
341 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
342 96a765a8 2018-08-04 stsp view_close(view);
343 ea5e7bb5 2018-08-01 stsp return NULL;
346 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
347 ea5e7bb5 2018-08-01 stsp return view;
351 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
353 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
355 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
358 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
360 5c60c32a 2018-10-18 stsp static const struct got_error *
361 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
363 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
365 5c60c32a 2018-10-18 stsp view->begin_y = 0;
366 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
367 5c60c32a 2018-10-18 stsp view->nlines = LINES;
368 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
369 5c60c32a 2018-10-18 stsp view->lines = LINES;
370 5c60c32a 2018-10-18 stsp view->cols = COLS;
371 5c60c32a 2018-10-18 stsp err = view_resize(view);
373 5c60c32a 2018-10-18 stsp return err;
375 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
376 5c60c32a 2018-10-18 stsp return got_error_from_errno();
378 5c60c32a 2018-10-18 stsp return NULL;
381 5c60c32a 2018-10-18 stsp static const struct got_error *
382 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
384 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
386 5c60c32a 2018-10-18 stsp view->begin_x = 0;
387 5c60c32a 2018-10-18 stsp view->begin_y = 0;
388 5c60c32a 2018-10-18 stsp view->nlines = LINES;
389 5c60c32a 2018-10-18 stsp view->ncols = COLS;
390 5c60c32a 2018-10-18 stsp view->lines = LINES;
391 5c60c32a 2018-10-18 stsp view->cols = COLS;
392 5c60c32a 2018-10-18 stsp err = view_resize(view);
394 5c60c32a 2018-10-18 stsp return err;
396 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
397 5c60c32a 2018-10-18 stsp return got_error_from_errno();
399 5c60c32a 2018-10-18 stsp return NULL;
403 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
405 5c60c32a 2018-10-18 stsp return view->parent == NULL;
408 4d8c2215 2018-08-19 stsp static const struct got_error *
409 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
411 f7d12f7e 2018-08-01 stsp int nlines, ncols;
413 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
414 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
416 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
418 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
419 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
421 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
423 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
424 0cf4efb1 2018-09-29 stsp return got_error_from_errno();
425 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
426 a6d7eb8d 2018-10-24 stsp return got_error_from_errno();
427 25791caa 2018-10-24 stsp wclear(view->window);
429 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
430 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
431 0cf4efb1 2018-09-29 stsp view->lines = LINES;
432 0cf4efb1 2018-09-29 stsp view->cols = COLS;
434 6e3e5d9c 2018-10-18 stsp if (view->child) {
435 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
436 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
437 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
438 5c60c32a 2018-10-18 stsp if (view->child->focussed)
439 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
441 5c60c32a 2018-10-18 stsp show_panel(view->panel);
443 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
444 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
448 5c60c32a 2018-10-18 stsp return NULL;
451 669b5ffa 2018-10-07 stsp static const struct got_error *
452 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
454 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
456 669b5ffa 2018-10-07 stsp if (view->child == NULL)
457 669b5ffa 2018-10-07 stsp return NULL;
459 669b5ffa 2018-10-07 stsp err = view_close(view->child);
460 669b5ffa 2018-10-07 stsp view->child = NULL;
461 669b5ffa 2018-10-07 stsp return err;
464 669b5ffa 2018-10-07 stsp static const struct got_error *
465 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
467 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
469 669b5ffa 2018-10-07 stsp view->child = child;
470 669b5ffa 2018-10-07 stsp child->parent = view;
471 669b5ffa 2018-10-07 stsp return err;
475 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
477 669b5ffa 2018-10-07 stsp return !view_is_parent_view(view) && view->begin_x > 0;
480 25791caa 2018-10-24 stsp static void
481 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
483 25791caa 2018-10-24 stsp int cols, lines;
484 25791caa 2018-10-24 stsp struct winsize size;
486 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
487 25791caa 2018-10-24 stsp cols = 80; /* Default */
488 25791caa 2018-10-24 stsp lines = 24;
490 25791caa 2018-10-24 stsp cols = size.ws_col;
491 25791caa 2018-10-24 stsp lines = size.ws_row;
493 25791caa 2018-10-24 stsp resize_term(lines, cols);
496 0cf4efb1 2018-09-29 stsp static const struct got_error *
497 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
498 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
499 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
501 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
502 669b5ffa 2018-10-07 stsp struct tog_view *v;
503 1a76625f 2018-10-22 stsp int ch, errcode;
505 e5a0f69f 2018-08-18 stsp *new = NULL;
506 e5a0f69f 2018-08-18 stsp *dead = NULL;
507 0cf4efb1 2018-09-29 stsp *focus = NULL;
509 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
510 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
511 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
512 1a76625f 2018-10-22 stsp if (errcode)
513 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
514 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
515 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
516 1a76625f 2018-10-22 stsp if (errcode)
517 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
518 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
520 25791caa 2018-10-24 stsp if (tog_sigwinch_received) {
521 25791caa 2018-10-24 stsp tog_resizeterm();
522 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
523 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
524 25791caa 2018-10-24 stsp err = view_resize(v);
526 25791caa 2018-10-24 stsp return err;
527 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
529 25791caa 2018-10-24 stsp return err;
533 e5a0f69f 2018-08-18 stsp switch (ch) {
537 669b5ffa 2018-10-07 stsp if (view->child) {
538 669b5ffa 2018-10-07 stsp *focus = view->child;
539 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
540 669b5ffa 2018-10-07 stsp } else if (view->parent) {
541 669b5ffa 2018-10-07 stsp *focus = view->parent;
542 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 0;
546 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
547 e5a0f69f 2018-08-18 stsp *dead = view;
553 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
554 669b5ffa 2018-10-07 stsp if (view->child == NULL)
556 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view->child)) {
557 669b5ffa 2018-10-07 stsp *focus = view->child;
558 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
559 669b5ffa 2018-10-07 stsp err = view_fullscreen(view->child);
561 669b5ffa 2018-10-07 stsp err = view_splitscreen(view->child);
564 669b5ffa 2018-10-07 stsp err = view->child->input(new, dead, focus,
565 669b5ffa 2018-10-07 stsp view->child, KEY_RESIZE);
567 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view)) {
568 669b5ffa 2018-10-07 stsp *focus = view;
569 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 1;
570 669b5ffa 2018-10-07 stsp err = view_fullscreen(view);
572 669b5ffa 2018-10-07 stsp err = view_splitscreen(view);
576 669b5ffa 2018-10-07 stsp err = view->input(new, dead, focus, view,
577 669b5ffa 2018-10-07 stsp KEY_RESIZE);
580 0cf4efb1 2018-09-29 stsp case KEY_RESIZE:
583 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
587 e5a0f69f 2018-08-18 stsp return err;
591 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
593 0cf4efb1 2018-09-29 stsp PANEL *panel;
594 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
596 669b5ffa 2018-10-07 stsp if (view->parent)
597 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
599 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
600 0cf4efb1 2018-09-29 stsp if (panel == NULL)
603 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
604 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
605 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
609 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
611 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
612 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
614 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
616 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
619 669b5ffa 2018-10-07 stsp return view->focussed;
622 bcbd79e2 2018-08-19 stsp static const struct got_error *
623 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
625 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
626 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
627 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
628 fd823528 2018-10-22 stsp int fast_refresh = 10;
629 1a76625f 2018-10-22 stsp int done = 0, errcode;
631 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
632 1a76625f 2018-10-22 stsp if (errcode)
633 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
635 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
636 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
638 a81bf10d 2018-09-29 stsp main_view = view;
639 1004088d 2018-09-29 stsp view->focussed = 1;
640 878940b7 2018-09-29 stsp err = view->show(view);
642 0cf4efb1 2018-09-29 stsp return err;
643 0cf4efb1 2018-09-29 stsp update_panels();
644 0cf4efb1 2018-09-29 stsp doupdate();
645 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
646 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
647 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
648 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
650 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
651 e4197bf9 2018-08-18 stsp view, &views);
654 e5a0f69f 2018-08-18 stsp if (dead_view) {
655 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
657 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
658 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
659 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
660 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
661 669b5ffa 2018-10-07 stsp prev = view->parent;
663 669b5ffa 2018-10-07 stsp if (dead_view->parent)
664 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
666 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
668 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
669 a81bf10d 2018-09-29 stsp if (err || dead_view == main_view)
672 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
673 0cf4efb1 2018-09-29 stsp if (focus_view)
674 0cf4efb1 2018-09-29 stsp view = focus_view;
675 669b5ffa 2018-10-07 stsp else if (prev)
676 669b5ffa 2018-10-07 stsp view = prev;
677 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
678 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
679 0cf4efb1 2018-09-29 stsp tog_view_list_head);
681 0cf4efb1 2018-09-29 stsp view = NULL;
682 669b5ffa 2018-10-07 stsp if (view) {
683 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
684 669b5ffa 2018-10-07 stsp focus_view = view->child;
686 669b5ffa 2018-10-07 stsp focus_view = view;
690 bcbd79e2 2018-08-19 stsp if (new_view) {
691 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
692 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
693 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
694 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
696 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
697 86c66b02 2018-10-18 stsp err = view_close(v);
702 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
703 fed7eaa8 2018-10-24 stsp view = new_view;
704 878940b7 2018-09-29 stsp if (focus_view == NULL)
705 878940b7 2018-09-29 stsp focus_view = new_view;
707 0cf4efb1 2018-09-29 stsp if (focus_view) {
708 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
710 0cf4efb1 2018-09-29 stsp view->focussed = 0;
711 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
712 0cf4efb1 2018-09-29 stsp view = focus_view;
713 878940b7 2018-09-29 stsp if (new_view)
714 878940b7 2018-09-29 stsp show_panel(new_view->panel);
715 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
716 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
718 669b5ffa 2018-10-07 stsp if (view) {
719 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
720 758194b5 2018-10-24 stsp view->focussed = 1;
721 758194b5 2018-10-24 stsp show_panel(view->panel);
722 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
723 758194b5 2018-10-24 stsp show_panel(view->child->panel);
724 1a76625f 2018-10-22 stsp focus_view = view;
726 669b5ffa 2018-10-07 stsp if (view->parent) {
727 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
731 669b5ffa 2018-10-07 stsp err = view->show(view);
734 669b5ffa 2018-10-07 stsp if (view->child) {
735 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
739 1a76625f 2018-10-22 stsp update_panels();
740 1a76625f 2018-10-22 stsp doupdate();
744 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
745 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
746 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
747 e5a0f69f 2018-08-18 stsp view_close(view);
750 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
751 1a76625f 2018-10-22 stsp if (errcode)
752 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
754 e5a0f69f 2018-08-18 stsp return err;
757 4ed7e80c 2018-05-20 stsp __dead static void
758 9f7d7167 2018-04-29 stsp usage_log(void)
761 c70c5802 2018-08-01 stsp fprintf(stderr,
762 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
763 9f7d7167 2018-04-29 stsp getprogname());
767 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
768 80ddbec8 2018-04-29 stsp static const struct got_error *
769 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
771 00dfcb92 2018-06-11 stsp char *vis = NULL;
772 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
774 963b370f 2018-05-20 stsp *ws = NULL;
775 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
776 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
777 00dfcb92 2018-06-11 stsp int vislen;
778 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
779 00dfcb92 2018-06-11 stsp return got_error_from_errno();
781 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
782 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
784 00dfcb92 2018-06-11 stsp return err;
785 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
786 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
787 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
792 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
793 a7f50699 2018-06-11 stsp if (*ws == NULL) {
794 a7f50699 2018-06-11 stsp err = got_error_from_errno();
798 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
799 963b370f 2018-05-20 stsp err = got_error_from_errno();
804 963b370f 2018-05-20 stsp *ws = NULL;
807 963b370f 2018-05-20 stsp return err;
810 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
811 963b370f 2018-05-20 stsp static const struct got_error *
812 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
814 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
815 963b370f 2018-05-20 stsp int cols = 0;
816 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
817 963b370f 2018-05-20 stsp size_t wlen;
820 963b370f 2018-05-20 stsp *wlinep = NULL;
821 b700b5d6 2018-07-10 stsp *widthp = 0;
823 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
825 963b370f 2018-05-20 stsp return err;
828 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
829 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
830 963b370f 2018-05-20 stsp switch (width) {
836 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
837 b700b5d6 2018-07-10 stsp cols += width;
841 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
842 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
846 963b370f 2018-05-20 stsp err = got_error_from_errno();
850 963b370f 2018-05-20 stsp wline[i] = L'\0';
851 b700b5d6 2018-07-10 stsp if (widthp)
852 b700b5d6 2018-07-10 stsp *widthp = cols;
855 963b370f 2018-05-20 stsp free(wline);
857 963b370f 2018-05-20 stsp *wlinep = wline;
858 963b370f 2018-05-20 stsp return err;
861 963b370f 2018-05-20 stsp static const struct got_error *
862 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
863 2814baeb 2018-08-01 stsp struct got_object_id *id)
865 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
866 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
867 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
868 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
869 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
870 bb737323 2018-05-20 stsp int author_width, logmsg_width;
871 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
872 80ddbec8 2018-04-29 stsp char *line = NULL;
873 bb737323 2018-05-20 stsp int col, limit;
874 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
875 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
876 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
877 ccb26ccd 2018-11-05 stsp struct tm tm;
878 45d799e2 2018-12-23 stsp time_t committer_time;
880 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
881 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
882 ccb26ccd 2018-11-05 stsp return got_error_from_errno();
883 ccb26ccd 2018-11-05 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
884 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
885 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
887 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
888 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
890 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
891 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
892 b39d25c7 2018-07-10 stsp col = limit + 1;
893 b39d25c7 2018-07-10 stsp if (col > avail)
896 45d799e2 2018-12-23 stsp author0 = strdup(got_object_commit_get_author(commit));
897 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
898 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
901 6d9fbc00 2018-04-29 stsp author = author0;
902 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
903 6d9fbc00 2018-04-29 stsp if (smallerthan)
904 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
906 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
908 6d9fbc00 2018-04-29 stsp *at = '\0';
910 b39d25c7 2018-07-10 stsp limit = avail - col;
911 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
914 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
915 bb737323 2018-05-20 stsp col += author_width;
916 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
917 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
919 bb737323 2018-05-20 stsp author_width++;
921 9c2eaf34 2018-05-20 stsp if (col > avail)
924 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
925 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
926 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
929 bb737323 2018-05-20 stsp logmsg = logmsg0;
930 bb737323 2018-05-20 stsp while (*logmsg == '\n')
932 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
933 bb737323 2018-05-20 stsp if (newline)
934 bb737323 2018-05-20 stsp *newline = '\0';
935 bb737323 2018-05-20 stsp limit = avail - col;
936 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
939 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
940 bb737323 2018-05-20 stsp col += logmsg_width;
941 bb737323 2018-05-20 stsp while (col <= avail) {
942 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
946 80ddbec8 2018-04-29 stsp free(logmsg0);
947 bb737323 2018-05-20 stsp free(wlogmsg);
948 6d9fbc00 2018-04-29 stsp free(author0);
949 bb737323 2018-05-20 stsp free(wauthor);
950 80ddbec8 2018-04-29 stsp free(line);
951 80ddbec8 2018-04-29 stsp return err;
954 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
955 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
956 899d86c2 2018-05-10 stsp struct got_object_id *id)
958 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
960 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
961 80ddbec8 2018-04-29 stsp if (entry == NULL)
962 899d86c2 2018-05-10 stsp return NULL;
964 899d86c2 2018-05-10 stsp entry->id = id;
965 99db9666 2018-05-07 stsp entry->commit = commit;
966 899d86c2 2018-05-10 stsp return entry;
969 99db9666 2018-05-07 stsp static void
970 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
972 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
974 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
975 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
976 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
977 ecb28ae0 2018-07-16 stsp commits->ncommits--;
978 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
979 99db9666 2018-05-07 stsp free(entry);
982 99db9666 2018-05-07 stsp static void
983 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
985 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
986 99db9666 2018-05-07 stsp pop_commit(commits);
989 c4972b91 2018-05-07 stsp static const struct got_error *
990 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
991 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
993 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
994 93e45b7c 2018-09-24 stsp int nqueued = 0;
997 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
998 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
999 1a76625f 2018-10-22 stsp * while updating the display.
1001 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
1002 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1003 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1004 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1005 1a76625f 2018-10-22 stsp int errcode;
1007 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1008 9ba79e04 2018-06-11 stsp if (err) {
1009 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1011 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
1012 93e45b7c 2018-09-24 stsp minqueue, repo);
1014 ecb28ae0 2018-07-16 stsp return err;
1018 ecb28ae0 2018-07-16 stsp if (id == NULL)
1021 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1024 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1025 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1026 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
1030 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1031 1a76625f 2018-10-22 stsp if (errcode) {
1032 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1036 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1037 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1038 ecb28ae0 2018-07-16 stsp nqueued++;
1039 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1041 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1042 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1043 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1046 9ba79e04 2018-06-11 stsp return err;
1049 99db9666 2018-05-07 stsp static const struct got_error *
1050 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1052 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1053 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1055 9ba79e04 2018-06-11 stsp *head_id = NULL;
1057 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1059 99db9666 2018-05-07 stsp return err;
1061 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1062 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1063 9ba79e04 2018-06-11 stsp if (err) {
1064 9ba79e04 2018-06-11 stsp *head_id = NULL;
1065 99db9666 2018-05-07 stsp return err;
1068 9ba79e04 2018-06-11 stsp return NULL;
1071 0553a4e3 2018-04-30 stsp static const struct got_error *
1072 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1073 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1074 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1075 1a76625f 2018-10-22 stsp const char *path, int commits_needed)
1077 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1078 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1079 ecb28ae0 2018-07-16 stsp int ncommits, width;
1080 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1081 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1083 e0d42f60 2018-07-22 stsp entry = first;
1084 e0d42f60 2018-07-22 stsp ncommits = 0;
1085 e0d42f60 2018-07-22 stsp while (entry) {
1086 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1087 e0d42f60 2018-07-22 stsp *selected = entry;
1090 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1091 e0d42f60 2018-07-22 stsp ncommits++;
1094 1a76625f 2018-10-22 stsp if (*selected) {
1095 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1097 ecb28ae0 2018-07-16 stsp return err;
1100 1a76625f 2018-10-22 stsp if (asprintf(&ncommits_str, " [%d/%d]%s ",
1101 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1102 1a76625f 2018-10-22 stsp commits_needed == 0 ? "" : " loading...") == -1)
1103 1a76625f 2018-10-22 stsp return got_error_from_errno();
1105 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1106 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1107 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1108 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1109 1a76625f 2018-10-22 stsp err = got_error_from_errno();
1110 1a76625f 2018-10-22 stsp header = NULL;
1111 1a76625f 2018-10-22 stsp goto done;
1113 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1114 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1115 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1116 1a76625f 2018-10-22 stsp err = got_error_from_errno();
1117 1a76625f 2018-10-22 stsp header = NULL;
1118 1a76625f 2018-10-22 stsp goto done;
1120 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1122 1a76625f 2018-10-22 stsp goto done;
1124 2814baeb 2018-08-01 stsp werase(view->window);
1126 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1127 a3404814 2018-09-02 stsp wstandout(view->window);
1128 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1129 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1130 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1133 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1134 a3404814 2018-09-02 stsp wstandend(view->window);
1135 ecb28ae0 2018-07-16 stsp free(wline);
1136 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1137 1a76625f 2018-10-22 stsp goto done;
1139 899d86c2 2018-05-10 stsp entry = first;
1140 899d86c2 2018-05-10 stsp *last = first;
1141 867c6645 2018-07-10 stsp ncommits = 0;
1142 899d86c2 2018-05-10 stsp while (entry) {
1143 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1145 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1146 2814baeb 2018-08-01 stsp wstandout(view->window);
1147 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
1148 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1149 2814baeb 2018-08-01 stsp wstandend(view->window);
1152 0553a4e3 2018-04-30 stsp ncommits++;
1153 899d86c2 2018-05-10 stsp *last = entry;
1154 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1157 1a57306a 2018-09-02 stsp view_vborder(view);
1159 1a76625f 2018-10-22 stsp free(id_str);
1160 1a76625f 2018-10-22 stsp free(ncommits_str);
1161 1a76625f 2018-10-22 stsp free(header);
1162 80ddbec8 2018-04-29 stsp return err;
1165 07b55e75 2018-05-10 stsp static void
1166 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1167 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1169 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1170 07b55e75 2018-05-10 stsp int nscrolled = 0;
1172 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1173 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
1176 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1177 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1178 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1179 07b55e75 2018-05-10 stsp if (entry) {
1180 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1181 07b55e75 2018-05-10 stsp nscrolled++;
1186 aa075928 2018-05-10 stsp static const struct got_error *
1187 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1188 93e45b7c 2018-09-24 stsp struct commit_queue_entry **last_displayed_entry,
1189 1a76625f 2018-10-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1190 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1192 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
1193 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
1194 aa075928 2018-05-10 stsp int nscrolled = 0;
1196 1a76625f 2018-10-22 stsp if (*last_displayed_entry == NULL)
1197 1a76625f 2018-10-22 stsp return NULL;
1200 93e45b7c 2018-09-24 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1201 aa075928 2018-05-10 stsp if (pentry == NULL) {
1202 1a76625f 2018-10-22 stsp int errcode;
1203 1a76625f 2018-10-22 stsp if (*log_complete)
1204 1a76625f 2018-10-22 stsp return NULL;
1205 1a76625f 2018-10-22 stsp *commits_needed = maxscroll + 20;
1206 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(need_commits);
1207 1a76625f 2018-10-22 stsp if (errcode)
1208 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1209 1a76625f 2018-10-22 stsp return NULL;
1211 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1213 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1214 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1216 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1217 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1219 dd0a52c1 2018-05-20 stsp return err;
1222 cd0acaa7 2018-05-20 stsp static const struct got_error *
1223 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1224 0cf4efb1 2018-09-29 stsp struct got_object_id *commit_id, struct got_commit_object *commit,
1225 2a4718d3 2018-09-29 stsp struct got_repository *repo)
1227 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1228 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1229 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1231 45d799e2 2018-12-23 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1233 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1234 15a94983 2018-12-23 stsp if (diff_view == NULL)
1235 15a94983 2018-12-23 stsp return got_error_from_errno();
1237 15a94983 2018-12-23 stsp err = open_diff_view(diff_view, parent_id->id, commit_id, repo);
1238 e5a0f69f 2018-08-18 stsp if (err == NULL)
1239 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1240 cd0acaa7 2018-05-20 stsp return err;
1243 80ddbec8 2018-04-29 stsp static const struct got_error *
1244 0cf4efb1 2018-09-29 stsp browse_commit(struct tog_view **new_view, int begin_x,
1245 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1247 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1248 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1249 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1251 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1252 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1254 9343a5fb 2018-06-23 stsp return err;
1256 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1257 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1258 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1260 e5a0f69f 2018-08-18 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1262 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1264 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1265 1a76625f 2018-10-22 stsp return err;
1268 1a76625f 2018-10-22 stsp static void *
1269 1a76625f 2018-10-22 stsp log_thread(void *arg)
1271 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1272 1a76625f 2018-10-22 stsp int errcode = 0;
1273 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1274 1a76625f 2018-10-22 stsp int done = 0;
1276 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1278 1a76625f 2018-10-22 stsp return (void *)err;
1280 1a76625f 2018-10-22 stsp while (!done && !err) {
1281 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1282 1a76625f 2018-10-22 stsp a->in_repo_path);
1283 1a76625f 2018-10-22 stsp if (err) {
1284 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1285 1a76625f 2018-10-22 stsp return (void *)err;
1286 1a76625f 2018-10-22 stsp err = NULL;
1288 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1289 1a76625f 2018-10-22 stsp a->commits_needed--;
1291 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1292 1a76625f 2018-10-22 stsp if (errcode)
1293 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
1296 1a76625f 2018-10-22 stsp a->log_complete = 1;
1297 1a76625f 2018-10-22 stsp else if (*a->quit) {
1299 1a76625f 2018-10-22 stsp a->log_complete = 1;
1300 1a76625f 2018-10-22 stsp } else if (*a->first_displayed_entry == NULL) {
1301 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1302 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1303 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1307 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1308 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1309 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1310 1a76625f 2018-10-22 stsp &tog_mutex);
1311 1a76625f 2018-10-22 stsp if (errcode)
1312 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1315 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1316 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1317 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1319 1a76625f 2018-10-22 stsp return (void *)err;
1322 1a76625f 2018-10-22 stsp static const struct got_error *
1323 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1325 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1326 1a76625f 2018-10-22 stsp int errcode;
1328 1a76625f 2018-10-22 stsp if (s->thread) {
1329 1a76625f 2018-10-22 stsp s->quit = 1;
1330 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1331 1a76625f 2018-10-22 stsp if (errcode)
1332 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1333 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1334 1a76625f 2018-10-22 stsp if (errcode)
1335 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1336 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1337 1a76625f 2018-10-22 stsp if (errcode)
1338 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1339 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1340 1a76625f 2018-10-22 stsp if (errcode)
1341 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1342 1a76625f 2018-10-22 stsp s->thread = NULL;
1345 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1346 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1347 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1349 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1350 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1351 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1354 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1355 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1356 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1359 9343a5fb 2018-06-23 stsp return err;
1362 9343a5fb 2018-06-23 stsp static const struct got_error *
1363 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1365 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1366 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1368 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1369 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1370 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1371 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1372 1a76625f 2018-10-22 stsp free(s->start_id);
1373 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1374 1a76625f 2018-10-22 stsp return err;
1377 1a76625f 2018-10-22 stsp static const struct got_error *
1378 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1379 23721109 2018-10-22 stsp struct got_repository *repo, const char *path, int check_disk)
1381 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1382 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1383 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1384 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1385 1a76625f 2018-10-22 stsp int errcode;
1387 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1388 ecb28ae0 2018-07-16 stsp if (err != NULL)
1389 ecb28ae0 2018-07-16 stsp goto done;
1391 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1392 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1393 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1395 fb2756b9 2018-08-04 stsp s->repo = repo;
1396 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1397 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1398 5036bf37 2018-09-24 stsp err = got_error_from_errno();
1399 5036bf37 2018-09-24 stsp goto done;
1402 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1403 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1404 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1406 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1408 1a76625f 2018-10-22 stsp goto done;
1409 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1410 1a76625f 2018-10-22 stsp 0, thread_repo);
1412 1a76625f 2018-10-22 stsp goto done;
1414 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1415 1a76625f 2018-10-22 stsp if (errcode) {
1416 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1417 1a76625f 2018-10-22 stsp goto done;
1420 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1421 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1422 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1423 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1424 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1425 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1426 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1427 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1428 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1429 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1430 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1433 1a76625f 2018-10-22 stsp close_log_view(view);
1434 ba4f502b 2018-08-04 stsp return err;
1437 e5a0f69f 2018-08-18 stsp static const struct got_error *
1438 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1440 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1442 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1443 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1444 2b380cc8 2018-10-24 stsp &s->thread_args);
1445 2b380cc8 2018-10-24 stsp if (errcode)
1446 2b380cc8 2018-10-24 stsp return got_error_set_errno(errcode);
1449 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1450 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1451 1a76625f 2018-10-22 stsp &s->commits, s->selected, view->nlines,
1452 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1455 e5a0f69f 2018-08-18 stsp static const struct got_error *
1456 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1457 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1459 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1460 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1461 5036bf37 2018-09-24 stsp char *parent_path;
1462 669b5ffa 2018-10-07 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
1463 669b5ffa 2018-10-07 stsp int begin_x = 0;
1465 e5a0f69f 2018-08-18 stsp switch (ch) {
1467 1a76625f 2018-10-22 stsp s->quit = 1;
1470 e5a0f69f 2018-08-18 stsp case KEY_UP:
1471 2b380cc8 2018-10-24 stsp if (s->first_displayed_entry == NULL)
1473 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1474 e5a0f69f 2018-08-18 stsp s->selected--;
1475 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1477 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1478 e5a0f69f 2018-08-18 stsp &s->commits);
1480 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1481 2b380cc8 2018-10-24 stsp if (s->first_displayed_entry == NULL)
1483 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1484 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1485 e5a0f69f 2018-08-18 stsp s->selected = 0;
1488 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1489 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1492 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1493 2b380cc8 2018-10-24 stsp if (s->first_displayed_entry == NULL)
1495 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1496 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1497 e5a0f69f 2018-08-18 stsp s->selected++;
1500 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1501 93e45b7c 2018-09-24 stsp &s->last_displayed_entry, &s->commits,
1502 1a76625f 2018-10-22 stsp &s->thread_args.log_complete,
1503 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1504 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1506 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1507 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1508 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1509 2b380cc8 2018-10-24 stsp if (first == NULL)
1511 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1512 93e45b7c 2018-09-24 stsp view->nlines, &s->last_displayed_entry,
1513 1a76625f 2018-10-22 stsp &s->commits, &s->thread_args.log_complete,
1514 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1515 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1516 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1517 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1518 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1519 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1520 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1521 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1523 e5a0f69f 2018-08-18 stsp err = NULL;
1526 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1527 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1528 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1529 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1530 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1532 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1533 e5a0f69f 2018-08-18 stsp case '\r':
1534 2b380cc8 2018-10-24 stsp if (s->selected_entry == NULL)
1536 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1537 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1538 669b5ffa 2018-10-07 stsp err = open_diff_view_for_commit(&diff_view, begin_x,
1539 2a4718d3 2018-09-29 stsp s->selected_entry->id, s->selected_entry->commit,
1543 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1544 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1546 669b5ffa 2018-10-07 stsp return err;
1547 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
1548 669b5ffa 2018-10-07 stsp if (err) {
1549 669b5ffa 2018-10-07 stsp view_close(diff_view);
1552 55566b34 2018-11-05 stsp *focus_view = diff_view;
1553 55566b34 2018-11-05 stsp view->child_focussed = 1;
1555 669b5ffa 2018-10-07 stsp *new_view = diff_view;
1558 2b380cc8 2018-10-24 stsp if (s->selected_entry == NULL)
1560 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1561 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1562 669b5ffa 2018-10-07 stsp err = browse_commit(&tree_view, begin_x,
1563 0cf4efb1 2018-09-29 stsp s->selected_entry, s->repo);
1566 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1567 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1569 669b5ffa 2018-10-07 stsp return err;
1570 669b5ffa 2018-10-07 stsp err = view_set_child(view, tree_view);
1571 669b5ffa 2018-10-07 stsp if (err) {
1572 669b5ffa 2018-10-07 stsp view_close(tree_view);
1575 669b5ffa 2018-10-07 stsp *focus_view = tree_view;
1576 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1578 669b5ffa 2018-10-07 stsp *new_view = tree_view;
1580 5036bf37 2018-09-24 stsp case KEY_BACKSPACE:
1581 5036bf37 2018-09-24 stsp if (strcmp(s->in_repo_path, "/") == 0)
1583 5036bf37 2018-09-24 stsp parent_path = dirname(s->in_repo_path);
1584 5036bf37 2018-09-24 stsp if (parent_path && strcmp(parent_path, ".") != 0) {
1585 5036bf37 2018-09-24 stsp struct tog_view *lv;
1586 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1588 1a76625f 2018-10-22 stsp return err;
1589 0cf4efb1 2018-09-29 stsp lv = view_open(view->nlines, view->ncols,
1590 0cf4efb1 2018-09-29 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
1591 5036bf37 2018-09-24 stsp if (lv == NULL)
1592 5036bf37 2018-09-24 stsp return got_error_from_errno();
1593 5036bf37 2018-09-24 stsp err = open_log_view(lv, s->start_id, s->repo,
1594 23721109 2018-10-22 stsp parent_path, 0);
1596 1a76625f 2018-10-22 stsp return err;;
1597 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1598 669b5ffa 2018-10-07 stsp *new_view = lv;
1600 669b5ffa 2018-10-07 stsp view_set_child(view->parent, lv);
1601 34095730 2018-10-22 stsp *focus_view = lv;
1603 1a76625f 2018-10-22 stsp return NULL;
1610 80ddbec8 2018-04-29 stsp return err;
1613 4ed7e80c 2018-05-20 stsp static const struct got_error *
1614 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1616 80ddbec8 2018-04-29 stsp const struct got_error *error;
1617 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1618 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1619 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1620 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1622 04cc582a 2018-08-01 stsp struct tog_view *view;
1624 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1625 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1627 80ddbec8 2018-04-29 stsp err(1, "pledge");
1630 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1631 80ddbec8 2018-04-29 stsp switch (ch) {
1633 80ddbec8 2018-04-29 stsp start_commit = optarg;
1636 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1637 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1638 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1642 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1646 80ddbec8 2018-04-29 stsp argc -= optind;
1647 80ddbec8 2018-04-29 stsp argv += optind;
1649 ecb28ae0 2018-07-16 stsp if (argc == 0)
1650 ecb28ae0 2018-07-16 stsp path = strdup("");
1651 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1652 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1654 80ddbec8 2018-04-29 stsp usage_log();
1655 ecb28ae0 2018-07-16 stsp if (path == NULL)
1656 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1658 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1659 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1660 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1661 ecb28ae0 2018-07-16 stsp goto done;
1663 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1664 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1665 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1666 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1667 ecb28ae0 2018-07-16 stsp goto done;
1671 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1672 80ddbec8 2018-04-29 stsp if (error != NULL)
1673 ecb28ae0 2018-07-16 stsp goto done;
1675 15a94983 2018-12-23 stsp if (start_commit == NULL)
1676 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1678 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&start_id, repo,
1679 15a94983 2018-12-23 stsp start_commit);
1680 80ddbec8 2018-04-29 stsp if (error != NULL)
1681 ecb28ae0 2018-07-16 stsp goto done;
1683 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1684 04cc582a 2018-08-01 stsp if (view == NULL) {
1685 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1686 04cc582a 2018-08-01 stsp goto done;
1688 23721109 2018-10-22 stsp error = open_log_view(view, start_id, repo, path, 1);
1689 ba4f502b 2018-08-04 stsp if (error)
1690 ba4f502b 2018-08-04 stsp goto done;
1691 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1693 ecb28ae0 2018-07-16 stsp free(repo_path);
1694 ecb28ae0 2018-07-16 stsp free(cwd);
1695 ecb28ae0 2018-07-16 stsp free(path);
1696 899d86c2 2018-05-10 stsp free(start_id);
1698 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1699 80ddbec8 2018-04-29 stsp return error;
1702 4ed7e80c 2018-05-20 stsp __dead static void
1703 9f7d7167 2018-04-29 stsp usage_diff(void)
1706 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1707 9f7d7167 2018-04-29 stsp getprogname());
1711 b304db33 2018-05-20 stsp static char *
1712 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1714 b304db33 2018-05-20 stsp char *line;
1715 b304db33 2018-05-20 stsp size_t linelen;
1716 b304db33 2018-05-20 stsp size_t lineno;
1717 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1719 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1721 b304db33 2018-05-20 stsp *len = linelen;
1722 b304db33 2018-05-20 stsp return line;
1725 4ed7e80c 2018-05-20 stsp static const struct got_error *
1726 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1727 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1728 a3404814 2018-09-02 stsp char * header)
1730 61e69b96 2018-05-20 stsp const struct got_error *err;
1731 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1732 b304db33 2018-05-20 stsp char *line;
1733 b304db33 2018-05-20 stsp size_t len;
1734 61e69b96 2018-05-20 stsp wchar_t *wline;
1735 e0b650dd 2018-05-20 stsp int width;
1737 26ed57b2 2018-05-19 stsp rewind(f);
1738 f7d12f7e 2018-08-01 stsp werase(view->window);
1740 a3404814 2018-09-02 stsp if (header) {
1741 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1742 a3404814 2018-09-02 stsp if (err) {
1743 a3404814 2018-09-02 stsp return err;
1746 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1747 a3404814 2018-09-02 stsp wstandout(view->window);
1748 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1749 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1750 a3404814 2018-09-02 stsp wstandend(view->window);
1751 a3404814 2018-09-02 stsp if (width < view->ncols)
1752 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1754 a3404814 2018-09-02 stsp if (max_lines <= 1)
1755 a3404814 2018-09-02 stsp return NULL;
1756 a3404814 2018-09-02 stsp max_lines--;
1760 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1761 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1762 26ed57b2 2018-05-19 stsp if (line == NULL) {
1766 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1767 26ed57b2 2018-05-19 stsp free(line);
1771 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1772 61e69b96 2018-05-20 stsp if (err) {
1773 61e69b96 2018-05-20 stsp free(line);
1774 61e69b96 2018-05-20 stsp return err;
1776 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1777 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1778 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1779 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1780 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1781 26ed57b2 2018-05-19 stsp free(line);
1782 2550e4c3 2018-07-13 stsp free(wline);
1783 2550e4c3 2018-07-13 stsp wline = NULL;
1785 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1787 1a57306a 2018-09-02 stsp view_vborder(view);
1789 26ed57b2 2018-05-19 stsp return NULL;
1792 abd2672a 2018-12-23 stsp static char *
1793 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
1795 abd2672a 2018-12-23 stsp char *p, *s = ctime_r(time, datebuf);
1796 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
1798 abd2672a 2018-12-23 stsp *p = '\0';
1802 4ed7e80c 2018-05-20 stsp static const struct got_error *
1803 15a94983 2018-12-23 stsp write_commit_info(struct got_object_id *commit_id, struct got_repository *repo,
1804 abd2672a 2018-12-23 stsp FILE *outfile)
1806 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
1807 abd2672a 2018-12-23 stsp char datebuf[26];
1808 15a94983 2018-12-23 stsp struct got_commit_object *commit;
1809 15a94983 2018-12-23 stsp char *id_str = NULL;
1810 45d799e2 2018-12-23 stsp time_t committer_time;
1811 45d799e2 2018-12-23 stsp const char *author, *committer;
1813 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1815 abd2672a 2018-12-23 stsp return err;
1817 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
1818 15a94983 2018-12-23 stsp if (err) {
1819 15a94983 2018-12-23 stsp err = got_error_from_errno();
1820 15a94983 2018-12-23 stsp goto done;
1823 c1124f18 2018-12-23 stsp if (fprintf(outfile, "commit %s\n", id_str) < 0) {
1824 abd2672a 2018-12-23 stsp err = got_error_from_errno();
1825 abd2672a 2018-12-23 stsp goto done;
1827 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
1828 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
1829 abd2672a 2018-12-23 stsp err = got_error_from_errno();
1830 abd2672a 2018-12-23 stsp goto done;
1832 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1833 abd2672a 2018-12-23 stsp if (fprintf(outfile, "date: %s UTC\n",
1834 45d799e2 2018-12-23 stsp get_datestr(&committer_time, datebuf)) < 0) {
1835 abd2672a 2018-12-23 stsp err = got_error_from_errno();
1836 abd2672a 2018-12-23 stsp goto done;
1838 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1839 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1840 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
1841 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
1842 abd2672a 2018-12-23 stsp err = got_error_from_errno();
1843 abd2672a 2018-12-23 stsp goto done;
1845 45d799e2 2018-12-23 stsp if (fprintf(outfile, "%s\n",
1846 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(commit)) < 0) {
1847 abd2672a 2018-12-23 stsp err = got_error_from_errno();
1848 abd2672a 2018-12-23 stsp goto done;
1851 abd2672a 2018-12-23 stsp free(id_str);
1852 15a94983 2018-12-23 stsp got_object_commit_close(commit);
1853 abd2672a 2018-12-23 stsp return err;
1856 abd2672a 2018-12-23 stsp static const struct got_error *
1857 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
1859 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
1860 48ae06ee 2018-10-18 stsp FILE *f = NULL;
1861 15a94983 2018-12-23 stsp int obj_type;
1863 511a516b 2018-05-19 stsp f = got_opentemp();
1864 48ae06ee 2018-10-18 stsp if (f == NULL) {
1865 48ae06ee 2018-10-18 stsp err = got_error_from_errno();
1866 48ae06ee 2018-10-18 stsp goto done;
1869 48ae06ee 2018-10-18 stsp fclose(s->f);
1872 15a94983 2018-12-23 stsp if (s->id1)
1873 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
1875 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
1877 15a94983 2018-12-23 stsp goto done;
1879 15a94983 2018-12-23 stsp switch (obj_type) {
1880 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1881 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
1882 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1884 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1885 15a94983 2018-12-23 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
1886 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1888 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
1889 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1890 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
1891 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
1893 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
1896 abd2672a 2018-12-23 stsp /* Show commit info if we're diffing to a parent commit. */
1897 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
1898 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
1899 15a94983 2018-12-23 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
1900 15a94983 2018-12-23 stsp write_commit_info(s->id2, s->repo, f);
1904 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
1906 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
1907 15a94983 2018-12-23 stsp s->diff_context, s->repo, f);
1911 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1916 48ae06ee 2018-10-18 stsp fflush(f);
1917 48ae06ee 2018-10-18 stsp return err;
1920 48ae06ee 2018-10-18 stsp static const struct got_error *
1921 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
1922 15a94983 2018-12-23 stsp struct got_object_id *id2, struct got_repository *repo)
1924 48ae06ee 2018-10-18 stsp const struct got_error *err;
1926 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
1927 15a94983 2018-12-23 stsp int type1, type2;
1928 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
1930 15a94983 2018-12-23 stsp return err;
1931 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
1933 15a94983 2018-12-23 stsp return err;
1935 15a94983 2018-12-23 stsp if (type1 != type2)
1936 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
1939 15a94983 2018-12-23 stsp if (id1) {
1940 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
1941 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
1942 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1944 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1946 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
1947 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
1948 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1949 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1950 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1952 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
1953 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1954 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1955 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
1956 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
1958 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
1959 48ae06ee 2018-10-18 stsp if (err) {
1960 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1961 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1962 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
1963 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
1964 48ae06ee 2018-10-18 stsp return err;
1967 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
1968 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
1969 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
1971 5dc9f4bc 2018-08-04 stsp return NULL;
1974 e5a0f69f 2018-08-18 stsp static const struct got_error *
1975 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1977 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1979 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1980 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1981 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
1982 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
1983 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1984 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1985 e5a0f69f 2018-08-18 stsp return err;
1988 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1989 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1991 a3404814 2018-09-02 stsp const struct got_error *err;
1992 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1993 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
1995 a3404814 2018-09-02 stsp if (s->id1) {
1996 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
1998 a3404814 2018-09-02 stsp return err;
2000 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2002 a3404814 2018-09-02 stsp return err;
2004 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2005 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2006 a3404814 2018-09-02 stsp err = got_error_from_errno();
2007 a3404814 2018-09-02 stsp free(id_str1);
2008 a3404814 2018-09-02 stsp free(id_str2);
2009 a3404814 2018-09-02 stsp return err;
2011 a3404814 2018-09-02 stsp free(id_str1);
2012 a3404814 2018-09-02 stsp free(id_str2);
2014 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2015 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2019 0cf4efb1 2018-09-29 stsp static const struct got_error *
2020 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2021 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2023 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2024 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2027 e5a0f69f 2018-08-18 stsp switch (ch) {
2029 e5a0f69f 2018-08-18 stsp case KEY_UP:
2030 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
2031 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2033 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2035 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
2036 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2037 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2040 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2041 e5a0f69f 2018-08-18 stsp if (!s->eof)
2042 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2044 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2047 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
2048 e5a0f69f 2018-08-18 stsp char *line;
2049 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
2050 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2051 e5a0f69f 2018-08-18 stsp if (line == NULL)
2056 48ae06ee 2018-10-18 stsp if (s->diff_context > 0) {
2057 48ae06ee 2018-10-18 stsp s->diff_context--;
2058 48ae06ee 2018-10-18 stsp err = create_diff(s);
2062 4a8520aa 2018-10-18 stsp if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2063 48ae06ee 2018-10-18 stsp s->diff_context++;
2064 48ae06ee 2018-10-18 stsp err = create_diff(s);
2071 bcbd79e2 2018-08-19 stsp return err;
2074 4ed7e80c 2018-05-20 stsp static const struct got_error *
2075 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2077 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2078 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2079 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2080 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2081 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2083 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2085 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2086 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2088 26ed57b2 2018-05-19 stsp err(1, "pledge");
2091 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2092 26ed57b2 2018-05-19 stsp switch (ch) {
2095 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2099 26ed57b2 2018-05-19 stsp argc -= optind;
2100 26ed57b2 2018-05-19 stsp argv += optind;
2102 26ed57b2 2018-05-19 stsp if (argc == 0) {
2103 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2104 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2105 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2106 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2107 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2108 15a94983 2018-12-23 stsp id_str1 = argv[0];
2109 15a94983 2018-12-23 stsp id_str2 = argv[1];
2110 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2111 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2112 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2113 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2114 15a94983 2018-12-23 stsp id_str1 = argv[1];
2115 15a94983 2018-12-23 stsp id_str2 = argv[2];
2117 26ed57b2 2018-05-19 stsp usage_diff();
2119 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
2120 26ed57b2 2018-05-19 stsp free(repo_path);
2121 26ed57b2 2018-05-19 stsp if (error)
2122 26ed57b2 2018-05-19 stsp goto done;
2124 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
2125 26ed57b2 2018-05-19 stsp if (error)
2126 26ed57b2 2018-05-19 stsp goto done;
2128 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
2129 26ed57b2 2018-05-19 stsp if (error)
2130 26ed57b2 2018-05-19 stsp goto done;
2132 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2133 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2134 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
2135 ea5e7bb5 2018-08-01 stsp goto done;
2137 15a94983 2018-12-23 stsp error = open_diff_view(view, id1, id2, repo);
2138 5dc9f4bc 2018-08-04 stsp if (error)
2139 5dc9f4bc 2018-08-04 stsp goto done;
2140 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2142 26ed57b2 2018-05-19 stsp got_repo_close(repo);
2143 26ed57b2 2018-05-19 stsp return error;
2146 4ed7e80c 2018-05-20 stsp __dead static void
2147 9f7d7167 2018-04-29 stsp usage_blame(void)
2150 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2151 9f7d7167 2018-04-29 stsp getprogname());
2155 84451b3e 2018-07-10 stsp struct tog_blame_line {
2156 84451b3e 2018-07-10 stsp int annotated;
2157 84451b3e 2018-07-10 stsp struct got_object_id *id;
2160 4ed7e80c 2018-05-20 stsp static const struct got_error *
2161 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2162 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2163 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2164 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2166 84451b3e 2018-07-10 stsp const struct got_error *err;
2167 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2168 84451b3e 2018-07-10 stsp char *line;
2169 84451b3e 2018-07-10 stsp size_t len;
2170 84451b3e 2018-07-10 stsp wchar_t *wline;
2171 b700b5d6 2018-07-10 stsp int width, wlimit;
2172 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2173 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2174 ab089a2a 2018-07-12 stsp char *id_str;
2176 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2178 ab089a2a 2018-07-12 stsp return err;
2180 84451b3e 2018-07-10 stsp rewind(f);
2181 f7d12f7e 2018-08-01 stsp werase(view->window);
2183 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
2184 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
2185 ab089a2a 2018-07-12 stsp free(id_str);
2186 ab089a2a 2018-07-12 stsp return err;
2189 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2190 ab089a2a 2018-07-12 stsp free(line);
2191 2550e4c3 2018-07-13 stsp line = NULL;
2192 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2193 a3404814 2018-09-02 stsp wstandout(view->window);
2194 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2195 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2196 a3404814 2018-09-02 stsp wstandend(view->window);
2197 2550e4c3 2018-07-13 stsp free(wline);
2198 2550e4c3 2018-07-13 stsp wline = NULL;
2199 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2200 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2202 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2203 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2204 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
2205 ab089a2a 2018-07-12 stsp free(id_str);
2206 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
2208 ab089a2a 2018-07-12 stsp free(id_str);
2209 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2210 3f60a8ef 2018-07-10 stsp free(line);
2211 2550e4c3 2018-07-13 stsp line = NULL;
2213 3f60a8ef 2018-07-10 stsp return err;
2214 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2215 2550e4c3 2018-07-13 stsp free(wline);
2216 2550e4c3 2018-07-13 stsp wline = NULL;
2217 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2218 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2221 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2222 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2223 84451b3e 2018-07-10 stsp if (line == NULL) {
2227 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2228 84451b3e 2018-07-10 stsp free(line);
2232 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2233 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2234 84451b3e 2018-07-10 stsp if (err) {
2235 84451b3e 2018-07-10 stsp free(line);
2236 84451b3e 2018-07-10 stsp return err;
2239 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2240 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2242 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2243 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2244 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2245 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2246 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2247 84451b3e 2018-07-10 stsp char *id_str;
2248 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2249 84451b3e 2018-07-10 stsp if (err) {
2250 84451b3e 2018-07-10 stsp free(line);
2251 2550e4c3 2018-07-13 stsp free(wline);
2252 84451b3e 2018-07-10 stsp return err;
2254 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2255 84451b3e 2018-07-10 stsp free(id_str);
2256 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2258 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2259 ee41ec32 2018-07-10 stsp prev_id = NULL;
2262 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2263 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2264 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2267 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2268 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2269 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2270 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2271 84451b3e 2018-07-10 stsp free(line);
2272 2550e4c3 2018-07-13 stsp free(wline);
2273 2550e4c3 2018-07-13 stsp wline = NULL;
2275 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2277 1a57306a 2018-09-02 stsp view_vborder(view);
2279 84451b3e 2018-07-10 stsp return NULL;
2282 84451b3e 2018-07-10 stsp static const struct got_error *
2283 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2285 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2286 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2287 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2288 1a76625f 2018-10-22 stsp int errcode;
2290 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2291 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2292 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2294 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2295 1a76625f 2018-10-22 stsp if (errcode)
2296 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2298 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2299 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2300 d68a0a7d 2018-07-10 stsp goto done;
2303 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2304 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2306 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2307 d68a0a7d 2018-07-10 stsp if (line->annotated)
2308 d68a0a7d 2018-07-10 stsp goto done;
2310 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2311 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2312 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2313 84451b3e 2018-07-10 stsp goto done;
2315 84451b3e 2018-07-10 stsp line->annotated = 1;
2317 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2318 1a76625f 2018-10-22 stsp if (errcode)
2319 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2320 84451b3e 2018-07-10 stsp return err;
2323 84451b3e 2018-07-10 stsp static void *
2324 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2326 18430de3 2018-07-10 stsp const struct got_error *err;
2327 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
2328 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
2329 1a76625f 2018-10-22 stsp int errcode;
2331 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2332 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
2334 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2335 1a76625f 2018-10-22 stsp if (errcode)
2336 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
2338 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
2339 c9beca56 2018-07-22 stsp ta->repo = NULL;
2340 c9beca56 2018-07-22 stsp *ta->complete = 1;
2342 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2343 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2344 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2346 18430de3 2018-07-10 stsp return (void *)err;
2349 245d91c1 2018-07-12 stsp static struct got_object_id *
2350 15a94983 2018-12-23 stsp get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2351 15a94983 2018-12-23 stsp int selected_line)
2353 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2355 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2356 245d91c1 2018-07-12 stsp if (!line->annotated)
2357 245d91c1 2018-07-12 stsp return NULL;
2359 245d91c1 2018-07-12 stsp return line->id;
2362 b880a918 2018-07-10 stsp static const struct got_error *
2363 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2365 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2368 245d91c1 2018-07-12 stsp if (blame->thread) {
2369 1a76625f 2018-10-22 stsp int errcode;
2370 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2371 1a76625f 2018-10-22 stsp if (errcode)
2372 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2373 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
2374 1a76625f 2018-10-22 stsp if (errcode)
2375 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2376 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2377 1a76625f 2018-10-22 stsp if (errcode)
2378 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2379 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2380 245d91c1 2018-07-12 stsp err = NULL;
2381 245d91c1 2018-07-12 stsp blame->thread = NULL;
2383 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2384 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2385 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2387 245d91c1 2018-07-12 stsp if (blame->f) {
2388 245d91c1 2018-07-12 stsp fclose(blame->f);
2389 245d91c1 2018-07-12 stsp blame->f = NULL;
2391 57670559 2018-12-23 stsp if (blame->lines) {
2392 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
2393 57670559 2018-12-23 stsp free(blame->lines[i].id);
2394 57670559 2018-12-23 stsp free(blame->lines);
2395 57670559 2018-12-23 stsp blame->lines = NULL;
2397 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2398 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2400 245d91c1 2018-07-12 stsp return err;
2403 245d91c1 2018-07-12 stsp static const struct got_error *
2404 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2405 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
2406 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
2407 245d91c1 2018-07-12 stsp struct got_repository *repo)
2409 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2410 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2411 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2412 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2413 15a94983 2018-12-23 stsp int obj_type;
2415 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2417 15a94983 2018-12-23 stsp return err;
2418 15a94983 2018-12-23 stsp if (obj_id == NULL)
2419 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
2421 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
2423 84451b3e 2018-07-10 stsp goto done;
2425 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2426 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2427 84451b3e 2018-07-10 stsp goto done;
2430 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2432 a70480e0 2018-06-23 stsp goto done;
2433 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2434 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2435 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2436 84451b3e 2018-07-10 stsp goto done;
2438 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2439 245d91c1 2018-07-12 stsp blame->f, blob);
2441 84451b3e 2018-07-10 stsp goto done;
2443 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2444 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2445 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2446 84451b3e 2018-07-10 stsp goto done;
2449 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2451 bd24772e 2018-07-11 stsp goto done;
2453 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2454 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2455 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2456 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2457 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2458 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2459 245d91c1 2018-07-12 stsp goto done;
2461 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2463 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2464 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2465 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2466 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2467 245d91c1 2018-07-12 stsp *blame_complete = 0;
2471 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2472 27d434c2 2018-09-15 stsp free(obj_id);
2474 245d91c1 2018-07-12 stsp stop_blame(blame);
2475 245d91c1 2018-07-12 stsp return err;
2478 245d91c1 2018-07-12 stsp static const struct got_error *
2479 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2480 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2482 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2483 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2485 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2487 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2489 7cbe629d 2018-08-04 stsp return err;
2491 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2492 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2493 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2494 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2495 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2496 fb2756b9 2018-08-04 stsp s->path = path;
2497 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2498 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2499 fb2756b9 2018-08-04 stsp s->repo = repo;
2500 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2501 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2503 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2504 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2505 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2507 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
2508 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2509 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2510 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2513 e5a0f69f 2018-08-18 stsp static const struct got_error *
2514 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2516 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2517 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2519 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2520 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2522 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2523 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2524 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2525 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2526 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2529 e5a0f69f 2018-08-18 stsp free(s->path);
2531 e5a0f69f 2018-08-18 stsp return err;
2534 7cbe629d 2018-08-04 stsp static const struct got_error *
2535 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2537 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2538 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2539 2b380cc8 2018-10-24 stsp int errcode;
2541 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
2542 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2543 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
2544 2b380cc8 2018-10-24 stsp if (errcode)
2545 2b380cc8 2018-10-24 stsp return got_error_set_errno(errcode);
2548 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2549 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2550 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2551 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2553 669b5ffa 2018-10-07 stsp view_vborder(view);
2554 e5a0f69f 2018-08-18 stsp return err;
2557 e5a0f69f 2018-08-18 stsp static const struct got_error *
2558 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2559 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2561 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2562 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2563 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2564 669b5ffa 2018-10-07 stsp int begin_x = 0;
2566 e5a0f69f 2018-08-18 stsp switch (ch) {
2568 e5a0f69f 2018-08-18 stsp s->done = 1;
2571 e5a0f69f 2018-08-18 stsp case KEY_UP:
2572 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2573 e5a0f69f 2018-08-18 stsp s->selected_line--;
2574 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2575 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2576 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2578 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2579 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2580 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2583 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2584 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2585 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2587 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2590 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2591 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2592 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2593 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2594 e5a0f69f 2018-08-18 stsp s->selected_line++;
2595 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2596 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2597 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2600 e5a0f69f 2018-08-18 stsp case 'p': {
2601 15a94983 2018-12-23 stsp struct got_object_id *id = NULL;
2602 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2603 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2604 15a94983 2018-12-23 stsp if (id == NULL)
2606 15a94983 2018-12-23 stsp if (ch == 'p') {
2607 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2608 15a94983 2018-12-23 stsp struct got_object_qid *pid;
2609 15a94983 2018-12-23 stsp struct got_object_id *blob_id = NULL;
2610 15a94983 2018-12-23 stsp int obj_type;
2611 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit,
2612 15a94983 2018-12-23 stsp s->repo, id);
2615 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
2616 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
2617 15a94983 2018-12-23 stsp if (pid == NULL) {
2618 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2621 15a94983 2018-12-23 stsp /* Check if path history ends here. */
2622 15a94983 2018-12-23 stsp err = got_object_id_by_path(&blob_id, s->repo,
2623 15a94983 2018-12-23 stsp pid->id, s->path);
2624 15a94983 2018-12-23 stsp if (err) {
2625 15a94983 2018-12-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
2626 15a94983 2018-12-23 stsp err = NULL;
2627 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2630 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo,
2632 15a94983 2018-12-23 stsp free(blob_id);
2633 15a94983 2018-12-23 stsp /* Can't blame non-blob type objects. */
2634 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2635 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2638 15a94983 2018-12-23 stsp err = got_object_qid_alloc(&s->blamed_commit,
2640 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2642 15a94983 2018-12-23 stsp if (got_object_id_cmp(id,
2643 15a94983 2018-12-23 stsp s->blamed_commit->id) == 0)
2645 15a94983 2018-12-23 stsp err = got_object_qid_alloc(&s->blamed_commit,
2650 e5a0f69f 2018-08-18 stsp s->done = 1;
2651 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2652 e5a0f69f 2018-08-18 stsp s->done = 0;
2653 e5a0f69f 2018-08-18 stsp if (thread_err)
2655 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2656 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2657 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2658 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2659 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2660 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2665 e5a0f69f 2018-08-18 stsp case 'B': {
2666 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2667 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2668 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2670 e5a0f69f 2018-08-18 stsp s->done = 1;
2671 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2672 e5a0f69f 2018-08-18 stsp s->done = 0;
2673 e5a0f69f 2018-08-18 stsp if (thread_err)
2675 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2676 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2677 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2678 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2679 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2680 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2681 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2682 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2687 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2688 15a94983 2018-12-23 stsp case '\r': {
2689 15a94983 2018-12-23 stsp struct got_object_id *id = NULL;
2690 15a94983 2018-12-23 stsp struct got_object_qid *pid;
2691 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2692 15a94983 2018-12-23 stsp id = get_selected_commit_id(s->blame.lines,
2693 15a94983 2018-12-23 stsp s->first_displayed_line, s->selected_line);
2694 15a94983 2018-12-23 stsp if (id == NULL || got_object_id_cmp(id,
2695 15a94983 2018-12-23 stsp s->blamed_commit->id) == 0)
2697 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, s->repo, id);
2700 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
2701 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
2702 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
2703 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
2704 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2705 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2706 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2707 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2710 15a94983 2018-12-23 stsp err = open_diff_view(diff_view, pid ? pid->id : NULL,
2711 15a94983 2018-12-23 stsp id, s->repo);
2712 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2713 e5a0f69f 2018-08-18 stsp if (err) {
2714 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2717 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
2718 669b5ffa 2018-10-07 stsp err = view_close_child(view);
2721 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
2722 669b5ffa 2018-10-07 stsp if (err) {
2723 669b5ffa 2018-10-07 stsp view_close(diff_view);
2726 51c68690 2018-11-07 stsp *focus_view = diff_view;
2727 51c68690 2018-11-07 stsp view->child_focussed = 1;
2729 669b5ffa 2018-10-07 stsp *new_view = diff_view;
2734 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2736 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2737 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2738 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2739 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2742 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2743 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2744 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2745 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2747 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2748 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2749 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2751 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2752 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2753 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2754 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2760 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2763 a70480e0 2018-06-23 stsp static const struct got_error *
2764 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2766 a70480e0 2018-06-23 stsp const struct got_error *error;
2767 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2768 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2769 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2770 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2772 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2774 a70480e0 2018-06-23 stsp #ifndef PROFILE
2775 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2777 a70480e0 2018-06-23 stsp err(1, "pledge");
2780 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2781 a70480e0 2018-06-23 stsp switch (ch) {
2783 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2786 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2787 69069811 2018-08-02 stsp if (repo_path == NULL)
2788 69069811 2018-08-02 stsp err(1, "-r option");
2792 a70480e0 2018-06-23 stsp /* NOTREACHED */
2796 a70480e0 2018-06-23 stsp argc -= optind;
2797 a70480e0 2018-06-23 stsp argv += optind;
2799 69069811 2018-08-02 stsp if (argc == 1)
2800 69069811 2018-08-02 stsp path = argv[0];
2802 a70480e0 2018-06-23 stsp usage_blame();
2804 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2805 69069811 2018-08-02 stsp if (cwd == NULL) {
2806 69069811 2018-08-02 stsp error = got_error_from_errno();
2807 69069811 2018-08-02 stsp goto done;
2809 69069811 2018-08-02 stsp if (repo_path == NULL) {
2810 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2811 69069811 2018-08-02 stsp if (repo_path == NULL) {
2812 69069811 2018-08-02 stsp error = got_error_from_errno();
2813 69069811 2018-08-02 stsp goto done;
2818 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2819 a70480e0 2018-06-23 stsp if (error != NULL)
2820 66b4983c 2018-06-23 stsp return error;
2822 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2823 69069811 2018-08-02 stsp if (error != NULL)
2824 69069811 2018-08-02 stsp goto done;
2826 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2827 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2828 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2829 a70480e0 2018-06-23 stsp if (error != NULL)
2830 66b4983c 2018-06-23 stsp goto done;
2831 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2832 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2834 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
2835 15a94983 2018-12-23 stsp commit_id_str);
2837 a19e88aa 2018-06-23 stsp if (error != NULL)
2838 a19e88aa 2018-06-23 stsp goto done;
2840 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2841 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2842 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2843 e1cd8fed 2018-08-01 stsp goto done;
2845 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2846 7cbe629d 2018-08-04 stsp if (error)
2847 7cbe629d 2018-08-04 stsp goto done;
2848 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2850 69069811 2018-08-02 stsp free(repo_path);
2851 69069811 2018-08-02 stsp free(cwd);
2852 a70480e0 2018-06-23 stsp free(commit_id);
2854 a70480e0 2018-06-23 stsp got_repo_close(repo);
2855 a70480e0 2018-06-23 stsp return error;
2858 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2859 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2860 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2861 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2862 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2863 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2864 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2866 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2867 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2868 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2869 ffd1d5e5 2018-06-23 stsp int width, n;
2871 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2873 f7d12f7e 2018-08-01 stsp werase(view->window);
2875 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2876 ffd1d5e5 2018-06-23 stsp return NULL;
2878 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2880 ffd1d5e5 2018-06-23 stsp return err;
2881 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2882 a3404814 2018-09-02 stsp wstandout(view->window);
2883 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2884 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2885 a3404814 2018-09-02 stsp wstandend(view->window);
2886 2550e4c3 2018-07-13 stsp free(wline);
2887 2550e4c3 2018-07-13 stsp wline = NULL;
2888 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2889 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2890 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2891 ffd1d5e5 2018-06-23 stsp return NULL;
2892 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2894 ce52c690 2018-06-23 stsp return err;
2895 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2896 2550e4c3 2018-07-13 stsp free(wline);
2897 2550e4c3 2018-07-13 stsp wline = NULL;
2898 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2899 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2900 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2901 ffd1d5e5 2018-06-23 stsp return NULL;
2902 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2903 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2904 a1eca9bb 2018-06-23 stsp return NULL;
2906 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2907 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2908 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2909 0cf4efb1 2018-09-29 stsp if (view->focussed)
2910 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2911 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2913 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2914 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
2915 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2916 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2917 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2918 ffd1d5e5 2018-06-23 stsp return NULL;
2922 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2923 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2926 ffd1d5e5 2018-06-23 stsp while (te) {
2927 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2929 1d13200f 2018-07-12 stsp if (show_ids) {
2930 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2932 1d13200f 2018-07-12 stsp return got_error_from_errno();
2934 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2935 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2936 1d13200f 2018-07-12 stsp free(id_str);
2937 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2939 1d13200f 2018-07-12 stsp free(id_str);
2940 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2941 ffd1d5e5 2018-06-23 stsp if (err) {
2942 ffd1d5e5 2018-06-23 stsp free(line);
2945 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2946 0cf4efb1 2018-09-29 stsp if (view->focussed)
2947 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2948 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2950 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2951 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2952 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2953 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
2954 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2955 ffd1d5e5 2018-06-23 stsp free(line);
2956 2550e4c3 2018-07-13 stsp free(wline);
2957 2550e4c3 2018-07-13 stsp wline = NULL;
2959 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2960 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2961 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2963 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2966 ffd1d5e5 2018-06-23 stsp return err;
2969 ffd1d5e5 2018-06-23 stsp static void
2970 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2971 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2973 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2976 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2979 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2980 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2981 ffd1d5e5 2018-06-23 stsp if (!isroot)
2982 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2986 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2987 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2988 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2989 ffd1d5e5 2018-06-23 stsp prev = te;
2990 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2992 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2993 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2995 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2996 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2999 ffd1d5e5 2018-06-23 stsp static void
3000 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3001 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
3002 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
3004 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
3005 ffd1d5e5 2018-06-23 stsp int n = 0;
3007 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
3010 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
3011 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3013 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
3014 ffd1d5e5 2018-06-23 stsp while (next) {
3015 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
3016 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
3018 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
3022 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3023 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
3024 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
3026 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
3027 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
3028 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
3030 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
3031 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
3033 ce52c690 2018-06-23 stsp len += strlen(te->name);
3035 ce52c690 2018-06-23 stsp *path = calloc(1, len);
3036 ffd1d5e5 2018-06-23 stsp if (path == NULL)
3037 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3039 ce52c690 2018-06-23 stsp (*path)[0] = '/';
3040 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
3041 d9765a41 2018-06-23 stsp while (pt) {
3042 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3043 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3044 cb2ebc8a 2018-06-23 stsp goto done;
3046 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
3047 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3048 cb2ebc8a 2018-06-23 stsp goto done;
3050 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3053 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3054 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3055 ce52c690 2018-06-23 stsp goto done;
3059 ce52c690 2018-06-23 stsp if (err) {
3060 ce52c690 2018-06-23 stsp free(*path);
3061 ce52c690 2018-06-23 stsp *path = NULL;
3063 ce52c690 2018-06-23 stsp return err;
3066 ce52c690 2018-06-23 stsp static const struct got_error *
3067 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3068 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3069 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
3071 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3072 ce52c690 2018-06-23 stsp char *path;
3073 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3075 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3077 ce52c690 2018-06-23 stsp return err;
3079 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3080 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3081 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3083 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
3084 e5a0f69f 2018-08-18 stsp if (err) {
3085 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3086 e5a0f69f 2018-08-18 stsp free(path);
3088 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3089 69efd4c4 2018-07-18 stsp return err;
3092 69efd4c4 2018-07-18 stsp static const struct got_error *
3093 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3094 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3095 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
3097 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3098 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3099 69efd4c4 2018-07-18 stsp char *path;
3101 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3102 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3103 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3105 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3107 69efd4c4 2018-07-18 stsp return err;
3109 23721109 2018-10-22 stsp err = open_log_view(log_view, commit_id, repo, path, 0);
3111 e5a0f69f 2018-08-18 stsp view_close(log_view);
3113 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3114 cb2ebc8a 2018-06-23 stsp free(path);
3115 cb2ebc8a 2018-06-23 stsp return err;
3118 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3119 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3120 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
3122 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3123 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3124 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3126 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3128 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3129 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3130 ffd1d5e5 2018-06-23 stsp goto done;
3132 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3133 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
3134 ffd1d5e5 2018-06-23 stsp goto done;
3137 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3138 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3139 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3140 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3141 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3142 6484ec90 2018-09-29 stsp err = got_error_from_errno();
3143 6484ec90 2018-09-29 stsp goto done;
3145 fb2756b9 2018-08-04 stsp s->repo = repo;
3147 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
3148 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
3149 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
3151 ad80ab7b 2018-08-04 stsp free(commit_id_str);
3152 6484ec90 2018-09-29 stsp if (err) {
3153 fb2756b9 2018-08-04 stsp free(s->tree_label);
3154 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3156 ad80ab7b 2018-08-04 stsp return err;
3159 e5a0f69f 2018-08-18 stsp static const struct got_error *
3160 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
3162 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3164 fb2756b9 2018-08-04 stsp free(s->tree_label);
3165 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3166 6484ec90 2018-09-29 stsp free(s->commit_id);
3167 6484ec90 2018-09-29 stsp s->commit_id = NULL;
3168 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
3169 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
3170 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
3171 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
3172 ad80ab7b 2018-08-04 stsp free(parent);
3175 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
3176 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
3177 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
3179 e5a0f69f 2018-08-18 stsp return NULL;
3182 ad80ab7b 2018-08-04 stsp static const struct got_error *
3183 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
3185 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
3186 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3187 e5a0f69f 2018-08-18 stsp char *parent_path;
3189 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
3191 e5a0f69f 2018-08-18 stsp return err;
3193 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
3194 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
3195 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3196 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
3197 e5a0f69f 2018-08-18 stsp free(parent_path);
3199 669b5ffa 2018-10-07 stsp view_vborder(view);
3200 e5a0f69f 2018-08-18 stsp return err;
3203 e5a0f69f 2018-08-18 stsp static const struct got_error *
3204 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3205 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3207 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3208 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
3209 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
3210 669b5ffa 2018-10-07 stsp int begin_x = 0;
3212 e5a0f69f 2018-08-18 stsp switch (ch) {
3214 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
3217 669b5ffa 2018-10-07 stsp if (!s->selected_entry)
3219 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
3220 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
3221 669b5ffa 2018-10-07 stsp err = log_tree_entry(&log_view, begin_x,
3222 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
3223 669b5ffa 2018-10-07 stsp s->commit_id, s->repo);
3224 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3225 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3227 669b5ffa 2018-10-07 stsp return err;
3228 669b5ffa 2018-10-07 stsp err = view_set_child(view, log_view);
3229 669b5ffa 2018-10-07 stsp if (err) {
3230 669b5ffa 2018-10-07 stsp view_close(log_view);
3233 669b5ffa 2018-10-07 stsp *focus_view = log_view;
3234 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3236 669b5ffa 2018-10-07 stsp *new_view = log_view;
3239 e5a0f69f 2018-08-18 stsp case KEY_UP:
3240 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3241 e5a0f69f 2018-08-18 stsp s->selected--;
3242 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3244 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
3245 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
3247 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
3248 8a0ead39 2018-11-03 stsp s->selected = 0;
3249 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
3250 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
3251 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
3252 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3255 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
3256 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
3257 e5a0f69f 2018-08-18 stsp s->tree == s->root);
3260 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
3261 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
3262 e5a0f69f 2018-08-18 stsp s->selected++;
3265 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
3266 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
3268 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
3269 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
3270 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
3271 e5a0f69f 2018-08-18 stsp s->entries);
3272 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
3275 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
3276 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
3277 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3279 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
3280 e5a0f69f 2018-08-18 stsp case '\r':
3281 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
3282 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3283 7837eeac 2018-09-24 stsp case KEY_BACKSPACE:
3284 e5a0f69f 2018-08-18 stsp /* user selected '..' */
3285 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
3287 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
3288 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
3290 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
3291 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
3292 e5a0f69f 2018-08-18 stsp s->entries =
3293 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3294 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
3295 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
3296 e5a0f69f 2018-08-18 stsp s->selected_entry =
3297 e5a0f69f 2018-08-18 stsp parent->selected_entry;
3298 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
3299 e5a0f69f 2018-08-18 stsp free(parent);
3300 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
3301 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3302 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
3303 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
3304 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
3307 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
3308 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
3309 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
3312 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
3313 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
3314 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
3315 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
3316 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
3317 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3318 e5a0f69f 2018-08-18 stsp s->tree = child;
3319 e5a0f69f 2018-08-18 stsp s->entries =
3320 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3321 e5a0f69f 2018-08-18 stsp s->selected = 0;
3322 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3323 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
3324 669b5ffa 2018-10-07 stsp struct tog_view *blame_view;
3325 669b5ffa 2018-10-07 stsp int begin_x = view_is_parent_view(view) ?
3326 669b5ffa 2018-10-07 stsp view_split_begin_x(view->begin_x) : 0;
3328 669b5ffa 2018-10-07 stsp err = blame_tree_entry(&blame_view, begin_x,
3329 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents, s->commit_id,
3333 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3334 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3336 669b5ffa 2018-10-07 stsp return err;
3337 669b5ffa 2018-10-07 stsp err = view_set_child(view, blame_view);
3338 669b5ffa 2018-10-07 stsp if (err) {
3339 669b5ffa 2018-10-07 stsp view_close(blame_view);
3342 669b5ffa 2018-10-07 stsp *focus_view = blame_view;
3343 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3345 669b5ffa 2018-10-07 stsp *new_view = blame_view;
3348 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3349 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3350 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3356 ffd1d5e5 2018-06-23 stsp return err;
3359 ffd1d5e5 2018-06-23 stsp __dead static void
3360 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3363 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3364 ffd1d5e5 2018-06-23 stsp getprogname());
3368 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3369 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3371 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3372 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3373 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3374 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3375 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3376 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3377 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3379 5221c383 2018-08-01 stsp struct tog_view *view;
3381 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3382 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3384 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3387 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3388 ffd1d5e5 2018-06-23 stsp switch (ch) {
3390 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3394 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3398 ffd1d5e5 2018-06-23 stsp argc -= optind;
3399 ffd1d5e5 2018-06-23 stsp argv += optind;
3401 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3402 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
3403 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3404 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3405 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3406 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3407 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3408 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3410 ffd1d5e5 2018-06-23 stsp usage_log();
3412 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3413 ffd1d5e5 2018-06-23 stsp free(repo_path);
3414 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3415 ffd1d5e5 2018-06-23 stsp return error;
3417 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
3418 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3420 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
3421 15a94983 2018-12-23 stsp commit_id_arg);
3422 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3423 ffd1d5e5 2018-06-23 stsp goto done;
3425 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3426 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3427 ffd1d5e5 2018-06-23 stsp goto done;
3429 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
3430 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
3431 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3432 ffd1d5e5 2018-06-23 stsp goto done;
3434 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3435 5221c383 2018-08-01 stsp if (view == NULL) {
3436 5221c383 2018-08-01 stsp error = got_error_from_errno();
3437 5221c383 2018-08-01 stsp goto done;
3439 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3440 ad80ab7b 2018-08-04 stsp if (error)
3441 ad80ab7b 2018-08-04 stsp goto done;
3442 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3444 ffd1d5e5 2018-06-23 stsp free(commit_id);
3445 ffd1d5e5 2018-06-23 stsp if (commit)
3446 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3448 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3450 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3451 ffd1d5e5 2018-06-23 stsp return error;
3454 5c5136c5 2018-05-20 stsp static void
3455 9f7d7167 2018-04-29 stsp init_curses(void)
3457 9f7d7167 2018-04-29 stsp initscr();
3459 fd823528 2018-10-22 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3462 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3463 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3464 1f475ad8 2018-05-10 stsp curs_set(0);
3465 25791caa 2018-10-24 stsp signal(SIGWINCH, tog_sigwinch);
3468 4ed7e80c 2018-05-20 stsp __dead static void
3469 9f7d7167 2018-04-29 stsp usage(void)
3473 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3474 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3475 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3476 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3477 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3482 c2301be8 2018-04-30 stsp static char **
3483 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3485 c2301be8 2018-04-30 stsp char **argv;
3486 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3488 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3489 c2301be8 2018-04-30 stsp if (argv == NULL)
3490 c2301be8 2018-04-30 stsp err(1, "calloc");
3491 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3492 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3493 c2301be8 2018-04-30 stsp err(1, "calloc");
3494 c2301be8 2018-04-30 stsp if (arg1) {
3495 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3496 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3497 c2301be8 2018-04-30 stsp err(1, "calloc");
3500 c2301be8 2018-04-30 stsp return argv;
3504 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3506 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3507 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3508 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3509 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3511 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
3513 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3514 9f7d7167 2018-04-29 stsp switch (ch) {
3516 9f7d7167 2018-04-29 stsp hflag = 1;
3520 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3524 9f7d7167 2018-04-29 stsp argc -= optind;
3525 9f7d7167 2018-04-29 stsp argv += optind;
3526 9f7d7167 2018-04-29 stsp optind = 0;
3527 c2301be8 2018-04-30 stsp optreset = 1;
3529 c2301be8 2018-04-30 stsp if (argc == 0) {
3530 f29d3e89 2018-06-23 stsp if (hflag)
3532 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3533 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3534 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3539 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3540 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3541 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3542 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3543 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3544 9f7d7167 2018-04-29 stsp if (hflag)
3545 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3549 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3550 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3551 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3552 c2301be8 2018-04-30 stsp if (repo_path) {
3553 c2301be8 2018-04-30 stsp struct got_repository *repo;
3554 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3555 c2301be8 2018-04-30 stsp if (error == NULL)
3556 c2301be8 2018-04-30 stsp got_repo_close(repo);
3558 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3559 c2301be8 2018-04-30 stsp if (error) {
3560 f29d3e89 2018-06-23 stsp if (hflag) {
3561 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3562 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3566 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3567 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3568 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3569 ad7de8d9 2018-04-30 stsp free(repo_path);
3572 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3573 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3575 c2301be8 2018-04-30 stsp free(repo_path);
3579 5c5136c5 2018-05-20 stsp init_curses();
3581 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3582 9f7d7167 2018-04-29 stsp if (error)
3583 9f7d7167 2018-04-29 stsp goto done;
3586 c2301be8 2018-04-30 stsp free(cmd_argv);
3587 9f7d7167 2018-04-29 stsp if (error)
3588 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);