2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
21 31120ada 2018-04-30 stsp #include <errno.h>
22 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
23 9f7d7167 2018-04-29 stsp #include <curses.h>
24 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
25 9f7d7167 2018-04-29 stsp #include <panel.h>
26 9f7d7167 2018-04-29 stsp #include <locale.h>
27 9f7d7167 2018-04-29 stsp #include <stdlib.h>
28 26ed57b2 2018-05-19 stsp #include <stdio.h>
29 9f7d7167 2018-04-29 stsp #include <getopt.h>
30 9f7d7167 2018-04-29 stsp #include <string.h>
31 9f7d7167 2018-04-29 stsp #include <err.h>
32 80ddbec8 2018-04-29 stsp #include <unistd.h>
33 26ed57b2 2018-05-19 stsp #include <util.h>
34 26ed57b2 2018-05-19 stsp #include <limits.h>
35 61e69b96 2018-05-20 stsp #include <wchar.h>
36 788c352e 2018-06-16 stsp #include <time.h>
37 84451b3e 2018-07-10 stsp #include <pthread.h>
38 5036bf37 2018-09-24 stsp #include <libgen.h>
39 60493ae3 2019-06-20 stsp #include <regex.h>
41 9f7d7167 2018-04-29 stsp #include "got_error.h"
42 80ddbec8 2018-04-29 stsp #include "got_object.h"
43 80ddbec8 2018-04-29 stsp #include "got_reference.h"
44 80ddbec8 2018-04-29 stsp #include "got_repository.h"
45 80ddbec8 2018-04-29 stsp #include "got_diff.h"
46 511a516b 2018-05-19 stsp #include "got_opentemp.h"
47 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
48 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
49 a70480e0 2018-06-23 stsp #include "got_blame.h"
50 c2db6724 2019-01-04 stsp #include "got_privsep.h"
51 1dd54920 2019-05-11 stsp #include "got_path.h"
52 b7165be3 2019-02-05 stsp #include "got_worktree.h"
55 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
64 9f7d7167 2018-04-29 stsp #ifndef nitems
65 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 9f7d7167 2018-04-29 stsp struct tog_cmd {
69 c2301be8 2018-04-30 stsp const char *name;
70 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
71 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
72 c2301be8 2018-04-30 stsp const char *descr;
75 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
76 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
77 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
78 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
79 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
81 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
82 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
83 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
84 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
86 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
87 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
88 9f7d7167 2018-04-29 stsp "show repository history" },
89 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
90 9f7d7167 2018-04-29 stsp "compare files and directories" },
91 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
92 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
93 ffd1d5e5 2018-06-23 stsp { "tree", cmd_tree, usage_tree,
94 ffd1d5e5 2018-06-23 stsp "browse trees in repository" },
97 d6b05b5b 2018-08-04 stsp enum tog_view_type {
98 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
99 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
100 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
101 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
104 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
106 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
107 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
108 ba4f502b 2018-08-04 stsp struct got_object_id *id;
109 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
112 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
113 ba4f502b 2018-08-04 stsp struct commit_queue {
114 ba4f502b 2018-08-04 stsp int ncommits;
115 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
118 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
119 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
121 15a087fe 2019-02-21 stsp int first_displayed_line;
122 15a087fe 2019-02-21 stsp int last_displayed_line;
124 15a087fe 2019-02-21 stsp int diff_context;
125 15a087fe 2019-02-21 stsp struct got_repository *repo;
126 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
128 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
129 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
132 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
134 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
135 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
136 1a76625f 2018-10-22 stsp int commits_needed;
137 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
138 1a76625f 2018-10-22 stsp struct commit_queue *commits;
139 1a76625f 2018-10-22 stsp const char *in_repo_path;
140 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
141 1a76625f 2018-10-22 stsp struct got_repository *repo;
142 1a76625f 2018-10-22 stsp int log_complete;
143 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
144 1a76625f 2018-10-22 stsp struct tog_view *view;
145 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
146 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
149 1a76625f 2018-10-22 stsp struct tog_log_view_state {
150 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
151 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
152 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
153 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
154 b01e7d3b 2018-08-04 stsp int selected;
155 b01e7d3b 2018-08-04 stsp char *in_repo_path;
156 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
157 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
158 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
159 1a76625f 2018-10-22 stsp sig_atomic_t quit;
160 1a76625f 2018-10-22 stsp pthread_t thread;
161 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
162 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
165 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
166 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
167 e9424729 2018-08-04 stsp int nlines;
169 e9424729 2018-08-04 stsp struct tog_view *view;
170 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
174 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
175 e9424729 2018-08-04 stsp const char *path;
176 e9424729 2018-08-04 stsp struct got_repository *repo;
177 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
178 e9424729 2018-08-04 stsp int *complete;
181 e9424729 2018-08-04 stsp struct tog_blame {
183 e9424729 2018-08-04 stsp size_t filesize;
184 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
185 6fcac457 2018-11-19 stsp int nlines;
186 e9424729 2018-08-04 stsp pthread_t thread;
187 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
188 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
189 e9424729 2018-08-04 stsp const char *path;
192 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
193 7cbe629d 2018-08-04 stsp int first_displayed_line;
194 7cbe629d 2018-08-04 stsp int last_displayed_line;
195 7cbe629d 2018-08-04 stsp int selected_line;
196 7cbe629d 2018-08-04 stsp int blame_complete;
199 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
200 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
201 e5a0f69f 2018-08-18 stsp char *path;
202 7cbe629d 2018-08-04 stsp struct got_repository *repo;
203 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
204 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
205 e9424729 2018-08-04 stsp struct tog_blame blame;
208 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
209 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
210 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
211 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
212 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
213 ad80ab7b 2018-08-04 stsp int selected;
216 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
218 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
219 ad80ab7b 2018-08-04 stsp char *tree_label;
220 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
221 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
222 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
223 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
224 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
225 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
226 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
227 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
228 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
229 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
230 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
234 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
236 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
237 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
238 669b5ffa 2018-10-07 stsp * there is enough screen estate.
240 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
241 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
243 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
244 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
245 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
247 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
248 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
250 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
252 cc3c9aac 2018-08-01 stsp struct tog_view {
253 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
254 26ed57b2 2018-05-19 stsp WINDOW *window;
255 26ed57b2 2018-05-19 stsp PANEL *panel;
256 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
257 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
258 1004088d 2018-09-29 stsp int focussed;
259 669b5ffa 2018-10-07 stsp struct tog_view *parent;
260 669b5ffa 2018-10-07 stsp struct tog_view *child;
261 669b5ffa 2018-10-07 stsp int child_focussed;
263 5dc9f4bc 2018-08-04 stsp /* type-specific state */
264 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
266 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
267 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
268 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
269 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
272 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
273 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
274 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
275 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
277 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
278 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
279 60493ae3 2019-06-20 stsp int searching;
280 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
281 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
282 60493ae3 2019-06-20 stsp int search_next_done;
283 1803e47f 2019-06-22 stsp regex_t regex;
286 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
287 fb872ab2 2019-02-21 stsp struct got_object_id *, struct got_object_id *, struct tog_view *,
288 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
289 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
290 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
291 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
292 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
294 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
295 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *,
296 8b473291 2019-02-21 stsp struct got_repository *, const char *, int);
297 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
298 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
299 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
300 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
301 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
302 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
304 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
305 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *, struct got_repository *);
306 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
307 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
308 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
309 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
311 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
312 8b473291 2019-02-21 stsp struct got_tree_object *, struct got_object_id *,
313 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
314 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
315 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
316 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
317 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
319 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
321 25791caa 2018-10-24 stsp static void
322 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
324 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
327 e5a0f69f 2018-08-18 stsp static const struct got_error *
328 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
330 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
332 669b5ffa 2018-10-07 stsp if (view->child) {
333 669b5ffa 2018-10-07 stsp view_close(view->child);
334 669b5ffa 2018-10-07 stsp view->child = NULL;
336 e5a0f69f 2018-08-18 stsp if (view->close)
337 e5a0f69f 2018-08-18 stsp err = view->close(view);
338 ea5e7bb5 2018-08-01 stsp if (view->panel)
339 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
340 ea5e7bb5 2018-08-01 stsp if (view->window)
341 ea5e7bb5 2018-08-01 stsp delwin(view->window);
342 ea5e7bb5 2018-08-01 stsp free(view);
343 e5a0f69f 2018-08-18 stsp return err;
346 ea5e7bb5 2018-08-01 stsp static struct tog_view *
347 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
348 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
350 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
352 ea5e7bb5 2018-08-01 stsp if (view == NULL)
353 ea5e7bb5 2018-08-01 stsp return NULL;
355 d6b05b5b 2018-08-04 stsp view->type = type;
356 f7d12f7e 2018-08-01 stsp view->lines = LINES;
357 f7d12f7e 2018-08-01 stsp view->cols = COLS;
358 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
359 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
360 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
361 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
362 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
363 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
364 96a765a8 2018-08-04 stsp view_close(view);
365 ea5e7bb5 2018-08-01 stsp return NULL;
367 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
368 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
369 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
370 96a765a8 2018-08-04 stsp view_close(view);
371 ea5e7bb5 2018-08-01 stsp return NULL;
374 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
375 ea5e7bb5 2018-08-01 stsp return view;
379 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
381 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
383 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
386 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
388 5c60c32a 2018-10-18 stsp static const struct got_error *
389 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
391 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
393 5c60c32a 2018-10-18 stsp view->begin_y = 0;
394 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
395 5c60c32a 2018-10-18 stsp view->nlines = LINES;
396 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
397 5c60c32a 2018-10-18 stsp view->lines = LINES;
398 5c60c32a 2018-10-18 stsp view->cols = COLS;
399 5c60c32a 2018-10-18 stsp err = view_resize(view);
401 5c60c32a 2018-10-18 stsp return err;
403 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
404 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
406 5c60c32a 2018-10-18 stsp return NULL;
409 5c60c32a 2018-10-18 stsp static const struct got_error *
410 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
412 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
414 5c60c32a 2018-10-18 stsp view->begin_x = 0;
415 5c60c32a 2018-10-18 stsp view->begin_y = 0;
416 5c60c32a 2018-10-18 stsp view->nlines = LINES;
417 5c60c32a 2018-10-18 stsp view->ncols = COLS;
418 5c60c32a 2018-10-18 stsp view->lines = LINES;
419 5c60c32a 2018-10-18 stsp view->cols = COLS;
420 5c60c32a 2018-10-18 stsp err = view_resize(view);
422 5c60c32a 2018-10-18 stsp return err;
424 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
425 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
427 5c60c32a 2018-10-18 stsp return NULL;
431 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
433 5c60c32a 2018-10-18 stsp return view->parent == NULL;
436 4d8c2215 2018-08-19 stsp static const struct got_error *
437 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
439 f7d12f7e 2018-08-01 stsp int nlines, ncols;
441 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
442 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
444 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
446 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
447 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
449 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
451 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
452 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
453 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
454 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
455 25791caa 2018-10-24 stsp wclear(view->window);
457 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
458 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
459 0cf4efb1 2018-09-29 stsp view->lines = LINES;
460 0cf4efb1 2018-09-29 stsp view->cols = COLS;
462 6e3e5d9c 2018-10-18 stsp if (view->child) {
463 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
464 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
465 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
466 5c60c32a 2018-10-18 stsp if (view->child->focussed)
467 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
469 5c60c32a 2018-10-18 stsp show_panel(view->panel);
471 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
472 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
476 5c60c32a 2018-10-18 stsp return NULL;
479 669b5ffa 2018-10-07 stsp static const struct got_error *
480 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
482 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
484 669b5ffa 2018-10-07 stsp if (view->child == NULL)
485 669b5ffa 2018-10-07 stsp return NULL;
487 669b5ffa 2018-10-07 stsp err = view_close(view->child);
488 669b5ffa 2018-10-07 stsp view->child = NULL;
489 669b5ffa 2018-10-07 stsp return err;
492 669b5ffa 2018-10-07 stsp static const struct got_error *
493 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
495 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
497 669b5ffa 2018-10-07 stsp view->child = child;
498 669b5ffa 2018-10-07 stsp child->parent = view;
499 669b5ffa 2018-10-07 stsp return err;
503 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
505 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
508 34bc9ec9 2019-02-22 stsp static void
509 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
511 25791caa 2018-10-24 stsp int cols, lines;
512 25791caa 2018-10-24 stsp struct winsize size;
514 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
515 25791caa 2018-10-24 stsp cols = 80; /* Default */
516 25791caa 2018-10-24 stsp lines = 24;
518 25791caa 2018-10-24 stsp cols = size.ws_col;
519 25791caa 2018-10-24 stsp lines = size.ws_row;
521 25791caa 2018-10-24 stsp resize_term(lines, cols);
524 0cf4efb1 2018-09-29 stsp static const struct got_error *
525 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
526 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
527 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
529 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
530 669b5ffa 2018-10-07 stsp struct tog_view *v;
531 1a76625f 2018-10-22 stsp int ch, errcode;
533 e5a0f69f 2018-08-18 stsp *new = NULL;
534 e5a0f69f 2018-08-18 stsp *dead = NULL;
535 0cf4efb1 2018-09-29 stsp *focus = NULL;
537 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
538 60493ae3 2019-06-20 stsp errcode = pthread_mutex_unlock(&tog_mutex);
539 60493ae3 2019-06-20 stsp if (errcode)
540 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
541 60493ae3 2019-06-20 stsp "pthread_mutex_unlock");
542 60493ae3 2019-06-20 stsp pthread_yield();
543 60493ae3 2019-06-20 stsp errcode = pthread_mutex_lock(&tog_mutex);
544 60493ae3 2019-06-20 stsp if (errcode)
545 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
546 60493ae3 2019-06-20 stsp "pthread_mutex_lock");
547 60493ae3 2019-06-20 stsp view->search_next(view);
548 60493ae3 2019-06-20 stsp return NULL;
551 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
552 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
553 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
554 1a76625f 2018-10-22 stsp if (errcode)
555 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
556 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
557 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
558 1a76625f 2018-10-22 stsp if (errcode)
559 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
560 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
562 25791caa 2018-10-24 stsp if (tog_sigwinch_received) {
563 25791caa 2018-10-24 stsp tog_resizeterm();
564 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
565 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
566 25791caa 2018-10-24 stsp err = view_resize(v);
568 25791caa 2018-10-24 stsp return err;
569 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
571 25791caa 2018-10-24 stsp return err;
575 e5a0f69f 2018-08-18 stsp switch (ch) {
579 1e37a5c2 2019-05-12 jcs if (view->child) {
580 1e37a5c2 2019-05-12 jcs *focus = view->child;
581 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
582 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
583 1e37a5c2 2019-05-12 jcs *focus = view->parent;
584 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
588 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
589 1e37a5c2 2019-05-12 jcs *dead = view;
595 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
596 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
598 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
599 669b5ffa 2018-10-07 stsp *focus = view->child;
600 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
601 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
603 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
606 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
607 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
609 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
610 1e37a5c2 2019-05-12 jcs *focus = view;
611 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
612 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
614 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
618 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
619 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
622 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
625 60493ae3 2019-06-20 stsp if (view->search_start)
626 60493ae3 2019-06-20 stsp view->search_start(view);
628 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
632 60493ae3 2019-06-20 stsp if (view->search_next && view->searching) {
633 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
634 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
635 60493ae3 2019-06-20 stsp view->search_next_done = 0;
636 60493ae3 2019-06-20 stsp view->search_next(view);
638 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
641 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
645 e5a0f69f 2018-08-18 stsp return err;
649 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
651 0cf4efb1 2018-09-29 stsp PANEL *panel;
652 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
654 669b5ffa 2018-10-07 stsp if (view->parent)
655 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
657 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
658 0cf4efb1 2018-09-29 stsp if (panel == NULL)
661 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
662 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
663 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
667 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
669 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
670 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
672 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
674 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
677 669b5ffa 2018-10-07 stsp return view->focussed;
680 bcbd79e2 2018-08-19 stsp static const struct got_error *
681 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
683 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
684 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
685 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
686 fd823528 2018-10-22 stsp int fast_refresh = 10;
687 1a76625f 2018-10-22 stsp int done = 0, errcode;
689 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
690 1a76625f 2018-10-22 stsp if (errcode)
691 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
693 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
694 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
696 a81bf10d 2018-09-29 stsp main_view = view;
697 1004088d 2018-09-29 stsp view->focussed = 1;
698 878940b7 2018-09-29 stsp err = view->show(view);
700 0cf4efb1 2018-09-29 stsp return err;
701 0cf4efb1 2018-09-29 stsp update_panels();
702 0cf4efb1 2018-09-29 stsp doupdate();
703 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
704 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
705 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
706 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
708 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
709 e4197bf9 2018-08-18 stsp view, &views);
712 e5a0f69f 2018-08-18 stsp if (dead_view) {
713 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
715 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
716 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
717 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
718 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
719 669b5ffa 2018-10-07 stsp prev = view->parent;
721 669b5ffa 2018-10-07 stsp if (dead_view->parent)
722 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
724 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
726 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
727 a81bf10d 2018-09-29 stsp if (err || dead_view == main_view)
730 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
731 0cf4efb1 2018-09-29 stsp if (focus_view)
732 0cf4efb1 2018-09-29 stsp view = focus_view;
733 669b5ffa 2018-10-07 stsp else if (prev)
734 669b5ffa 2018-10-07 stsp view = prev;
735 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
736 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
737 0cf4efb1 2018-09-29 stsp tog_view_list_head);
739 0cf4efb1 2018-09-29 stsp view = NULL;
740 669b5ffa 2018-10-07 stsp if (view) {
741 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
742 669b5ffa 2018-10-07 stsp focus_view = view->child;
744 669b5ffa 2018-10-07 stsp focus_view = view;
748 bcbd79e2 2018-08-19 stsp if (new_view) {
749 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
750 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
751 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
752 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
754 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
755 86c66b02 2018-10-18 stsp err = view_close(v);
760 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
761 fed7eaa8 2018-10-24 stsp view = new_view;
762 878940b7 2018-09-29 stsp if (focus_view == NULL)
763 878940b7 2018-09-29 stsp focus_view = new_view;
765 0cf4efb1 2018-09-29 stsp if (focus_view) {
766 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
768 0cf4efb1 2018-09-29 stsp view->focussed = 0;
769 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
770 0cf4efb1 2018-09-29 stsp view = focus_view;
771 878940b7 2018-09-29 stsp if (new_view)
772 878940b7 2018-09-29 stsp show_panel(new_view->panel);
773 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
774 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
776 669b5ffa 2018-10-07 stsp if (view) {
777 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
778 758194b5 2018-10-24 stsp view->focussed = 1;
779 758194b5 2018-10-24 stsp show_panel(view->panel);
780 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
781 758194b5 2018-10-24 stsp show_panel(view->child->panel);
782 1a76625f 2018-10-22 stsp focus_view = view;
784 669b5ffa 2018-10-07 stsp if (view->parent) {
785 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
789 669b5ffa 2018-10-07 stsp err = view->show(view);
792 669b5ffa 2018-10-07 stsp if (view->child) {
793 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
797 1a76625f 2018-10-22 stsp update_panels();
798 1a76625f 2018-10-22 stsp doupdate();
802 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
803 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
804 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
805 e5a0f69f 2018-08-18 stsp view_close(view);
808 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
809 1a76625f 2018-10-22 stsp if (errcode)
810 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
812 e5a0f69f 2018-08-18 stsp return err;
815 4ed7e80c 2018-05-20 stsp __dead static void
816 9f7d7167 2018-04-29 stsp usage_log(void)
819 c70c5802 2018-08-01 stsp fprintf(stderr,
820 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
821 9f7d7167 2018-04-29 stsp getprogname());
825 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
826 80ddbec8 2018-04-29 stsp static const struct got_error *
827 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
829 00dfcb92 2018-06-11 stsp char *vis = NULL;
830 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
832 963b370f 2018-05-20 stsp *ws = NULL;
833 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
834 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
835 00dfcb92 2018-06-11 stsp int vislen;
836 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
837 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
839 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
840 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
842 00dfcb92 2018-06-11 stsp return err;
843 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
844 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
845 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
850 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
851 a7f50699 2018-06-11 stsp if (*ws == NULL) {
852 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
856 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
857 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
862 963b370f 2018-05-20 stsp *ws = NULL;
865 963b370f 2018-05-20 stsp return err;
868 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
869 963b370f 2018-05-20 stsp static const struct got_error *
870 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
872 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
873 963b370f 2018-05-20 stsp int cols = 0;
874 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
875 963b370f 2018-05-20 stsp size_t wlen;
878 963b370f 2018-05-20 stsp *wlinep = NULL;
879 b700b5d6 2018-07-10 stsp *widthp = 0;
881 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
883 963b370f 2018-05-20 stsp return err;
886 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
887 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
888 963b370f 2018-05-20 stsp switch (width) {
894 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
895 b700b5d6 2018-07-10 stsp cols += width;
899 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
900 67ceb59d 2019-03-03 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
904 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
908 963b370f 2018-05-20 stsp wline[i] = L'\0';
909 b700b5d6 2018-07-10 stsp if (widthp)
910 b700b5d6 2018-07-10 stsp *widthp = cols;
913 963b370f 2018-05-20 stsp free(wline);
915 963b370f 2018-05-20 stsp *wlinep = wline;
916 963b370f 2018-05-20 stsp return err;
919 8b473291 2019-02-21 stsp static const struct got_error*
920 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
921 8b473291 2019-02-21 stsp struct got_object_id *id)
923 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
924 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
926 8b473291 2019-02-21 stsp const char *name;
928 8b473291 2019-02-21 stsp *refs_str = NULL;
930 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
931 8b473291 2019-02-21 stsp if (got_object_id_cmp(re->id, id) != 0)
933 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
934 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
936 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
938 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
940 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
942 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
944 8b473291 2019-02-21 stsp s = *refs_str;
945 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
946 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
947 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
949 8b473291 2019-02-21 stsp *refs_str = NULL;
955 8b473291 2019-02-21 stsp return err;
958 963b370f 2018-05-20 stsp static const struct got_error *
959 5813d178 2019-03-09 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
961 5813d178 2019-03-09 stsp char *smallerthan, *at;
963 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
964 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
965 5813d178 2019-03-09 stsp author = smallerthan + 1;
966 5813d178 2019-03-09 stsp at = strchr(author, '@');
968 5813d178 2019-03-09 stsp *at = '\0';
969 5813d178 2019-03-09 stsp return format_line(wauthor, author_width, author, limit);
972 5813d178 2019-03-09 stsp static const struct got_error *
973 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
974 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
975 5813d178 2019-03-09 stsp int author_display_cols)
977 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
978 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
979 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
980 5813d178 2019-03-09 stsp char *author = NULL;
981 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
982 bb737323 2018-05-20 stsp int author_width, logmsg_width;
983 5813d178 2019-03-09 stsp char *newline, *line = NULL;
984 bb737323 2018-05-20 stsp int col, limit;
985 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
986 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
987 ccb26ccd 2018-11-05 stsp struct tm tm;
988 45d799e2 2018-12-23 stsp time_t committer_time;
990 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
991 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
992 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
993 ccb26ccd 2018-11-05 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
994 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
995 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
997 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
998 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1000 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1001 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1002 b39d25c7 2018-07-10 stsp col = limit + 1;
1003 b39d25c7 2018-07-10 stsp if (col > avail)
1004 b39d25c7 2018-07-10 stsp goto done;
1006 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1007 5813d178 2019-03-09 stsp if (author == NULL) {
1008 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1009 80ddbec8 2018-04-29 stsp goto done;
1011 5813d178 2019-03-09 stsp err = format_author(&wauthor, &author_width, author, avail - col);
1013 bb737323 2018-05-20 stsp goto done;
1014 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1015 bb737323 2018-05-20 stsp col += author_width;
1016 5813d178 2019-03-09 stsp while (col <= avail && author_width < author_display_cols + 2) {
1017 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1019 bb737323 2018-05-20 stsp author_width++;
1021 9c2eaf34 2018-05-20 stsp if (col > avail)
1022 9c2eaf34 2018-05-20 stsp goto done;
1024 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1025 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
1026 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1027 6d9fbc00 2018-04-29 stsp goto done;
1029 bb737323 2018-05-20 stsp logmsg = logmsg0;
1030 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1032 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1033 bb737323 2018-05-20 stsp if (newline)
1034 bb737323 2018-05-20 stsp *newline = '\0';
1035 bb737323 2018-05-20 stsp limit = avail - col;
1036 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1038 bb737323 2018-05-20 stsp goto done;
1039 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1040 bb737323 2018-05-20 stsp col += logmsg_width;
1041 bb737323 2018-05-20 stsp while (col <= avail) {
1042 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1046 80ddbec8 2018-04-29 stsp free(logmsg0);
1047 bb737323 2018-05-20 stsp free(wlogmsg);
1048 5813d178 2019-03-09 stsp free(author);
1049 bb737323 2018-05-20 stsp free(wauthor);
1050 80ddbec8 2018-04-29 stsp free(line);
1051 80ddbec8 2018-04-29 stsp return err;
1054 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1055 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1056 899d86c2 2018-05-10 stsp struct got_object_id *id)
1058 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1060 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1061 80ddbec8 2018-04-29 stsp if (entry == NULL)
1062 899d86c2 2018-05-10 stsp return NULL;
1064 899d86c2 2018-05-10 stsp entry->id = id;
1065 99db9666 2018-05-07 stsp entry->commit = commit;
1066 899d86c2 2018-05-10 stsp return entry;
1069 99db9666 2018-05-07 stsp static void
1070 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1072 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1074 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1075 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1076 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1077 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1078 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1079 99db9666 2018-05-07 stsp free(entry);
1082 99db9666 2018-05-07 stsp static void
1083 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1085 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1086 99db9666 2018-05-07 stsp pop_commit(commits);
1089 c4972b91 2018-05-07 stsp static const struct got_error *
1090 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1091 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
1093 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1094 93e45b7c 2018-09-24 stsp int nqueued = 0;
1097 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1098 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1099 1a76625f 2018-10-22 stsp * while updating the display.
1101 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
1102 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1103 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1104 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1105 1a76625f 2018-10-22 stsp int errcode;
1107 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1108 9ba79e04 2018-06-11 stsp if (err) {
1109 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1111 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
1112 93e45b7c 2018-09-24 stsp minqueue, repo);
1114 ecb28ae0 2018-07-16 stsp return err;
1118 ecb28ae0 2018-07-16 stsp if (id == NULL)
1121 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1124 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1125 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1126 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1130 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1131 1a76625f 2018-10-22 stsp if (errcode) {
1132 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_lock");
1136 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1137 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1138 ecb28ae0 2018-07-16 stsp nqueued++;
1139 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1141 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1142 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1143 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1144 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1147 9ba79e04 2018-06-11 stsp return err;
1150 99db9666 2018-05-07 stsp static const struct got_error *
1151 19e70ad6 2019-05-14 stsp get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1152 19e70ad6 2019-05-14 stsp struct got_repository *repo)
1154 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1155 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1157 9ba79e04 2018-06-11 stsp *head_id = NULL;
1159 19e70ad6 2019-05-14 stsp err = got_ref_open(&head_ref, repo, branch_name, 0);
1161 99db9666 2018-05-07 stsp return err;
1163 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1164 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1165 9ba79e04 2018-06-11 stsp if (err) {
1166 9ba79e04 2018-06-11 stsp *head_id = NULL;
1167 99db9666 2018-05-07 stsp return err;
1170 9ba79e04 2018-06-11 stsp return NULL;
1173 0553a4e3 2018-04-30 stsp static const struct got_error *
1174 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1175 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1176 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1177 8b473291 2019-02-21 stsp struct got_reflist_head *refs, const char *path, int commits_needed)
1179 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1180 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1181 60493ae3 2019-06-20 stsp int width;
1182 60493ae3 2019-06-20 stsp int ncommits, author_cols = 10;
1183 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1184 8b473291 2019-02-21 stsp char *refs_str = NULL;
1185 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1187 e0d42f60 2018-07-22 stsp entry = first;
1188 e0d42f60 2018-07-22 stsp ncommits = 0;
1189 e0d42f60 2018-07-22 stsp while (entry) {
1190 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1191 e0d42f60 2018-07-22 stsp *selected = entry;
1194 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1195 e0d42f60 2018-07-22 stsp ncommits++;
1198 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1199 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1201 ecb28ae0 2018-07-16 stsp return err;
1202 8b473291 2019-02-21 stsp if (refs) {
1203 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, (*selected)->id);
1205 8b473291 2019-02-21 stsp goto done;
1209 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1210 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1212 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1213 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1214 60493ae3 2019-06-20 stsp commits_needed > 0 ?
1215 60493ae3 2019-06-20 stsp (view->searching && view->search_next_done == 0
1216 60493ae3 2019-06-20 stsp ? "searching..." : "loading... ") :
1217 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1218 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1219 8b473291 2019-02-21 stsp goto done;
1222 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1223 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1224 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1225 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1226 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1227 1a76625f 2018-10-22 stsp header = NULL;
1228 1a76625f 2018-10-22 stsp goto done;
1230 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1231 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1232 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1233 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1234 1a76625f 2018-10-22 stsp header = NULL;
1235 1a76625f 2018-10-22 stsp goto done;
1237 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1239 1a76625f 2018-10-22 stsp goto done;
1241 2814baeb 2018-08-01 stsp werase(view->window);
1243 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1244 a3404814 2018-09-02 stsp wstandout(view->window);
1245 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1246 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1247 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1250 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1251 a3404814 2018-09-02 stsp wstandend(view->window);
1252 ecb28ae0 2018-07-16 stsp free(wline);
1253 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1254 1a76625f 2018-10-22 stsp goto done;
1256 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1257 899d86c2 2018-05-10 stsp entry = first;
1258 5813d178 2019-03-09 stsp ncommits = 0;
1259 5813d178 2019-03-09 stsp while (entry) {
1260 5813d178 2019-03-09 stsp char *author;
1261 5813d178 2019-03-09 stsp wchar_t *wauthor;
1262 5813d178 2019-03-09 stsp int width;
1263 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1265 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1266 5813d178 2019-03-09 stsp if (author == NULL) {
1267 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1268 5813d178 2019-03-09 stsp goto done;
1270 5813d178 2019-03-09 stsp err = format_author(&wauthor, &width, author, COLS);
1271 5813d178 2019-03-09 stsp if (author_cols < width)
1272 5813d178 2019-03-09 stsp author_cols = width;
1273 5813d178 2019-03-09 stsp free(wauthor);
1274 5813d178 2019-03-09 stsp free(author);
1275 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1278 5813d178 2019-03-09 stsp entry = first;
1279 899d86c2 2018-05-10 stsp *last = first;
1280 867c6645 2018-07-10 stsp ncommits = 0;
1281 899d86c2 2018-05-10 stsp while (entry) {
1282 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1284 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1285 2814baeb 2018-08-01 stsp wstandout(view->window);
1286 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1287 5813d178 2019-03-09 stsp author_cols);
1288 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1289 2814baeb 2018-08-01 stsp wstandend(view->window);
1291 60493ae3 2019-06-20 stsp goto done;
1292 0553a4e3 2018-04-30 stsp ncommits++;
1293 899d86c2 2018-05-10 stsp *last = entry;
1294 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1297 1a57306a 2018-09-02 stsp view_vborder(view);
1299 1a76625f 2018-10-22 stsp free(id_str);
1300 8b473291 2019-02-21 stsp free(refs_str);
1301 1a76625f 2018-10-22 stsp free(ncommits_str);
1302 1a76625f 2018-10-22 stsp free(header);
1303 80ddbec8 2018-04-29 stsp return err;
1306 07b55e75 2018-05-10 stsp static void
1307 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1308 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1309 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1311 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1312 07b55e75 2018-05-10 stsp int nscrolled = 0;
1314 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1315 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1318 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1319 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1320 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1321 07b55e75 2018-05-10 stsp if (entry) {
1322 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1323 07b55e75 2018-05-10 stsp nscrolled++;
1328 aa075928 2018-05-10 stsp static const struct got_error *
1329 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1330 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1332 5e224a3e 2019-02-22 stsp int errcode;
1333 8a42fca8 2019-02-22 stsp int max_wait = 20;
1335 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1337 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1338 5e224a3e 2019-02-22 stsp if (*log_complete)
1341 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1342 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1343 7aafa0d1 2019-02-22 stsp if (errcode)
1344 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1345 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1346 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1347 7aafa0d1 2019-02-22 stsp if (errcode)
1348 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1349 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1350 7aafa0d1 2019-02-22 stsp pthread_yield();
1351 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1352 7aafa0d1 2019-02-22 stsp if (errcode)
1353 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1354 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1356 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1358 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1359 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1360 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1361 5e224a3e 2019-02-22 stsp * with few commits in history.
1363 7aafa0d1 2019-02-22 stsp return NULL;
1367 5e224a3e 2019-02-22 stsp return NULL;
1370 5e224a3e 2019-02-22 stsp static const struct got_error *
1371 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1372 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1373 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1374 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1375 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1377 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1378 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1379 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1381 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1382 5e224a3e 2019-02-22 stsp return NULL;
1384 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1385 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1387 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1388 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1390 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1391 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1392 5e224a3e 2019-02-22 stsp need_commits);
1394 5e224a3e 2019-02-22 stsp return err;
1398 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1399 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1402 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1404 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1405 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1407 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1408 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1410 dd0a52c1 2018-05-20 stsp return err;
1413 cd0acaa7 2018-05-20 stsp static const struct got_error *
1414 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1415 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1416 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1417 8b473291 2019-02-21 stsp struct got_repository *repo)
1419 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1420 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1421 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1423 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1424 15a94983 2018-12-23 stsp if (diff_view == NULL)
1425 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1427 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1428 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1429 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1430 e5a0f69f 2018-08-18 stsp if (err == NULL)
1431 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1432 cd0acaa7 2018-05-20 stsp return err;
1435 80ddbec8 2018-04-29 stsp static const struct got_error *
1436 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1437 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1439 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1441 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1442 941e9f74 2019-05-21 stsp if (parent == NULL)
1443 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1445 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1446 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1447 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1448 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1449 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1450 941e9f74 2019-05-21 stsp s->tree = subtree;
1451 941e9f74 2019-05-21 stsp s->entries = got_object_tree_get_entries(s->tree);
1452 941e9f74 2019-05-21 stsp s->selected = 0;
1453 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1454 941e9f74 2019-05-21 stsp return NULL;
1458 941e9f74 2019-05-21 stsp static const struct got_error *
1459 941e9f74 2019-05-21 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1460 941e9f74 2019-05-21 stsp struct commit_queue_entry *entry, const char *path,
1461 941e9f74 2019-05-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
1463 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1464 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1465 941e9f74 2019-05-21 stsp struct got_tree_entry *te;
1466 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s;
1467 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1468 b03c880f 2019-05-21 stsp char *slash, *subpath = NULL;
1469 941e9f74 2019-05-21 stsp const char *p;
1471 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1472 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1474 9343a5fb 2018-06-23 stsp return err;
1476 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1477 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1478 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1480 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1481 941e9f74 2019-05-21 stsp if (err) {
1482 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1483 941e9f74 2019-05-21 stsp return err;
1485 941e9f74 2019-05-21 stsp s = &tree_view->state.tree;
1487 941e9f74 2019-05-21 stsp *new_view = tree_view;
1489 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1491 941e9f74 2019-05-21 stsp while (*p) {
1492 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1494 941e9f74 2019-05-21 stsp while (p[0] == '/')
1497 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1498 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1499 941e9f74 2019-05-21 stsp if (slash == NULL)
1500 941e9f74 2019-05-21 stsp slash = strchr(p, '\0');
1501 941e9f74 2019-05-21 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1502 941e9f74 2019-05-21 stsp if (strncmp(p, te->name, slash - p) == 0) {
1503 941e9f74 2019-05-21 stsp s->selected_entry = te;
1506 b03c880f 2019-05-21 stsp s->selected++;
1508 941e9f74 2019-05-21 stsp if (s->selected_entry == NULL) {
1509 941e9f74 2019-05-21 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1512 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1513 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1515 67409a31 2019-05-24 stsp if (!S_ISDIR(s->selected_entry->mode)) {
1516 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1517 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1518 67409a31 2019-05-24 stsp s->selected = 0;
1522 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1523 941e9f74 2019-05-21 stsp if (slash)
1524 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1526 941e9f74 2019-05-21 stsp subpath = strdup(path);
1527 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1528 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1532 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1537 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1538 941e9f74 2019-05-21 stsp free(tree_id);
1542 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1543 941e9f74 2019-05-21 stsp if (err) {
1544 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1547 941e9f74 2019-05-21 stsp if (slash == NULL)
1549 941e9f74 2019-05-21 stsp free(subpath);
1550 941e9f74 2019-05-21 stsp subpath = NULL;
1551 941e9f74 2019-05-21 stsp p = slash;
1554 941e9f74 2019-05-21 stsp free(subpath);
1555 1a76625f 2018-10-22 stsp return err;
1558 1a76625f 2018-10-22 stsp static void *
1559 1a76625f 2018-10-22 stsp log_thread(void *arg)
1561 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1562 1a76625f 2018-10-22 stsp int errcode = 0;
1563 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1564 1a76625f 2018-10-22 stsp int done = 0;
1566 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1568 1a76625f 2018-10-22 stsp return (void *)err;
1570 1a76625f 2018-10-22 stsp while (!done && !err) {
1571 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1572 1a76625f 2018-10-22 stsp a->in_repo_path);
1573 1a76625f 2018-10-22 stsp if (err) {
1574 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1575 1a76625f 2018-10-22 stsp return (void *)err;
1576 1a76625f 2018-10-22 stsp err = NULL;
1578 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1579 1a76625f 2018-10-22 stsp a->commits_needed--;
1581 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1582 3abe8080 2019-04-10 stsp if (errcode) {
1583 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1584 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1586 3abe8080 2019-04-10 stsp } else if (*a->quit)
1588 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1589 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1590 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1591 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1595 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1596 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1597 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1598 1a76625f 2018-10-22 stsp &tog_mutex);
1599 1a76625f 2018-10-22 stsp if (errcode)
1600 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1601 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1604 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1605 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1606 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1607 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1609 3abe8080 2019-04-10 stsp a->log_complete = 1;
1610 1a76625f 2018-10-22 stsp return (void *)err;
1613 1a76625f 2018-10-22 stsp static const struct got_error *
1614 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1616 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1617 1a76625f 2018-10-22 stsp int errcode;
1619 1a76625f 2018-10-22 stsp if (s->thread) {
1620 1a76625f 2018-10-22 stsp s->quit = 1;
1621 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1622 1a76625f 2018-10-22 stsp if (errcode)
1623 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1624 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1625 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1626 1a76625f 2018-10-22 stsp if (errcode)
1627 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1628 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1629 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1630 1a76625f 2018-10-22 stsp if (errcode)
1631 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1632 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1633 1a76625f 2018-10-22 stsp if (errcode)
1634 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1635 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1636 1a76625f 2018-10-22 stsp s->thread = NULL;
1639 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1640 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1641 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1643 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1644 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1645 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1648 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1649 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1650 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1653 9343a5fb 2018-06-23 stsp return err;
1656 9343a5fb 2018-06-23 stsp static const struct got_error *
1657 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1659 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1660 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1662 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1663 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1664 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1665 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1666 1a76625f 2018-10-22 stsp free(s->start_id);
1667 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1668 1a76625f 2018-10-22 stsp return err;
1671 1a76625f 2018-10-22 stsp static const struct got_error *
1672 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
1674 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1675 60493ae3 2019-06-20 stsp char pattern[1024];
1678 60493ae3 2019-06-20 stsp if (view->nlines < 1)
1679 60493ae3 2019-06-20 stsp return NULL;
1681 60493ae3 2019-06-20 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1,
1682 60493ae3 2019-06-20 stsp view->begin_x, "/");
1683 60493ae3 2019-06-20 stsp wclrtoeol(view->window);
1685 60493ae3 2019-06-20 stsp nocbreak();
1687 60493ae3 2019-06-20 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
1690 60493ae3 2019-06-20 stsp if (ret == ERR)
1691 60493ae3 2019-06-20 stsp return NULL;
1693 60493ae3 2019-06-20 stsp if (view->searching) {
1694 1803e47f 2019-06-22 stsp regfree(&view->regex);
1695 60493ae3 2019-06-20 stsp view->searching = 0;
1698 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
1699 1803e47f 2019-06-22 stsp if (regcomp(&view->regex, pattern,
1700 f2d4ba2a 2019-06-21 stsp REG_EXTENDED | REG_ICASE | REG_NOSUB | REG_NEWLINE) == 0) {
1701 b1bf1435 2019-06-21 stsp view->searching = TOG_SEARCH_FORWARD;
1702 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1703 60493ae3 2019-06-20 stsp view->search_next(view);
1706 60493ae3 2019-06-20 stsp return NULL;
1709 60493ae3 2019-06-20 stsp static int
1710 60493ae3 2019-06-20 stsp match_commit(struct got_commit_object *commit, regex_t *regex)
1712 60493ae3 2019-06-20 stsp regmatch_t regmatch;
1714 60493ae3 2019-06-20 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1715 60493ae3 2019-06-20 stsp ®match, 0) == 0 ||
1716 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1717 60493ae3 2019-06-20 stsp ®match, 0) == 0 ||
1718 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_logmsg(commit), 1,
1719 60493ae3 2019-06-20 stsp ®match, 0) == 0)
1725 60493ae3 2019-06-20 stsp static const struct got_error *
1726 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
1728 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
1729 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1730 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
1732 60493ae3 2019-06-20 stsp if (!view->searching) {
1733 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1734 60493ae3 2019-06-20 stsp return NULL;
1737 b1bf1435 2019-06-21 stsp if (s->matched_entry) {
1738 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1739 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
1741 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
1742 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
1744 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1745 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
1747 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1750 60493ae3 2019-06-20 stsp while (1) {
1751 60493ae3 2019-06-20 stsp if (entry == NULL) {
1752 60493ae3 2019-06-20 stsp if (s->thread_args.log_complete) {
1753 60493ae3 2019-06-20 stsp if (s->matched_entry == NULL) {
1754 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1755 60493ae3 2019-06-20 stsp return NULL;
1757 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1758 60493ae3 2019-06-20 stsp entry = TAILQ_FIRST(&s->commits.head);
1760 b1bf1435 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head,
1761 b1bf1435 2019-06-21 stsp commit_queue_head);
1763 60493ae3 2019-06-20 stsp s->thread_args.commits_needed = 1;
1764 60493ae3 2019-06-20 stsp return trigger_log_thread(0,
1765 60493ae3 2019-06-20 stsp &s->thread_args.commits_needed,
1766 60493ae3 2019-06-20 stsp &s->thread_args.log_complete,
1767 60493ae3 2019-06-20 stsp &s->thread_args.need_commits);
1771 1803e47f 2019-06-22 stsp if (match_commit(entry->commit, &view->regex)) {
1772 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1773 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
1776 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1777 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
1779 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1782 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
1783 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
1784 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
1785 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1787 60493ae3 2019-06-20 stsp return err;
1790 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
1791 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1793 60493ae3 2019-06-20 stsp return err;
1798 60493ae3 2019-06-20 stsp return NULL;
1802 60493ae3 2019-06-20 stsp static const struct got_error *
1803 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1804 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
1805 8b473291 2019-02-21 stsp const char *path, int check_disk)
1807 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1808 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1809 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1810 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1811 1a76625f 2018-10-22 stsp int errcode;
1813 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1814 ecb28ae0 2018-07-16 stsp if (err != NULL)
1815 ecb28ae0 2018-07-16 stsp goto done;
1817 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1818 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1819 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1821 8b473291 2019-02-21 stsp s->refs = refs;
1822 fb2756b9 2018-08-04 stsp s->repo = repo;
1823 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1824 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1825 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1826 5036bf37 2018-09-24 stsp goto done;
1829 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1830 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1831 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1832 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
1833 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
1835 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1837 1a76625f 2018-10-22 stsp goto done;
1838 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1839 1a76625f 2018-10-22 stsp 0, thread_repo);
1841 1a76625f 2018-10-22 stsp goto done;
1843 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1844 1a76625f 2018-10-22 stsp if (errcode) {
1845 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
1846 1a76625f 2018-10-22 stsp goto done;
1849 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1850 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1851 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1852 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1853 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1854 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1855 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1856 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1857 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1858 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1859 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1862 1a76625f 2018-10-22 stsp close_log_view(view);
1863 ba4f502b 2018-08-04 stsp return err;
1866 e5a0f69f 2018-08-18 stsp static const struct got_error *
1867 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1869 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1871 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1872 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1873 2b380cc8 2018-10-24 stsp &s->thread_args);
1874 2b380cc8 2018-10-24 stsp if (errcode)
1875 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
1878 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1879 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1880 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
1881 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1884 e5a0f69f 2018-08-18 stsp static const struct got_error *
1885 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1886 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1888 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1889 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1890 5036bf37 2018-09-24 stsp char *parent_path;
1891 669b5ffa 2018-10-07 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
1892 669b5ffa 2018-10-07 stsp int begin_x = 0;
1894 e5a0f69f 2018-08-18 stsp switch (ch) {
1896 1e37a5c2 2019-05-12 jcs s->quit = 1;
1899 1e37a5c2 2019-05-12 jcs case KEY_UP:
1902 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1904 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
1905 1e37a5c2 2019-05-12 jcs s->selected--;
1907 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
1908 1144d21a 2019-06-21 stsp &s->commits);
1910 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
1911 a4292ac5 2019-05-12 jcs case CTRL('b'):
1912 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1914 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
1915 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
1916 1e37a5c2 2019-05-12 jcs s->selected = 0;
1919 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
1920 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
1923 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
1926 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1928 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
1929 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1930 1e37a5c2 2019-05-12 jcs s->selected++;
1933 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
1934 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
1935 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
1936 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1937 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1939 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
1940 a4292ac5 2019-05-12 jcs case CTRL('f'): {
1941 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
1942 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
1943 1e37a5c2 2019-05-12 jcs if (first == NULL)
1945 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
1946 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
1947 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
1948 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1949 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1950 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
1951 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
1952 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1953 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
1954 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
1955 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
1957 1e37a5c2 2019-05-12 jcs err = NULL;
1960 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1961 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
1962 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
1963 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
1964 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
1966 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
1969 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
1971 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
1972 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
1973 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
1974 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
1975 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
1978 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1979 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
1981 1e37a5c2 2019-05-12 jcs return err;
1982 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
1984 1e37a5c2 2019-05-12 jcs view_close(diff_view);
1987 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
1988 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
1990 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
1993 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
1995 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
1996 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
1997 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
1998 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2001 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2002 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2004 1e37a5c2 2019-05-12 jcs return err;
2005 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2007 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2010 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2011 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2013 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2015 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2016 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2018 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2019 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2020 1e37a5c2 2019-05-12 jcs struct tog_view *lv;
2021 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2023 1e37a5c2 2019-05-12 jcs return err;
2024 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2025 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2026 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2027 638f9024 2019-05-13 stsp return got_error_from_errno(
2028 1e37a5c2 2019-05-12 jcs "view_open");
2029 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2030 1e37a5c2 2019-05-12 jcs s->repo, parent_path, 0);
2032 1e37a5c2 2019-05-12 jcs return err;;
2033 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2034 1e37a5c2 2019-05-12 jcs *new_view = lv;
2036 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2037 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2039 1e37a5c2 2019-05-12 jcs return NULL;
2046 80ddbec8 2018-04-29 stsp return err;
2049 4ed7e80c 2018-05-20 stsp static const struct got_error *
2050 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2052 c2db6724 2019-01-04 stsp const struct got_error *error;
2054 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2055 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2057 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2058 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2060 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2061 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2063 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2064 c2db6724 2019-01-04 stsp if (error != NULL)
2065 c2db6724 2019-01-04 stsp return error;
2067 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2068 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2070 c2db6724 2019-01-04 stsp return NULL;
2073 a915003a 2019-02-05 stsp static void
2074 a915003a 2019-02-05 stsp init_curses(void)
2076 a915003a 2019-02-05 stsp initscr();
2078 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2081 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2082 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2083 a915003a 2019-02-05 stsp curs_set(0);
2084 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2087 c2db6724 2019-01-04 stsp static const struct got_error *
2088 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2090 80ddbec8 2018-04-29 stsp const struct got_error *error;
2091 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2092 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2093 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2094 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2095 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2096 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
2098 04cc582a 2018-08-01 stsp struct tog_view *view;
2100 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2102 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2103 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2104 c2db6724 2019-01-04 stsp NULL) == -1)
2105 80ddbec8 2018-04-29 stsp err(1, "pledge");
2108 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2109 80ddbec8 2018-04-29 stsp switch (ch) {
2111 80ddbec8 2018-04-29 stsp start_commit = optarg;
2114 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2115 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2116 ecb28ae0 2018-07-16 stsp err(1, "-r option");
2119 17020d27 2019-03-07 stsp usage_log();
2120 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2124 80ddbec8 2018-04-29 stsp argc -= optind;
2125 80ddbec8 2018-04-29 stsp argv += optind;
2127 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2128 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2129 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2130 ecb28ae0 2018-07-16 stsp goto done;
2132 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2133 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2134 963f97a1 2019-03-18 stsp goto done;
2135 963f97a1 2019-03-18 stsp error = NULL;
2137 963f97a1 2019-03-18 stsp if (argc == 0) {
2138 963f97a1 2019-03-18 stsp path = strdup("");
2139 963f97a1 2019-03-18 stsp if (path == NULL) {
2140 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2141 ecb28ae0 2018-07-16 stsp goto done;
2143 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2144 963f97a1 2019-03-18 stsp if (worktree) {
2145 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2147 963f97a1 2019-03-18 stsp if (error)
2148 963f97a1 2019-03-18 stsp goto done;
2150 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2151 963f97a1 2019-03-18 stsp if (path == NULL) {
2152 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2153 963f97a1 2019-03-18 stsp goto done;
2157 963f97a1 2019-03-18 stsp usage_log();
2159 963f97a1 2019-03-18 stsp repo_path = worktree ?
2160 963f97a1 2019-03-18 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2161 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2162 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2163 963f97a1 2019-03-18 stsp goto done;
2166 a915003a 2019-02-05 stsp init_curses();
2168 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
2169 80ddbec8 2018-04-29 stsp if (error != NULL)
2170 ecb28ae0 2018-07-16 stsp goto done;
2172 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2173 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2174 c02c541e 2019-03-29 stsp if (error)
2175 c02c541e 2019-03-29 stsp goto done;
2177 15a94983 2018-12-23 stsp if (start_commit == NULL)
2178 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&start_id, worktree ?
2179 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2182 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&start_id, repo,
2183 15a94983 2018-12-23 stsp start_commit);
2184 80ddbec8 2018-04-29 stsp if (error != NULL)
2185 8b473291 2019-02-21 stsp goto done;
2187 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2188 8b473291 2019-02-21 stsp if (error)
2189 ecb28ae0 2018-07-16 stsp goto done;
2191 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2192 04cc582a 2018-08-01 stsp if (view == NULL) {
2193 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2194 04cc582a 2018-08-01 stsp goto done;
2196 8b473291 2019-02-21 stsp error = open_log_view(view, start_id, &refs, repo, path, 1);
2197 ba4f502b 2018-08-04 stsp if (error)
2198 ba4f502b 2018-08-04 stsp goto done;
2199 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2201 ecb28ae0 2018-07-16 stsp free(repo_path);
2202 ecb28ae0 2018-07-16 stsp free(cwd);
2203 ecb28ae0 2018-07-16 stsp free(path);
2204 899d86c2 2018-05-10 stsp free(start_id);
2206 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2207 ec142235 2019-03-07 stsp if (worktree)
2208 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2209 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2210 80ddbec8 2018-04-29 stsp return error;
2213 4ed7e80c 2018-05-20 stsp __dead static void
2214 9f7d7167 2018-04-29 stsp usage_diff(void)
2217 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2218 9f7d7167 2018-04-29 stsp getprogname());
2222 b304db33 2018-05-20 stsp static char *
2223 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2225 b304db33 2018-05-20 stsp char *line;
2226 b304db33 2018-05-20 stsp size_t linelen;
2227 b304db33 2018-05-20 stsp size_t lineno;
2228 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2230 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2232 b304db33 2018-05-20 stsp *len = linelen;
2233 b304db33 2018-05-20 stsp return line;
2236 4ed7e80c 2018-05-20 stsp static const struct got_error *
2237 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2238 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
2239 c3e9aa98 2019-05-13 jcs char *header)
2241 61e69b96 2018-05-20 stsp const struct got_error *err;
2242 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2243 b304db33 2018-05-20 stsp char *line;
2244 b304db33 2018-05-20 stsp size_t len;
2245 61e69b96 2018-05-20 stsp wchar_t *wline;
2246 e0b650dd 2018-05-20 stsp int width;
2248 26ed57b2 2018-05-19 stsp rewind(f);
2249 f7d12f7e 2018-08-01 stsp werase(view->window);
2251 a3404814 2018-09-02 stsp if (header) {
2252 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
2253 a3404814 2018-09-02 stsp if (err) {
2254 a3404814 2018-09-02 stsp return err;
2257 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2258 a3404814 2018-09-02 stsp wstandout(view->window);
2259 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2260 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2261 a3404814 2018-09-02 stsp wstandend(view->window);
2262 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2263 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2265 a3404814 2018-09-02 stsp if (max_lines <= 1)
2266 a3404814 2018-09-02 stsp return NULL;
2267 a3404814 2018-09-02 stsp max_lines--;
2271 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2272 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2273 26ed57b2 2018-05-19 stsp if (line == NULL) {
2277 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2278 26ed57b2 2018-05-19 stsp free(line);
2282 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2283 61e69b96 2018-05-20 stsp if (err) {
2284 61e69b96 2018-05-20 stsp free(line);
2285 61e69b96 2018-05-20 stsp return err;
2287 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2288 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2289 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2290 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2291 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2292 26ed57b2 2018-05-19 stsp free(line);
2293 2550e4c3 2018-07-13 stsp free(wline);
2294 2550e4c3 2018-07-13 stsp wline = NULL;
2296 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2298 1a57306a 2018-09-02 stsp view_vborder(view);
2300 c3e9aa98 2019-05-13 jcs if (*eof) {
2301 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2302 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2303 c3e9aa98 2019-05-13 jcs nprinted++;
2306 c3e9aa98 2019-05-13 jcs err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2308 c3e9aa98 2019-05-13 jcs return err;
2311 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2312 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2313 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2316 26ed57b2 2018-05-19 stsp return NULL;
2319 abd2672a 2018-12-23 stsp static char *
2320 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2322 abd2672a 2018-12-23 stsp char *p, *s = ctime_r(time, datebuf);
2323 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2325 abd2672a 2018-12-23 stsp *p = '\0';
2329 4ed7e80c 2018-05-20 stsp static const struct got_error *
2330 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2331 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2333 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2334 abd2672a 2018-12-23 stsp char datebuf[26];
2335 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2336 15a94983 2018-12-23 stsp char *id_str = NULL;
2337 45d799e2 2018-12-23 stsp time_t committer_time;
2338 45d799e2 2018-12-23 stsp const char *author, *committer;
2339 8b473291 2019-02-21 stsp char *refs_str = NULL;
2341 8b473291 2019-02-21 stsp if (refs) {
2342 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, commit_id);
2344 8b473291 2019-02-21 stsp return err;
2347 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2349 abd2672a 2018-12-23 stsp return err;
2351 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2352 15a94983 2018-12-23 stsp if (err) {
2353 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2354 15a94983 2018-12-23 stsp goto done;
2357 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2358 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2359 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2360 abd2672a 2018-12-23 stsp goto done;
2362 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2363 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2364 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2365 abd2672a 2018-12-23 stsp goto done;
2367 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2368 abd2672a 2018-12-23 stsp if (fprintf(outfile, "date: %s UTC\n",
2369 45d799e2 2018-12-23 stsp get_datestr(&committer_time, datebuf)) < 0) {
2370 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2371 abd2672a 2018-12-23 stsp goto done;
2373 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2374 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2375 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2376 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2377 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2378 abd2672a 2018-12-23 stsp goto done;
2380 45d799e2 2018-12-23 stsp if (fprintf(outfile, "%s\n",
2381 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(commit)) < 0) {
2382 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2383 abd2672a 2018-12-23 stsp goto done;
2386 abd2672a 2018-12-23 stsp free(id_str);
2387 8b473291 2019-02-21 stsp free(refs_str);
2388 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2389 abd2672a 2018-12-23 stsp return err;
2392 abd2672a 2018-12-23 stsp static const struct got_error *
2393 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2395 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2396 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2397 15a94983 2018-12-23 stsp int obj_type;
2399 511a516b 2018-05-19 stsp f = got_opentemp();
2400 48ae06ee 2018-10-18 stsp if (f == NULL) {
2401 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2402 48ae06ee 2018-10-18 stsp goto done;
2404 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2405 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2406 fb43ecf1 2019-02-11 stsp goto done;
2410 15a94983 2018-12-23 stsp if (s->id1)
2411 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2413 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2415 15a94983 2018-12-23 stsp goto done;
2417 15a94983 2018-12-23 stsp switch (obj_type) {
2418 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2419 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2420 54156555 2018-12-24 stsp s->diff_context, s->repo, f);
2422 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2423 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2424 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
2426 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2427 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2428 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2429 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2431 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2434 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2435 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2436 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2438 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2439 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2440 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2441 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2442 8b473291 2019-02-21 stsp s->repo, f);
2447 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2449 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2450 15a94983 2018-12-23 stsp s->diff_context, s->repo, f);
2454 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2458 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2459 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2460 48ae06ee 2018-10-18 stsp return err;
2463 f5215bb9 2019-02-22 stsp static void
2464 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2466 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2467 f5215bb9 2019-02-22 stsp update_panels();
2468 f5215bb9 2019-02-22 stsp doupdate();
2471 48ae06ee 2018-10-18 stsp static const struct got_error *
2472 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2473 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2474 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2476 48ae06ee 2018-10-18 stsp const struct got_error *err;
2478 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2479 15a94983 2018-12-23 stsp int type1, type2;
2480 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2482 15a94983 2018-12-23 stsp return err;
2483 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2485 15a94983 2018-12-23 stsp return err;
2487 15a94983 2018-12-23 stsp if (type1 != type2)
2488 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2491 15a94983 2018-12-23 stsp if (id1) {
2492 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2493 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2494 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2496 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2498 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2499 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2500 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2501 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2502 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2504 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2505 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2506 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2507 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2508 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2509 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2510 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2512 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2513 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2514 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2516 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2517 48ae06ee 2018-10-18 stsp if (err) {
2518 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2519 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2520 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2521 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2522 48ae06ee 2018-10-18 stsp return err;
2525 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2526 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2527 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2529 5dc9f4bc 2018-08-04 stsp return NULL;
2532 e5a0f69f 2018-08-18 stsp static const struct got_error *
2533 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2535 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2537 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2538 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2539 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2540 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2541 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2542 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2543 e5a0f69f 2018-08-18 stsp return err;
2546 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2547 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2549 a3404814 2018-09-02 stsp const struct got_error *err;
2550 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2551 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2553 a3404814 2018-09-02 stsp if (s->id1) {
2554 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2556 a3404814 2018-09-02 stsp return err;
2558 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2560 a3404814 2018-09-02 stsp return err;
2562 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2563 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2564 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2565 a3404814 2018-09-02 stsp free(id_str1);
2566 a3404814 2018-09-02 stsp free(id_str2);
2567 a3404814 2018-09-02 stsp return err;
2569 a3404814 2018-09-02 stsp free(id_str1);
2570 a3404814 2018-09-02 stsp free(id_str2);
2572 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2573 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2577 15a087fe 2019-02-21 stsp static const struct got_error *
2578 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2579 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2581 d7a04538 2019-02-21 stsp const struct got_error *err;
2582 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
2583 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
2584 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
2586 15a087fe 2019-02-21 stsp free(s->id2);
2587 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2588 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2589 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2591 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2593 d7a04538 2019-02-21 stsp return err;
2594 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
2595 15a087fe 2019-02-21 stsp free(s->id1);
2596 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
2597 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2598 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
2599 15a087fe 2019-02-21 stsp return NULL;
2602 0cf4efb1 2018-09-29 stsp static const struct got_error *
2603 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2604 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2606 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2607 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2608 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
2609 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
2612 e5a0f69f 2018-08-18 stsp switch (ch) {
2614 1e37a5c2 2019-05-12 jcs case KEY_UP:
2615 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
2616 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2618 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2619 a60a9dc4 2019-05-13 jcs case CTRL('b'):
2620 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
2623 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
2624 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
2625 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2628 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2629 1e37a5c2 2019-05-12 jcs if (!s->eof)
2630 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2632 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
2633 a60a9dc4 2019-05-13 jcs case CTRL('f'):
2635 00ba99a7 2019-05-12 jcs if (s->eof)
2638 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
2639 1e37a5c2 2019-05-12 jcs char *line;
2640 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
2641 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2642 1e37a5c2 2019-05-12 jcs if (line == NULL)
2647 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
2648 1e37a5c2 2019-05-12 jcs s->diff_context--;
2649 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2650 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2654 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2655 1e37a5c2 2019-05-12 jcs s->diff_context++;
2656 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2657 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2662 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2664 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2665 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
2666 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
2667 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2670 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2675 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2679 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2680 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2682 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2683 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2687 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2689 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2691 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2692 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
2694 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
2695 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
2696 1e37a5c2 2019-05-12 jcs update_panels();
2697 1e37a5c2 2019-05-12 jcs doupdate();
2699 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
2700 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
2701 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
2702 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
2706 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2711 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
2712 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2715 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2719 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2720 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2722 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2723 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2729 bcbd79e2 2018-08-19 stsp return err;
2732 4ed7e80c 2018-05-20 stsp static const struct got_error *
2733 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2735 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2736 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2737 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2738 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2739 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2740 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2742 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2744 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2746 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2747 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2748 eb6600df 2019-01-04 stsp NULL) == -1)
2749 26ed57b2 2018-05-19 stsp err(1, "pledge");
2752 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2753 26ed57b2 2018-05-19 stsp switch (ch) {
2755 17020d27 2019-03-07 stsp usage_diff();
2756 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2760 26ed57b2 2018-05-19 stsp argc -= optind;
2761 26ed57b2 2018-05-19 stsp argv += optind;
2763 26ed57b2 2018-05-19 stsp if (argc == 0) {
2764 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2765 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2766 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2767 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2768 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2769 15a94983 2018-12-23 stsp id_str1 = argv[0];
2770 15a94983 2018-12-23 stsp id_str2 = argv[1];
2771 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2772 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2773 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2774 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2775 15a94983 2018-12-23 stsp id_str1 = argv[1];
2776 15a94983 2018-12-23 stsp id_str2 = argv[2];
2778 26ed57b2 2018-05-19 stsp usage_diff();
2780 a915003a 2019-02-05 stsp init_curses();
2782 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2783 eb6600df 2019-01-04 stsp if (error)
2784 eb6600df 2019-01-04 stsp goto done;
2786 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
2787 26ed57b2 2018-05-19 stsp if (error)
2788 26ed57b2 2018-05-19 stsp goto done;
2790 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
2791 26ed57b2 2018-05-19 stsp if (error)
2792 26ed57b2 2018-05-19 stsp goto done;
2794 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
2795 26ed57b2 2018-05-19 stsp if (error)
2796 26ed57b2 2018-05-19 stsp goto done;
2798 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2799 8b473291 2019-02-21 stsp if (error)
2800 8b473291 2019-02-21 stsp goto done;
2802 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2803 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2804 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2805 ea5e7bb5 2018-08-01 stsp goto done;
2807 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2808 5dc9f4bc 2018-08-04 stsp if (error)
2809 5dc9f4bc 2018-08-04 stsp goto done;
2810 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2812 c02c541e 2019-03-29 stsp free(repo_path);
2813 26ed57b2 2018-05-19 stsp got_repo_close(repo);
2814 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2815 26ed57b2 2018-05-19 stsp return error;
2818 4ed7e80c 2018-05-20 stsp __dead static void
2819 9f7d7167 2018-04-29 stsp usage_blame(void)
2822 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2823 9f7d7167 2018-04-29 stsp getprogname());
2827 84451b3e 2018-07-10 stsp struct tog_blame_line {
2828 84451b3e 2018-07-10 stsp int annotated;
2829 84451b3e 2018-07-10 stsp struct got_object_id *id;
2832 4ed7e80c 2018-05-20 stsp static const struct got_error *
2833 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2834 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2835 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2836 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2838 84451b3e 2018-07-10 stsp const struct got_error *err;
2839 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2840 84451b3e 2018-07-10 stsp char *line;
2841 84451b3e 2018-07-10 stsp size_t len;
2842 84451b3e 2018-07-10 stsp wchar_t *wline;
2843 b700b5d6 2018-07-10 stsp int width, wlimit;
2844 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2845 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2846 ab089a2a 2018-07-12 stsp char *id_str;
2848 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2850 ab089a2a 2018-07-12 stsp return err;
2852 84451b3e 2018-07-10 stsp rewind(f);
2853 f7d12f7e 2018-08-01 stsp werase(view->window);
2855 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
2856 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2857 ab089a2a 2018-07-12 stsp free(id_str);
2858 ab089a2a 2018-07-12 stsp return err;
2861 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2862 ab089a2a 2018-07-12 stsp free(line);
2863 2550e4c3 2018-07-13 stsp line = NULL;
2864 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2865 a3404814 2018-09-02 stsp wstandout(view->window);
2866 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2867 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2868 a3404814 2018-09-02 stsp wstandend(view->window);
2869 2550e4c3 2018-07-13 stsp free(wline);
2870 2550e4c3 2018-07-13 stsp wline = NULL;
2871 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2872 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2874 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2875 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2876 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
2877 ab089a2a 2018-07-12 stsp free(id_str);
2878 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2880 ab089a2a 2018-07-12 stsp free(id_str);
2881 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2882 3f60a8ef 2018-07-10 stsp free(line);
2883 2550e4c3 2018-07-13 stsp line = NULL;
2885 3f60a8ef 2018-07-10 stsp return err;
2886 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2887 2550e4c3 2018-07-13 stsp free(wline);
2888 2550e4c3 2018-07-13 stsp wline = NULL;
2889 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2890 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2893 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2894 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2895 84451b3e 2018-07-10 stsp if (line == NULL) {
2899 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2900 84451b3e 2018-07-10 stsp free(line);
2904 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2905 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2906 84451b3e 2018-07-10 stsp if (err) {
2907 84451b3e 2018-07-10 stsp free(line);
2908 84451b3e 2018-07-10 stsp return err;
2911 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2912 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2914 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2915 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2916 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2917 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2918 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2919 84451b3e 2018-07-10 stsp char *id_str;
2920 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2921 84451b3e 2018-07-10 stsp if (err) {
2922 84451b3e 2018-07-10 stsp free(line);
2923 2550e4c3 2018-07-13 stsp free(wline);
2924 84451b3e 2018-07-10 stsp return err;
2926 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2927 84451b3e 2018-07-10 stsp free(id_str);
2928 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2930 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2931 ee41ec32 2018-07-10 stsp prev_id = NULL;
2934 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2935 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2936 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2939 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2940 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2941 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2942 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2943 84451b3e 2018-07-10 stsp free(line);
2944 2550e4c3 2018-07-13 stsp free(wline);
2945 2550e4c3 2018-07-13 stsp wline = NULL;
2947 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2949 1a57306a 2018-09-02 stsp view_vborder(view);
2951 84451b3e 2018-07-10 stsp return NULL;
2954 84451b3e 2018-07-10 stsp static const struct got_error *
2955 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2957 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2958 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2959 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2960 1a76625f 2018-10-22 stsp int errcode;
2962 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2963 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2964 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2966 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2967 1a76625f 2018-10-22 stsp if (errcode)
2968 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
2970 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2971 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2972 d68a0a7d 2018-07-10 stsp goto done;
2975 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2976 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2978 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2979 d68a0a7d 2018-07-10 stsp if (line->annotated)
2980 d68a0a7d 2018-07-10 stsp goto done;
2982 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2983 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2984 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2985 84451b3e 2018-07-10 stsp goto done;
2987 84451b3e 2018-07-10 stsp line->annotated = 1;
2989 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2990 1a76625f 2018-10-22 stsp if (errcode)
2991 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2992 84451b3e 2018-07-10 stsp return err;
2995 84451b3e 2018-07-10 stsp static void *
2996 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2998 18430de3 2018-07-10 stsp const struct got_error *err;
2999 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3000 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3001 1a76625f 2018-10-22 stsp int errcode;
3003 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3004 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
3006 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3007 1a76625f 2018-10-22 stsp if (errcode)
3008 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3009 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3011 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3012 c9beca56 2018-07-22 stsp ta->repo = NULL;
3013 c9beca56 2018-07-22 stsp *ta->complete = 1;
3015 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3016 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3017 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3019 18430de3 2018-07-10 stsp return (void *)err;
3022 245d91c1 2018-07-12 stsp static struct got_object_id *
3023 15a94983 2018-12-23 stsp get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3024 15a94983 2018-12-23 stsp int selected_line)
3026 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3028 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3029 245d91c1 2018-07-12 stsp if (!line->annotated)
3030 245d91c1 2018-07-12 stsp return NULL;
3032 245d91c1 2018-07-12 stsp return line->id;
3035 b880a918 2018-07-10 stsp static const struct got_error *
3036 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3038 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3041 245d91c1 2018-07-12 stsp if (blame->thread) {
3042 1a76625f 2018-10-22 stsp int errcode;
3043 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3044 1a76625f 2018-10-22 stsp if (errcode)
3045 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3046 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3047 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3048 1a76625f 2018-10-22 stsp if (errcode)
3049 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3050 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3051 1a76625f 2018-10-22 stsp if (errcode)
3052 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3053 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3054 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3055 245d91c1 2018-07-12 stsp err = NULL;
3056 245d91c1 2018-07-12 stsp blame->thread = NULL;
3058 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3059 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3060 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3062 245d91c1 2018-07-12 stsp if (blame->f) {
3063 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3064 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3065 245d91c1 2018-07-12 stsp blame->f = NULL;
3067 57670559 2018-12-23 stsp if (blame->lines) {
3068 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3069 57670559 2018-12-23 stsp free(blame->lines[i].id);
3070 57670559 2018-12-23 stsp free(blame->lines);
3071 57670559 2018-12-23 stsp blame->lines = NULL;
3073 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3074 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3076 245d91c1 2018-07-12 stsp return err;
3079 245d91c1 2018-07-12 stsp static const struct got_error *
3080 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3081 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3082 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3083 245d91c1 2018-07-12 stsp struct got_repository *repo)
3085 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3086 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3087 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3088 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3089 15a94983 2018-12-23 stsp int obj_type;
3091 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3093 15a94983 2018-12-23 stsp return err;
3094 15a94983 2018-12-23 stsp if (obj_id == NULL)
3095 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
3097 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3099 84451b3e 2018-07-10 stsp goto done;
3101 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3102 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3103 84451b3e 2018-07-10 stsp goto done;
3106 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3108 a70480e0 2018-06-23 stsp goto done;
3109 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3110 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3111 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3112 84451b3e 2018-07-10 stsp goto done;
3114 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3115 245d91c1 2018-07-12 stsp blame->f, blob);
3117 84451b3e 2018-07-10 stsp goto done;
3119 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3120 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3121 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3122 84451b3e 2018-07-10 stsp goto done;
3125 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3127 bd24772e 2018-07-11 stsp goto done;
3129 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3130 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3131 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3132 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3133 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3134 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3135 245d91c1 2018-07-12 stsp goto done;
3137 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3139 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3140 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3141 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3142 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3143 245d91c1 2018-07-12 stsp *blame_complete = 0;
3147 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3148 27d434c2 2018-09-15 stsp free(obj_id);
3150 245d91c1 2018-07-12 stsp stop_blame(blame);
3151 245d91c1 2018-07-12 stsp return err;
3154 245d91c1 2018-07-12 stsp static const struct got_error *
3155 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3156 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3157 8b473291 2019-02-21 stsp struct got_repository *repo)
3159 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3160 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3162 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3164 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3166 7cbe629d 2018-08-04 stsp return err;
3168 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3169 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3170 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3171 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3172 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3173 fb2756b9 2018-08-04 stsp s->path = path;
3174 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
3175 638f9024 2019-05-13 stsp return got_error_from_errno("open_blame_view");
3176 fb2756b9 2018-08-04 stsp s->repo = repo;
3177 8b473291 2019-02-21 stsp s->refs = refs;
3178 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3179 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3181 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3182 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3183 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3185 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3186 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3187 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3188 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3191 e5a0f69f 2018-08-18 stsp static const struct got_error *
3192 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3194 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3195 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3197 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3198 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3200 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3201 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3202 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3203 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3204 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3207 e5a0f69f 2018-08-18 stsp free(s->path);
3209 e5a0f69f 2018-08-18 stsp return err;
3212 7cbe629d 2018-08-04 stsp static const struct got_error *
3213 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
3215 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3216 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
3217 2b380cc8 2018-10-24 stsp int errcode;
3219 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
3220 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3221 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
3222 2b380cc8 2018-10-24 stsp if (errcode)
3223 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3226 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3227 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3228 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
3229 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
3231 669b5ffa 2018-10-07 stsp view_vborder(view);
3232 e5a0f69f 2018-08-18 stsp return err;
3235 e5a0f69f 2018-08-18 stsp static const struct got_error *
3236 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3237 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3239 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
3240 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
3241 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3242 669b5ffa 2018-10-07 stsp int begin_x = 0;
3244 e5a0f69f 2018-08-18 stsp switch (ch) {
3246 1e37a5c2 2019-05-12 jcs s->done = 1;
3249 1e37a5c2 2019-05-12 jcs case KEY_UP:
3250 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
3251 1e37a5c2 2019-05-12 jcs s->selected_line--;
3252 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
3253 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3254 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3256 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3257 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
3258 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3261 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3262 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3263 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3265 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3268 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3269 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3270 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3271 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3272 1e37a5c2 2019-05-12 jcs s->selected_line++;
3273 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3274 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3275 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3278 1e37a5c2 2019-05-12 jcs case 'p': {
3279 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3280 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3281 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3282 1e37a5c2 2019-05-12 jcs if (id == NULL)
3284 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3285 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3286 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3287 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3288 1e37a5c2 2019-05-12 jcs int obj_type;
3289 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3290 1e37a5c2 2019-05-12 jcs s->repo, id);
3293 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3294 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3295 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3296 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3299 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3300 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3301 1e37a5c2 2019-05-12 jcs pid->id, s->path);
3302 e5a0f69f 2018-08-18 stsp if (err) {
3303 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
3304 1e37a5c2 2019-05-12 jcs err = NULL;
3305 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3308 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
3310 1e37a5c2 2019-05-12 jcs free(blob_id);
3311 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
3312 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
3313 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3316 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3318 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3320 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
3321 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
3323 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3328 1e37a5c2 2019-05-12 jcs s->done = 1;
3329 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3330 1e37a5c2 2019-05-12 jcs s->done = 0;
3331 1e37a5c2 2019-05-12 jcs if (thread_err)
3333 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3334 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
3335 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3336 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3337 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
3338 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
3343 1e37a5c2 2019-05-12 jcs case 'B': {
3344 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
3345 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
3346 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
3348 1e37a5c2 2019-05-12 jcs s->done = 1;
3349 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3350 1e37a5c2 2019-05-12 jcs s->done = 0;
3351 1e37a5c2 2019-05-12 jcs if (thread_err)
3353 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3354 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
3355 1e37a5c2 2019-05-12 jcs s->blamed_commit =
3356 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
3357 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3358 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3359 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
3360 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
3365 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3366 1e37a5c2 2019-05-12 jcs case '\r': {
3367 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3368 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
3369 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
3370 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3371 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3372 1e37a5c2 2019-05-12 jcs if (id == NULL)
3374 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
3377 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
3378 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
3379 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3380 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3381 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3382 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
3383 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3384 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
3387 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
3388 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
3389 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3391 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3394 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3395 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3398 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
3400 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3403 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
3404 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
3406 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3411 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3413 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3414 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
3415 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
3418 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3419 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
3420 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3421 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3424 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
3425 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
3426 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
3427 1e37a5c2 2019-05-12 jcs view->nlines - 2;
3429 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
3430 1e37a5c2 2019-05-12 jcs s->blame.nlines -
3431 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
3433 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3434 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
3435 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3436 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3442 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
3445 a70480e0 2018-06-23 stsp static const struct got_error *
3446 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
3448 a70480e0 2018-06-23 stsp const struct got_error *error;
3449 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
3450 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3451 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
3452 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3453 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3454 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
3456 e1cd8fed 2018-08-01 stsp struct tog_view *view;
3458 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3460 a70480e0 2018-06-23 stsp #ifndef PROFILE
3461 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3462 8e94dd5b 2019-01-04 stsp NULL) == -1)
3463 a70480e0 2018-06-23 stsp err(1, "pledge");
3466 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3467 a70480e0 2018-06-23 stsp switch (ch) {
3469 a70480e0 2018-06-23 stsp commit_id_str = optarg;
3472 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3473 69069811 2018-08-02 stsp if (repo_path == NULL)
3474 69069811 2018-08-02 stsp err(1, "-r option");
3477 17020d27 2019-03-07 stsp usage_blame();
3478 a70480e0 2018-06-23 stsp /* NOTREACHED */
3482 a70480e0 2018-06-23 stsp argc -= optind;
3483 a70480e0 2018-06-23 stsp argv += optind;
3485 69069811 2018-08-02 stsp if (argc == 1)
3486 69069811 2018-08-02 stsp path = argv[0];
3488 a70480e0 2018-06-23 stsp usage_blame();
3490 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
3491 69069811 2018-08-02 stsp if (cwd == NULL) {
3492 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3493 69069811 2018-08-02 stsp goto done;
3495 69069811 2018-08-02 stsp if (repo_path == NULL) {
3496 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3497 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3498 69069811 2018-08-02 stsp goto done;
3500 eb41ed75 2019-02-05 stsp error = NULL;
3501 eb41ed75 2019-02-05 stsp if (worktree) {
3502 eb41ed75 2019-02-05 stsp repo_path =
3503 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3504 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
3505 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3506 eb41ed75 2019-02-05 stsp if (error)
3507 eb41ed75 2019-02-05 stsp goto done;
3509 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
3510 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
3511 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3512 eb41ed75 2019-02-05 stsp goto done;
3517 a915003a 2019-02-05 stsp init_curses();
3519 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
3520 c02c541e 2019-03-29 stsp if (error != NULL)
3521 8e94dd5b 2019-01-04 stsp goto done;
3523 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3524 c02c541e 2019-03-29 stsp if (error)
3525 92205607 2019-01-04 stsp goto done;
3527 eb41ed75 2019-02-05 stsp if (worktree) {
3528 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3529 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3530 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3531 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3532 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3533 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3534 eb41ed75 2019-02-05 stsp path) == -1) {
3535 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3536 eb41ed75 2019-02-05 stsp goto done;
3538 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3541 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3543 eb41ed75 2019-02-05 stsp if (error)
3544 69069811 2018-08-02 stsp goto done;
3546 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
3547 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
3548 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3549 a70480e0 2018-06-23 stsp if (error != NULL)
3550 66b4983c 2018-06-23 stsp goto done;
3551 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3552 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
3554 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
3555 15a94983 2018-12-23 stsp commit_id_str);
3557 a19e88aa 2018-06-23 stsp if (error != NULL)
3558 8b473291 2019-02-21 stsp goto done;
3560 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
3561 8b473291 2019-02-21 stsp if (error)
3562 a19e88aa 2018-06-23 stsp goto done;
3564 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3565 e1cd8fed 2018-08-01 stsp if (view == NULL) {
3566 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3567 e1cd8fed 2018-08-01 stsp goto done;
3569 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3570 7cbe629d 2018-08-04 stsp if (error)
3571 7cbe629d 2018-08-04 stsp goto done;
3572 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3574 69069811 2018-08-02 stsp free(repo_path);
3575 69069811 2018-08-02 stsp free(cwd);
3576 a70480e0 2018-06-23 stsp free(commit_id);
3577 eb41ed75 2019-02-05 stsp if (worktree)
3578 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
3580 a70480e0 2018-06-23 stsp got_repo_close(repo);
3581 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3582 a70480e0 2018-06-23 stsp return error;
3585 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3586 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
3587 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
3588 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
3589 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
3590 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
3591 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
3593 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3594 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
3595 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
3596 ffd1d5e5 2018-06-23 stsp int width, n;
3598 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
3600 f7d12f7e 2018-08-01 stsp werase(view->window);
3602 ffd1d5e5 2018-06-23 stsp if (limit == 0)
3603 ffd1d5e5 2018-06-23 stsp return NULL;
3605 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
3607 ffd1d5e5 2018-06-23 stsp return err;
3608 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3609 a3404814 2018-09-02 stsp wstandout(view->window);
3610 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3611 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3612 a3404814 2018-09-02 stsp wstandend(view->window);
3613 2550e4c3 2018-07-13 stsp free(wline);
3614 2550e4c3 2018-07-13 stsp wline = NULL;
3615 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3616 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3617 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3618 ffd1d5e5 2018-06-23 stsp return NULL;
3619 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
3621 ce52c690 2018-06-23 stsp return err;
3622 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3623 2550e4c3 2018-07-13 stsp free(wline);
3624 2550e4c3 2018-07-13 stsp wline = NULL;
3625 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3626 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3627 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3628 ffd1d5e5 2018-06-23 stsp return NULL;
3629 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3630 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
3631 a1eca9bb 2018-06-23 stsp return NULL;
3633 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3634 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
3635 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
3636 0cf4efb1 2018-09-29 stsp if (view->focussed)
3637 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3638 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
3640 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
3641 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
3642 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3643 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3644 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3645 ffd1d5e5 2018-06-23 stsp return NULL;
3649 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
3650 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3653 ffd1d5e5 2018-06-23 stsp while (te) {
3654 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
3656 1d13200f 2018-07-12 stsp if (show_ids) {
3657 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
3659 638f9024 2019-05-13 stsp return got_error_from_errno(
3660 230a42bd 2019-05-11 jcs "got_object_id_str");
3662 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3663 d8355ef1 2019-02-10 stsp te->name, S_ISDIR(te->mode) ? "/" :
3664 d8355ef1 2019-02-10 stsp ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3665 1d13200f 2018-07-12 stsp free(id_str);
3666 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3668 1d13200f 2018-07-12 stsp free(id_str);
3669 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
3670 ffd1d5e5 2018-06-23 stsp if (err) {
3671 ffd1d5e5 2018-06-23 stsp free(line);
3674 ffd1d5e5 2018-06-23 stsp if (n == selected) {
3675 0cf4efb1 2018-09-29 stsp if (view->focussed)
3676 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3677 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
3679 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3680 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3681 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3682 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
3683 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3684 ffd1d5e5 2018-06-23 stsp free(line);
3685 2550e4c3 2018-07-13 stsp free(wline);
3686 2550e4c3 2018-07-13 stsp wline = NULL;
3688 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3689 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
3690 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3692 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3695 ffd1d5e5 2018-06-23 stsp return err;
3698 ffd1d5e5 2018-06-23 stsp static void
3699 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
3700 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
3701 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
3703 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
3706 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
3709 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3710 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
3711 ffd1d5e5 2018-06-23 stsp if (!isroot)
3712 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3716 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
3717 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
3718 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
3719 ffd1d5e5 2018-06-23 stsp prev = te;
3720 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3722 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
3723 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3725 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3726 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3729 768394f3 2019-01-24 stsp static int
3730 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3731 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
3732 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
3734 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
3735 ffd1d5e5 2018-06-23 stsp int n = 0;
3737 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
3738 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3740 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
3741 768394f3 2019-01-24 stsp last = last_displayed_entry;
3742 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
3743 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
3744 768394f3 2019-01-24 stsp if (last) {
3745 768394f3 2019-01-24 stsp *first_displayed_entry = next;
3746 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
3752 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3753 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
3754 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
3756 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
3757 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
3758 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
3760 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
3761 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
3763 ce52c690 2018-06-23 stsp len += strlen(te->name);
3765 ce52c690 2018-06-23 stsp *path = calloc(1, len);
3766 ffd1d5e5 2018-06-23 stsp if (path == NULL)
3767 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
3769 ce52c690 2018-06-23 stsp (*path)[0] = '/';
3770 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
3771 d9765a41 2018-06-23 stsp while (pt) {
3772 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3773 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3774 cb2ebc8a 2018-06-23 stsp goto done;
3776 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
3777 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3778 cb2ebc8a 2018-06-23 stsp goto done;
3780 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3783 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3784 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3785 ce52c690 2018-06-23 stsp goto done;
3789 ce52c690 2018-06-23 stsp if (err) {
3790 ce52c690 2018-06-23 stsp free(*path);
3791 ce52c690 2018-06-23 stsp *path = NULL;
3793 ce52c690 2018-06-23 stsp return err;
3796 ce52c690 2018-06-23 stsp static const struct got_error *
3797 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3798 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3799 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3800 8b473291 2019-02-21 stsp struct got_repository *repo)
3802 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3803 ce52c690 2018-06-23 stsp char *path;
3804 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3806 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3808 ce52c690 2018-06-23 stsp return err;
3810 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3811 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3812 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3814 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
3815 e5a0f69f 2018-08-18 stsp if (err) {
3816 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3817 e5a0f69f 2018-08-18 stsp free(path);
3819 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3820 69efd4c4 2018-07-18 stsp return err;
3823 69efd4c4 2018-07-18 stsp static const struct got_error *
3824 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3825 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3826 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3827 8b473291 2019-02-21 stsp struct got_repository *repo)
3829 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3830 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3831 69efd4c4 2018-07-18 stsp char *path;
3833 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3834 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3835 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3837 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3839 69efd4c4 2018-07-18 stsp return err;
3841 8b473291 2019-02-21 stsp err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3843 e5a0f69f 2018-08-18 stsp view_close(log_view);
3845 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3846 cb2ebc8a 2018-06-23 stsp free(path);
3847 cb2ebc8a 2018-06-23 stsp return err;
3850 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3851 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3852 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3853 8b473291 2019-02-21 stsp struct got_repository *repo)
3855 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3856 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3857 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3859 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3861 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3862 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3863 ffd1d5e5 2018-06-23 stsp goto done;
3865 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3866 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3867 ffd1d5e5 2018-06-23 stsp goto done;
3870 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3871 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3872 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3873 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3874 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3875 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3876 6484ec90 2018-09-29 stsp goto done;
3878 8b473291 2019-02-21 stsp s->refs = refs;
3879 fb2756b9 2018-08-04 stsp s->repo = repo;
3881 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
3882 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
3883 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
3885 ad80ab7b 2018-08-04 stsp free(commit_id_str);
3886 6484ec90 2018-09-29 stsp if (err) {
3887 fb2756b9 2018-08-04 stsp free(s->tree_label);
3888 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3890 ad80ab7b 2018-08-04 stsp return err;
3893 e5a0f69f 2018-08-18 stsp static const struct got_error *
3894 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
3896 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3898 fb2756b9 2018-08-04 stsp free(s->tree_label);
3899 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3900 6484ec90 2018-09-29 stsp free(s->commit_id);
3901 6484ec90 2018-09-29 stsp s->commit_id = NULL;
3902 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
3903 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
3904 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
3905 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
3906 ad80ab7b 2018-08-04 stsp free(parent);
3909 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
3910 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
3911 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
3913 e5a0f69f 2018-08-18 stsp return NULL;
3916 ad80ab7b 2018-08-04 stsp static const struct got_error *
3917 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
3919 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
3920 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3921 e5a0f69f 2018-08-18 stsp char *parent_path;
3923 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
3925 e5a0f69f 2018-08-18 stsp return err;
3927 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
3928 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
3929 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3930 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
3931 e5a0f69f 2018-08-18 stsp free(parent_path);
3933 669b5ffa 2018-10-07 stsp view_vborder(view);
3934 e5a0f69f 2018-08-18 stsp return err;
3937 e5a0f69f 2018-08-18 stsp static const struct got_error *
3938 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3939 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3941 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3942 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
3943 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
3944 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
3946 e5a0f69f 2018-08-18 stsp switch (ch) {
3948 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
3951 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
3953 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3954 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3955 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
3956 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
3957 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
3958 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3959 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3961 1e37a5c2 2019-05-12 jcs return err;
3962 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
3964 1e37a5c2 2019-05-12 jcs view_close(log_view);
3967 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
3968 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
3970 1e37a5c2 2019-05-12 jcs *new_view = log_view;
3973 1e37a5c2 2019-05-12 jcs case KEY_UP:
3974 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
3975 1e37a5c2 2019-05-12 jcs s->selected--;
3976 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
3979 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
3981 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
3982 1e37a5c2 2019-05-12 jcs s->entries, s->tree == s->root);
3984 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3985 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
3986 1e37a5c2 2019-05-12 jcs MAX(0, view->nlines - 4 - s->selected), s->entries,
3987 1e37a5c2 2019-05-12 jcs s->tree == s->root);
3988 1e37a5c2 2019-05-12 jcs s->selected = 0;
3989 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_FIRST(&s->entries->head) ==
3990 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
3991 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
3994 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3995 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
3996 1e37a5c2 2019-05-12 jcs s->selected++;
3999 00ba99a7 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4000 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
4002 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
4003 1e37a5c2 2019-05-12 jcs s->last_displayed_entry, s->entries);
4005 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4006 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4008 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
4009 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
4010 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4013 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
4014 1e37a5c2 2019-05-12 jcs view->nlines, s->last_displayed_entry, s->entries);
4015 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
4016 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
4017 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
4018 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
4020 1e37a5c2 2019-05-12 jcs ndisplayed++;
4021 1e37a5c2 2019-05-12 jcs te = SIMPLEQ_NEXT(te, entry);
4022 1e37a5c2 2019-05-12 jcs } while (te);
4023 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
4026 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4028 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4029 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4030 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
4031 1e37a5c2 2019-05-12 jcs /* user selected '..' */
4032 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
4034 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
4035 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
4037 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
4038 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
4039 1e37a5c2 2019-05-12 jcs s->entries =
4040 1e37a5c2 2019-05-12 jcs got_object_tree_get_entries(s->tree);
4041 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
4042 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
4043 1e37a5c2 2019-05-12 jcs s->selected_entry =
4044 1e37a5c2 2019-05-12 jcs parent->selected_entry;
4045 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
4046 1e37a5c2 2019-05-12 jcs free(parent);
4047 1e37a5c2 2019-05-12 jcs } else if (S_ISDIR(s->selected_entry->mode)) {
4048 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
4049 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&subtree,
4050 1e37a5c2 2019-05-12 jcs s->repo, s->selected_entry->id);
4053 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
4054 941e9f74 2019-05-21 stsp if (err) {
4055 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
4058 1e37a5c2 2019-05-12 jcs } else if (S_ISREG(s->selected_entry->mode)) {
4059 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
4060 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
4061 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
4063 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
4064 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
4065 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
4068 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
4069 669b5ffa 2018-10-07 stsp err = view_close_child(view);
4071 669b5ffa 2018-10-07 stsp return err;
4072 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
4073 669b5ffa 2018-10-07 stsp if (err) {
4074 1e37a5c2 2019-05-12 jcs view_close(blame_view);
4077 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
4078 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
4080 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
4083 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4084 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
4085 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4091 ffd1d5e5 2018-06-23 stsp return err;
4094 ffd1d5e5 2018-06-23 stsp __dead static void
4095 ffd1d5e5 2018-06-23 stsp usage_tree(void)
4098 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4099 ffd1d5e5 2018-06-23 stsp getprogname());
4103 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4104 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
4106 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
4107 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
4108 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4109 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
4110 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4111 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
4112 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
4113 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
4115 5221c383 2018-08-01 stsp struct tog_view *view;
4117 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4119 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
4120 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4121 d188b9a6 2019-01-04 stsp NULL) == -1)
4122 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
4125 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
4126 ffd1d5e5 2018-06-23 stsp switch (ch) {
4128 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
4131 17020d27 2019-03-07 stsp usage_tree();
4132 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
4136 ffd1d5e5 2018-06-23 stsp argc -= optind;
4137 ffd1d5e5 2018-06-23 stsp argv += optind;
4139 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
4140 52185f70 2019-02-05 stsp struct got_worktree *worktree;
4141 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
4142 52185f70 2019-02-05 stsp if (cwd == NULL)
4143 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
4144 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4145 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4146 52185f70 2019-02-05 stsp goto done;
4147 52185f70 2019-02-05 stsp if (worktree) {
4148 52185f70 2019-02-05 stsp free(cwd);
4149 52185f70 2019-02-05 stsp repo_path =
4150 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4151 52185f70 2019-02-05 stsp got_worktree_close(worktree);
4153 52185f70 2019-02-05 stsp repo_path = cwd;
4154 52185f70 2019-02-05 stsp if (repo_path == NULL) {
4155 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4156 52185f70 2019-02-05 stsp goto done;
4158 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
4159 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
4160 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
4161 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
4163 ffd1d5e5 2018-06-23 stsp usage_log();
4165 a915003a 2019-02-05 stsp init_curses();
4167 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
4168 c02c541e 2019-03-29 stsp if (error != NULL)
4169 52185f70 2019-02-05 stsp goto done;
4171 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4172 c02c541e 2019-03-29 stsp if (error)
4173 52185f70 2019-02-05 stsp goto done;
4175 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
4176 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4178 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
4179 15a94983 2018-12-23 stsp commit_id_arg);
4180 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4181 ffd1d5e5 2018-06-23 stsp goto done;
4183 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4184 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4185 ffd1d5e5 2018-06-23 stsp goto done;
4187 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
4188 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
4189 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4190 8b473291 2019-02-21 stsp goto done;
4192 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
4193 8b473291 2019-02-21 stsp if (error)
4194 ffd1d5e5 2018-06-23 stsp goto done;
4196 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4197 5221c383 2018-08-01 stsp if (view == NULL) {
4198 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4199 5221c383 2018-08-01 stsp goto done;
4201 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
4202 ad80ab7b 2018-08-04 stsp if (error)
4203 ad80ab7b 2018-08-04 stsp goto done;
4204 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4206 52185f70 2019-02-05 stsp free(repo_path);
4207 ffd1d5e5 2018-06-23 stsp free(commit_id);
4208 ffd1d5e5 2018-06-23 stsp if (commit)
4209 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
4211 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
4213 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
4214 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4215 ffd1d5e5 2018-06-23 stsp return error;
4218 4ed7e80c 2018-05-20 stsp __dead static void
4219 9f7d7167 2018-04-29 stsp usage(void)
4223 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
4224 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
4225 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
4226 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
4227 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
4232 c2301be8 2018-04-30 stsp static char **
4233 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
4235 c2301be8 2018-04-30 stsp char **argv;
4236 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
4238 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
4239 c2301be8 2018-04-30 stsp if (argv == NULL)
4240 c2301be8 2018-04-30 stsp err(1, "calloc");
4241 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
4242 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
4243 c2301be8 2018-04-30 stsp err(1, "calloc");
4244 c2301be8 2018-04-30 stsp if (arg1) {
4245 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
4246 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
4247 c2301be8 2018-04-30 stsp err(1, "calloc");
4250 c2301be8 2018-04-30 stsp return argv;
4254 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
4256 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
4257 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
4258 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
4259 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
4261 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
4263 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
4264 9f7d7167 2018-04-29 stsp switch (ch) {
4266 9f7d7167 2018-04-29 stsp hflag = 1;
4270 9f7d7167 2018-04-29 stsp /* NOTREACHED */
4274 9f7d7167 2018-04-29 stsp argc -= optind;
4275 9f7d7167 2018-04-29 stsp argv += optind;
4276 9f7d7167 2018-04-29 stsp optind = 0;
4277 c2301be8 2018-04-30 stsp optreset = 1;
4279 c2301be8 2018-04-30 stsp if (argc == 0) {
4280 f29d3e89 2018-06-23 stsp if (hflag)
4282 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
4283 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
4284 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
4289 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
4290 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
4291 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
4292 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
4293 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
4294 9f7d7167 2018-04-29 stsp if (hflag)
4295 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
4299 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
4300 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
4301 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
4302 c2301be8 2018-04-30 stsp if (repo_path) {
4303 c2301be8 2018-04-30 stsp struct got_repository *repo;
4304 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
4305 c2301be8 2018-04-30 stsp if (error == NULL)
4306 c2301be8 2018-04-30 stsp got_repo_close(repo);
4308 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath",
4310 c2301be8 2018-04-30 stsp if (error) {
4311 f29d3e89 2018-06-23 stsp if (hflag) {
4312 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
4313 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
4317 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
4318 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
4319 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
4320 ad7de8d9 2018-04-30 stsp free(repo_path);
4323 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
4324 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
4326 c2301be8 2018-04-30 stsp free(repo_path);
4330 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4331 9f7d7167 2018-04-29 stsp if (error)
4332 9f7d7167 2018-04-29 stsp goto done;
4335 c2301be8 2018-04-30 stsp free(cmd_argv);
4336 9f7d7167 2018-04-29 stsp if (error)
4337 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);