2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 9f7d7167 2018-04-29 stsp * Permission to use, copy, modify, and distribute this software for any
5 9f7d7167 2018-04-29 stsp * purpose with or without fee is hereby granted, provided that the above
6 9f7d7167 2018-04-29 stsp * copyright notice and this permission notice appear in all copies.
8 9f7d7167 2018-04-29 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 9f7d7167 2018-04-29 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 9f7d7167 2018-04-29 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 9f7d7167 2018-04-29 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 9f7d7167 2018-04-29 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 9f7d7167 2018-04-29 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 9f7d7167 2018-04-29 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 80ddbec8 2018-04-29 stsp #include <sys/queue.h>
18 ffd1d5e5 2018-06-23 stsp #include <sys/stat.h>
19 25791caa 2018-10-24 stsp #include <sys/ioctl.h>
21 31120ada 2018-04-30 stsp #include <errno.h>
22 61e69b96 2018-05-20 stsp #define _XOPEN_SOURCE_EXTENDED
23 9f7d7167 2018-04-29 stsp #include <curses.h>
24 61e69b96 2018-05-20 stsp #undef _XOPEN_SOURCE_EXTENDED
25 9f7d7167 2018-04-29 stsp #include <panel.h>
26 9f7d7167 2018-04-29 stsp #include <locale.h>
27 9f7d7167 2018-04-29 stsp #include <stdlib.h>
28 26ed57b2 2018-05-19 stsp #include <stdio.h>
29 9f7d7167 2018-04-29 stsp #include <getopt.h>
30 9f7d7167 2018-04-29 stsp #include <string.h>
31 9f7d7167 2018-04-29 stsp #include <err.h>
32 80ddbec8 2018-04-29 stsp #include <unistd.h>
33 26ed57b2 2018-05-19 stsp #include <util.h>
34 26ed57b2 2018-05-19 stsp #include <limits.h>
35 61e69b96 2018-05-20 stsp #include <wchar.h>
36 788c352e 2018-06-16 stsp #include <time.h>
37 84451b3e 2018-07-10 stsp #include <pthread.h>
38 5036bf37 2018-09-24 stsp #include <libgen.h>
39 60493ae3 2019-06-20 stsp #include <regex.h>
41 9f7d7167 2018-04-29 stsp #include "got_error.h"
42 80ddbec8 2018-04-29 stsp #include "got_object.h"
43 80ddbec8 2018-04-29 stsp #include "got_reference.h"
44 80ddbec8 2018-04-29 stsp #include "got_repository.h"
45 80ddbec8 2018-04-29 stsp #include "got_diff.h"
46 511a516b 2018-05-19 stsp #include "got_opentemp.h"
47 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
48 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
49 a70480e0 2018-06-23 stsp #include "got_blame.h"
50 c2db6724 2019-01-04 stsp #include "got_privsep.h"
51 1dd54920 2019-05-11 stsp #include "got_path.h"
52 b7165be3 2019-02-05 stsp #include "got_worktree.h"
55 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
64 9f7d7167 2018-04-29 stsp #ifndef nitems
65 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 9f7d7167 2018-04-29 stsp struct tog_cmd {
69 c2301be8 2018-04-30 stsp const char *name;
70 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
71 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
72 c2301be8 2018-04-30 stsp const char *descr;
75 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
76 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
77 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
78 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
79 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
81 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
82 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
83 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
84 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
86 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
87 cbb6b58a 2018-05-20 stsp { "log", cmd_log, usage_log,
88 9f7d7167 2018-04-29 stsp "show repository history" },
89 cbb6b58a 2018-05-20 stsp { "diff", cmd_diff, usage_diff,
90 9f7d7167 2018-04-29 stsp "compare files and directories" },
91 cbb6b58a 2018-05-20 stsp { "blame", cmd_blame, usage_blame,
92 9f7d7167 2018-04-29 stsp "show line-by-line file history" },
93 ffd1d5e5 2018-06-23 stsp { "tree", cmd_tree, usage_tree,
94 ffd1d5e5 2018-06-23 stsp "browse trees in repository" },
97 d6b05b5b 2018-08-04 stsp enum tog_view_type {
98 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
99 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
100 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
101 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
104 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
106 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
107 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
108 ba4f502b 2018-08-04 stsp struct got_object_id *id;
109 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
112 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
113 ba4f502b 2018-08-04 stsp struct commit_queue {
114 ba4f502b 2018-08-04 stsp int ncommits;
115 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
118 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
119 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
121 15a087fe 2019-02-21 stsp int first_displayed_line;
122 15a087fe 2019-02-21 stsp int last_displayed_line;
124 15a087fe 2019-02-21 stsp int diff_context;
125 15a087fe 2019-02-21 stsp struct got_repository *repo;
126 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
128 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
129 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
132 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
134 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
135 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
136 1a76625f 2018-10-22 stsp int commits_needed;
137 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
138 1a76625f 2018-10-22 stsp struct commit_queue *commits;
139 1a76625f 2018-10-22 stsp const char *in_repo_path;
140 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
141 1a76625f 2018-10-22 stsp struct got_repository *repo;
142 1a76625f 2018-10-22 stsp int log_complete;
143 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
144 1a76625f 2018-10-22 stsp struct tog_view *view;
145 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
146 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
149 1a76625f 2018-10-22 stsp struct tog_log_view_state {
150 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
151 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
152 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
153 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
154 b01e7d3b 2018-08-04 stsp int selected;
155 b01e7d3b 2018-08-04 stsp char *in_repo_path;
156 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
157 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
158 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
159 1a76625f 2018-10-22 stsp sig_atomic_t quit;
160 1a76625f 2018-10-22 stsp pthread_t thread;
161 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
163 60493ae3 2019-06-20 stsp regex_t regex;
164 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
167 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
168 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
169 e9424729 2018-08-04 stsp int nlines;
171 e9424729 2018-08-04 stsp struct tog_view *view;
172 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
176 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
177 e9424729 2018-08-04 stsp const char *path;
178 e9424729 2018-08-04 stsp struct got_repository *repo;
179 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
180 e9424729 2018-08-04 stsp int *complete;
183 e9424729 2018-08-04 stsp struct tog_blame {
185 e9424729 2018-08-04 stsp size_t filesize;
186 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
187 6fcac457 2018-11-19 stsp int nlines;
188 e9424729 2018-08-04 stsp pthread_t thread;
189 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
190 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
191 e9424729 2018-08-04 stsp const char *path;
194 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
195 7cbe629d 2018-08-04 stsp int first_displayed_line;
196 7cbe629d 2018-08-04 stsp int last_displayed_line;
197 7cbe629d 2018-08-04 stsp int selected_line;
198 7cbe629d 2018-08-04 stsp int blame_complete;
201 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
202 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
203 e5a0f69f 2018-08-18 stsp char *path;
204 7cbe629d 2018-08-04 stsp struct got_repository *repo;
205 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
206 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
207 e9424729 2018-08-04 stsp struct tog_blame blame;
210 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
211 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
212 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
213 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
214 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
215 ad80ab7b 2018-08-04 stsp int selected;
218 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
220 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
221 ad80ab7b 2018-08-04 stsp char *tree_label;
222 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
223 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
224 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
225 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
226 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
227 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
228 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
229 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
230 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
231 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
232 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
236 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
238 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
239 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
240 669b5ffa 2018-10-07 stsp * there is enough screen estate.
242 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
243 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
245 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
246 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
247 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
249 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
250 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
252 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
254 cc3c9aac 2018-08-01 stsp struct tog_view {
255 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
256 26ed57b2 2018-05-19 stsp WINDOW *window;
257 26ed57b2 2018-05-19 stsp PANEL *panel;
258 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
259 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
260 1004088d 2018-09-29 stsp int focussed;
261 669b5ffa 2018-10-07 stsp struct tog_view *parent;
262 669b5ffa 2018-10-07 stsp struct tog_view *child;
263 669b5ffa 2018-10-07 stsp int child_focussed;
265 5dc9f4bc 2018-08-04 stsp /* type-specific state */
266 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
268 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
269 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
270 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
271 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
274 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
275 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
276 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
277 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
279 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
280 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
281 60493ae3 2019-06-20 stsp int searching;
282 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
283 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
284 60493ae3 2019-06-20 stsp int search_next_done;
287 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
288 fb872ab2 2019-02-21 stsp struct got_object_id *, struct got_object_id *, struct tog_view *,
289 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
290 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
291 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
292 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
293 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
295 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
296 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *,
297 8b473291 2019-02-21 stsp struct got_repository *, const char *, int);
298 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
299 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
300 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
301 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
302 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
303 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
305 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
306 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *, struct got_repository *);
307 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
308 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
309 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
310 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
312 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
313 8b473291 2019-02-21 stsp struct got_tree_object *, struct got_object_id *,
314 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
315 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
316 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
317 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
318 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
320 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
322 25791caa 2018-10-24 stsp static void
323 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
325 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
328 e5a0f69f 2018-08-18 stsp static const struct got_error *
329 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
331 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
333 669b5ffa 2018-10-07 stsp if (view->child) {
334 669b5ffa 2018-10-07 stsp view_close(view->child);
335 669b5ffa 2018-10-07 stsp view->child = NULL;
337 e5a0f69f 2018-08-18 stsp if (view->close)
338 e5a0f69f 2018-08-18 stsp err = view->close(view);
339 ea5e7bb5 2018-08-01 stsp if (view->panel)
340 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
341 ea5e7bb5 2018-08-01 stsp if (view->window)
342 ea5e7bb5 2018-08-01 stsp delwin(view->window);
343 ea5e7bb5 2018-08-01 stsp free(view);
344 e5a0f69f 2018-08-18 stsp return err;
347 ea5e7bb5 2018-08-01 stsp static struct tog_view *
348 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
349 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
351 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
353 ea5e7bb5 2018-08-01 stsp if (view == NULL)
354 ea5e7bb5 2018-08-01 stsp return NULL;
356 d6b05b5b 2018-08-04 stsp view->type = type;
357 f7d12f7e 2018-08-01 stsp view->lines = LINES;
358 f7d12f7e 2018-08-01 stsp view->cols = COLS;
359 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
360 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
361 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
362 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
363 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
364 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
365 96a765a8 2018-08-04 stsp view_close(view);
366 ea5e7bb5 2018-08-01 stsp return NULL;
368 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
369 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
370 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
371 96a765a8 2018-08-04 stsp view_close(view);
372 ea5e7bb5 2018-08-01 stsp return NULL;
375 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
376 ea5e7bb5 2018-08-01 stsp return view;
380 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
382 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
384 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
387 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
389 5c60c32a 2018-10-18 stsp static const struct got_error *
390 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
392 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
394 5c60c32a 2018-10-18 stsp view->begin_y = 0;
395 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
396 5c60c32a 2018-10-18 stsp view->nlines = LINES;
397 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
398 5c60c32a 2018-10-18 stsp view->lines = LINES;
399 5c60c32a 2018-10-18 stsp view->cols = COLS;
400 5c60c32a 2018-10-18 stsp err = view_resize(view);
402 5c60c32a 2018-10-18 stsp return err;
404 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
405 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
407 5c60c32a 2018-10-18 stsp return NULL;
410 5c60c32a 2018-10-18 stsp static const struct got_error *
411 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
413 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
415 5c60c32a 2018-10-18 stsp view->begin_x = 0;
416 5c60c32a 2018-10-18 stsp view->begin_y = 0;
417 5c60c32a 2018-10-18 stsp view->nlines = LINES;
418 5c60c32a 2018-10-18 stsp view->ncols = COLS;
419 5c60c32a 2018-10-18 stsp view->lines = LINES;
420 5c60c32a 2018-10-18 stsp view->cols = COLS;
421 5c60c32a 2018-10-18 stsp err = view_resize(view);
423 5c60c32a 2018-10-18 stsp return err;
425 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
426 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
428 5c60c32a 2018-10-18 stsp return NULL;
432 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
434 5c60c32a 2018-10-18 stsp return view->parent == NULL;
437 4d8c2215 2018-08-19 stsp static const struct got_error *
438 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
440 f7d12f7e 2018-08-01 stsp int nlines, ncols;
442 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
443 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
445 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
447 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
448 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
450 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
452 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
453 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
454 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
455 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
456 25791caa 2018-10-24 stsp wclear(view->window);
458 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
459 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
460 0cf4efb1 2018-09-29 stsp view->lines = LINES;
461 0cf4efb1 2018-09-29 stsp view->cols = COLS;
463 6e3e5d9c 2018-10-18 stsp if (view->child) {
464 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
465 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
466 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
467 5c60c32a 2018-10-18 stsp if (view->child->focussed)
468 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
470 5c60c32a 2018-10-18 stsp show_panel(view->panel);
472 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
473 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
477 5c60c32a 2018-10-18 stsp return NULL;
480 669b5ffa 2018-10-07 stsp static const struct got_error *
481 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
483 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
485 669b5ffa 2018-10-07 stsp if (view->child == NULL)
486 669b5ffa 2018-10-07 stsp return NULL;
488 669b5ffa 2018-10-07 stsp err = view_close(view->child);
489 669b5ffa 2018-10-07 stsp view->child = NULL;
490 669b5ffa 2018-10-07 stsp return err;
493 669b5ffa 2018-10-07 stsp static const struct got_error *
494 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
496 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
498 669b5ffa 2018-10-07 stsp view->child = child;
499 669b5ffa 2018-10-07 stsp child->parent = view;
500 669b5ffa 2018-10-07 stsp return err;
504 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
506 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
509 34bc9ec9 2019-02-22 stsp static void
510 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
512 25791caa 2018-10-24 stsp int cols, lines;
513 25791caa 2018-10-24 stsp struct winsize size;
515 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
516 25791caa 2018-10-24 stsp cols = 80; /* Default */
517 25791caa 2018-10-24 stsp lines = 24;
519 25791caa 2018-10-24 stsp cols = size.ws_col;
520 25791caa 2018-10-24 stsp lines = size.ws_row;
522 25791caa 2018-10-24 stsp resize_term(lines, cols);
525 0cf4efb1 2018-09-29 stsp static const struct got_error *
526 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
527 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
528 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
530 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
531 669b5ffa 2018-10-07 stsp struct tog_view *v;
532 1a76625f 2018-10-22 stsp int ch, errcode;
534 e5a0f69f 2018-08-18 stsp *new = NULL;
535 e5a0f69f 2018-08-18 stsp *dead = NULL;
536 0cf4efb1 2018-09-29 stsp *focus = NULL;
538 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
539 60493ae3 2019-06-20 stsp errcode = pthread_mutex_unlock(&tog_mutex);
540 60493ae3 2019-06-20 stsp if (errcode)
541 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
542 60493ae3 2019-06-20 stsp "pthread_mutex_unlock");
543 60493ae3 2019-06-20 stsp pthread_yield();
544 60493ae3 2019-06-20 stsp errcode = pthread_mutex_lock(&tog_mutex);
545 60493ae3 2019-06-20 stsp if (errcode)
546 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
547 60493ae3 2019-06-20 stsp "pthread_mutex_lock");
548 60493ae3 2019-06-20 stsp view->search_next(view);
549 60493ae3 2019-06-20 stsp nodelay(stdscr, TRUE);
550 60493ae3 2019-06-20 stsp return NULL;
553 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
554 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
555 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
556 1a76625f 2018-10-22 stsp if (errcode)
557 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
558 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
559 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
560 1a76625f 2018-10-22 stsp if (errcode)
561 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
562 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
564 25791caa 2018-10-24 stsp if (tog_sigwinch_received) {
565 25791caa 2018-10-24 stsp tog_resizeterm();
566 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
567 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
568 25791caa 2018-10-24 stsp err = view_resize(v);
570 25791caa 2018-10-24 stsp return err;
571 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
573 25791caa 2018-10-24 stsp return err;
577 e5a0f69f 2018-08-18 stsp switch (ch) {
581 1e37a5c2 2019-05-12 jcs if (view->child) {
582 1e37a5c2 2019-05-12 jcs *focus = view->child;
583 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
584 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
585 1e37a5c2 2019-05-12 jcs *focus = view->parent;
586 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
590 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
591 1e37a5c2 2019-05-12 jcs *dead = view;
597 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
598 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
600 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
601 669b5ffa 2018-10-07 stsp *focus = view->child;
602 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
603 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
605 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
608 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
609 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
611 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
612 1e37a5c2 2019-05-12 jcs *focus = view;
613 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
614 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
616 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
620 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
621 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
624 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
627 60493ae3 2019-06-20 stsp if (view->search_start)
628 60493ae3 2019-06-20 stsp view->search_start(view);
630 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
634 60493ae3 2019-06-20 stsp if (view->search_next && view->searching) {
635 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
636 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
637 60493ae3 2019-06-20 stsp view->search_next_done = 0;
638 60493ae3 2019-06-20 stsp view->search_next(view);
640 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
643 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
647 e5a0f69f 2018-08-18 stsp return err;
651 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
653 0cf4efb1 2018-09-29 stsp PANEL *panel;
654 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
656 669b5ffa 2018-10-07 stsp if (view->parent)
657 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
659 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
660 0cf4efb1 2018-09-29 stsp if (panel == NULL)
663 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
664 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
665 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
669 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
671 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
672 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
674 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
676 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
679 669b5ffa 2018-10-07 stsp return view->focussed;
682 bcbd79e2 2018-08-19 stsp static const struct got_error *
683 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
685 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
686 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
687 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
688 fd823528 2018-10-22 stsp int fast_refresh = 10;
689 1a76625f 2018-10-22 stsp int done = 0, errcode;
691 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
692 1a76625f 2018-10-22 stsp if (errcode)
693 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
695 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
696 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
698 a81bf10d 2018-09-29 stsp main_view = view;
699 1004088d 2018-09-29 stsp view->focussed = 1;
700 878940b7 2018-09-29 stsp err = view->show(view);
702 0cf4efb1 2018-09-29 stsp return err;
703 0cf4efb1 2018-09-29 stsp update_panels();
704 0cf4efb1 2018-09-29 stsp doupdate();
705 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
706 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
707 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
708 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
710 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
711 e4197bf9 2018-08-18 stsp view, &views);
714 e5a0f69f 2018-08-18 stsp if (dead_view) {
715 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
717 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
718 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
719 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
720 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
721 669b5ffa 2018-10-07 stsp prev = view->parent;
723 669b5ffa 2018-10-07 stsp if (dead_view->parent)
724 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
726 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
728 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
729 a81bf10d 2018-09-29 stsp if (err || dead_view == main_view)
732 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
733 0cf4efb1 2018-09-29 stsp if (focus_view)
734 0cf4efb1 2018-09-29 stsp view = focus_view;
735 669b5ffa 2018-10-07 stsp else if (prev)
736 669b5ffa 2018-10-07 stsp view = prev;
737 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
738 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
739 0cf4efb1 2018-09-29 stsp tog_view_list_head);
741 0cf4efb1 2018-09-29 stsp view = NULL;
742 669b5ffa 2018-10-07 stsp if (view) {
743 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
744 669b5ffa 2018-10-07 stsp focus_view = view->child;
746 669b5ffa 2018-10-07 stsp focus_view = view;
750 bcbd79e2 2018-08-19 stsp if (new_view) {
751 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
752 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
753 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
754 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
756 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
757 86c66b02 2018-10-18 stsp err = view_close(v);
762 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
763 fed7eaa8 2018-10-24 stsp view = new_view;
764 878940b7 2018-09-29 stsp if (focus_view == NULL)
765 878940b7 2018-09-29 stsp focus_view = new_view;
767 0cf4efb1 2018-09-29 stsp if (focus_view) {
768 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
770 0cf4efb1 2018-09-29 stsp view->focussed = 0;
771 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
772 0cf4efb1 2018-09-29 stsp view = focus_view;
773 878940b7 2018-09-29 stsp if (new_view)
774 878940b7 2018-09-29 stsp show_panel(new_view->panel);
775 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
776 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
778 669b5ffa 2018-10-07 stsp if (view) {
779 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
780 758194b5 2018-10-24 stsp view->focussed = 1;
781 758194b5 2018-10-24 stsp show_panel(view->panel);
782 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
783 758194b5 2018-10-24 stsp show_panel(view->child->panel);
784 1a76625f 2018-10-22 stsp focus_view = view;
786 669b5ffa 2018-10-07 stsp if (view->parent) {
787 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
791 669b5ffa 2018-10-07 stsp err = view->show(view);
794 669b5ffa 2018-10-07 stsp if (view->child) {
795 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
799 1a76625f 2018-10-22 stsp update_panels();
800 1a76625f 2018-10-22 stsp doupdate();
804 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
805 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
806 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
807 e5a0f69f 2018-08-18 stsp view_close(view);
810 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
811 1a76625f 2018-10-22 stsp if (errcode)
812 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
814 e5a0f69f 2018-08-18 stsp return err;
817 4ed7e80c 2018-05-20 stsp __dead static void
818 9f7d7167 2018-04-29 stsp usage_log(void)
821 c70c5802 2018-08-01 stsp fprintf(stderr,
822 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
823 9f7d7167 2018-04-29 stsp getprogname());
827 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
828 80ddbec8 2018-04-29 stsp static const struct got_error *
829 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
831 00dfcb92 2018-06-11 stsp char *vis = NULL;
832 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
834 963b370f 2018-05-20 stsp *ws = NULL;
835 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
836 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
837 00dfcb92 2018-06-11 stsp int vislen;
838 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
839 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
841 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
842 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
844 00dfcb92 2018-06-11 stsp return err;
845 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
846 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
847 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
852 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
853 a7f50699 2018-06-11 stsp if (*ws == NULL) {
854 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
858 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
859 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
864 963b370f 2018-05-20 stsp *ws = NULL;
867 963b370f 2018-05-20 stsp return err;
870 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
871 963b370f 2018-05-20 stsp static const struct got_error *
872 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
874 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
875 963b370f 2018-05-20 stsp int cols = 0;
876 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
877 963b370f 2018-05-20 stsp size_t wlen;
880 963b370f 2018-05-20 stsp *wlinep = NULL;
881 b700b5d6 2018-07-10 stsp *widthp = 0;
883 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
885 963b370f 2018-05-20 stsp return err;
888 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
889 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
890 963b370f 2018-05-20 stsp switch (width) {
896 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
897 b700b5d6 2018-07-10 stsp cols += width;
901 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
902 67ceb59d 2019-03-03 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
906 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
910 963b370f 2018-05-20 stsp wline[i] = L'\0';
911 b700b5d6 2018-07-10 stsp if (widthp)
912 b700b5d6 2018-07-10 stsp *widthp = cols;
915 963b370f 2018-05-20 stsp free(wline);
917 963b370f 2018-05-20 stsp *wlinep = wline;
918 963b370f 2018-05-20 stsp return err;
921 8b473291 2019-02-21 stsp static const struct got_error*
922 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
923 8b473291 2019-02-21 stsp struct got_object_id *id)
925 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
926 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
928 8b473291 2019-02-21 stsp const char *name;
930 8b473291 2019-02-21 stsp *refs_str = NULL;
932 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
933 8b473291 2019-02-21 stsp if (got_object_id_cmp(re->id, id) != 0)
935 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
936 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
938 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
940 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
942 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
944 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
946 8b473291 2019-02-21 stsp s = *refs_str;
947 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
948 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
949 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
951 8b473291 2019-02-21 stsp *refs_str = NULL;
957 8b473291 2019-02-21 stsp return err;
960 963b370f 2018-05-20 stsp static const struct got_error *
961 5813d178 2019-03-09 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
963 5813d178 2019-03-09 stsp char *smallerthan, *at;
965 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
966 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
967 5813d178 2019-03-09 stsp author = smallerthan + 1;
968 5813d178 2019-03-09 stsp at = strchr(author, '@');
970 5813d178 2019-03-09 stsp *at = '\0';
971 5813d178 2019-03-09 stsp return format_line(wauthor, author_width, author, limit);
974 5813d178 2019-03-09 stsp static const struct got_error *
975 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
976 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
977 5813d178 2019-03-09 stsp int author_display_cols)
979 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
980 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
981 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
982 5813d178 2019-03-09 stsp char *author = NULL;
983 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
984 bb737323 2018-05-20 stsp int author_width, logmsg_width;
985 5813d178 2019-03-09 stsp char *newline, *line = NULL;
986 bb737323 2018-05-20 stsp int col, limit;
987 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
988 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
989 ccb26ccd 2018-11-05 stsp struct tm tm;
990 45d799e2 2018-12-23 stsp time_t committer_time;
992 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
993 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
994 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
995 ccb26ccd 2018-11-05 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
996 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
997 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
999 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
1000 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1002 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1003 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1004 b39d25c7 2018-07-10 stsp col = limit + 1;
1005 b39d25c7 2018-07-10 stsp if (col > avail)
1006 b39d25c7 2018-07-10 stsp goto done;
1008 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1009 5813d178 2019-03-09 stsp if (author == NULL) {
1010 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1011 80ddbec8 2018-04-29 stsp goto done;
1013 5813d178 2019-03-09 stsp err = format_author(&wauthor, &author_width, author, avail - col);
1015 bb737323 2018-05-20 stsp goto done;
1016 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1017 bb737323 2018-05-20 stsp col += author_width;
1018 5813d178 2019-03-09 stsp while (col <= avail && author_width < author_display_cols + 2) {
1019 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1021 bb737323 2018-05-20 stsp author_width++;
1023 9c2eaf34 2018-05-20 stsp if (col > avail)
1024 9c2eaf34 2018-05-20 stsp goto done;
1026 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1027 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
1028 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1029 6d9fbc00 2018-04-29 stsp goto done;
1031 bb737323 2018-05-20 stsp logmsg = logmsg0;
1032 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1034 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1035 bb737323 2018-05-20 stsp if (newline)
1036 bb737323 2018-05-20 stsp *newline = '\0';
1037 bb737323 2018-05-20 stsp limit = avail - col;
1038 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1040 bb737323 2018-05-20 stsp goto done;
1041 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1042 bb737323 2018-05-20 stsp col += logmsg_width;
1043 bb737323 2018-05-20 stsp while (col <= avail) {
1044 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1048 80ddbec8 2018-04-29 stsp free(logmsg0);
1049 bb737323 2018-05-20 stsp free(wlogmsg);
1050 5813d178 2019-03-09 stsp free(author);
1051 bb737323 2018-05-20 stsp free(wauthor);
1052 80ddbec8 2018-04-29 stsp free(line);
1053 80ddbec8 2018-04-29 stsp return err;
1056 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1057 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1058 899d86c2 2018-05-10 stsp struct got_object_id *id)
1060 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1062 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1063 80ddbec8 2018-04-29 stsp if (entry == NULL)
1064 899d86c2 2018-05-10 stsp return NULL;
1066 899d86c2 2018-05-10 stsp entry->id = id;
1067 99db9666 2018-05-07 stsp entry->commit = commit;
1068 899d86c2 2018-05-10 stsp return entry;
1071 99db9666 2018-05-07 stsp static void
1072 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1074 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1076 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1077 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1078 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1079 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1080 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1081 99db9666 2018-05-07 stsp free(entry);
1084 99db9666 2018-05-07 stsp static void
1085 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1087 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1088 99db9666 2018-05-07 stsp pop_commit(commits);
1091 c4972b91 2018-05-07 stsp static const struct got_error *
1092 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1093 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
1095 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1096 93e45b7c 2018-09-24 stsp int nqueued = 0;
1099 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1100 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1101 1a76625f 2018-10-22 stsp * while updating the display.
1103 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
1104 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1105 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1106 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1107 1a76625f 2018-10-22 stsp int errcode;
1109 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1110 9ba79e04 2018-06-11 stsp if (err) {
1111 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1113 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
1114 93e45b7c 2018-09-24 stsp minqueue, repo);
1116 ecb28ae0 2018-07-16 stsp return err;
1120 ecb28ae0 2018-07-16 stsp if (id == NULL)
1123 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1126 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1127 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1128 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1132 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1133 1a76625f 2018-10-22 stsp if (errcode) {
1134 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_lock");
1138 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1139 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1140 ecb28ae0 2018-07-16 stsp nqueued++;
1141 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1143 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1144 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1145 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1146 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1149 9ba79e04 2018-06-11 stsp return err;
1152 99db9666 2018-05-07 stsp static const struct got_error *
1153 19e70ad6 2019-05-14 stsp get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1154 19e70ad6 2019-05-14 stsp struct got_repository *repo)
1156 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1157 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1159 9ba79e04 2018-06-11 stsp *head_id = NULL;
1161 19e70ad6 2019-05-14 stsp err = got_ref_open(&head_ref, repo, branch_name, 0);
1163 99db9666 2018-05-07 stsp return err;
1165 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1166 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1167 9ba79e04 2018-06-11 stsp if (err) {
1168 9ba79e04 2018-06-11 stsp *head_id = NULL;
1169 99db9666 2018-05-07 stsp return err;
1172 9ba79e04 2018-06-11 stsp return NULL;
1175 0553a4e3 2018-04-30 stsp static const struct got_error *
1176 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1177 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1178 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1179 8b473291 2019-02-21 stsp struct got_reflist_head *refs, const char *path, int commits_needed)
1181 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1182 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1183 60493ae3 2019-06-20 stsp int width;
1184 60493ae3 2019-06-20 stsp int ncommits, author_cols = 10;
1185 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1186 8b473291 2019-02-21 stsp char *refs_str = NULL;
1187 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1189 e0d42f60 2018-07-22 stsp entry = first;
1190 e0d42f60 2018-07-22 stsp ncommits = 0;
1191 e0d42f60 2018-07-22 stsp while (entry) {
1192 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1193 e0d42f60 2018-07-22 stsp *selected = entry;
1196 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1197 e0d42f60 2018-07-22 stsp ncommits++;
1200 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1201 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1203 ecb28ae0 2018-07-16 stsp return err;
1204 8b473291 2019-02-21 stsp if (refs) {
1205 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, (*selected)->id);
1207 8b473291 2019-02-21 stsp goto done;
1211 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1212 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1214 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1215 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1216 60493ae3 2019-06-20 stsp commits_needed > 0 ?
1217 60493ae3 2019-06-20 stsp (view->searching && view->search_next_done == 0
1218 60493ae3 2019-06-20 stsp ? "searching..." : "loading... ") :
1219 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1220 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1221 8b473291 2019-02-21 stsp goto done;
1224 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1225 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1226 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1227 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1228 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1229 1a76625f 2018-10-22 stsp header = NULL;
1230 1a76625f 2018-10-22 stsp goto done;
1232 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1233 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1234 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1235 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1236 1a76625f 2018-10-22 stsp header = NULL;
1237 1a76625f 2018-10-22 stsp goto done;
1239 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1241 1a76625f 2018-10-22 stsp goto done;
1243 2814baeb 2018-08-01 stsp werase(view->window);
1245 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1246 a3404814 2018-09-02 stsp wstandout(view->window);
1247 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1248 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1249 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1252 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1253 a3404814 2018-09-02 stsp wstandend(view->window);
1254 ecb28ae0 2018-07-16 stsp free(wline);
1255 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1256 1a76625f 2018-10-22 stsp goto done;
1258 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1259 899d86c2 2018-05-10 stsp entry = first;
1260 5813d178 2019-03-09 stsp ncommits = 0;
1261 5813d178 2019-03-09 stsp while (entry) {
1262 5813d178 2019-03-09 stsp char *author;
1263 5813d178 2019-03-09 stsp wchar_t *wauthor;
1264 5813d178 2019-03-09 stsp int width;
1265 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1267 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1268 5813d178 2019-03-09 stsp if (author == NULL) {
1269 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1270 5813d178 2019-03-09 stsp goto done;
1272 5813d178 2019-03-09 stsp err = format_author(&wauthor, &width, author, COLS);
1273 5813d178 2019-03-09 stsp if (author_cols < width)
1274 5813d178 2019-03-09 stsp author_cols = width;
1275 5813d178 2019-03-09 stsp free(wauthor);
1276 5813d178 2019-03-09 stsp free(author);
1277 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1280 5813d178 2019-03-09 stsp entry = first;
1281 899d86c2 2018-05-10 stsp *last = first;
1282 867c6645 2018-07-10 stsp ncommits = 0;
1283 899d86c2 2018-05-10 stsp while (entry) {
1284 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1286 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1287 2814baeb 2018-08-01 stsp wstandout(view->window);
1288 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1289 5813d178 2019-03-09 stsp author_cols);
1290 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1291 2814baeb 2018-08-01 stsp wstandend(view->window);
1293 60493ae3 2019-06-20 stsp goto done;
1294 0553a4e3 2018-04-30 stsp ncommits++;
1295 899d86c2 2018-05-10 stsp *last = entry;
1296 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1299 1a57306a 2018-09-02 stsp view_vborder(view);
1301 1a76625f 2018-10-22 stsp free(id_str);
1302 8b473291 2019-02-21 stsp free(refs_str);
1303 1a76625f 2018-10-22 stsp free(ncommits_str);
1304 1a76625f 2018-10-22 stsp free(header);
1305 80ddbec8 2018-04-29 stsp return err;
1308 07b55e75 2018-05-10 stsp static void
1309 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1310 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1311 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1313 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1314 07b55e75 2018-05-10 stsp int nscrolled = 0;
1316 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1317 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1320 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1321 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1322 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1323 07b55e75 2018-05-10 stsp if (entry) {
1324 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1325 07b55e75 2018-05-10 stsp nscrolled++;
1330 aa075928 2018-05-10 stsp static const struct got_error *
1331 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1332 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1334 5e224a3e 2019-02-22 stsp int errcode;
1335 8a42fca8 2019-02-22 stsp int max_wait = 20;
1337 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1339 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1340 5e224a3e 2019-02-22 stsp if (*log_complete)
1343 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1344 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1345 7aafa0d1 2019-02-22 stsp if (errcode)
1346 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1347 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1348 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1349 7aafa0d1 2019-02-22 stsp if (errcode)
1350 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1351 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1352 7aafa0d1 2019-02-22 stsp pthread_yield();
1353 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1354 7aafa0d1 2019-02-22 stsp if (errcode)
1355 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1356 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1358 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1360 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1361 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1362 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1363 5e224a3e 2019-02-22 stsp * with few commits in history.
1365 7aafa0d1 2019-02-22 stsp return NULL;
1369 5e224a3e 2019-02-22 stsp return NULL;
1372 5e224a3e 2019-02-22 stsp static const struct got_error *
1373 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1374 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1375 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1376 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1377 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1379 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1380 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1381 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1383 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1384 5e224a3e 2019-02-22 stsp return NULL;
1386 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1387 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1389 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1390 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1392 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1393 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1394 5e224a3e 2019-02-22 stsp need_commits);
1396 5e224a3e 2019-02-22 stsp return err;
1400 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1401 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1404 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1406 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1407 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1409 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1410 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1412 dd0a52c1 2018-05-20 stsp return err;
1415 cd0acaa7 2018-05-20 stsp static const struct got_error *
1416 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1417 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1418 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1419 8b473291 2019-02-21 stsp struct got_repository *repo)
1421 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1422 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1423 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1425 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1426 15a94983 2018-12-23 stsp if (diff_view == NULL)
1427 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1429 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1430 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1431 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1432 e5a0f69f 2018-08-18 stsp if (err == NULL)
1433 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1434 cd0acaa7 2018-05-20 stsp return err;
1437 80ddbec8 2018-04-29 stsp static const struct got_error *
1438 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1439 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1441 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1443 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1444 941e9f74 2019-05-21 stsp if (parent == NULL)
1445 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1447 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1448 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1449 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1450 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1451 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1452 941e9f74 2019-05-21 stsp s->tree = subtree;
1453 941e9f74 2019-05-21 stsp s->entries = got_object_tree_get_entries(s->tree);
1454 941e9f74 2019-05-21 stsp s->selected = 0;
1455 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1456 941e9f74 2019-05-21 stsp return NULL;
1460 941e9f74 2019-05-21 stsp static const struct got_error *
1461 941e9f74 2019-05-21 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1462 941e9f74 2019-05-21 stsp struct commit_queue_entry *entry, const char *path,
1463 941e9f74 2019-05-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
1465 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1466 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1467 941e9f74 2019-05-21 stsp struct got_tree_entry *te;
1468 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s;
1469 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1470 b03c880f 2019-05-21 stsp char *slash, *subpath = NULL;
1471 941e9f74 2019-05-21 stsp const char *p;
1473 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1474 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1476 9343a5fb 2018-06-23 stsp return err;
1478 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1479 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1480 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1482 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1483 941e9f74 2019-05-21 stsp if (err) {
1484 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1485 941e9f74 2019-05-21 stsp return err;
1487 941e9f74 2019-05-21 stsp s = &tree_view->state.tree;
1489 941e9f74 2019-05-21 stsp *new_view = tree_view;
1491 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1493 941e9f74 2019-05-21 stsp while (*p) {
1494 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1496 941e9f74 2019-05-21 stsp while (p[0] == '/')
1499 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1500 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1501 941e9f74 2019-05-21 stsp if (slash == NULL)
1502 941e9f74 2019-05-21 stsp slash = strchr(p, '\0');
1503 941e9f74 2019-05-21 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1504 941e9f74 2019-05-21 stsp if (strncmp(p, te->name, slash - p) == 0) {
1505 941e9f74 2019-05-21 stsp s->selected_entry = te;
1508 b03c880f 2019-05-21 stsp s->selected++;
1510 941e9f74 2019-05-21 stsp if (s->selected_entry == NULL) {
1511 941e9f74 2019-05-21 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1514 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1515 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1517 67409a31 2019-05-24 stsp if (!S_ISDIR(s->selected_entry->mode)) {
1518 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1519 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1520 67409a31 2019-05-24 stsp s->selected = 0;
1524 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1525 941e9f74 2019-05-21 stsp if (slash)
1526 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1528 941e9f74 2019-05-21 stsp subpath = strdup(path);
1529 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1530 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1534 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1539 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1540 941e9f74 2019-05-21 stsp free(tree_id);
1544 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1545 941e9f74 2019-05-21 stsp if (err) {
1546 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1549 941e9f74 2019-05-21 stsp if (slash == NULL)
1551 941e9f74 2019-05-21 stsp free(subpath);
1552 941e9f74 2019-05-21 stsp subpath = NULL;
1553 941e9f74 2019-05-21 stsp p = slash;
1556 941e9f74 2019-05-21 stsp free(subpath);
1557 1a76625f 2018-10-22 stsp return err;
1560 1a76625f 2018-10-22 stsp static void *
1561 1a76625f 2018-10-22 stsp log_thread(void *arg)
1563 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1564 1a76625f 2018-10-22 stsp int errcode = 0;
1565 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1566 1a76625f 2018-10-22 stsp int done = 0;
1568 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1570 1a76625f 2018-10-22 stsp return (void *)err;
1572 1a76625f 2018-10-22 stsp while (!done && !err) {
1573 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1574 1a76625f 2018-10-22 stsp a->in_repo_path);
1575 1a76625f 2018-10-22 stsp if (err) {
1576 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1577 1a76625f 2018-10-22 stsp return (void *)err;
1578 1a76625f 2018-10-22 stsp err = NULL;
1580 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1581 1a76625f 2018-10-22 stsp a->commits_needed--;
1583 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1584 3abe8080 2019-04-10 stsp if (errcode) {
1585 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1586 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1588 3abe8080 2019-04-10 stsp } else if (*a->quit)
1590 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1591 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1592 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1593 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1597 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1598 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1599 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1600 1a76625f 2018-10-22 stsp &tog_mutex);
1601 1a76625f 2018-10-22 stsp if (errcode)
1602 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1603 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1606 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1607 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1608 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1609 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1611 3abe8080 2019-04-10 stsp a->log_complete = 1;
1612 1a76625f 2018-10-22 stsp return (void *)err;
1615 1a76625f 2018-10-22 stsp static const struct got_error *
1616 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1618 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1619 1a76625f 2018-10-22 stsp int errcode;
1621 1a76625f 2018-10-22 stsp if (s->thread) {
1622 1a76625f 2018-10-22 stsp s->quit = 1;
1623 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1624 1a76625f 2018-10-22 stsp if (errcode)
1625 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1626 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1627 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1628 1a76625f 2018-10-22 stsp if (errcode)
1629 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1630 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1631 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1632 1a76625f 2018-10-22 stsp if (errcode)
1633 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1634 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1635 1a76625f 2018-10-22 stsp if (errcode)
1636 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1637 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1638 1a76625f 2018-10-22 stsp s->thread = NULL;
1641 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1642 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1643 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1645 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1646 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1647 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1650 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1651 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1652 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1655 9343a5fb 2018-06-23 stsp return err;
1658 9343a5fb 2018-06-23 stsp static const struct got_error *
1659 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1661 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1662 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1664 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1665 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1666 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1667 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1668 1a76625f 2018-10-22 stsp free(s->start_id);
1669 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1670 1a76625f 2018-10-22 stsp return err;
1673 1a76625f 2018-10-22 stsp static const struct got_error *
1674 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
1676 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1677 60493ae3 2019-06-20 stsp char pattern[1024];
1680 60493ae3 2019-06-20 stsp if (view->nlines < 1)
1681 60493ae3 2019-06-20 stsp return NULL;
1683 60493ae3 2019-06-20 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1,
1684 60493ae3 2019-06-20 stsp view->begin_x, "/");
1685 60493ae3 2019-06-20 stsp wclrtoeol(view->window);
1687 60493ae3 2019-06-20 stsp nocbreak();
1689 60493ae3 2019-06-20 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
1692 60493ae3 2019-06-20 stsp if (ret == ERR)
1693 60493ae3 2019-06-20 stsp return NULL;
1695 60493ae3 2019-06-20 stsp if (view->searching) {
1696 60493ae3 2019-06-20 stsp regfree(&s->regex);
1697 60493ae3 2019-06-20 stsp view->searching = 0;
1700 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
1701 60493ae3 2019-06-20 stsp if (regcomp(&s->regex, pattern, REG_EXTENDED | REG_ICASE) == 0) {
1702 b1bf1435 2019-06-21 stsp view->searching = TOG_SEARCH_FORWARD;
1703 60493ae3 2019-06-20 stsp view->search_next_done = 0;
1704 60493ae3 2019-06-20 stsp view->search_next(view);
1707 60493ae3 2019-06-20 stsp return NULL;
1710 60493ae3 2019-06-20 stsp static int
1711 60493ae3 2019-06-20 stsp match_commit(struct got_commit_object *commit, regex_t *regex)
1713 60493ae3 2019-06-20 stsp regmatch_t regmatch;
1715 60493ae3 2019-06-20 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1716 60493ae3 2019-06-20 stsp ®match, 0) == 0 ||
1717 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1718 60493ae3 2019-06-20 stsp ®match, 0) == 0 ||
1719 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_logmsg(commit), 1,
1720 60493ae3 2019-06-20 stsp ®match, 0) == 0)
1726 60493ae3 2019-06-20 stsp static const struct got_error *
1727 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
1729 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
1730 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1731 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
1733 60493ae3 2019-06-20 stsp if (!view->searching) {
1734 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1735 60493ae3 2019-06-20 stsp return NULL;
1738 b1bf1435 2019-06-21 stsp if (s->matched_entry) {
1739 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1740 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(s->matched_entry, entry);
1742 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(s->matched_entry,
1743 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
1745 60493ae3 2019-06-20 stsp entry = TAILQ_FIRST(&s->commits.head);
1747 60493ae3 2019-06-20 stsp while (1) {
1748 60493ae3 2019-06-20 stsp if (entry == NULL) {
1749 60493ae3 2019-06-20 stsp if (s->thread_args.log_complete) {
1750 60493ae3 2019-06-20 stsp if (s->matched_entry == NULL) {
1751 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1752 60493ae3 2019-06-20 stsp return NULL;
1754 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1755 60493ae3 2019-06-20 stsp entry = TAILQ_FIRST(&s->commits.head);
1757 b1bf1435 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head,
1758 b1bf1435 2019-06-21 stsp commit_queue_head);
1760 60493ae3 2019-06-20 stsp s->thread_args.commits_needed = 1;
1761 60493ae3 2019-06-20 stsp return trigger_log_thread(0,
1762 60493ae3 2019-06-20 stsp &s->thread_args.commits_needed,
1763 60493ae3 2019-06-20 stsp &s->thread_args.log_complete,
1764 60493ae3 2019-06-20 stsp &s->thread_args.need_commits);
1768 60493ae3 2019-06-20 stsp if (match_commit(entry->commit, &s->regex)) {
1769 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1772 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1773 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
1775 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1778 60493ae3 2019-06-20 stsp if (entry) {
1779 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
1780 60493ae3 2019-06-20 stsp s->matched_entry = entry;
1781 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
1782 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1784 60493ae3 2019-06-20 stsp return err;
1787 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
1788 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1790 60493ae3 2019-06-20 stsp return err;
1795 60493ae3 2019-06-20 stsp return NULL;
1799 60493ae3 2019-06-20 stsp static const struct got_error *
1800 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1801 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
1802 8b473291 2019-02-21 stsp const char *path, int check_disk)
1804 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1805 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1806 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1807 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1808 1a76625f 2018-10-22 stsp int errcode;
1810 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1811 ecb28ae0 2018-07-16 stsp if (err != NULL)
1812 ecb28ae0 2018-07-16 stsp goto done;
1814 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1815 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1816 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1818 8b473291 2019-02-21 stsp s->refs = refs;
1819 fb2756b9 2018-08-04 stsp s->repo = repo;
1820 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1821 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1822 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1823 5036bf37 2018-09-24 stsp goto done;
1826 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1827 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1828 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1829 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
1830 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
1832 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1834 1a76625f 2018-10-22 stsp goto done;
1835 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1836 1a76625f 2018-10-22 stsp 0, thread_repo);
1838 1a76625f 2018-10-22 stsp goto done;
1840 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1841 1a76625f 2018-10-22 stsp if (errcode) {
1842 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
1843 1a76625f 2018-10-22 stsp goto done;
1846 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1847 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1848 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1849 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1850 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1851 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1852 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1853 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1854 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1855 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1856 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1859 1a76625f 2018-10-22 stsp close_log_view(view);
1860 ba4f502b 2018-08-04 stsp return err;
1863 e5a0f69f 2018-08-18 stsp static const struct got_error *
1864 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1866 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1868 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1869 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1870 2b380cc8 2018-10-24 stsp &s->thread_args);
1871 2b380cc8 2018-10-24 stsp if (errcode)
1872 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
1875 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1876 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1877 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
1878 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1881 e5a0f69f 2018-08-18 stsp static const struct got_error *
1882 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1883 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1885 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1886 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1887 5036bf37 2018-09-24 stsp char *parent_path;
1888 669b5ffa 2018-10-07 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
1889 669b5ffa 2018-10-07 stsp int begin_x = 0;
1891 e5a0f69f 2018-08-18 stsp switch (ch) {
1893 1e37a5c2 2019-05-12 jcs s->quit = 1;
1896 1e37a5c2 2019-05-12 jcs case KEY_UP:
1899 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1901 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
1902 1e37a5c2 2019-05-12 jcs s->selected--;
1903 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
1905 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry, 1,
1906 1e37a5c2 2019-05-12 jcs &s->commits);
1908 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
1909 a4292ac5 2019-05-12 jcs case CTRL('b'):
1910 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1912 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
1913 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
1914 1e37a5c2 2019-05-12 jcs s->selected = 0;
1917 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
1918 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
1921 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
1924 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1926 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
1927 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1928 1e37a5c2 2019-05-12 jcs s->selected++;
1931 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
1932 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
1933 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
1934 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1935 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1937 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
1938 a4292ac5 2019-05-12 jcs case CTRL('f'): {
1939 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
1940 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
1941 1e37a5c2 2019-05-12 jcs if (first == NULL)
1943 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
1944 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
1945 1e37a5c2 2019-05-12 jcs &s->commits, &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);
1948 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
1949 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
1950 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1951 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
1952 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
1953 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
1955 1e37a5c2 2019-05-12 jcs err = NULL;
1958 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1959 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
1960 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
1961 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
1962 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
1964 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
1967 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
1969 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
1970 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
1971 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
1972 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
1973 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
1976 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1977 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
1979 1e37a5c2 2019-05-12 jcs return err;
1980 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
1982 1e37a5c2 2019-05-12 jcs view_close(diff_view);
1985 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
1986 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
1988 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
1991 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
1993 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
1994 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
1995 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
1996 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
1999 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2000 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2002 1e37a5c2 2019-05-12 jcs return err;
2003 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2005 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2008 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2009 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2011 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2013 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2014 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2016 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2017 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2018 1e37a5c2 2019-05-12 jcs struct tog_view *lv;
2019 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2021 1e37a5c2 2019-05-12 jcs return err;
2022 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2023 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2024 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2025 638f9024 2019-05-13 stsp return got_error_from_errno(
2026 1e37a5c2 2019-05-12 jcs "view_open");
2027 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2028 1e37a5c2 2019-05-12 jcs s->repo, parent_path, 0);
2030 1e37a5c2 2019-05-12 jcs return err;;
2031 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2032 1e37a5c2 2019-05-12 jcs *new_view = lv;
2034 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2035 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2037 1e37a5c2 2019-05-12 jcs return NULL;
2044 80ddbec8 2018-04-29 stsp return err;
2047 4ed7e80c 2018-05-20 stsp static const struct got_error *
2048 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2050 c2db6724 2019-01-04 stsp const struct got_error *error;
2052 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2053 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2055 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2056 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2058 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2059 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2061 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2062 c2db6724 2019-01-04 stsp if (error != NULL)
2063 c2db6724 2019-01-04 stsp return error;
2065 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2066 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2068 c2db6724 2019-01-04 stsp return NULL;
2071 a915003a 2019-02-05 stsp static void
2072 a915003a 2019-02-05 stsp init_curses(void)
2074 a915003a 2019-02-05 stsp initscr();
2076 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2079 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2080 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2081 a915003a 2019-02-05 stsp curs_set(0);
2082 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2085 c2db6724 2019-01-04 stsp static const struct got_error *
2086 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2088 80ddbec8 2018-04-29 stsp const struct got_error *error;
2089 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2090 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2091 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2092 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2093 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2094 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
2096 04cc582a 2018-08-01 stsp struct tog_view *view;
2098 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2100 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2101 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2102 c2db6724 2019-01-04 stsp NULL) == -1)
2103 80ddbec8 2018-04-29 stsp err(1, "pledge");
2106 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2107 80ddbec8 2018-04-29 stsp switch (ch) {
2109 80ddbec8 2018-04-29 stsp start_commit = optarg;
2112 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2113 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2114 ecb28ae0 2018-07-16 stsp err(1, "-r option");
2117 17020d27 2019-03-07 stsp usage_log();
2118 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2122 80ddbec8 2018-04-29 stsp argc -= optind;
2123 80ddbec8 2018-04-29 stsp argv += optind;
2125 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2126 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2127 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2128 ecb28ae0 2018-07-16 stsp goto done;
2130 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2131 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2132 963f97a1 2019-03-18 stsp goto done;
2133 963f97a1 2019-03-18 stsp error = NULL;
2135 963f97a1 2019-03-18 stsp if (argc == 0) {
2136 963f97a1 2019-03-18 stsp path = strdup("");
2137 963f97a1 2019-03-18 stsp if (path == NULL) {
2138 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2139 ecb28ae0 2018-07-16 stsp goto done;
2141 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2142 963f97a1 2019-03-18 stsp if (worktree) {
2143 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2145 963f97a1 2019-03-18 stsp if (error)
2146 963f97a1 2019-03-18 stsp goto done;
2148 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2149 963f97a1 2019-03-18 stsp if (path == NULL) {
2150 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2151 963f97a1 2019-03-18 stsp goto done;
2155 963f97a1 2019-03-18 stsp usage_log();
2157 963f97a1 2019-03-18 stsp repo_path = worktree ?
2158 963f97a1 2019-03-18 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2159 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2160 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2161 963f97a1 2019-03-18 stsp goto done;
2164 a915003a 2019-02-05 stsp init_curses();
2166 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
2167 80ddbec8 2018-04-29 stsp if (error != NULL)
2168 ecb28ae0 2018-07-16 stsp goto done;
2170 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2171 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2172 c02c541e 2019-03-29 stsp if (error)
2173 c02c541e 2019-03-29 stsp goto done;
2175 15a94983 2018-12-23 stsp if (start_commit == NULL)
2176 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&start_id, worktree ?
2177 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2180 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&start_id, repo,
2181 15a94983 2018-12-23 stsp start_commit);
2182 80ddbec8 2018-04-29 stsp if (error != NULL)
2183 8b473291 2019-02-21 stsp goto done;
2185 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2186 8b473291 2019-02-21 stsp if (error)
2187 ecb28ae0 2018-07-16 stsp goto done;
2189 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2190 04cc582a 2018-08-01 stsp if (view == NULL) {
2191 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2192 04cc582a 2018-08-01 stsp goto done;
2194 8b473291 2019-02-21 stsp error = open_log_view(view, start_id, &refs, repo, path, 1);
2195 ba4f502b 2018-08-04 stsp if (error)
2196 ba4f502b 2018-08-04 stsp goto done;
2197 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2199 ecb28ae0 2018-07-16 stsp free(repo_path);
2200 ecb28ae0 2018-07-16 stsp free(cwd);
2201 ecb28ae0 2018-07-16 stsp free(path);
2202 899d86c2 2018-05-10 stsp free(start_id);
2204 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2205 ec142235 2019-03-07 stsp if (worktree)
2206 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2207 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2208 80ddbec8 2018-04-29 stsp return error;
2211 4ed7e80c 2018-05-20 stsp __dead static void
2212 9f7d7167 2018-04-29 stsp usage_diff(void)
2215 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2216 9f7d7167 2018-04-29 stsp getprogname());
2220 b304db33 2018-05-20 stsp static char *
2221 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2223 b304db33 2018-05-20 stsp char *line;
2224 b304db33 2018-05-20 stsp size_t linelen;
2225 b304db33 2018-05-20 stsp size_t lineno;
2226 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2228 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2230 b304db33 2018-05-20 stsp *len = linelen;
2231 b304db33 2018-05-20 stsp return line;
2234 4ed7e80c 2018-05-20 stsp static const struct got_error *
2235 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2236 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
2237 c3e9aa98 2019-05-13 jcs char *header)
2239 61e69b96 2018-05-20 stsp const struct got_error *err;
2240 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2241 b304db33 2018-05-20 stsp char *line;
2242 b304db33 2018-05-20 stsp size_t len;
2243 61e69b96 2018-05-20 stsp wchar_t *wline;
2244 e0b650dd 2018-05-20 stsp int width;
2246 26ed57b2 2018-05-19 stsp rewind(f);
2247 f7d12f7e 2018-08-01 stsp werase(view->window);
2249 a3404814 2018-09-02 stsp if (header) {
2250 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
2251 a3404814 2018-09-02 stsp if (err) {
2252 a3404814 2018-09-02 stsp return err;
2255 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2256 a3404814 2018-09-02 stsp wstandout(view->window);
2257 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2258 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2259 a3404814 2018-09-02 stsp wstandend(view->window);
2260 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2261 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2263 a3404814 2018-09-02 stsp if (max_lines <= 1)
2264 a3404814 2018-09-02 stsp return NULL;
2265 a3404814 2018-09-02 stsp max_lines--;
2269 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2270 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2271 26ed57b2 2018-05-19 stsp if (line == NULL) {
2275 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2276 26ed57b2 2018-05-19 stsp free(line);
2280 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2281 61e69b96 2018-05-20 stsp if (err) {
2282 61e69b96 2018-05-20 stsp free(line);
2283 61e69b96 2018-05-20 stsp return err;
2285 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2286 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2287 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2288 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2289 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2290 26ed57b2 2018-05-19 stsp free(line);
2291 2550e4c3 2018-07-13 stsp free(wline);
2292 2550e4c3 2018-07-13 stsp wline = NULL;
2294 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2296 1a57306a 2018-09-02 stsp view_vborder(view);
2298 c3e9aa98 2019-05-13 jcs if (*eof) {
2299 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2300 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2301 c3e9aa98 2019-05-13 jcs nprinted++;
2304 c3e9aa98 2019-05-13 jcs err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2306 c3e9aa98 2019-05-13 jcs return err;
2309 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2310 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2311 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2314 26ed57b2 2018-05-19 stsp return NULL;
2317 abd2672a 2018-12-23 stsp static char *
2318 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2320 abd2672a 2018-12-23 stsp char *p, *s = ctime_r(time, datebuf);
2321 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2323 abd2672a 2018-12-23 stsp *p = '\0';
2327 4ed7e80c 2018-05-20 stsp static const struct got_error *
2328 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2329 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2331 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2332 abd2672a 2018-12-23 stsp char datebuf[26];
2333 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2334 15a94983 2018-12-23 stsp char *id_str = NULL;
2335 45d799e2 2018-12-23 stsp time_t committer_time;
2336 45d799e2 2018-12-23 stsp const char *author, *committer;
2337 8b473291 2019-02-21 stsp char *refs_str = NULL;
2339 8b473291 2019-02-21 stsp if (refs) {
2340 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, commit_id);
2342 8b473291 2019-02-21 stsp return err;
2345 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2347 abd2672a 2018-12-23 stsp return err;
2349 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2350 15a94983 2018-12-23 stsp if (err) {
2351 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2352 15a94983 2018-12-23 stsp goto done;
2355 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2356 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2357 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2358 abd2672a 2018-12-23 stsp goto done;
2360 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2361 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2362 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2363 abd2672a 2018-12-23 stsp goto done;
2365 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2366 abd2672a 2018-12-23 stsp if (fprintf(outfile, "date: %s UTC\n",
2367 45d799e2 2018-12-23 stsp get_datestr(&committer_time, datebuf)) < 0) {
2368 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2369 abd2672a 2018-12-23 stsp goto done;
2371 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2372 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2373 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2374 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2375 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2376 abd2672a 2018-12-23 stsp goto done;
2378 45d799e2 2018-12-23 stsp if (fprintf(outfile, "%s\n",
2379 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(commit)) < 0) {
2380 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2381 abd2672a 2018-12-23 stsp goto done;
2384 abd2672a 2018-12-23 stsp free(id_str);
2385 8b473291 2019-02-21 stsp free(refs_str);
2386 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2387 abd2672a 2018-12-23 stsp return err;
2390 abd2672a 2018-12-23 stsp static const struct got_error *
2391 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2393 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2394 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2395 15a94983 2018-12-23 stsp int obj_type;
2397 511a516b 2018-05-19 stsp f = got_opentemp();
2398 48ae06ee 2018-10-18 stsp if (f == NULL) {
2399 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2400 48ae06ee 2018-10-18 stsp goto done;
2402 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2403 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2404 fb43ecf1 2019-02-11 stsp goto done;
2408 15a94983 2018-12-23 stsp if (s->id1)
2409 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2411 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2413 15a94983 2018-12-23 stsp goto done;
2415 15a94983 2018-12-23 stsp switch (obj_type) {
2416 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2417 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2418 54156555 2018-12-24 stsp s->diff_context, s->repo, f);
2420 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2421 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2422 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
2424 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2425 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2426 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2427 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2429 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2432 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2433 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2434 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2436 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2437 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2438 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2439 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2440 8b473291 2019-02-21 stsp s->repo, f);
2445 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2447 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2448 15a94983 2018-12-23 stsp s->diff_context, s->repo, f);
2452 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2456 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2457 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2458 48ae06ee 2018-10-18 stsp return err;
2461 f5215bb9 2019-02-22 stsp static void
2462 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2464 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2465 f5215bb9 2019-02-22 stsp update_panels();
2466 f5215bb9 2019-02-22 stsp doupdate();
2469 48ae06ee 2018-10-18 stsp static const struct got_error *
2470 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2471 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2472 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2474 48ae06ee 2018-10-18 stsp const struct got_error *err;
2476 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2477 15a94983 2018-12-23 stsp int type1, type2;
2478 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2480 15a94983 2018-12-23 stsp return err;
2481 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2483 15a94983 2018-12-23 stsp return err;
2485 15a94983 2018-12-23 stsp if (type1 != type2)
2486 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2489 15a94983 2018-12-23 stsp if (id1) {
2490 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2491 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2492 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2494 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2496 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2497 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2498 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2499 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2500 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2502 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2503 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2504 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2505 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2506 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2507 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2508 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2510 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2511 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2512 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2514 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2515 48ae06ee 2018-10-18 stsp if (err) {
2516 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2517 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2518 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2519 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2520 48ae06ee 2018-10-18 stsp return err;
2523 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2524 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2525 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2527 5dc9f4bc 2018-08-04 stsp return NULL;
2530 e5a0f69f 2018-08-18 stsp static const struct got_error *
2531 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2533 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2535 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2536 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2537 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2538 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2539 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2540 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2541 e5a0f69f 2018-08-18 stsp return err;
2544 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2545 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2547 a3404814 2018-09-02 stsp const struct got_error *err;
2548 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2549 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2551 a3404814 2018-09-02 stsp if (s->id1) {
2552 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2554 a3404814 2018-09-02 stsp return err;
2556 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2558 a3404814 2018-09-02 stsp return err;
2560 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2561 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2562 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2563 a3404814 2018-09-02 stsp free(id_str1);
2564 a3404814 2018-09-02 stsp free(id_str2);
2565 a3404814 2018-09-02 stsp return err;
2567 a3404814 2018-09-02 stsp free(id_str1);
2568 a3404814 2018-09-02 stsp free(id_str2);
2570 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2571 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2575 15a087fe 2019-02-21 stsp static const struct got_error *
2576 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2577 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2579 d7a04538 2019-02-21 stsp const struct got_error *err;
2580 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
2581 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
2582 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
2584 15a087fe 2019-02-21 stsp free(s->id2);
2585 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2586 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2587 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2589 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2591 d7a04538 2019-02-21 stsp return err;
2592 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
2593 15a087fe 2019-02-21 stsp free(s->id1);
2594 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
2595 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2596 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
2597 15a087fe 2019-02-21 stsp return NULL;
2600 0cf4efb1 2018-09-29 stsp static const struct got_error *
2601 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2602 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2604 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2605 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2606 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
2607 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
2610 e5a0f69f 2018-08-18 stsp switch (ch) {
2612 1e37a5c2 2019-05-12 jcs case KEY_UP:
2613 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
2614 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2616 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2617 a60a9dc4 2019-05-13 jcs case CTRL('b'):
2618 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
2621 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
2622 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
2623 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2626 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2627 1e37a5c2 2019-05-12 jcs if (!s->eof)
2628 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2630 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
2631 a60a9dc4 2019-05-13 jcs case CTRL('f'):
2633 00ba99a7 2019-05-12 jcs if (s->eof)
2636 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
2637 1e37a5c2 2019-05-12 jcs char *line;
2638 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
2639 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2640 1e37a5c2 2019-05-12 jcs if (line == NULL)
2645 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
2646 1e37a5c2 2019-05-12 jcs s->diff_context--;
2647 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2648 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2652 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2653 1e37a5c2 2019-05-12 jcs s->diff_context++;
2654 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2655 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2660 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2662 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2663 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
2664 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
2665 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2668 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2673 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2677 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2678 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2680 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2681 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2685 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2687 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2689 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2690 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
2692 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
2693 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
2694 1e37a5c2 2019-05-12 jcs update_panels();
2695 1e37a5c2 2019-05-12 jcs doupdate();
2697 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
2698 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
2699 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
2700 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
2704 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2709 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
2710 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2713 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2717 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2718 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2720 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2721 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2727 bcbd79e2 2018-08-19 stsp return err;
2730 4ed7e80c 2018-05-20 stsp static const struct got_error *
2731 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2733 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2734 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2735 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2736 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2737 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2738 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2740 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2742 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2744 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2745 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2746 eb6600df 2019-01-04 stsp NULL) == -1)
2747 26ed57b2 2018-05-19 stsp err(1, "pledge");
2750 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2751 26ed57b2 2018-05-19 stsp switch (ch) {
2753 17020d27 2019-03-07 stsp usage_diff();
2754 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2758 26ed57b2 2018-05-19 stsp argc -= optind;
2759 26ed57b2 2018-05-19 stsp argv += optind;
2761 26ed57b2 2018-05-19 stsp if (argc == 0) {
2762 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2763 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2764 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2765 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2766 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2767 15a94983 2018-12-23 stsp id_str1 = argv[0];
2768 15a94983 2018-12-23 stsp id_str2 = argv[1];
2769 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2770 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2771 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2772 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2773 15a94983 2018-12-23 stsp id_str1 = argv[1];
2774 15a94983 2018-12-23 stsp id_str2 = argv[2];
2776 26ed57b2 2018-05-19 stsp usage_diff();
2778 a915003a 2019-02-05 stsp init_curses();
2780 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2781 eb6600df 2019-01-04 stsp if (error)
2782 eb6600df 2019-01-04 stsp goto done;
2784 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
2785 26ed57b2 2018-05-19 stsp if (error)
2786 26ed57b2 2018-05-19 stsp goto done;
2788 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
2789 26ed57b2 2018-05-19 stsp if (error)
2790 26ed57b2 2018-05-19 stsp goto done;
2792 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
2793 26ed57b2 2018-05-19 stsp if (error)
2794 26ed57b2 2018-05-19 stsp goto done;
2796 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2797 8b473291 2019-02-21 stsp if (error)
2798 8b473291 2019-02-21 stsp goto done;
2800 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2801 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2802 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2803 ea5e7bb5 2018-08-01 stsp goto done;
2805 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2806 5dc9f4bc 2018-08-04 stsp if (error)
2807 5dc9f4bc 2018-08-04 stsp goto done;
2808 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2810 c02c541e 2019-03-29 stsp free(repo_path);
2811 26ed57b2 2018-05-19 stsp got_repo_close(repo);
2812 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2813 26ed57b2 2018-05-19 stsp return error;
2816 4ed7e80c 2018-05-20 stsp __dead static void
2817 9f7d7167 2018-04-29 stsp usage_blame(void)
2820 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2821 9f7d7167 2018-04-29 stsp getprogname());
2825 84451b3e 2018-07-10 stsp struct tog_blame_line {
2826 84451b3e 2018-07-10 stsp int annotated;
2827 84451b3e 2018-07-10 stsp struct got_object_id *id;
2830 4ed7e80c 2018-05-20 stsp static const struct got_error *
2831 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2832 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2833 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2834 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2836 84451b3e 2018-07-10 stsp const struct got_error *err;
2837 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2838 84451b3e 2018-07-10 stsp char *line;
2839 84451b3e 2018-07-10 stsp size_t len;
2840 84451b3e 2018-07-10 stsp wchar_t *wline;
2841 b700b5d6 2018-07-10 stsp int width, wlimit;
2842 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2843 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2844 ab089a2a 2018-07-12 stsp char *id_str;
2846 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2848 ab089a2a 2018-07-12 stsp return err;
2850 84451b3e 2018-07-10 stsp rewind(f);
2851 f7d12f7e 2018-08-01 stsp werase(view->window);
2853 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
2854 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2855 ab089a2a 2018-07-12 stsp free(id_str);
2856 ab089a2a 2018-07-12 stsp return err;
2859 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2860 ab089a2a 2018-07-12 stsp free(line);
2861 2550e4c3 2018-07-13 stsp line = NULL;
2862 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2863 a3404814 2018-09-02 stsp wstandout(view->window);
2864 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2865 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2866 a3404814 2018-09-02 stsp wstandend(view->window);
2867 2550e4c3 2018-07-13 stsp free(wline);
2868 2550e4c3 2018-07-13 stsp wline = NULL;
2869 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2870 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2872 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2873 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2874 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
2875 ab089a2a 2018-07-12 stsp free(id_str);
2876 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2878 ab089a2a 2018-07-12 stsp free(id_str);
2879 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2880 3f60a8ef 2018-07-10 stsp free(line);
2881 2550e4c3 2018-07-13 stsp line = NULL;
2883 3f60a8ef 2018-07-10 stsp return err;
2884 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2885 2550e4c3 2018-07-13 stsp free(wline);
2886 2550e4c3 2018-07-13 stsp wline = NULL;
2887 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2888 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2891 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2892 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2893 84451b3e 2018-07-10 stsp if (line == NULL) {
2897 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2898 84451b3e 2018-07-10 stsp free(line);
2902 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2903 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2904 84451b3e 2018-07-10 stsp if (err) {
2905 84451b3e 2018-07-10 stsp free(line);
2906 84451b3e 2018-07-10 stsp return err;
2909 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2910 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2912 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2913 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2914 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2915 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2916 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2917 84451b3e 2018-07-10 stsp char *id_str;
2918 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2919 84451b3e 2018-07-10 stsp if (err) {
2920 84451b3e 2018-07-10 stsp free(line);
2921 2550e4c3 2018-07-13 stsp free(wline);
2922 84451b3e 2018-07-10 stsp return err;
2924 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2925 84451b3e 2018-07-10 stsp free(id_str);
2926 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2928 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2929 ee41ec32 2018-07-10 stsp prev_id = NULL;
2932 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2933 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2934 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2937 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2938 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2939 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2940 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2941 84451b3e 2018-07-10 stsp free(line);
2942 2550e4c3 2018-07-13 stsp free(wline);
2943 2550e4c3 2018-07-13 stsp wline = NULL;
2945 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2947 1a57306a 2018-09-02 stsp view_vborder(view);
2949 84451b3e 2018-07-10 stsp return NULL;
2952 84451b3e 2018-07-10 stsp static const struct got_error *
2953 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2955 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2956 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2957 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2958 1a76625f 2018-10-22 stsp int errcode;
2960 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2961 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2962 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2964 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2965 1a76625f 2018-10-22 stsp if (errcode)
2966 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
2968 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2969 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2970 d68a0a7d 2018-07-10 stsp goto done;
2973 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2974 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2976 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2977 d68a0a7d 2018-07-10 stsp if (line->annotated)
2978 d68a0a7d 2018-07-10 stsp goto done;
2980 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2981 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2982 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2983 84451b3e 2018-07-10 stsp goto done;
2985 84451b3e 2018-07-10 stsp line->annotated = 1;
2987 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2988 1a76625f 2018-10-22 stsp if (errcode)
2989 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2990 84451b3e 2018-07-10 stsp return err;
2993 84451b3e 2018-07-10 stsp static void *
2994 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2996 18430de3 2018-07-10 stsp const struct got_error *err;
2997 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
2998 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
2999 1a76625f 2018-10-22 stsp int errcode;
3001 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3002 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
3004 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3005 1a76625f 2018-10-22 stsp if (errcode)
3006 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3007 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3009 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3010 c9beca56 2018-07-22 stsp ta->repo = NULL;
3011 c9beca56 2018-07-22 stsp *ta->complete = 1;
3013 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3014 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3015 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3017 18430de3 2018-07-10 stsp return (void *)err;
3020 245d91c1 2018-07-12 stsp static struct got_object_id *
3021 15a94983 2018-12-23 stsp get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3022 15a94983 2018-12-23 stsp int selected_line)
3024 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3026 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3027 245d91c1 2018-07-12 stsp if (!line->annotated)
3028 245d91c1 2018-07-12 stsp return NULL;
3030 245d91c1 2018-07-12 stsp return line->id;
3033 b880a918 2018-07-10 stsp static const struct got_error *
3034 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3036 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3039 245d91c1 2018-07-12 stsp if (blame->thread) {
3040 1a76625f 2018-10-22 stsp int errcode;
3041 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3042 1a76625f 2018-10-22 stsp if (errcode)
3043 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3044 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3045 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3046 1a76625f 2018-10-22 stsp if (errcode)
3047 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3048 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3049 1a76625f 2018-10-22 stsp if (errcode)
3050 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3051 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3052 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3053 245d91c1 2018-07-12 stsp err = NULL;
3054 245d91c1 2018-07-12 stsp blame->thread = NULL;
3056 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3057 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3058 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3060 245d91c1 2018-07-12 stsp if (blame->f) {
3061 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3062 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3063 245d91c1 2018-07-12 stsp blame->f = NULL;
3065 57670559 2018-12-23 stsp if (blame->lines) {
3066 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3067 57670559 2018-12-23 stsp free(blame->lines[i].id);
3068 57670559 2018-12-23 stsp free(blame->lines);
3069 57670559 2018-12-23 stsp blame->lines = NULL;
3071 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3072 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3074 245d91c1 2018-07-12 stsp return err;
3077 245d91c1 2018-07-12 stsp static const struct got_error *
3078 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3079 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3080 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3081 245d91c1 2018-07-12 stsp struct got_repository *repo)
3083 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3084 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3085 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3086 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3087 15a94983 2018-12-23 stsp int obj_type;
3089 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3091 15a94983 2018-12-23 stsp return err;
3092 15a94983 2018-12-23 stsp if (obj_id == NULL)
3093 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
3095 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3097 84451b3e 2018-07-10 stsp goto done;
3099 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3100 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3101 84451b3e 2018-07-10 stsp goto done;
3104 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3106 a70480e0 2018-06-23 stsp goto done;
3107 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3108 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3109 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3110 84451b3e 2018-07-10 stsp goto done;
3112 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3113 245d91c1 2018-07-12 stsp blame->f, blob);
3115 84451b3e 2018-07-10 stsp goto done;
3117 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3118 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3119 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3120 84451b3e 2018-07-10 stsp goto done;
3123 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3125 bd24772e 2018-07-11 stsp goto done;
3127 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3128 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3129 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3130 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3131 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3132 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3133 245d91c1 2018-07-12 stsp goto done;
3135 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3137 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3138 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3139 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3140 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3141 245d91c1 2018-07-12 stsp *blame_complete = 0;
3145 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3146 27d434c2 2018-09-15 stsp free(obj_id);
3148 245d91c1 2018-07-12 stsp stop_blame(blame);
3149 245d91c1 2018-07-12 stsp return err;
3152 245d91c1 2018-07-12 stsp static const struct got_error *
3153 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3154 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3155 8b473291 2019-02-21 stsp struct got_repository *repo)
3157 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3158 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3160 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3162 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3164 7cbe629d 2018-08-04 stsp return err;
3166 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3167 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3168 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3169 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3170 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3171 fb2756b9 2018-08-04 stsp s->path = path;
3172 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
3173 638f9024 2019-05-13 stsp return got_error_from_errno("open_blame_view");
3174 fb2756b9 2018-08-04 stsp s->repo = repo;
3175 8b473291 2019-02-21 stsp s->refs = refs;
3176 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3177 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3179 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3180 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3181 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3183 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3184 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3185 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3186 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3189 e5a0f69f 2018-08-18 stsp static const struct got_error *
3190 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3192 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3193 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3195 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3196 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3198 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3199 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3200 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3201 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3202 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3205 e5a0f69f 2018-08-18 stsp free(s->path);
3207 e5a0f69f 2018-08-18 stsp return err;
3210 7cbe629d 2018-08-04 stsp static const struct got_error *
3211 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
3213 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3214 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
3215 2b380cc8 2018-10-24 stsp int errcode;
3217 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
3218 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3219 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
3220 2b380cc8 2018-10-24 stsp if (errcode)
3221 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3224 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3225 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3226 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
3227 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
3229 669b5ffa 2018-10-07 stsp view_vborder(view);
3230 e5a0f69f 2018-08-18 stsp return err;
3233 e5a0f69f 2018-08-18 stsp static const struct got_error *
3234 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3235 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3237 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
3238 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
3239 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3240 669b5ffa 2018-10-07 stsp int begin_x = 0;
3242 e5a0f69f 2018-08-18 stsp switch (ch) {
3244 1e37a5c2 2019-05-12 jcs s->done = 1;
3247 1e37a5c2 2019-05-12 jcs case KEY_UP:
3248 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
3249 1e37a5c2 2019-05-12 jcs s->selected_line--;
3250 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
3251 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3252 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3254 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3255 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
3256 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3259 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3260 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3261 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3263 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3266 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3267 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3268 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3269 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3270 1e37a5c2 2019-05-12 jcs s->selected_line++;
3271 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3272 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3273 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3276 1e37a5c2 2019-05-12 jcs case 'p': {
3277 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3278 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3279 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3280 1e37a5c2 2019-05-12 jcs if (id == NULL)
3282 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3283 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3284 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3285 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3286 1e37a5c2 2019-05-12 jcs int obj_type;
3287 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3288 1e37a5c2 2019-05-12 jcs s->repo, id);
3291 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3292 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3293 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3294 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3297 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3298 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3299 1e37a5c2 2019-05-12 jcs pid->id, s->path);
3300 e5a0f69f 2018-08-18 stsp if (err) {
3301 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
3302 1e37a5c2 2019-05-12 jcs err = NULL;
3303 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3306 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
3308 1e37a5c2 2019-05-12 jcs free(blob_id);
3309 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
3310 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
3311 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3314 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3316 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3318 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
3319 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
3321 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3326 1e37a5c2 2019-05-12 jcs s->done = 1;
3327 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3328 1e37a5c2 2019-05-12 jcs s->done = 0;
3329 1e37a5c2 2019-05-12 jcs if (thread_err)
3331 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3332 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
3333 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3334 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3335 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
3336 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
3341 1e37a5c2 2019-05-12 jcs case 'B': {
3342 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
3343 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
3344 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
3346 1e37a5c2 2019-05-12 jcs s->done = 1;
3347 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3348 1e37a5c2 2019-05-12 jcs s->done = 0;
3349 1e37a5c2 2019-05-12 jcs if (thread_err)
3351 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3352 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
3353 1e37a5c2 2019-05-12 jcs s->blamed_commit =
3354 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
3355 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3356 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3357 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
3358 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
3363 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3364 1e37a5c2 2019-05-12 jcs case '\r': {
3365 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3366 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
3367 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
3368 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3369 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3370 1e37a5c2 2019-05-12 jcs if (id == NULL)
3372 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
3375 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
3376 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
3377 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3378 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3379 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3380 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
3381 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3382 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
3385 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
3386 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
3387 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3389 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3392 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3393 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3396 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
3398 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3401 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
3402 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
3404 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3409 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3411 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3412 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
3413 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
3416 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3417 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
3418 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3419 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3422 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
3423 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
3424 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
3425 1e37a5c2 2019-05-12 jcs view->nlines - 2;
3427 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
3428 1e37a5c2 2019-05-12 jcs s->blame.nlines -
3429 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
3431 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3432 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
3433 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3434 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3440 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
3443 a70480e0 2018-06-23 stsp static const struct got_error *
3444 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
3446 a70480e0 2018-06-23 stsp const struct got_error *error;
3447 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
3448 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3449 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
3450 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3451 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3452 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
3454 e1cd8fed 2018-08-01 stsp struct tog_view *view;
3456 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3458 a70480e0 2018-06-23 stsp #ifndef PROFILE
3459 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3460 8e94dd5b 2019-01-04 stsp NULL) == -1)
3461 a70480e0 2018-06-23 stsp err(1, "pledge");
3464 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3465 a70480e0 2018-06-23 stsp switch (ch) {
3467 a70480e0 2018-06-23 stsp commit_id_str = optarg;
3470 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3471 69069811 2018-08-02 stsp if (repo_path == NULL)
3472 69069811 2018-08-02 stsp err(1, "-r option");
3475 17020d27 2019-03-07 stsp usage_blame();
3476 a70480e0 2018-06-23 stsp /* NOTREACHED */
3480 a70480e0 2018-06-23 stsp argc -= optind;
3481 a70480e0 2018-06-23 stsp argv += optind;
3483 69069811 2018-08-02 stsp if (argc == 1)
3484 69069811 2018-08-02 stsp path = argv[0];
3486 a70480e0 2018-06-23 stsp usage_blame();
3488 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
3489 69069811 2018-08-02 stsp if (cwd == NULL) {
3490 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3491 69069811 2018-08-02 stsp goto done;
3493 69069811 2018-08-02 stsp if (repo_path == NULL) {
3494 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3495 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3496 69069811 2018-08-02 stsp goto done;
3498 eb41ed75 2019-02-05 stsp error = NULL;
3499 eb41ed75 2019-02-05 stsp if (worktree) {
3500 eb41ed75 2019-02-05 stsp repo_path =
3501 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3502 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
3503 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3504 eb41ed75 2019-02-05 stsp if (error)
3505 eb41ed75 2019-02-05 stsp goto done;
3507 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
3508 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
3509 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3510 eb41ed75 2019-02-05 stsp goto done;
3515 a915003a 2019-02-05 stsp init_curses();
3517 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
3518 c02c541e 2019-03-29 stsp if (error != NULL)
3519 8e94dd5b 2019-01-04 stsp goto done;
3521 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3522 c02c541e 2019-03-29 stsp if (error)
3523 92205607 2019-01-04 stsp goto done;
3525 eb41ed75 2019-02-05 stsp if (worktree) {
3526 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3527 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3528 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3529 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3530 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3531 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3532 eb41ed75 2019-02-05 stsp path) == -1) {
3533 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3534 eb41ed75 2019-02-05 stsp goto done;
3536 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3539 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3541 eb41ed75 2019-02-05 stsp if (error)
3542 69069811 2018-08-02 stsp goto done;
3544 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
3545 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
3546 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3547 a70480e0 2018-06-23 stsp if (error != NULL)
3548 66b4983c 2018-06-23 stsp goto done;
3549 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3550 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
3552 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
3553 15a94983 2018-12-23 stsp commit_id_str);
3555 a19e88aa 2018-06-23 stsp if (error != NULL)
3556 8b473291 2019-02-21 stsp goto done;
3558 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
3559 8b473291 2019-02-21 stsp if (error)
3560 a19e88aa 2018-06-23 stsp goto done;
3562 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3563 e1cd8fed 2018-08-01 stsp if (view == NULL) {
3564 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3565 e1cd8fed 2018-08-01 stsp goto done;
3567 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3568 7cbe629d 2018-08-04 stsp if (error)
3569 7cbe629d 2018-08-04 stsp goto done;
3570 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3572 69069811 2018-08-02 stsp free(repo_path);
3573 69069811 2018-08-02 stsp free(cwd);
3574 a70480e0 2018-06-23 stsp free(commit_id);
3575 eb41ed75 2019-02-05 stsp if (worktree)
3576 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
3578 a70480e0 2018-06-23 stsp got_repo_close(repo);
3579 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3580 a70480e0 2018-06-23 stsp return error;
3583 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3584 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
3585 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
3586 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
3587 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
3588 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
3589 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
3591 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3592 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
3593 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
3594 ffd1d5e5 2018-06-23 stsp int width, n;
3596 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
3598 f7d12f7e 2018-08-01 stsp werase(view->window);
3600 ffd1d5e5 2018-06-23 stsp if (limit == 0)
3601 ffd1d5e5 2018-06-23 stsp return NULL;
3603 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
3605 ffd1d5e5 2018-06-23 stsp return err;
3606 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3607 a3404814 2018-09-02 stsp wstandout(view->window);
3608 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3609 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3610 a3404814 2018-09-02 stsp wstandend(view->window);
3611 2550e4c3 2018-07-13 stsp free(wline);
3612 2550e4c3 2018-07-13 stsp wline = NULL;
3613 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3614 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3615 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3616 ffd1d5e5 2018-06-23 stsp return NULL;
3617 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
3619 ce52c690 2018-06-23 stsp return err;
3620 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3621 2550e4c3 2018-07-13 stsp free(wline);
3622 2550e4c3 2018-07-13 stsp wline = NULL;
3623 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3624 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3625 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3626 ffd1d5e5 2018-06-23 stsp return NULL;
3627 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3628 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
3629 a1eca9bb 2018-06-23 stsp return NULL;
3631 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3632 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
3633 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
3634 0cf4efb1 2018-09-29 stsp if (view->focussed)
3635 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3636 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
3638 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
3639 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
3640 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3641 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3642 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3643 ffd1d5e5 2018-06-23 stsp return NULL;
3647 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
3648 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3651 ffd1d5e5 2018-06-23 stsp while (te) {
3652 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
3654 1d13200f 2018-07-12 stsp if (show_ids) {
3655 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
3657 638f9024 2019-05-13 stsp return got_error_from_errno(
3658 230a42bd 2019-05-11 jcs "got_object_id_str");
3660 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3661 d8355ef1 2019-02-10 stsp te->name, S_ISDIR(te->mode) ? "/" :
3662 d8355ef1 2019-02-10 stsp ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3663 1d13200f 2018-07-12 stsp free(id_str);
3664 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3666 1d13200f 2018-07-12 stsp free(id_str);
3667 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
3668 ffd1d5e5 2018-06-23 stsp if (err) {
3669 ffd1d5e5 2018-06-23 stsp free(line);
3672 ffd1d5e5 2018-06-23 stsp if (n == selected) {
3673 0cf4efb1 2018-09-29 stsp if (view->focussed)
3674 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3675 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
3677 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3678 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3679 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3680 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
3681 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3682 ffd1d5e5 2018-06-23 stsp free(line);
3683 2550e4c3 2018-07-13 stsp free(wline);
3684 2550e4c3 2018-07-13 stsp wline = NULL;
3686 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3687 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
3688 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3690 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3693 ffd1d5e5 2018-06-23 stsp return err;
3696 ffd1d5e5 2018-06-23 stsp static void
3697 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
3698 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
3699 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
3701 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
3704 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
3707 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3708 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
3709 ffd1d5e5 2018-06-23 stsp if (!isroot)
3710 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3714 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
3715 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
3716 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
3717 ffd1d5e5 2018-06-23 stsp prev = te;
3718 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3720 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
3721 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3723 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3724 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3727 768394f3 2019-01-24 stsp static int
3728 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3729 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
3730 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
3732 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
3733 ffd1d5e5 2018-06-23 stsp int n = 0;
3735 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
3736 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3738 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
3739 768394f3 2019-01-24 stsp last = last_displayed_entry;
3740 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
3741 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
3742 768394f3 2019-01-24 stsp if (last) {
3743 768394f3 2019-01-24 stsp *first_displayed_entry = next;
3744 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
3750 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3751 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
3752 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
3754 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
3755 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
3756 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
3758 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
3759 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
3761 ce52c690 2018-06-23 stsp len += strlen(te->name);
3763 ce52c690 2018-06-23 stsp *path = calloc(1, len);
3764 ffd1d5e5 2018-06-23 stsp if (path == NULL)
3765 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
3767 ce52c690 2018-06-23 stsp (*path)[0] = '/';
3768 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
3769 d9765a41 2018-06-23 stsp while (pt) {
3770 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3771 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3772 cb2ebc8a 2018-06-23 stsp goto done;
3774 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
3775 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3776 cb2ebc8a 2018-06-23 stsp goto done;
3778 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3781 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3782 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3783 ce52c690 2018-06-23 stsp goto done;
3787 ce52c690 2018-06-23 stsp if (err) {
3788 ce52c690 2018-06-23 stsp free(*path);
3789 ce52c690 2018-06-23 stsp *path = NULL;
3791 ce52c690 2018-06-23 stsp return err;
3794 ce52c690 2018-06-23 stsp static const struct got_error *
3795 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3796 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3797 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3798 8b473291 2019-02-21 stsp struct got_repository *repo)
3800 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3801 ce52c690 2018-06-23 stsp char *path;
3802 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3804 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3806 ce52c690 2018-06-23 stsp return err;
3808 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3809 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3810 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3812 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
3813 e5a0f69f 2018-08-18 stsp if (err) {
3814 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3815 e5a0f69f 2018-08-18 stsp free(path);
3817 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3818 69efd4c4 2018-07-18 stsp return err;
3821 69efd4c4 2018-07-18 stsp static const struct got_error *
3822 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3823 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3824 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3825 8b473291 2019-02-21 stsp struct got_repository *repo)
3827 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3828 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3829 69efd4c4 2018-07-18 stsp char *path;
3831 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3832 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3833 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3835 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3837 69efd4c4 2018-07-18 stsp return err;
3839 8b473291 2019-02-21 stsp err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3841 e5a0f69f 2018-08-18 stsp view_close(log_view);
3843 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3844 cb2ebc8a 2018-06-23 stsp free(path);
3845 cb2ebc8a 2018-06-23 stsp return err;
3848 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3849 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3850 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3851 8b473291 2019-02-21 stsp struct got_repository *repo)
3853 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3854 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3855 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3857 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3859 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3860 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3861 ffd1d5e5 2018-06-23 stsp goto done;
3863 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3864 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3865 ffd1d5e5 2018-06-23 stsp goto done;
3868 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3869 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3870 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3871 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3872 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3873 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3874 6484ec90 2018-09-29 stsp goto done;
3876 8b473291 2019-02-21 stsp s->refs = refs;
3877 fb2756b9 2018-08-04 stsp s->repo = repo;
3879 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
3880 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
3881 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
3883 ad80ab7b 2018-08-04 stsp free(commit_id_str);
3884 6484ec90 2018-09-29 stsp if (err) {
3885 fb2756b9 2018-08-04 stsp free(s->tree_label);
3886 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3888 ad80ab7b 2018-08-04 stsp return err;
3891 e5a0f69f 2018-08-18 stsp static const struct got_error *
3892 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
3894 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3896 fb2756b9 2018-08-04 stsp free(s->tree_label);
3897 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3898 6484ec90 2018-09-29 stsp free(s->commit_id);
3899 6484ec90 2018-09-29 stsp s->commit_id = NULL;
3900 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
3901 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
3902 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
3903 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
3904 ad80ab7b 2018-08-04 stsp free(parent);
3907 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
3908 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
3909 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
3911 e5a0f69f 2018-08-18 stsp return NULL;
3914 ad80ab7b 2018-08-04 stsp static const struct got_error *
3915 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
3917 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
3918 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3919 e5a0f69f 2018-08-18 stsp char *parent_path;
3921 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
3923 e5a0f69f 2018-08-18 stsp return err;
3925 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
3926 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
3927 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3928 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
3929 e5a0f69f 2018-08-18 stsp free(parent_path);
3931 669b5ffa 2018-10-07 stsp view_vborder(view);
3932 e5a0f69f 2018-08-18 stsp return err;
3935 e5a0f69f 2018-08-18 stsp static const struct got_error *
3936 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3937 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3939 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3940 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
3941 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
3942 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
3944 e5a0f69f 2018-08-18 stsp switch (ch) {
3946 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
3949 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
3951 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3952 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3953 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
3954 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
3955 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
3956 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3957 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3959 1e37a5c2 2019-05-12 jcs return err;
3960 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
3962 1e37a5c2 2019-05-12 jcs view_close(log_view);
3965 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
3966 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
3968 1e37a5c2 2019-05-12 jcs *new_view = log_view;
3971 1e37a5c2 2019-05-12 jcs case KEY_UP:
3972 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
3973 1e37a5c2 2019-05-12 jcs s->selected--;
3974 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
3977 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
3979 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
3980 1e37a5c2 2019-05-12 jcs s->entries, s->tree == s->root);
3982 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3983 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
3984 1e37a5c2 2019-05-12 jcs MAX(0, view->nlines - 4 - s->selected), s->entries,
3985 1e37a5c2 2019-05-12 jcs s->tree == s->root);
3986 1e37a5c2 2019-05-12 jcs s->selected = 0;
3987 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_FIRST(&s->entries->head) ==
3988 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
3989 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
3992 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3993 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
3994 1e37a5c2 2019-05-12 jcs s->selected++;
3997 00ba99a7 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
3998 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
4000 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
4001 1e37a5c2 2019-05-12 jcs s->last_displayed_entry, s->entries);
4003 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4004 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4006 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
4007 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
4008 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4011 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
4012 1e37a5c2 2019-05-12 jcs view->nlines, s->last_displayed_entry, s->entries);
4013 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
4014 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
4015 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
4016 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
4018 1e37a5c2 2019-05-12 jcs ndisplayed++;
4019 1e37a5c2 2019-05-12 jcs te = SIMPLEQ_NEXT(te, entry);
4020 1e37a5c2 2019-05-12 jcs } while (te);
4021 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
4024 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4026 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4027 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4028 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
4029 1e37a5c2 2019-05-12 jcs /* user selected '..' */
4030 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
4032 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
4033 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
4035 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
4036 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
4037 1e37a5c2 2019-05-12 jcs s->entries =
4038 1e37a5c2 2019-05-12 jcs got_object_tree_get_entries(s->tree);
4039 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
4040 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
4041 1e37a5c2 2019-05-12 jcs s->selected_entry =
4042 1e37a5c2 2019-05-12 jcs parent->selected_entry;
4043 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
4044 1e37a5c2 2019-05-12 jcs free(parent);
4045 1e37a5c2 2019-05-12 jcs } else if (S_ISDIR(s->selected_entry->mode)) {
4046 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
4047 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&subtree,
4048 1e37a5c2 2019-05-12 jcs s->repo, s->selected_entry->id);
4051 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
4052 941e9f74 2019-05-21 stsp if (err) {
4053 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
4056 1e37a5c2 2019-05-12 jcs } else if (S_ISREG(s->selected_entry->mode)) {
4057 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
4058 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
4059 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
4061 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
4062 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
4063 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
4066 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
4067 669b5ffa 2018-10-07 stsp err = view_close_child(view);
4069 669b5ffa 2018-10-07 stsp return err;
4070 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
4071 669b5ffa 2018-10-07 stsp if (err) {
4072 1e37a5c2 2019-05-12 jcs view_close(blame_view);
4075 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
4076 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
4078 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
4081 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4082 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
4083 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4089 ffd1d5e5 2018-06-23 stsp return err;
4092 ffd1d5e5 2018-06-23 stsp __dead static void
4093 ffd1d5e5 2018-06-23 stsp usage_tree(void)
4096 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4097 ffd1d5e5 2018-06-23 stsp getprogname());
4101 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4102 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
4104 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
4105 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
4106 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4107 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
4108 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4109 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
4110 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
4111 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
4113 5221c383 2018-08-01 stsp struct tog_view *view;
4115 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4117 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
4118 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4119 d188b9a6 2019-01-04 stsp NULL) == -1)
4120 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
4123 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
4124 ffd1d5e5 2018-06-23 stsp switch (ch) {
4126 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
4129 17020d27 2019-03-07 stsp usage_tree();
4130 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
4134 ffd1d5e5 2018-06-23 stsp argc -= optind;
4135 ffd1d5e5 2018-06-23 stsp argv += optind;
4137 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
4138 52185f70 2019-02-05 stsp struct got_worktree *worktree;
4139 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
4140 52185f70 2019-02-05 stsp if (cwd == NULL)
4141 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
4142 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4143 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4144 52185f70 2019-02-05 stsp goto done;
4145 52185f70 2019-02-05 stsp if (worktree) {
4146 52185f70 2019-02-05 stsp free(cwd);
4147 52185f70 2019-02-05 stsp repo_path =
4148 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4149 52185f70 2019-02-05 stsp got_worktree_close(worktree);
4151 52185f70 2019-02-05 stsp repo_path = cwd;
4152 52185f70 2019-02-05 stsp if (repo_path == NULL) {
4153 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4154 52185f70 2019-02-05 stsp goto done;
4156 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
4157 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
4158 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
4159 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
4161 ffd1d5e5 2018-06-23 stsp usage_log();
4163 a915003a 2019-02-05 stsp init_curses();
4165 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
4166 c02c541e 2019-03-29 stsp if (error != NULL)
4167 52185f70 2019-02-05 stsp goto done;
4169 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4170 c02c541e 2019-03-29 stsp if (error)
4171 52185f70 2019-02-05 stsp goto done;
4173 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
4174 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4176 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
4177 15a94983 2018-12-23 stsp commit_id_arg);
4178 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4179 ffd1d5e5 2018-06-23 stsp goto done;
4181 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4182 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4183 ffd1d5e5 2018-06-23 stsp goto done;
4185 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
4186 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
4187 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4188 8b473291 2019-02-21 stsp goto done;
4190 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
4191 8b473291 2019-02-21 stsp if (error)
4192 ffd1d5e5 2018-06-23 stsp goto done;
4194 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4195 5221c383 2018-08-01 stsp if (view == NULL) {
4196 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4197 5221c383 2018-08-01 stsp goto done;
4199 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
4200 ad80ab7b 2018-08-04 stsp if (error)
4201 ad80ab7b 2018-08-04 stsp goto done;
4202 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4204 52185f70 2019-02-05 stsp free(repo_path);
4205 ffd1d5e5 2018-06-23 stsp free(commit_id);
4206 ffd1d5e5 2018-06-23 stsp if (commit)
4207 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
4209 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
4211 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
4212 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4213 ffd1d5e5 2018-06-23 stsp return error;
4216 4ed7e80c 2018-05-20 stsp __dead static void
4217 9f7d7167 2018-04-29 stsp usage(void)
4221 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
4222 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
4223 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
4224 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
4225 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
4230 c2301be8 2018-04-30 stsp static char **
4231 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
4233 c2301be8 2018-04-30 stsp char **argv;
4234 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
4236 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
4237 c2301be8 2018-04-30 stsp if (argv == NULL)
4238 c2301be8 2018-04-30 stsp err(1, "calloc");
4239 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
4240 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
4241 c2301be8 2018-04-30 stsp err(1, "calloc");
4242 c2301be8 2018-04-30 stsp if (arg1) {
4243 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
4244 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
4245 c2301be8 2018-04-30 stsp err(1, "calloc");
4248 c2301be8 2018-04-30 stsp return argv;
4252 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
4254 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
4255 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
4256 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
4257 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
4259 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
4261 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
4262 9f7d7167 2018-04-29 stsp switch (ch) {
4264 9f7d7167 2018-04-29 stsp hflag = 1;
4268 9f7d7167 2018-04-29 stsp /* NOTREACHED */
4272 9f7d7167 2018-04-29 stsp argc -= optind;
4273 9f7d7167 2018-04-29 stsp argv += optind;
4274 9f7d7167 2018-04-29 stsp optind = 0;
4275 c2301be8 2018-04-30 stsp optreset = 1;
4277 c2301be8 2018-04-30 stsp if (argc == 0) {
4278 f29d3e89 2018-06-23 stsp if (hflag)
4280 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
4281 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
4282 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
4287 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
4288 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
4289 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
4290 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
4291 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
4292 9f7d7167 2018-04-29 stsp if (hflag)
4293 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
4297 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
4298 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
4299 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
4300 c2301be8 2018-04-30 stsp if (repo_path) {
4301 c2301be8 2018-04-30 stsp struct got_repository *repo;
4302 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
4303 c2301be8 2018-04-30 stsp if (error == NULL)
4304 c2301be8 2018-04-30 stsp got_repo_close(repo);
4306 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath",
4308 c2301be8 2018-04-30 stsp if (error) {
4309 f29d3e89 2018-06-23 stsp if (hflag) {
4310 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
4311 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
4315 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
4316 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
4317 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
4318 ad7de8d9 2018-04-30 stsp free(repo_path);
4321 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
4322 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
4324 c2301be8 2018-04-30 stsp free(repo_path);
4328 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4329 9f7d7167 2018-04-29 stsp if (error)
4330 9f7d7167 2018-04-29 stsp goto done;
4333 c2301be8 2018-04-30 stsp free(cmd_argv);
4334 9f7d7167 2018-04-29 stsp if (error)
4335 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);