2 9f7d7167 2018-04-29 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
20 31120ada 2018-04-30 stsp #include <errno.h>
21 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
22 9f7d7167 2018-04-29 stsp #include <curses.h>
23 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
24 9f7d7167 2018-04-29 stsp #include <panel.h>
25 9f7d7167 2018-04-29 stsp #include <locale.h>
26 9f7d7167 2018-04-29 stsp #include <stdlib.h>
27 26ed57b2 2018-05-19 stsp #include <stdio.h>
28 9f7d7167 2018-04-29 stsp #include <getopt.h>
29 9f7d7167 2018-04-29 stsp #include <string.h>
30 9f7d7167 2018-04-29 stsp #include <err.h>
31 80ddbec8 2018-04-29 stsp #include <unistd.h>
32 26ed57b2 2018-05-19 stsp #include <util.h>
33 26ed57b2 2018-05-19 stsp #include <limits.h>
34 61e69b96 2018-05-20 stsp #include <wchar.h>
35 788c352e 2018-06-16 stsp #include <time.h>
36 84451b3e 2018-07-10 stsp #include <pthread.h>
37 5036bf37 2018-09-24 stsp #include <libgen.h>
39 9f7d7167 2018-04-29 stsp #include "got_error.h"
40 80ddbec8 2018-04-29 stsp #include "got_object.h"
41 80ddbec8 2018-04-29 stsp #include "got_reference.h"
42 80ddbec8 2018-04-29 stsp #include "got_repository.h"
43 80ddbec8 2018-04-29 stsp #include "got_diff.h"
44 511a516b 2018-05-19 stsp #include "got_opentemp.h"
45 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
46 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
47 a70480e0 2018-06-23 stsp #include "got_blame.h"
50 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
58 9f7d7167 2018-04-29 stsp #ifndef nitems
59 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 9f7d7167 2018-04-29 stsp struct tog_cmd {
63 c2301be8 2018-04-30 stsp const char *name;
64 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
65 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
66 c2301be8 2018-04-30 stsp const char *descr;
69 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
70 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
71 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
72 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
73 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
75 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
76 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
77 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
78 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
80 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
81 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
82 9f7d7167 2018-04-29 stsp "show repository history" },
83 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
84 9f7d7167 2018-04-29 stsp "compare files and directories" },
85 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
86 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
87 ffd1d5e5 2018-06-23 stsp { "tree", cmd_tree, usage_tree,
88 ffd1d5e5 2018-06-23 stsp "browse trees in repository" },
91 d6b05b5b 2018-08-04 stsp enum tog_view_type {
92 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
93 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
94 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
95 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
98 ad80ab7b 2018-08-04 stsp struct tog_diff_view_state {
99 a3404814 2018-09-02 stsp struct got_object_id *id1, *id2;
101 ad80ab7b 2018-08-04 stsp int first_displayed_line;
102 ad80ab7b 2018-08-04 stsp int last_displayed_line;
104 48ae06ee 2018-10-18 stsp int diff_context;
105 48ae06ee 2018-10-18 stsp struct got_repository *repo;
108 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
109 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
110 ba4f502b 2018-08-04 stsp struct got_object_id *id;
111 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
114 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
115 ba4f502b 2018-08-04 stsp struct commit_queue {
116 ba4f502b 2018-08-04 stsp int ncommits;
117 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
120 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
122 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
123 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
124 1a76625f 2018-10-22 stsp int commits_needed;
125 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
126 1a76625f 2018-10-22 stsp struct commit_queue *commits;
127 1a76625f 2018-10-22 stsp const char *in_repo_path;
128 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
129 1a76625f 2018-10-22 stsp struct got_repository *repo;
130 1a76625f 2018-10-22 stsp int log_complete;
131 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
132 1a76625f 2018-10-22 stsp struct tog_view *view;
133 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
134 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
137 1a76625f 2018-10-22 stsp struct tog_log_view_state {
138 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
139 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
140 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
141 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
142 b01e7d3b 2018-08-04 stsp int selected;
143 b01e7d3b 2018-08-04 stsp char *in_repo_path;
144 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
145 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
146 1a76625f 2018-10-22 stsp sig_atomic_t quit;
147 1a76625f 2018-10-22 stsp pthread_t thread;
148 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
151 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
152 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
153 e9424729 2018-08-04 stsp int nlines;
155 e9424729 2018-08-04 stsp struct tog_view *view;
156 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
160 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
161 e9424729 2018-08-04 stsp const char *path;
162 e9424729 2018-08-04 stsp struct got_repository *repo;
163 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
164 e9424729 2018-08-04 stsp int *complete;
167 e9424729 2018-08-04 stsp struct tog_blame {
169 e9424729 2018-08-04 stsp size_t filesize;
170 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
171 e9424729 2018-08-04 stsp size_t nlines;
172 e9424729 2018-08-04 stsp pthread_t thread;
173 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
174 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
175 e9424729 2018-08-04 stsp const char *path;
178 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
179 7cbe629d 2018-08-04 stsp int first_displayed_line;
180 7cbe629d 2018-08-04 stsp int last_displayed_line;
181 7cbe629d 2018-08-04 stsp int selected_line;
182 7cbe629d 2018-08-04 stsp int blame_complete;
185 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
186 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
187 e5a0f69f 2018-08-18 stsp char *path;
188 7cbe629d 2018-08-04 stsp struct got_repository *repo;
189 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
190 e9424729 2018-08-04 stsp struct tog_blame blame;
193 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
194 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
195 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
196 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
197 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
198 ad80ab7b 2018-08-04 stsp int selected;
201 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
203 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
204 ad80ab7b 2018-08-04 stsp char *tree_label;
205 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
206 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
207 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
208 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
209 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
210 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
211 ad80ab7b 2018-08-04 stsp int nentries, ndisplayed, selected, show_ids;
212 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
213 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
214 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
218 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
220 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
221 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
222 669b5ffa 2018-10-07 stsp * there is enough screen estate.
224 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
225 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
227 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
228 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
229 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
231 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
232 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
234 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
236 cc3c9aac 2018-08-01 stsp struct tog_view {
237 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
238 26ed57b2 2018-05-19 stsp WINDOW *window;
239 26ed57b2 2018-05-19 stsp PANEL *panel;
240 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
241 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
242 1004088d 2018-09-29 stsp int focussed;
243 669b5ffa 2018-10-07 stsp struct tog_view *parent;
244 669b5ffa 2018-10-07 stsp struct tog_view *child;
245 669b5ffa 2018-10-07 stsp int child_focussed;
247 5dc9f4bc 2018-08-04 stsp /* type-specific state */
248 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
250 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
251 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
252 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
253 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
256 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
257 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
258 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
259 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
262 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
263 ba4f502b 2018-08-04 stsp struct got_object *, struct got_object *, struct got_repository *);
264 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
265 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
266 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
267 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
269 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
270 23721109 2018-10-22 stsp struct got_object_id *, struct got_repository *, const char *, int);
271 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
272 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
273 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
274 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
276 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
277 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
278 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
279 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
280 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
281 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
283 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
284 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
285 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
286 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
287 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
288 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
290 e5a0f69f 2018-08-18 stsp static const struct got_error *
291 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
293 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
295 669b5ffa 2018-10-07 stsp if (view->child) {
296 669b5ffa 2018-10-07 stsp view_close(view->child);
297 669b5ffa 2018-10-07 stsp view->child = NULL;
299 e5a0f69f 2018-08-18 stsp if (view->close)
300 e5a0f69f 2018-08-18 stsp err = view->close(view);
301 ea5e7bb5 2018-08-01 stsp if (view->panel)
302 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
303 ea5e7bb5 2018-08-01 stsp if (view->window)
304 ea5e7bb5 2018-08-01 stsp delwin(view->window);
305 ea5e7bb5 2018-08-01 stsp free(view);
306 e5a0f69f 2018-08-18 stsp return err;
309 ea5e7bb5 2018-08-01 stsp static struct tog_view *
310 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
311 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
313 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
315 ea5e7bb5 2018-08-01 stsp if (view == NULL)
316 ea5e7bb5 2018-08-01 stsp return NULL;
318 d6b05b5b 2018-08-04 stsp view->type = type;
319 f7d12f7e 2018-08-01 stsp view->lines = LINES;
320 f7d12f7e 2018-08-01 stsp view->cols = COLS;
321 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
322 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
323 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
324 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
325 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
326 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
327 96a765a8 2018-08-04 stsp view_close(view);
328 ea5e7bb5 2018-08-01 stsp return NULL;
330 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
331 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
332 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
333 96a765a8 2018-08-04 stsp view_close(view);
334 ea5e7bb5 2018-08-01 stsp return NULL;
337 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
338 ea5e7bb5 2018-08-01 stsp return view;
342 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
344 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
346 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
349 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
351 5c60c32a 2018-10-18 stsp static const struct got_error *
352 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
354 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
356 5c60c32a 2018-10-18 stsp view->begin_y = 0;
357 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
358 5c60c32a 2018-10-18 stsp view->nlines = LINES;
359 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
360 5c60c32a 2018-10-18 stsp view->lines = LINES;
361 5c60c32a 2018-10-18 stsp view->cols = COLS;
362 5c60c32a 2018-10-18 stsp err = view_resize(view);
364 5c60c32a 2018-10-18 stsp return err;
366 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
367 5c60c32a 2018-10-18 stsp return got_error_from_errno();
369 5c60c32a 2018-10-18 stsp return NULL;
372 5c60c32a 2018-10-18 stsp static const struct got_error *
373 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
375 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
377 5c60c32a 2018-10-18 stsp view->begin_x = 0;
378 5c60c32a 2018-10-18 stsp view->begin_y = 0;
379 5c60c32a 2018-10-18 stsp view->nlines = LINES;
380 5c60c32a 2018-10-18 stsp view->ncols = COLS;
381 5c60c32a 2018-10-18 stsp view->lines = LINES;
382 5c60c32a 2018-10-18 stsp view->cols = COLS;
383 5c60c32a 2018-10-18 stsp err = view_resize(view);
385 5c60c32a 2018-10-18 stsp return err;
387 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
388 5c60c32a 2018-10-18 stsp return got_error_from_errno();
390 5c60c32a 2018-10-18 stsp return NULL;
394 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
396 5c60c32a 2018-10-18 stsp return view->parent == NULL;
399 4d8c2215 2018-08-19 stsp static const struct got_error *
400 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
402 f7d12f7e 2018-08-01 stsp int nlines, ncols;
404 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
405 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
407 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
409 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
410 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
412 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
414 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
415 0cf4efb1 2018-09-29 stsp return got_error_from_errno();
416 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
417 a6d7eb8d 2018-10-24 stsp return got_error_from_errno();
419 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
420 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
421 0cf4efb1 2018-09-29 stsp view->lines = LINES;
422 0cf4efb1 2018-09-29 stsp view->cols = COLS;
424 6e3e5d9c 2018-10-18 stsp if (view->child) {
425 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
426 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
427 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
428 5c60c32a 2018-10-18 stsp if (view->child->focussed)
429 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
431 5c60c32a 2018-10-18 stsp show_panel(view->panel);
433 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
434 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
438 5c60c32a 2018-10-18 stsp return NULL;
441 669b5ffa 2018-10-07 stsp static const struct got_error *
442 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
444 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
446 669b5ffa 2018-10-07 stsp if (view->child == NULL)
447 669b5ffa 2018-10-07 stsp return NULL;
449 669b5ffa 2018-10-07 stsp err = view_close(view->child);
450 669b5ffa 2018-10-07 stsp view->child = NULL;
451 669b5ffa 2018-10-07 stsp return err;
454 669b5ffa 2018-10-07 stsp static const struct got_error *
455 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
457 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
459 669b5ffa 2018-10-07 stsp view->child = child;
460 669b5ffa 2018-10-07 stsp child->parent = view;
461 669b5ffa 2018-10-07 stsp return err;
465 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
467 669b5ffa 2018-10-07 stsp return !view_is_parent_view(view) && view->begin_x > 0;
470 0cf4efb1 2018-09-29 stsp static const struct got_error *
471 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
472 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
473 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
475 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
476 669b5ffa 2018-10-07 stsp struct tog_view *v;
477 1a76625f 2018-10-22 stsp int ch, errcode;
479 e5a0f69f 2018-08-18 stsp *new = NULL;
480 e5a0f69f 2018-08-18 stsp *dead = NULL;
481 0cf4efb1 2018-09-29 stsp *focus = NULL;
483 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
484 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
485 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
486 1a76625f 2018-10-22 stsp if (errcode)
487 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
488 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
489 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
490 1a76625f 2018-10-22 stsp if (errcode)
491 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
492 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
493 e5a0f69f 2018-08-18 stsp switch (ch) {
497 669b5ffa 2018-10-07 stsp if (view->child) {
498 669b5ffa 2018-10-07 stsp *focus = view->child;
499 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
500 669b5ffa 2018-10-07 stsp } else if (view->parent) {
501 669b5ffa 2018-10-07 stsp *focus = view->parent;
502 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 0;
506 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
507 e5a0f69f 2018-08-18 stsp *dead = view;
513 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
514 669b5ffa 2018-10-07 stsp if (view->child == NULL)
516 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view->child)) {
517 669b5ffa 2018-10-07 stsp *focus = view->child;
518 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
519 669b5ffa 2018-10-07 stsp err = view_fullscreen(view->child);
521 669b5ffa 2018-10-07 stsp err = view_splitscreen(view->child);
524 669b5ffa 2018-10-07 stsp err = view->child->input(new, dead, focus,
525 669b5ffa 2018-10-07 stsp view->child, KEY_RESIZE);
527 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view)) {
528 669b5ffa 2018-10-07 stsp *focus = view;
529 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 1;
530 669b5ffa 2018-10-07 stsp err = view_fullscreen(view);
532 669b5ffa 2018-10-07 stsp err = view_splitscreen(view);
536 669b5ffa 2018-10-07 stsp err = view->input(new, dead, focus, view,
537 669b5ffa 2018-10-07 stsp KEY_RESIZE);
540 0cf4efb1 2018-09-29 stsp case KEY_RESIZE:
541 0cf4efb1 2018-09-29 stsp TAILQ_FOREACH(v, views, entry) {
542 0cf4efb1 2018-09-29 stsp err = view_resize(v);
544 0cf4efb1 2018-09-29 stsp return err;
545 878940b7 2018-09-29 stsp err = v->input(new, dead, focus, v, ch);
547 876c7f55 2018-10-22 stsp return err;
551 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
555 e5a0f69f 2018-08-18 stsp return err;
559 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
561 0cf4efb1 2018-09-29 stsp PANEL *panel;
562 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
564 669b5ffa 2018-10-07 stsp if (view->parent)
565 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
567 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
568 0cf4efb1 2018-09-29 stsp if (panel == NULL)
571 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
572 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
573 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
577 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
579 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
580 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
582 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
584 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
587 669b5ffa 2018-10-07 stsp return view->focussed;
590 bcbd79e2 2018-08-19 stsp static const struct got_error *
591 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
593 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
594 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
595 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
596 fd823528 2018-10-22 stsp int fast_refresh = 10;
597 1a76625f 2018-10-22 stsp int done = 0, errcode;
599 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
600 1a76625f 2018-10-22 stsp if (errcode)
601 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
603 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
604 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
606 a81bf10d 2018-09-29 stsp main_view = view;
607 1004088d 2018-09-29 stsp view->focussed = 1;
608 878940b7 2018-09-29 stsp err = view->show(view);
610 0cf4efb1 2018-09-29 stsp return err;
611 0cf4efb1 2018-09-29 stsp update_panels();
612 0cf4efb1 2018-09-29 stsp doupdate();
613 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
614 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
615 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
616 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
618 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
619 e4197bf9 2018-08-18 stsp view, &views);
622 e5a0f69f 2018-08-18 stsp if (dead_view) {
623 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
625 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
626 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
627 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
629 669b5ffa 2018-10-07 stsp prev = view->parent;
631 669b5ffa 2018-10-07 stsp if (dead_view->parent)
632 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
634 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
636 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
637 a81bf10d 2018-09-29 stsp if (err || dead_view == main_view)
640 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
641 0cf4efb1 2018-09-29 stsp if (focus_view)
642 0cf4efb1 2018-09-29 stsp view = focus_view;
643 669b5ffa 2018-10-07 stsp else if (prev)
644 669b5ffa 2018-10-07 stsp view = prev;
645 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
646 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
647 0cf4efb1 2018-09-29 stsp tog_view_list_head);
649 0cf4efb1 2018-09-29 stsp view = NULL;
650 669b5ffa 2018-10-07 stsp if (view) {
651 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
652 669b5ffa 2018-10-07 stsp focus_view = view->child;
654 669b5ffa 2018-10-07 stsp focus_view = view;
658 bcbd79e2 2018-08-19 stsp if (new_view) {
659 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
660 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
661 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
662 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
664 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
665 86c66b02 2018-10-18 stsp err = view_close(v);
668 86c66b02 2018-10-18 stsp if (v == view)
669 86c66b02 2018-10-18 stsp view = new_view;
672 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
673 878940b7 2018-09-29 stsp if (focus_view == NULL)
674 878940b7 2018-09-29 stsp focus_view = new_view;
676 0cf4efb1 2018-09-29 stsp if (focus_view) {
677 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
679 0cf4efb1 2018-09-29 stsp view->focussed = 0;
680 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
681 0cf4efb1 2018-09-29 stsp view = focus_view;
682 878940b7 2018-09-29 stsp if (new_view)
683 878940b7 2018-09-29 stsp show_panel(new_view->panel);
684 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
685 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
687 669b5ffa 2018-10-07 stsp if (view) {
688 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
689 758194b5 2018-10-24 stsp view->focussed = 1;
690 758194b5 2018-10-24 stsp show_panel(view->panel);
691 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
692 758194b5 2018-10-24 stsp show_panel(view->child->panel);
693 1a76625f 2018-10-22 stsp focus_view = view;
695 669b5ffa 2018-10-07 stsp if (view->parent) {
696 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
700 669b5ffa 2018-10-07 stsp err = view->show(view);
703 669b5ffa 2018-10-07 stsp if (view->child) {
704 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
708 1a76625f 2018-10-22 stsp update_panels();
709 1a76625f 2018-10-22 stsp doupdate();
713 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
714 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
715 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
716 e5a0f69f 2018-08-18 stsp view_close(view);
719 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
720 1a76625f 2018-10-22 stsp if (errcode)
721 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
723 e5a0f69f 2018-08-18 stsp return err;
726 4ed7e80c 2018-05-20 stsp __dead static void
727 9f7d7167 2018-04-29 stsp usage_log(void)
730 c70c5802 2018-08-01 stsp fprintf(stderr,
731 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
732 9f7d7167 2018-04-29 stsp getprogname());
736 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
737 80ddbec8 2018-04-29 stsp static const struct got_error *
738 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
740 00dfcb92 2018-06-11 stsp char *vis = NULL;
741 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
743 963b370f 2018-05-20 stsp *ws = NULL;
744 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
745 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
746 00dfcb92 2018-06-11 stsp int vislen;
747 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
748 00dfcb92 2018-06-11 stsp return got_error_from_errno();
750 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
751 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
753 00dfcb92 2018-06-11 stsp return err;
754 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
755 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
756 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
761 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
762 a7f50699 2018-06-11 stsp if (*ws == NULL) {
763 a7f50699 2018-06-11 stsp err = got_error_from_errno();
767 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
768 963b370f 2018-05-20 stsp err = got_error_from_errno();
773 963b370f 2018-05-20 stsp *ws = NULL;
776 963b370f 2018-05-20 stsp return err;
779 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
780 963b370f 2018-05-20 stsp static const struct got_error *
781 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
783 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
784 963b370f 2018-05-20 stsp int cols = 0;
785 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
786 963b370f 2018-05-20 stsp size_t wlen;
789 963b370f 2018-05-20 stsp *wlinep = NULL;
790 b700b5d6 2018-07-10 stsp *widthp = 0;
792 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
794 963b370f 2018-05-20 stsp return err;
797 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
798 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
799 963b370f 2018-05-20 stsp switch (width) {
805 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
806 b700b5d6 2018-07-10 stsp cols += width;
810 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
811 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
815 963b370f 2018-05-20 stsp err = got_error_from_errno();
819 963b370f 2018-05-20 stsp wline[i] = L'\0';
820 b700b5d6 2018-07-10 stsp if (widthp)
821 b700b5d6 2018-07-10 stsp *widthp = cols;
824 963b370f 2018-05-20 stsp free(wline);
826 963b370f 2018-05-20 stsp *wlinep = wline;
827 963b370f 2018-05-20 stsp return err;
830 963b370f 2018-05-20 stsp static const struct got_error *
831 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
832 2814baeb 2018-08-01 stsp struct got_object_id *id)
834 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
835 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
836 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
837 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
838 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
839 bb737323 2018-05-20 stsp int author_width, logmsg_width;
840 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
841 80ddbec8 2018-04-29 stsp char *line = NULL;
842 bb737323 2018-05-20 stsp int col, limit;
843 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
844 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
845 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
847 c70c5802 2018-08-01 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
848 c70c5802 2018-08-01 stsp &commit->tm_committer) >= sizeof(datebuf))
849 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
851 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
852 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
854 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
855 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
856 b39d25c7 2018-07-10 stsp col = limit + 1;
857 b39d25c7 2018-07-10 stsp if (col > avail)
860 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
861 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
862 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
865 6d9fbc00 2018-04-29 stsp author = author0;
866 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
867 6d9fbc00 2018-04-29 stsp if (smallerthan)
868 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
870 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
872 6d9fbc00 2018-04-29 stsp *at = '\0';
874 b39d25c7 2018-07-10 stsp limit = avail - col;
875 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
878 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
879 bb737323 2018-05-20 stsp col += author_width;
880 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
881 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
883 bb737323 2018-05-20 stsp author_width++;
885 9c2eaf34 2018-05-20 stsp if (col > avail)
888 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
889 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
890 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
893 bb737323 2018-05-20 stsp logmsg = logmsg0;
894 bb737323 2018-05-20 stsp while (*logmsg == '\n')
896 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
897 bb737323 2018-05-20 stsp if (newline)
898 bb737323 2018-05-20 stsp *newline = '\0';
899 bb737323 2018-05-20 stsp limit = avail - col;
900 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
903 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
904 bb737323 2018-05-20 stsp col += logmsg_width;
905 bb737323 2018-05-20 stsp while (col <= avail) {
906 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
910 80ddbec8 2018-04-29 stsp free(logmsg0);
911 bb737323 2018-05-20 stsp free(wlogmsg);
912 6d9fbc00 2018-04-29 stsp free(author0);
913 bb737323 2018-05-20 stsp free(wauthor);
914 80ddbec8 2018-04-29 stsp free(line);
915 80ddbec8 2018-04-29 stsp return err;
918 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
919 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
920 899d86c2 2018-05-10 stsp struct got_object_id *id)
922 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
924 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
925 80ddbec8 2018-04-29 stsp if (entry == NULL)
926 899d86c2 2018-05-10 stsp return NULL;
928 899d86c2 2018-05-10 stsp entry->id = id;
929 99db9666 2018-05-07 stsp entry->commit = commit;
930 899d86c2 2018-05-10 stsp return entry;
933 99db9666 2018-05-07 stsp static void
934 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
936 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
938 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
939 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
940 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
941 ecb28ae0 2018-07-16 stsp commits->ncommits--;
942 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
943 99db9666 2018-05-07 stsp free(entry);
946 99db9666 2018-05-07 stsp static void
947 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
949 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
950 99db9666 2018-05-07 stsp pop_commit(commits);
953 c4972b91 2018-05-07 stsp static const struct got_error *
954 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
955 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
957 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
958 93e45b7c 2018-09-24 stsp int nqueued = 0;
961 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
962 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
963 1a76625f 2018-10-22 stsp * while updating the display.
965 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
966 93e45b7c 2018-09-24 stsp struct got_object_id *id;
967 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
968 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
969 1a76625f 2018-10-22 stsp int errcode;
971 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
973 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
975 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
976 93e45b7c 2018-09-24 stsp minqueue, repo);
978 ecb28ae0 2018-07-16 stsp return err;
982 ecb28ae0 2018-07-16 stsp if (id == NULL)
985 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
988 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
989 9ba79e04 2018-06-11 stsp if (entry == NULL) {
990 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
994 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
995 1a76625f 2018-10-22 stsp if (errcode) {
996 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1000 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1001 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1002 ecb28ae0 2018-07-16 stsp nqueued++;
1003 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1005 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1006 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1007 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1010 9ba79e04 2018-06-11 stsp return err;
1013 99db9666 2018-05-07 stsp static const struct got_error *
1014 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1016 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1017 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1019 9ba79e04 2018-06-11 stsp *head_id = NULL;
1021 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1023 99db9666 2018-05-07 stsp return err;
1025 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1026 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1027 9ba79e04 2018-06-11 stsp if (err) {
1028 9ba79e04 2018-06-11 stsp *head_id = NULL;
1029 99db9666 2018-05-07 stsp return err;
1032 9ba79e04 2018-06-11 stsp return NULL;
1035 0553a4e3 2018-04-30 stsp static const struct got_error *
1036 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1037 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1038 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1039 1a76625f 2018-10-22 stsp const char *path, int commits_needed)
1041 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1042 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1043 ecb28ae0 2018-07-16 stsp int ncommits, width;
1044 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1045 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1047 e0d42f60 2018-07-22 stsp entry = first;
1048 e0d42f60 2018-07-22 stsp ncommits = 0;
1049 e0d42f60 2018-07-22 stsp while (entry) {
1050 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1051 e0d42f60 2018-07-22 stsp *selected = entry;
1054 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1055 e0d42f60 2018-07-22 stsp ncommits++;
1058 1a76625f 2018-10-22 stsp if (*selected) {
1059 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1061 ecb28ae0 2018-07-16 stsp return err;
1064 1a76625f 2018-10-22 stsp if (asprintf(&ncommits_str, " [%d/%d]%s ",
1065 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1066 1a76625f 2018-10-22 stsp commits_needed == 0 ? "" : " loading...") == -1)
1067 1a76625f 2018-10-22 stsp return got_error_from_errno();
1069 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1070 1a76625f 2018-10-22 stsp if (asprintf(&header, "commit: %s %s%s",
1071 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1072 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1073 1a76625f 2018-10-22 stsp err = got_error_from_errno();
1074 1a76625f 2018-10-22 stsp header = NULL;
1075 1a76625f 2018-10-22 stsp goto done;
1077 1a76625f 2018-10-22 stsp } else if (asprintf(&header, "commit: %s%s",
1078 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1079 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1080 1a76625f 2018-10-22 stsp err = got_error_from_errno();
1081 1a76625f 2018-10-22 stsp header = NULL;
1082 1a76625f 2018-10-22 stsp goto done;
1084 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1086 1a76625f 2018-10-22 stsp goto done;
1088 2814baeb 2018-08-01 stsp werase(view->window);
1090 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1091 a3404814 2018-09-02 stsp wstandout(view->window);
1092 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1093 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1094 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1097 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1098 a3404814 2018-09-02 stsp wstandend(view->window);
1099 ecb28ae0 2018-07-16 stsp free(wline);
1100 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1101 1a76625f 2018-10-22 stsp goto done;
1103 899d86c2 2018-05-10 stsp entry = first;
1104 899d86c2 2018-05-10 stsp *last = first;
1105 867c6645 2018-07-10 stsp ncommits = 0;
1106 899d86c2 2018-05-10 stsp while (entry) {
1107 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1109 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1110 2814baeb 2018-08-01 stsp wstandout(view->window);
1111 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
1112 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1113 2814baeb 2018-08-01 stsp wstandend(view->window);
1116 0553a4e3 2018-04-30 stsp ncommits++;
1117 899d86c2 2018-05-10 stsp *last = entry;
1118 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1121 1a57306a 2018-09-02 stsp view_vborder(view);
1123 1a76625f 2018-10-22 stsp free(id_str);
1124 1a76625f 2018-10-22 stsp free(ncommits_str);
1125 1a76625f 2018-10-22 stsp free(header);
1126 80ddbec8 2018-04-29 stsp return err;
1129 07b55e75 2018-05-10 stsp static void
1130 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1131 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1133 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1134 07b55e75 2018-05-10 stsp int nscrolled = 0;
1136 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1137 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
1140 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1141 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1142 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1143 07b55e75 2018-05-10 stsp if (entry) {
1144 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1145 07b55e75 2018-05-10 stsp nscrolled++;
1150 aa075928 2018-05-10 stsp static const struct got_error *
1151 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1152 93e45b7c 2018-09-24 stsp struct commit_queue_entry **last_displayed_entry,
1153 1a76625f 2018-10-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1154 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1156 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
1157 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
1158 aa075928 2018-05-10 stsp int nscrolled = 0;
1160 1a76625f 2018-10-22 stsp if (*last_displayed_entry == NULL)
1161 1a76625f 2018-10-22 stsp return NULL;
1164 93e45b7c 2018-09-24 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1165 aa075928 2018-05-10 stsp if (pentry == NULL) {
1166 1a76625f 2018-10-22 stsp int errcode;
1167 1a76625f 2018-10-22 stsp if (*log_complete)
1168 1a76625f 2018-10-22 stsp return NULL;
1169 1a76625f 2018-10-22 stsp *commits_needed = maxscroll + 20;
1170 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(need_commits);
1171 1a76625f 2018-10-22 stsp if (errcode)
1172 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1173 1a76625f 2018-10-22 stsp return NULL;
1175 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1177 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1178 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1180 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1181 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1183 dd0a52c1 2018-05-20 stsp return err;
1186 cd0acaa7 2018-05-20 stsp static const struct got_error *
1187 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1188 0cf4efb1 2018-09-29 stsp struct got_object_id *commit_id, struct got_commit_object *commit,
1189 2a4718d3 2018-09-29 stsp struct got_repository *repo)
1191 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1192 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1193 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1194 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1196 2a4718d3 2018-09-29 stsp err = got_object_open(&obj2, repo, commit_id);
1198 cd0acaa7 2018-05-20 stsp return err;
1200 2a4718d3 2018-09-29 stsp parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1201 9ba79e04 2018-06-11 stsp if (parent_id) {
1202 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
1204 cd0acaa7 2018-05-20 stsp goto done;
1207 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1208 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
1209 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1210 ea5e7bb5 2018-08-01 stsp goto done;
1213 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, obj1, obj2, repo);
1214 e5a0f69f 2018-08-18 stsp if (err == NULL)
1215 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1218 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
1220 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
1221 cd0acaa7 2018-05-20 stsp return err;
1224 80ddbec8 2018-04-29 stsp static const struct got_error *
1225 0cf4efb1 2018-09-29 stsp browse_commit(struct tog_view **new_view, int begin_x,
1226 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1228 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1229 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1230 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1232 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1234 9343a5fb 2018-06-23 stsp return err;
1236 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1237 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1238 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1240 e5a0f69f 2018-08-18 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1242 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1244 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1245 1a76625f 2018-10-22 stsp return err;
1248 1a76625f 2018-10-22 stsp static void *
1249 1a76625f 2018-10-22 stsp log_thread(void *arg)
1251 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1252 1a76625f 2018-10-22 stsp int errcode = 0;
1253 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1254 1a76625f 2018-10-22 stsp int done = 0;
1256 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1258 1a76625f 2018-10-22 stsp return (void *)err;
1260 1a76625f 2018-10-22 stsp while (!done && !err) {
1261 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1262 1a76625f 2018-10-22 stsp a->in_repo_path);
1263 1a76625f 2018-10-22 stsp if (err) {
1264 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1265 1a76625f 2018-10-22 stsp return (void *)err;
1266 1a76625f 2018-10-22 stsp err = NULL;
1268 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1269 1a76625f 2018-10-22 stsp a->commits_needed--;
1271 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1272 1a76625f 2018-10-22 stsp if (errcode)
1273 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
1276 1a76625f 2018-10-22 stsp a->log_complete = 1;
1277 1a76625f 2018-10-22 stsp else if (*a->quit) {
1279 1a76625f 2018-10-22 stsp a->log_complete = 1;
1280 1a76625f 2018-10-22 stsp } else if (*a->first_displayed_entry == NULL) {
1281 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1282 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1283 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1287 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1288 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1289 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1290 1a76625f 2018-10-22 stsp &tog_mutex);
1291 1a76625f 2018-10-22 stsp if (errcode)
1292 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1295 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1296 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1297 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1299 1a76625f 2018-10-22 stsp return (void *)err;
1302 1a76625f 2018-10-22 stsp static const struct got_error *
1303 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1305 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1306 1a76625f 2018-10-22 stsp int errcode;
1308 1a76625f 2018-10-22 stsp if (s->thread) {
1309 1a76625f 2018-10-22 stsp s->quit = 1;
1310 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1311 1a76625f 2018-10-22 stsp if (errcode)
1312 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1313 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1314 1a76625f 2018-10-22 stsp if (errcode)
1315 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1316 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1317 1a76625f 2018-10-22 stsp if (errcode)
1318 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1319 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1320 1a76625f 2018-10-22 stsp if (errcode)
1321 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1322 1a76625f 2018-10-22 stsp s->thread = NULL;
1325 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1326 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1327 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1329 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1330 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1331 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1334 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1335 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1336 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1339 9343a5fb 2018-06-23 stsp return err;
1342 9343a5fb 2018-06-23 stsp static const struct got_error *
1343 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1345 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1346 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1348 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1349 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1350 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1351 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1352 1a76625f 2018-10-22 stsp free(s->start_id);
1353 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1354 1a76625f 2018-10-22 stsp return err;
1357 1a76625f 2018-10-22 stsp static const struct got_error *
1358 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1359 23721109 2018-10-22 stsp struct got_repository *repo, const char *path, int check_disk)
1361 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1362 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1363 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1364 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1365 1a76625f 2018-10-22 stsp int errcode;
1367 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1368 ecb28ae0 2018-07-16 stsp if (err != NULL)
1369 ecb28ae0 2018-07-16 stsp goto done;
1371 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1372 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1373 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1375 fb2756b9 2018-08-04 stsp s->repo = repo;
1376 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1377 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1378 5036bf37 2018-09-24 stsp err = got_error_from_errno();
1379 5036bf37 2018-09-24 stsp goto done;
1382 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1383 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1384 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1386 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1388 1a76625f 2018-10-22 stsp goto done;
1389 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1390 1a76625f 2018-10-22 stsp 0, thread_repo);
1392 1a76625f 2018-10-22 stsp goto done;
1394 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1395 1a76625f 2018-10-22 stsp if (errcode) {
1396 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1397 1a76625f 2018-10-22 stsp goto done;
1400 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1401 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1402 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1403 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1404 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1405 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1406 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1407 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1408 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1409 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1410 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1412 1a76625f 2018-10-22 stsp errcode = pthread_create(&s->thread, NULL, log_thread,
1413 1a76625f 2018-10-22 stsp &s->thread_args);
1414 1a76625f 2018-10-22 stsp if (errcode) {
1415 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1416 1a76625f 2018-10-22 stsp goto done;
1421 1a76625f 2018-10-22 stsp close_log_view(view);
1422 ba4f502b 2018-08-04 stsp return err;
1425 e5a0f69f 2018-08-18 stsp static const struct got_error *
1426 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1428 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1430 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1431 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1432 1a76625f 2018-10-22 stsp &s->commits, s->selected, view->nlines,
1433 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1436 e5a0f69f 2018-08-18 stsp static const struct got_error *
1437 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1438 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1440 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1441 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1442 5036bf37 2018-09-24 stsp char *parent_path;
1443 669b5ffa 2018-10-07 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
1444 669b5ffa 2018-10-07 stsp int begin_x = 0;
1446 e5a0f69f 2018-08-18 stsp switch (ch) {
1448 1a76625f 2018-10-22 stsp s->quit = 1;
1451 e5a0f69f 2018-08-18 stsp case KEY_UP:
1452 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1453 e5a0f69f 2018-08-18 stsp s->selected--;
1454 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1456 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1457 e5a0f69f 2018-08-18 stsp &s->commits);
1459 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1460 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1461 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1462 e5a0f69f 2018-08-18 stsp s->selected = 0;
1465 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1466 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1469 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1470 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1471 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1472 e5a0f69f 2018-08-18 stsp s->selected++;
1475 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1476 93e45b7c 2018-09-24 stsp &s->last_displayed_entry, &s->commits,
1477 1a76625f 2018-10-22 stsp &s->thread_args.log_complete,
1478 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1479 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1481 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1482 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1483 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1484 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1485 93e45b7c 2018-09-24 stsp view->nlines, &s->last_displayed_entry,
1486 1a76625f 2018-10-22 stsp &s->commits, &s->thread_args.log_complete,
1487 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1488 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1489 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1490 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1491 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1492 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1493 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1494 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1496 e5a0f69f 2018-08-18 stsp err = NULL;
1499 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1500 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1501 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1502 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1503 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1505 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1506 e5a0f69f 2018-08-18 stsp case '\r':
1507 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1508 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1509 669b5ffa 2018-10-07 stsp err = open_diff_view_for_commit(&diff_view, begin_x,
1510 2a4718d3 2018-09-29 stsp s->selected_entry->id, s->selected_entry->commit,
1514 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1515 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1517 669b5ffa 2018-10-07 stsp return err;
1518 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
1519 669b5ffa 2018-10-07 stsp if (err) {
1520 669b5ffa 2018-10-07 stsp view_close(diff_view);
1523 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(diff_view)) {
1524 669b5ffa 2018-10-07 stsp *focus_view = diff_view;
1525 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1528 669b5ffa 2018-10-07 stsp *new_view = diff_view;
1531 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1532 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1533 669b5ffa 2018-10-07 stsp err = browse_commit(&tree_view, begin_x,
1534 0cf4efb1 2018-09-29 stsp s->selected_entry, s->repo);
1535 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1536 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1538 669b5ffa 2018-10-07 stsp return err;
1539 669b5ffa 2018-10-07 stsp err = view_set_child(view, tree_view);
1540 669b5ffa 2018-10-07 stsp if (err) {
1541 669b5ffa 2018-10-07 stsp view_close(tree_view);
1544 669b5ffa 2018-10-07 stsp *focus_view = tree_view;
1545 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1547 669b5ffa 2018-10-07 stsp *new_view = tree_view;
1549 5036bf37 2018-09-24 stsp case KEY_BACKSPACE:
1550 5036bf37 2018-09-24 stsp if (strcmp(s->in_repo_path, "/") == 0)
1552 5036bf37 2018-09-24 stsp parent_path = dirname(s->in_repo_path);
1553 5036bf37 2018-09-24 stsp if (parent_path && strcmp(parent_path, ".") != 0) {
1554 5036bf37 2018-09-24 stsp struct tog_view *lv;
1555 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1557 1a76625f 2018-10-22 stsp return err;
1558 0cf4efb1 2018-09-29 stsp lv = view_open(view->nlines, view->ncols,
1559 0cf4efb1 2018-09-29 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
1560 5036bf37 2018-09-24 stsp if (lv == NULL)
1561 5036bf37 2018-09-24 stsp return got_error_from_errno();
1562 5036bf37 2018-09-24 stsp err = open_log_view(lv, s->start_id, s->repo,
1563 23721109 2018-10-22 stsp parent_path, 0);
1565 1a76625f 2018-10-22 stsp return err;;
1566 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1567 669b5ffa 2018-10-07 stsp *new_view = lv;
1569 669b5ffa 2018-10-07 stsp view_set_child(view->parent, lv);
1570 34095730 2018-10-22 stsp *focus_view = lv;
1572 1a76625f 2018-10-22 stsp return NULL;
1579 80ddbec8 2018-04-29 stsp return err;
1582 4ed7e80c 2018-05-20 stsp static const struct got_error *
1583 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1585 80ddbec8 2018-04-29 stsp const struct got_error *error;
1586 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1587 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1588 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1589 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1591 04cc582a 2018-08-01 stsp struct tog_view *view;
1593 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1594 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1596 80ddbec8 2018-04-29 stsp err(1, "pledge");
1599 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1600 80ddbec8 2018-04-29 stsp switch (ch) {
1602 80ddbec8 2018-04-29 stsp start_commit = optarg;
1605 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1606 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1607 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1611 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1615 80ddbec8 2018-04-29 stsp argc -= optind;
1616 80ddbec8 2018-04-29 stsp argv += optind;
1618 ecb28ae0 2018-07-16 stsp if (argc == 0)
1619 ecb28ae0 2018-07-16 stsp path = strdup("");
1620 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1621 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1623 80ddbec8 2018-04-29 stsp usage_log();
1624 ecb28ae0 2018-07-16 stsp if (path == NULL)
1625 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1627 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1628 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1629 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1630 ecb28ae0 2018-07-16 stsp goto done;
1632 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1633 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1634 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1635 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1636 ecb28ae0 2018-07-16 stsp goto done;
1640 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1641 80ddbec8 2018-04-29 stsp if (error != NULL)
1642 ecb28ae0 2018-07-16 stsp goto done;
1644 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1645 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1646 80ddbec8 2018-04-29 stsp if (error != NULL)
1647 ecb28ae0 2018-07-16 stsp goto done;
1649 80ddbec8 2018-04-29 stsp struct got_object *obj;
1650 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1651 80ddbec8 2018-04-29 stsp if (error == NULL) {
1652 6402fb3c 2018-09-15 stsp start_id = got_object_id_dup(got_object_get_id(obj));
1653 899d86c2 2018-05-10 stsp if (start_id == NULL)
1654 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1655 ecb28ae0 2018-07-16 stsp goto done;
1658 80ddbec8 2018-04-29 stsp if (error != NULL)
1659 ecb28ae0 2018-07-16 stsp goto done;
1661 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1662 04cc582a 2018-08-01 stsp if (view == NULL) {
1663 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1664 04cc582a 2018-08-01 stsp goto done;
1666 23721109 2018-10-22 stsp error = open_log_view(view, start_id, repo, path, 1);
1667 ba4f502b 2018-08-04 stsp if (error)
1668 ba4f502b 2018-08-04 stsp goto done;
1669 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1671 ecb28ae0 2018-07-16 stsp free(repo_path);
1672 ecb28ae0 2018-07-16 stsp free(cwd);
1673 ecb28ae0 2018-07-16 stsp free(path);
1674 899d86c2 2018-05-10 stsp free(start_id);
1676 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1677 80ddbec8 2018-04-29 stsp return error;
1680 4ed7e80c 2018-05-20 stsp __dead static void
1681 9f7d7167 2018-04-29 stsp usage_diff(void)
1684 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1685 9f7d7167 2018-04-29 stsp getprogname());
1689 b304db33 2018-05-20 stsp static char *
1690 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1692 b304db33 2018-05-20 stsp char *line;
1693 b304db33 2018-05-20 stsp size_t linelen;
1694 b304db33 2018-05-20 stsp size_t lineno;
1695 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1697 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1699 b304db33 2018-05-20 stsp *len = linelen;
1700 b304db33 2018-05-20 stsp return line;
1703 4ed7e80c 2018-05-20 stsp static const struct got_error *
1704 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1705 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1706 a3404814 2018-09-02 stsp char * header)
1708 61e69b96 2018-05-20 stsp const struct got_error *err;
1709 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1710 b304db33 2018-05-20 stsp char *line;
1711 b304db33 2018-05-20 stsp size_t len;
1712 61e69b96 2018-05-20 stsp wchar_t *wline;
1713 e0b650dd 2018-05-20 stsp int width;
1715 26ed57b2 2018-05-19 stsp rewind(f);
1716 f7d12f7e 2018-08-01 stsp werase(view->window);
1718 a3404814 2018-09-02 stsp if (header) {
1719 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1720 a3404814 2018-09-02 stsp if (err) {
1721 a3404814 2018-09-02 stsp return err;
1724 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1725 a3404814 2018-09-02 stsp wstandout(view->window);
1726 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1727 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1728 a3404814 2018-09-02 stsp wstandend(view->window);
1729 a3404814 2018-09-02 stsp if (width < view->ncols)
1730 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1732 a3404814 2018-09-02 stsp if (max_lines <= 1)
1733 a3404814 2018-09-02 stsp return NULL;
1734 a3404814 2018-09-02 stsp max_lines--;
1738 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1739 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1740 26ed57b2 2018-05-19 stsp if (line == NULL) {
1744 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1745 26ed57b2 2018-05-19 stsp free(line);
1749 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1750 61e69b96 2018-05-20 stsp if (err) {
1751 61e69b96 2018-05-20 stsp free(line);
1752 61e69b96 2018-05-20 stsp return err;
1754 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1755 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1756 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1757 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1758 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1759 26ed57b2 2018-05-19 stsp free(line);
1760 2550e4c3 2018-07-13 stsp free(wline);
1761 2550e4c3 2018-07-13 stsp wline = NULL;
1763 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1765 1a57306a 2018-09-02 stsp view_vborder(view);
1767 26ed57b2 2018-05-19 stsp return NULL;
1770 4ed7e80c 2018-05-20 stsp static const struct got_error *
1771 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
1773 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
1774 48ae06ee 2018-10-18 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1775 48ae06ee 2018-10-18 stsp FILE *f = NULL;
1777 48ae06ee 2018-10-18 stsp if (s->id1) {
1778 48ae06ee 2018-10-18 stsp err = got_object_open(&obj1, s->repo, s->id1);
1780 48ae06ee 2018-10-18 stsp return err;
1783 48ae06ee 2018-10-18 stsp err = got_object_open(&obj2, s->repo, s->id2);
1785 48ae06ee 2018-10-18 stsp goto done;
1787 511a516b 2018-05-19 stsp f = got_opentemp();
1788 48ae06ee 2018-10-18 stsp if (f == NULL) {
1789 48ae06ee 2018-10-18 stsp err = got_error_from_errno();
1790 48ae06ee 2018-10-18 stsp goto done;
1793 48ae06ee 2018-10-18 stsp fclose(s->f);
1796 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1797 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1798 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1799 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1801 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1802 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_trees(obj1, obj2, "", "",
1803 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1805 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1806 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1807 48ae06ee 2018-10-18 stsp s->repo, f);
1810 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1815 48ae06ee 2018-10-18 stsp got_object_close(obj1);
1816 48ae06ee 2018-10-18 stsp got_object_close(obj2);
1818 48ae06ee 2018-10-18 stsp fflush(f);
1819 48ae06ee 2018-10-18 stsp return err;
1822 48ae06ee 2018-10-18 stsp static const struct got_error *
1823 48ae06ee 2018-10-18 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1824 48ae06ee 2018-10-18 stsp struct got_object *obj2, struct got_repository *repo)
1826 48ae06ee 2018-10-18 stsp const struct got_error *err;
1828 48ae06ee 2018-10-18 stsp if (obj1 != NULL && obj2 != NULL &&
1829 48ae06ee 2018-10-18 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1830 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
1832 48ae06ee 2018-10-18 stsp if (obj1) {
1833 48ae06ee 2018-10-18 stsp struct got_object_id *id1;
1834 48ae06ee 2018-10-18 stsp id1 = got_object_id_dup(got_object_get_id(obj1));
1835 48ae06ee 2018-10-18 stsp if (id1 == NULL)
1836 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1837 48ae06ee 2018-10-18 stsp view->state.diff.id1 = id1;
1839 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1841 48ae06ee 2018-10-18 stsp view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1842 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
1843 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1844 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1845 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1847 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
1848 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1849 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1850 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
1851 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
1853 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
1854 48ae06ee 2018-10-18 stsp if (err) {
1855 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1856 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1857 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
1858 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
1859 48ae06ee 2018-10-18 stsp return err;
1862 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
1863 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
1864 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
1866 5dc9f4bc 2018-08-04 stsp return NULL;
1869 e5a0f69f 2018-08-18 stsp static const struct got_error *
1870 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1872 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1874 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1875 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1876 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
1877 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
1878 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1879 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1880 e5a0f69f 2018-08-18 stsp return err;
1883 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1884 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1886 a3404814 2018-09-02 stsp const struct got_error *err;
1887 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1888 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
1890 a3404814 2018-09-02 stsp if (s->id1) {
1891 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
1893 a3404814 2018-09-02 stsp return err;
1895 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
1897 a3404814 2018-09-02 stsp return err;
1899 a3404814 2018-09-02 stsp if (asprintf(&header, "diff: %s %s",
1900 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1901 a3404814 2018-09-02 stsp err = got_error_from_errno();
1902 a3404814 2018-09-02 stsp free(id_str1);
1903 a3404814 2018-09-02 stsp free(id_str2);
1904 a3404814 2018-09-02 stsp return err;
1906 a3404814 2018-09-02 stsp free(id_str1);
1907 a3404814 2018-09-02 stsp free(id_str2);
1909 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
1910 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
1914 0cf4efb1 2018-09-29 stsp static const struct got_error *
1915 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1916 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1918 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1919 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
1922 e5a0f69f 2018-08-18 stsp switch (ch) {
1924 e5a0f69f 2018-08-18 stsp case KEY_UP:
1925 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
1926 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1928 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1930 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
1931 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
1932 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1935 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1936 e5a0f69f 2018-08-18 stsp if (!s->eof)
1937 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1939 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
1942 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
1943 e5a0f69f 2018-08-18 stsp char *line;
1944 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
1945 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1946 e5a0f69f 2018-08-18 stsp if (line == NULL)
1951 48ae06ee 2018-10-18 stsp if (s->diff_context > 0) {
1952 48ae06ee 2018-10-18 stsp s->diff_context--;
1953 48ae06ee 2018-10-18 stsp err = create_diff(s);
1957 4a8520aa 2018-10-18 stsp if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
1958 48ae06ee 2018-10-18 stsp s->diff_context++;
1959 48ae06ee 2018-10-18 stsp err = create_diff(s);
1966 bcbd79e2 2018-08-19 stsp return err;
1969 4ed7e80c 2018-05-20 stsp static const struct got_error *
1970 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1972 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1973 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1974 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1975 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1976 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1978 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1980 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1981 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1983 26ed57b2 2018-05-19 stsp err(1, "pledge");
1986 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1987 26ed57b2 2018-05-19 stsp switch (ch) {
1990 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1994 26ed57b2 2018-05-19 stsp argc -= optind;
1995 26ed57b2 2018-05-19 stsp argv += optind;
1997 26ed57b2 2018-05-19 stsp if (argc == 0) {
1998 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1999 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2000 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2001 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2002 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2003 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
2004 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
2005 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2006 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2007 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2008 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2009 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
2010 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
2012 26ed57b2 2018-05-19 stsp usage_diff();
2014 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
2015 26ed57b2 2018-05-19 stsp free(repo_path);
2016 26ed57b2 2018-05-19 stsp if (error)
2017 26ed57b2 2018-05-19 stsp goto done;
2019 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
2020 26ed57b2 2018-05-19 stsp if (error)
2021 26ed57b2 2018-05-19 stsp goto done;
2023 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
2024 26ed57b2 2018-05-19 stsp if (error)
2025 26ed57b2 2018-05-19 stsp goto done;
2027 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2028 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2029 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
2030 ea5e7bb5 2018-08-01 stsp goto done;
2032 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
2033 5dc9f4bc 2018-08-04 stsp if (error)
2034 5dc9f4bc 2018-08-04 stsp goto done;
2035 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2037 26ed57b2 2018-05-19 stsp got_repo_close(repo);
2039 26ed57b2 2018-05-19 stsp got_object_close(obj1);
2041 26ed57b2 2018-05-19 stsp got_object_close(obj2);
2042 26ed57b2 2018-05-19 stsp return error;
2045 4ed7e80c 2018-05-20 stsp __dead static void
2046 9f7d7167 2018-04-29 stsp usage_blame(void)
2049 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2050 9f7d7167 2018-04-29 stsp getprogname());
2054 84451b3e 2018-07-10 stsp struct tog_blame_line {
2055 84451b3e 2018-07-10 stsp int annotated;
2056 84451b3e 2018-07-10 stsp struct got_object_id *id;
2059 4ed7e80c 2018-05-20 stsp static const struct got_error *
2060 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2061 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2062 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2063 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2065 84451b3e 2018-07-10 stsp const struct got_error *err;
2066 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2067 84451b3e 2018-07-10 stsp char *line;
2068 84451b3e 2018-07-10 stsp size_t len;
2069 84451b3e 2018-07-10 stsp wchar_t *wline;
2070 b700b5d6 2018-07-10 stsp int width, wlimit;
2071 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2072 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2073 ab089a2a 2018-07-12 stsp char *id_str;
2075 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2077 ab089a2a 2018-07-12 stsp return err;
2079 84451b3e 2018-07-10 stsp rewind(f);
2080 f7d12f7e 2018-08-01 stsp werase(view->window);
2082 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
2083 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
2084 ab089a2a 2018-07-12 stsp free(id_str);
2085 ab089a2a 2018-07-12 stsp return err;
2088 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2089 ab089a2a 2018-07-12 stsp free(line);
2090 2550e4c3 2018-07-13 stsp line = NULL;
2091 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2092 a3404814 2018-09-02 stsp wstandout(view->window);
2093 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2094 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2095 a3404814 2018-09-02 stsp wstandend(view->window);
2096 2550e4c3 2018-07-13 stsp free(wline);
2097 2550e4c3 2018-07-13 stsp wline = NULL;
2098 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2099 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2101 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2102 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2103 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
2104 ab089a2a 2018-07-12 stsp free(id_str);
2105 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
2107 ab089a2a 2018-07-12 stsp free(id_str);
2108 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2109 3f60a8ef 2018-07-10 stsp free(line);
2110 2550e4c3 2018-07-13 stsp line = NULL;
2112 3f60a8ef 2018-07-10 stsp return err;
2113 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2114 2550e4c3 2018-07-13 stsp free(wline);
2115 2550e4c3 2018-07-13 stsp wline = NULL;
2116 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2117 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2120 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2121 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2122 84451b3e 2018-07-10 stsp if (line == NULL) {
2126 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2127 84451b3e 2018-07-10 stsp free(line);
2131 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2132 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2133 84451b3e 2018-07-10 stsp if (err) {
2134 84451b3e 2018-07-10 stsp free(line);
2135 84451b3e 2018-07-10 stsp return err;
2138 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2139 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2141 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2142 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2143 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2144 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2145 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2146 84451b3e 2018-07-10 stsp char *id_str;
2147 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2148 84451b3e 2018-07-10 stsp if (err) {
2149 84451b3e 2018-07-10 stsp free(line);
2150 2550e4c3 2018-07-13 stsp free(wline);
2151 84451b3e 2018-07-10 stsp return err;
2153 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2154 84451b3e 2018-07-10 stsp free(id_str);
2155 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2157 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2158 ee41ec32 2018-07-10 stsp prev_id = NULL;
2161 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2162 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2163 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2166 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2167 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2168 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2169 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2170 84451b3e 2018-07-10 stsp free(line);
2171 2550e4c3 2018-07-13 stsp free(wline);
2172 2550e4c3 2018-07-13 stsp wline = NULL;
2174 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2176 1a57306a 2018-09-02 stsp view_vborder(view);
2178 84451b3e 2018-07-10 stsp return NULL;
2181 84451b3e 2018-07-10 stsp static const struct got_error *
2182 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2184 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2185 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2186 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2187 1a76625f 2018-10-22 stsp int errcode;
2189 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2190 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2191 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2193 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2194 1a76625f 2018-10-22 stsp if (errcode)
2195 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2197 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2198 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2199 d68a0a7d 2018-07-10 stsp goto done;
2202 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2203 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2205 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2206 d68a0a7d 2018-07-10 stsp if (line->annotated)
2207 d68a0a7d 2018-07-10 stsp goto done;
2209 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2210 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2211 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2212 84451b3e 2018-07-10 stsp goto done;
2214 84451b3e 2018-07-10 stsp line->annotated = 1;
2216 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2217 1a76625f 2018-10-22 stsp if (errcode)
2218 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2219 84451b3e 2018-07-10 stsp return err;
2222 84451b3e 2018-07-10 stsp static void *
2223 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2225 18430de3 2018-07-10 stsp const struct got_error *err;
2226 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
2227 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
2228 1a76625f 2018-10-22 stsp int errcode;
2230 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2231 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
2233 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2234 1a76625f 2018-10-22 stsp if (errcode)
2235 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
2237 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
2238 c9beca56 2018-07-22 stsp ta->repo = NULL;
2239 c9beca56 2018-07-22 stsp *ta->complete = 1;
2241 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2242 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2243 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2245 18430de3 2018-07-10 stsp return (void *)err;
2248 245d91c1 2018-07-12 stsp static struct got_object_id *
2249 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
2250 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
2252 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2254 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2255 245d91c1 2018-07-12 stsp if (!line->annotated)
2256 245d91c1 2018-07-12 stsp return NULL;
2258 245d91c1 2018-07-12 stsp return line->id;
2261 84451b3e 2018-07-10 stsp static const struct got_error *
2262 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
2263 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
2264 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
2266 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
2267 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
2268 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
2269 b880a918 2018-07-10 stsp struct got_object_qid *pid;
2271 b880a918 2018-07-10 stsp *pobj = NULL;
2272 b880a918 2018-07-10 stsp *obj = NULL;
2274 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
2275 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
2276 245d91c1 2018-07-12 stsp if (selected_id == NULL)
2277 b880a918 2018-07-10 stsp return NULL;
2279 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
2281 b880a918 2018-07-10 stsp goto done;
2283 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
2285 b880a918 2018-07-10 stsp goto done;
2287 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
2288 b880a918 2018-07-10 stsp if (pid) {
2289 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
2291 b880a918 2018-07-10 stsp goto done;
2294 b880a918 2018-07-10 stsp if (commit)
2295 b880a918 2018-07-10 stsp got_object_commit_close(commit);
2296 b880a918 2018-07-10 stsp return err;
2299 b880a918 2018-07-10 stsp static const struct got_error *
2300 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2302 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2305 245d91c1 2018-07-12 stsp if (blame->thread) {
2306 1a76625f 2018-10-22 stsp int errcode;
2307 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2308 1a76625f 2018-10-22 stsp if (errcode)
2309 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2310 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
2311 1a76625f 2018-10-22 stsp if (errcode)
2312 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2313 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2314 1a76625f 2018-10-22 stsp if (errcode)
2315 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2316 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2317 245d91c1 2018-07-12 stsp err = NULL;
2318 245d91c1 2018-07-12 stsp blame->thread = NULL;
2320 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2321 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2322 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2324 245d91c1 2018-07-12 stsp if (blame->f) {
2325 245d91c1 2018-07-12 stsp fclose(blame->f);
2326 245d91c1 2018-07-12 stsp blame->f = NULL;
2328 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
2329 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
2330 245d91c1 2018-07-12 stsp free(blame->lines);
2331 245d91c1 2018-07-12 stsp blame->lines = NULL;
2332 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2333 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2335 245d91c1 2018-07-12 stsp return err;
2338 245d91c1 2018-07-12 stsp static const struct got_error *
2339 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2340 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
2341 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
2342 245d91c1 2018-07-12 stsp struct got_repository *repo)
2344 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2345 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2346 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2347 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2348 27d434c2 2018-09-15 stsp struct got_object *obj = NULL;
2349 1a76625f 2018-10-22 stsp int errcode;
2351 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2353 27d434c2 2018-09-15 stsp goto done;
2355 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
2357 84451b3e 2018-07-10 stsp goto done;
2359 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2360 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2361 84451b3e 2018-07-10 stsp goto done;
2364 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
2366 a70480e0 2018-06-23 stsp goto done;
2367 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2368 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2369 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2370 84451b3e 2018-07-10 stsp goto done;
2372 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2373 245d91c1 2018-07-12 stsp blame->f, blob);
2375 84451b3e 2018-07-10 stsp goto done;
2377 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2378 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2379 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2380 84451b3e 2018-07-10 stsp goto done;
2383 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2385 bd24772e 2018-07-11 stsp goto done;
2387 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2388 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2389 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2390 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2391 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2392 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2393 245d91c1 2018-07-12 stsp goto done;
2395 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2397 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2398 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2399 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2400 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2401 245d91c1 2018-07-12 stsp *blame_complete = 0;
2403 1a76625f 2018-10-22 stsp errcode = pthread_create(&blame->thread, NULL, blame_thread,
2404 1a76625f 2018-10-22 stsp &blame->thread_args);
2405 1a76625f 2018-10-22 stsp if (errcode) {
2406 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2407 245d91c1 2018-07-12 stsp goto done;
2412 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2413 27d434c2 2018-09-15 stsp free(obj_id);
2415 245d91c1 2018-07-12 stsp got_object_close(obj);
2417 245d91c1 2018-07-12 stsp stop_blame(blame);
2418 245d91c1 2018-07-12 stsp return err;
2421 245d91c1 2018-07-12 stsp static const struct got_error *
2422 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2423 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2425 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2426 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2428 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2430 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2432 7cbe629d 2018-08-04 stsp return err;
2434 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2435 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2436 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2437 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2438 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2439 fb2756b9 2018-08-04 stsp s->path = path;
2440 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2441 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2442 fb2756b9 2018-08-04 stsp s->repo = repo;
2443 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2444 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2446 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2447 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2448 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2450 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
2451 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2452 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2453 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2456 e5a0f69f 2018-08-18 stsp static const struct got_error *
2457 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2459 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2460 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2462 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2463 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2465 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2466 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2467 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2468 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2469 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2472 e5a0f69f 2018-08-18 stsp free(s->path);
2474 e5a0f69f 2018-08-18 stsp return err;
2477 7cbe629d 2018-08-04 stsp static const struct got_error *
2478 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2480 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2481 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2483 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2484 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2485 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2486 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2488 669b5ffa 2018-10-07 stsp view_vborder(view);
2489 e5a0f69f 2018-08-18 stsp return err;
2492 e5a0f69f 2018-08-18 stsp static const struct got_error *
2493 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2494 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2496 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2497 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
2498 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2499 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2500 669b5ffa 2018-10-07 stsp int begin_x = 0;
2502 e5a0f69f 2018-08-18 stsp switch (ch) {
2504 e5a0f69f 2018-08-18 stsp s->done = 1;
2507 e5a0f69f 2018-08-18 stsp case KEY_UP:
2508 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2509 e5a0f69f 2018-08-18 stsp s->selected_line--;
2510 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2511 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2512 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2514 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2515 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2516 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2519 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2520 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2521 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2523 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2526 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2527 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2528 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2529 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2530 e5a0f69f 2018-08-18 stsp s->selected_line++;
2531 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2532 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2533 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2536 e5a0f69f 2018-08-18 stsp case 'p': {
2537 e5a0f69f 2018-08-18 stsp struct got_object_id *id;
2538 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2539 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2540 e5a0f69f 2018-08-18 stsp if (id == NULL || got_object_id_cmp(id,
2541 e5a0f69f 2018-08-18 stsp s->blamed_commit->id) == 0)
2543 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2544 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2545 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2548 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2550 e5a0f69f 2018-08-18 stsp if (ch == 'p' && pobj == NULL)
2552 e5a0f69f 2018-08-18 stsp s->done = 1;
2553 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2554 e5a0f69f 2018-08-18 stsp s->done = 0;
2555 e5a0f69f 2018-08-18 stsp if (thread_err)
2557 e5a0f69f 2018-08-18 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
2558 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2559 e5a0f69f 2018-08-18 stsp obj = NULL;
2560 e5a0f69f 2018-08-18 stsp if (pobj) {
2561 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2562 e5a0f69f 2018-08-18 stsp pobj = NULL;
2564 6402fb3c 2018-09-15 stsp err = got_object_qid_alloc(&s->blamed_commit, id);
2566 e5a0f69f 2018-08-18 stsp goto done;
2567 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2568 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2569 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2570 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2571 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2572 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2577 e5a0f69f 2018-08-18 stsp case 'B': {
2578 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2579 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2580 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2582 e5a0f69f 2018-08-18 stsp s->done = 1;
2583 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2584 e5a0f69f 2018-08-18 stsp s->done = 0;
2585 e5a0f69f 2018-08-18 stsp if (thread_err)
2587 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2588 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2589 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2590 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2591 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2592 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2593 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2594 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2599 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2600 e5a0f69f 2018-08-18 stsp case '\r':
2601 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2602 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2603 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2606 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2609 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
2610 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
2611 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2612 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2613 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2616 669b5ffa 2018-10-07 stsp err = open_diff_view(diff_view, pobj, obj, s->repo);
2617 e5a0f69f 2018-08-18 stsp if (err) {
2618 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2621 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
2622 669b5ffa 2018-10-07 stsp err = view_close_child(view);
2624 669b5ffa 2018-10-07 stsp return err;
2625 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
2626 669b5ffa 2018-10-07 stsp if (err) {
2627 669b5ffa 2018-10-07 stsp view_close(diff_view);
2630 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(diff_view)) {
2631 669b5ffa 2018-10-07 stsp *focus_view = diff_view;
2632 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
2635 669b5ffa 2018-10-07 stsp *new_view = diff_view;
2636 e5a0f69f 2018-08-18 stsp if (pobj) {
2637 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2638 e5a0f69f 2018-08-18 stsp pobj = NULL;
2640 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2641 e5a0f69f 2018-08-18 stsp obj = NULL;
2645 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2647 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2648 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2649 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2650 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2653 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2654 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2655 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2656 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2658 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2659 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2660 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2662 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2663 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2664 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2665 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2673 b880a918 2018-07-10 stsp got_object_close(pobj);
2674 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2677 a70480e0 2018-06-23 stsp static const struct got_error *
2678 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2680 a70480e0 2018-06-23 stsp const struct got_error *error;
2681 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2682 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2683 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2684 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2686 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2688 a70480e0 2018-06-23 stsp #ifndef PROFILE
2689 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2691 a70480e0 2018-06-23 stsp err(1, "pledge");
2694 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2695 a70480e0 2018-06-23 stsp switch (ch) {
2697 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2700 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2701 69069811 2018-08-02 stsp if (repo_path == NULL)
2702 69069811 2018-08-02 stsp err(1, "-r option");
2706 a70480e0 2018-06-23 stsp /* NOTREACHED */
2710 a70480e0 2018-06-23 stsp argc -= optind;
2711 a70480e0 2018-06-23 stsp argv += optind;
2713 69069811 2018-08-02 stsp if (argc == 1)
2714 69069811 2018-08-02 stsp path = argv[0];
2716 a70480e0 2018-06-23 stsp usage_blame();
2718 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2719 69069811 2018-08-02 stsp if (cwd == NULL) {
2720 69069811 2018-08-02 stsp error = got_error_from_errno();
2721 69069811 2018-08-02 stsp goto done;
2723 69069811 2018-08-02 stsp if (repo_path == NULL) {
2724 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2725 69069811 2018-08-02 stsp if (repo_path == NULL) {
2726 69069811 2018-08-02 stsp error = got_error_from_errno();
2727 69069811 2018-08-02 stsp goto done;
2732 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2733 a70480e0 2018-06-23 stsp if (error != NULL)
2734 66b4983c 2018-06-23 stsp return error;
2736 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2737 69069811 2018-08-02 stsp if (error != NULL)
2738 69069811 2018-08-02 stsp goto done;
2740 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2741 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2742 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2743 a70480e0 2018-06-23 stsp if (error != NULL)
2744 66b4983c 2018-06-23 stsp goto done;
2745 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2746 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2748 a70480e0 2018-06-23 stsp struct got_object *obj;
2749 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2750 a70480e0 2018-06-23 stsp if (error != NULL)
2751 66b4983c 2018-06-23 stsp goto done;
2752 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
2753 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2754 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2755 a19e88aa 2018-06-23 stsp got_object_close(obj);
2757 a19e88aa 2018-06-23 stsp if (error != NULL)
2758 a19e88aa 2018-06-23 stsp goto done;
2760 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2761 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2762 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2763 e1cd8fed 2018-08-01 stsp goto done;
2765 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2766 7cbe629d 2018-08-04 stsp if (error)
2767 7cbe629d 2018-08-04 stsp goto done;
2768 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2770 69069811 2018-08-02 stsp free(repo_path);
2771 69069811 2018-08-02 stsp free(cwd);
2772 a70480e0 2018-06-23 stsp free(commit_id);
2774 a70480e0 2018-06-23 stsp got_repo_close(repo);
2775 a70480e0 2018-06-23 stsp return error;
2778 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2779 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2780 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2781 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2782 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2783 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2784 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2786 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2787 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2788 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2789 ffd1d5e5 2018-06-23 stsp int width, n;
2791 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2793 f7d12f7e 2018-08-01 stsp werase(view->window);
2795 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2796 ffd1d5e5 2018-06-23 stsp return NULL;
2798 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2800 ffd1d5e5 2018-06-23 stsp return err;
2801 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2802 a3404814 2018-09-02 stsp wstandout(view->window);
2803 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2804 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2805 a3404814 2018-09-02 stsp wstandend(view->window);
2806 2550e4c3 2018-07-13 stsp free(wline);
2807 2550e4c3 2018-07-13 stsp wline = NULL;
2808 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2809 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2810 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2811 ffd1d5e5 2018-06-23 stsp return NULL;
2812 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2814 ce52c690 2018-06-23 stsp return err;
2815 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2816 2550e4c3 2018-07-13 stsp free(wline);
2817 2550e4c3 2018-07-13 stsp wline = NULL;
2818 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2819 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2820 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2821 ffd1d5e5 2018-06-23 stsp return NULL;
2822 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2823 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2824 a1eca9bb 2018-06-23 stsp return NULL;
2826 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2827 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2828 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2829 0cf4efb1 2018-09-29 stsp if (view->focussed)
2830 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2831 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2833 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2834 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
2835 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2836 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2837 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2838 ffd1d5e5 2018-06-23 stsp return NULL;
2842 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2843 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2846 ffd1d5e5 2018-06-23 stsp while (te) {
2847 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2849 1d13200f 2018-07-12 stsp if (show_ids) {
2850 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2852 1d13200f 2018-07-12 stsp return got_error_from_errno();
2854 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2855 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2856 1d13200f 2018-07-12 stsp free(id_str);
2857 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2859 1d13200f 2018-07-12 stsp free(id_str);
2860 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2861 ffd1d5e5 2018-06-23 stsp if (err) {
2862 ffd1d5e5 2018-06-23 stsp free(line);
2865 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2866 0cf4efb1 2018-09-29 stsp if (view->focussed)
2867 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2868 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2870 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2871 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2872 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2873 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
2874 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2875 ffd1d5e5 2018-06-23 stsp free(line);
2876 2550e4c3 2018-07-13 stsp free(wline);
2877 2550e4c3 2018-07-13 stsp wline = NULL;
2879 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2880 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2881 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2883 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2886 ffd1d5e5 2018-06-23 stsp return err;
2889 ffd1d5e5 2018-06-23 stsp static void
2890 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2891 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2893 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2896 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2899 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2900 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2901 ffd1d5e5 2018-06-23 stsp if (!isroot)
2902 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2906 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2907 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2908 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2909 ffd1d5e5 2018-06-23 stsp prev = te;
2910 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2912 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2913 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2915 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2916 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2919 ffd1d5e5 2018-06-23 stsp static void
2920 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2921 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2922 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2924 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2925 ffd1d5e5 2018-06-23 stsp int n = 0;
2927 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2930 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2931 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2933 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2934 ffd1d5e5 2018-06-23 stsp while (next) {
2935 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2936 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2938 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2942 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2943 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2944 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2946 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2947 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2948 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2950 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2951 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2953 ce52c690 2018-06-23 stsp len += strlen(te->name);
2955 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2956 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2957 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2959 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2960 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2961 d9765a41 2018-06-23 stsp while (pt) {
2962 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2963 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2964 cb2ebc8a 2018-06-23 stsp goto done;
2966 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2967 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2968 cb2ebc8a 2018-06-23 stsp goto done;
2970 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2973 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2974 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2975 ce52c690 2018-06-23 stsp goto done;
2979 ce52c690 2018-06-23 stsp if (err) {
2980 ce52c690 2018-06-23 stsp free(*path);
2981 ce52c690 2018-06-23 stsp *path = NULL;
2983 ce52c690 2018-06-23 stsp return err;
2986 ce52c690 2018-06-23 stsp static const struct got_error *
2987 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
2988 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2989 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2991 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2992 ce52c690 2018-06-23 stsp char *path;
2993 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
2995 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2997 ce52c690 2018-06-23 stsp return err;
2999 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3000 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3001 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3003 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
3004 e5a0f69f 2018-08-18 stsp if (err) {
3005 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3006 e5a0f69f 2018-08-18 stsp free(path);
3008 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3009 69efd4c4 2018-07-18 stsp return err;
3012 69efd4c4 2018-07-18 stsp static const struct got_error *
3013 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3014 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3015 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
3017 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3018 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3019 69efd4c4 2018-07-18 stsp char *path;
3021 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3022 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3023 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3025 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3027 69efd4c4 2018-07-18 stsp return err;
3029 23721109 2018-10-22 stsp err = open_log_view(log_view, commit_id, repo, path, 0);
3031 e5a0f69f 2018-08-18 stsp view_close(log_view);
3033 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3034 cb2ebc8a 2018-06-23 stsp free(path);
3035 cb2ebc8a 2018-06-23 stsp return err;
3038 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3039 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3040 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
3042 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3043 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3044 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3046 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3048 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3049 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3050 ffd1d5e5 2018-06-23 stsp goto done;
3052 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3053 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
3054 ffd1d5e5 2018-06-23 stsp goto done;
3057 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3058 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3059 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3060 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3061 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3062 6484ec90 2018-09-29 stsp err = got_error_from_errno();
3063 6484ec90 2018-09-29 stsp goto done;
3065 fb2756b9 2018-08-04 stsp s->repo = repo;
3067 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
3068 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
3069 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
3071 ad80ab7b 2018-08-04 stsp free(commit_id_str);
3072 6484ec90 2018-09-29 stsp if (err) {
3073 fb2756b9 2018-08-04 stsp free(s->tree_label);
3074 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3076 ad80ab7b 2018-08-04 stsp return err;
3079 e5a0f69f 2018-08-18 stsp static const struct got_error *
3080 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
3082 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3084 fb2756b9 2018-08-04 stsp free(s->tree_label);
3085 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3086 6484ec90 2018-09-29 stsp free(s->commit_id);
3087 6484ec90 2018-09-29 stsp s->commit_id = NULL;
3088 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
3089 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
3090 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
3091 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
3092 ad80ab7b 2018-08-04 stsp free(parent);
3095 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
3096 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
3097 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
3099 e5a0f69f 2018-08-18 stsp return NULL;
3102 ad80ab7b 2018-08-04 stsp static const struct got_error *
3103 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
3105 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
3106 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3107 e5a0f69f 2018-08-18 stsp char *parent_path;
3109 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
3111 e5a0f69f 2018-08-18 stsp return err;
3113 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
3114 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
3115 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3116 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
3117 e5a0f69f 2018-08-18 stsp free(parent_path);
3119 669b5ffa 2018-10-07 stsp view_vborder(view);
3120 e5a0f69f 2018-08-18 stsp return err;
3123 e5a0f69f 2018-08-18 stsp static const struct got_error *
3124 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3125 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3127 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3128 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
3129 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
3130 669b5ffa 2018-10-07 stsp int begin_x = 0;
3132 e5a0f69f 2018-08-18 stsp switch (ch) {
3134 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
3137 669b5ffa 2018-10-07 stsp if (!s->selected_entry)
3139 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
3140 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
3141 669b5ffa 2018-10-07 stsp err = log_tree_entry(&log_view, begin_x,
3142 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
3143 669b5ffa 2018-10-07 stsp s->commit_id, s->repo);
3144 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3145 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3147 669b5ffa 2018-10-07 stsp return err;
3148 669b5ffa 2018-10-07 stsp err = view_set_child(view, log_view);
3149 669b5ffa 2018-10-07 stsp if (err) {
3150 669b5ffa 2018-10-07 stsp view_close(log_view);
3153 669b5ffa 2018-10-07 stsp *focus_view = log_view;
3154 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3156 669b5ffa 2018-10-07 stsp *new_view = log_view;
3159 e5a0f69f 2018-08-18 stsp case KEY_UP:
3160 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3161 e5a0f69f 2018-08-18 stsp s->selected--;
3162 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3164 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
3165 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
3167 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
3168 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
3169 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
3170 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
3171 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3172 e5a0f69f 2018-08-18 stsp s->selected = 0;
3175 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
3176 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
3177 e5a0f69f 2018-08-18 stsp s->tree == s->root);
3180 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
3181 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
3182 e5a0f69f 2018-08-18 stsp s->selected++;
3185 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
3186 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
3188 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
3189 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
3190 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
3191 e5a0f69f 2018-08-18 stsp s->entries);
3192 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
3195 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
3196 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
3197 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3199 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
3200 e5a0f69f 2018-08-18 stsp case '\r':
3201 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
3202 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3203 7837eeac 2018-09-24 stsp case KEY_BACKSPACE:
3204 e5a0f69f 2018-08-18 stsp /* user selected '..' */
3205 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
3207 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
3208 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
3210 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
3211 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
3212 e5a0f69f 2018-08-18 stsp s->entries =
3213 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3214 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
3215 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
3216 e5a0f69f 2018-08-18 stsp s->selected_entry =
3217 e5a0f69f 2018-08-18 stsp parent->selected_entry;
3218 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
3219 e5a0f69f 2018-08-18 stsp free(parent);
3220 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
3221 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3222 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
3223 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
3224 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
3227 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
3228 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
3229 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
3232 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
3233 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
3234 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
3235 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
3236 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
3237 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3238 e5a0f69f 2018-08-18 stsp s->tree = child;
3239 e5a0f69f 2018-08-18 stsp s->entries =
3240 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3241 e5a0f69f 2018-08-18 stsp s->selected = 0;
3242 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3243 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
3244 669b5ffa 2018-10-07 stsp struct tog_view *blame_view;
3245 669b5ffa 2018-10-07 stsp int begin_x = view_is_parent_view(view) ?
3246 669b5ffa 2018-10-07 stsp view_split_begin_x(view->begin_x) : 0;
3248 669b5ffa 2018-10-07 stsp err = blame_tree_entry(&blame_view, begin_x,
3249 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents, s->commit_id,
3253 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3254 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3256 669b5ffa 2018-10-07 stsp return err;
3257 669b5ffa 2018-10-07 stsp err = view_set_child(view, blame_view);
3258 669b5ffa 2018-10-07 stsp if (err) {
3259 669b5ffa 2018-10-07 stsp view_close(blame_view);
3262 669b5ffa 2018-10-07 stsp *focus_view = blame_view;
3263 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3265 669b5ffa 2018-10-07 stsp *new_view = blame_view;
3268 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3269 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3270 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3276 ffd1d5e5 2018-06-23 stsp return err;
3279 ffd1d5e5 2018-06-23 stsp __dead static void
3280 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3283 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3284 ffd1d5e5 2018-06-23 stsp getprogname());
3288 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3289 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3291 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3292 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3293 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3294 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3295 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3296 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3297 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3299 5221c383 2018-08-01 stsp struct tog_view *view;
3301 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3302 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3304 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3307 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3308 ffd1d5e5 2018-06-23 stsp switch (ch) {
3310 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3314 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3318 ffd1d5e5 2018-06-23 stsp argc -= optind;
3319 ffd1d5e5 2018-06-23 stsp argv += optind;
3321 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3322 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
3323 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3324 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3325 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3326 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3327 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3328 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3330 ffd1d5e5 2018-06-23 stsp usage_log();
3332 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3333 ffd1d5e5 2018-06-23 stsp free(repo_path);
3334 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3335 ffd1d5e5 2018-06-23 stsp return error;
3337 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
3338 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3339 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3340 ffd1d5e5 2018-06-23 stsp goto done;
3342 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
3343 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3344 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
3345 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
3346 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
3347 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
3350 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3351 ffd1d5e5 2018-06-23 stsp goto done;
3353 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3354 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3355 ffd1d5e5 2018-06-23 stsp goto done;
3357 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3358 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3359 ffd1d5e5 2018-06-23 stsp goto done;
3361 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3362 5221c383 2018-08-01 stsp if (view == NULL) {
3363 5221c383 2018-08-01 stsp error = got_error_from_errno();
3364 5221c383 2018-08-01 stsp goto done;
3366 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3367 ad80ab7b 2018-08-04 stsp if (error)
3368 ad80ab7b 2018-08-04 stsp goto done;
3369 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3371 ffd1d5e5 2018-06-23 stsp free(commit_id);
3372 ffd1d5e5 2018-06-23 stsp if (commit)
3373 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3375 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3377 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3378 ffd1d5e5 2018-06-23 stsp return error;
3380 5c5136c5 2018-05-20 stsp static void
3381 9f7d7167 2018-04-29 stsp init_curses(void)
3383 9f7d7167 2018-04-29 stsp initscr();
3385 fd823528 2018-10-22 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3388 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3389 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3390 1f475ad8 2018-05-10 stsp curs_set(0);
3393 4ed7e80c 2018-05-20 stsp __dead static void
3394 9f7d7167 2018-04-29 stsp usage(void)
3398 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3399 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3400 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3401 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3402 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3407 c2301be8 2018-04-30 stsp static char **
3408 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3410 c2301be8 2018-04-30 stsp char **argv;
3411 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3413 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3414 c2301be8 2018-04-30 stsp if (argv == NULL)
3415 c2301be8 2018-04-30 stsp err(1, "calloc");
3416 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3417 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3418 c2301be8 2018-04-30 stsp err(1, "calloc");
3419 c2301be8 2018-04-30 stsp if (arg1) {
3420 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3421 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3422 c2301be8 2018-04-30 stsp err(1, "calloc");
3425 c2301be8 2018-04-30 stsp return argv;
3429 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3431 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3432 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3433 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3434 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3436 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
3438 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3439 9f7d7167 2018-04-29 stsp switch (ch) {
3441 9f7d7167 2018-04-29 stsp hflag = 1;
3445 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3449 9f7d7167 2018-04-29 stsp argc -= optind;
3450 9f7d7167 2018-04-29 stsp argv += optind;
3451 9f7d7167 2018-04-29 stsp optind = 0;
3452 c2301be8 2018-04-30 stsp optreset = 1;
3454 c2301be8 2018-04-30 stsp if (argc == 0) {
3455 f29d3e89 2018-06-23 stsp if (hflag)
3457 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3458 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3459 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3464 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3465 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3466 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3467 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3468 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3469 9f7d7167 2018-04-29 stsp if (hflag)
3470 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3474 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3475 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3476 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3477 c2301be8 2018-04-30 stsp if (repo_path) {
3478 c2301be8 2018-04-30 stsp struct got_repository *repo;
3479 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3480 c2301be8 2018-04-30 stsp if (error == NULL)
3481 c2301be8 2018-04-30 stsp got_repo_close(repo);
3483 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3484 c2301be8 2018-04-30 stsp if (error) {
3485 f29d3e89 2018-06-23 stsp if (hflag) {
3486 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3487 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3491 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3492 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3493 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3494 ad7de8d9 2018-04-30 stsp free(repo_path);
3497 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3498 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3500 c2301be8 2018-04-30 stsp free(repo_path);
3504 5c5136c5 2018-05-20 stsp init_curses();
3506 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3507 9f7d7167 2018-04-29 stsp if (error)
3508 9f7d7167 2018-04-29 stsp goto done;
3511 c2301be8 2018-04-30 stsp free(cmd_argv);
3512 9f7d7167 2018-04-29 stsp if (error)
3513 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);