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);
74 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
75 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
76 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
77 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
78 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
80 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
81 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
82 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
83 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
85 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
86 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
87 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
88 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
89 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
92 d6b05b5b 2018-08-04 stsp enum tog_view_type {
93 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
94 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
95 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
96 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
99 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
101 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
102 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
103 ba4f502b 2018-08-04 stsp struct got_object_id *id;
104 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
107 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
108 ba4f502b 2018-08-04 stsp struct commit_queue {
109 ba4f502b 2018-08-04 stsp int ncommits;
110 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
113 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
114 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
116 15a087fe 2019-02-21 stsp int first_displayed_line;
117 15a087fe 2019-02-21 stsp int last_displayed_line;
119 15a087fe 2019-02-21 stsp int diff_context;
120 15a087fe 2019-02-21 stsp struct got_repository *repo;
121 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
123 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
124 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
127 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
129 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
130 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
131 1a76625f 2018-10-22 stsp int commits_needed;
132 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
133 1a76625f 2018-10-22 stsp struct commit_queue *commits;
134 1a76625f 2018-10-22 stsp const char *in_repo_path;
135 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
136 1a76625f 2018-10-22 stsp struct got_repository *repo;
137 1a76625f 2018-10-22 stsp int log_complete;
138 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
139 1a76625f 2018-10-22 stsp struct tog_view *view;
140 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
141 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
144 1a76625f 2018-10-22 stsp struct tog_log_view_state {
145 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
146 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
147 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
148 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
149 b01e7d3b 2018-08-04 stsp int selected;
150 b01e7d3b 2018-08-04 stsp char *in_repo_path;
151 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
152 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
153 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
154 1a76625f 2018-10-22 stsp sig_atomic_t quit;
155 1a76625f 2018-10-22 stsp pthread_t thread;
156 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
157 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
160 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
161 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
162 e9424729 2018-08-04 stsp int nlines;
164 e9424729 2018-08-04 stsp struct tog_view *view;
165 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
169 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
170 e9424729 2018-08-04 stsp const char *path;
171 e9424729 2018-08-04 stsp struct got_repository *repo;
172 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
173 e9424729 2018-08-04 stsp int *complete;
176 e9424729 2018-08-04 stsp struct tog_blame {
178 e9424729 2018-08-04 stsp size_t filesize;
179 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
180 6fcac457 2018-11-19 stsp int nlines;
181 e9424729 2018-08-04 stsp pthread_t thread;
182 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
183 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
184 e9424729 2018-08-04 stsp const char *path;
187 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
188 7cbe629d 2018-08-04 stsp int first_displayed_line;
189 7cbe629d 2018-08-04 stsp int last_displayed_line;
190 7cbe629d 2018-08-04 stsp int selected_line;
191 7cbe629d 2018-08-04 stsp int blame_complete;
194 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
195 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
196 e5a0f69f 2018-08-18 stsp char *path;
197 7cbe629d 2018-08-04 stsp struct got_repository *repo;
198 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
199 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
200 e9424729 2018-08-04 stsp struct tog_blame blame;
203 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
204 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
205 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
206 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
207 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
208 ad80ab7b 2018-08-04 stsp int selected;
211 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
213 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
214 ad80ab7b 2018-08-04 stsp char *tree_label;
215 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
216 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
217 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
218 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
219 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
220 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
221 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
222 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
223 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
224 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
225 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
226 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
230 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
232 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
233 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
234 669b5ffa 2018-10-07 stsp * there is enough screen estate.
236 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
237 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
239 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
240 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
241 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
243 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
244 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
246 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
248 cc3c9aac 2018-08-01 stsp struct tog_view {
249 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
250 26ed57b2 2018-05-19 stsp WINDOW *window;
251 26ed57b2 2018-05-19 stsp PANEL *panel;
252 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
253 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
254 1004088d 2018-09-29 stsp int focussed;
255 669b5ffa 2018-10-07 stsp struct tog_view *parent;
256 669b5ffa 2018-10-07 stsp struct tog_view *child;
257 669b5ffa 2018-10-07 stsp int child_focussed;
259 5dc9f4bc 2018-08-04 stsp /* type-specific state */
260 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
262 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
263 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
264 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
265 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
268 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
269 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
270 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
271 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
273 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
274 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
275 60493ae3 2019-06-20 stsp int searching;
276 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
277 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
278 60493ae3 2019-06-20 stsp int search_next_done;
279 1803e47f 2019-06-22 stsp regex_t regex;
282 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
283 fb872ab2 2019-02-21 stsp struct got_object_id *, struct got_object_id *, struct tog_view *,
284 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
285 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
286 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
287 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
288 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
290 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
291 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *,
292 8b473291 2019-02-21 stsp struct got_repository *, const char *, int);
293 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
294 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
295 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
296 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
297 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
298 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
300 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
301 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *, struct got_repository *);
302 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
303 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
304 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
305 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
307 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
308 8b473291 2019-02-21 stsp struct got_tree_object *, struct got_object_id *,
309 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
310 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
311 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
312 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
313 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
314 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
315 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
317 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
319 25791caa 2018-10-24 stsp static void
320 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
322 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
325 e5a0f69f 2018-08-18 stsp static const struct got_error *
326 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
328 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
330 669b5ffa 2018-10-07 stsp if (view->child) {
331 669b5ffa 2018-10-07 stsp view_close(view->child);
332 669b5ffa 2018-10-07 stsp view->child = NULL;
334 e5a0f69f 2018-08-18 stsp if (view->close)
335 e5a0f69f 2018-08-18 stsp err = view->close(view);
336 ea5e7bb5 2018-08-01 stsp if (view->panel)
337 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
338 ea5e7bb5 2018-08-01 stsp if (view->window)
339 ea5e7bb5 2018-08-01 stsp delwin(view->window);
340 ea5e7bb5 2018-08-01 stsp free(view);
341 e5a0f69f 2018-08-18 stsp return err;
344 ea5e7bb5 2018-08-01 stsp static struct tog_view *
345 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
346 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
348 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
350 ea5e7bb5 2018-08-01 stsp if (view == NULL)
351 ea5e7bb5 2018-08-01 stsp return NULL;
353 d6b05b5b 2018-08-04 stsp view->type = type;
354 f7d12f7e 2018-08-01 stsp view->lines = LINES;
355 f7d12f7e 2018-08-01 stsp view->cols = COLS;
356 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
357 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
358 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
359 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
360 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
361 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
362 96a765a8 2018-08-04 stsp view_close(view);
363 ea5e7bb5 2018-08-01 stsp return NULL;
365 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
366 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
367 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
368 96a765a8 2018-08-04 stsp view_close(view);
369 ea5e7bb5 2018-08-01 stsp return NULL;
372 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
373 ea5e7bb5 2018-08-01 stsp return view;
377 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
379 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
381 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
384 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
386 5c60c32a 2018-10-18 stsp static const struct got_error *
387 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
389 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
391 5c60c32a 2018-10-18 stsp view->begin_y = 0;
392 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
393 5c60c32a 2018-10-18 stsp view->nlines = LINES;
394 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
395 5c60c32a 2018-10-18 stsp view->lines = LINES;
396 5c60c32a 2018-10-18 stsp view->cols = COLS;
397 5c60c32a 2018-10-18 stsp err = view_resize(view);
399 5c60c32a 2018-10-18 stsp return err;
401 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
402 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
404 5c60c32a 2018-10-18 stsp return NULL;
407 5c60c32a 2018-10-18 stsp static const struct got_error *
408 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
410 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
412 5c60c32a 2018-10-18 stsp view->begin_x = 0;
413 5c60c32a 2018-10-18 stsp view->begin_y = 0;
414 5c60c32a 2018-10-18 stsp view->nlines = LINES;
415 5c60c32a 2018-10-18 stsp view->ncols = COLS;
416 5c60c32a 2018-10-18 stsp view->lines = LINES;
417 5c60c32a 2018-10-18 stsp view->cols = COLS;
418 5c60c32a 2018-10-18 stsp err = view_resize(view);
420 5c60c32a 2018-10-18 stsp return err;
422 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
423 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
425 5c60c32a 2018-10-18 stsp return NULL;
429 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
431 5c60c32a 2018-10-18 stsp return view->parent == NULL;
434 4d8c2215 2018-08-19 stsp static const struct got_error *
435 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
437 f7d12f7e 2018-08-01 stsp int nlines, ncols;
439 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
440 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
442 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
444 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
445 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
447 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
449 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
450 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
451 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
452 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
453 25791caa 2018-10-24 stsp wclear(view->window);
455 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
456 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
457 0cf4efb1 2018-09-29 stsp view->lines = LINES;
458 0cf4efb1 2018-09-29 stsp view->cols = COLS;
460 6e3e5d9c 2018-10-18 stsp if (view->child) {
461 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
462 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
463 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
464 5c60c32a 2018-10-18 stsp if (view->child->focussed)
465 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
467 5c60c32a 2018-10-18 stsp show_panel(view->panel);
469 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
470 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
474 5c60c32a 2018-10-18 stsp return NULL;
477 669b5ffa 2018-10-07 stsp static const struct got_error *
478 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
480 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
482 669b5ffa 2018-10-07 stsp if (view->child == NULL)
483 669b5ffa 2018-10-07 stsp return NULL;
485 669b5ffa 2018-10-07 stsp err = view_close(view->child);
486 669b5ffa 2018-10-07 stsp view->child = NULL;
487 669b5ffa 2018-10-07 stsp return err;
490 669b5ffa 2018-10-07 stsp static const struct got_error *
491 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
493 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
495 669b5ffa 2018-10-07 stsp view->child = child;
496 669b5ffa 2018-10-07 stsp child->parent = view;
497 669b5ffa 2018-10-07 stsp return err;
501 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
503 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
506 34bc9ec9 2019-02-22 stsp static void
507 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
509 25791caa 2018-10-24 stsp int cols, lines;
510 25791caa 2018-10-24 stsp struct winsize size;
512 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
513 25791caa 2018-10-24 stsp cols = 80; /* Default */
514 25791caa 2018-10-24 stsp lines = 24;
516 25791caa 2018-10-24 stsp cols = size.ws_col;
517 25791caa 2018-10-24 stsp lines = size.ws_row;
519 25791caa 2018-10-24 stsp resize_term(lines, cols);
522 2b49a8ae 2019-06-22 stsp static const struct got_error *
523 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
525 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
526 2b49a8ae 2019-06-22 stsp char pattern[1024];
529 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
530 2b49a8ae 2019-06-22 stsp return NULL;
532 2b49a8ae 2019-06-22 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1,
533 2b49a8ae 2019-06-22 stsp view->begin_x, "/");
534 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
536 2b49a8ae 2019-06-22 stsp nocbreak();
538 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
541 2b49a8ae 2019-06-22 stsp if (ret == ERR)
542 2b49a8ae 2019-06-22 stsp return NULL;
544 2b49a8ae 2019-06-22 stsp if (view->searching) {
545 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
546 2b49a8ae 2019-06-22 stsp view->searching = 0;
549 2b49a8ae 2019-06-22 stsp if (regcomp(&view->regex, pattern,
550 2b49a8ae 2019-06-22 stsp REG_EXTENDED | REG_ICASE | REG_NOSUB | REG_NEWLINE) == 0) {
551 7c32bd05 2019-06-22 stsp err = view->search_start(view);
553 7c32bd05 2019-06-22 stsp regfree(&view->regex);
554 7c32bd05 2019-06-22 stsp return err;
556 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
557 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
558 2b49a8ae 2019-06-22 stsp view->search_next(view);
561 2b49a8ae 2019-06-22 stsp return NULL;
564 0cf4efb1 2018-09-29 stsp static const struct got_error *
565 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
566 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
567 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
569 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
570 669b5ffa 2018-10-07 stsp struct tog_view *v;
571 1a76625f 2018-10-22 stsp int ch, errcode;
573 e5a0f69f 2018-08-18 stsp *new = NULL;
574 e5a0f69f 2018-08-18 stsp *dead = NULL;
575 0cf4efb1 2018-09-29 stsp *focus = NULL;
577 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
578 60493ae3 2019-06-20 stsp errcode = pthread_mutex_unlock(&tog_mutex);
579 60493ae3 2019-06-20 stsp if (errcode)
580 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
581 60493ae3 2019-06-20 stsp "pthread_mutex_unlock");
582 60493ae3 2019-06-20 stsp pthread_yield();
583 60493ae3 2019-06-20 stsp errcode = pthread_mutex_lock(&tog_mutex);
584 60493ae3 2019-06-20 stsp if (errcode)
585 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
586 60493ae3 2019-06-20 stsp "pthread_mutex_lock");
587 60493ae3 2019-06-20 stsp view->search_next(view);
588 60493ae3 2019-06-20 stsp return NULL;
591 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
592 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
593 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
594 1a76625f 2018-10-22 stsp if (errcode)
595 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
596 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
597 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
598 1a76625f 2018-10-22 stsp if (errcode)
599 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
600 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
602 25791caa 2018-10-24 stsp if (tog_sigwinch_received) {
603 25791caa 2018-10-24 stsp tog_resizeterm();
604 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
605 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
606 25791caa 2018-10-24 stsp err = view_resize(v);
608 25791caa 2018-10-24 stsp return err;
609 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
611 25791caa 2018-10-24 stsp return err;
615 e5a0f69f 2018-08-18 stsp switch (ch) {
619 1e37a5c2 2019-05-12 jcs if (view->child) {
620 1e37a5c2 2019-05-12 jcs *focus = view->child;
621 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
622 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
623 1e37a5c2 2019-05-12 jcs *focus = view->parent;
624 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
628 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
629 1e37a5c2 2019-05-12 jcs *dead = view;
635 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
636 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
638 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
639 669b5ffa 2018-10-07 stsp *focus = view->child;
640 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
641 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
643 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
646 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
647 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
649 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
650 1e37a5c2 2019-05-12 jcs *focus = view;
651 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
652 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
654 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
658 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
659 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
662 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
665 60493ae3 2019-06-20 stsp if (view->search_start)
666 2b49a8ae 2019-06-22 stsp view_search_start(view);
668 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
672 60493ae3 2019-06-20 stsp if (view->search_next && view->searching) {
673 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
674 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
675 60493ae3 2019-06-20 stsp view->search_next_done = 0;
676 60493ae3 2019-06-20 stsp view->search_next(view);
678 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
681 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
685 e5a0f69f 2018-08-18 stsp return err;
689 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
691 0cf4efb1 2018-09-29 stsp PANEL *panel;
692 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
694 669b5ffa 2018-10-07 stsp if (view->parent)
695 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
697 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
698 0cf4efb1 2018-09-29 stsp if (panel == NULL)
701 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
702 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
703 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
707 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
709 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
710 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
712 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
714 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
717 669b5ffa 2018-10-07 stsp return view->focussed;
720 bcbd79e2 2018-08-19 stsp static const struct got_error *
721 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
723 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
724 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
725 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
726 fd823528 2018-10-22 stsp int fast_refresh = 10;
727 1a76625f 2018-10-22 stsp int done = 0, errcode;
729 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
730 1a76625f 2018-10-22 stsp if (errcode)
731 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
733 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
734 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
736 a81bf10d 2018-09-29 stsp main_view = view;
737 1004088d 2018-09-29 stsp view->focussed = 1;
738 878940b7 2018-09-29 stsp err = view->show(view);
740 0cf4efb1 2018-09-29 stsp return err;
741 0cf4efb1 2018-09-29 stsp update_panels();
742 0cf4efb1 2018-09-29 stsp doupdate();
743 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
744 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
745 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
746 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
748 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
749 e4197bf9 2018-08-18 stsp view, &views);
752 e5a0f69f 2018-08-18 stsp if (dead_view) {
753 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
755 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
756 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
757 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
758 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
759 669b5ffa 2018-10-07 stsp prev = view->parent;
761 669b5ffa 2018-10-07 stsp if (dead_view->parent)
762 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
764 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
766 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
767 a81bf10d 2018-09-29 stsp if (err || dead_view == main_view)
770 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
771 0cf4efb1 2018-09-29 stsp if (focus_view)
772 0cf4efb1 2018-09-29 stsp view = focus_view;
773 669b5ffa 2018-10-07 stsp else if (prev)
774 669b5ffa 2018-10-07 stsp view = prev;
775 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
776 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
777 0cf4efb1 2018-09-29 stsp tog_view_list_head);
779 0cf4efb1 2018-09-29 stsp view = NULL;
780 669b5ffa 2018-10-07 stsp if (view) {
781 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
782 669b5ffa 2018-10-07 stsp focus_view = view->child;
784 669b5ffa 2018-10-07 stsp focus_view = view;
788 bcbd79e2 2018-08-19 stsp if (new_view) {
789 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
790 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
791 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
792 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
794 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
795 86c66b02 2018-10-18 stsp err = view_close(v);
800 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
801 fed7eaa8 2018-10-24 stsp view = new_view;
802 878940b7 2018-09-29 stsp if (focus_view == NULL)
803 878940b7 2018-09-29 stsp focus_view = new_view;
805 0cf4efb1 2018-09-29 stsp if (focus_view) {
806 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
808 0cf4efb1 2018-09-29 stsp view->focussed = 0;
809 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
810 0cf4efb1 2018-09-29 stsp view = focus_view;
811 878940b7 2018-09-29 stsp if (new_view)
812 878940b7 2018-09-29 stsp show_panel(new_view->panel);
813 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
814 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
816 669b5ffa 2018-10-07 stsp if (view) {
817 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
818 758194b5 2018-10-24 stsp view->focussed = 1;
819 758194b5 2018-10-24 stsp show_panel(view->panel);
820 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
821 758194b5 2018-10-24 stsp show_panel(view->child->panel);
822 1a76625f 2018-10-22 stsp focus_view = view;
824 669b5ffa 2018-10-07 stsp if (view->parent) {
825 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
829 669b5ffa 2018-10-07 stsp err = view->show(view);
832 669b5ffa 2018-10-07 stsp if (view->child) {
833 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
837 1a76625f 2018-10-22 stsp update_panels();
838 1a76625f 2018-10-22 stsp doupdate();
842 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
843 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
844 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
845 e5a0f69f 2018-08-18 stsp view_close(view);
848 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
849 1a76625f 2018-10-22 stsp if (errcode)
850 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
852 e5a0f69f 2018-08-18 stsp return err;
855 4ed7e80c 2018-05-20 stsp __dead static void
856 9f7d7167 2018-04-29 stsp usage_log(void)
859 c70c5802 2018-08-01 stsp fprintf(stderr,
860 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
861 9f7d7167 2018-04-29 stsp getprogname());
865 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
866 80ddbec8 2018-04-29 stsp static const struct got_error *
867 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
869 00dfcb92 2018-06-11 stsp char *vis = NULL;
870 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
872 963b370f 2018-05-20 stsp *ws = NULL;
873 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
874 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
875 00dfcb92 2018-06-11 stsp int vislen;
876 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
877 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
879 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
880 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
882 00dfcb92 2018-06-11 stsp return err;
883 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
884 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
885 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
890 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
891 a7f50699 2018-06-11 stsp if (*ws == NULL) {
892 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
896 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
897 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
902 963b370f 2018-05-20 stsp *ws = NULL;
905 963b370f 2018-05-20 stsp return err;
908 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
909 963b370f 2018-05-20 stsp static const struct got_error *
910 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
912 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
913 963b370f 2018-05-20 stsp int cols = 0;
914 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
915 963b370f 2018-05-20 stsp size_t wlen;
918 963b370f 2018-05-20 stsp *wlinep = NULL;
919 b700b5d6 2018-07-10 stsp *widthp = 0;
921 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
923 963b370f 2018-05-20 stsp return err;
926 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
927 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
928 963b370f 2018-05-20 stsp switch (width) {
934 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
935 b700b5d6 2018-07-10 stsp cols += width;
939 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
940 67ceb59d 2019-03-03 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
944 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
948 963b370f 2018-05-20 stsp wline[i] = L'\0';
949 b700b5d6 2018-07-10 stsp if (widthp)
950 b700b5d6 2018-07-10 stsp *widthp = cols;
953 963b370f 2018-05-20 stsp free(wline);
955 963b370f 2018-05-20 stsp *wlinep = wline;
956 963b370f 2018-05-20 stsp return err;
959 8b473291 2019-02-21 stsp static const struct got_error*
960 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
961 8b473291 2019-02-21 stsp struct got_object_id *id)
963 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
964 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
966 8b473291 2019-02-21 stsp const char *name;
968 8b473291 2019-02-21 stsp *refs_str = NULL;
970 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
971 8b473291 2019-02-21 stsp if (got_object_id_cmp(re->id, id) != 0)
973 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
974 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
976 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
978 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
980 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
982 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
984 8b473291 2019-02-21 stsp s = *refs_str;
985 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
986 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
987 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
989 8b473291 2019-02-21 stsp *refs_str = NULL;
995 8b473291 2019-02-21 stsp return err;
998 963b370f 2018-05-20 stsp static const struct got_error *
999 5813d178 2019-03-09 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1001 5813d178 2019-03-09 stsp char *smallerthan, *at;
1003 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1004 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1005 5813d178 2019-03-09 stsp author = smallerthan + 1;
1006 5813d178 2019-03-09 stsp at = strchr(author, '@');
1008 5813d178 2019-03-09 stsp *at = '\0';
1009 5813d178 2019-03-09 stsp return format_line(wauthor, author_width, author, limit);
1012 5813d178 2019-03-09 stsp static const struct got_error *
1013 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1014 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
1015 5813d178 2019-03-09 stsp int author_display_cols)
1017 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1018 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1019 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1020 5813d178 2019-03-09 stsp char *author = NULL;
1021 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1022 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1023 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1024 bb737323 2018-05-20 stsp int col, limit;
1025 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
1026 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1027 ccb26ccd 2018-11-05 stsp struct tm tm;
1028 45d799e2 2018-12-23 stsp time_t committer_time;
1030 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1031 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1032 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1033 ccb26ccd 2018-11-05 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1034 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1035 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1037 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
1038 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1040 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1041 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1042 b39d25c7 2018-07-10 stsp col = limit + 1;
1043 b39d25c7 2018-07-10 stsp if (col > avail)
1044 b39d25c7 2018-07-10 stsp goto done;
1046 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1047 5813d178 2019-03-09 stsp if (author == NULL) {
1048 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1049 80ddbec8 2018-04-29 stsp goto done;
1051 5813d178 2019-03-09 stsp err = format_author(&wauthor, &author_width, author, avail - col);
1053 bb737323 2018-05-20 stsp goto done;
1054 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1055 bb737323 2018-05-20 stsp col += author_width;
1056 5813d178 2019-03-09 stsp while (col <= avail && author_width < author_display_cols + 2) {
1057 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1059 bb737323 2018-05-20 stsp author_width++;
1061 9c2eaf34 2018-05-20 stsp if (col > avail)
1062 9c2eaf34 2018-05-20 stsp goto done;
1064 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1065 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
1066 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1067 6d9fbc00 2018-04-29 stsp goto done;
1069 bb737323 2018-05-20 stsp logmsg = logmsg0;
1070 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1072 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1073 bb737323 2018-05-20 stsp if (newline)
1074 bb737323 2018-05-20 stsp *newline = '\0';
1075 bb737323 2018-05-20 stsp limit = avail - col;
1076 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1078 bb737323 2018-05-20 stsp goto done;
1079 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1080 bb737323 2018-05-20 stsp col += logmsg_width;
1081 bb737323 2018-05-20 stsp while (col <= avail) {
1082 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1086 80ddbec8 2018-04-29 stsp free(logmsg0);
1087 bb737323 2018-05-20 stsp free(wlogmsg);
1088 5813d178 2019-03-09 stsp free(author);
1089 bb737323 2018-05-20 stsp free(wauthor);
1090 80ddbec8 2018-04-29 stsp free(line);
1091 80ddbec8 2018-04-29 stsp return err;
1094 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1095 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1096 899d86c2 2018-05-10 stsp struct got_object_id *id)
1098 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1100 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1101 80ddbec8 2018-04-29 stsp if (entry == NULL)
1102 899d86c2 2018-05-10 stsp return NULL;
1104 899d86c2 2018-05-10 stsp entry->id = id;
1105 99db9666 2018-05-07 stsp entry->commit = commit;
1106 899d86c2 2018-05-10 stsp return entry;
1109 99db9666 2018-05-07 stsp static void
1110 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1112 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1114 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1115 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1116 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1117 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1118 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1119 99db9666 2018-05-07 stsp free(entry);
1122 99db9666 2018-05-07 stsp static void
1123 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1125 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1126 99db9666 2018-05-07 stsp pop_commit(commits);
1129 c4972b91 2018-05-07 stsp static const struct got_error *
1130 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1131 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
1133 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1134 93e45b7c 2018-09-24 stsp int nqueued = 0;
1137 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1138 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1139 1a76625f 2018-10-22 stsp * while updating the display.
1141 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
1142 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1143 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1144 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1145 1a76625f 2018-10-22 stsp int errcode;
1147 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1148 9ba79e04 2018-06-11 stsp if (err) {
1149 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1151 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
1152 93e45b7c 2018-09-24 stsp minqueue, repo);
1154 ecb28ae0 2018-07-16 stsp return err;
1158 ecb28ae0 2018-07-16 stsp if (id == NULL)
1161 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1164 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1165 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1166 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1170 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1171 1a76625f 2018-10-22 stsp if (errcode) {
1172 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_lock");
1176 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1177 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1178 ecb28ae0 2018-07-16 stsp nqueued++;
1179 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1181 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1182 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1183 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1184 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1187 9ba79e04 2018-06-11 stsp return err;
1190 99db9666 2018-05-07 stsp static const struct got_error *
1191 19e70ad6 2019-05-14 stsp get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1192 19e70ad6 2019-05-14 stsp struct got_repository *repo)
1194 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1195 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1197 9ba79e04 2018-06-11 stsp *head_id = NULL;
1199 19e70ad6 2019-05-14 stsp err = got_ref_open(&head_ref, repo, branch_name, 0);
1201 99db9666 2018-05-07 stsp return err;
1203 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1204 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1205 9ba79e04 2018-06-11 stsp if (err) {
1206 9ba79e04 2018-06-11 stsp *head_id = NULL;
1207 99db9666 2018-05-07 stsp return err;
1210 9ba79e04 2018-06-11 stsp return NULL;
1213 0553a4e3 2018-04-30 stsp static const struct got_error *
1214 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1215 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1216 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1217 8b473291 2019-02-21 stsp struct got_reflist_head *refs, const char *path, int commits_needed)
1219 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1220 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1221 60493ae3 2019-06-20 stsp int width;
1222 60493ae3 2019-06-20 stsp int ncommits, author_cols = 10;
1223 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1224 8b473291 2019-02-21 stsp char *refs_str = NULL;
1225 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1227 e0d42f60 2018-07-22 stsp entry = first;
1228 e0d42f60 2018-07-22 stsp ncommits = 0;
1229 e0d42f60 2018-07-22 stsp while (entry) {
1230 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1231 e0d42f60 2018-07-22 stsp *selected = entry;
1234 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1235 e0d42f60 2018-07-22 stsp ncommits++;
1238 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1239 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1241 ecb28ae0 2018-07-16 stsp return err;
1242 8b473291 2019-02-21 stsp if (refs) {
1243 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, (*selected)->id);
1245 8b473291 2019-02-21 stsp goto done;
1249 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1250 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1252 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1253 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1254 60493ae3 2019-06-20 stsp commits_needed > 0 ?
1255 60493ae3 2019-06-20 stsp (view->searching && view->search_next_done == 0
1256 60493ae3 2019-06-20 stsp ? "searching..." : "loading... ") :
1257 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1258 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1259 8b473291 2019-02-21 stsp goto done;
1262 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1263 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1264 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1265 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1266 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1267 1a76625f 2018-10-22 stsp header = NULL;
1268 1a76625f 2018-10-22 stsp goto done;
1270 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1271 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1272 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1273 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1274 1a76625f 2018-10-22 stsp header = NULL;
1275 1a76625f 2018-10-22 stsp goto done;
1277 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1279 1a76625f 2018-10-22 stsp goto done;
1281 2814baeb 2018-08-01 stsp werase(view->window);
1283 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1284 a3404814 2018-09-02 stsp wstandout(view->window);
1285 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1286 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1287 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1290 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1291 a3404814 2018-09-02 stsp wstandend(view->window);
1292 ecb28ae0 2018-07-16 stsp free(wline);
1293 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1294 1a76625f 2018-10-22 stsp goto done;
1296 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1297 899d86c2 2018-05-10 stsp entry = first;
1298 5813d178 2019-03-09 stsp ncommits = 0;
1299 5813d178 2019-03-09 stsp while (entry) {
1300 5813d178 2019-03-09 stsp char *author;
1301 5813d178 2019-03-09 stsp wchar_t *wauthor;
1302 5813d178 2019-03-09 stsp int width;
1303 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1305 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1306 5813d178 2019-03-09 stsp if (author == NULL) {
1307 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1308 5813d178 2019-03-09 stsp goto done;
1310 5813d178 2019-03-09 stsp err = format_author(&wauthor, &width, author, COLS);
1311 5813d178 2019-03-09 stsp if (author_cols < width)
1312 5813d178 2019-03-09 stsp author_cols = width;
1313 5813d178 2019-03-09 stsp free(wauthor);
1314 5813d178 2019-03-09 stsp free(author);
1315 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1318 5813d178 2019-03-09 stsp entry = first;
1319 899d86c2 2018-05-10 stsp *last = first;
1320 867c6645 2018-07-10 stsp ncommits = 0;
1321 899d86c2 2018-05-10 stsp while (entry) {
1322 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1324 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1325 2814baeb 2018-08-01 stsp wstandout(view->window);
1326 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1327 5813d178 2019-03-09 stsp author_cols);
1328 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1329 2814baeb 2018-08-01 stsp wstandend(view->window);
1331 60493ae3 2019-06-20 stsp goto done;
1332 0553a4e3 2018-04-30 stsp ncommits++;
1333 899d86c2 2018-05-10 stsp *last = entry;
1334 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1337 1a57306a 2018-09-02 stsp view_vborder(view);
1339 1a76625f 2018-10-22 stsp free(id_str);
1340 8b473291 2019-02-21 stsp free(refs_str);
1341 1a76625f 2018-10-22 stsp free(ncommits_str);
1342 1a76625f 2018-10-22 stsp free(header);
1343 80ddbec8 2018-04-29 stsp return err;
1346 07b55e75 2018-05-10 stsp static void
1347 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1348 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1349 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1351 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1352 07b55e75 2018-05-10 stsp int nscrolled = 0;
1354 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1355 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1358 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1359 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1360 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1361 07b55e75 2018-05-10 stsp if (entry) {
1362 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1363 07b55e75 2018-05-10 stsp nscrolled++;
1368 aa075928 2018-05-10 stsp static const struct got_error *
1369 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1370 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1372 5e224a3e 2019-02-22 stsp int errcode;
1373 8a42fca8 2019-02-22 stsp int max_wait = 20;
1375 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1377 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1378 5e224a3e 2019-02-22 stsp if (*log_complete)
1381 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1382 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1383 7aafa0d1 2019-02-22 stsp if (errcode)
1384 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1385 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1386 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1387 7aafa0d1 2019-02-22 stsp if (errcode)
1388 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1389 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1390 7aafa0d1 2019-02-22 stsp pthread_yield();
1391 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1392 7aafa0d1 2019-02-22 stsp if (errcode)
1393 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1394 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1396 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1398 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1399 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1400 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1401 5e224a3e 2019-02-22 stsp * with few commits in history.
1403 7aafa0d1 2019-02-22 stsp return NULL;
1407 5e224a3e 2019-02-22 stsp return NULL;
1410 5e224a3e 2019-02-22 stsp static const struct got_error *
1411 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1412 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1413 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1414 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1415 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1417 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1418 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1419 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1421 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1422 5e224a3e 2019-02-22 stsp return NULL;
1424 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1425 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1427 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1428 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1430 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1431 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1432 5e224a3e 2019-02-22 stsp need_commits);
1434 5e224a3e 2019-02-22 stsp return err;
1438 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1439 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1442 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1444 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1445 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1447 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1448 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1450 dd0a52c1 2018-05-20 stsp return err;
1453 cd0acaa7 2018-05-20 stsp static const struct got_error *
1454 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1455 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1456 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1457 8b473291 2019-02-21 stsp struct got_repository *repo)
1459 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1460 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1461 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1463 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1464 15a94983 2018-12-23 stsp if (diff_view == NULL)
1465 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1467 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1468 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1469 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1470 e5a0f69f 2018-08-18 stsp if (err == NULL)
1471 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1472 cd0acaa7 2018-05-20 stsp return err;
1475 80ddbec8 2018-04-29 stsp static const struct got_error *
1476 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1477 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1479 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1481 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1482 941e9f74 2019-05-21 stsp if (parent == NULL)
1483 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1485 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1486 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1487 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1488 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1489 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1490 941e9f74 2019-05-21 stsp s->tree = subtree;
1491 941e9f74 2019-05-21 stsp s->entries = got_object_tree_get_entries(s->tree);
1492 941e9f74 2019-05-21 stsp s->selected = 0;
1493 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1494 941e9f74 2019-05-21 stsp return NULL;
1498 941e9f74 2019-05-21 stsp static const struct got_error *
1499 941e9f74 2019-05-21 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1500 941e9f74 2019-05-21 stsp struct commit_queue_entry *entry, const char *path,
1501 941e9f74 2019-05-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
1503 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1504 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1505 941e9f74 2019-05-21 stsp struct got_tree_entry *te;
1506 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s;
1507 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1508 b03c880f 2019-05-21 stsp char *slash, *subpath = NULL;
1509 941e9f74 2019-05-21 stsp const char *p;
1511 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1512 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1514 9343a5fb 2018-06-23 stsp return err;
1516 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1517 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1518 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1520 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1521 941e9f74 2019-05-21 stsp if (err) {
1522 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1523 941e9f74 2019-05-21 stsp return err;
1525 941e9f74 2019-05-21 stsp s = &tree_view->state.tree;
1527 941e9f74 2019-05-21 stsp *new_view = tree_view;
1529 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1531 941e9f74 2019-05-21 stsp while (*p) {
1532 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1534 941e9f74 2019-05-21 stsp while (p[0] == '/')
1537 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1538 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1539 941e9f74 2019-05-21 stsp if (slash == NULL)
1540 941e9f74 2019-05-21 stsp slash = strchr(p, '\0');
1541 941e9f74 2019-05-21 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1542 941e9f74 2019-05-21 stsp if (strncmp(p, te->name, slash - p) == 0) {
1543 941e9f74 2019-05-21 stsp s->selected_entry = te;
1546 b03c880f 2019-05-21 stsp s->selected++;
1548 941e9f74 2019-05-21 stsp if (s->selected_entry == NULL) {
1549 941e9f74 2019-05-21 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1552 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1553 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1555 67409a31 2019-05-24 stsp if (!S_ISDIR(s->selected_entry->mode)) {
1556 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1557 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1558 67409a31 2019-05-24 stsp s->selected = 0;
1562 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1563 941e9f74 2019-05-21 stsp if (slash)
1564 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1566 941e9f74 2019-05-21 stsp subpath = strdup(path);
1567 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1568 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1572 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1577 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1578 941e9f74 2019-05-21 stsp free(tree_id);
1582 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1583 941e9f74 2019-05-21 stsp if (err) {
1584 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1587 941e9f74 2019-05-21 stsp if (slash == NULL)
1589 941e9f74 2019-05-21 stsp free(subpath);
1590 941e9f74 2019-05-21 stsp subpath = NULL;
1591 941e9f74 2019-05-21 stsp p = slash;
1594 941e9f74 2019-05-21 stsp free(subpath);
1595 1a76625f 2018-10-22 stsp return err;
1598 1a76625f 2018-10-22 stsp static void *
1599 1a76625f 2018-10-22 stsp log_thread(void *arg)
1601 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1602 1a76625f 2018-10-22 stsp int errcode = 0;
1603 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1604 1a76625f 2018-10-22 stsp int done = 0;
1606 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1608 1a76625f 2018-10-22 stsp return (void *)err;
1610 1a76625f 2018-10-22 stsp while (!done && !err) {
1611 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1612 1a76625f 2018-10-22 stsp a->in_repo_path);
1613 1a76625f 2018-10-22 stsp if (err) {
1614 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1615 1a76625f 2018-10-22 stsp return (void *)err;
1616 1a76625f 2018-10-22 stsp err = NULL;
1618 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1619 1a76625f 2018-10-22 stsp a->commits_needed--;
1621 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1622 3abe8080 2019-04-10 stsp if (errcode) {
1623 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1624 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1626 3abe8080 2019-04-10 stsp } else if (*a->quit)
1628 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1629 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1630 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1631 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1635 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1636 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1637 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1638 1a76625f 2018-10-22 stsp &tog_mutex);
1639 1a76625f 2018-10-22 stsp if (errcode)
1640 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1641 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1644 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1645 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1646 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1647 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1649 3abe8080 2019-04-10 stsp a->log_complete = 1;
1650 1a76625f 2018-10-22 stsp return (void *)err;
1653 1a76625f 2018-10-22 stsp static const struct got_error *
1654 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1656 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1657 1a76625f 2018-10-22 stsp int errcode;
1659 1a76625f 2018-10-22 stsp if (s->thread) {
1660 1a76625f 2018-10-22 stsp s->quit = 1;
1661 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1662 1a76625f 2018-10-22 stsp if (errcode)
1663 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1664 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1665 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1666 1a76625f 2018-10-22 stsp if (errcode)
1667 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1668 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1669 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1670 1a76625f 2018-10-22 stsp if (errcode)
1671 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1672 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1673 1a76625f 2018-10-22 stsp if (errcode)
1674 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1675 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1676 1a76625f 2018-10-22 stsp s->thread = NULL;
1679 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1680 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1681 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1683 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1684 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1685 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1688 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1689 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1690 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1693 9343a5fb 2018-06-23 stsp return err;
1696 9343a5fb 2018-06-23 stsp static const struct got_error *
1697 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1699 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1700 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1702 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1703 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1704 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1705 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1706 1a76625f 2018-10-22 stsp free(s->start_id);
1707 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1708 1a76625f 2018-10-22 stsp return err;
1711 1a76625f 2018-10-22 stsp static const struct got_error *
1712 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
1714 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1716 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
1717 60493ae3 2019-06-20 stsp return NULL;
1720 60493ae3 2019-06-20 stsp static int
1721 60493ae3 2019-06-20 stsp match_commit(struct got_commit_object *commit, regex_t *regex)
1723 60493ae3 2019-06-20 stsp regmatch_t regmatch;
1725 60493ae3 2019-06-20 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1726 60493ae3 2019-06-20 stsp ®match, 0) == 0 ||
1727 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1728 60493ae3 2019-06-20 stsp ®match, 0) == 0 ||
1729 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_logmsg(commit), 1,
1730 60493ae3 2019-06-20 stsp ®match, 0) == 0)
1736 60493ae3 2019-06-20 stsp static const struct got_error *
1737 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
1739 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
1740 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1741 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
1743 60493ae3 2019-06-20 stsp if (!view->searching) {
1744 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1745 60493ae3 2019-06-20 stsp return NULL;
1748 b1bf1435 2019-06-21 stsp if (s->matched_entry) {
1749 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1750 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
1752 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
1753 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
1755 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1756 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
1758 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1761 60493ae3 2019-06-20 stsp while (1) {
1762 60493ae3 2019-06-20 stsp if (entry == NULL) {
1763 60493ae3 2019-06-20 stsp if (s->thread_args.log_complete) {
1764 60493ae3 2019-06-20 stsp if (s->matched_entry == NULL) {
1765 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1766 60493ae3 2019-06-20 stsp return NULL;
1768 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1769 60493ae3 2019-06-20 stsp entry = TAILQ_FIRST(&s->commits.head);
1771 b1bf1435 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head,
1772 b1bf1435 2019-06-21 stsp commit_queue_head);
1774 60493ae3 2019-06-20 stsp s->thread_args.commits_needed = 1;
1775 60493ae3 2019-06-20 stsp return trigger_log_thread(0,
1776 60493ae3 2019-06-20 stsp &s->thread_args.commits_needed,
1777 60493ae3 2019-06-20 stsp &s->thread_args.log_complete,
1778 60493ae3 2019-06-20 stsp &s->thread_args.need_commits);
1782 1803e47f 2019-06-22 stsp if (match_commit(entry->commit, &view->regex)) {
1783 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1784 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
1787 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1788 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
1790 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1793 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
1794 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
1795 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
1796 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1798 60493ae3 2019-06-20 stsp return err;
1801 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
1802 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1804 60493ae3 2019-06-20 stsp return err;
1809 60493ae3 2019-06-20 stsp return NULL;
1812 60493ae3 2019-06-20 stsp static const struct got_error *
1813 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1814 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
1815 8b473291 2019-02-21 stsp const char *path, int check_disk)
1817 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1818 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1819 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1820 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1821 1a76625f 2018-10-22 stsp int errcode;
1823 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1824 ecb28ae0 2018-07-16 stsp if (err != NULL)
1825 ecb28ae0 2018-07-16 stsp goto done;
1827 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1828 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1829 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1831 8b473291 2019-02-21 stsp s->refs = refs;
1832 fb2756b9 2018-08-04 stsp s->repo = repo;
1833 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1834 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1835 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1836 5036bf37 2018-09-24 stsp goto done;
1839 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1840 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1841 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1842 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
1843 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
1845 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1847 1a76625f 2018-10-22 stsp goto done;
1848 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1849 1a76625f 2018-10-22 stsp 0, thread_repo);
1851 1a76625f 2018-10-22 stsp goto done;
1853 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1854 1a76625f 2018-10-22 stsp if (errcode) {
1855 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
1856 1a76625f 2018-10-22 stsp goto done;
1859 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1860 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1861 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1862 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1863 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1864 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1865 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1866 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1867 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1868 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1869 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1872 1a76625f 2018-10-22 stsp close_log_view(view);
1873 ba4f502b 2018-08-04 stsp return err;
1876 e5a0f69f 2018-08-18 stsp static const struct got_error *
1877 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1879 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1881 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1882 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1883 2b380cc8 2018-10-24 stsp &s->thread_args);
1884 2b380cc8 2018-10-24 stsp if (errcode)
1885 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
1888 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1889 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1890 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
1891 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1894 e5a0f69f 2018-08-18 stsp static const struct got_error *
1895 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1896 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1898 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1899 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1900 5036bf37 2018-09-24 stsp char *parent_path;
1901 669b5ffa 2018-10-07 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
1902 669b5ffa 2018-10-07 stsp int begin_x = 0;
1904 e5a0f69f 2018-08-18 stsp switch (ch) {
1906 1e37a5c2 2019-05-12 jcs s->quit = 1;
1909 1e37a5c2 2019-05-12 jcs case KEY_UP:
1912 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1914 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
1915 1e37a5c2 2019-05-12 jcs s->selected--;
1917 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
1918 1144d21a 2019-06-21 stsp &s->commits);
1920 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
1921 a4292ac5 2019-05-12 jcs case CTRL('b'):
1922 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1924 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
1925 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
1926 1e37a5c2 2019-05-12 jcs s->selected = 0;
1929 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
1930 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
1933 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
1936 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1938 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
1939 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1940 1e37a5c2 2019-05-12 jcs s->selected++;
1943 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
1944 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
1945 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
1946 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1947 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1949 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
1950 a4292ac5 2019-05-12 jcs case CTRL('f'): {
1951 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
1952 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
1953 1e37a5c2 2019-05-12 jcs if (first == NULL)
1955 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
1956 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
1957 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
1958 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1959 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1960 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
1961 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
1962 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1963 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
1964 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
1965 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
1967 1e37a5c2 2019-05-12 jcs err = NULL;
1970 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1971 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
1972 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
1973 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
1974 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
1976 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
1979 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
1981 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
1982 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
1983 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
1984 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
1985 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
1988 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1989 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
1991 1e37a5c2 2019-05-12 jcs return err;
1992 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
1994 1e37a5c2 2019-05-12 jcs view_close(diff_view);
1997 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
1998 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2000 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2003 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2005 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2006 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2007 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2008 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2011 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2012 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2014 1e37a5c2 2019-05-12 jcs return err;
2015 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2017 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2020 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2021 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2023 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2025 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2026 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2028 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2029 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2030 1e37a5c2 2019-05-12 jcs struct tog_view *lv;
2031 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2033 1e37a5c2 2019-05-12 jcs return err;
2034 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2035 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2036 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2037 638f9024 2019-05-13 stsp return got_error_from_errno(
2038 1e37a5c2 2019-05-12 jcs "view_open");
2039 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2040 1e37a5c2 2019-05-12 jcs s->repo, parent_path, 0);
2042 1e37a5c2 2019-05-12 jcs return err;;
2043 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2044 1e37a5c2 2019-05-12 jcs *new_view = lv;
2046 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2047 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2049 1e37a5c2 2019-05-12 jcs return NULL;
2056 80ddbec8 2018-04-29 stsp return err;
2059 4ed7e80c 2018-05-20 stsp static const struct got_error *
2060 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2062 c2db6724 2019-01-04 stsp const struct got_error *error;
2064 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2065 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2067 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2068 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2070 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2071 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2073 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2074 c2db6724 2019-01-04 stsp if (error != NULL)
2075 c2db6724 2019-01-04 stsp return error;
2077 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2078 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2080 c2db6724 2019-01-04 stsp return NULL;
2083 a915003a 2019-02-05 stsp static void
2084 a915003a 2019-02-05 stsp init_curses(void)
2086 a915003a 2019-02-05 stsp initscr();
2088 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2091 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2092 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2093 a915003a 2019-02-05 stsp curs_set(0);
2094 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2097 c2db6724 2019-01-04 stsp static const struct got_error *
2098 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2100 80ddbec8 2018-04-29 stsp const struct got_error *error;
2101 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2102 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2103 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2104 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2105 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2106 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
2108 04cc582a 2018-08-01 stsp struct tog_view *view;
2110 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2112 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2113 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2114 c2db6724 2019-01-04 stsp NULL) == -1)
2115 80ddbec8 2018-04-29 stsp err(1, "pledge");
2118 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2119 80ddbec8 2018-04-29 stsp switch (ch) {
2121 80ddbec8 2018-04-29 stsp start_commit = optarg;
2124 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2125 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2126 ecb28ae0 2018-07-16 stsp err(1, "-r option");
2129 17020d27 2019-03-07 stsp usage_log();
2130 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2134 80ddbec8 2018-04-29 stsp argc -= optind;
2135 80ddbec8 2018-04-29 stsp argv += optind;
2137 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2138 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2139 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2140 ecb28ae0 2018-07-16 stsp goto done;
2142 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2143 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2144 963f97a1 2019-03-18 stsp goto done;
2145 963f97a1 2019-03-18 stsp error = NULL;
2147 963f97a1 2019-03-18 stsp if (argc == 0) {
2148 963f97a1 2019-03-18 stsp path = strdup("");
2149 963f97a1 2019-03-18 stsp if (path == NULL) {
2150 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2151 ecb28ae0 2018-07-16 stsp goto done;
2153 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2154 963f97a1 2019-03-18 stsp if (worktree) {
2155 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2157 963f97a1 2019-03-18 stsp if (error)
2158 963f97a1 2019-03-18 stsp goto done;
2160 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2161 963f97a1 2019-03-18 stsp if (path == NULL) {
2162 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2163 963f97a1 2019-03-18 stsp goto done;
2167 963f97a1 2019-03-18 stsp usage_log();
2169 963f97a1 2019-03-18 stsp repo_path = worktree ?
2170 963f97a1 2019-03-18 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2171 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2172 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2173 963f97a1 2019-03-18 stsp goto done;
2176 a915003a 2019-02-05 stsp init_curses();
2178 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
2179 80ddbec8 2018-04-29 stsp if (error != NULL)
2180 ecb28ae0 2018-07-16 stsp goto done;
2182 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2183 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2184 c02c541e 2019-03-29 stsp if (error)
2185 c02c541e 2019-03-29 stsp goto done;
2187 15a94983 2018-12-23 stsp if (start_commit == NULL)
2188 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&start_id, worktree ?
2189 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2192 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&start_id, repo,
2193 15a94983 2018-12-23 stsp start_commit);
2194 80ddbec8 2018-04-29 stsp if (error != NULL)
2195 8b473291 2019-02-21 stsp goto done;
2197 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2198 8b473291 2019-02-21 stsp if (error)
2199 ecb28ae0 2018-07-16 stsp goto done;
2201 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2202 04cc582a 2018-08-01 stsp if (view == NULL) {
2203 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2204 04cc582a 2018-08-01 stsp goto done;
2206 8b473291 2019-02-21 stsp error = open_log_view(view, start_id, &refs, repo, path, 1);
2207 ba4f502b 2018-08-04 stsp if (error)
2208 ba4f502b 2018-08-04 stsp goto done;
2209 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2211 ecb28ae0 2018-07-16 stsp free(repo_path);
2212 ecb28ae0 2018-07-16 stsp free(cwd);
2213 ecb28ae0 2018-07-16 stsp free(path);
2214 899d86c2 2018-05-10 stsp free(start_id);
2216 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2217 ec142235 2019-03-07 stsp if (worktree)
2218 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2219 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2220 80ddbec8 2018-04-29 stsp return error;
2223 4ed7e80c 2018-05-20 stsp __dead static void
2224 9f7d7167 2018-04-29 stsp usage_diff(void)
2227 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2228 9f7d7167 2018-04-29 stsp getprogname());
2232 b304db33 2018-05-20 stsp static char *
2233 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2235 b304db33 2018-05-20 stsp char *line;
2236 b304db33 2018-05-20 stsp size_t linelen;
2237 b304db33 2018-05-20 stsp size_t lineno;
2238 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2240 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2242 b304db33 2018-05-20 stsp *len = linelen;
2243 b304db33 2018-05-20 stsp return line;
2246 4ed7e80c 2018-05-20 stsp static const struct got_error *
2247 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2248 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
2249 c3e9aa98 2019-05-13 jcs char *header)
2251 61e69b96 2018-05-20 stsp const struct got_error *err;
2252 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2253 b304db33 2018-05-20 stsp char *line;
2254 b304db33 2018-05-20 stsp size_t len;
2255 61e69b96 2018-05-20 stsp wchar_t *wline;
2256 e0b650dd 2018-05-20 stsp int width;
2258 26ed57b2 2018-05-19 stsp rewind(f);
2259 f7d12f7e 2018-08-01 stsp werase(view->window);
2261 a3404814 2018-09-02 stsp if (header) {
2262 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
2263 a3404814 2018-09-02 stsp if (err) {
2264 a3404814 2018-09-02 stsp return err;
2267 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2268 a3404814 2018-09-02 stsp wstandout(view->window);
2269 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2270 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2271 a3404814 2018-09-02 stsp wstandend(view->window);
2272 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2273 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2275 a3404814 2018-09-02 stsp if (max_lines <= 1)
2276 a3404814 2018-09-02 stsp return NULL;
2277 a3404814 2018-09-02 stsp max_lines--;
2281 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2282 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2283 26ed57b2 2018-05-19 stsp if (line == NULL) {
2287 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2288 26ed57b2 2018-05-19 stsp free(line);
2292 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2293 61e69b96 2018-05-20 stsp if (err) {
2294 61e69b96 2018-05-20 stsp free(line);
2295 61e69b96 2018-05-20 stsp return err;
2297 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2298 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2299 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2300 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2301 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2302 26ed57b2 2018-05-19 stsp free(line);
2303 2550e4c3 2018-07-13 stsp free(wline);
2304 2550e4c3 2018-07-13 stsp wline = NULL;
2306 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2308 1a57306a 2018-09-02 stsp view_vborder(view);
2310 c3e9aa98 2019-05-13 jcs if (*eof) {
2311 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2312 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2313 c3e9aa98 2019-05-13 jcs nprinted++;
2316 c3e9aa98 2019-05-13 jcs err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2318 c3e9aa98 2019-05-13 jcs return err;
2321 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2322 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2323 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2326 26ed57b2 2018-05-19 stsp return NULL;
2329 abd2672a 2018-12-23 stsp static char *
2330 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2332 abd2672a 2018-12-23 stsp char *p, *s = ctime_r(time, datebuf);
2333 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2335 abd2672a 2018-12-23 stsp *p = '\0';
2339 4ed7e80c 2018-05-20 stsp static const struct got_error *
2340 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2341 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2343 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2344 abd2672a 2018-12-23 stsp char datebuf[26];
2345 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2346 15a94983 2018-12-23 stsp char *id_str = NULL;
2347 45d799e2 2018-12-23 stsp time_t committer_time;
2348 45d799e2 2018-12-23 stsp const char *author, *committer;
2349 8b473291 2019-02-21 stsp char *refs_str = NULL;
2351 8b473291 2019-02-21 stsp if (refs) {
2352 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, commit_id);
2354 8b473291 2019-02-21 stsp return err;
2357 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2359 abd2672a 2018-12-23 stsp return err;
2361 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2362 15a94983 2018-12-23 stsp if (err) {
2363 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2364 15a94983 2018-12-23 stsp goto done;
2367 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2368 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2369 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2370 abd2672a 2018-12-23 stsp goto done;
2372 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2373 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2374 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2375 abd2672a 2018-12-23 stsp goto done;
2377 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2378 abd2672a 2018-12-23 stsp if (fprintf(outfile, "date: %s UTC\n",
2379 45d799e2 2018-12-23 stsp get_datestr(&committer_time, datebuf)) < 0) {
2380 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2381 abd2672a 2018-12-23 stsp goto done;
2383 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2384 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2385 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2386 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2387 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2388 abd2672a 2018-12-23 stsp goto done;
2390 45d799e2 2018-12-23 stsp if (fprintf(outfile, "%s\n",
2391 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(commit)) < 0) {
2392 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2393 abd2672a 2018-12-23 stsp goto done;
2396 abd2672a 2018-12-23 stsp free(id_str);
2397 8b473291 2019-02-21 stsp free(refs_str);
2398 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2399 abd2672a 2018-12-23 stsp return err;
2402 abd2672a 2018-12-23 stsp static const struct got_error *
2403 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2405 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2406 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2407 15a94983 2018-12-23 stsp int obj_type;
2409 511a516b 2018-05-19 stsp f = got_opentemp();
2410 48ae06ee 2018-10-18 stsp if (f == NULL) {
2411 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2412 48ae06ee 2018-10-18 stsp goto done;
2414 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2415 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2416 fb43ecf1 2019-02-11 stsp goto done;
2420 15a94983 2018-12-23 stsp if (s->id1)
2421 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2423 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2425 15a94983 2018-12-23 stsp goto done;
2427 15a94983 2018-12-23 stsp switch (obj_type) {
2428 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2429 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2430 54156555 2018-12-24 stsp s->diff_context, s->repo, f);
2432 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2433 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2434 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
2436 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2437 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2438 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2439 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2441 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2444 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2445 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2446 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2448 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2449 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2450 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2451 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2452 8b473291 2019-02-21 stsp s->repo, f);
2457 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2459 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2460 15a94983 2018-12-23 stsp s->diff_context, s->repo, f);
2464 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2468 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2469 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2470 48ae06ee 2018-10-18 stsp return err;
2473 f5215bb9 2019-02-22 stsp static void
2474 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2476 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2477 f5215bb9 2019-02-22 stsp update_panels();
2478 f5215bb9 2019-02-22 stsp doupdate();
2481 48ae06ee 2018-10-18 stsp static const struct got_error *
2482 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2483 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2484 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2486 48ae06ee 2018-10-18 stsp const struct got_error *err;
2488 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2489 15a94983 2018-12-23 stsp int type1, type2;
2490 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2492 15a94983 2018-12-23 stsp return err;
2493 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2495 15a94983 2018-12-23 stsp return err;
2497 15a94983 2018-12-23 stsp if (type1 != type2)
2498 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2501 15a94983 2018-12-23 stsp if (id1) {
2502 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2503 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2504 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2506 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2508 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2509 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2510 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2511 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2512 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2514 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2515 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2516 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2517 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2518 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2519 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2520 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2522 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2523 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2524 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2526 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2527 48ae06ee 2018-10-18 stsp if (err) {
2528 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2529 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2530 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2531 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2532 48ae06ee 2018-10-18 stsp return err;
2535 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2536 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2537 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2539 5dc9f4bc 2018-08-04 stsp return NULL;
2542 e5a0f69f 2018-08-18 stsp static const struct got_error *
2543 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2545 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2547 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2548 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2549 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2550 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2551 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2552 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2553 e5a0f69f 2018-08-18 stsp return err;
2556 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2557 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2559 a3404814 2018-09-02 stsp const struct got_error *err;
2560 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2561 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2563 a3404814 2018-09-02 stsp if (s->id1) {
2564 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2566 a3404814 2018-09-02 stsp return err;
2568 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2570 a3404814 2018-09-02 stsp return err;
2572 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2573 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2574 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2575 a3404814 2018-09-02 stsp free(id_str1);
2576 a3404814 2018-09-02 stsp free(id_str2);
2577 a3404814 2018-09-02 stsp return err;
2579 a3404814 2018-09-02 stsp free(id_str1);
2580 a3404814 2018-09-02 stsp free(id_str2);
2582 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2583 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2587 15a087fe 2019-02-21 stsp static const struct got_error *
2588 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2589 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2591 d7a04538 2019-02-21 stsp const struct got_error *err;
2592 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
2593 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
2594 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
2596 15a087fe 2019-02-21 stsp free(s->id2);
2597 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2598 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2599 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2601 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2603 d7a04538 2019-02-21 stsp return err;
2604 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
2605 15a087fe 2019-02-21 stsp free(s->id1);
2606 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
2607 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2608 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
2609 15a087fe 2019-02-21 stsp return NULL;
2612 0cf4efb1 2018-09-29 stsp static const struct got_error *
2613 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2614 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2616 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2617 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2618 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
2619 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
2622 e5a0f69f 2018-08-18 stsp switch (ch) {
2624 1e37a5c2 2019-05-12 jcs case KEY_UP:
2625 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
2626 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2628 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2629 a60a9dc4 2019-05-13 jcs case CTRL('b'):
2630 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
2633 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
2634 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
2635 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2638 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2639 1e37a5c2 2019-05-12 jcs if (!s->eof)
2640 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2642 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
2643 a60a9dc4 2019-05-13 jcs case CTRL('f'):
2645 00ba99a7 2019-05-12 jcs if (s->eof)
2648 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
2649 1e37a5c2 2019-05-12 jcs char *line;
2650 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
2651 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2652 1e37a5c2 2019-05-12 jcs if (line == NULL)
2657 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
2658 1e37a5c2 2019-05-12 jcs s->diff_context--;
2659 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2660 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2664 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2665 1e37a5c2 2019-05-12 jcs s->diff_context++;
2666 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2667 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2672 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2674 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2675 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
2676 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
2677 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2680 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2685 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2689 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2690 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2692 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2693 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2697 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2699 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2701 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2702 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
2704 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
2705 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
2706 1e37a5c2 2019-05-12 jcs update_panels();
2707 1e37a5c2 2019-05-12 jcs doupdate();
2709 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
2710 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
2711 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
2712 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
2716 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2721 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
2722 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2725 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2729 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2730 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2732 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2733 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2739 bcbd79e2 2018-08-19 stsp return err;
2742 4ed7e80c 2018-05-20 stsp static const struct got_error *
2743 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2745 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2746 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2747 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2748 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2749 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2750 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2752 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2754 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2756 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2757 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2758 eb6600df 2019-01-04 stsp NULL) == -1)
2759 26ed57b2 2018-05-19 stsp err(1, "pledge");
2762 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2763 26ed57b2 2018-05-19 stsp switch (ch) {
2765 17020d27 2019-03-07 stsp usage_diff();
2766 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2770 26ed57b2 2018-05-19 stsp argc -= optind;
2771 26ed57b2 2018-05-19 stsp argv += optind;
2773 26ed57b2 2018-05-19 stsp if (argc == 0) {
2774 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2775 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2776 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2777 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2778 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2779 15a94983 2018-12-23 stsp id_str1 = argv[0];
2780 15a94983 2018-12-23 stsp id_str2 = argv[1];
2781 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2782 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2783 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2784 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2785 15a94983 2018-12-23 stsp id_str1 = argv[1];
2786 15a94983 2018-12-23 stsp id_str2 = argv[2];
2788 26ed57b2 2018-05-19 stsp usage_diff();
2790 a915003a 2019-02-05 stsp init_curses();
2792 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2793 eb6600df 2019-01-04 stsp if (error)
2794 eb6600df 2019-01-04 stsp goto done;
2796 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
2797 26ed57b2 2018-05-19 stsp if (error)
2798 26ed57b2 2018-05-19 stsp goto done;
2800 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
2801 26ed57b2 2018-05-19 stsp if (error)
2802 26ed57b2 2018-05-19 stsp goto done;
2804 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
2805 26ed57b2 2018-05-19 stsp if (error)
2806 26ed57b2 2018-05-19 stsp goto done;
2808 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2809 8b473291 2019-02-21 stsp if (error)
2810 8b473291 2019-02-21 stsp goto done;
2812 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2813 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2814 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2815 ea5e7bb5 2018-08-01 stsp goto done;
2817 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2818 5dc9f4bc 2018-08-04 stsp if (error)
2819 5dc9f4bc 2018-08-04 stsp goto done;
2820 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2822 c02c541e 2019-03-29 stsp free(repo_path);
2823 26ed57b2 2018-05-19 stsp got_repo_close(repo);
2824 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2825 26ed57b2 2018-05-19 stsp return error;
2828 4ed7e80c 2018-05-20 stsp __dead static void
2829 9f7d7167 2018-04-29 stsp usage_blame(void)
2832 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2833 9f7d7167 2018-04-29 stsp getprogname());
2837 84451b3e 2018-07-10 stsp struct tog_blame_line {
2838 84451b3e 2018-07-10 stsp int annotated;
2839 84451b3e 2018-07-10 stsp struct got_object_id *id;
2842 4ed7e80c 2018-05-20 stsp static const struct got_error *
2843 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2844 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2845 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2846 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2848 84451b3e 2018-07-10 stsp const struct got_error *err;
2849 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2850 84451b3e 2018-07-10 stsp char *line;
2851 84451b3e 2018-07-10 stsp size_t len;
2852 84451b3e 2018-07-10 stsp wchar_t *wline;
2853 b700b5d6 2018-07-10 stsp int width, wlimit;
2854 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2855 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2856 ab089a2a 2018-07-12 stsp char *id_str;
2858 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2860 ab089a2a 2018-07-12 stsp return err;
2862 84451b3e 2018-07-10 stsp rewind(f);
2863 f7d12f7e 2018-08-01 stsp werase(view->window);
2865 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
2866 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2867 ab089a2a 2018-07-12 stsp free(id_str);
2868 ab089a2a 2018-07-12 stsp return err;
2871 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2872 ab089a2a 2018-07-12 stsp free(line);
2873 2550e4c3 2018-07-13 stsp line = NULL;
2874 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2875 a3404814 2018-09-02 stsp wstandout(view->window);
2876 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2877 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2878 a3404814 2018-09-02 stsp wstandend(view->window);
2879 2550e4c3 2018-07-13 stsp free(wline);
2880 2550e4c3 2018-07-13 stsp wline = NULL;
2881 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2882 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2884 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2885 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2886 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
2887 ab089a2a 2018-07-12 stsp free(id_str);
2888 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2890 ab089a2a 2018-07-12 stsp free(id_str);
2891 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2892 3f60a8ef 2018-07-10 stsp free(line);
2893 2550e4c3 2018-07-13 stsp line = NULL;
2895 3f60a8ef 2018-07-10 stsp return err;
2896 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2897 2550e4c3 2018-07-13 stsp free(wline);
2898 2550e4c3 2018-07-13 stsp wline = NULL;
2899 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2900 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2903 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2904 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2905 84451b3e 2018-07-10 stsp if (line == NULL) {
2909 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2910 84451b3e 2018-07-10 stsp free(line);
2914 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2915 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2916 84451b3e 2018-07-10 stsp if (err) {
2917 84451b3e 2018-07-10 stsp free(line);
2918 84451b3e 2018-07-10 stsp return err;
2921 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2922 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2924 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2925 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2926 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2927 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2928 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2929 84451b3e 2018-07-10 stsp char *id_str;
2930 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2931 84451b3e 2018-07-10 stsp if (err) {
2932 84451b3e 2018-07-10 stsp free(line);
2933 2550e4c3 2018-07-13 stsp free(wline);
2934 84451b3e 2018-07-10 stsp return err;
2936 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2937 84451b3e 2018-07-10 stsp free(id_str);
2938 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2940 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2941 ee41ec32 2018-07-10 stsp prev_id = NULL;
2944 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2945 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2946 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2949 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2950 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2951 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2952 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2953 84451b3e 2018-07-10 stsp free(line);
2954 2550e4c3 2018-07-13 stsp free(wline);
2955 2550e4c3 2018-07-13 stsp wline = NULL;
2957 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2959 1a57306a 2018-09-02 stsp view_vborder(view);
2961 84451b3e 2018-07-10 stsp return NULL;
2964 84451b3e 2018-07-10 stsp static const struct got_error *
2965 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2967 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2968 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2969 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2970 1a76625f 2018-10-22 stsp int errcode;
2972 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2973 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2974 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2976 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2977 1a76625f 2018-10-22 stsp if (errcode)
2978 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
2980 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2981 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2982 d68a0a7d 2018-07-10 stsp goto done;
2985 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2986 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2988 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2989 d68a0a7d 2018-07-10 stsp if (line->annotated)
2990 d68a0a7d 2018-07-10 stsp goto done;
2992 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2993 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2994 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2995 84451b3e 2018-07-10 stsp goto done;
2997 84451b3e 2018-07-10 stsp line->annotated = 1;
2999 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3000 1a76625f 2018-10-22 stsp if (errcode)
3001 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3002 84451b3e 2018-07-10 stsp return err;
3005 84451b3e 2018-07-10 stsp static void *
3006 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3008 18430de3 2018-07-10 stsp const struct got_error *err;
3009 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3010 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3011 1a76625f 2018-10-22 stsp int errcode;
3013 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3014 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
3016 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3017 1a76625f 2018-10-22 stsp if (errcode)
3018 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3019 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3021 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3022 c9beca56 2018-07-22 stsp ta->repo = NULL;
3023 c9beca56 2018-07-22 stsp *ta->complete = 1;
3025 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3026 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3027 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3029 18430de3 2018-07-10 stsp return (void *)err;
3032 245d91c1 2018-07-12 stsp static struct got_object_id *
3033 15a94983 2018-12-23 stsp get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3034 15a94983 2018-12-23 stsp int selected_line)
3036 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3038 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3039 245d91c1 2018-07-12 stsp if (!line->annotated)
3040 245d91c1 2018-07-12 stsp return NULL;
3042 245d91c1 2018-07-12 stsp return line->id;
3045 b880a918 2018-07-10 stsp static const struct got_error *
3046 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3048 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3051 245d91c1 2018-07-12 stsp if (blame->thread) {
3052 1a76625f 2018-10-22 stsp int errcode;
3053 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3054 1a76625f 2018-10-22 stsp if (errcode)
3055 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3056 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3057 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3058 1a76625f 2018-10-22 stsp if (errcode)
3059 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3060 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3061 1a76625f 2018-10-22 stsp if (errcode)
3062 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3063 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3064 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3065 245d91c1 2018-07-12 stsp err = NULL;
3066 245d91c1 2018-07-12 stsp blame->thread = NULL;
3068 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3069 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3070 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3072 245d91c1 2018-07-12 stsp if (blame->f) {
3073 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3074 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3075 245d91c1 2018-07-12 stsp blame->f = NULL;
3077 57670559 2018-12-23 stsp if (blame->lines) {
3078 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3079 57670559 2018-12-23 stsp free(blame->lines[i].id);
3080 57670559 2018-12-23 stsp free(blame->lines);
3081 57670559 2018-12-23 stsp blame->lines = NULL;
3083 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3084 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3086 245d91c1 2018-07-12 stsp return err;
3089 245d91c1 2018-07-12 stsp static const struct got_error *
3090 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3091 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3092 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3093 245d91c1 2018-07-12 stsp struct got_repository *repo)
3095 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3096 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3097 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3098 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3099 15a94983 2018-12-23 stsp int obj_type;
3101 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3103 15a94983 2018-12-23 stsp return err;
3104 15a94983 2018-12-23 stsp if (obj_id == NULL)
3105 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
3107 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3109 84451b3e 2018-07-10 stsp goto done;
3111 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3112 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3113 84451b3e 2018-07-10 stsp goto done;
3116 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3118 a70480e0 2018-06-23 stsp goto done;
3119 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3120 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3121 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3122 84451b3e 2018-07-10 stsp goto done;
3124 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3125 245d91c1 2018-07-12 stsp blame->f, blob);
3127 84451b3e 2018-07-10 stsp goto done;
3129 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3130 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3131 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3132 84451b3e 2018-07-10 stsp goto done;
3135 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3137 bd24772e 2018-07-11 stsp goto done;
3139 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3140 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3141 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3142 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3143 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3144 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3145 245d91c1 2018-07-12 stsp goto done;
3147 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3149 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3150 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3151 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3152 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3153 245d91c1 2018-07-12 stsp *blame_complete = 0;
3157 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3158 27d434c2 2018-09-15 stsp free(obj_id);
3160 245d91c1 2018-07-12 stsp stop_blame(blame);
3161 245d91c1 2018-07-12 stsp return err;
3164 245d91c1 2018-07-12 stsp static const struct got_error *
3165 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3166 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3167 8b473291 2019-02-21 stsp struct got_repository *repo)
3169 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3170 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3172 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3174 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3176 7cbe629d 2018-08-04 stsp return err;
3178 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3179 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3180 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3181 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3182 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3183 fb2756b9 2018-08-04 stsp s->path = path;
3184 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
3185 638f9024 2019-05-13 stsp return got_error_from_errno("open_blame_view");
3186 fb2756b9 2018-08-04 stsp s->repo = repo;
3187 8b473291 2019-02-21 stsp s->refs = refs;
3188 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3189 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3191 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3192 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3193 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3195 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3196 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3197 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3198 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3201 e5a0f69f 2018-08-18 stsp static const struct got_error *
3202 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3204 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3205 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3207 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3208 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3210 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3211 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3212 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3213 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3214 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3217 e5a0f69f 2018-08-18 stsp free(s->path);
3219 e5a0f69f 2018-08-18 stsp return err;
3222 7cbe629d 2018-08-04 stsp static const struct got_error *
3223 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
3225 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3226 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
3227 2b380cc8 2018-10-24 stsp int errcode;
3229 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
3230 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3231 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
3232 2b380cc8 2018-10-24 stsp if (errcode)
3233 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3236 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3237 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3238 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
3239 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
3241 669b5ffa 2018-10-07 stsp view_vborder(view);
3242 e5a0f69f 2018-08-18 stsp return err;
3245 e5a0f69f 2018-08-18 stsp static const struct got_error *
3246 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3247 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3249 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
3250 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
3251 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3252 669b5ffa 2018-10-07 stsp int begin_x = 0;
3254 e5a0f69f 2018-08-18 stsp switch (ch) {
3256 1e37a5c2 2019-05-12 jcs s->done = 1;
3259 1e37a5c2 2019-05-12 jcs case KEY_UP:
3260 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
3261 1e37a5c2 2019-05-12 jcs s->selected_line--;
3262 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
3263 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3264 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3266 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3267 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
3268 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3271 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3272 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3273 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3275 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3278 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3279 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3280 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3281 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3282 1e37a5c2 2019-05-12 jcs s->selected_line++;
3283 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3284 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3285 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3288 1e37a5c2 2019-05-12 jcs case 'p': {
3289 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3290 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3291 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3292 1e37a5c2 2019-05-12 jcs if (id == NULL)
3294 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3295 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3296 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3297 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3298 1e37a5c2 2019-05-12 jcs int obj_type;
3299 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3300 1e37a5c2 2019-05-12 jcs s->repo, id);
3303 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3304 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3305 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3306 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3309 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3310 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3311 1e37a5c2 2019-05-12 jcs pid->id, s->path);
3312 e5a0f69f 2018-08-18 stsp if (err) {
3313 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
3314 1e37a5c2 2019-05-12 jcs err = NULL;
3315 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3318 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
3320 1e37a5c2 2019-05-12 jcs free(blob_id);
3321 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
3322 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
3323 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3326 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3328 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3330 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
3331 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
3333 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3338 1e37a5c2 2019-05-12 jcs s->done = 1;
3339 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3340 1e37a5c2 2019-05-12 jcs s->done = 0;
3341 1e37a5c2 2019-05-12 jcs if (thread_err)
3343 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3344 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
3345 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3346 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3347 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
3348 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
3353 1e37a5c2 2019-05-12 jcs case 'B': {
3354 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
3355 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
3356 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
3358 1e37a5c2 2019-05-12 jcs s->done = 1;
3359 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3360 1e37a5c2 2019-05-12 jcs s->done = 0;
3361 1e37a5c2 2019-05-12 jcs if (thread_err)
3363 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3364 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
3365 1e37a5c2 2019-05-12 jcs s->blamed_commit =
3366 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
3367 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3368 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3369 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
3370 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
3375 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3376 1e37a5c2 2019-05-12 jcs case '\r': {
3377 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3378 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
3379 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
3380 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3381 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3382 1e37a5c2 2019-05-12 jcs if (id == NULL)
3384 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
3387 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
3388 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
3389 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3390 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3391 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3392 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
3393 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3394 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
3397 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
3398 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
3399 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3401 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3404 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3405 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3408 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
3410 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3413 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
3414 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
3416 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3421 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3423 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3424 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
3425 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
3428 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3429 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
3430 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3431 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3434 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
3435 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
3436 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
3437 1e37a5c2 2019-05-12 jcs view->nlines - 2;
3439 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
3440 1e37a5c2 2019-05-12 jcs s->blame.nlines -
3441 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
3443 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3444 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
3445 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3446 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3452 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
3455 a70480e0 2018-06-23 stsp static const struct got_error *
3456 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
3458 a70480e0 2018-06-23 stsp const struct got_error *error;
3459 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
3460 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3461 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
3462 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3463 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3464 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
3466 e1cd8fed 2018-08-01 stsp struct tog_view *view;
3468 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3470 a70480e0 2018-06-23 stsp #ifndef PROFILE
3471 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3472 8e94dd5b 2019-01-04 stsp NULL) == -1)
3473 a70480e0 2018-06-23 stsp err(1, "pledge");
3476 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3477 a70480e0 2018-06-23 stsp switch (ch) {
3479 a70480e0 2018-06-23 stsp commit_id_str = optarg;
3482 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3483 69069811 2018-08-02 stsp if (repo_path == NULL)
3484 69069811 2018-08-02 stsp err(1, "-r option");
3487 17020d27 2019-03-07 stsp usage_blame();
3488 a70480e0 2018-06-23 stsp /* NOTREACHED */
3492 a70480e0 2018-06-23 stsp argc -= optind;
3493 a70480e0 2018-06-23 stsp argv += optind;
3495 69069811 2018-08-02 stsp if (argc == 1)
3496 69069811 2018-08-02 stsp path = argv[0];
3498 a70480e0 2018-06-23 stsp usage_blame();
3500 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
3501 69069811 2018-08-02 stsp if (cwd == NULL) {
3502 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3503 69069811 2018-08-02 stsp goto done;
3505 69069811 2018-08-02 stsp if (repo_path == NULL) {
3506 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3507 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3508 69069811 2018-08-02 stsp goto done;
3510 eb41ed75 2019-02-05 stsp error = NULL;
3511 eb41ed75 2019-02-05 stsp if (worktree) {
3512 eb41ed75 2019-02-05 stsp repo_path =
3513 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3514 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
3515 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3516 eb41ed75 2019-02-05 stsp if (error)
3517 eb41ed75 2019-02-05 stsp goto done;
3519 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
3520 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
3521 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3522 eb41ed75 2019-02-05 stsp goto done;
3527 a915003a 2019-02-05 stsp init_curses();
3529 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
3530 c02c541e 2019-03-29 stsp if (error != NULL)
3531 8e94dd5b 2019-01-04 stsp goto done;
3533 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3534 c02c541e 2019-03-29 stsp if (error)
3535 92205607 2019-01-04 stsp goto done;
3537 eb41ed75 2019-02-05 stsp if (worktree) {
3538 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3539 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3540 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3541 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3542 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3543 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3544 eb41ed75 2019-02-05 stsp path) == -1) {
3545 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3546 eb41ed75 2019-02-05 stsp goto done;
3548 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3551 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3553 eb41ed75 2019-02-05 stsp if (error)
3554 69069811 2018-08-02 stsp goto done;
3556 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
3557 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
3558 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3559 a70480e0 2018-06-23 stsp if (error != NULL)
3560 66b4983c 2018-06-23 stsp goto done;
3561 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3562 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
3564 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
3565 15a94983 2018-12-23 stsp commit_id_str);
3567 a19e88aa 2018-06-23 stsp if (error != NULL)
3568 8b473291 2019-02-21 stsp goto done;
3570 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
3571 8b473291 2019-02-21 stsp if (error)
3572 a19e88aa 2018-06-23 stsp goto done;
3574 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3575 e1cd8fed 2018-08-01 stsp if (view == NULL) {
3576 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3577 e1cd8fed 2018-08-01 stsp goto done;
3579 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3580 7cbe629d 2018-08-04 stsp if (error)
3581 7cbe629d 2018-08-04 stsp goto done;
3582 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3584 69069811 2018-08-02 stsp free(repo_path);
3585 69069811 2018-08-02 stsp free(cwd);
3586 a70480e0 2018-06-23 stsp free(commit_id);
3587 eb41ed75 2019-02-05 stsp if (worktree)
3588 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
3590 a70480e0 2018-06-23 stsp got_repo_close(repo);
3591 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3592 a70480e0 2018-06-23 stsp return error;
3595 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3596 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
3597 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
3598 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
3599 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
3600 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
3601 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
3603 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3604 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
3605 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
3606 ffd1d5e5 2018-06-23 stsp int width, n;
3608 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
3610 f7d12f7e 2018-08-01 stsp werase(view->window);
3612 ffd1d5e5 2018-06-23 stsp if (limit == 0)
3613 ffd1d5e5 2018-06-23 stsp return NULL;
3615 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
3617 ffd1d5e5 2018-06-23 stsp return err;
3618 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3619 a3404814 2018-09-02 stsp wstandout(view->window);
3620 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3621 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3622 a3404814 2018-09-02 stsp wstandend(view->window);
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 err = format_line(&wline, &width, parent_path, view->ncols);
3631 ce52c690 2018-06-23 stsp return err;
3632 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3633 2550e4c3 2018-07-13 stsp free(wline);
3634 2550e4c3 2018-07-13 stsp wline = NULL;
3635 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3636 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3637 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3638 ffd1d5e5 2018-06-23 stsp return NULL;
3639 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3640 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
3641 a1eca9bb 2018-06-23 stsp return NULL;
3643 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3644 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
3645 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
3646 0cf4efb1 2018-09-29 stsp if (view->focussed)
3647 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3648 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
3650 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
3651 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
3652 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3653 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3654 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3655 ffd1d5e5 2018-06-23 stsp return NULL;
3659 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
3660 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3663 ffd1d5e5 2018-06-23 stsp while (te) {
3664 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
3666 1d13200f 2018-07-12 stsp if (show_ids) {
3667 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
3669 638f9024 2019-05-13 stsp return got_error_from_errno(
3670 230a42bd 2019-05-11 jcs "got_object_id_str");
3672 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3673 d8355ef1 2019-02-10 stsp te->name, S_ISDIR(te->mode) ? "/" :
3674 d8355ef1 2019-02-10 stsp ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3675 1d13200f 2018-07-12 stsp free(id_str);
3676 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3678 1d13200f 2018-07-12 stsp free(id_str);
3679 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
3680 ffd1d5e5 2018-06-23 stsp if (err) {
3681 ffd1d5e5 2018-06-23 stsp free(line);
3684 ffd1d5e5 2018-06-23 stsp if (n == selected) {
3685 0cf4efb1 2018-09-29 stsp if (view->focussed)
3686 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3687 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
3689 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3690 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3691 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3692 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
3693 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3694 ffd1d5e5 2018-06-23 stsp free(line);
3695 2550e4c3 2018-07-13 stsp free(wline);
3696 2550e4c3 2018-07-13 stsp wline = NULL;
3698 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3699 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
3700 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3702 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3705 ffd1d5e5 2018-06-23 stsp return err;
3708 ffd1d5e5 2018-06-23 stsp static void
3709 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
3710 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
3711 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
3713 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
3716 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
3719 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3720 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
3721 ffd1d5e5 2018-06-23 stsp if (!isroot)
3722 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3726 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
3727 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
3728 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
3729 ffd1d5e5 2018-06-23 stsp prev = te;
3730 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3732 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
3733 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3735 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3736 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3739 768394f3 2019-01-24 stsp static int
3740 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3741 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
3742 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
3744 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
3745 ffd1d5e5 2018-06-23 stsp int n = 0;
3747 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
3748 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3750 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
3751 768394f3 2019-01-24 stsp last = last_displayed_entry;
3752 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
3753 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
3754 768394f3 2019-01-24 stsp if (last) {
3755 768394f3 2019-01-24 stsp *first_displayed_entry = next;
3756 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
3762 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3763 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
3764 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
3766 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
3767 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
3768 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
3770 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
3771 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
3773 ce52c690 2018-06-23 stsp len += strlen(te->name);
3775 ce52c690 2018-06-23 stsp *path = calloc(1, len);
3776 ffd1d5e5 2018-06-23 stsp if (path == NULL)
3777 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
3779 ce52c690 2018-06-23 stsp (*path)[0] = '/';
3780 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
3781 d9765a41 2018-06-23 stsp while (pt) {
3782 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3783 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3784 cb2ebc8a 2018-06-23 stsp goto done;
3786 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
3787 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3788 cb2ebc8a 2018-06-23 stsp goto done;
3790 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3793 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3794 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3795 ce52c690 2018-06-23 stsp goto done;
3799 ce52c690 2018-06-23 stsp if (err) {
3800 ce52c690 2018-06-23 stsp free(*path);
3801 ce52c690 2018-06-23 stsp *path = NULL;
3803 ce52c690 2018-06-23 stsp return err;
3806 ce52c690 2018-06-23 stsp static const struct got_error *
3807 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3808 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3809 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3810 8b473291 2019-02-21 stsp struct got_repository *repo)
3812 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3813 ce52c690 2018-06-23 stsp char *path;
3814 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3816 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3818 ce52c690 2018-06-23 stsp return err;
3820 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3821 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3822 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3824 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
3825 e5a0f69f 2018-08-18 stsp if (err) {
3826 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3827 e5a0f69f 2018-08-18 stsp free(path);
3829 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3830 69efd4c4 2018-07-18 stsp return err;
3833 69efd4c4 2018-07-18 stsp static const struct got_error *
3834 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3835 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3836 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3837 8b473291 2019-02-21 stsp struct got_repository *repo)
3839 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3840 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3841 69efd4c4 2018-07-18 stsp char *path;
3843 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3844 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3845 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3847 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3849 69efd4c4 2018-07-18 stsp return err;
3851 8b473291 2019-02-21 stsp err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3853 e5a0f69f 2018-08-18 stsp view_close(log_view);
3855 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3856 cb2ebc8a 2018-06-23 stsp free(path);
3857 cb2ebc8a 2018-06-23 stsp return err;
3860 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3861 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3862 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3863 8b473291 2019-02-21 stsp struct got_repository *repo)
3865 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3866 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3867 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3869 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3871 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3872 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3873 ffd1d5e5 2018-06-23 stsp goto done;
3875 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3876 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3877 ffd1d5e5 2018-06-23 stsp goto done;
3880 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3881 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3882 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3883 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3884 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3885 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3886 6484ec90 2018-09-29 stsp goto done;
3888 8b473291 2019-02-21 stsp s->refs = refs;
3889 fb2756b9 2018-08-04 stsp s->repo = repo;
3891 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
3892 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
3893 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
3894 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
3895 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
3897 ad80ab7b 2018-08-04 stsp free(commit_id_str);
3898 6484ec90 2018-09-29 stsp if (err) {
3899 fb2756b9 2018-08-04 stsp free(s->tree_label);
3900 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3902 ad80ab7b 2018-08-04 stsp return err;
3905 e5a0f69f 2018-08-18 stsp static const struct got_error *
3906 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
3908 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3910 fb2756b9 2018-08-04 stsp free(s->tree_label);
3911 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3912 6484ec90 2018-09-29 stsp free(s->commit_id);
3913 6484ec90 2018-09-29 stsp s->commit_id = NULL;
3914 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
3915 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
3916 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
3917 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
3918 ad80ab7b 2018-08-04 stsp free(parent);
3921 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
3922 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
3923 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
3925 7c32bd05 2019-06-22 stsp return NULL;
3928 7c32bd05 2019-06-22 stsp static const struct got_error *
3929 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
3931 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
3933 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
3934 7c32bd05 2019-06-22 stsp return NULL;
3937 7c32bd05 2019-06-22 stsp static int
3938 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
3940 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
3942 7c32bd05 2019-06-22 stsp return regexec(regex, te->name, 1, ®match, 0) == 0;
3945 7c32bd05 2019-06-22 stsp static const struct got_error *
3946 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
3948 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
3949 7c32bd05 2019-06-22 stsp struct got_tree_entry *entry, *te;
3951 7c32bd05 2019-06-22 stsp if (!view->searching) {
3952 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
3953 7c32bd05 2019-06-22 stsp return NULL;
3956 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
3957 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
3958 7c32bd05 2019-06-22 stsp if (s->selected_entry)
3959 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(s->selected_entry, entry);
3961 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
3964 7c32bd05 2019-06-22 stsp if (s->selected_entry == NULL) {
3965 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
3966 7c32bd05 2019-06-22 stsp entry = te;
3968 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
3969 7c32bd05 2019-06-22 stsp entry = te;
3970 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) ==
3971 7c32bd05 2019-06-22 stsp s->selected_entry)
3977 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
3978 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
3980 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
3981 7c32bd05 2019-06-22 stsp entry = te;
3985 7c32bd05 2019-06-22 stsp while (1) {
3986 7c32bd05 2019-06-22 stsp if (entry == NULL) {
3987 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
3988 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
3990 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
3991 7c32bd05 2019-06-22 stsp entry = te;
3995 7c32bd05 2019-06-22 stsp if (match_tree_entry(entry, &view->regex)) {
3996 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
3997 7c32bd05 2019-06-22 stsp s->matched_entry = entry;
4001 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4002 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(entry, entry);
4004 7c32bd05 2019-06-22 stsp if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4005 7c32bd05 2019-06-22 stsp entry = NULL;
4007 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4008 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) == entry) {
4009 7c32bd05 2019-06-22 stsp entry = te;
4017 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4018 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
4019 7c32bd05 2019-06-22 stsp s->selected = 0;
4022 e5a0f69f 2018-08-18 stsp return NULL;
4025 ad80ab7b 2018-08-04 stsp static const struct got_error *
4026 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
4028 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
4029 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4030 e5a0f69f 2018-08-18 stsp char *parent_path;
4032 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
4034 e5a0f69f 2018-08-18 stsp return err;
4036 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
4037 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
4038 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4039 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
4040 e5a0f69f 2018-08-18 stsp free(parent_path);
4042 669b5ffa 2018-10-07 stsp view_vborder(view);
4043 e5a0f69f 2018-08-18 stsp return err;
4046 e5a0f69f 2018-08-18 stsp static const struct got_error *
4047 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4048 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4050 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4051 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
4052 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
4053 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
4055 e5a0f69f 2018-08-18 stsp switch (ch) {
4057 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
4060 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
4062 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4063 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4064 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
4065 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
4066 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
4067 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4068 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4070 1e37a5c2 2019-05-12 jcs return err;
4071 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
4073 1e37a5c2 2019-05-12 jcs view_close(log_view);
4076 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
4077 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4079 1e37a5c2 2019-05-12 jcs *new_view = log_view;
4082 1e37a5c2 2019-05-12 jcs case KEY_UP:
4083 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
4084 1e37a5c2 2019-05-12 jcs s->selected--;
4085 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
4088 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
4090 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
4091 1e37a5c2 2019-05-12 jcs s->entries, s->tree == s->root);
4093 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4094 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
4095 1e37a5c2 2019-05-12 jcs MAX(0, view->nlines - 4 - s->selected), s->entries,
4096 1e37a5c2 2019-05-12 jcs s->tree == s->root);
4097 1e37a5c2 2019-05-12 jcs s->selected = 0;
4098 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_FIRST(&s->entries->head) ==
4099 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
4100 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
4103 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4104 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
4105 1e37a5c2 2019-05-12 jcs s->selected++;
4108 00ba99a7 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4109 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
4111 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
4112 1e37a5c2 2019-05-12 jcs s->last_displayed_entry, s->entries);
4114 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4115 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4117 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
4118 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
4119 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4122 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
4123 1e37a5c2 2019-05-12 jcs view->nlines, s->last_displayed_entry, s->entries);
4124 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
4125 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
4126 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
4127 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
4129 1e37a5c2 2019-05-12 jcs ndisplayed++;
4130 1e37a5c2 2019-05-12 jcs te = SIMPLEQ_NEXT(te, entry);
4131 1e37a5c2 2019-05-12 jcs } while (te);
4132 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
4135 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4137 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4138 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4139 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
4140 1e37a5c2 2019-05-12 jcs /* user selected '..' */
4141 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
4143 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
4144 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
4146 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
4147 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
4148 1e37a5c2 2019-05-12 jcs s->entries =
4149 1e37a5c2 2019-05-12 jcs got_object_tree_get_entries(s->tree);
4150 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
4151 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
4152 1e37a5c2 2019-05-12 jcs s->selected_entry =
4153 1e37a5c2 2019-05-12 jcs parent->selected_entry;
4154 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
4155 1e37a5c2 2019-05-12 jcs free(parent);
4156 1e37a5c2 2019-05-12 jcs } else if (S_ISDIR(s->selected_entry->mode)) {
4157 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
4158 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&subtree,
4159 1e37a5c2 2019-05-12 jcs s->repo, s->selected_entry->id);
4162 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
4163 941e9f74 2019-05-21 stsp if (err) {
4164 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
4167 1e37a5c2 2019-05-12 jcs } else if (S_ISREG(s->selected_entry->mode)) {
4168 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
4169 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
4170 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
4172 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
4173 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
4174 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
4177 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
4178 669b5ffa 2018-10-07 stsp err = view_close_child(view);
4180 669b5ffa 2018-10-07 stsp return err;
4181 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
4182 669b5ffa 2018-10-07 stsp if (err) {
4183 1e37a5c2 2019-05-12 jcs view_close(blame_view);
4186 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
4187 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
4189 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
4192 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4193 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
4194 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4200 ffd1d5e5 2018-06-23 stsp return err;
4203 ffd1d5e5 2018-06-23 stsp __dead static void
4204 ffd1d5e5 2018-06-23 stsp usage_tree(void)
4207 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4208 ffd1d5e5 2018-06-23 stsp getprogname());
4212 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4213 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
4215 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
4216 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
4217 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4218 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
4219 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4220 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
4221 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
4222 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
4224 5221c383 2018-08-01 stsp struct tog_view *view;
4226 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4228 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
4229 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4230 d188b9a6 2019-01-04 stsp NULL) == -1)
4231 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
4234 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
4235 ffd1d5e5 2018-06-23 stsp switch (ch) {
4237 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
4240 17020d27 2019-03-07 stsp usage_tree();
4241 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
4245 ffd1d5e5 2018-06-23 stsp argc -= optind;
4246 ffd1d5e5 2018-06-23 stsp argv += optind;
4248 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
4249 52185f70 2019-02-05 stsp struct got_worktree *worktree;
4250 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
4251 52185f70 2019-02-05 stsp if (cwd == NULL)
4252 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
4253 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4254 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4255 52185f70 2019-02-05 stsp goto done;
4256 52185f70 2019-02-05 stsp if (worktree) {
4257 52185f70 2019-02-05 stsp free(cwd);
4258 52185f70 2019-02-05 stsp repo_path =
4259 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4260 52185f70 2019-02-05 stsp got_worktree_close(worktree);
4262 52185f70 2019-02-05 stsp repo_path = cwd;
4263 52185f70 2019-02-05 stsp if (repo_path == NULL) {
4264 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4265 52185f70 2019-02-05 stsp goto done;
4267 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
4268 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
4269 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
4270 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
4272 ffd1d5e5 2018-06-23 stsp usage_log();
4274 a915003a 2019-02-05 stsp init_curses();
4276 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
4277 c02c541e 2019-03-29 stsp if (error != NULL)
4278 52185f70 2019-02-05 stsp goto done;
4280 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4281 c02c541e 2019-03-29 stsp if (error)
4282 52185f70 2019-02-05 stsp goto done;
4284 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
4285 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4287 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
4288 15a94983 2018-12-23 stsp commit_id_arg);
4289 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4290 ffd1d5e5 2018-06-23 stsp goto done;
4292 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4293 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4294 ffd1d5e5 2018-06-23 stsp goto done;
4296 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
4297 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
4298 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4299 8b473291 2019-02-21 stsp goto done;
4301 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
4302 8b473291 2019-02-21 stsp if (error)
4303 ffd1d5e5 2018-06-23 stsp goto done;
4305 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4306 5221c383 2018-08-01 stsp if (view == NULL) {
4307 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4308 5221c383 2018-08-01 stsp goto done;
4310 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
4311 ad80ab7b 2018-08-04 stsp if (error)
4312 ad80ab7b 2018-08-04 stsp goto done;
4313 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4315 52185f70 2019-02-05 stsp free(repo_path);
4316 ffd1d5e5 2018-06-23 stsp free(commit_id);
4317 ffd1d5e5 2018-06-23 stsp if (commit)
4318 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
4320 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
4322 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
4323 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4324 ffd1d5e5 2018-06-23 stsp return error;
4327 4ed7e80c 2018-05-20 stsp __dead static void
4328 9f7d7167 2018-04-29 stsp usage(void)
4330 5e070240 2019-06-22 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n", getprogname());
4334 c2301be8 2018-04-30 stsp static char **
4335 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
4337 c2301be8 2018-04-30 stsp char **argv;
4338 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
4340 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
4341 c2301be8 2018-04-30 stsp if (argv == NULL)
4342 c2301be8 2018-04-30 stsp err(1, "calloc");
4343 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
4344 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
4345 c2301be8 2018-04-30 stsp err(1, "calloc");
4346 c2301be8 2018-04-30 stsp if (arg1) {
4347 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
4348 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
4349 c2301be8 2018-04-30 stsp err(1, "calloc");
4352 c2301be8 2018-04-30 stsp return argv;
4356 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
4358 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
4359 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
4360 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
4361 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
4363 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
4365 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
4366 9f7d7167 2018-04-29 stsp switch (ch) {
4368 9f7d7167 2018-04-29 stsp hflag = 1;
4372 9f7d7167 2018-04-29 stsp /* NOTREACHED */
4376 9f7d7167 2018-04-29 stsp argc -= optind;
4377 9f7d7167 2018-04-29 stsp argv += optind;
4378 9f7d7167 2018-04-29 stsp optind = 0;
4379 c2301be8 2018-04-30 stsp optreset = 1;
4381 c2301be8 2018-04-30 stsp if (argc == 0) {
4382 f29d3e89 2018-06-23 stsp if (hflag)
4384 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
4385 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
4386 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
4391 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
4392 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
4393 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
4394 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
4395 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
4396 9f7d7167 2018-04-29 stsp if (hflag)
4397 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
4401 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
4402 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
4403 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
4404 c2301be8 2018-04-30 stsp if (repo_path) {
4405 c2301be8 2018-04-30 stsp struct got_repository *repo;
4406 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
4407 c2301be8 2018-04-30 stsp if (error == NULL)
4408 c2301be8 2018-04-30 stsp got_repo_close(repo);
4410 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath",
4412 c2301be8 2018-04-30 stsp if (error) {
4413 f29d3e89 2018-06-23 stsp if (hflag) {
4414 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
4415 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
4419 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
4420 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
4421 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
4422 ad7de8d9 2018-04-30 stsp free(repo_path);
4425 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
4426 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
4428 c2301be8 2018-04-30 stsp free(repo_path);
4432 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4433 9f7d7167 2018-04-29 stsp if (error)
4434 9f7d7167 2018-04-29 stsp goto done;
4437 c2301be8 2018-04-30 stsp free(cmd_argv);
4438 9f7d7167 2018-04-29 stsp if (error)
4439 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);