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 return NULL;
552 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
553 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
554 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
555 1a76625f 2018-10-22 stsp if (errcode)
556 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
557 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
558 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
559 1a76625f 2018-10-22 stsp if (errcode)
560 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
561 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
563 25791caa 2018-10-24 stsp if (tog_sigwinch_received) {
564 25791caa 2018-10-24 stsp tog_resizeterm();
565 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
566 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
567 25791caa 2018-10-24 stsp err = view_resize(v);
569 25791caa 2018-10-24 stsp return err;
570 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
572 25791caa 2018-10-24 stsp return err;
576 e5a0f69f 2018-08-18 stsp switch (ch) {
580 1e37a5c2 2019-05-12 jcs if (view->child) {
581 1e37a5c2 2019-05-12 jcs *focus = view->child;
582 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
583 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
584 1e37a5c2 2019-05-12 jcs *focus = view->parent;
585 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
589 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
590 1e37a5c2 2019-05-12 jcs *dead = view;
596 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
597 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
599 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
600 669b5ffa 2018-10-07 stsp *focus = view->child;
601 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
602 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
604 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
607 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
608 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
610 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
611 1e37a5c2 2019-05-12 jcs *focus = view;
612 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
613 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
615 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
619 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
620 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
623 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
626 60493ae3 2019-06-20 stsp if (view->search_start)
627 60493ae3 2019-06-20 stsp view->search_start(view);
629 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
633 60493ae3 2019-06-20 stsp if (view->search_next && view->searching) {
634 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
635 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
636 60493ae3 2019-06-20 stsp view->search_next_done = 0;
637 60493ae3 2019-06-20 stsp view->search_next(view);
639 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
642 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
646 e5a0f69f 2018-08-18 stsp return err;
650 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
652 0cf4efb1 2018-09-29 stsp PANEL *panel;
653 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
655 669b5ffa 2018-10-07 stsp if (view->parent)
656 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
658 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
659 0cf4efb1 2018-09-29 stsp if (panel == NULL)
662 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
663 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
664 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
668 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
670 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
671 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
673 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
675 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
678 669b5ffa 2018-10-07 stsp return view->focussed;
681 bcbd79e2 2018-08-19 stsp static const struct got_error *
682 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
684 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
685 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
686 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
687 fd823528 2018-10-22 stsp int fast_refresh = 10;
688 1a76625f 2018-10-22 stsp int done = 0, errcode;
690 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
691 1a76625f 2018-10-22 stsp if (errcode)
692 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
694 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
695 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
697 a81bf10d 2018-09-29 stsp main_view = view;
698 1004088d 2018-09-29 stsp view->focussed = 1;
699 878940b7 2018-09-29 stsp err = view->show(view);
701 0cf4efb1 2018-09-29 stsp return err;
702 0cf4efb1 2018-09-29 stsp update_panels();
703 0cf4efb1 2018-09-29 stsp doupdate();
704 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
705 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
706 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
707 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
709 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
710 e4197bf9 2018-08-18 stsp view, &views);
713 e5a0f69f 2018-08-18 stsp if (dead_view) {
714 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
716 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
717 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
718 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
719 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
720 669b5ffa 2018-10-07 stsp prev = view->parent;
722 669b5ffa 2018-10-07 stsp if (dead_view->parent)
723 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
725 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
727 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
728 a81bf10d 2018-09-29 stsp if (err || dead_view == main_view)
731 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
732 0cf4efb1 2018-09-29 stsp if (focus_view)
733 0cf4efb1 2018-09-29 stsp view = focus_view;
734 669b5ffa 2018-10-07 stsp else if (prev)
735 669b5ffa 2018-10-07 stsp view = prev;
736 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
737 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
738 0cf4efb1 2018-09-29 stsp tog_view_list_head);
740 0cf4efb1 2018-09-29 stsp view = NULL;
741 669b5ffa 2018-10-07 stsp if (view) {
742 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
743 669b5ffa 2018-10-07 stsp focus_view = view->child;
745 669b5ffa 2018-10-07 stsp focus_view = view;
749 bcbd79e2 2018-08-19 stsp if (new_view) {
750 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
751 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
752 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
753 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
755 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
756 86c66b02 2018-10-18 stsp err = view_close(v);
761 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
762 fed7eaa8 2018-10-24 stsp view = new_view;
763 878940b7 2018-09-29 stsp if (focus_view == NULL)
764 878940b7 2018-09-29 stsp focus_view = new_view;
766 0cf4efb1 2018-09-29 stsp if (focus_view) {
767 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
769 0cf4efb1 2018-09-29 stsp view->focussed = 0;
770 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
771 0cf4efb1 2018-09-29 stsp view = focus_view;
772 878940b7 2018-09-29 stsp if (new_view)
773 878940b7 2018-09-29 stsp show_panel(new_view->panel);
774 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
775 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
777 669b5ffa 2018-10-07 stsp if (view) {
778 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
779 758194b5 2018-10-24 stsp view->focussed = 1;
780 758194b5 2018-10-24 stsp show_panel(view->panel);
781 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
782 758194b5 2018-10-24 stsp show_panel(view->child->panel);
783 1a76625f 2018-10-22 stsp focus_view = view;
785 669b5ffa 2018-10-07 stsp if (view->parent) {
786 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
790 669b5ffa 2018-10-07 stsp err = view->show(view);
793 669b5ffa 2018-10-07 stsp if (view->child) {
794 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
798 1a76625f 2018-10-22 stsp update_panels();
799 1a76625f 2018-10-22 stsp doupdate();
803 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
804 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
805 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
806 e5a0f69f 2018-08-18 stsp view_close(view);
809 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
810 1a76625f 2018-10-22 stsp if (errcode)
811 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
813 e5a0f69f 2018-08-18 stsp return err;
816 4ed7e80c 2018-05-20 stsp __dead static void
817 9f7d7167 2018-04-29 stsp usage_log(void)
820 c70c5802 2018-08-01 stsp fprintf(stderr,
821 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
822 9f7d7167 2018-04-29 stsp getprogname());
826 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
827 80ddbec8 2018-04-29 stsp static const struct got_error *
828 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
830 00dfcb92 2018-06-11 stsp char *vis = NULL;
831 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
833 963b370f 2018-05-20 stsp *ws = NULL;
834 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
835 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
836 00dfcb92 2018-06-11 stsp int vislen;
837 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
838 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
840 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
841 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
843 00dfcb92 2018-06-11 stsp return err;
844 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
845 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
846 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
851 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
852 a7f50699 2018-06-11 stsp if (*ws == NULL) {
853 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
857 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
858 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
863 963b370f 2018-05-20 stsp *ws = NULL;
866 963b370f 2018-05-20 stsp return err;
869 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
870 963b370f 2018-05-20 stsp static const struct got_error *
871 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
873 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
874 963b370f 2018-05-20 stsp int cols = 0;
875 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
876 963b370f 2018-05-20 stsp size_t wlen;
879 963b370f 2018-05-20 stsp *wlinep = NULL;
880 b700b5d6 2018-07-10 stsp *widthp = 0;
882 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
884 963b370f 2018-05-20 stsp return err;
887 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
888 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
889 963b370f 2018-05-20 stsp switch (width) {
895 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
896 b700b5d6 2018-07-10 stsp cols += width;
900 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
901 67ceb59d 2019-03-03 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
905 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
909 963b370f 2018-05-20 stsp wline[i] = L'\0';
910 b700b5d6 2018-07-10 stsp if (widthp)
911 b700b5d6 2018-07-10 stsp *widthp = cols;
914 963b370f 2018-05-20 stsp free(wline);
916 963b370f 2018-05-20 stsp *wlinep = wline;
917 963b370f 2018-05-20 stsp return err;
920 8b473291 2019-02-21 stsp static const struct got_error*
921 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
922 8b473291 2019-02-21 stsp struct got_object_id *id)
924 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
925 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
927 8b473291 2019-02-21 stsp const char *name;
929 8b473291 2019-02-21 stsp *refs_str = NULL;
931 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
932 8b473291 2019-02-21 stsp if (got_object_id_cmp(re->id, id) != 0)
934 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
935 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
937 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
939 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
941 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
943 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
945 8b473291 2019-02-21 stsp s = *refs_str;
946 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
947 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
948 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
950 8b473291 2019-02-21 stsp *refs_str = NULL;
956 8b473291 2019-02-21 stsp return err;
959 963b370f 2018-05-20 stsp static const struct got_error *
960 5813d178 2019-03-09 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
962 5813d178 2019-03-09 stsp char *smallerthan, *at;
964 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
965 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
966 5813d178 2019-03-09 stsp author = smallerthan + 1;
967 5813d178 2019-03-09 stsp at = strchr(author, '@');
969 5813d178 2019-03-09 stsp *at = '\0';
970 5813d178 2019-03-09 stsp return format_line(wauthor, author_width, author, limit);
973 5813d178 2019-03-09 stsp static const struct got_error *
974 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
975 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
976 5813d178 2019-03-09 stsp int author_display_cols)
978 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
979 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
980 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
981 5813d178 2019-03-09 stsp char *author = NULL;
982 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
983 bb737323 2018-05-20 stsp int author_width, logmsg_width;
984 5813d178 2019-03-09 stsp char *newline, *line = NULL;
985 bb737323 2018-05-20 stsp int col, limit;
986 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
987 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
988 ccb26ccd 2018-11-05 stsp struct tm tm;
989 45d799e2 2018-12-23 stsp time_t committer_time;
991 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
992 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
993 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
994 ccb26ccd 2018-11-05 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
995 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
996 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
998 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
999 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1001 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1002 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1003 b39d25c7 2018-07-10 stsp col = limit + 1;
1004 b39d25c7 2018-07-10 stsp if (col > avail)
1005 b39d25c7 2018-07-10 stsp goto done;
1007 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1008 5813d178 2019-03-09 stsp if (author == NULL) {
1009 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1010 80ddbec8 2018-04-29 stsp goto done;
1012 5813d178 2019-03-09 stsp err = format_author(&wauthor, &author_width, author, avail - col);
1014 bb737323 2018-05-20 stsp goto done;
1015 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1016 bb737323 2018-05-20 stsp col += author_width;
1017 5813d178 2019-03-09 stsp while (col <= avail && author_width < author_display_cols + 2) {
1018 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1020 bb737323 2018-05-20 stsp author_width++;
1022 9c2eaf34 2018-05-20 stsp if (col > avail)
1023 9c2eaf34 2018-05-20 stsp goto done;
1025 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1026 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
1027 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1028 6d9fbc00 2018-04-29 stsp goto done;
1030 bb737323 2018-05-20 stsp logmsg = logmsg0;
1031 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1033 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1034 bb737323 2018-05-20 stsp if (newline)
1035 bb737323 2018-05-20 stsp *newline = '\0';
1036 bb737323 2018-05-20 stsp limit = avail - col;
1037 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1039 bb737323 2018-05-20 stsp goto done;
1040 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1041 bb737323 2018-05-20 stsp col += logmsg_width;
1042 bb737323 2018-05-20 stsp while (col <= avail) {
1043 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1047 80ddbec8 2018-04-29 stsp free(logmsg0);
1048 bb737323 2018-05-20 stsp free(wlogmsg);
1049 5813d178 2019-03-09 stsp free(author);
1050 bb737323 2018-05-20 stsp free(wauthor);
1051 80ddbec8 2018-04-29 stsp free(line);
1052 80ddbec8 2018-04-29 stsp return err;
1055 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1056 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1057 899d86c2 2018-05-10 stsp struct got_object_id *id)
1059 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1061 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1062 80ddbec8 2018-04-29 stsp if (entry == NULL)
1063 899d86c2 2018-05-10 stsp return NULL;
1065 899d86c2 2018-05-10 stsp entry->id = id;
1066 99db9666 2018-05-07 stsp entry->commit = commit;
1067 899d86c2 2018-05-10 stsp return entry;
1070 99db9666 2018-05-07 stsp static void
1071 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1073 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1075 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1076 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1077 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1078 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1079 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1080 99db9666 2018-05-07 stsp free(entry);
1083 99db9666 2018-05-07 stsp static void
1084 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1086 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1087 99db9666 2018-05-07 stsp pop_commit(commits);
1090 c4972b91 2018-05-07 stsp static const struct got_error *
1091 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1092 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
1094 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1095 93e45b7c 2018-09-24 stsp int nqueued = 0;
1098 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1099 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1100 1a76625f 2018-10-22 stsp * while updating the display.
1102 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
1103 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1104 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1105 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1106 1a76625f 2018-10-22 stsp int errcode;
1108 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1109 9ba79e04 2018-06-11 stsp if (err) {
1110 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1112 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
1113 93e45b7c 2018-09-24 stsp minqueue, repo);
1115 ecb28ae0 2018-07-16 stsp return err;
1119 ecb28ae0 2018-07-16 stsp if (id == NULL)
1122 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1125 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1126 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1127 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1131 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1132 1a76625f 2018-10-22 stsp if (errcode) {
1133 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_lock");
1137 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1138 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1139 ecb28ae0 2018-07-16 stsp nqueued++;
1140 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1142 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1143 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1144 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1145 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1148 9ba79e04 2018-06-11 stsp return err;
1151 99db9666 2018-05-07 stsp static const struct got_error *
1152 19e70ad6 2019-05-14 stsp get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1153 19e70ad6 2019-05-14 stsp struct got_repository *repo)
1155 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1156 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1158 9ba79e04 2018-06-11 stsp *head_id = NULL;
1160 19e70ad6 2019-05-14 stsp err = got_ref_open(&head_ref, repo, branch_name, 0);
1162 99db9666 2018-05-07 stsp return err;
1164 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1165 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1166 9ba79e04 2018-06-11 stsp if (err) {
1167 9ba79e04 2018-06-11 stsp *head_id = NULL;
1168 99db9666 2018-05-07 stsp return err;
1171 9ba79e04 2018-06-11 stsp return NULL;
1174 0553a4e3 2018-04-30 stsp static const struct got_error *
1175 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1176 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1177 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1178 8b473291 2019-02-21 stsp struct got_reflist_head *refs, const char *path, int commits_needed)
1180 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1181 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1182 60493ae3 2019-06-20 stsp int width;
1183 60493ae3 2019-06-20 stsp int ncommits, author_cols = 10;
1184 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1185 8b473291 2019-02-21 stsp char *refs_str = NULL;
1186 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1188 e0d42f60 2018-07-22 stsp entry = first;
1189 e0d42f60 2018-07-22 stsp ncommits = 0;
1190 e0d42f60 2018-07-22 stsp while (entry) {
1191 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1192 e0d42f60 2018-07-22 stsp *selected = entry;
1195 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1196 e0d42f60 2018-07-22 stsp ncommits++;
1199 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1200 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1202 ecb28ae0 2018-07-16 stsp return err;
1203 8b473291 2019-02-21 stsp if (refs) {
1204 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, (*selected)->id);
1206 8b473291 2019-02-21 stsp goto done;
1210 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1211 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1213 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1214 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1215 60493ae3 2019-06-20 stsp commits_needed > 0 ?
1216 60493ae3 2019-06-20 stsp (view->searching && view->search_next_done == 0
1217 60493ae3 2019-06-20 stsp ? "searching..." : "loading... ") :
1218 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1219 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1220 8b473291 2019-02-21 stsp goto done;
1223 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1224 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1225 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1226 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1227 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1228 1a76625f 2018-10-22 stsp header = NULL;
1229 1a76625f 2018-10-22 stsp goto done;
1231 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1232 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1233 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1234 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1235 1a76625f 2018-10-22 stsp header = NULL;
1236 1a76625f 2018-10-22 stsp goto done;
1238 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1240 1a76625f 2018-10-22 stsp goto done;
1242 2814baeb 2018-08-01 stsp werase(view->window);
1244 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1245 a3404814 2018-09-02 stsp wstandout(view->window);
1246 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1247 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1248 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1251 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1252 a3404814 2018-09-02 stsp wstandend(view->window);
1253 ecb28ae0 2018-07-16 stsp free(wline);
1254 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1255 1a76625f 2018-10-22 stsp goto done;
1257 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1258 899d86c2 2018-05-10 stsp entry = first;
1259 5813d178 2019-03-09 stsp ncommits = 0;
1260 5813d178 2019-03-09 stsp while (entry) {
1261 5813d178 2019-03-09 stsp char *author;
1262 5813d178 2019-03-09 stsp wchar_t *wauthor;
1263 5813d178 2019-03-09 stsp int width;
1264 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1266 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1267 5813d178 2019-03-09 stsp if (author == NULL) {
1268 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1269 5813d178 2019-03-09 stsp goto done;
1271 5813d178 2019-03-09 stsp err = format_author(&wauthor, &width, author, COLS);
1272 5813d178 2019-03-09 stsp if (author_cols < width)
1273 5813d178 2019-03-09 stsp author_cols = width;
1274 5813d178 2019-03-09 stsp free(wauthor);
1275 5813d178 2019-03-09 stsp free(author);
1276 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1279 5813d178 2019-03-09 stsp entry = first;
1280 899d86c2 2018-05-10 stsp *last = first;
1281 867c6645 2018-07-10 stsp ncommits = 0;
1282 899d86c2 2018-05-10 stsp while (entry) {
1283 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1285 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1286 2814baeb 2018-08-01 stsp wstandout(view->window);
1287 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1288 5813d178 2019-03-09 stsp author_cols);
1289 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1290 2814baeb 2018-08-01 stsp wstandend(view->window);
1292 60493ae3 2019-06-20 stsp goto done;
1293 0553a4e3 2018-04-30 stsp ncommits++;
1294 899d86c2 2018-05-10 stsp *last = entry;
1295 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1298 1a57306a 2018-09-02 stsp view_vborder(view);
1300 1a76625f 2018-10-22 stsp free(id_str);
1301 8b473291 2019-02-21 stsp free(refs_str);
1302 1a76625f 2018-10-22 stsp free(ncommits_str);
1303 1a76625f 2018-10-22 stsp free(header);
1304 80ddbec8 2018-04-29 stsp return err;
1307 07b55e75 2018-05-10 stsp static void
1308 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1309 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1310 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1312 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1313 07b55e75 2018-05-10 stsp int nscrolled = 0;
1315 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1316 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1319 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1320 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1321 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1322 07b55e75 2018-05-10 stsp if (entry) {
1323 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1324 07b55e75 2018-05-10 stsp nscrolled++;
1329 aa075928 2018-05-10 stsp static const struct got_error *
1330 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1331 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1333 5e224a3e 2019-02-22 stsp int errcode;
1334 8a42fca8 2019-02-22 stsp int max_wait = 20;
1336 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1338 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1339 5e224a3e 2019-02-22 stsp if (*log_complete)
1342 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1343 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1344 7aafa0d1 2019-02-22 stsp if (errcode)
1345 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1346 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1347 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1348 7aafa0d1 2019-02-22 stsp if (errcode)
1349 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1350 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1351 7aafa0d1 2019-02-22 stsp pthread_yield();
1352 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1353 7aafa0d1 2019-02-22 stsp if (errcode)
1354 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1355 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1357 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1359 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1360 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1361 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1362 5e224a3e 2019-02-22 stsp * with few commits in history.
1364 7aafa0d1 2019-02-22 stsp return NULL;
1368 5e224a3e 2019-02-22 stsp return NULL;
1371 5e224a3e 2019-02-22 stsp static const struct got_error *
1372 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1373 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1374 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1375 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1376 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1378 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1379 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1380 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1382 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1383 5e224a3e 2019-02-22 stsp return NULL;
1385 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1386 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1388 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1389 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1391 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1392 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1393 5e224a3e 2019-02-22 stsp need_commits);
1395 5e224a3e 2019-02-22 stsp return err;
1399 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1400 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1403 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1405 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1406 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1408 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1409 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1411 dd0a52c1 2018-05-20 stsp return err;
1414 cd0acaa7 2018-05-20 stsp static const struct got_error *
1415 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1416 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1417 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1418 8b473291 2019-02-21 stsp struct got_repository *repo)
1420 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1421 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1422 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1424 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1425 15a94983 2018-12-23 stsp if (diff_view == NULL)
1426 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1428 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1429 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1430 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1431 e5a0f69f 2018-08-18 stsp if (err == NULL)
1432 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1433 cd0acaa7 2018-05-20 stsp return err;
1436 80ddbec8 2018-04-29 stsp static const struct got_error *
1437 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1438 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1440 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1442 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1443 941e9f74 2019-05-21 stsp if (parent == NULL)
1444 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1446 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1447 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1448 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1449 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1450 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1451 941e9f74 2019-05-21 stsp s->tree = subtree;
1452 941e9f74 2019-05-21 stsp s->entries = got_object_tree_get_entries(s->tree);
1453 941e9f74 2019-05-21 stsp s->selected = 0;
1454 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1455 941e9f74 2019-05-21 stsp return NULL;
1459 941e9f74 2019-05-21 stsp static const struct got_error *
1460 941e9f74 2019-05-21 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1461 941e9f74 2019-05-21 stsp struct commit_queue_entry *entry, const char *path,
1462 941e9f74 2019-05-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
1464 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1465 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1466 941e9f74 2019-05-21 stsp struct got_tree_entry *te;
1467 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s;
1468 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1469 b03c880f 2019-05-21 stsp char *slash, *subpath = NULL;
1470 941e9f74 2019-05-21 stsp const char *p;
1472 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1473 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1475 9343a5fb 2018-06-23 stsp return err;
1477 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1478 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1479 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1481 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1482 941e9f74 2019-05-21 stsp if (err) {
1483 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1484 941e9f74 2019-05-21 stsp return err;
1486 941e9f74 2019-05-21 stsp s = &tree_view->state.tree;
1488 941e9f74 2019-05-21 stsp *new_view = tree_view;
1490 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1492 941e9f74 2019-05-21 stsp while (*p) {
1493 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1495 941e9f74 2019-05-21 stsp while (p[0] == '/')
1498 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1499 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1500 941e9f74 2019-05-21 stsp if (slash == NULL)
1501 941e9f74 2019-05-21 stsp slash = strchr(p, '\0');
1502 941e9f74 2019-05-21 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1503 941e9f74 2019-05-21 stsp if (strncmp(p, te->name, slash - p) == 0) {
1504 941e9f74 2019-05-21 stsp s->selected_entry = te;
1507 b03c880f 2019-05-21 stsp s->selected++;
1509 941e9f74 2019-05-21 stsp if (s->selected_entry == NULL) {
1510 941e9f74 2019-05-21 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1513 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1514 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1516 67409a31 2019-05-24 stsp if (!S_ISDIR(s->selected_entry->mode)) {
1517 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1518 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1519 67409a31 2019-05-24 stsp s->selected = 0;
1523 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1524 941e9f74 2019-05-21 stsp if (slash)
1525 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1527 941e9f74 2019-05-21 stsp subpath = strdup(path);
1528 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1529 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1533 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1538 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1539 941e9f74 2019-05-21 stsp free(tree_id);
1543 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1544 941e9f74 2019-05-21 stsp if (err) {
1545 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1548 941e9f74 2019-05-21 stsp if (slash == NULL)
1550 941e9f74 2019-05-21 stsp free(subpath);
1551 941e9f74 2019-05-21 stsp subpath = NULL;
1552 941e9f74 2019-05-21 stsp p = slash;
1555 941e9f74 2019-05-21 stsp free(subpath);
1556 1a76625f 2018-10-22 stsp return err;
1559 1a76625f 2018-10-22 stsp static void *
1560 1a76625f 2018-10-22 stsp log_thread(void *arg)
1562 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1563 1a76625f 2018-10-22 stsp int errcode = 0;
1564 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1565 1a76625f 2018-10-22 stsp int done = 0;
1567 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1569 1a76625f 2018-10-22 stsp return (void *)err;
1571 1a76625f 2018-10-22 stsp while (!done && !err) {
1572 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1573 1a76625f 2018-10-22 stsp a->in_repo_path);
1574 1a76625f 2018-10-22 stsp if (err) {
1575 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1576 1a76625f 2018-10-22 stsp return (void *)err;
1577 1a76625f 2018-10-22 stsp err = NULL;
1579 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1580 1a76625f 2018-10-22 stsp a->commits_needed--;
1582 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1583 3abe8080 2019-04-10 stsp if (errcode) {
1584 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1585 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1587 3abe8080 2019-04-10 stsp } else if (*a->quit)
1589 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1590 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1591 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1592 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1596 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1597 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1598 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1599 1a76625f 2018-10-22 stsp &tog_mutex);
1600 1a76625f 2018-10-22 stsp if (errcode)
1601 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1602 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1605 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1606 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1607 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1608 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1610 3abe8080 2019-04-10 stsp a->log_complete = 1;
1611 1a76625f 2018-10-22 stsp return (void *)err;
1614 1a76625f 2018-10-22 stsp static const struct got_error *
1615 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1617 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1618 1a76625f 2018-10-22 stsp int errcode;
1620 1a76625f 2018-10-22 stsp if (s->thread) {
1621 1a76625f 2018-10-22 stsp s->quit = 1;
1622 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1623 1a76625f 2018-10-22 stsp if (errcode)
1624 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1625 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1626 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1627 1a76625f 2018-10-22 stsp if (errcode)
1628 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1629 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1630 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1631 1a76625f 2018-10-22 stsp if (errcode)
1632 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1633 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1634 1a76625f 2018-10-22 stsp if (errcode)
1635 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1636 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1637 1a76625f 2018-10-22 stsp s->thread = NULL;
1640 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1641 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1642 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1644 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1645 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1646 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1649 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1650 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1651 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1654 9343a5fb 2018-06-23 stsp return err;
1657 9343a5fb 2018-06-23 stsp static const struct got_error *
1658 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1660 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1661 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1663 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1664 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1665 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1666 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1667 1a76625f 2018-10-22 stsp free(s->start_id);
1668 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1669 1a76625f 2018-10-22 stsp return err;
1672 1a76625f 2018-10-22 stsp static const struct got_error *
1673 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
1675 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1676 60493ae3 2019-06-20 stsp char pattern[1024];
1679 60493ae3 2019-06-20 stsp if (view->nlines < 1)
1680 60493ae3 2019-06-20 stsp return NULL;
1682 60493ae3 2019-06-20 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1,
1683 60493ae3 2019-06-20 stsp view->begin_x, "/");
1684 60493ae3 2019-06-20 stsp wclrtoeol(view->window);
1686 60493ae3 2019-06-20 stsp nocbreak();
1688 60493ae3 2019-06-20 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
1691 60493ae3 2019-06-20 stsp if (ret == ERR)
1692 60493ae3 2019-06-20 stsp return NULL;
1694 60493ae3 2019-06-20 stsp if (view->searching) {
1695 60493ae3 2019-06-20 stsp regfree(&s->regex);
1696 60493ae3 2019-06-20 stsp view->searching = 0;
1699 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
1700 f2d4ba2a 2019-06-21 stsp if (regcomp(&s->regex, pattern,
1701 f2d4ba2a 2019-06-21 stsp REG_EXTENDED | REG_ICASE | REG_NOSUB | REG_NEWLINE) == 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 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
1742 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
1743 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
1745 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1746 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
1748 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1751 60493ae3 2019-06-20 stsp while (1) {
1752 60493ae3 2019-06-20 stsp if (entry == NULL) {
1753 60493ae3 2019-06-20 stsp if (s->thread_args.log_complete) {
1754 60493ae3 2019-06-20 stsp if (s->matched_entry == NULL) {
1755 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1756 60493ae3 2019-06-20 stsp return NULL;
1758 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1759 60493ae3 2019-06-20 stsp entry = TAILQ_FIRST(&s->commits.head);
1761 b1bf1435 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head,
1762 b1bf1435 2019-06-21 stsp commit_queue_head);
1764 60493ae3 2019-06-20 stsp s->thread_args.commits_needed = 1;
1765 60493ae3 2019-06-20 stsp return trigger_log_thread(0,
1766 60493ae3 2019-06-20 stsp &s->thread_args.commits_needed,
1767 60493ae3 2019-06-20 stsp &s->thread_args.log_complete,
1768 60493ae3 2019-06-20 stsp &s->thread_args.need_commits);
1772 60493ae3 2019-06-20 stsp if (match_commit(entry->commit, &s->regex)) {
1773 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1776 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1777 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
1779 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1782 60493ae3 2019-06-20 stsp if (entry) {
1783 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
1784 60493ae3 2019-06-20 stsp s->matched_entry = entry;
1785 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
1786 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1788 60493ae3 2019-06-20 stsp return err;
1791 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
1792 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1794 60493ae3 2019-06-20 stsp return err;
1799 60493ae3 2019-06-20 stsp return NULL;
1803 60493ae3 2019-06-20 stsp static const struct got_error *
1804 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1805 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
1806 8b473291 2019-02-21 stsp const char *path, int check_disk)
1808 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1809 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1810 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1811 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1812 1a76625f 2018-10-22 stsp int errcode;
1814 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1815 ecb28ae0 2018-07-16 stsp if (err != NULL)
1816 ecb28ae0 2018-07-16 stsp goto done;
1818 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1819 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1820 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1822 8b473291 2019-02-21 stsp s->refs = refs;
1823 fb2756b9 2018-08-04 stsp s->repo = repo;
1824 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1825 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1826 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1827 5036bf37 2018-09-24 stsp goto done;
1830 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1831 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1832 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1833 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
1834 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
1836 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1838 1a76625f 2018-10-22 stsp goto done;
1839 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1840 1a76625f 2018-10-22 stsp 0, thread_repo);
1842 1a76625f 2018-10-22 stsp goto done;
1844 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1845 1a76625f 2018-10-22 stsp if (errcode) {
1846 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
1847 1a76625f 2018-10-22 stsp goto done;
1850 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1851 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1852 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1853 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1854 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1855 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1856 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1857 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1858 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1859 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1860 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1863 1a76625f 2018-10-22 stsp close_log_view(view);
1864 ba4f502b 2018-08-04 stsp return err;
1867 e5a0f69f 2018-08-18 stsp static const struct got_error *
1868 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1870 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1872 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1873 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1874 2b380cc8 2018-10-24 stsp &s->thread_args);
1875 2b380cc8 2018-10-24 stsp if (errcode)
1876 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
1879 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1880 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1881 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
1882 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1885 e5a0f69f 2018-08-18 stsp static const struct got_error *
1886 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1887 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1889 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1890 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1891 5036bf37 2018-09-24 stsp char *parent_path;
1892 669b5ffa 2018-10-07 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
1893 669b5ffa 2018-10-07 stsp int begin_x = 0;
1895 e5a0f69f 2018-08-18 stsp switch (ch) {
1897 1e37a5c2 2019-05-12 jcs s->quit = 1;
1900 1e37a5c2 2019-05-12 jcs case KEY_UP:
1903 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1905 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
1906 1e37a5c2 2019-05-12 jcs s->selected--;
1908 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
1909 1144d21a 2019-06-21 stsp &s->commits);
1911 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
1912 a4292ac5 2019-05-12 jcs case CTRL('b'):
1913 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1915 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
1916 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
1917 1e37a5c2 2019-05-12 jcs s->selected = 0;
1920 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
1921 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
1924 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
1927 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1929 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
1930 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1931 1e37a5c2 2019-05-12 jcs s->selected++;
1934 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
1935 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
1936 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
1937 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1938 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1940 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
1941 a4292ac5 2019-05-12 jcs case CTRL('f'): {
1942 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
1943 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
1944 1e37a5c2 2019-05-12 jcs if (first == NULL)
1946 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
1947 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
1948 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
1949 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1950 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1951 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
1952 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
1953 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1954 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
1955 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
1956 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
1958 1e37a5c2 2019-05-12 jcs err = NULL;
1961 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1962 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
1963 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
1964 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
1965 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
1967 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
1970 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
1972 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
1973 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
1974 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
1975 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
1976 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
1979 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1980 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
1982 1e37a5c2 2019-05-12 jcs return err;
1983 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
1985 1e37a5c2 2019-05-12 jcs view_close(diff_view);
1988 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
1989 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
1991 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
1994 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
1996 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
1997 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
1998 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
1999 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2002 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2003 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2005 1e37a5c2 2019-05-12 jcs return err;
2006 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2008 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2011 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2012 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2014 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2016 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2017 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2019 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2020 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2021 1e37a5c2 2019-05-12 jcs struct tog_view *lv;
2022 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2024 1e37a5c2 2019-05-12 jcs return err;
2025 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2026 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2027 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2028 638f9024 2019-05-13 stsp return got_error_from_errno(
2029 1e37a5c2 2019-05-12 jcs "view_open");
2030 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2031 1e37a5c2 2019-05-12 jcs s->repo, parent_path, 0);
2033 1e37a5c2 2019-05-12 jcs return err;;
2034 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2035 1e37a5c2 2019-05-12 jcs *new_view = lv;
2037 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2038 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2040 1e37a5c2 2019-05-12 jcs return NULL;
2047 80ddbec8 2018-04-29 stsp return err;
2050 4ed7e80c 2018-05-20 stsp static const struct got_error *
2051 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2053 c2db6724 2019-01-04 stsp const struct got_error *error;
2055 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2056 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2058 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2059 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2061 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2062 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2064 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2065 c2db6724 2019-01-04 stsp if (error != NULL)
2066 c2db6724 2019-01-04 stsp return error;
2068 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2069 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2071 c2db6724 2019-01-04 stsp return NULL;
2074 a915003a 2019-02-05 stsp static void
2075 a915003a 2019-02-05 stsp init_curses(void)
2077 a915003a 2019-02-05 stsp initscr();
2079 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2082 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2083 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2084 a915003a 2019-02-05 stsp curs_set(0);
2085 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2088 c2db6724 2019-01-04 stsp static const struct got_error *
2089 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2091 80ddbec8 2018-04-29 stsp const struct got_error *error;
2092 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2093 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2094 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2095 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2096 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2097 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
2099 04cc582a 2018-08-01 stsp struct tog_view *view;
2101 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2103 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2104 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2105 c2db6724 2019-01-04 stsp NULL) == -1)
2106 80ddbec8 2018-04-29 stsp err(1, "pledge");
2109 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2110 80ddbec8 2018-04-29 stsp switch (ch) {
2112 80ddbec8 2018-04-29 stsp start_commit = optarg;
2115 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2116 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2117 ecb28ae0 2018-07-16 stsp err(1, "-r option");
2120 17020d27 2019-03-07 stsp usage_log();
2121 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2125 80ddbec8 2018-04-29 stsp argc -= optind;
2126 80ddbec8 2018-04-29 stsp argv += optind;
2128 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2129 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2130 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2131 ecb28ae0 2018-07-16 stsp goto done;
2133 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2134 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2135 963f97a1 2019-03-18 stsp goto done;
2136 963f97a1 2019-03-18 stsp error = NULL;
2138 963f97a1 2019-03-18 stsp if (argc == 0) {
2139 963f97a1 2019-03-18 stsp path = strdup("");
2140 963f97a1 2019-03-18 stsp if (path == NULL) {
2141 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2142 ecb28ae0 2018-07-16 stsp goto done;
2144 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2145 963f97a1 2019-03-18 stsp if (worktree) {
2146 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2148 963f97a1 2019-03-18 stsp if (error)
2149 963f97a1 2019-03-18 stsp goto done;
2151 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2152 963f97a1 2019-03-18 stsp if (path == NULL) {
2153 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2154 963f97a1 2019-03-18 stsp goto done;
2158 963f97a1 2019-03-18 stsp usage_log();
2160 963f97a1 2019-03-18 stsp repo_path = worktree ?
2161 963f97a1 2019-03-18 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2162 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2163 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2164 963f97a1 2019-03-18 stsp goto done;
2167 a915003a 2019-02-05 stsp init_curses();
2169 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
2170 80ddbec8 2018-04-29 stsp if (error != NULL)
2171 ecb28ae0 2018-07-16 stsp goto done;
2173 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2174 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2175 c02c541e 2019-03-29 stsp if (error)
2176 c02c541e 2019-03-29 stsp goto done;
2178 15a94983 2018-12-23 stsp if (start_commit == NULL)
2179 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&start_id, worktree ?
2180 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2183 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&start_id, repo,
2184 15a94983 2018-12-23 stsp start_commit);
2185 80ddbec8 2018-04-29 stsp if (error != NULL)
2186 8b473291 2019-02-21 stsp goto done;
2188 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2189 8b473291 2019-02-21 stsp if (error)
2190 ecb28ae0 2018-07-16 stsp goto done;
2192 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2193 04cc582a 2018-08-01 stsp if (view == NULL) {
2194 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2195 04cc582a 2018-08-01 stsp goto done;
2197 8b473291 2019-02-21 stsp error = open_log_view(view, start_id, &refs, repo, path, 1);
2198 ba4f502b 2018-08-04 stsp if (error)
2199 ba4f502b 2018-08-04 stsp goto done;
2200 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2202 ecb28ae0 2018-07-16 stsp free(repo_path);
2203 ecb28ae0 2018-07-16 stsp free(cwd);
2204 ecb28ae0 2018-07-16 stsp free(path);
2205 899d86c2 2018-05-10 stsp free(start_id);
2207 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2208 ec142235 2019-03-07 stsp if (worktree)
2209 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2210 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2211 80ddbec8 2018-04-29 stsp return error;
2214 4ed7e80c 2018-05-20 stsp __dead static void
2215 9f7d7167 2018-04-29 stsp usage_diff(void)
2218 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2219 9f7d7167 2018-04-29 stsp getprogname());
2223 b304db33 2018-05-20 stsp static char *
2224 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2226 b304db33 2018-05-20 stsp char *line;
2227 b304db33 2018-05-20 stsp size_t linelen;
2228 b304db33 2018-05-20 stsp size_t lineno;
2229 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2231 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2233 b304db33 2018-05-20 stsp *len = linelen;
2234 b304db33 2018-05-20 stsp return line;
2237 4ed7e80c 2018-05-20 stsp static const struct got_error *
2238 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2239 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
2240 c3e9aa98 2019-05-13 jcs char *header)
2242 61e69b96 2018-05-20 stsp const struct got_error *err;
2243 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2244 b304db33 2018-05-20 stsp char *line;
2245 b304db33 2018-05-20 stsp size_t len;
2246 61e69b96 2018-05-20 stsp wchar_t *wline;
2247 e0b650dd 2018-05-20 stsp int width;
2249 26ed57b2 2018-05-19 stsp rewind(f);
2250 f7d12f7e 2018-08-01 stsp werase(view->window);
2252 a3404814 2018-09-02 stsp if (header) {
2253 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
2254 a3404814 2018-09-02 stsp if (err) {
2255 a3404814 2018-09-02 stsp return err;
2258 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2259 a3404814 2018-09-02 stsp wstandout(view->window);
2260 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2261 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2262 a3404814 2018-09-02 stsp wstandend(view->window);
2263 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2264 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2266 a3404814 2018-09-02 stsp if (max_lines <= 1)
2267 a3404814 2018-09-02 stsp return NULL;
2268 a3404814 2018-09-02 stsp max_lines--;
2272 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2273 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2274 26ed57b2 2018-05-19 stsp if (line == NULL) {
2278 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2279 26ed57b2 2018-05-19 stsp free(line);
2283 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2284 61e69b96 2018-05-20 stsp if (err) {
2285 61e69b96 2018-05-20 stsp free(line);
2286 61e69b96 2018-05-20 stsp return err;
2288 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2289 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2290 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2291 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2292 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2293 26ed57b2 2018-05-19 stsp free(line);
2294 2550e4c3 2018-07-13 stsp free(wline);
2295 2550e4c3 2018-07-13 stsp wline = NULL;
2297 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2299 1a57306a 2018-09-02 stsp view_vborder(view);
2301 c3e9aa98 2019-05-13 jcs if (*eof) {
2302 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2303 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2304 c3e9aa98 2019-05-13 jcs nprinted++;
2307 c3e9aa98 2019-05-13 jcs err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2309 c3e9aa98 2019-05-13 jcs return err;
2312 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2313 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2314 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2317 26ed57b2 2018-05-19 stsp return NULL;
2320 abd2672a 2018-12-23 stsp static char *
2321 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2323 abd2672a 2018-12-23 stsp char *p, *s = ctime_r(time, datebuf);
2324 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2326 abd2672a 2018-12-23 stsp *p = '\0';
2330 4ed7e80c 2018-05-20 stsp static const struct got_error *
2331 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2332 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2334 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2335 abd2672a 2018-12-23 stsp char datebuf[26];
2336 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2337 15a94983 2018-12-23 stsp char *id_str = NULL;
2338 45d799e2 2018-12-23 stsp time_t committer_time;
2339 45d799e2 2018-12-23 stsp const char *author, *committer;
2340 8b473291 2019-02-21 stsp char *refs_str = NULL;
2342 8b473291 2019-02-21 stsp if (refs) {
2343 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, commit_id);
2345 8b473291 2019-02-21 stsp return err;
2348 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2350 abd2672a 2018-12-23 stsp return err;
2352 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2353 15a94983 2018-12-23 stsp if (err) {
2354 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2355 15a94983 2018-12-23 stsp goto done;
2358 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2359 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2360 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2361 abd2672a 2018-12-23 stsp goto done;
2363 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2364 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2365 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2366 abd2672a 2018-12-23 stsp goto done;
2368 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2369 abd2672a 2018-12-23 stsp if (fprintf(outfile, "date: %s UTC\n",
2370 45d799e2 2018-12-23 stsp get_datestr(&committer_time, datebuf)) < 0) {
2371 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2372 abd2672a 2018-12-23 stsp goto done;
2374 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2375 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2376 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2377 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2378 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2379 abd2672a 2018-12-23 stsp goto done;
2381 45d799e2 2018-12-23 stsp if (fprintf(outfile, "%s\n",
2382 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(commit)) < 0) {
2383 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2384 abd2672a 2018-12-23 stsp goto done;
2387 abd2672a 2018-12-23 stsp free(id_str);
2388 8b473291 2019-02-21 stsp free(refs_str);
2389 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2390 abd2672a 2018-12-23 stsp return err;
2393 abd2672a 2018-12-23 stsp static const struct got_error *
2394 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2396 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2397 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2398 15a94983 2018-12-23 stsp int obj_type;
2400 511a516b 2018-05-19 stsp f = got_opentemp();
2401 48ae06ee 2018-10-18 stsp if (f == NULL) {
2402 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2403 48ae06ee 2018-10-18 stsp goto done;
2405 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2406 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2407 fb43ecf1 2019-02-11 stsp goto done;
2411 15a94983 2018-12-23 stsp if (s->id1)
2412 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2414 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2416 15a94983 2018-12-23 stsp goto done;
2418 15a94983 2018-12-23 stsp switch (obj_type) {
2419 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2420 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2421 54156555 2018-12-24 stsp s->diff_context, s->repo, f);
2423 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2424 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2425 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
2427 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2428 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2429 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2430 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2432 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2435 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2436 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2437 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2439 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2440 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2441 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2442 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2443 8b473291 2019-02-21 stsp s->repo, f);
2448 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2450 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2451 15a94983 2018-12-23 stsp s->diff_context, s->repo, f);
2455 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2459 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2460 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2461 48ae06ee 2018-10-18 stsp return err;
2464 f5215bb9 2019-02-22 stsp static void
2465 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2467 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2468 f5215bb9 2019-02-22 stsp update_panels();
2469 f5215bb9 2019-02-22 stsp doupdate();
2472 48ae06ee 2018-10-18 stsp static const struct got_error *
2473 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2474 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2475 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2477 48ae06ee 2018-10-18 stsp const struct got_error *err;
2479 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2480 15a94983 2018-12-23 stsp int type1, type2;
2481 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2483 15a94983 2018-12-23 stsp return err;
2484 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2486 15a94983 2018-12-23 stsp return err;
2488 15a94983 2018-12-23 stsp if (type1 != type2)
2489 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2492 15a94983 2018-12-23 stsp if (id1) {
2493 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2494 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2495 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2497 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2499 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2500 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2501 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2502 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2503 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2505 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2506 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2507 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2508 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2509 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2510 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2511 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2513 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2514 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2515 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2517 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2518 48ae06ee 2018-10-18 stsp if (err) {
2519 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2520 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2521 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2522 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2523 48ae06ee 2018-10-18 stsp return err;
2526 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2527 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2528 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2530 5dc9f4bc 2018-08-04 stsp return NULL;
2533 e5a0f69f 2018-08-18 stsp static const struct got_error *
2534 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2536 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2538 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2539 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2540 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2541 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2542 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2543 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2544 e5a0f69f 2018-08-18 stsp return err;
2547 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2548 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2550 a3404814 2018-09-02 stsp const struct got_error *err;
2551 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2552 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2554 a3404814 2018-09-02 stsp if (s->id1) {
2555 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2557 a3404814 2018-09-02 stsp return err;
2559 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2561 a3404814 2018-09-02 stsp return err;
2563 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2564 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2565 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2566 a3404814 2018-09-02 stsp free(id_str1);
2567 a3404814 2018-09-02 stsp free(id_str2);
2568 a3404814 2018-09-02 stsp return err;
2570 a3404814 2018-09-02 stsp free(id_str1);
2571 a3404814 2018-09-02 stsp free(id_str2);
2573 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2574 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2578 15a087fe 2019-02-21 stsp static const struct got_error *
2579 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2580 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2582 d7a04538 2019-02-21 stsp const struct got_error *err;
2583 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
2584 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
2585 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
2587 15a087fe 2019-02-21 stsp free(s->id2);
2588 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2589 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2590 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2592 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2594 d7a04538 2019-02-21 stsp return err;
2595 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
2596 15a087fe 2019-02-21 stsp free(s->id1);
2597 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
2598 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2599 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
2600 15a087fe 2019-02-21 stsp return NULL;
2603 0cf4efb1 2018-09-29 stsp static const struct got_error *
2604 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2605 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2607 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2608 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2609 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
2610 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
2613 e5a0f69f 2018-08-18 stsp switch (ch) {
2615 1e37a5c2 2019-05-12 jcs case KEY_UP:
2616 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
2617 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2619 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2620 a60a9dc4 2019-05-13 jcs case CTRL('b'):
2621 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
2624 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
2625 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
2626 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2629 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2630 1e37a5c2 2019-05-12 jcs if (!s->eof)
2631 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2633 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
2634 a60a9dc4 2019-05-13 jcs case CTRL('f'):
2636 00ba99a7 2019-05-12 jcs if (s->eof)
2639 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
2640 1e37a5c2 2019-05-12 jcs char *line;
2641 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
2642 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2643 1e37a5c2 2019-05-12 jcs if (line == NULL)
2648 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
2649 1e37a5c2 2019-05-12 jcs s->diff_context--;
2650 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2651 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2655 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2656 1e37a5c2 2019-05-12 jcs s->diff_context++;
2657 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2658 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2663 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2665 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2666 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
2667 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
2668 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2671 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2676 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2680 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2681 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2683 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2684 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2688 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2690 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2692 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2693 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
2695 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
2696 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
2697 1e37a5c2 2019-05-12 jcs update_panels();
2698 1e37a5c2 2019-05-12 jcs doupdate();
2700 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
2701 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
2702 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
2703 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
2707 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2712 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
2713 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2716 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2720 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2721 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2723 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2724 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2730 bcbd79e2 2018-08-19 stsp return err;
2733 4ed7e80c 2018-05-20 stsp static const struct got_error *
2734 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2736 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2737 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2738 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2739 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2740 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2741 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2743 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2745 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2747 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2748 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2749 eb6600df 2019-01-04 stsp NULL) == -1)
2750 26ed57b2 2018-05-19 stsp err(1, "pledge");
2753 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2754 26ed57b2 2018-05-19 stsp switch (ch) {
2756 17020d27 2019-03-07 stsp usage_diff();
2757 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2761 26ed57b2 2018-05-19 stsp argc -= optind;
2762 26ed57b2 2018-05-19 stsp argv += optind;
2764 26ed57b2 2018-05-19 stsp if (argc == 0) {
2765 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2766 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2767 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2768 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2769 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2770 15a94983 2018-12-23 stsp id_str1 = argv[0];
2771 15a94983 2018-12-23 stsp id_str2 = argv[1];
2772 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2773 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2774 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2775 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2776 15a94983 2018-12-23 stsp id_str1 = argv[1];
2777 15a94983 2018-12-23 stsp id_str2 = argv[2];
2779 26ed57b2 2018-05-19 stsp usage_diff();
2781 a915003a 2019-02-05 stsp init_curses();
2783 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2784 eb6600df 2019-01-04 stsp if (error)
2785 eb6600df 2019-01-04 stsp goto done;
2787 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
2788 26ed57b2 2018-05-19 stsp if (error)
2789 26ed57b2 2018-05-19 stsp goto done;
2791 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
2792 26ed57b2 2018-05-19 stsp if (error)
2793 26ed57b2 2018-05-19 stsp goto done;
2795 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
2796 26ed57b2 2018-05-19 stsp if (error)
2797 26ed57b2 2018-05-19 stsp goto done;
2799 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2800 8b473291 2019-02-21 stsp if (error)
2801 8b473291 2019-02-21 stsp goto done;
2803 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2804 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2805 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2806 ea5e7bb5 2018-08-01 stsp goto done;
2808 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2809 5dc9f4bc 2018-08-04 stsp if (error)
2810 5dc9f4bc 2018-08-04 stsp goto done;
2811 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2813 c02c541e 2019-03-29 stsp free(repo_path);
2814 26ed57b2 2018-05-19 stsp got_repo_close(repo);
2815 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2816 26ed57b2 2018-05-19 stsp return error;
2819 4ed7e80c 2018-05-20 stsp __dead static void
2820 9f7d7167 2018-04-29 stsp usage_blame(void)
2823 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2824 9f7d7167 2018-04-29 stsp getprogname());
2828 84451b3e 2018-07-10 stsp struct tog_blame_line {
2829 84451b3e 2018-07-10 stsp int annotated;
2830 84451b3e 2018-07-10 stsp struct got_object_id *id;
2833 4ed7e80c 2018-05-20 stsp static const struct got_error *
2834 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2835 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2836 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2837 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2839 84451b3e 2018-07-10 stsp const struct got_error *err;
2840 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2841 84451b3e 2018-07-10 stsp char *line;
2842 84451b3e 2018-07-10 stsp size_t len;
2843 84451b3e 2018-07-10 stsp wchar_t *wline;
2844 b700b5d6 2018-07-10 stsp int width, wlimit;
2845 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2846 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2847 ab089a2a 2018-07-12 stsp char *id_str;
2849 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2851 ab089a2a 2018-07-12 stsp return err;
2853 84451b3e 2018-07-10 stsp rewind(f);
2854 f7d12f7e 2018-08-01 stsp werase(view->window);
2856 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
2857 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2858 ab089a2a 2018-07-12 stsp free(id_str);
2859 ab089a2a 2018-07-12 stsp return err;
2862 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2863 ab089a2a 2018-07-12 stsp free(line);
2864 2550e4c3 2018-07-13 stsp line = NULL;
2865 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2866 a3404814 2018-09-02 stsp wstandout(view->window);
2867 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2868 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2869 a3404814 2018-09-02 stsp wstandend(view->window);
2870 2550e4c3 2018-07-13 stsp free(wline);
2871 2550e4c3 2018-07-13 stsp wline = NULL;
2872 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2873 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2875 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2876 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2877 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
2878 ab089a2a 2018-07-12 stsp free(id_str);
2879 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2881 ab089a2a 2018-07-12 stsp free(id_str);
2882 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2883 3f60a8ef 2018-07-10 stsp free(line);
2884 2550e4c3 2018-07-13 stsp line = NULL;
2886 3f60a8ef 2018-07-10 stsp return err;
2887 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2888 2550e4c3 2018-07-13 stsp free(wline);
2889 2550e4c3 2018-07-13 stsp wline = NULL;
2890 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2891 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2894 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2895 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2896 84451b3e 2018-07-10 stsp if (line == NULL) {
2900 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2901 84451b3e 2018-07-10 stsp free(line);
2905 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2906 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2907 84451b3e 2018-07-10 stsp if (err) {
2908 84451b3e 2018-07-10 stsp free(line);
2909 84451b3e 2018-07-10 stsp return err;
2912 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2913 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2915 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2916 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2917 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2918 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2919 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2920 84451b3e 2018-07-10 stsp char *id_str;
2921 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2922 84451b3e 2018-07-10 stsp if (err) {
2923 84451b3e 2018-07-10 stsp free(line);
2924 2550e4c3 2018-07-13 stsp free(wline);
2925 84451b3e 2018-07-10 stsp return err;
2927 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2928 84451b3e 2018-07-10 stsp free(id_str);
2929 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2931 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2932 ee41ec32 2018-07-10 stsp prev_id = NULL;
2935 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2936 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2937 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2940 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2941 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2942 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2943 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2944 84451b3e 2018-07-10 stsp free(line);
2945 2550e4c3 2018-07-13 stsp free(wline);
2946 2550e4c3 2018-07-13 stsp wline = NULL;
2948 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2950 1a57306a 2018-09-02 stsp view_vborder(view);
2952 84451b3e 2018-07-10 stsp return NULL;
2955 84451b3e 2018-07-10 stsp static const struct got_error *
2956 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2958 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2959 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2960 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2961 1a76625f 2018-10-22 stsp int errcode;
2963 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2964 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2965 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2967 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2968 1a76625f 2018-10-22 stsp if (errcode)
2969 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
2971 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2972 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2973 d68a0a7d 2018-07-10 stsp goto done;
2976 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2977 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2979 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2980 d68a0a7d 2018-07-10 stsp if (line->annotated)
2981 d68a0a7d 2018-07-10 stsp goto done;
2983 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2984 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2985 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
2986 84451b3e 2018-07-10 stsp goto done;
2988 84451b3e 2018-07-10 stsp line->annotated = 1;
2990 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2991 1a76625f 2018-10-22 stsp if (errcode)
2992 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2993 84451b3e 2018-07-10 stsp return err;
2996 84451b3e 2018-07-10 stsp static void *
2997 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2999 18430de3 2018-07-10 stsp const struct got_error *err;
3000 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3001 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3002 1a76625f 2018-10-22 stsp int errcode;
3004 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3005 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
3007 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3008 1a76625f 2018-10-22 stsp if (errcode)
3009 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3010 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3012 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3013 c9beca56 2018-07-22 stsp ta->repo = NULL;
3014 c9beca56 2018-07-22 stsp *ta->complete = 1;
3016 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3017 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3018 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3020 18430de3 2018-07-10 stsp return (void *)err;
3023 245d91c1 2018-07-12 stsp static struct got_object_id *
3024 15a94983 2018-12-23 stsp get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3025 15a94983 2018-12-23 stsp int selected_line)
3027 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3029 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3030 245d91c1 2018-07-12 stsp if (!line->annotated)
3031 245d91c1 2018-07-12 stsp return NULL;
3033 245d91c1 2018-07-12 stsp return line->id;
3036 b880a918 2018-07-10 stsp static const struct got_error *
3037 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3039 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3042 245d91c1 2018-07-12 stsp if (blame->thread) {
3043 1a76625f 2018-10-22 stsp int errcode;
3044 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3045 1a76625f 2018-10-22 stsp if (errcode)
3046 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3047 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3048 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3049 1a76625f 2018-10-22 stsp if (errcode)
3050 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3051 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3052 1a76625f 2018-10-22 stsp if (errcode)
3053 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3054 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3055 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3056 245d91c1 2018-07-12 stsp err = NULL;
3057 245d91c1 2018-07-12 stsp blame->thread = NULL;
3059 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3060 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3061 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3063 245d91c1 2018-07-12 stsp if (blame->f) {
3064 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3065 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3066 245d91c1 2018-07-12 stsp blame->f = NULL;
3068 57670559 2018-12-23 stsp if (blame->lines) {
3069 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3070 57670559 2018-12-23 stsp free(blame->lines[i].id);
3071 57670559 2018-12-23 stsp free(blame->lines);
3072 57670559 2018-12-23 stsp blame->lines = NULL;
3074 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3075 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3077 245d91c1 2018-07-12 stsp return err;
3080 245d91c1 2018-07-12 stsp static const struct got_error *
3081 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3082 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3083 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3084 245d91c1 2018-07-12 stsp struct got_repository *repo)
3086 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3087 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3088 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3089 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3090 15a94983 2018-12-23 stsp int obj_type;
3092 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3094 15a94983 2018-12-23 stsp return err;
3095 15a94983 2018-12-23 stsp if (obj_id == NULL)
3096 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
3098 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3100 84451b3e 2018-07-10 stsp goto done;
3102 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3103 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3104 84451b3e 2018-07-10 stsp goto done;
3107 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3109 a70480e0 2018-06-23 stsp goto done;
3110 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3111 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3112 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3113 84451b3e 2018-07-10 stsp goto done;
3115 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3116 245d91c1 2018-07-12 stsp blame->f, blob);
3118 84451b3e 2018-07-10 stsp goto done;
3120 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3121 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3122 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3123 84451b3e 2018-07-10 stsp goto done;
3126 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3128 bd24772e 2018-07-11 stsp goto done;
3130 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3131 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3132 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3133 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3134 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3135 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3136 245d91c1 2018-07-12 stsp goto done;
3138 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3140 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3141 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3142 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3143 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3144 245d91c1 2018-07-12 stsp *blame_complete = 0;
3148 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3149 27d434c2 2018-09-15 stsp free(obj_id);
3151 245d91c1 2018-07-12 stsp stop_blame(blame);
3152 245d91c1 2018-07-12 stsp return err;
3155 245d91c1 2018-07-12 stsp static const struct got_error *
3156 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3157 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3158 8b473291 2019-02-21 stsp struct got_repository *repo)
3160 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3161 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3163 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3165 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3167 7cbe629d 2018-08-04 stsp return err;
3169 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3170 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3171 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3172 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3173 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3174 fb2756b9 2018-08-04 stsp s->path = path;
3175 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
3176 638f9024 2019-05-13 stsp return got_error_from_errno("open_blame_view");
3177 fb2756b9 2018-08-04 stsp s->repo = repo;
3178 8b473291 2019-02-21 stsp s->refs = refs;
3179 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3180 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3182 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3183 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3184 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3186 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3187 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3188 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3189 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3192 e5a0f69f 2018-08-18 stsp static const struct got_error *
3193 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3195 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3196 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3198 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3199 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3201 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3202 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3203 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3204 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3205 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3208 e5a0f69f 2018-08-18 stsp free(s->path);
3210 e5a0f69f 2018-08-18 stsp return err;
3213 7cbe629d 2018-08-04 stsp static const struct got_error *
3214 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
3216 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3217 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
3218 2b380cc8 2018-10-24 stsp int errcode;
3220 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
3221 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3222 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
3223 2b380cc8 2018-10-24 stsp if (errcode)
3224 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3227 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3228 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3229 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
3230 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
3232 669b5ffa 2018-10-07 stsp view_vborder(view);
3233 e5a0f69f 2018-08-18 stsp return err;
3236 e5a0f69f 2018-08-18 stsp static const struct got_error *
3237 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3238 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3240 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
3241 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
3242 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3243 669b5ffa 2018-10-07 stsp int begin_x = 0;
3245 e5a0f69f 2018-08-18 stsp switch (ch) {
3247 1e37a5c2 2019-05-12 jcs s->done = 1;
3250 1e37a5c2 2019-05-12 jcs case KEY_UP:
3251 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
3252 1e37a5c2 2019-05-12 jcs s->selected_line--;
3253 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
3254 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3255 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3257 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3258 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
3259 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3262 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3263 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3264 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3266 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3269 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3270 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3271 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3272 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3273 1e37a5c2 2019-05-12 jcs s->selected_line++;
3274 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3275 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3276 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3279 1e37a5c2 2019-05-12 jcs case 'p': {
3280 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3281 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3282 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3283 1e37a5c2 2019-05-12 jcs if (id == NULL)
3285 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3286 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3287 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3288 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3289 1e37a5c2 2019-05-12 jcs int obj_type;
3290 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3291 1e37a5c2 2019-05-12 jcs s->repo, id);
3294 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3295 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3296 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3297 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3300 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3301 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3302 1e37a5c2 2019-05-12 jcs pid->id, s->path);
3303 e5a0f69f 2018-08-18 stsp if (err) {
3304 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
3305 1e37a5c2 2019-05-12 jcs err = NULL;
3306 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3309 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
3311 1e37a5c2 2019-05-12 jcs free(blob_id);
3312 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
3313 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
3314 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3317 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3319 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3321 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
3322 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
3324 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3329 1e37a5c2 2019-05-12 jcs s->done = 1;
3330 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3331 1e37a5c2 2019-05-12 jcs s->done = 0;
3332 1e37a5c2 2019-05-12 jcs if (thread_err)
3334 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3335 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
3336 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3337 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3338 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
3339 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
3344 1e37a5c2 2019-05-12 jcs case 'B': {
3345 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
3346 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
3347 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
3349 1e37a5c2 2019-05-12 jcs s->done = 1;
3350 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3351 1e37a5c2 2019-05-12 jcs s->done = 0;
3352 1e37a5c2 2019-05-12 jcs if (thread_err)
3354 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3355 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
3356 1e37a5c2 2019-05-12 jcs s->blamed_commit =
3357 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
3358 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3359 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3360 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
3361 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
3366 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3367 1e37a5c2 2019-05-12 jcs case '\r': {
3368 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3369 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
3370 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
3371 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3372 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3373 1e37a5c2 2019-05-12 jcs if (id == NULL)
3375 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
3378 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
3379 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
3380 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3381 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3382 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3383 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
3384 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3385 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
3388 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
3389 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
3390 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3392 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3395 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3396 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3399 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
3401 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3404 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
3405 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
3407 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3412 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3414 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3415 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
3416 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
3419 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3420 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
3421 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3422 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3425 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
3426 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
3427 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
3428 1e37a5c2 2019-05-12 jcs view->nlines - 2;
3430 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
3431 1e37a5c2 2019-05-12 jcs s->blame.nlines -
3432 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
3434 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3435 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
3436 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3437 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3443 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
3446 a70480e0 2018-06-23 stsp static const struct got_error *
3447 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
3449 a70480e0 2018-06-23 stsp const struct got_error *error;
3450 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
3451 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3452 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
3453 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3454 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3455 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
3457 e1cd8fed 2018-08-01 stsp struct tog_view *view;
3459 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3461 a70480e0 2018-06-23 stsp #ifndef PROFILE
3462 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3463 8e94dd5b 2019-01-04 stsp NULL) == -1)
3464 a70480e0 2018-06-23 stsp err(1, "pledge");
3467 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3468 a70480e0 2018-06-23 stsp switch (ch) {
3470 a70480e0 2018-06-23 stsp commit_id_str = optarg;
3473 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3474 69069811 2018-08-02 stsp if (repo_path == NULL)
3475 69069811 2018-08-02 stsp err(1, "-r option");
3478 17020d27 2019-03-07 stsp usage_blame();
3479 a70480e0 2018-06-23 stsp /* NOTREACHED */
3483 a70480e0 2018-06-23 stsp argc -= optind;
3484 a70480e0 2018-06-23 stsp argv += optind;
3486 69069811 2018-08-02 stsp if (argc == 1)
3487 69069811 2018-08-02 stsp path = argv[0];
3489 a70480e0 2018-06-23 stsp usage_blame();
3491 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
3492 69069811 2018-08-02 stsp if (cwd == NULL) {
3493 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3494 69069811 2018-08-02 stsp goto done;
3496 69069811 2018-08-02 stsp if (repo_path == NULL) {
3497 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3498 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3499 69069811 2018-08-02 stsp goto done;
3501 eb41ed75 2019-02-05 stsp error = NULL;
3502 eb41ed75 2019-02-05 stsp if (worktree) {
3503 eb41ed75 2019-02-05 stsp repo_path =
3504 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3505 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
3506 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3507 eb41ed75 2019-02-05 stsp if (error)
3508 eb41ed75 2019-02-05 stsp goto done;
3510 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
3511 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
3512 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3513 eb41ed75 2019-02-05 stsp goto done;
3518 a915003a 2019-02-05 stsp init_curses();
3520 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
3521 c02c541e 2019-03-29 stsp if (error != NULL)
3522 8e94dd5b 2019-01-04 stsp goto done;
3524 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3525 c02c541e 2019-03-29 stsp if (error)
3526 92205607 2019-01-04 stsp goto done;
3528 eb41ed75 2019-02-05 stsp if (worktree) {
3529 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3530 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3531 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3532 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3533 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3534 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3535 eb41ed75 2019-02-05 stsp path) == -1) {
3536 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3537 eb41ed75 2019-02-05 stsp goto done;
3539 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3542 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3544 eb41ed75 2019-02-05 stsp if (error)
3545 69069811 2018-08-02 stsp goto done;
3547 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
3548 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
3549 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3550 a70480e0 2018-06-23 stsp if (error != NULL)
3551 66b4983c 2018-06-23 stsp goto done;
3552 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3553 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
3555 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
3556 15a94983 2018-12-23 stsp commit_id_str);
3558 a19e88aa 2018-06-23 stsp if (error != NULL)
3559 8b473291 2019-02-21 stsp goto done;
3561 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
3562 8b473291 2019-02-21 stsp if (error)
3563 a19e88aa 2018-06-23 stsp goto done;
3565 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3566 e1cd8fed 2018-08-01 stsp if (view == NULL) {
3567 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3568 e1cd8fed 2018-08-01 stsp goto done;
3570 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3571 7cbe629d 2018-08-04 stsp if (error)
3572 7cbe629d 2018-08-04 stsp goto done;
3573 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3575 69069811 2018-08-02 stsp free(repo_path);
3576 69069811 2018-08-02 stsp free(cwd);
3577 a70480e0 2018-06-23 stsp free(commit_id);
3578 eb41ed75 2019-02-05 stsp if (worktree)
3579 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
3581 a70480e0 2018-06-23 stsp got_repo_close(repo);
3582 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3583 a70480e0 2018-06-23 stsp return error;
3586 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3587 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
3588 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
3589 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
3590 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
3591 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
3592 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
3594 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3595 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
3596 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
3597 ffd1d5e5 2018-06-23 stsp int width, n;
3599 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
3601 f7d12f7e 2018-08-01 stsp werase(view->window);
3603 ffd1d5e5 2018-06-23 stsp if (limit == 0)
3604 ffd1d5e5 2018-06-23 stsp return NULL;
3606 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
3608 ffd1d5e5 2018-06-23 stsp return err;
3609 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3610 a3404814 2018-09-02 stsp wstandout(view->window);
3611 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3612 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3613 a3404814 2018-09-02 stsp wstandend(view->window);
3614 2550e4c3 2018-07-13 stsp free(wline);
3615 2550e4c3 2018-07-13 stsp wline = NULL;
3616 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3617 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3618 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3619 ffd1d5e5 2018-06-23 stsp return NULL;
3620 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
3622 ce52c690 2018-06-23 stsp return err;
3623 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3624 2550e4c3 2018-07-13 stsp free(wline);
3625 2550e4c3 2018-07-13 stsp wline = NULL;
3626 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3627 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3628 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3629 ffd1d5e5 2018-06-23 stsp return NULL;
3630 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3631 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
3632 a1eca9bb 2018-06-23 stsp return NULL;
3634 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3635 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
3636 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
3637 0cf4efb1 2018-09-29 stsp if (view->focussed)
3638 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3639 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
3641 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
3642 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
3643 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3644 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3645 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3646 ffd1d5e5 2018-06-23 stsp return NULL;
3650 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
3651 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3654 ffd1d5e5 2018-06-23 stsp while (te) {
3655 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
3657 1d13200f 2018-07-12 stsp if (show_ids) {
3658 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
3660 638f9024 2019-05-13 stsp return got_error_from_errno(
3661 230a42bd 2019-05-11 jcs "got_object_id_str");
3663 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3664 d8355ef1 2019-02-10 stsp te->name, S_ISDIR(te->mode) ? "/" :
3665 d8355ef1 2019-02-10 stsp ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3666 1d13200f 2018-07-12 stsp free(id_str);
3667 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3669 1d13200f 2018-07-12 stsp free(id_str);
3670 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
3671 ffd1d5e5 2018-06-23 stsp if (err) {
3672 ffd1d5e5 2018-06-23 stsp free(line);
3675 ffd1d5e5 2018-06-23 stsp if (n == selected) {
3676 0cf4efb1 2018-09-29 stsp if (view->focussed)
3677 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3678 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
3680 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3681 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3682 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3683 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
3684 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3685 ffd1d5e5 2018-06-23 stsp free(line);
3686 2550e4c3 2018-07-13 stsp free(wline);
3687 2550e4c3 2018-07-13 stsp wline = NULL;
3689 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3690 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
3691 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3693 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3696 ffd1d5e5 2018-06-23 stsp return err;
3699 ffd1d5e5 2018-06-23 stsp static void
3700 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
3701 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
3702 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
3704 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
3707 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
3710 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3711 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
3712 ffd1d5e5 2018-06-23 stsp if (!isroot)
3713 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3717 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
3718 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
3719 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
3720 ffd1d5e5 2018-06-23 stsp prev = te;
3721 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3723 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
3724 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3726 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3727 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3730 768394f3 2019-01-24 stsp static int
3731 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3732 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
3733 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
3735 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
3736 ffd1d5e5 2018-06-23 stsp int n = 0;
3738 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
3739 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3741 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
3742 768394f3 2019-01-24 stsp last = last_displayed_entry;
3743 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
3744 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
3745 768394f3 2019-01-24 stsp if (last) {
3746 768394f3 2019-01-24 stsp *first_displayed_entry = next;
3747 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
3753 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3754 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
3755 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
3757 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
3758 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
3759 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
3761 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
3762 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
3764 ce52c690 2018-06-23 stsp len += strlen(te->name);
3766 ce52c690 2018-06-23 stsp *path = calloc(1, len);
3767 ffd1d5e5 2018-06-23 stsp if (path == NULL)
3768 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
3770 ce52c690 2018-06-23 stsp (*path)[0] = '/';
3771 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
3772 d9765a41 2018-06-23 stsp while (pt) {
3773 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3774 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3775 cb2ebc8a 2018-06-23 stsp goto done;
3777 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
3778 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3779 cb2ebc8a 2018-06-23 stsp goto done;
3781 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3784 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3785 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3786 ce52c690 2018-06-23 stsp goto done;
3790 ce52c690 2018-06-23 stsp if (err) {
3791 ce52c690 2018-06-23 stsp free(*path);
3792 ce52c690 2018-06-23 stsp *path = NULL;
3794 ce52c690 2018-06-23 stsp return err;
3797 ce52c690 2018-06-23 stsp static const struct got_error *
3798 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3799 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3800 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3801 8b473291 2019-02-21 stsp struct got_repository *repo)
3803 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3804 ce52c690 2018-06-23 stsp char *path;
3805 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3807 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3809 ce52c690 2018-06-23 stsp return err;
3811 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3812 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3813 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3815 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
3816 e5a0f69f 2018-08-18 stsp if (err) {
3817 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3818 e5a0f69f 2018-08-18 stsp free(path);
3820 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3821 69efd4c4 2018-07-18 stsp return err;
3824 69efd4c4 2018-07-18 stsp static const struct got_error *
3825 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3826 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3827 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3828 8b473291 2019-02-21 stsp struct got_repository *repo)
3830 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3831 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3832 69efd4c4 2018-07-18 stsp char *path;
3834 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3835 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3836 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3838 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3840 69efd4c4 2018-07-18 stsp return err;
3842 8b473291 2019-02-21 stsp err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3844 e5a0f69f 2018-08-18 stsp view_close(log_view);
3846 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3847 cb2ebc8a 2018-06-23 stsp free(path);
3848 cb2ebc8a 2018-06-23 stsp return err;
3851 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3852 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3853 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3854 8b473291 2019-02-21 stsp struct got_repository *repo)
3856 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3857 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3858 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3860 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3862 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3863 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3864 ffd1d5e5 2018-06-23 stsp goto done;
3866 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3867 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3868 ffd1d5e5 2018-06-23 stsp goto done;
3871 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3872 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3873 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3874 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3875 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3876 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3877 6484ec90 2018-09-29 stsp goto done;
3879 8b473291 2019-02-21 stsp s->refs = refs;
3880 fb2756b9 2018-08-04 stsp s->repo = repo;
3882 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
3883 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
3884 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
3886 ad80ab7b 2018-08-04 stsp free(commit_id_str);
3887 6484ec90 2018-09-29 stsp if (err) {
3888 fb2756b9 2018-08-04 stsp free(s->tree_label);
3889 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3891 ad80ab7b 2018-08-04 stsp return err;
3894 e5a0f69f 2018-08-18 stsp static const struct got_error *
3895 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
3897 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3899 fb2756b9 2018-08-04 stsp free(s->tree_label);
3900 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3901 6484ec90 2018-09-29 stsp free(s->commit_id);
3902 6484ec90 2018-09-29 stsp s->commit_id = NULL;
3903 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
3904 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
3905 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
3906 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
3907 ad80ab7b 2018-08-04 stsp free(parent);
3910 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
3911 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
3912 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
3914 e5a0f69f 2018-08-18 stsp return NULL;
3917 ad80ab7b 2018-08-04 stsp static const struct got_error *
3918 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
3920 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
3921 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3922 e5a0f69f 2018-08-18 stsp char *parent_path;
3924 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
3926 e5a0f69f 2018-08-18 stsp return err;
3928 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
3929 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
3930 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3931 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
3932 e5a0f69f 2018-08-18 stsp free(parent_path);
3934 669b5ffa 2018-10-07 stsp view_vborder(view);
3935 e5a0f69f 2018-08-18 stsp return err;
3938 e5a0f69f 2018-08-18 stsp static const struct got_error *
3939 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3940 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3942 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3943 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
3944 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
3945 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
3947 e5a0f69f 2018-08-18 stsp switch (ch) {
3949 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
3952 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
3954 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3955 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3956 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
3957 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
3958 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
3959 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3960 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3962 1e37a5c2 2019-05-12 jcs return err;
3963 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
3965 1e37a5c2 2019-05-12 jcs view_close(log_view);
3968 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
3969 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
3971 1e37a5c2 2019-05-12 jcs *new_view = log_view;
3974 1e37a5c2 2019-05-12 jcs case KEY_UP:
3975 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
3976 1e37a5c2 2019-05-12 jcs s->selected--;
3977 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
3980 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
3982 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
3983 1e37a5c2 2019-05-12 jcs s->entries, s->tree == s->root);
3985 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3986 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
3987 1e37a5c2 2019-05-12 jcs MAX(0, view->nlines - 4 - s->selected), s->entries,
3988 1e37a5c2 2019-05-12 jcs s->tree == s->root);
3989 1e37a5c2 2019-05-12 jcs s->selected = 0;
3990 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_FIRST(&s->entries->head) ==
3991 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
3992 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
3995 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3996 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
3997 1e37a5c2 2019-05-12 jcs s->selected++;
4000 00ba99a7 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4001 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
4003 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
4004 1e37a5c2 2019-05-12 jcs s->last_displayed_entry, s->entries);
4006 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4007 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4009 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
4010 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
4011 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4014 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
4015 1e37a5c2 2019-05-12 jcs view->nlines, s->last_displayed_entry, s->entries);
4016 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
4017 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
4018 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
4019 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
4021 1e37a5c2 2019-05-12 jcs ndisplayed++;
4022 1e37a5c2 2019-05-12 jcs te = SIMPLEQ_NEXT(te, entry);
4023 1e37a5c2 2019-05-12 jcs } while (te);
4024 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
4027 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4029 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4030 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4031 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
4032 1e37a5c2 2019-05-12 jcs /* user selected '..' */
4033 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
4035 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
4036 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
4038 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
4039 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
4040 1e37a5c2 2019-05-12 jcs s->entries =
4041 1e37a5c2 2019-05-12 jcs got_object_tree_get_entries(s->tree);
4042 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
4043 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
4044 1e37a5c2 2019-05-12 jcs s->selected_entry =
4045 1e37a5c2 2019-05-12 jcs parent->selected_entry;
4046 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
4047 1e37a5c2 2019-05-12 jcs free(parent);
4048 1e37a5c2 2019-05-12 jcs } else if (S_ISDIR(s->selected_entry->mode)) {
4049 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
4050 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&subtree,
4051 1e37a5c2 2019-05-12 jcs s->repo, s->selected_entry->id);
4054 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
4055 941e9f74 2019-05-21 stsp if (err) {
4056 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
4059 1e37a5c2 2019-05-12 jcs } else if (S_ISREG(s->selected_entry->mode)) {
4060 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
4061 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
4062 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
4064 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
4065 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
4066 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
4069 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
4070 669b5ffa 2018-10-07 stsp err = view_close_child(view);
4072 669b5ffa 2018-10-07 stsp return err;
4073 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
4074 669b5ffa 2018-10-07 stsp if (err) {
4075 1e37a5c2 2019-05-12 jcs view_close(blame_view);
4078 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
4079 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
4081 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
4084 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4085 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
4086 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4092 ffd1d5e5 2018-06-23 stsp return err;
4095 ffd1d5e5 2018-06-23 stsp __dead static void
4096 ffd1d5e5 2018-06-23 stsp usage_tree(void)
4099 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4100 ffd1d5e5 2018-06-23 stsp getprogname());
4104 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4105 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
4107 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
4108 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
4109 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4110 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
4111 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4112 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
4113 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
4114 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
4116 5221c383 2018-08-01 stsp struct tog_view *view;
4118 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4120 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
4121 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4122 d188b9a6 2019-01-04 stsp NULL) == -1)
4123 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
4126 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
4127 ffd1d5e5 2018-06-23 stsp switch (ch) {
4129 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
4132 17020d27 2019-03-07 stsp usage_tree();
4133 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
4137 ffd1d5e5 2018-06-23 stsp argc -= optind;
4138 ffd1d5e5 2018-06-23 stsp argv += optind;
4140 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
4141 52185f70 2019-02-05 stsp struct got_worktree *worktree;
4142 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
4143 52185f70 2019-02-05 stsp if (cwd == NULL)
4144 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
4145 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4146 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4147 52185f70 2019-02-05 stsp goto done;
4148 52185f70 2019-02-05 stsp if (worktree) {
4149 52185f70 2019-02-05 stsp free(cwd);
4150 52185f70 2019-02-05 stsp repo_path =
4151 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4152 52185f70 2019-02-05 stsp got_worktree_close(worktree);
4154 52185f70 2019-02-05 stsp repo_path = cwd;
4155 52185f70 2019-02-05 stsp if (repo_path == NULL) {
4156 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4157 52185f70 2019-02-05 stsp goto done;
4159 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
4160 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
4161 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
4162 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
4164 ffd1d5e5 2018-06-23 stsp usage_log();
4166 a915003a 2019-02-05 stsp init_curses();
4168 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
4169 c02c541e 2019-03-29 stsp if (error != NULL)
4170 52185f70 2019-02-05 stsp goto done;
4172 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4173 c02c541e 2019-03-29 stsp if (error)
4174 52185f70 2019-02-05 stsp goto done;
4176 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
4177 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4179 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
4180 15a94983 2018-12-23 stsp commit_id_arg);
4181 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4182 ffd1d5e5 2018-06-23 stsp goto done;
4184 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4185 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4186 ffd1d5e5 2018-06-23 stsp goto done;
4188 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
4189 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
4190 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4191 8b473291 2019-02-21 stsp goto done;
4193 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
4194 8b473291 2019-02-21 stsp if (error)
4195 ffd1d5e5 2018-06-23 stsp goto done;
4197 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4198 5221c383 2018-08-01 stsp if (view == NULL) {
4199 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4200 5221c383 2018-08-01 stsp goto done;
4202 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
4203 ad80ab7b 2018-08-04 stsp if (error)
4204 ad80ab7b 2018-08-04 stsp goto done;
4205 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4207 52185f70 2019-02-05 stsp free(repo_path);
4208 ffd1d5e5 2018-06-23 stsp free(commit_id);
4209 ffd1d5e5 2018-06-23 stsp if (commit)
4210 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
4212 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
4214 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
4215 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4216 ffd1d5e5 2018-06-23 stsp return error;
4219 4ed7e80c 2018-05-20 stsp __dead static void
4220 9f7d7167 2018-04-29 stsp usage(void)
4224 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
4225 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
4226 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
4227 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
4228 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
4233 c2301be8 2018-04-30 stsp static char **
4234 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
4236 c2301be8 2018-04-30 stsp char **argv;
4237 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
4239 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
4240 c2301be8 2018-04-30 stsp if (argv == NULL)
4241 c2301be8 2018-04-30 stsp err(1, "calloc");
4242 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
4243 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
4244 c2301be8 2018-04-30 stsp err(1, "calloc");
4245 c2301be8 2018-04-30 stsp if (arg1) {
4246 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
4247 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
4248 c2301be8 2018-04-30 stsp err(1, "calloc");
4251 c2301be8 2018-04-30 stsp return argv;
4255 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
4257 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
4258 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
4259 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
4260 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
4262 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
4264 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
4265 9f7d7167 2018-04-29 stsp switch (ch) {
4267 9f7d7167 2018-04-29 stsp hflag = 1;
4271 9f7d7167 2018-04-29 stsp /* NOTREACHED */
4275 9f7d7167 2018-04-29 stsp argc -= optind;
4276 9f7d7167 2018-04-29 stsp argv += optind;
4277 9f7d7167 2018-04-29 stsp optind = 0;
4278 c2301be8 2018-04-30 stsp optreset = 1;
4280 c2301be8 2018-04-30 stsp if (argc == 0) {
4281 f29d3e89 2018-06-23 stsp if (hflag)
4283 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
4284 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
4285 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
4290 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
4291 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
4292 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
4293 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
4294 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
4295 9f7d7167 2018-04-29 stsp if (hflag)
4296 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
4300 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
4301 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
4302 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
4303 c2301be8 2018-04-30 stsp if (repo_path) {
4304 c2301be8 2018-04-30 stsp struct got_repository *repo;
4305 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
4306 c2301be8 2018-04-30 stsp if (error == NULL)
4307 c2301be8 2018-04-30 stsp got_repo_close(repo);
4309 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath",
4311 c2301be8 2018-04-30 stsp if (error) {
4312 f29d3e89 2018-06-23 stsp if (hflag) {
4313 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
4314 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
4318 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
4319 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
4320 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
4321 ad7de8d9 2018-04-30 stsp free(repo_path);
4324 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
4325 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
4327 c2301be8 2018-04-30 stsp free(repo_path);
4331 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4332 9f7d7167 2018-04-29 stsp if (error)
4333 9f7d7167 2018-04-29 stsp goto done;
4336 c2301be8 2018-04-30 stsp free(cmd_argv);
4337 9f7d7167 2018-04-29 stsp if (error)
4338 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);