2 9f7d7167 2018-04-29 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
20 31120ada 2018-04-30 stsp #include <errno.h>
21 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
22 9f7d7167 2018-04-29 stsp #include <curses.h>
23 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
24 9f7d7167 2018-04-29 stsp #include <panel.h>
25 9f7d7167 2018-04-29 stsp #include <locale.h>
26 9f7d7167 2018-04-29 stsp #include <stdlib.h>
27 26ed57b2 2018-05-19 stsp #include <stdio.h>
28 9f7d7167 2018-04-29 stsp #include <getopt.h>
29 9f7d7167 2018-04-29 stsp #include <string.h>
30 9f7d7167 2018-04-29 stsp #include <err.h>
31 80ddbec8 2018-04-29 stsp #include <unistd.h>
32 26ed57b2 2018-05-19 stsp #include <util.h>
33 26ed57b2 2018-05-19 stsp #include <limits.h>
34 61e69b96 2018-05-20 stsp #include <wchar.h>
35 788c352e 2018-06-16 stsp #include <time.h>
36 84451b3e 2018-07-10 stsp #include <pthread.h>
37 5036bf37 2018-09-24 stsp #include <libgen.h>
39 9f7d7167 2018-04-29 stsp #include "got_error.h"
40 80ddbec8 2018-04-29 stsp #include "got_object.h"
41 80ddbec8 2018-04-29 stsp #include "got_reference.h"
42 80ddbec8 2018-04-29 stsp #include "got_repository.h"
43 80ddbec8 2018-04-29 stsp #include "got_diff.h"
44 511a516b 2018-05-19 stsp #include "got_opentemp.h"
45 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
46 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
47 a70480e0 2018-06-23 stsp #include "got_blame.h"
50 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 9f7d7167 2018-04-29 stsp #ifndef nitems
54 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 9f7d7167 2018-04-29 stsp struct tog_cmd {
58 c2301be8 2018-04-30 stsp const char *name;
59 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
60 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
61 c2301be8 2018-04-30 stsp const char *descr;
64 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
65 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
66 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
67 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
68 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
70 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
71 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
72 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
73 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
75 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
76 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
77 9f7d7167 2018-04-29 stsp "show repository history" },
78 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
79 9f7d7167 2018-04-29 stsp "compare files and directories" },
80 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
81 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
82 ffd1d5e5 2018-06-23 stsp { "tree", cmd_tree, usage_tree,
83 ffd1d5e5 2018-06-23 stsp "browse trees in repository" },
86 d6b05b5b 2018-08-04 stsp enum tog_view_type {
87 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
88 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
89 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
90 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
93 ad80ab7b 2018-08-04 stsp struct tog_diff_view_state {
94 a3404814 2018-09-02 stsp struct got_object_id *id1, *id2;
96 ad80ab7b 2018-08-04 stsp int first_displayed_line;
97 ad80ab7b 2018-08-04 stsp int last_displayed_line;
99 48ae06ee 2018-10-18 stsp int diff_context;
100 48ae06ee 2018-10-18 stsp struct got_repository *repo;
103 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
104 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
105 ba4f502b 2018-08-04 stsp struct got_object_id *id;
106 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
109 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
110 ba4f502b 2018-08-04 stsp struct commit_queue {
111 ba4f502b 2018-08-04 stsp int ncommits;
112 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
115 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
117 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
118 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
119 1a76625f 2018-10-22 stsp int commits_needed;
120 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
121 1a76625f 2018-10-22 stsp struct commit_queue *commits;
122 1a76625f 2018-10-22 stsp const char *in_repo_path;
123 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
124 1a76625f 2018-10-22 stsp struct got_repository *repo;
125 1a76625f 2018-10-22 stsp int log_complete;
126 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
127 1a76625f 2018-10-22 stsp struct tog_view *view;
128 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
129 1a76625f 2018-10-22 stsp struct commit_queue_entry **last_displayed_entry;
130 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
131 1a76625f 2018-10-22 stsp int *selected;
134 1a76625f 2018-10-22 stsp struct tog_log_view_state {
135 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
136 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
137 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
138 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
139 b01e7d3b 2018-08-04 stsp int selected;
140 b01e7d3b 2018-08-04 stsp char *in_repo_path;
141 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
142 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
143 1a76625f 2018-10-22 stsp sig_atomic_t quit;
144 1a76625f 2018-10-22 stsp pthread_t thread;
145 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
148 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
149 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
150 e9424729 2018-08-04 stsp int nlines;
152 e9424729 2018-08-04 stsp struct tog_view *view;
153 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
155 e9424729 2018-08-04 stsp const char *path;
156 e9424729 2018-08-04 stsp int *first_displayed_line;
157 e9424729 2018-08-04 stsp int *last_displayed_line;
158 e9424729 2018-08-04 stsp int *selected_line;
163 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
164 e9424729 2018-08-04 stsp const char *path;
165 e9424729 2018-08-04 stsp struct got_repository *repo;
166 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
167 e9424729 2018-08-04 stsp int *complete;
170 e9424729 2018-08-04 stsp struct tog_blame {
172 e9424729 2018-08-04 stsp size_t filesize;
173 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
174 e9424729 2018-08-04 stsp size_t nlines;
175 e9424729 2018-08-04 stsp pthread_t thread;
176 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
177 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
178 e9424729 2018-08-04 stsp const char *path;
181 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
182 7cbe629d 2018-08-04 stsp int first_displayed_line;
183 7cbe629d 2018-08-04 stsp int last_displayed_line;
184 7cbe629d 2018-08-04 stsp int selected_line;
185 7cbe629d 2018-08-04 stsp int blame_complete;
188 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
189 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
190 e5a0f69f 2018-08-18 stsp char *path;
191 7cbe629d 2018-08-04 stsp struct got_repository *repo;
192 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
193 e9424729 2018-08-04 stsp struct tog_blame blame;
196 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
197 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
198 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
199 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
200 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
201 ad80ab7b 2018-08-04 stsp int selected;
204 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
206 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
207 ad80ab7b 2018-08-04 stsp char *tree_label;
208 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
209 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
210 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
211 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
212 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
213 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
214 ad80ab7b 2018-08-04 stsp int nentries, ndisplayed, selected, show_ids;
215 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
216 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
217 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
221 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
223 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
224 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
225 669b5ffa 2018-10-07 stsp * there is enough screen estate.
227 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
228 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
230 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
231 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
232 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
234 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
235 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
237 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
239 cc3c9aac 2018-08-01 stsp struct tog_view {
240 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
241 26ed57b2 2018-05-19 stsp WINDOW *window;
242 26ed57b2 2018-05-19 stsp PANEL *panel;
243 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
244 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
245 1004088d 2018-09-29 stsp int focussed;
246 669b5ffa 2018-10-07 stsp struct tog_view *parent;
247 669b5ffa 2018-10-07 stsp struct tog_view *child;
248 669b5ffa 2018-10-07 stsp int child_focussed;
250 5dc9f4bc 2018-08-04 stsp /* type-specific state */
251 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
253 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
254 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
255 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
256 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
259 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
260 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
261 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
262 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
265 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
266 ba4f502b 2018-08-04 stsp struct got_object *, struct got_object *, struct got_repository *);
267 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
268 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
269 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
270 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
272 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
273 ba4f502b 2018-08-04 stsp struct got_object_id *, struct got_repository *, const char *);
274 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
275 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
276 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
277 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
279 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
280 5221c383 2018-08-01 stsp struct got_object_id *, struct got_repository *);
281 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
282 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
283 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
284 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
286 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
287 4fc679ca 2018-08-04 stsp struct got_tree_object *, struct got_object_id *, struct got_repository *);
288 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
289 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
290 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
291 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
293 e5a0f69f 2018-08-18 stsp static const struct got_error *
294 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
296 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
298 669b5ffa 2018-10-07 stsp if (view->child) {
299 669b5ffa 2018-10-07 stsp view_close(view->child);
300 669b5ffa 2018-10-07 stsp view->child = NULL;
302 e5a0f69f 2018-08-18 stsp if (view->close)
303 e5a0f69f 2018-08-18 stsp err = view->close(view);
304 ea5e7bb5 2018-08-01 stsp if (view->panel)
305 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
306 ea5e7bb5 2018-08-01 stsp if (view->window)
307 ea5e7bb5 2018-08-01 stsp delwin(view->window);
308 ea5e7bb5 2018-08-01 stsp free(view);
309 e5a0f69f 2018-08-18 stsp return err;
312 ea5e7bb5 2018-08-01 stsp static struct tog_view *
313 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
314 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
316 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
318 ea5e7bb5 2018-08-01 stsp if (view == NULL)
319 ea5e7bb5 2018-08-01 stsp return NULL;
321 d6b05b5b 2018-08-04 stsp view->type = type;
322 f7d12f7e 2018-08-01 stsp view->lines = LINES;
323 f7d12f7e 2018-08-01 stsp view->cols = COLS;
324 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
325 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
326 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
327 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
328 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
329 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
330 96a765a8 2018-08-04 stsp view_close(view);
331 ea5e7bb5 2018-08-01 stsp return NULL;
333 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
334 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
335 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
336 96a765a8 2018-08-04 stsp view_close(view);
337 ea5e7bb5 2018-08-01 stsp return NULL;
340 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
341 ea5e7bb5 2018-08-01 stsp return view;
345 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
347 0cf4efb1 2018-09-29 stsp if (begin_x > 0)
349 0cf4efb1 2018-09-29 stsp return (COLS >= 120 ? COLS/2 : 0);
352 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
354 5c60c32a 2018-10-18 stsp static const struct got_error *
355 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
357 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
359 5c60c32a 2018-10-18 stsp view->begin_y = 0;
360 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
361 5c60c32a 2018-10-18 stsp view->nlines = LINES;
362 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
363 5c60c32a 2018-10-18 stsp view->lines = LINES;
364 5c60c32a 2018-10-18 stsp view->cols = COLS;
365 5c60c32a 2018-10-18 stsp err = view_resize(view);
367 5c60c32a 2018-10-18 stsp return err;
369 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
370 5c60c32a 2018-10-18 stsp return got_error_from_errno();
372 5c60c32a 2018-10-18 stsp return NULL;
375 5c60c32a 2018-10-18 stsp static const struct got_error *
376 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
378 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
380 5c60c32a 2018-10-18 stsp view->begin_x = 0;
381 5c60c32a 2018-10-18 stsp view->begin_y = 0;
382 5c60c32a 2018-10-18 stsp view->nlines = LINES;
383 5c60c32a 2018-10-18 stsp view->ncols = COLS;
384 5c60c32a 2018-10-18 stsp view->lines = LINES;
385 5c60c32a 2018-10-18 stsp view->cols = COLS;
386 5c60c32a 2018-10-18 stsp err = view_resize(view);
388 5c60c32a 2018-10-18 stsp return err;
390 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
391 5c60c32a 2018-10-18 stsp return got_error_from_errno();
393 5c60c32a 2018-10-18 stsp return NULL;
397 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
399 5c60c32a 2018-10-18 stsp return view->parent == NULL;
402 4d8c2215 2018-08-19 stsp static const struct got_error *
403 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
405 f7d12f7e 2018-08-01 stsp int nlines, ncols;
407 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
408 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
410 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
412 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
413 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
415 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
417 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
418 0cf4efb1 2018-09-29 stsp return got_error_from_errno();
419 0cf4efb1 2018-09-29 stsp replace_panel(view->panel, view->window);
421 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
422 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
423 0cf4efb1 2018-09-29 stsp view->lines = LINES;
424 0cf4efb1 2018-09-29 stsp view->cols = COLS;
426 6e3e5d9c 2018-10-18 stsp if (view->child) {
427 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
428 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
429 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
430 5c60c32a 2018-10-18 stsp if (view->child->focussed)
431 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
433 5c60c32a 2018-10-18 stsp show_panel(view->panel);
435 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
436 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
440 5c60c32a 2018-10-18 stsp return NULL;
443 669b5ffa 2018-10-07 stsp static const struct got_error *
444 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
446 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
448 669b5ffa 2018-10-07 stsp if (view->child == NULL)
449 669b5ffa 2018-10-07 stsp return NULL;
451 669b5ffa 2018-10-07 stsp err = view_close(view->child);
452 669b5ffa 2018-10-07 stsp view->child = NULL;
453 669b5ffa 2018-10-07 stsp return err;
456 669b5ffa 2018-10-07 stsp static const struct got_error *
457 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
459 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
461 669b5ffa 2018-10-07 stsp view->child = child;
462 669b5ffa 2018-10-07 stsp child->parent = view;
463 669b5ffa 2018-10-07 stsp return err;
467 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
469 669b5ffa 2018-10-07 stsp return !view_is_parent_view(view) && view->begin_x > 0;
472 0cf4efb1 2018-09-29 stsp static const struct got_error *
473 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
474 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
475 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
477 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
478 669b5ffa 2018-10-07 stsp struct tog_view *v;
479 1a76625f 2018-10-22 stsp int ch, errcode;
481 e5a0f69f 2018-08-18 stsp *new = NULL;
482 e5a0f69f 2018-08-18 stsp *dead = NULL;
483 0cf4efb1 2018-09-29 stsp *focus = NULL;
485 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
486 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
487 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
488 1a76625f 2018-10-22 stsp if (errcode)
489 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
490 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
491 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
492 1a76625f 2018-10-22 stsp if (errcode)
493 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
494 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
495 e5a0f69f 2018-08-18 stsp switch (ch) {
499 669b5ffa 2018-10-07 stsp if (view->child) {
500 669b5ffa 2018-10-07 stsp *focus = view->child;
501 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
502 669b5ffa 2018-10-07 stsp } else if (view->parent) {
503 669b5ffa 2018-10-07 stsp *focus = view->parent;
504 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 0;
508 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
509 e5a0f69f 2018-08-18 stsp *dead = view;
515 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
516 669b5ffa 2018-10-07 stsp if (view->child == NULL)
518 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view->child)) {
519 669b5ffa 2018-10-07 stsp *focus = view->child;
520 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
521 669b5ffa 2018-10-07 stsp err = view_fullscreen(view->child);
523 669b5ffa 2018-10-07 stsp err = view_splitscreen(view->child);
526 669b5ffa 2018-10-07 stsp err = view->child->input(new, dead, focus,
527 669b5ffa 2018-10-07 stsp view->child, KEY_RESIZE);
529 669b5ffa 2018-10-07 stsp if (view_is_splitscreen(view)) {
530 669b5ffa 2018-10-07 stsp *focus = view;
531 669b5ffa 2018-10-07 stsp view->parent->child_focussed = 1;
532 669b5ffa 2018-10-07 stsp err = view_fullscreen(view);
534 669b5ffa 2018-10-07 stsp err = view_splitscreen(view);
538 669b5ffa 2018-10-07 stsp err = view->input(new, dead, focus, view,
539 669b5ffa 2018-10-07 stsp KEY_RESIZE);
542 0cf4efb1 2018-09-29 stsp case KEY_RESIZE:
543 0cf4efb1 2018-09-29 stsp TAILQ_FOREACH(v, views, entry) {
544 0cf4efb1 2018-09-29 stsp err = view_resize(v);
546 0cf4efb1 2018-09-29 stsp return err;
547 878940b7 2018-09-29 stsp err = v->input(new, dead, focus, v, ch);
549 876c7f55 2018-10-22 stsp return err;
553 878940b7 2018-09-29 stsp err = view->input(new, dead, focus, view, ch);
557 e5a0f69f 2018-08-18 stsp return err;
561 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
563 0cf4efb1 2018-09-29 stsp PANEL *panel;
564 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
566 669b5ffa 2018-10-07 stsp if (view->parent)
567 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
569 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
570 0cf4efb1 2018-09-29 stsp if (panel == NULL)
573 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
574 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
575 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
579 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
581 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
582 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
584 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
586 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
589 669b5ffa 2018-10-07 stsp return view->focussed;
592 bcbd79e2 2018-08-19 stsp static const struct got_error *
593 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
595 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
596 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
597 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
598 1a76625f 2018-10-22 stsp int done = 0, errcode;
600 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
601 1a76625f 2018-10-22 stsp if (errcode)
602 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
604 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
605 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
607 a81bf10d 2018-09-29 stsp main_view = view;
608 1004088d 2018-09-29 stsp view->focussed = 1;
609 878940b7 2018-09-29 stsp err = view->show(view);
611 0cf4efb1 2018-09-29 stsp return err;
612 0cf4efb1 2018-09-29 stsp update_panels();
613 0cf4efb1 2018-09-29 stsp doupdate();
614 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
615 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
616 e4197bf9 2018-08-18 stsp view, &views);
619 e5a0f69f 2018-08-18 stsp if (dead_view) {
620 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
622 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
623 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
624 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
626 669b5ffa 2018-10-07 stsp prev = view->parent;
628 669b5ffa 2018-10-07 stsp if (dead_view->parent)
629 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
631 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
633 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
634 a81bf10d 2018-09-29 stsp if (err || dead_view == main_view)
637 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
638 0cf4efb1 2018-09-29 stsp if (focus_view)
639 0cf4efb1 2018-09-29 stsp view = focus_view;
640 669b5ffa 2018-10-07 stsp else if (prev)
641 669b5ffa 2018-10-07 stsp view = prev;
642 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
643 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
644 0cf4efb1 2018-09-29 stsp tog_view_list_head);
646 0cf4efb1 2018-09-29 stsp view = NULL;
647 669b5ffa 2018-10-07 stsp if (view) {
648 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
649 669b5ffa 2018-10-07 stsp focus_view = view->child;
651 669b5ffa 2018-10-07 stsp focus_view = view;
655 bcbd79e2 2018-08-19 stsp if (new_view) {
656 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
657 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
658 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
659 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
661 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
662 86c66b02 2018-10-18 stsp err = view_close(v);
665 86c66b02 2018-10-18 stsp if (v == view)
666 86c66b02 2018-10-18 stsp view = new_view;
669 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
670 878940b7 2018-09-29 stsp if (focus_view == NULL)
671 878940b7 2018-09-29 stsp focus_view = new_view;
673 0cf4efb1 2018-09-29 stsp if (focus_view) {
674 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
676 0cf4efb1 2018-09-29 stsp view->focussed = 0;
677 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
678 0cf4efb1 2018-09-29 stsp view = focus_view;
679 878940b7 2018-09-29 stsp if (new_view)
680 878940b7 2018-09-29 stsp show_panel(new_view->panel);
681 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
682 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
684 669b5ffa 2018-10-07 stsp if (view) {
685 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
686 1a76625f 2018-10-22 stsp focus_view = view;
687 1a76625f 2018-10-22 stsp focus_view->focussed = 1;
689 669b5ffa 2018-10-07 stsp if (view->parent) {
690 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
694 669b5ffa 2018-10-07 stsp err = view->show(view);
697 669b5ffa 2018-10-07 stsp if (view->child) {
698 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
702 1a76625f 2018-10-22 stsp update_panels();
703 1a76625f 2018-10-22 stsp doupdate();
707 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
708 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
709 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
710 e5a0f69f 2018-08-18 stsp view_close(view);
713 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
714 1a76625f 2018-10-22 stsp if (errcode)
715 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
717 e5a0f69f 2018-08-18 stsp return err;
720 4ed7e80c 2018-05-20 stsp __dead static void
721 9f7d7167 2018-04-29 stsp usage_log(void)
724 c70c5802 2018-08-01 stsp fprintf(stderr,
725 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
726 9f7d7167 2018-04-29 stsp getprogname());
730 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
731 80ddbec8 2018-04-29 stsp static const struct got_error *
732 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
734 00dfcb92 2018-06-11 stsp char *vis = NULL;
735 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
737 963b370f 2018-05-20 stsp *ws = NULL;
738 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
739 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
740 00dfcb92 2018-06-11 stsp int vislen;
741 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
742 00dfcb92 2018-06-11 stsp return got_error_from_errno();
744 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
745 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
747 00dfcb92 2018-06-11 stsp return err;
748 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
749 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
750 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
755 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
756 a7f50699 2018-06-11 stsp if (*ws == NULL) {
757 a7f50699 2018-06-11 stsp err = got_error_from_errno();
761 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
762 963b370f 2018-05-20 stsp err = got_error_from_errno();
767 963b370f 2018-05-20 stsp *ws = NULL;
770 963b370f 2018-05-20 stsp return err;
773 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
774 963b370f 2018-05-20 stsp static const struct got_error *
775 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
777 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
778 963b370f 2018-05-20 stsp int cols = 0;
779 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
780 963b370f 2018-05-20 stsp size_t wlen;
783 963b370f 2018-05-20 stsp *wlinep = NULL;
784 b700b5d6 2018-07-10 stsp *widthp = 0;
786 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
788 963b370f 2018-05-20 stsp return err;
791 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
792 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
793 963b370f 2018-05-20 stsp switch (width) {
799 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
800 b700b5d6 2018-07-10 stsp cols += width;
804 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
805 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
809 963b370f 2018-05-20 stsp err = got_error_from_errno();
813 963b370f 2018-05-20 stsp wline[i] = L'\0';
814 b700b5d6 2018-07-10 stsp if (widthp)
815 b700b5d6 2018-07-10 stsp *widthp = cols;
818 963b370f 2018-05-20 stsp free(wline);
820 963b370f 2018-05-20 stsp *wlinep = wline;
821 963b370f 2018-05-20 stsp return err;
824 963b370f 2018-05-20 stsp static const struct got_error *
825 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
826 2814baeb 2018-08-01 stsp struct got_object_id *id)
828 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
829 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
830 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
831 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
832 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
833 bb737323 2018-05-20 stsp int author_width, logmsg_width;
834 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
835 80ddbec8 2018-04-29 stsp char *line = NULL;
836 bb737323 2018-05-20 stsp int col, limit;
837 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
838 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
839 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
841 c70c5802 2018-08-01 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
842 c70c5802 2018-08-01 stsp &commit->tm_committer) >= sizeof(datebuf))
843 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
845 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
846 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
848 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
849 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
850 b39d25c7 2018-07-10 stsp col = limit + 1;
851 b39d25c7 2018-07-10 stsp if (col > avail)
854 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
855 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
856 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
859 6d9fbc00 2018-04-29 stsp author = author0;
860 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
861 6d9fbc00 2018-04-29 stsp if (smallerthan)
862 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
864 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
866 6d9fbc00 2018-04-29 stsp *at = '\0';
868 b39d25c7 2018-07-10 stsp limit = avail - col;
869 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
872 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
873 bb737323 2018-05-20 stsp col += author_width;
874 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
875 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
877 bb737323 2018-05-20 stsp author_width++;
879 9c2eaf34 2018-05-20 stsp if (col > avail)
882 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
883 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
884 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
887 bb737323 2018-05-20 stsp logmsg = logmsg0;
888 bb737323 2018-05-20 stsp while (*logmsg == '\n')
890 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
891 bb737323 2018-05-20 stsp if (newline)
892 bb737323 2018-05-20 stsp *newline = '\0';
893 bb737323 2018-05-20 stsp limit = avail - col;
894 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
897 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
898 bb737323 2018-05-20 stsp col += logmsg_width;
899 bb737323 2018-05-20 stsp while (col <= avail) {
900 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
904 80ddbec8 2018-04-29 stsp free(logmsg0);
905 bb737323 2018-05-20 stsp free(wlogmsg);
906 6d9fbc00 2018-04-29 stsp free(author0);
907 bb737323 2018-05-20 stsp free(wauthor);
908 80ddbec8 2018-04-29 stsp free(line);
909 80ddbec8 2018-04-29 stsp return err;
912 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
913 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
914 899d86c2 2018-05-10 stsp struct got_object_id *id)
916 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
918 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
919 80ddbec8 2018-04-29 stsp if (entry == NULL)
920 899d86c2 2018-05-10 stsp return NULL;
922 899d86c2 2018-05-10 stsp entry->id = id;
923 99db9666 2018-05-07 stsp entry->commit = commit;
924 899d86c2 2018-05-10 stsp return entry;
927 99db9666 2018-05-07 stsp static void
928 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
930 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
932 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
933 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
934 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
935 ecb28ae0 2018-07-16 stsp commits->ncommits--;
936 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
937 99db9666 2018-05-07 stsp free(entry);
940 99db9666 2018-05-07 stsp static void
941 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
943 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
944 99db9666 2018-05-07 stsp pop_commit(commits);
947 c4972b91 2018-05-07 stsp static const struct got_error *
948 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
949 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
951 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
952 93e45b7c 2018-09-24 stsp int nqueued = 0;
955 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
956 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
957 1a76625f 2018-10-22 stsp * while updating the display.
959 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
960 93e45b7c 2018-09-24 stsp struct got_object_id *id;
961 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
962 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
963 1a76625f 2018-10-22 stsp int errcode;
965 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
967 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
969 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
970 93e45b7c 2018-09-24 stsp minqueue, repo);
972 ecb28ae0 2018-07-16 stsp return err;
976 ecb28ae0 2018-07-16 stsp if (id == NULL)
979 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
982 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
983 9ba79e04 2018-06-11 stsp if (entry == NULL) {
984 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
988 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
989 1a76625f 2018-10-22 stsp if (errcode) {
990 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
994 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
995 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
997 ecb28ae0 2018-07-16 stsp commits->ncommits++;
999 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1000 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1001 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1004 9ba79e04 2018-06-11 stsp return err;
1007 99db9666 2018-05-07 stsp static const struct got_error *
1008 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1010 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1011 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1013 9ba79e04 2018-06-11 stsp *head_id = NULL;
1015 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1017 99db9666 2018-05-07 stsp return err;
1019 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1020 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1021 9ba79e04 2018-06-11 stsp if (err) {
1022 9ba79e04 2018-06-11 stsp *head_id = NULL;
1023 99db9666 2018-05-07 stsp return err;
1026 9ba79e04 2018-06-11 stsp return NULL;
1029 0553a4e3 2018-04-30 stsp static const struct got_error *
1030 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1031 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1032 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1033 1a76625f 2018-10-22 stsp const char *path, int commits_needed)
1035 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1036 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1037 ecb28ae0 2018-07-16 stsp int ncommits, width;
1038 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1039 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1041 e0d42f60 2018-07-22 stsp entry = first;
1042 e0d42f60 2018-07-22 stsp ncommits = 0;
1043 e0d42f60 2018-07-22 stsp while (entry) {
1044 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1045 e0d42f60 2018-07-22 stsp *selected = entry;
1048 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1049 e0d42f60 2018-07-22 stsp ncommits++;
1052 1a76625f 2018-10-22 stsp if (*selected) {
1053 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1055 ecb28ae0 2018-07-16 stsp return err;
1058 1a76625f 2018-10-22 stsp if (asprintf(&ncommits_str, " [%d/%d]%s ",
1059 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1060 1a76625f 2018-10-22 stsp commits_needed == 0 ? "" : " loading...") == -1)
1061 1a76625f 2018-10-22 stsp return got_error_from_errno();
1063 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1064 1a76625f 2018-10-22 stsp if (asprintf(&header, "commit: %s %s%s",
1065 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1066 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1067 1a76625f 2018-10-22 stsp err = got_error_from_errno();
1068 1a76625f 2018-10-22 stsp header = NULL;
1069 1a76625f 2018-10-22 stsp goto done;
1071 1a76625f 2018-10-22 stsp } else if (asprintf(&header, "commit: %s%s",
1072 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1073 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1074 1a76625f 2018-10-22 stsp err = got_error_from_errno();
1075 1a76625f 2018-10-22 stsp header = NULL;
1076 1a76625f 2018-10-22 stsp goto done;
1078 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1080 1a76625f 2018-10-22 stsp goto done;
1082 2814baeb 2018-08-01 stsp werase(view->window);
1084 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1085 a3404814 2018-09-02 stsp wstandout(view->window);
1086 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1087 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1088 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1091 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1092 a3404814 2018-09-02 stsp wstandend(view->window);
1093 ecb28ae0 2018-07-16 stsp free(wline);
1094 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1095 1a76625f 2018-10-22 stsp goto done;
1097 899d86c2 2018-05-10 stsp entry = first;
1098 899d86c2 2018-05-10 stsp *last = first;
1099 867c6645 2018-07-10 stsp ncommits = 0;
1100 899d86c2 2018-05-10 stsp while (entry) {
1101 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1103 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1104 2814baeb 2018-08-01 stsp wstandout(view->window);
1105 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
1106 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1107 2814baeb 2018-08-01 stsp wstandend(view->window);
1110 0553a4e3 2018-04-30 stsp ncommits++;
1111 899d86c2 2018-05-10 stsp *last = entry;
1112 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1115 1a57306a 2018-09-02 stsp view_vborder(view);
1117 1a76625f 2018-10-22 stsp free(id_str);
1118 1a76625f 2018-10-22 stsp free(ncommits_str);
1119 1a76625f 2018-10-22 stsp free(header);
1120 80ddbec8 2018-04-29 stsp return err;
1123 07b55e75 2018-05-10 stsp static void
1124 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1125 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1127 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1128 07b55e75 2018-05-10 stsp int nscrolled = 0;
1130 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1131 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
1134 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1135 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1136 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1137 07b55e75 2018-05-10 stsp if (entry) {
1138 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1139 07b55e75 2018-05-10 stsp nscrolled++;
1144 aa075928 2018-05-10 stsp static const struct got_error *
1145 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1146 93e45b7c 2018-09-24 stsp struct commit_queue_entry **last_displayed_entry,
1147 1a76625f 2018-10-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1148 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1150 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
1151 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
1152 aa075928 2018-05-10 stsp int nscrolled = 0;
1154 1a76625f 2018-10-22 stsp if (*last_displayed_entry == NULL)
1155 1a76625f 2018-10-22 stsp return NULL;
1158 93e45b7c 2018-09-24 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1159 aa075928 2018-05-10 stsp if (pentry == NULL) {
1160 1a76625f 2018-10-22 stsp int errcode;
1161 1a76625f 2018-10-22 stsp if (*log_complete)
1162 1a76625f 2018-10-22 stsp return NULL;
1163 1a76625f 2018-10-22 stsp *commits_needed = maxscroll + 20;
1164 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(need_commits);
1165 1a76625f 2018-10-22 stsp if (errcode)
1166 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1167 1a76625f 2018-10-22 stsp return NULL;
1169 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1171 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1172 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1174 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1175 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1177 dd0a52c1 2018-05-20 stsp return err;
1180 cd0acaa7 2018-05-20 stsp static const struct got_error *
1181 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1182 0cf4efb1 2018-09-29 stsp struct got_object_id *commit_id, struct got_commit_object *commit,
1183 2a4718d3 2018-09-29 stsp struct got_repository *repo)
1185 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1186 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1187 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1188 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1190 2a4718d3 2018-09-29 stsp err = got_object_open(&obj2, repo, commit_id);
1192 cd0acaa7 2018-05-20 stsp return err;
1194 2a4718d3 2018-09-29 stsp parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1195 9ba79e04 2018-06-11 stsp if (parent_id) {
1196 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
1198 cd0acaa7 2018-05-20 stsp goto done;
1201 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1202 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
1203 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1204 ea5e7bb5 2018-08-01 stsp goto done;
1207 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, obj1, obj2, repo);
1208 e5a0f69f 2018-08-18 stsp if (err == NULL)
1209 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1212 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
1214 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
1215 cd0acaa7 2018-05-20 stsp return err;
1218 80ddbec8 2018-04-29 stsp static const struct got_error *
1219 0cf4efb1 2018-09-29 stsp browse_commit(struct tog_view **new_view, int begin_x,
1220 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1222 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1223 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1224 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1226 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1228 9343a5fb 2018-06-23 stsp return err;
1230 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1231 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1232 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1234 e5a0f69f 2018-08-18 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1236 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1238 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1239 1a76625f 2018-10-22 stsp return err;
1242 1a76625f 2018-10-22 stsp static void *
1243 1a76625f 2018-10-22 stsp log_thread(void *arg)
1245 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1246 1a76625f 2018-10-22 stsp int errcode = 0;
1247 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1248 1a76625f 2018-10-22 stsp int done = 0;
1250 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1252 1a76625f 2018-10-22 stsp return (void *)err;
1254 1a76625f 2018-10-22 stsp while (!done && !err) {
1255 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1256 1a76625f 2018-10-22 stsp a->in_repo_path);
1257 1a76625f 2018-10-22 stsp if (err) {
1258 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1259 1a76625f 2018-10-22 stsp return (void *)err;
1260 1a76625f 2018-10-22 stsp err = NULL;
1262 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1263 1a76625f 2018-10-22 stsp a->commits_needed--;
1265 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1266 1a76625f 2018-10-22 stsp if (errcode)
1267 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
1270 1a76625f 2018-10-22 stsp a->log_complete = 1;
1271 1a76625f 2018-10-22 stsp else if (*a->quit) {
1273 1a76625f 2018-10-22 stsp a->log_complete = 1;
1274 1a76625f 2018-10-22 stsp } else if (*a->first_displayed_entry == NULL) {
1275 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1276 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1277 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1280 1a76625f 2018-10-22 stsp err = draw_commits(a->view, a->last_displayed_entry,
1281 1a76625f 2018-10-22 stsp a->selected_entry, *a->first_displayed_entry,
1282 1a76625f 2018-10-22 stsp a->commits, *a->selected, a->view->nlines,
1283 1a76625f 2018-10-22 stsp a->in_repo_path, a->commits_needed);
1284 510e8b0a 2018-10-22 stsp #if 0 /* XXX ncurses isn't thread-safe */
1285 510e8b0a 2018-10-22 stsp if (!err) {
1286 1a76625f 2018-10-22 stsp update_panels();
1287 1a76625f 2018-10-22 stsp doupdate();
1291 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1292 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1293 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1294 1a76625f 2018-10-22 stsp &tog_mutex);
1295 1a76625f 2018-10-22 stsp if (errcode)
1296 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1299 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1300 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1301 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1303 1a76625f 2018-10-22 stsp return (void *)err;
1306 1a76625f 2018-10-22 stsp static const struct got_error *
1307 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1309 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1310 1a76625f 2018-10-22 stsp int errcode;
1312 1a76625f 2018-10-22 stsp if (s->thread) {
1313 1a76625f 2018-10-22 stsp s->quit = 1;
1314 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1315 1a76625f 2018-10-22 stsp if (errcode)
1316 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1317 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1318 1a76625f 2018-10-22 stsp if (errcode)
1319 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1320 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1321 1a76625f 2018-10-22 stsp if (errcode)
1322 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1323 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1324 1a76625f 2018-10-22 stsp if (errcode)
1325 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1326 1a76625f 2018-10-22 stsp s->thread = NULL;
1329 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1330 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1331 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1333 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1334 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1335 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1338 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1339 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1340 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1343 9343a5fb 2018-06-23 stsp return err;
1346 9343a5fb 2018-06-23 stsp static const struct got_error *
1347 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1349 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1350 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1352 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1353 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1354 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1355 1a76625f 2018-10-22 stsp free(s->start_id);
1356 1a76625f 2018-10-22 stsp return err;
1359 1a76625f 2018-10-22 stsp static const struct got_error *
1360 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1361 04cc582a 2018-08-01 stsp struct got_repository *repo, const char *path)
1363 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1364 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1365 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1366 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1367 1a76625f 2018-10-22 stsp int errcode;
1369 fb2756b9 2018-08-04 stsp err = got_repo_map_path(&s->in_repo_path, repo, path);
1370 ecb28ae0 2018-07-16 stsp if (err != NULL)
1371 ecb28ae0 2018-07-16 stsp goto done;
1373 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1374 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1375 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1377 fb2756b9 2018-08-04 stsp s->repo = repo;
1378 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1379 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1380 5036bf37 2018-09-24 stsp err = got_error_from_errno();
1381 5036bf37 2018-09-24 stsp goto done;
1384 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1385 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1386 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1388 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1390 1a76625f 2018-10-22 stsp goto done;
1391 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1392 1a76625f 2018-10-22 stsp 0, thread_repo);
1394 1a76625f 2018-10-22 stsp goto done;
1396 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1397 1a76625f 2018-10-22 stsp if (errcode) {
1398 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1399 1a76625f 2018-10-22 stsp goto done;
1402 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1403 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1404 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1405 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1406 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1407 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1408 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1409 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1410 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1411 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1412 1a76625f 2018-10-22 stsp s->thread_args.last_displayed_entry = &s->last_displayed_entry;
1413 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1414 1a76625f 2018-10-22 stsp s->thread_args.selected = &s->selected;
1416 1a76625f 2018-10-22 stsp errcode = pthread_create(&s->thread, NULL, log_thread,
1417 1a76625f 2018-10-22 stsp &s->thread_args);
1418 1a76625f 2018-10-22 stsp if (errcode) {
1419 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1420 1a76625f 2018-10-22 stsp goto done;
1425 1a76625f 2018-10-22 stsp close_log_view(view);
1426 ba4f502b 2018-08-04 stsp return err;
1429 e5a0f69f 2018-08-18 stsp static const struct got_error *
1430 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1432 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1434 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1435 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1436 1a76625f 2018-10-22 stsp &s->commits, s->selected, view->nlines,
1437 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1440 e5a0f69f 2018-08-18 stsp static const struct got_error *
1441 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1442 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1444 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1445 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1446 5036bf37 2018-09-24 stsp char *parent_path;
1447 669b5ffa 2018-10-07 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
1448 669b5ffa 2018-10-07 stsp int begin_x = 0;
1450 e5a0f69f 2018-08-18 stsp switch (ch) {
1452 1a76625f 2018-10-22 stsp s->quit = 1;
1455 e5a0f69f 2018-08-18 stsp case KEY_UP:
1456 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1457 e5a0f69f 2018-08-18 stsp s->selected--;
1458 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1460 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1461 e5a0f69f 2018-08-18 stsp &s->commits);
1463 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1464 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1465 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1466 e5a0f69f 2018-08-18 stsp s->selected = 0;
1469 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1470 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1473 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1474 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1475 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1476 e5a0f69f 2018-08-18 stsp s->selected++;
1479 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1480 93e45b7c 2018-09-24 stsp &s->last_displayed_entry, &s->commits,
1481 1a76625f 2018-10-22 stsp &s->thread_args.log_complete,
1482 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1483 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1485 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1486 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1487 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1488 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1489 93e45b7c 2018-09-24 stsp view->nlines, &s->last_displayed_entry,
1490 1a76625f 2018-10-22 stsp &s->commits, &s->thread_args.log_complete,
1491 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1492 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1493 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1494 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1495 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1496 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1497 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1498 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1500 e5a0f69f 2018-08-18 stsp err = NULL;
1503 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1504 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1505 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1506 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1507 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1509 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1510 e5a0f69f 2018-08-18 stsp case '\r':
1511 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1512 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1513 669b5ffa 2018-10-07 stsp err = open_diff_view_for_commit(&diff_view, begin_x,
1514 2a4718d3 2018-09-29 stsp s->selected_entry->id, s->selected_entry->commit,
1518 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1519 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1521 669b5ffa 2018-10-07 stsp return err;
1522 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
1523 669b5ffa 2018-10-07 stsp if (err) {
1524 669b5ffa 2018-10-07 stsp view_close(diff_view);
1527 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(diff_view)) {
1528 669b5ffa 2018-10-07 stsp *focus_view = diff_view;
1529 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1532 669b5ffa 2018-10-07 stsp *new_view = diff_view;
1535 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1536 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1537 669b5ffa 2018-10-07 stsp err = browse_commit(&tree_view, begin_x,
1538 0cf4efb1 2018-09-29 stsp s->selected_entry, s->repo);
1539 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1540 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1542 669b5ffa 2018-10-07 stsp return err;
1543 669b5ffa 2018-10-07 stsp err = view_set_child(view, tree_view);
1544 669b5ffa 2018-10-07 stsp if (err) {
1545 669b5ffa 2018-10-07 stsp view_close(tree_view);
1548 669b5ffa 2018-10-07 stsp *focus_view = tree_view;
1549 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1551 669b5ffa 2018-10-07 stsp *new_view = tree_view;
1553 5036bf37 2018-09-24 stsp case KEY_BACKSPACE:
1554 5036bf37 2018-09-24 stsp if (strcmp(s->in_repo_path, "/") == 0)
1556 5036bf37 2018-09-24 stsp parent_path = dirname(s->in_repo_path);
1557 5036bf37 2018-09-24 stsp if (parent_path && strcmp(parent_path, ".") != 0) {
1558 5036bf37 2018-09-24 stsp struct tog_view *lv;
1559 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1561 1a76625f 2018-10-22 stsp return err;
1562 0cf4efb1 2018-09-29 stsp lv = view_open(view->nlines, view->ncols,
1563 0cf4efb1 2018-09-29 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
1564 5036bf37 2018-09-24 stsp if (lv == NULL)
1565 5036bf37 2018-09-24 stsp return got_error_from_errno();
1566 5036bf37 2018-09-24 stsp err = open_log_view(lv, s->start_id, s->repo,
1567 5036bf37 2018-09-24 stsp parent_path);
1569 1a76625f 2018-10-22 stsp return err;;
1570 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1571 669b5ffa 2018-10-07 stsp *new_view = lv;
1573 669b5ffa 2018-10-07 stsp view_set_child(view->parent, lv);
1574 669b5ffa 2018-10-07 stsp *dead_view = view;
1576 1a76625f 2018-10-22 stsp return NULL;
1583 80ddbec8 2018-04-29 stsp return err;
1586 4ed7e80c 2018-05-20 stsp static const struct got_error *
1587 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1589 80ddbec8 2018-04-29 stsp const struct got_error *error;
1590 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1591 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1592 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1593 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1595 04cc582a 2018-08-01 stsp struct tog_view *view;
1597 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1598 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1600 80ddbec8 2018-04-29 stsp err(1, "pledge");
1603 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1604 80ddbec8 2018-04-29 stsp switch (ch) {
1606 80ddbec8 2018-04-29 stsp start_commit = optarg;
1609 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1610 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1611 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1615 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1619 80ddbec8 2018-04-29 stsp argc -= optind;
1620 80ddbec8 2018-04-29 stsp argv += optind;
1622 ecb28ae0 2018-07-16 stsp if (argc == 0)
1623 ecb28ae0 2018-07-16 stsp path = strdup("");
1624 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1625 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1627 80ddbec8 2018-04-29 stsp usage_log();
1628 ecb28ae0 2018-07-16 stsp if (path == NULL)
1629 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1631 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1632 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1633 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1634 ecb28ae0 2018-07-16 stsp goto done;
1636 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1637 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1638 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1639 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1640 ecb28ae0 2018-07-16 stsp goto done;
1644 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1645 80ddbec8 2018-04-29 stsp if (error != NULL)
1646 ecb28ae0 2018-07-16 stsp goto done;
1648 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1649 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1650 80ddbec8 2018-04-29 stsp if (error != NULL)
1651 ecb28ae0 2018-07-16 stsp goto done;
1653 80ddbec8 2018-04-29 stsp struct got_object *obj;
1654 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1655 80ddbec8 2018-04-29 stsp if (error == NULL) {
1656 6402fb3c 2018-09-15 stsp start_id = got_object_id_dup(got_object_get_id(obj));
1657 899d86c2 2018-05-10 stsp if (start_id == NULL)
1658 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1659 ecb28ae0 2018-07-16 stsp goto done;
1662 80ddbec8 2018-04-29 stsp if (error != NULL)
1663 ecb28ae0 2018-07-16 stsp goto done;
1665 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1666 04cc582a 2018-08-01 stsp if (view == NULL) {
1667 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1668 04cc582a 2018-08-01 stsp goto done;
1670 ba4f502b 2018-08-04 stsp error = open_log_view(view, start_id, repo, path);
1671 ba4f502b 2018-08-04 stsp if (error)
1672 ba4f502b 2018-08-04 stsp goto done;
1673 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1675 ecb28ae0 2018-07-16 stsp free(repo_path);
1676 ecb28ae0 2018-07-16 stsp free(cwd);
1677 ecb28ae0 2018-07-16 stsp free(path);
1678 899d86c2 2018-05-10 stsp free(start_id);
1680 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1681 80ddbec8 2018-04-29 stsp return error;
1684 4ed7e80c 2018-05-20 stsp __dead static void
1685 9f7d7167 2018-04-29 stsp usage_diff(void)
1688 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1689 9f7d7167 2018-04-29 stsp getprogname());
1693 b304db33 2018-05-20 stsp static char *
1694 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1696 b304db33 2018-05-20 stsp char *line;
1697 b304db33 2018-05-20 stsp size_t linelen;
1698 b304db33 2018-05-20 stsp size_t lineno;
1699 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1701 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1703 b304db33 2018-05-20 stsp *len = linelen;
1704 b304db33 2018-05-20 stsp return line;
1707 4ed7e80c 2018-05-20 stsp static const struct got_error *
1708 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1709 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1710 a3404814 2018-09-02 stsp char * header)
1712 61e69b96 2018-05-20 stsp const struct got_error *err;
1713 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1714 b304db33 2018-05-20 stsp char *line;
1715 b304db33 2018-05-20 stsp size_t len;
1716 61e69b96 2018-05-20 stsp wchar_t *wline;
1717 e0b650dd 2018-05-20 stsp int width;
1719 26ed57b2 2018-05-19 stsp rewind(f);
1720 f7d12f7e 2018-08-01 stsp werase(view->window);
1722 a3404814 2018-09-02 stsp if (header) {
1723 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1724 a3404814 2018-09-02 stsp if (err) {
1725 a3404814 2018-09-02 stsp return err;
1728 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1729 a3404814 2018-09-02 stsp wstandout(view->window);
1730 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1731 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1732 a3404814 2018-09-02 stsp wstandend(view->window);
1733 a3404814 2018-09-02 stsp if (width < view->ncols)
1734 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1736 a3404814 2018-09-02 stsp if (max_lines <= 1)
1737 a3404814 2018-09-02 stsp return NULL;
1738 a3404814 2018-09-02 stsp max_lines--;
1742 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1743 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1744 26ed57b2 2018-05-19 stsp if (line == NULL) {
1748 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1749 26ed57b2 2018-05-19 stsp free(line);
1753 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1754 61e69b96 2018-05-20 stsp if (err) {
1755 61e69b96 2018-05-20 stsp free(line);
1756 61e69b96 2018-05-20 stsp return err;
1758 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1759 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1760 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1761 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1762 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1763 26ed57b2 2018-05-19 stsp free(line);
1764 2550e4c3 2018-07-13 stsp free(wline);
1765 2550e4c3 2018-07-13 stsp wline = NULL;
1767 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1769 1a57306a 2018-09-02 stsp view_vborder(view);
1771 26ed57b2 2018-05-19 stsp return NULL;
1774 4ed7e80c 2018-05-20 stsp static const struct got_error *
1775 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
1777 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
1778 48ae06ee 2018-10-18 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1779 48ae06ee 2018-10-18 stsp FILE *f = NULL;
1781 48ae06ee 2018-10-18 stsp if (s->id1) {
1782 48ae06ee 2018-10-18 stsp err = got_object_open(&obj1, s->repo, s->id1);
1784 48ae06ee 2018-10-18 stsp return err;
1787 48ae06ee 2018-10-18 stsp err = got_object_open(&obj2, s->repo, s->id2);
1789 48ae06ee 2018-10-18 stsp goto done;
1791 511a516b 2018-05-19 stsp f = got_opentemp();
1792 48ae06ee 2018-10-18 stsp if (f == NULL) {
1793 48ae06ee 2018-10-18 stsp err = got_error_from_errno();
1794 48ae06ee 2018-10-18 stsp goto done;
1797 48ae06ee 2018-10-18 stsp fclose(s->f);
1800 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1801 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1802 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1803 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1805 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1806 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_trees(obj1, obj2, "", "",
1807 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1809 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1810 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1811 48ae06ee 2018-10-18 stsp s->repo, f);
1814 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1819 48ae06ee 2018-10-18 stsp got_object_close(obj1);
1820 48ae06ee 2018-10-18 stsp got_object_close(obj2);
1822 48ae06ee 2018-10-18 stsp fflush(f);
1823 48ae06ee 2018-10-18 stsp return err;
1826 48ae06ee 2018-10-18 stsp static const struct got_error *
1827 48ae06ee 2018-10-18 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1828 48ae06ee 2018-10-18 stsp struct got_object *obj2, struct got_repository *repo)
1830 48ae06ee 2018-10-18 stsp const struct got_error *err;
1832 48ae06ee 2018-10-18 stsp if (obj1 != NULL && obj2 != NULL &&
1833 48ae06ee 2018-10-18 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1834 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
1836 48ae06ee 2018-10-18 stsp if (obj1) {
1837 48ae06ee 2018-10-18 stsp struct got_object_id *id1;
1838 48ae06ee 2018-10-18 stsp id1 = got_object_id_dup(got_object_get_id(obj1));
1839 48ae06ee 2018-10-18 stsp if (id1 == NULL)
1840 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1841 48ae06ee 2018-10-18 stsp view->state.diff.id1 = id1;
1843 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1845 48ae06ee 2018-10-18 stsp view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1846 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
1847 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1848 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1849 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1851 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
1852 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1853 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1854 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
1855 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
1857 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
1858 48ae06ee 2018-10-18 stsp if (err) {
1859 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1860 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1861 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
1862 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
1863 48ae06ee 2018-10-18 stsp return err;
1866 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
1867 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
1868 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
1870 5dc9f4bc 2018-08-04 stsp return NULL;
1873 e5a0f69f 2018-08-18 stsp static const struct got_error *
1874 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1876 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1878 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1879 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1880 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
1881 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
1882 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1883 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1884 e5a0f69f 2018-08-18 stsp return err;
1887 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1888 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1890 a3404814 2018-09-02 stsp const struct got_error *err;
1891 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1892 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
1894 a3404814 2018-09-02 stsp if (s->id1) {
1895 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
1897 a3404814 2018-09-02 stsp return err;
1899 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
1901 a3404814 2018-09-02 stsp return err;
1903 a3404814 2018-09-02 stsp if (asprintf(&header, "diff: %s %s",
1904 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1905 a3404814 2018-09-02 stsp err = got_error_from_errno();
1906 a3404814 2018-09-02 stsp free(id_str1);
1907 a3404814 2018-09-02 stsp free(id_str2);
1908 a3404814 2018-09-02 stsp return err;
1910 a3404814 2018-09-02 stsp free(id_str1);
1911 a3404814 2018-09-02 stsp free(id_str2);
1913 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
1914 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
1918 0cf4efb1 2018-09-29 stsp static const struct got_error *
1919 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1920 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1922 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1923 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
1926 e5a0f69f 2018-08-18 stsp switch (ch) {
1928 e5a0f69f 2018-08-18 stsp case KEY_UP:
1929 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
1930 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1932 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1934 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
1935 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
1936 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1939 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1940 e5a0f69f 2018-08-18 stsp if (!s->eof)
1941 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1943 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
1946 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
1947 e5a0f69f 2018-08-18 stsp char *line;
1948 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
1949 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1950 e5a0f69f 2018-08-18 stsp if (line == NULL)
1955 48ae06ee 2018-10-18 stsp if (s->diff_context > 0) {
1956 48ae06ee 2018-10-18 stsp s->diff_context--;
1957 48ae06ee 2018-10-18 stsp err = create_diff(s);
1961 4a8520aa 2018-10-18 stsp if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
1962 48ae06ee 2018-10-18 stsp s->diff_context++;
1963 48ae06ee 2018-10-18 stsp err = create_diff(s);
1970 bcbd79e2 2018-08-19 stsp return err;
1973 4ed7e80c 2018-05-20 stsp static const struct got_error *
1974 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1976 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1977 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1978 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1979 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1980 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1982 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1984 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1985 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1987 26ed57b2 2018-05-19 stsp err(1, "pledge");
1990 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1991 26ed57b2 2018-05-19 stsp switch (ch) {
1994 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1998 26ed57b2 2018-05-19 stsp argc -= optind;
1999 26ed57b2 2018-05-19 stsp argv += optind;
2001 26ed57b2 2018-05-19 stsp if (argc == 0) {
2002 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2003 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2004 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2005 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2006 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2007 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
2008 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
2009 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2010 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2011 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2012 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2013 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
2014 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
2016 26ed57b2 2018-05-19 stsp usage_diff();
2018 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
2019 26ed57b2 2018-05-19 stsp free(repo_path);
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(&obj1, repo, obj_id_str1);
2024 26ed57b2 2018-05-19 stsp if (error)
2025 26ed57b2 2018-05-19 stsp goto done;
2027 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
2028 26ed57b2 2018-05-19 stsp if (error)
2029 26ed57b2 2018-05-19 stsp goto done;
2031 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2032 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2033 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
2034 ea5e7bb5 2018-08-01 stsp goto done;
2036 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
2037 5dc9f4bc 2018-08-04 stsp if (error)
2038 5dc9f4bc 2018-08-04 stsp goto done;
2039 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2041 26ed57b2 2018-05-19 stsp got_repo_close(repo);
2043 26ed57b2 2018-05-19 stsp got_object_close(obj1);
2045 26ed57b2 2018-05-19 stsp got_object_close(obj2);
2046 26ed57b2 2018-05-19 stsp return error;
2049 4ed7e80c 2018-05-20 stsp __dead static void
2050 9f7d7167 2018-04-29 stsp usage_blame(void)
2053 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2054 9f7d7167 2018-04-29 stsp getprogname());
2058 84451b3e 2018-07-10 stsp struct tog_blame_line {
2059 84451b3e 2018-07-10 stsp int annotated;
2060 84451b3e 2018-07-10 stsp struct got_object_id *id;
2063 4ed7e80c 2018-05-20 stsp static const struct got_error *
2064 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2065 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2066 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2067 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2069 84451b3e 2018-07-10 stsp const struct got_error *err;
2070 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2071 84451b3e 2018-07-10 stsp char *line;
2072 84451b3e 2018-07-10 stsp size_t len;
2073 84451b3e 2018-07-10 stsp wchar_t *wline;
2074 b700b5d6 2018-07-10 stsp int width, wlimit;
2075 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2076 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2077 ab089a2a 2018-07-12 stsp char *id_str;
2079 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2081 ab089a2a 2018-07-12 stsp return err;
2083 84451b3e 2018-07-10 stsp rewind(f);
2084 f7d12f7e 2018-08-01 stsp werase(view->window);
2086 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
2087 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
2088 ab089a2a 2018-07-12 stsp free(id_str);
2089 ab089a2a 2018-07-12 stsp return err;
2092 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2093 ab089a2a 2018-07-12 stsp free(line);
2094 2550e4c3 2018-07-13 stsp line = NULL;
2095 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2096 a3404814 2018-09-02 stsp wstandout(view->window);
2097 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2098 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2099 a3404814 2018-09-02 stsp wstandend(view->window);
2100 2550e4c3 2018-07-13 stsp free(wline);
2101 2550e4c3 2018-07-13 stsp wline = NULL;
2102 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2103 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2105 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2106 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2107 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
2108 ab089a2a 2018-07-12 stsp free(id_str);
2109 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
2111 ab089a2a 2018-07-12 stsp free(id_str);
2112 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2113 3f60a8ef 2018-07-10 stsp free(line);
2114 2550e4c3 2018-07-13 stsp line = NULL;
2116 3f60a8ef 2018-07-10 stsp return err;
2117 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2118 2550e4c3 2018-07-13 stsp free(wline);
2119 2550e4c3 2018-07-13 stsp wline = NULL;
2120 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2121 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2124 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2125 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2126 84451b3e 2018-07-10 stsp if (line == NULL) {
2130 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2131 84451b3e 2018-07-10 stsp free(line);
2135 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2136 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2137 84451b3e 2018-07-10 stsp if (err) {
2138 84451b3e 2018-07-10 stsp free(line);
2139 84451b3e 2018-07-10 stsp return err;
2142 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2143 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2145 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2146 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2147 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2148 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2149 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2150 84451b3e 2018-07-10 stsp char *id_str;
2151 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2152 84451b3e 2018-07-10 stsp if (err) {
2153 84451b3e 2018-07-10 stsp free(line);
2154 2550e4c3 2018-07-13 stsp free(wline);
2155 84451b3e 2018-07-10 stsp return err;
2157 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2158 84451b3e 2018-07-10 stsp free(id_str);
2159 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2161 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2162 ee41ec32 2018-07-10 stsp prev_id = NULL;
2165 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2166 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2167 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2170 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2171 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2172 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2173 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2174 84451b3e 2018-07-10 stsp free(line);
2175 2550e4c3 2018-07-13 stsp free(wline);
2176 2550e4c3 2018-07-13 stsp wline = NULL;
2178 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2180 1a57306a 2018-09-02 stsp view_vborder(view);
2182 84451b3e 2018-07-10 stsp return NULL;
2185 84451b3e 2018-07-10 stsp static const struct got_error *
2186 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2188 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2189 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2190 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2191 1a76625f 2018-10-22 stsp int errcode;
2193 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2194 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2195 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2197 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2198 1a76625f 2018-10-22 stsp if (errcode)
2199 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2201 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2202 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2203 d68a0a7d 2018-07-10 stsp goto done;
2206 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2207 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2209 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2210 d68a0a7d 2018-07-10 stsp if (line->annotated)
2211 d68a0a7d 2018-07-10 stsp goto done;
2213 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2214 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2215 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2216 84451b3e 2018-07-10 stsp goto done;
2218 84451b3e 2018-07-10 stsp line->annotated = 1;
2220 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
2221 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
2222 e5a0f69f 2018-08-18 stsp a->last_displayed_line, a->eof, a->view->nlines);
2223 510e8b0a 2018-10-22 stsp #if 0 /* XXX ncurses isn't thread-safe */
2224 d6144aad 2018-10-22 stsp if (!err) {
2225 d6144aad 2018-10-22 stsp update_panels();
2226 d6144aad 2018-10-22 stsp doupdate();
2230 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2231 1a76625f 2018-10-22 stsp if (errcode)
2232 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2233 84451b3e 2018-07-10 stsp return err;
2236 84451b3e 2018-07-10 stsp static void *
2237 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2239 18430de3 2018-07-10 stsp const struct got_error *err;
2240 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
2241 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
2242 1a76625f 2018-10-22 stsp int errcode;
2244 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2245 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
2247 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2248 1a76625f 2018-10-22 stsp if (errcode)
2249 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
2251 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
2252 c9beca56 2018-07-22 stsp ta->repo = NULL;
2253 c9beca56 2018-07-22 stsp *ta->complete = 1;
2254 1a76625f 2018-10-22 stsp if (!err) {
2255 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
2256 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
2257 e5a0f69f 2018-08-18 stsp a->first_displayed_line, a->last_displayed_line, a->eof,
2258 f7d12f7e 2018-08-01 stsp a->view->nlines);
2259 510e8b0a 2018-10-22 stsp #if 0 /* XXX ncurses isn't thread-safe */
2260 1a76625f 2018-10-22 stsp if (!err) {
2261 1a76625f 2018-10-22 stsp update_panels();
2262 1a76625f 2018-10-22 stsp doupdate();
2267 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2268 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2269 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2271 18430de3 2018-07-10 stsp return (void *)err;
2274 245d91c1 2018-07-12 stsp static struct got_object_id *
2275 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
2276 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
2278 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2280 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2281 245d91c1 2018-07-12 stsp if (!line->annotated)
2282 245d91c1 2018-07-12 stsp return NULL;
2284 245d91c1 2018-07-12 stsp return line->id;
2287 84451b3e 2018-07-10 stsp static const struct got_error *
2288 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
2289 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
2290 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
2292 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
2293 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
2294 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
2295 b880a918 2018-07-10 stsp struct got_object_qid *pid;
2297 b880a918 2018-07-10 stsp *pobj = NULL;
2298 b880a918 2018-07-10 stsp *obj = NULL;
2300 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
2301 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
2302 245d91c1 2018-07-12 stsp if (selected_id == NULL)
2303 b880a918 2018-07-10 stsp return NULL;
2305 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
2307 b880a918 2018-07-10 stsp goto done;
2309 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
2311 b880a918 2018-07-10 stsp goto done;
2313 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
2314 b880a918 2018-07-10 stsp if (pid) {
2315 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
2317 b880a918 2018-07-10 stsp goto done;
2320 b880a918 2018-07-10 stsp if (commit)
2321 b880a918 2018-07-10 stsp got_object_commit_close(commit);
2322 b880a918 2018-07-10 stsp return err;
2325 b880a918 2018-07-10 stsp static const struct got_error *
2326 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2328 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2331 245d91c1 2018-07-12 stsp if (blame->thread) {
2332 1a76625f 2018-10-22 stsp int errcode;
2333 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2334 1a76625f 2018-10-22 stsp if (errcode)
2335 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2336 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
2337 1a76625f 2018-10-22 stsp if (errcode)
2338 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2339 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2340 1a76625f 2018-10-22 stsp if (errcode)
2341 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2342 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2343 245d91c1 2018-07-12 stsp err = NULL;
2344 245d91c1 2018-07-12 stsp blame->thread = NULL;
2346 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2347 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2348 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2350 245d91c1 2018-07-12 stsp if (blame->f) {
2351 245d91c1 2018-07-12 stsp fclose(blame->f);
2352 245d91c1 2018-07-12 stsp blame->f = NULL;
2354 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
2355 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
2356 245d91c1 2018-07-12 stsp free(blame->lines);
2357 245d91c1 2018-07-12 stsp blame->lines = NULL;
2358 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2359 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2361 245d91c1 2018-07-12 stsp return err;
2364 245d91c1 2018-07-12 stsp static const struct got_error *
2365 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2366 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
2367 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
2368 245d91c1 2018-07-12 stsp struct got_repository *repo)
2370 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2371 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2372 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2373 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2374 27d434c2 2018-09-15 stsp struct got_object *obj = NULL;
2375 1a76625f 2018-10-22 stsp int errcode;
2377 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2379 27d434c2 2018-09-15 stsp goto done;
2381 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
2383 84451b3e 2018-07-10 stsp goto done;
2385 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2386 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2387 84451b3e 2018-07-10 stsp goto done;
2390 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
2392 a70480e0 2018-06-23 stsp goto done;
2393 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2394 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2395 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2396 84451b3e 2018-07-10 stsp goto done;
2398 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2399 245d91c1 2018-07-12 stsp blame->f, blob);
2401 84451b3e 2018-07-10 stsp goto done;
2403 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2404 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2405 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2406 84451b3e 2018-07-10 stsp goto done;
2409 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2411 bd24772e 2018-07-11 stsp goto done;
2413 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2414 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2415 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2416 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2417 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2418 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2419 245d91c1 2018-07-12 stsp goto done;
2421 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
2422 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
2423 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
2424 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
2425 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
2426 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2427 e5a0f69f 2018-08-18 stsp blame->cb_args.eof = eof;
2429 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2430 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2431 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2432 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2433 245d91c1 2018-07-12 stsp *blame_complete = 0;
2435 1a76625f 2018-10-22 stsp errcode = pthread_create(&blame->thread, NULL, blame_thread,
2436 1a76625f 2018-10-22 stsp &blame->thread_args);
2437 1a76625f 2018-10-22 stsp if (errcode) {
2438 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2439 245d91c1 2018-07-12 stsp goto done;
2444 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2445 27d434c2 2018-09-15 stsp free(obj_id);
2447 245d91c1 2018-07-12 stsp got_object_close(obj);
2449 245d91c1 2018-07-12 stsp stop_blame(blame);
2450 245d91c1 2018-07-12 stsp return err;
2453 245d91c1 2018-07-12 stsp static const struct got_error *
2454 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2455 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2457 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2458 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2460 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2462 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2464 7cbe629d 2018-08-04 stsp return err;
2466 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2467 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2468 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2469 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2470 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2471 fb2756b9 2018-08-04 stsp s->path = path;
2472 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2473 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2474 fb2756b9 2018-08-04 stsp s->repo = repo;
2475 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2476 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2478 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2479 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2480 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2482 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
2483 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2484 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2485 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2488 e5a0f69f 2018-08-18 stsp static const struct got_error *
2489 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2491 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2492 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2494 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2495 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2497 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2498 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2499 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2500 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2501 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2504 e5a0f69f 2018-08-18 stsp free(s->path);
2506 e5a0f69f 2018-08-18 stsp return err;
2509 7cbe629d 2018-08-04 stsp static const struct got_error *
2510 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2512 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2513 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2515 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2516 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2517 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2518 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2520 669b5ffa 2018-10-07 stsp view_vborder(view);
2521 e5a0f69f 2018-08-18 stsp return err;
2524 e5a0f69f 2018-08-18 stsp static const struct got_error *
2525 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2526 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2528 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2529 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
2530 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2531 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2532 669b5ffa 2018-10-07 stsp int begin_x = 0;
2534 e5a0f69f 2018-08-18 stsp switch (ch) {
2536 e5a0f69f 2018-08-18 stsp s->done = 1;
2539 e5a0f69f 2018-08-18 stsp case KEY_UP:
2540 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2541 e5a0f69f 2018-08-18 stsp s->selected_line--;
2542 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2543 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2544 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2546 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2547 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2548 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2551 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2552 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2553 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2555 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2558 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2559 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2560 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2561 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2562 e5a0f69f 2018-08-18 stsp s->selected_line++;
2563 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2564 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2565 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2568 e5a0f69f 2018-08-18 stsp case 'p': {
2569 e5a0f69f 2018-08-18 stsp struct got_object_id *id;
2570 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2571 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2572 e5a0f69f 2018-08-18 stsp if (id == NULL || got_object_id_cmp(id,
2573 e5a0f69f 2018-08-18 stsp s->blamed_commit->id) == 0)
2575 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2576 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2577 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2580 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2582 e5a0f69f 2018-08-18 stsp if (ch == 'p' && pobj == NULL)
2584 e5a0f69f 2018-08-18 stsp s->done = 1;
2585 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2586 e5a0f69f 2018-08-18 stsp s->done = 0;
2587 e5a0f69f 2018-08-18 stsp if (thread_err)
2589 e5a0f69f 2018-08-18 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
2590 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2591 e5a0f69f 2018-08-18 stsp obj = NULL;
2592 e5a0f69f 2018-08-18 stsp if (pobj) {
2593 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2594 e5a0f69f 2018-08-18 stsp pobj = NULL;
2596 6402fb3c 2018-09-15 stsp err = got_object_qid_alloc(&s->blamed_commit, id);
2598 e5a0f69f 2018-08-18 stsp goto done;
2599 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2600 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2601 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2602 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2603 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2604 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2609 e5a0f69f 2018-08-18 stsp case 'B': {
2610 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2611 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2612 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2614 e5a0f69f 2018-08-18 stsp s->done = 1;
2615 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2616 e5a0f69f 2018-08-18 stsp s->done = 0;
2617 e5a0f69f 2018-08-18 stsp if (thread_err)
2619 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2620 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2621 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2622 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2623 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2624 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2625 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2626 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2631 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2632 e5a0f69f 2018-08-18 stsp case '\r':
2633 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2634 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2635 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2638 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2641 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
2642 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
2643 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2644 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2645 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2648 669b5ffa 2018-10-07 stsp err = open_diff_view(diff_view, pobj, obj, s->repo);
2649 e5a0f69f 2018-08-18 stsp if (err) {
2650 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2653 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
2654 669b5ffa 2018-10-07 stsp err = view_close_child(view);
2656 669b5ffa 2018-10-07 stsp return err;
2657 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
2658 669b5ffa 2018-10-07 stsp if (err) {
2659 669b5ffa 2018-10-07 stsp view_close(diff_view);
2662 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(diff_view)) {
2663 669b5ffa 2018-10-07 stsp *focus_view = diff_view;
2664 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
2667 669b5ffa 2018-10-07 stsp *new_view = diff_view;
2668 e5a0f69f 2018-08-18 stsp if (pobj) {
2669 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2670 e5a0f69f 2018-08-18 stsp pobj = NULL;
2672 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2673 e5a0f69f 2018-08-18 stsp obj = NULL;
2677 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2679 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2680 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2681 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2682 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2685 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2686 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2687 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2688 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2690 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2691 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2692 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2694 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2695 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2696 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2697 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2705 b880a918 2018-07-10 stsp got_object_close(pobj);
2706 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2709 a70480e0 2018-06-23 stsp static const struct got_error *
2710 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2712 a70480e0 2018-06-23 stsp const struct got_error *error;
2713 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2714 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2715 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2716 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2718 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2720 a70480e0 2018-06-23 stsp #ifndef PROFILE
2721 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2723 a70480e0 2018-06-23 stsp err(1, "pledge");
2726 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2727 a70480e0 2018-06-23 stsp switch (ch) {
2729 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2732 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2733 69069811 2018-08-02 stsp if (repo_path == NULL)
2734 69069811 2018-08-02 stsp err(1, "-r option");
2738 a70480e0 2018-06-23 stsp /* NOTREACHED */
2742 a70480e0 2018-06-23 stsp argc -= optind;
2743 a70480e0 2018-06-23 stsp argv += optind;
2745 69069811 2018-08-02 stsp if (argc == 1)
2746 69069811 2018-08-02 stsp path = argv[0];
2748 a70480e0 2018-06-23 stsp usage_blame();
2750 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2751 69069811 2018-08-02 stsp if (cwd == NULL) {
2752 69069811 2018-08-02 stsp error = got_error_from_errno();
2753 69069811 2018-08-02 stsp goto done;
2755 69069811 2018-08-02 stsp if (repo_path == NULL) {
2756 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2757 69069811 2018-08-02 stsp if (repo_path == NULL) {
2758 69069811 2018-08-02 stsp error = got_error_from_errno();
2759 69069811 2018-08-02 stsp goto done;
2764 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2765 a70480e0 2018-06-23 stsp if (error != NULL)
2766 66b4983c 2018-06-23 stsp return error;
2768 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2769 69069811 2018-08-02 stsp if (error != NULL)
2770 69069811 2018-08-02 stsp goto done;
2772 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2773 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2774 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2775 a70480e0 2018-06-23 stsp if (error != NULL)
2776 66b4983c 2018-06-23 stsp goto done;
2777 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2778 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2780 a70480e0 2018-06-23 stsp struct got_object *obj;
2781 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2782 a70480e0 2018-06-23 stsp if (error != NULL)
2783 66b4983c 2018-06-23 stsp goto done;
2784 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
2785 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2786 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2787 a19e88aa 2018-06-23 stsp got_object_close(obj);
2789 a19e88aa 2018-06-23 stsp if (error != NULL)
2790 a19e88aa 2018-06-23 stsp goto done;
2792 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2793 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2794 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2795 e1cd8fed 2018-08-01 stsp goto done;
2797 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2798 7cbe629d 2018-08-04 stsp if (error)
2799 7cbe629d 2018-08-04 stsp goto done;
2800 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2802 69069811 2018-08-02 stsp free(repo_path);
2803 69069811 2018-08-02 stsp free(cwd);
2804 a70480e0 2018-06-23 stsp free(commit_id);
2806 a70480e0 2018-06-23 stsp got_repo_close(repo);
2807 a70480e0 2018-06-23 stsp return error;
2810 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2811 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2812 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2813 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2814 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2815 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2816 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2818 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2819 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2820 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2821 ffd1d5e5 2018-06-23 stsp int width, n;
2823 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2825 f7d12f7e 2018-08-01 stsp werase(view->window);
2827 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2828 ffd1d5e5 2018-06-23 stsp return NULL;
2830 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2832 ffd1d5e5 2018-06-23 stsp return err;
2833 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2834 a3404814 2018-09-02 stsp wstandout(view->window);
2835 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2836 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2837 a3404814 2018-09-02 stsp wstandend(view->window);
2838 2550e4c3 2018-07-13 stsp free(wline);
2839 2550e4c3 2018-07-13 stsp wline = NULL;
2840 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2841 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2842 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2843 ffd1d5e5 2018-06-23 stsp return NULL;
2844 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2846 ce52c690 2018-06-23 stsp return err;
2847 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2848 2550e4c3 2018-07-13 stsp free(wline);
2849 2550e4c3 2018-07-13 stsp wline = NULL;
2850 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2851 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2852 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2853 ffd1d5e5 2018-06-23 stsp return NULL;
2854 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2855 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2856 a1eca9bb 2018-06-23 stsp return NULL;
2858 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2859 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2860 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2861 0cf4efb1 2018-09-29 stsp if (view->focussed)
2862 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2863 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2865 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2866 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
2867 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2868 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2869 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2870 ffd1d5e5 2018-06-23 stsp return NULL;
2874 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2875 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2878 ffd1d5e5 2018-06-23 stsp while (te) {
2879 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2881 1d13200f 2018-07-12 stsp if (show_ids) {
2882 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2884 1d13200f 2018-07-12 stsp return got_error_from_errno();
2886 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2887 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2888 1d13200f 2018-07-12 stsp free(id_str);
2889 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2891 1d13200f 2018-07-12 stsp free(id_str);
2892 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2893 ffd1d5e5 2018-06-23 stsp if (err) {
2894 ffd1d5e5 2018-06-23 stsp free(line);
2897 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2898 0cf4efb1 2018-09-29 stsp if (view->focussed)
2899 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2900 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2902 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2903 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2904 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2905 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
2906 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2907 ffd1d5e5 2018-06-23 stsp free(line);
2908 2550e4c3 2018-07-13 stsp free(wline);
2909 2550e4c3 2018-07-13 stsp wline = NULL;
2911 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2912 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2913 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2915 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2918 ffd1d5e5 2018-06-23 stsp return err;
2921 ffd1d5e5 2018-06-23 stsp static void
2922 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2923 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2925 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2928 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2931 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2932 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2933 ffd1d5e5 2018-06-23 stsp if (!isroot)
2934 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2938 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2939 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2940 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2941 ffd1d5e5 2018-06-23 stsp prev = te;
2942 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2944 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2945 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2947 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2948 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2951 ffd1d5e5 2018-06-23 stsp static void
2952 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2953 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2954 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2956 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2957 ffd1d5e5 2018-06-23 stsp int n = 0;
2959 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2962 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2963 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2965 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2966 ffd1d5e5 2018-06-23 stsp while (next) {
2967 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2968 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2970 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2974 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2975 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2976 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2978 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2979 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2980 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2982 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2983 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2985 ce52c690 2018-06-23 stsp len += strlen(te->name);
2987 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2988 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2989 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2991 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2992 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2993 d9765a41 2018-06-23 stsp while (pt) {
2994 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2995 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2996 cb2ebc8a 2018-06-23 stsp goto done;
2998 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2999 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3000 cb2ebc8a 2018-06-23 stsp goto done;
3002 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3005 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3006 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3007 ce52c690 2018-06-23 stsp goto done;
3011 ce52c690 2018-06-23 stsp if (err) {
3012 ce52c690 2018-06-23 stsp free(*path);
3013 ce52c690 2018-06-23 stsp *path = NULL;
3015 ce52c690 2018-06-23 stsp return err;
3018 ce52c690 2018-06-23 stsp static const struct got_error *
3019 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3020 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3021 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
3023 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3024 ce52c690 2018-06-23 stsp char *path;
3025 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3027 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3029 ce52c690 2018-06-23 stsp return err;
3031 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3032 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3033 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3035 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
3036 e5a0f69f 2018-08-18 stsp if (err) {
3037 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3038 e5a0f69f 2018-08-18 stsp free(path);
3040 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3041 69efd4c4 2018-07-18 stsp return err;
3044 69efd4c4 2018-07-18 stsp static const struct got_error *
3045 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3046 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3047 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
3049 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3050 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3051 69efd4c4 2018-07-18 stsp char *path;
3053 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3054 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3055 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3057 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3059 69efd4c4 2018-07-18 stsp return err;
3061 e5a0f69f 2018-08-18 stsp err = open_log_view(log_view, commit_id, repo, path);
3063 e5a0f69f 2018-08-18 stsp view_close(log_view);
3065 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3066 cb2ebc8a 2018-06-23 stsp free(path);
3067 cb2ebc8a 2018-06-23 stsp return err;
3070 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3071 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3072 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
3074 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3075 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3076 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3078 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3080 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3081 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3082 ffd1d5e5 2018-06-23 stsp goto done;
3084 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3085 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
3086 ffd1d5e5 2018-06-23 stsp goto done;
3089 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3090 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3091 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3092 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3093 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3094 6484ec90 2018-09-29 stsp err = got_error_from_errno();
3095 6484ec90 2018-09-29 stsp goto done;
3097 fb2756b9 2018-08-04 stsp s->repo = repo;
3099 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
3100 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
3101 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
3103 ad80ab7b 2018-08-04 stsp free(commit_id_str);
3104 6484ec90 2018-09-29 stsp if (err) {
3105 fb2756b9 2018-08-04 stsp free(s->tree_label);
3106 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3108 ad80ab7b 2018-08-04 stsp return err;
3111 e5a0f69f 2018-08-18 stsp static const struct got_error *
3112 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
3114 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3116 fb2756b9 2018-08-04 stsp free(s->tree_label);
3117 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3118 6484ec90 2018-09-29 stsp free(s->commit_id);
3119 6484ec90 2018-09-29 stsp s->commit_id = NULL;
3120 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
3121 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
3122 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
3123 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
3124 ad80ab7b 2018-08-04 stsp free(parent);
3127 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
3128 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
3129 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
3131 e5a0f69f 2018-08-18 stsp return NULL;
3134 ad80ab7b 2018-08-04 stsp static const struct got_error *
3135 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
3137 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
3138 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3139 e5a0f69f 2018-08-18 stsp char *parent_path;
3141 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
3143 e5a0f69f 2018-08-18 stsp return err;
3145 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
3146 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
3147 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3148 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
3149 e5a0f69f 2018-08-18 stsp free(parent_path);
3151 669b5ffa 2018-10-07 stsp view_vborder(view);
3152 e5a0f69f 2018-08-18 stsp return err;
3155 e5a0f69f 2018-08-18 stsp static const struct got_error *
3156 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3157 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3159 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3160 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
3161 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
3162 669b5ffa 2018-10-07 stsp int begin_x = 0;
3164 e5a0f69f 2018-08-18 stsp switch (ch) {
3166 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
3169 669b5ffa 2018-10-07 stsp if (!s->selected_entry)
3171 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
3172 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
3173 669b5ffa 2018-10-07 stsp err = log_tree_entry(&log_view, begin_x,
3174 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
3175 669b5ffa 2018-10-07 stsp s->commit_id, s->repo);
3176 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3177 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3179 669b5ffa 2018-10-07 stsp return err;
3180 669b5ffa 2018-10-07 stsp err = view_set_child(view, log_view);
3181 669b5ffa 2018-10-07 stsp if (err) {
3182 669b5ffa 2018-10-07 stsp view_close(log_view);
3185 669b5ffa 2018-10-07 stsp *focus_view = log_view;
3186 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3188 669b5ffa 2018-10-07 stsp *new_view = log_view;
3191 e5a0f69f 2018-08-18 stsp case KEY_UP:
3192 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3193 e5a0f69f 2018-08-18 stsp s->selected--;
3194 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3196 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
3197 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
3199 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
3200 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
3201 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
3202 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
3203 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3204 e5a0f69f 2018-08-18 stsp s->selected = 0;
3207 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
3208 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
3209 e5a0f69f 2018-08-18 stsp s->tree == s->root);
3212 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
3213 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
3214 e5a0f69f 2018-08-18 stsp s->selected++;
3217 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
3218 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
3220 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
3221 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
3222 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
3223 e5a0f69f 2018-08-18 stsp s->entries);
3224 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
3227 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
3228 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
3229 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3231 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
3232 e5a0f69f 2018-08-18 stsp case '\r':
3233 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
3234 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3235 7837eeac 2018-09-24 stsp case KEY_BACKSPACE:
3236 e5a0f69f 2018-08-18 stsp /* user selected '..' */
3237 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
3239 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
3240 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
3242 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
3243 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
3244 e5a0f69f 2018-08-18 stsp s->entries =
3245 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3246 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
3247 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
3248 e5a0f69f 2018-08-18 stsp s->selected_entry =
3249 e5a0f69f 2018-08-18 stsp parent->selected_entry;
3250 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
3251 e5a0f69f 2018-08-18 stsp free(parent);
3252 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
3253 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3254 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
3255 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
3256 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
3259 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
3260 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
3261 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
3264 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
3265 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
3266 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
3267 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
3268 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
3269 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3270 e5a0f69f 2018-08-18 stsp s->tree = child;
3271 e5a0f69f 2018-08-18 stsp s->entries =
3272 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3273 e5a0f69f 2018-08-18 stsp s->selected = 0;
3274 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3275 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
3276 669b5ffa 2018-10-07 stsp struct tog_view *blame_view;
3277 669b5ffa 2018-10-07 stsp int begin_x = view_is_parent_view(view) ?
3278 669b5ffa 2018-10-07 stsp view_split_begin_x(view->begin_x) : 0;
3280 669b5ffa 2018-10-07 stsp err = blame_tree_entry(&blame_view, begin_x,
3281 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents, s->commit_id,
3285 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3286 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3288 669b5ffa 2018-10-07 stsp return err;
3289 669b5ffa 2018-10-07 stsp err = view_set_child(view, blame_view);
3290 669b5ffa 2018-10-07 stsp if (err) {
3291 669b5ffa 2018-10-07 stsp view_close(blame_view);
3294 669b5ffa 2018-10-07 stsp *focus_view = blame_view;
3295 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3297 669b5ffa 2018-10-07 stsp *new_view = blame_view;
3300 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3301 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3302 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3308 ffd1d5e5 2018-06-23 stsp return err;
3311 ffd1d5e5 2018-06-23 stsp __dead static void
3312 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3315 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3316 ffd1d5e5 2018-06-23 stsp getprogname());
3320 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3321 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3323 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3324 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3325 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3326 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3327 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3328 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3329 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3331 5221c383 2018-08-01 stsp struct tog_view *view;
3333 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3334 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3336 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3339 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3340 ffd1d5e5 2018-06-23 stsp switch (ch) {
3342 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3346 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3350 ffd1d5e5 2018-06-23 stsp argc -= optind;
3351 ffd1d5e5 2018-06-23 stsp argv += optind;
3353 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3354 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
3355 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3356 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3357 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3358 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3359 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3360 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3362 ffd1d5e5 2018-06-23 stsp usage_log();
3364 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3365 ffd1d5e5 2018-06-23 stsp free(repo_path);
3366 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3367 ffd1d5e5 2018-06-23 stsp return error;
3369 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
3370 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3371 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3372 ffd1d5e5 2018-06-23 stsp goto done;
3374 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
3375 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3376 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
3377 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
3378 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
3379 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
3382 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3383 ffd1d5e5 2018-06-23 stsp goto done;
3385 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3386 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3387 ffd1d5e5 2018-06-23 stsp goto done;
3389 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3390 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3391 ffd1d5e5 2018-06-23 stsp goto done;
3393 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3394 5221c383 2018-08-01 stsp if (view == NULL) {
3395 5221c383 2018-08-01 stsp error = got_error_from_errno();
3396 5221c383 2018-08-01 stsp goto done;
3398 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3399 ad80ab7b 2018-08-04 stsp if (error)
3400 ad80ab7b 2018-08-04 stsp goto done;
3401 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3403 ffd1d5e5 2018-06-23 stsp free(commit_id);
3404 ffd1d5e5 2018-06-23 stsp if (commit)
3405 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3407 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3409 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3410 ffd1d5e5 2018-06-23 stsp return error;
3412 5c5136c5 2018-05-20 stsp static void
3413 9f7d7167 2018-04-29 stsp init_curses(void)
3415 9f7d7167 2018-04-29 stsp initscr();
3417 0833f15d 2018-10-22 stsp halfdelay(10);
3420 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3421 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3422 1f475ad8 2018-05-10 stsp curs_set(0);
3425 4ed7e80c 2018-05-20 stsp __dead static void
3426 9f7d7167 2018-04-29 stsp usage(void)
3430 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3431 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3432 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3433 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3434 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3439 c2301be8 2018-04-30 stsp static char **
3440 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3442 c2301be8 2018-04-30 stsp char **argv;
3443 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3445 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3446 c2301be8 2018-04-30 stsp if (argv == NULL)
3447 c2301be8 2018-04-30 stsp err(1, "calloc");
3448 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3449 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3450 c2301be8 2018-04-30 stsp err(1, "calloc");
3451 c2301be8 2018-04-30 stsp if (arg1) {
3452 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3453 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3454 c2301be8 2018-04-30 stsp err(1, "calloc");
3457 c2301be8 2018-04-30 stsp return argv;
3461 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3463 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3464 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3465 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3466 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3468 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
3470 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3471 9f7d7167 2018-04-29 stsp switch (ch) {
3473 9f7d7167 2018-04-29 stsp hflag = 1;
3477 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3481 9f7d7167 2018-04-29 stsp argc -= optind;
3482 9f7d7167 2018-04-29 stsp argv += optind;
3483 9f7d7167 2018-04-29 stsp optind = 0;
3484 c2301be8 2018-04-30 stsp optreset = 1;
3486 c2301be8 2018-04-30 stsp if (argc == 0) {
3487 f29d3e89 2018-06-23 stsp if (hflag)
3489 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3490 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3491 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3496 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3497 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3498 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3499 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3500 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3501 9f7d7167 2018-04-29 stsp if (hflag)
3502 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3506 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3507 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3508 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3509 c2301be8 2018-04-30 stsp if (repo_path) {
3510 c2301be8 2018-04-30 stsp struct got_repository *repo;
3511 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3512 c2301be8 2018-04-30 stsp if (error == NULL)
3513 c2301be8 2018-04-30 stsp got_repo_close(repo);
3515 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3516 c2301be8 2018-04-30 stsp if (error) {
3517 f29d3e89 2018-06-23 stsp if (hflag) {
3518 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3519 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3523 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3524 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3525 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3526 ad7de8d9 2018-04-30 stsp free(repo_path);
3529 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3530 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3532 c2301be8 2018-04-30 stsp free(repo_path);
3536 5c5136c5 2018-05-20 stsp init_curses();
3538 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3539 9f7d7167 2018-04-29 stsp if (error)
3540 9f7d7167 2018-04-29 stsp goto done;
3543 c2301be8 2018-04-30 stsp free(cmd_argv);
3544 9f7d7167 2018-04-29 stsp if (error)
3545 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);