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 53ccebc2 2019-07-30 stsp #include "got_version.h"
42 9f7d7167 2018-04-29 stsp #include "got_error.h"
43 80ddbec8 2018-04-29 stsp #include "got_object.h"
44 80ddbec8 2018-04-29 stsp #include "got_reference.h"
45 80ddbec8 2018-04-29 stsp #include "got_repository.h"
46 80ddbec8 2018-04-29 stsp #include "got_diff.h"
47 511a516b 2018-05-19 stsp #include "got_opentemp.h"
48 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
49 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
50 a70480e0 2018-06-23 stsp #include "got_blame.h"
51 c2db6724 2019-01-04 stsp #include "got_privsep.h"
52 1dd54920 2019-05-11 stsp #include "got_path.h"
53 b7165be3 2019-02-05 stsp #include "got_worktree.h"
56 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
63 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
65 9f7d7167 2018-04-29 stsp #ifndef nitems
66 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 9f7d7167 2018-04-29 stsp struct tog_cmd {
70 c2301be8 2018-04-30 stsp const char *name;
71 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
72 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
75 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
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 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
88 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
89 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
90 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
93 d6b05b5b 2018-08-04 stsp enum tog_view_type {
94 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
95 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
96 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
97 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
100 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
102 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
103 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
104 ba4f502b 2018-08-04 stsp struct got_object_id *id;
105 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
108 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
109 ba4f502b 2018-08-04 stsp struct commit_queue {
110 ba4f502b 2018-08-04 stsp int ncommits;
111 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
114 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
115 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
117 15a087fe 2019-02-21 stsp int first_displayed_line;
118 15a087fe 2019-02-21 stsp int last_displayed_line;
120 15a087fe 2019-02-21 stsp int diff_context;
121 15a087fe 2019-02-21 stsp struct got_repository *repo;
122 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
124 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
125 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
128 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
130 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
131 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
132 1a76625f 2018-10-22 stsp int commits_needed;
133 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
134 1a76625f 2018-10-22 stsp struct commit_queue *commits;
135 1a76625f 2018-10-22 stsp const char *in_repo_path;
136 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
137 1a76625f 2018-10-22 stsp struct got_repository *repo;
138 1a76625f 2018-10-22 stsp int log_complete;
139 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
140 1a76625f 2018-10-22 stsp struct tog_view *view;
141 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
142 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
145 1a76625f 2018-10-22 stsp struct tog_log_view_state {
146 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
147 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
148 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
149 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
150 b01e7d3b 2018-08-04 stsp int selected;
151 b01e7d3b 2018-08-04 stsp char *in_repo_path;
152 d01904d4 2019-06-25 stsp const char *head_ref_name;
153 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
154 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
155 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
156 1a76625f 2018-10-22 stsp sig_atomic_t quit;
157 1a76625f 2018-10-22 stsp pthread_t thread;
158 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
159 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
160 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
163 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
164 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
165 e9424729 2018-08-04 stsp int nlines;
167 e9424729 2018-08-04 stsp struct tog_view *view;
168 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
172 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
173 e9424729 2018-08-04 stsp const char *path;
174 e9424729 2018-08-04 stsp struct got_repository *repo;
175 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
176 e9424729 2018-08-04 stsp int *complete;
179 e9424729 2018-08-04 stsp struct tog_blame {
181 e9424729 2018-08-04 stsp size_t filesize;
182 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
183 6fcac457 2018-11-19 stsp int nlines;
184 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
185 e9424729 2018-08-04 stsp pthread_t thread;
186 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
187 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
188 e9424729 2018-08-04 stsp const char *path;
191 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
192 7cbe629d 2018-08-04 stsp int first_displayed_line;
193 7cbe629d 2018-08-04 stsp int last_displayed_line;
194 7cbe629d 2018-08-04 stsp int selected_line;
195 7cbe629d 2018-08-04 stsp int blame_complete;
198 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
199 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
200 e5a0f69f 2018-08-18 stsp char *path;
201 7cbe629d 2018-08-04 stsp struct got_repository *repo;
202 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
203 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
204 e9424729 2018-08-04 stsp struct tog_blame blame;
205 6c4c42e0 2019-06-24 stsp int matched_line;
208 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
209 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
210 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
211 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
212 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
213 ad80ab7b 2018-08-04 stsp int selected;
216 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
218 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
219 ad80ab7b 2018-08-04 stsp char *tree_label;
220 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
221 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
222 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
223 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
224 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
225 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
226 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
227 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
228 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
229 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
230 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
231 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
235 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
237 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
238 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
239 669b5ffa 2018-10-07 stsp * there is enough screen estate.
241 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
242 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
244 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
245 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
246 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
248 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
249 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
251 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
253 cc3c9aac 2018-08-01 stsp struct tog_view {
254 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
255 26ed57b2 2018-05-19 stsp WINDOW *window;
256 26ed57b2 2018-05-19 stsp PANEL *panel;
257 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
258 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
259 1004088d 2018-09-29 stsp int focussed;
260 669b5ffa 2018-10-07 stsp struct tog_view *parent;
261 669b5ffa 2018-10-07 stsp struct tog_view *child;
262 669b5ffa 2018-10-07 stsp int child_focussed;
264 5dc9f4bc 2018-08-04 stsp /* type-specific state */
265 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
267 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
268 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
269 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
270 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
273 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
274 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
275 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
276 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
278 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
279 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
280 60493ae3 2019-06-20 stsp int searching;
281 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
282 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
283 60493ae3 2019-06-20 stsp int search_next_done;
284 1803e47f 2019-06-22 stsp regex_t regex;
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 d01904d4 2019-06-25 stsp struct got_repository *, const char *, 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 *);
311 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
312 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
314 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
315 8b473291 2019-02-21 stsp struct got_tree_object *, struct got_object_id *,
316 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
317 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
318 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
319 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
320 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
321 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
322 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
324 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
326 25791caa 2018-10-24 stsp static void
327 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
329 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
332 e5a0f69f 2018-08-18 stsp static const struct got_error *
333 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
335 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
337 669b5ffa 2018-10-07 stsp if (view->child) {
338 669b5ffa 2018-10-07 stsp view_close(view->child);
339 669b5ffa 2018-10-07 stsp view->child = NULL;
341 e5a0f69f 2018-08-18 stsp if (view->close)
342 e5a0f69f 2018-08-18 stsp err = view->close(view);
343 ea5e7bb5 2018-08-01 stsp if (view->panel)
344 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
345 ea5e7bb5 2018-08-01 stsp if (view->window)
346 ea5e7bb5 2018-08-01 stsp delwin(view->window);
347 ea5e7bb5 2018-08-01 stsp free(view);
348 e5a0f69f 2018-08-18 stsp return err;
351 ea5e7bb5 2018-08-01 stsp static struct tog_view *
352 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
353 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
355 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
357 ea5e7bb5 2018-08-01 stsp if (view == NULL)
358 ea5e7bb5 2018-08-01 stsp return NULL;
360 d6b05b5b 2018-08-04 stsp view->type = type;
361 f7d12f7e 2018-08-01 stsp view->lines = LINES;
362 f7d12f7e 2018-08-01 stsp view->cols = COLS;
363 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
364 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
365 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
366 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
367 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
368 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
369 96a765a8 2018-08-04 stsp view_close(view);
370 ea5e7bb5 2018-08-01 stsp return NULL;
372 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
373 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
374 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
375 96a765a8 2018-08-04 stsp view_close(view);
376 ea5e7bb5 2018-08-01 stsp return NULL;
379 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
380 ea5e7bb5 2018-08-01 stsp return view;
384 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
386 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
388 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
391 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
393 5c60c32a 2018-10-18 stsp static const struct got_error *
394 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
396 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
398 5c60c32a 2018-10-18 stsp view->begin_y = 0;
399 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
400 5c60c32a 2018-10-18 stsp view->nlines = LINES;
401 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
402 5c60c32a 2018-10-18 stsp view->lines = LINES;
403 5c60c32a 2018-10-18 stsp view->cols = COLS;
404 5c60c32a 2018-10-18 stsp err = view_resize(view);
406 5c60c32a 2018-10-18 stsp return err;
408 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
409 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
411 5c60c32a 2018-10-18 stsp return NULL;
414 5c60c32a 2018-10-18 stsp static const struct got_error *
415 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
417 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
419 5c60c32a 2018-10-18 stsp view->begin_x = 0;
420 5c60c32a 2018-10-18 stsp view->begin_y = 0;
421 5c60c32a 2018-10-18 stsp view->nlines = LINES;
422 5c60c32a 2018-10-18 stsp view->ncols = COLS;
423 5c60c32a 2018-10-18 stsp view->lines = LINES;
424 5c60c32a 2018-10-18 stsp view->cols = COLS;
425 5c60c32a 2018-10-18 stsp err = view_resize(view);
427 5c60c32a 2018-10-18 stsp return err;
429 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
430 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
432 5c60c32a 2018-10-18 stsp return NULL;
436 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
438 5c60c32a 2018-10-18 stsp return view->parent == NULL;
441 4d8c2215 2018-08-19 stsp static const struct got_error *
442 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
444 f7d12f7e 2018-08-01 stsp int nlines, ncols;
446 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
447 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
449 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
451 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
452 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
454 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
456 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
457 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
458 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
459 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
460 25791caa 2018-10-24 stsp wclear(view->window);
462 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
463 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
464 0cf4efb1 2018-09-29 stsp view->lines = LINES;
465 0cf4efb1 2018-09-29 stsp view->cols = COLS;
467 6e3e5d9c 2018-10-18 stsp if (view->child) {
468 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
469 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
470 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
471 5c60c32a 2018-10-18 stsp if (view->child->focussed)
472 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
474 5c60c32a 2018-10-18 stsp show_panel(view->panel);
476 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
477 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
481 5c60c32a 2018-10-18 stsp return NULL;
484 669b5ffa 2018-10-07 stsp static const struct got_error *
485 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
487 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
489 669b5ffa 2018-10-07 stsp if (view->child == NULL)
490 669b5ffa 2018-10-07 stsp return NULL;
492 669b5ffa 2018-10-07 stsp err = view_close(view->child);
493 669b5ffa 2018-10-07 stsp view->child = NULL;
494 669b5ffa 2018-10-07 stsp return err;
497 669b5ffa 2018-10-07 stsp static const struct got_error *
498 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
500 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
502 669b5ffa 2018-10-07 stsp view->child = child;
503 669b5ffa 2018-10-07 stsp child->parent = view;
504 669b5ffa 2018-10-07 stsp return err;
508 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
510 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
513 34bc9ec9 2019-02-22 stsp static void
514 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
516 25791caa 2018-10-24 stsp int cols, lines;
517 25791caa 2018-10-24 stsp struct winsize size;
519 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
520 25791caa 2018-10-24 stsp cols = 80; /* Default */
521 25791caa 2018-10-24 stsp lines = 24;
523 25791caa 2018-10-24 stsp cols = size.ws_col;
524 25791caa 2018-10-24 stsp lines = size.ws_row;
526 25791caa 2018-10-24 stsp resize_term(lines, cols);
529 2b49a8ae 2019-06-22 stsp static const struct got_error *
530 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
532 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
533 2b49a8ae 2019-06-22 stsp char pattern[1024];
536 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
537 2b49a8ae 2019-06-22 stsp return NULL;
539 2b49a8ae 2019-06-22 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1,
540 2b49a8ae 2019-06-22 stsp view->begin_x, "/");
541 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
543 2b49a8ae 2019-06-22 stsp nocbreak();
545 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
548 2b49a8ae 2019-06-22 stsp if (ret == ERR)
549 2b49a8ae 2019-06-22 stsp return NULL;
551 2b49a8ae 2019-06-22 stsp if (view->searching) {
552 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
553 2b49a8ae 2019-06-22 stsp view->searching = 0;
556 2b49a8ae 2019-06-22 stsp if (regcomp(&view->regex, pattern,
557 f5daf9b1 2019-06-24 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
558 7c32bd05 2019-06-22 stsp err = view->search_start(view);
560 7c32bd05 2019-06-22 stsp regfree(&view->regex);
561 7c32bd05 2019-06-22 stsp return err;
563 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
564 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
565 2b49a8ae 2019-06-22 stsp view->search_next(view);
568 2b49a8ae 2019-06-22 stsp return NULL;
571 0cf4efb1 2018-09-29 stsp static const struct got_error *
572 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
573 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
574 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
576 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
577 669b5ffa 2018-10-07 stsp struct tog_view *v;
578 1a76625f 2018-10-22 stsp int ch, errcode;
580 e5a0f69f 2018-08-18 stsp *new = NULL;
581 e5a0f69f 2018-08-18 stsp *dead = NULL;
582 0cf4efb1 2018-09-29 stsp *focus = NULL;
584 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
585 60493ae3 2019-06-20 stsp errcode = pthread_mutex_unlock(&tog_mutex);
586 60493ae3 2019-06-20 stsp if (errcode)
587 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
588 60493ae3 2019-06-20 stsp "pthread_mutex_unlock");
589 60493ae3 2019-06-20 stsp pthread_yield();
590 60493ae3 2019-06-20 stsp errcode = pthread_mutex_lock(&tog_mutex);
591 60493ae3 2019-06-20 stsp if (errcode)
592 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
593 60493ae3 2019-06-20 stsp "pthread_mutex_lock");
594 60493ae3 2019-06-20 stsp view->search_next(view);
595 60493ae3 2019-06-20 stsp return NULL;
598 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
599 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
600 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
601 1a76625f 2018-10-22 stsp if (errcode)
602 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
603 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
604 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
605 1a76625f 2018-10-22 stsp if (errcode)
606 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
607 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
609 25791caa 2018-10-24 stsp if (tog_sigwinch_received) {
610 25791caa 2018-10-24 stsp tog_resizeterm();
611 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
612 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
613 25791caa 2018-10-24 stsp err = view_resize(v);
615 25791caa 2018-10-24 stsp return err;
616 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
618 25791caa 2018-10-24 stsp return err;
622 e5a0f69f 2018-08-18 stsp switch (ch) {
626 1e37a5c2 2019-05-12 jcs if (view->child) {
627 1e37a5c2 2019-05-12 jcs *focus = view->child;
628 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
629 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
630 1e37a5c2 2019-05-12 jcs *focus = view->parent;
631 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
635 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
636 1e37a5c2 2019-05-12 jcs *dead = view;
642 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
643 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
645 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
646 669b5ffa 2018-10-07 stsp *focus = view->child;
647 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
648 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
650 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
653 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
654 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
656 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
657 1e37a5c2 2019-05-12 jcs *focus = view;
658 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
659 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
661 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
665 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
666 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
669 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
672 60493ae3 2019-06-20 stsp if (view->search_start)
673 2b49a8ae 2019-06-22 stsp view_search_start(view);
675 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
679 60493ae3 2019-06-20 stsp if (view->search_next && view->searching) {
680 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
681 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
682 60493ae3 2019-06-20 stsp view->search_next_done = 0;
683 60493ae3 2019-06-20 stsp view->search_next(view);
685 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
688 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
692 e5a0f69f 2018-08-18 stsp return err;
696 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
698 0cf4efb1 2018-09-29 stsp PANEL *panel;
699 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
701 669b5ffa 2018-10-07 stsp if (view->parent)
702 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
704 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
705 0cf4efb1 2018-09-29 stsp if (panel == NULL)
708 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
709 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
710 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
714 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
716 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
717 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
719 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
721 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
724 669b5ffa 2018-10-07 stsp return view->focussed;
727 bcbd79e2 2018-08-19 stsp static const struct got_error *
728 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
730 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
731 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
732 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
733 fd823528 2018-10-22 stsp int fast_refresh = 10;
734 1a76625f 2018-10-22 stsp int done = 0, errcode;
736 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
737 1a76625f 2018-10-22 stsp if (errcode)
738 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
740 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
741 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
743 a81bf10d 2018-09-29 stsp main_view = view;
744 1004088d 2018-09-29 stsp view->focussed = 1;
745 878940b7 2018-09-29 stsp err = view->show(view);
747 0cf4efb1 2018-09-29 stsp return err;
748 0cf4efb1 2018-09-29 stsp update_panels();
749 0cf4efb1 2018-09-29 stsp doupdate();
750 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
751 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
752 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
753 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
755 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
756 e4197bf9 2018-08-18 stsp view, &views);
759 e5a0f69f 2018-08-18 stsp if (dead_view) {
760 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
762 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
763 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
764 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
765 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
766 669b5ffa 2018-10-07 stsp prev = view->parent;
768 669b5ffa 2018-10-07 stsp if (dead_view->parent)
769 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
771 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
773 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
774 d01904d4 2019-06-25 stsp if (err || (dead_view == main_view && new_view == NULL))
777 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
778 0cf4efb1 2018-09-29 stsp if (focus_view)
779 0cf4efb1 2018-09-29 stsp view = focus_view;
780 669b5ffa 2018-10-07 stsp else if (prev)
781 669b5ffa 2018-10-07 stsp view = prev;
782 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
783 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
784 0cf4efb1 2018-09-29 stsp tog_view_list_head);
786 0cf4efb1 2018-09-29 stsp view = NULL;
787 669b5ffa 2018-10-07 stsp if (view) {
788 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
789 669b5ffa 2018-10-07 stsp focus_view = view->child;
791 669b5ffa 2018-10-07 stsp focus_view = view;
795 bcbd79e2 2018-08-19 stsp if (new_view) {
796 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
797 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
798 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
799 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
801 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
802 86c66b02 2018-10-18 stsp err = view_close(v);
807 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
808 fed7eaa8 2018-10-24 stsp view = new_view;
809 878940b7 2018-09-29 stsp if (focus_view == NULL)
810 878940b7 2018-09-29 stsp focus_view = new_view;
812 0cf4efb1 2018-09-29 stsp if (focus_view) {
813 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
815 0cf4efb1 2018-09-29 stsp view->focussed = 0;
816 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
817 0cf4efb1 2018-09-29 stsp view = focus_view;
818 878940b7 2018-09-29 stsp if (new_view)
819 878940b7 2018-09-29 stsp show_panel(new_view->panel);
820 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
821 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
823 669b5ffa 2018-10-07 stsp if (view) {
824 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
825 758194b5 2018-10-24 stsp view->focussed = 1;
826 758194b5 2018-10-24 stsp show_panel(view->panel);
827 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
828 758194b5 2018-10-24 stsp show_panel(view->child->panel);
829 1a76625f 2018-10-22 stsp focus_view = view;
831 669b5ffa 2018-10-07 stsp if (view->parent) {
832 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
836 669b5ffa 2018-10-07 stsp err = view->show(view);
839 669b5ffa 2018-10-07 stsp if (view->child) {
840 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
844 1a76625f 2018-10-22 stsp update_panels();
845 1a76625f 2018-10-22 stsp doupdate();
849 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
850 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
851 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
852 e5a0f69f 2018-08-18 stsp view_close(view);
855 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
856 1a76625f 2018-10-22 stsp if (errcode)
857 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
859 e5a0f69f 2018-08-18 stsp return err;
862 4ed7e80c 2018-05-20 stsp __dead static void
863 9f7d7167 2018-04-29 stsp usage_log(void)
866 c70c5802 2018-08-01 stsp fprintf(stderr,
867 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
868 9f7d7167 2018-04-29 stsp getprogname());
872 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
873 80ddbec8 2018-04-29 stsp static const struct got_error *
874 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
876 00dfcb92 2018-06-11 stsp char *vis = NULL;
877 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
879 963b370f 2018-05-20 stsp *ws = NULL;
880 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
881 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
882 00dfcb92 2018-06-11 stsp int vislen;
883 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
884 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
886 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
887 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
889 00dfcb92 2018-06-11 stsp return err;
890 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
891 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
892 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
897 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
898 a7f50699 2018-06-11 stsp if (*ws == NULL) {
899 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
903 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
904 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
909 963b370f 2018-05-20 stsp *ws = NULL;
912 963b370f 2018-05-20 stsp return err;
915 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
916 963b370f 2018-05-20 stsp static const struct got_error *
917 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
919 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
920 963b370f 2018-05-20 stsp int cols = 0;
921 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
922 963b370f 2018-05-20 stsp size_t wlen;
925 963b370f 2018-05-20 stsp *wlinep = NULL;
926 b700b5d6 2018-07-10 stsp *widthp = 0;
928 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
930 963b370f 2018-05-20 stsp return err;
933 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
934 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
935 963b370f 2018-05-20 stsp switch (width) {
941 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
942 b700b5d6 2018-07-10 stsp cols += width;
946 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
947 67ceb59d 2019-03-03 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
951 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
955 963b370f 2018-05-20 stsp wline[i] = L'\0';
956 b700b5d6 2018-07-10 stsp if (widthp)
957 b700b5d6 2018-07-10 stsp *widthp = cols;
960 963b370f 2018-05-20 stsp free(wline);
962 963b370f 2018-05-20 stsp *wlinep = wline;
963 963b370f 2018-05-20 stsp return err;
966 8b473291 2019-02-21 stsp static const struct got_error*
967 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
968 8b473291 2019-02-21 stsp struct got_object_id *id)
970 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
971 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
973 8b473291 2019-02-21 stsp const char *name;
975 8b473291 2019-02-21 stsp *refs_str = NULL;
977 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
978 8b473291 2019-02-21 stsp if (got_object_id_cmp(re->id, id) != 0)
980 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
981 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
983 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
985 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
987 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
989 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
991 8b473291 2019-02-21 stsp s = *refs_str;
992 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
993 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
994 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
996 8b473291 2019-02-21 stsp *refs_str = NULL;
1002 8b473291 2019-02-21 stsp return err;
1005 963b370f 2018-05-20 stsp static const struct got_error *
1006 5813d178 2019-03-09 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1008 5813d178 2019-03-09 stsp char *smallerthan, *at;
1010 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1011 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1012 5813d178 2019-03-09 stsp author = smallerthan + 1;
1013 5813d178 2019-03-09 stsp at = strchr(author, '@');
1015 5813d178 2019-03-09 stsp *at = '\0';
1016 5813d178 2019-03-09 stsp return format_line(wauthor, author_width, author, limit);
1019 5813d178 2019-03-09 stsp static const struct got_error *
1020 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1021 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
1022 5813d178 2019-03-09 stsp int author_display_cols)
1024 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1025 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1026 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1027 5813d178 2019-03-09 stsp char *author = NULL;
1028 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1029 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1030 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1031 bb737323 2018-05-20 stsp int col, limit;
1032 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
1033 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1034 ccb26ccd 2018-11-05 stsp struct tm tm;
1035 45d799e2 2018-12-23 stsp time_t committer_time;
1037 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1038 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1039 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1040 ccb26ccd 2018-11-05 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1041 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1042 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1044 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
1045 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1047 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1048 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1049 b39d25c7 2018-07-10 stsp col = limit + 1;
1050 b39d25c7 2018-07-10 stsp if (col > avail)
1051 b39d25c7 2018-07-10 stsp goto done;
1053 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1054 5813d178 2019-03-09 stsp if (author == NULL) {
1055 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1056 80ddbec8 2018-04-29 stsp goto done;
1058 5813d178 2019-03-09 stsp err = format_author(&wauthor, &author_width, author, avail - col);
1060 bb737323 2018-05-20 stsp goto done;
1061 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1062 bb737323 2018-05-20 stsp col += author_width;
1063 5813d178 2019-03-09 stsp while (col <= avail && author_width < author_display_cols + 2) {
1064 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1066 bb737323 2018-05-20 stsp author_width++;
1068 9c2eaf34 2018-05-20 stsp if (col > avail)
1069 9c2eaf34 2018-05-20 stsp goto done;
1071 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1072 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
1073 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1074 6d9fbc00 2018-04-29 stsp goto done;
1076 bb737323 2018-05-20 stsp logmsg = logmsg0;
1077 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1079 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1080 bb737323 2018-05-20 stsp if (newline)
1081 bb737323 2018-05-20 stsp *newline = '\0';
1082 bb737323 2018-05-20 stsp limit = avail - col;
1083 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1085 bb737323 2018-05-20 stsp goto done;
1086 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1087 bb737323 2018-05-20 stsp col += logmsg_width;
1088 bb737323 2018-05-20 stsp while (col <= avail) {
1089 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1093 80ddbec8 2018-04-29 stsp free(logmsg0);
1094 bb737323 2018-05-20 stsp free(wlogmsg);
1095 5813d178 2019-03-09 stsp free(author);
1096 bb737323 2018-05-20 stsp free(wauthor);
1097 80ddbec8 2018-04-29 stsp free(line);
1098 80ddbec8 2018-04-29 stsp return err;
1101 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1102 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1103 899d86c2 2018-05-10 stsp struct got_object_id *id)
1105 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1107 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1108 80ddbec8 2018-04-29 stsp if (entry == NULL)
1109 899d86c2 2018-05-10 stsp return NULL;
1111 899d86c2 2018-05-10 stsp entry->id = id;
1112 99db9666 2018-05-07 stsp entry->commit = commit;
1113 899d86c2 2018-05-10 stsp return entry;
1116 99db9666 2018-05-07 stsp static void
1117 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1119 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1121 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1122 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1123 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1124 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1125 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1126 99db9666 2018-05-07 stsp free(entry);
1129 99db9666 2018-05-07 stsp static void
1130 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1132 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1133 99db9666 2018-05-07 stsp pop_commit(commits);
1136 c4972b91 2018-05-07 stsp static const struct got_error *
1137 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1138 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
1140 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1141 93e45b7c 2018-09-24 stsp int nqueued = 0;
1144 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1145 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1146 1a76625f 2018-10-22 stsp * while updating the display.
1148 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
1149 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1150 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1151 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1152 1a76625f 2018-10-22 stsp int errcode;
1154 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1155 9ba79e04 2018-06-11 stsp if (err) {
1156 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1158 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
1159 93e45b7c 2018-09-24 stsp minqueue, repo);
1161 ecb28ae0 2018-07-16 stsp return err;
1165 ecb28ae0 2018-07-16 stsp if (id == NULL)
1168 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1171 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1172 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1173 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1177 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1178 1a76625f 2018-10-22 stsp if (errcode) {
1179 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_lock");
1183 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1184 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1185 ecb28ae0 2018-07-16 stsp nqueued++;
1186 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1188 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1189 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1190 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1191 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1194 9ba79e04 2018-06-11 stsp return err;
1197 99db9666 2018-05-07 stsp static const struct got_error *
1198 19e70ad6 2019-05-14 stsp get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1199 19e70ad6 2019-05-14 stsp struct got_repository *repo)
1201 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1202 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1204 9ba79e04 2018-06-11 stsp *head_id = NULL;
1206 19e70ad6 2019-05-14 stsp err = got_ref_open(&head_ref, repo, branch_name, 0);
1208 99db9666 2018-05-07 stsp return err;
1210 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1211 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1212 9ba79e04 2018-06-11 stsp if (err) {
1213 9ba79e04 2018-06-11 stsp *head_id = NULL;
1214 99db9666 2018-05-07 stsp return err;
1217 9ba79e04 2018-06-11 stsp return NULL;
1220 0553a4e3 2018-04-30 stsp static const struct got_error *
1221 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1222 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1223 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1224 8b473291 2019-02-21 stsp struct got_reflist_head *refs, const char *path, int commits_needed)
1226 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1227 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1228 60493ae3 2019-06-20 stsp int width;
1229 60493ae3 2019-06-20 stsp int ncommits, author_cols = 10;
1230 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1231 8b473291 2019-02-21 stsp char *refs_str = NULL;
1232 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1234 e0d42f60 2018-07-22 stsp entry = first;
1235 e0d42f60 2018-07-22 stsp ncommits = 0;
1236 e0d42f60 2018-07-22 stsp while (entry) {
1237 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1238 e0d42f60 2018-07-22 stsp *selected = entry;
1241 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1242 e0d42f60 2018-07-22 stsp ncommits++;
1245 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1246 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1248 ecb28ae0 2018-07-16 stsp return err;
1249 8b473291 2019-02-21 stsp if (refs) {
1250 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, (*selected)->id);
1252 8b473291 2019-02-21 stsp goto done;
1256 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1257 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1259 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1260 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1261 60493ae3 2019-06-20 stsp commits_needed > 0 ?
1262 60493ae3 2019-06-20 stsp (view->searching && view->search_next_done == 0
1263 60493ae3 2019-06-20 stsp ? "searching..." : "loading... ") :
1264 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1265 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1266 8b473291 2019-02-21 stsp goto done;
1269 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1270 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1271 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1272 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1273 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1274 1a76625f 2018-10-22 stsp header = NULL;
1275 1a76625f 2018-10-22 stsp goto done;
1277 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1278 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1279 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1280 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1281 1a76625f 2018-10-22 stsp header = NULL;
1282 1a76625f 2018-10-22 stsp goto done;
1284 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1286 1a76625f 2018-10-22 stsp goto done;
1288 2814baeb 2018-08-01 stsp werase(view->window);
1290 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1291 a3404814 2018-09-02 stsp wstandout(view->window);
1292 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1293 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1294 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1297 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1298 a3404814 2018-09-02 stsp wstandend(view->window);
1299 ecb28ae0 2018-07-16 stsp free(wline);
1300 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1301 1a76625f 2018-10-22 stsp goto done;
1303 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1304 899d86c2 2018-05-10 stsp entry = first;
1305 5813d178 2019-03-09 stsp ncommits = 0;
1306 5813d178 2019-03-09 stsp while (entry) {
1307 5813d178 2019-03-09 stsp char *author;
1308 5813d178 2019-03-09 stsp wchar_t *wauthor;
1309 5813d178 2019-03-09 stsp int width;
1310 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1312 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1313 5813d178 2019-03-09 stsp if (author == NULL) {
1314 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1315 5813d178 2019-03-09 stsp goto done;
1317 5813d178 2019-03-09 stsp err = format_author(&wauthor, &width, author, COLS);
1318 5813d178 2019-03-09 stsp if (author_cols < width)
1319 5813d178 2019-03-09 stsp author_cols = width;
1320 5813d178 2019-03-09 stsp free(wauthor);
1321 5813d178 2019-03-09 stsp free(author);
1322 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1325 5813d178 2019-03-09 stsp entry = first;
1326 899d86c2 2018-05-10 stsp *last = first;
1327 867c6645 2018-07-10 stsp ncommits = 0;
1328 899d86c2 2018-05-10 stsp while (entry) {
1329 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1331 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1332 2814baeb 2018-08-01 stsp wstandout(view->window);
1333 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1334 5813d178 2019-03-09 stsp author_cols);
1335 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1336 2814baeb 2018-08-01 stsp wstandend(view->window);
1338 60493ae3 2019-06-20 stsp goto done;
1339 0553a4e3 2018-04-30 stsp ncommits++;
1340 899d86c2 2018-05-10 stsp *last = entry;
1341 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1344 1a57306a 2018-09-02 stsp view_vborder(view);
1346 1a76625f 2018-10-22 stsp free(id_str);
1347 8b473291 2019-02-21 stsp free(refs_str);
1348 1a76625f 2018-10-22 stsp free(ncommits_str);
1349 1a76625f 2018-10-22 stsp free(header);
1350 80ddbec8 2018-04-29 stsp return err;
1353 07b55e75 2018-05-10 stsp static void
1354 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1355 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1356 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1358 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1359 07b55e75 2018-05-10 stsp int nscrolled = 0;
1361 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1362 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1365 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1366 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1367 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1368 07b55e75 2018-05-10 stsp if (entry) {
1369 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1370 07b55e75 2018-05-10 stsp nscrolled++;
1375 aa075928 2018-05-10 stsp static const struct got_error *
1376 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1377 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1379 5e224a3e 2019-02-22 stsp int errcode;
1380 8a42fca8 2019-02-22 stsp int max_wait = 20;
1382 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1384 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1385 5e224a3e 2019-02-22 stsp if (*log_complete)
1388 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1389 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1390 7aafa0d1 2019-02-22 stsp if (errcode)
1391 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1392 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1393 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1394 7aafa0d1 2019-02-22 stsp if (errcode)
1395 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1396 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1397 7aafa0d1 2019-02-22 stsp pthread_yield();
1398 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1399 7aafa0d1 2019-02-22 stsp if (errcode)
1400 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1401 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1403 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1405 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1406 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1407 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1408 5e224a3e 2019-02-22 stsp * with few commits in history.
1410 7aafa0d1 2019-02-22 stsp return NULL;
1414 5e224a3e 2019-02-22 stsp return NULL;
1417 5e224a3e 2019-02-22 stsp static const struct got_error *
1418 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1419 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1420 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1421 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1422 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1424 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1425 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1426 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1428 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1429 5e224a3e 2019-02-22 stsp return NULL;
1431 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1432 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1434 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1435 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1437 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1438 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1439 5e224a3e 2019-02-22 stsp need_commits);
1441 5e224a3e 2019-02-22 stsp return err;
1445 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1446 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1449 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1451 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1452 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1454 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1455 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1457 dd0a52c1 2018-05-20 stsp return err;
1460 cd0acaa7 2018-05-20 stsp static const struct got_error *
1461 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1462 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1463 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1464 8b473291 2019-02-21 stsp struct got_repository *repo)
1466 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1467 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1468 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1470 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1471 15a94983 2018-12-23 stsp if (diff_view == NULL)
1472 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1474 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1475 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1476 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1477 e5a0f69f 2018-08-18 stsp if (err == NULL)
1478 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1479 cd0acaa7 2018-05-20 stsp return err;
1482 80ddbec8 2018-04-29 stsp static const struct got_error *
1483 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1484 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1486 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1488 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1489 941e9f74 2019-05-21 stsp if (parent == NULL)
1490 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1492 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1493 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1494 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1495 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1496 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1497 941e9f74 2019-05-21 stsp s->tree = subtree;
1498 941e9f74 2019-05-21 stsp s->entries = got_object_tree_get_entries(s->tree);
1499 941e9f74 2019-05-21 stsp s->selected = 0;
1500 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1501 941e9f74 2019-05-21 stsp return NULL;
1505 941e9f74 2019-05-21 stsp static const struct got_error *
1506 941e9f74 2019-05-21 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1507 941e9f74 2019-05-21 stsp struct commit_queue_entry *entry, const char *path,
1508 941e9f74 2019-05-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
1510 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1511 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1512 941e9f74 2019-05-21 stsp struct got_tree_entry *te;
1513 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s;
1514 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1515 b03c880f 2019-05-21 stsp char *slash, *subpath = NULL;
1516 941e9f74 2019-05-21 stsp const char *p;
1518 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1519 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1521 9343a5fb 2018-06-23 stsp return err;
1523 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1524 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1525 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1527 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1528 941e9f74 2019-05-21 stsp if (err) {
1529 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1530 941e9f74 2019-05-21 stsp return err;
1532 941e9f74 2019-05-21 stsp s = &tree_view->state.tree;
1534 941e9f74 2019-05-21 stsp *new_view = tree_view;
1536 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1538 941e9f74 2019-05-21 stsp while (*p) {
1539 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1541 941e9f74 2019-05-21 stsp while (p[0] == '/')
1544 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1545 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1546 941e9f74 2019-05-21 stsp if (slash == NULL)
1547 941e9f74 2019-05-21 stsp slash = strchr(p, '\0');
1548 941e9f74 2019-05-21 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1549 941e9f74 2019-05-21 stsp if (strncmp(p, te->name, slash - p) == 0) {
1550 941e9f74 2019-05-21 stsp s->selected_entry = te;
1553 b03c880f 2019-05-21 stsp s->selected++;
1555 941e9f74 2019-05-21 stsp if (s->selected_entry == NULL) {
1556 941e9f74 2019-05-21 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1559 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1560 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1562 67409a31 2019-05-24 stsp if (!S_ISDIR(s->selected_entry->mode)) {
1563 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1564 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1565 67409a31 2019-05-24 stsp s->selected = 0;
1569 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1570 941e9f74 2019-05-21 stsp if (slash)
1571 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1573 941e9f74 2019-05-21 stsp subpath = strdup(path);
1574 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1575 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1579 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1584 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1585 941e9f74 2019-05-21 stsp free(tree_id);
1589 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1590 941e9f74 2019-05-21 stsp if (err) {
1591 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1594 941e9f74 2019-05-21 stsp if (slash == NULL)
1596 941e9f74 2019-05-21 stsp free(subpath);
1597 941e9f74 2019-05-21 stsp subpath = NULL;
1598 941e9f74 2019-05-21 stsp p = slash;
1601 941e9f74 2019-05-21 stsp free(subpath);
1602 1a76625f 2018-10-22 stsp return err;
1605 1a76625f 2018-10-22 stsp static void *
1606 1a76625f 2018-10-22 stsp log_thread(void *arg)
1608 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1609 1a76625f 2018-10-22 stsp int errcode = 0;
1610 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1611 1a76625f 2018-10-22 stsp int done = 0;
1613 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1615 1a76625f 2018-10-22 stsp return (void *)err;
1617 1a76625f 2018-10-22 stsp while (!done && !err) {
1618 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1619 1a76625f 2018-10-22 stsp a->in_repo_path);
1620 1a76625f 2018-10-22 stsp if (err) {
1621 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1622 1a76625f 2018-10-22 stsp return (void *)err;
1623 1a76625f 2018-10-22 stsp err = NULL;
1625 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1626 1a76625f 2018-10-22 stsp a->commits_needed--;
1628 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1629 3abe8080 2019-04-10 stsp if (errcode) {
1630 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1631 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1633 3abe8080 2019-04-10 stsp } else if (*a->quit)
1635 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1636 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1637 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1638 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1642 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1643 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1644 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1645 1a76625f 2018-10-22 stsp &tog_mutex);
1646 1a76625f 2018-10-22 stsp if (errcode)
1647 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1648 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1651 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1652 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1653 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1654 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1656 3abe8080 2019-04-10 stsp a->log_complete = 1;
1657 1a76625f 2018-10-22 stsp return (void *)err;
1660 1a76625f 2018-10-22 stsp static const struct got_error *
1661 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1663 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1664 1a76625f 2018-10-22 stsp int errcode;
1666 1a76625f 2018-10-22 stsp if (s->thread) {
1667 1a76625f 2018-10-22 stsp s->quit = 1;
1668 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1669 1a76625f 2018-10-22 stsp if (errcode)
1670 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1671 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1672 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1673 1a76625f 2018-10-22 stsp if (errcode)
1674 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1675 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1676 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1677 1a76625f 2018-10-22 stsp if (errcode)
1678 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1679 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1680 1a76625f 2018-10-22 stsp if (errcode)
1681 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1682 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1683 1a76625f 2018-10-22 stsp s->thread = NULL;
1686 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1687 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1688 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1690 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1691 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1692 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1695 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1696 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1697 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1700 9343a5fb 2018-06-23 stsp return err;
1703 9343a5fb 2018-06-23 stsp static const struct got_error *
1704 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1706 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1707 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1709 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1710 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1711 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1712 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1713 1a76625f 2018-10-22 stsp free(s->start_id);
1714 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1715 1a76625f 2018-10-22 stsp return err;
1718 1a76625f 2018-10-22 stsp static const struct got_error *
1719 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
1721 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1723 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
1724 96e2b566 2019-07-08 stsp s->search_entry = NULL;
1725 60493ae3 2019-06-20 stsp return NULL;
1728 60493ae3 2019-06-20 stsp static int
1729 df0b3d8a 2019-06-28 stsp match_commit(struct got_commit_object *commit, const char *id_str,
1730 df0b3d8a 2019-06-28 stsp regex_t *regex)
1732 60493ae3 2019-06-20 stsp regmatch_t regmatch;
1734 60493ae3 2019-06-20 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1735 60493ae3 2019-06-20 stsp ®match, 0) == 0 ||
1736 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1737 60493ae3 2019-06-20 stsp ®match, 0) == 0 ||
1738 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_logmsg(commit), 1,
1739 df0b3d8a 2019-06-28 stsp ®match, 0) == 0 ||
1740 df0b3d8a 2019-06-28 stsp regexec(regex, id_str, 1, ®match, 0) == 0)
1746 60493ae3 2019-06-20 stsp static const struct got_error *
1747 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
1749 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
1750 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1751 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
1753 60493ae3 2019-06-20 stsp if (!view->searching) {
1754 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1755 60493ae3 2019-06-20 stsp return NULL;
1758 96e2b566 2019-07-08 stsp if (s->search_entry) {
1759 678cbce5 2019-07-28 stsp if (wgetch(view->window) == KEY_BACKSPACE) {
1760 678cbce5 2019-07-28 stsp view->search_next_done = 1;
1761 678cbce5 2019-07-28 stsp return NULL;
1763 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
1764 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
1766 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
1767 96e2b566 2019-07-08 stsp commit_queue_head, entry);
1768 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
1769 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1770 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
1772 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
1773 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
1775 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1776 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
1778 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1781 60493ae3 2019-06-20 stsp while (1) {
1782 df0b3d8a 2019-06-28 stsp char *id_str;
1783 60493ae3 2019-06-20 stsp if (entry == NULL) {
1784 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
1785 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
1786 f801134a 2019-06-25 stsp view->search_next_done = 1;
1787 f801134a 2019-06-25 stsp return NULL;
1790 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
1791 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
1792 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
1794 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
1795 57b33b64 2019-07-08 stsp return trigger_log_thread(1,
1796 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
1797 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
1798 f801134a 2019-06-25 stsp &s->thread_args.need_commits);
1801 df0b3d8a 2019-06-28 stsp err = got_object_id_str(&id_str, entry->id);
1803 df0b3d8a 2019-06-28 stsp return err;
1805 df0b3d8a 2019-06-28 stsp if (match_commit(entry->commit, id_str, &view->regex)) {
1806 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1807 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
1808 df0b3d8a 2019-06-28 stsp free(id_str);
1811 df0b3d8a 2019-06-28 stsp free(id_str);
1812 96e2b566 2019-07-08 stsp s->search_entry = entry;
1813 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1814 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
1816 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1819 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
1820 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
1821 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
1822 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1824 60493ae3 2019-06-20 stsp return err;
1827 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
1828 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1830 60493ae3 2019-06-20 stsp return err;
1835 96e2b566 2019-07-08 stsp s->search_entry = NULL;
1837 60493ae3 2019-06-20 stsp return NULL;
1840 60493ae3 2019-06-20 stsp static const struct got_error *
1841 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1842 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
1843 d01904d4 2019-06-25 stsp const char *head_ref_name, const char *path, int check_disk)
1845 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1846 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1847 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1848 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1849 1a76625f 2018-10-22 stsp int errcode;
1851 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1852 ecb28ae0 2018-07-16 stsp if (err != NULL)
1853 ecb28ae0 2018-07-16 stsp goto done;
1855 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1856 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1857 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1859 8b473291 2019-02-21 stsp s->refs = refs;
1860 fb2756b9 2018-08-04 stsp s->repo = repo;
1861 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
1862 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1863 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1864 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1865 5036bf37 2018-09-24 stsp goto done;
1868 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1869 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1870 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1871 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
1872 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
1874 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1876 1a76625f 2018-10-22 stsp goto done;
1877 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1878 1a76625f 2018-10-22 stsp 0, thread_repo);
1880 1a76625f 2018-10-22 stsp goto done;
1882 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1883 1a76625f 2018-10-22 stsp if (errcode) {
1884 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
1885 1a76625f 2018-10-22 stsp goto done;
1888 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1889 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1890 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1891 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1892 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1893 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1894 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1895 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1896 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1897 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1898 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1901 1a76625f 2018-10-22 stsp close_log_view(view);
1902 ba4f502b 2018-08-04 stsp return err;
1905 e5a0f69f 2018-08-18 stsp static const struct got_error *
1906 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1908 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1910 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1911 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1912 2b380cc8 2018-10-24 stsp &s->thread_args);
1913 2b380cc8 2018-10-24 stsp if (errcode)
1914 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
1917 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1918 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1919 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
1920 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1923 e5a0f69f 2018-08-18 stsp static const struct got_error *
1924 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1925 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1927 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1928 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1929 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
1930 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1931 669b5ffa 2018-10-07 stsp int begin_x = 0;
1932 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
1934 e5a0f69f 2018-08-18 stsp switch (ch) {
1936 1e37a5c2 2019-05-12 jcs s->quit = 1;
1939 1e37a5c2 2019-05-12 jcs case KEY_UP:
1942 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1944 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
1945 1e37a5c2 2019-05-12 jcs s->selected--;
1947 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
1948 1144d21a 2019-06-21 stsp &s->commits);
1950 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
1951 a4292ac5 2019-05-12 jcs case CTRL('b'):
1952 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1954 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
1955 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
1956 1e37a5c2 2019-05-12 jcs s->selected = 0;
1959 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
1960 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
1963 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
1966 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1968 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
1969 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1970 1e37a5c2 2019-05-12 jcs s->selected++;
1973 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
1974 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
1975 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
1976 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1977 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1979 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
1980 a4292ac5 2019-05-12 jcs case CTRL('f'): {
1981 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
1982 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
1983 1e37a5c2 2019-05-12 jcs if (first == NULL)
1985 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
1986 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
1987 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
1988 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1989 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1990 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
1991 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
1992 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1993 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
1994 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
1995 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
1997 1e37a5c2 2019-05-12 jcs err = NULL;
2000 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2001 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2002 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2003 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2004 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2006 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2009 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2011 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2012 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2013 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2014 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2015 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
2018 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2019 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2021 1e37a5c2 2019-05-12 jcs return err;
2022 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2024 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2027 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2028 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2030 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2033 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2035 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2036 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2037 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2038 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2041 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2042 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2044 1e37a5c2 2019-05-12 jcs return err;
2045 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2047 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2050 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2051 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2053 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2055 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2056 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2058 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2059 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2060 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2062 1e37a5c2 2019-05-12 jcs return err;
2063 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2064 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2065 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2066 638f9024 2019-05-13 stsp return got_error_from_errno(
2067 1e37a5c2 2019-05-12 jcs "view_open");
2068 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2069 d01904d4 2019-06-25 stsp s->repo, s->head_ref_name, parent_path, 0);
2071 1e37a5c2 2019-05-12 jcs return err;;
2072 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2073 1e37a5c2 2019-05-12 jcs *new_view = lv;
2075 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2076 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2078 1e37a5c2 2019-05-12 jcs return NULL;
2081 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2082 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2084 d01904d4 2019-06-25 stsp return err;
2085 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2086 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2087 d01904d4 2019-06-25 stsp if (lv == NULL)
2088 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2089 d01904d4 2019-06-25 stsp err = get_head_commit_id(&start_id, s->head_ref_name ?
2090 d01904d4 2019-06-25 stsp s->head_ref_name : GOT_REF_HEAD, s->repo);
2092 d01904d4 2019-06-25 stsp return err;
2093 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2094 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2095 d01904d4 2019-06-25 stsp free(start_id);
2096 d01904d4 2019-06-25 stsp return got_error_from_errno("strdup");
2098 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2099 d01904d4 2019-06-25 stsp s->head_ref_name, in_repo_path, 0);
2101 d01904d4 2019-06-25 stsp return err;;
2102 d01904d4 2019-06-25 stsp *dead_view = view;
2103 d01904d4 2019-06-25 stsp *new_view = lv;
2109 80ddbec8 2018-04-29 stsp return err;
2112 4ed7e80c 2018-05-20 stsp static const struct got_error *
2113 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2115 c2db6724 2019-01-04 stsp const struct got_error *error;
2117 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2118 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2119 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2121 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2122 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2124 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2125 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2127 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2128 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2130 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2131 c2db6724 2019-01-04 stsp if (error != NULL)
2132 c2db6724 2019-01-04 stsp return error;
2134 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2135 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2137 c2db6724 2019-01-04 stsp return NULL;
2140 a915003a 2019-02-05 stsp static void
2141 a915003a 2019-02-05 stsp init_curses(void)
2143 a915003a 2019-02-05 stsp initscr();
2145 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2148 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2149 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2150 a915003a 2019-02-05 stsp curs_set(0);
2151 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2154 c2db6724 2019-01-04 stsp static const struct got_error *
2155 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2157 80ddbec8 2018-04-29 stsp const struct got_error *error;
2158 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2159 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2160 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2161 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2162 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2163 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
2165 04cc582a 2018-08-01 stsp struct tog_view *view;
2167 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2169 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2170 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2171 c2db6724 2019-01-04 stsp NULL) == -1)
2172 80ddbec8 2018-04-29 stsp err(1, "pledge");
2175 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2176 80ddbec8 2018-04-29 stsp switch (ch) {
2178 80ddbec8 2018-04-29 stsp start_commit = optarg;
2181 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2182 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2183 ecb28ae0 2018-07-16 stsp err(1, "-r option");
2186 17020d27 2019-03-07 stsp usage_log();
2187 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2191 80ddbec8 2018-04-29 stsp argc -= optind;
2192 80ddbec8 2018-04-29 stsp argv += optind;
2194 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2195 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2196 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2197 ecb28ae0 2018-07-16 stsp goto done;
2199 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2200 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2201 963f97a1 2019-03-18 stsp goto done;
2202 963f97a1 2019-03-18 stsp error = NULL;
2204 963f97a1 2019-03-18 stsp if (argc == 0) {
2205 963f97a1 2019-03-18 stsp path = strdup("");
2206 963f97a1 2019-03-18 stsp if (path == NULL) {
2207 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2208 ecb28ae0 2018-07-16 stsp goto done;
2210 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2211 963f97a1 2019-03-18 stsp if (worktree) {
2212 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2214 963f97a1 2019-03-18 stsp if (error)
2215 963f97a1 2019-03-18 stsp goto done;
2217 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2218 963f97a1 2019-03-18 stsp if (path == NULL) {
2219 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2220 963f97a1 2019-03-18 stsp goto done;
2224 963f97a1 2019-03-18 stsp usage_log();
2226 963f97a1 2019-03-18 stsp repo_path = worktree ?
2227 963f97a1 2019-03-18 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2228 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2229 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2230 963f97a1 2019-03-18 stsp goto done;
2233 a915003a 2019-02-05 stsp init_curses();
2235 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
2236 80ddbec8 2018-04-29 stsp if (error != NULL)
2237 ecb28ae0 2018-07-16 stsp goto done;
2239 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2240 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2241 c02c541e 2019-03-29 stsp if (error)
2242 c02c541e 2019-03-29 stsp goto done;
2244 15a94983 2018-12-23 stsp if (start_commit == NULL)
2245 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&start_id, worktree ?
2246 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2249 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&start_id, start_commit, repo);
2250 f2b6a97d 2019-07-15 stsp if (error) {
2251 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
2252 f2b6a97d 2019-07-15 stsp goto done;
2253 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&start_id,
2254 f2b6a97d 2019-07-15 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2257 80ddbec8 2018-04-29 stsp if (error != NULL)
2258 8b473291 2019-02-21 stsp goto done;
2260 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2261 8b473291 2019-02-21 stsp if (error)
2262 ecb28ae0 2018-07-16 stsp goto done;
2264 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2265 04cc582a 2018-08-01 stsp if (view == NULL) {
2266 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2267 04cc582a 2018-08-01 stsp goto done;
2269 d01904d4 2019-06-25 stsp error = open_log_view(view, start_id, &refs, repo, worktree ?
2270 d01904d4 2019-06-25 stsp got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2271 ba4f502b 2018-08-04 stsp if (error)
2272 ba4f502b 2018-08-04 stsp goto done;
2273 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2275 ecb28ae0 2018-07-16 stsp free(repo_path);
2276 ecb28ae0 2018-07-16 stsp free(cwd);
2277 ecb28ae0 2018-07-16 stsp free(path);
2278 899d86c2 2018-05-10 stsp free(start_id);
2280 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2281 ec142235 2019-03-07 stsp if (worktree)
2282 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2283 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2284 80ddbec8 2018-04-29 stsp return error;
2287 4ed7e80c 2018-05-20 stsp __dead static void
2288 9f7d7167 2018-04-29 stsp usage_diff(void)
2291 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2292 9f7d7167 2018-04-29 stsp getprogname());
2296 b304db33 2018-05-20 stsp static char *
2297 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2299 b304db33 2018-05-20 stsp char *line;
2300 b304db33 2018-05-20 stsp size_t linelen;
2301 b304db33 2018-05-20 stsp size_t lineno;
2302 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2304 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2306 b304db33 2018-05-20 stsp *len = linelen;
2307 b304db33 2018-05-20 stsp return line;
2310 4ed7e80c 2018-05-20 stsp static const struct got_error *
2311 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2312 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
2313 c3e9aa98 2019-05-13 jcs char *header)
2315 61e69b96 2018-05-20 stsp const struct got_error *err;
2316 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2317 b304db33 2018-05-20 stsp char *line;
2318 b304db33 2018-05-20 stsp size_t len;
2319 61e69b96 2018-05-20 stsp wchar_t *wline;
2320 e0b650dd 2018-05-20 stsp int width;
2322 26ed57b2 2018-05-19 stsp rewind(f);
2323 f7d12f7e 2018-08-01 stsp werase(view->window);
2325 a3404814 2018-09-02 stsp if (header) {
2326 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
2327 a3404814 2018-09-02 stsp if (err) {
2328 a3404814 2018-09-02 stsp return err;
2331 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2332 a3404814 2018-09-02 stsp wstandout(view->window);
2333 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2334 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2335 a3404814 2018-09-02 stsp wstandend(view->window);
2336 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2337 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2339 a3404814 2018-09-02 stsp if (max_lines <= 1)
2340 a3404814 2018-09-02 stsp return NULL;
2341 a3404814 2018-09-02 stsp max_lines--;
2345 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2346 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2347 26ed57b2 2018-05-19 stsp if (line == NULL) {
2351 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2352 26ed57b2 2018-05-19 stsp free(line);
2356 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2357 61e69b96 2018-05-20 stsp if (err) {
2358 61e69b96 2018-05-20 stsp free(line);
2359 61e69b96 2018-05-20 stsp return err;
2361 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2362 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2363 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2364 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2365 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2366 26ed57b2 2018-05-19 stsp free(line);
2367 2550e4c3 2018-07-13 stsp free(wline);
2368 2550e4c3 2018-07-13 stsp wline = NULL;
2370 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2372 1a57306a 2018-09-02 stsp view_vborder(view);
2374 c3e9aa98 2019-05-13 jcs if (*eof) {
2375 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2376 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2377 c3e9aa98 2019-05-13 jcs nprinted++;
2380 c3e9aa98 2019-05-13 jcs err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2382 c3e9aa98 2019-05-13 jcs return err;
2385 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2386 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2387 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2390 26ed57b2 2018-05-19 stsp return NULL;
2393 abd2672a 2018-12-23 stsp static char *
2394 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2396 abd2672a 2018-12-23 stsp char *p, *s = ctime_r(time, datebuf);
2397 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2399 abd2672a 2018-12-23 stsp *p = '\0';
2403 4ed7e80c 2018-05-20 stsp static const struct got_error *
2404 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2405 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2407 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2408 abd2672a 2018-12-23 stsp char datebuf[26];
2409 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2410 15a94983 2018-12-23 stsp char *id_str = NULL;
2411 45d799e2 2018-12-23 stsp time_t committer_time;
2412 45d799e2 2018-12-23 stsp const char *author, *committer;
2413 8b473291 2019-02-21 stsp char *refs_str = NULL;
2415 8b473291 2019-02-21 stsp if (refs) {
2416 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, commit_id);
2418 8b473291 2019-02-21 stsp return err;
2421 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2423 abd2672a 2018-12-23 stsp return err;
2425 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2426 15a94983 2018-12-23 stsp if (err) {
2427 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2428 15a94983 2018-12-23 stsp goto done;
2431 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2432 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2433 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2434 abd2672a 2018-12-23 stsp goto done;
2436 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2437 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2438 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2439 abd2672a 2018-12-23 stsp goto done;
2441 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2442 abd2672a 2018-12-23 stsp if (fprintf(outfile, "date: %s UTC\n",
2443 45d799e2 2018-12-23 stsp get_datestr(&committer_time, datebuf)) < 0) {
2444 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2445 abd2672a 2018-12-23 stsp goto done;
2447 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2448 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2449 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2450 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2451 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2452 abd2672a 2018-12-23 stsp goto done;
2454 45d799e2 2018-12-23 stsp if (fprintf(outfile, "%s\n",
2455 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(commit)) < 0) {
2456 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2457 abd2672a 2018-12-23 stsp goto done;
2460 abd2672a 2018-12-23 stsp free(id_str);
2461 8b473291 2019-02-21 stsp free(refs_str);
2462 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2463 abd2672a 2018-12-23 stsp return err;
2466 abd2672a 2018-12-23 stsp static const struct got_error *
2467 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2469 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2470 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2471 15a94983 2018-12-23 stsp int obj_type;
2473 511a516b 2018-05-19 stsp f = got_opentemp();
2474 48ae06ee 2018-10-18 stsp if (f == NULL) {
2475 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2476 48ae06ee 2018-10-18 stsp goto done;
2478 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2479 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2480 fb43ecf1 2019-02-11 stsp goto done;
2484 15a94983 2018-12-23 stsp if (s->id1)
2485 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2487 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2489 15a94983 2018-12-23 stsp goto done;
2491 15a94983 2018-12-23 stsp switch (obj_type) {
2492 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2493 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2494 54156555 2018-12-24 stsp s->diff_context, s->repo, f);
2496 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2497 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2498 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
2500 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2501 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2502 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2503 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2505 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2508 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2509 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2510 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2512 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2513 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2514 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2515 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2516 8b473291 2019-02-21 stsp s->repo, f);
2521 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2523 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2524 15a94983 2018-12-23 stsp s->diff_context, s->repo, f);
2528 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2532 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2533 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2534 48ae06ee 2018-10-18 stsp return err;
2537 f5215bb9 2019-02-22 stsp static void
2538 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2540 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2541 f5215bb9 2019-02-22 stsp update_panels();
2542 f5215bb9 2019-02-22 stsp doupdate();
2545 48ae06ee 2018-10-18 stsp static const struct got_error *
2546 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2547 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2548 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2550 48ae06ee 2018-10-18 stsp const struct got_error *err;
2552 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2553 15a94983 2018-12-23 stsp int type1, type2;
2554 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2556 15a94983 2018-12-23 stsp return err;
2557 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2559 15a94983 2018-12-23 stsp return err;
2561 15a94983 2018-12-23 stsp if (type1 != type2)
2562 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2565 15a94983 2018-12-23 stsp if (id1) {
2566 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2567 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2568 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2570 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2572 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2573 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2574 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2575 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2576 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2578 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2579 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2580 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2581 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2582 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2583 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2584 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2586 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2587 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2588 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2590 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2591 48ae06ee 2018-10-18 stsp if (err) {
2592 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2593 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2594 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2595 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2596 48ae06ee 2018-10-18 stsp return err;
2599 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2600 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2601 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2603 5dc9f4bc 2018-08-04 stsp return NULL;
2606 e5a0f69f 2018-08-18 stsp static const struct got_error *
2607 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2609 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2611 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2612 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2613 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2614 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2615 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2616 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2617 e5a0f69f 2018-08-18 stsp return err;
2620 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2621 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2623 a3404814 2018-09-02 stsp const struct got_error *err;
2624 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2625 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2627 a3404814 2018-09-02 stsp if (s->id1) {
2628 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2630 a3404814 2018-09-02 stsp return err;
2632 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2634 a3404814 2018-09-02 stsp return err;
2636 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2637 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2638 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2639 a3404814 2018-09-02 stsp free(id_str1);
2640 a3404814 2018-09-02 stsp free(id_str2);
2641 a3404814 2018-09-02 stsp return err;
2643 a3404814 2018-09-02 stsp free(id_str1);
2644 a3404814 2018-09-02 stsp free(id_str2);
2646 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2647 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2651 15a087fe 2019-02-21 stsp static const struct got_error *
2652 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2653 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2655 d7a04538 2019-02-21 stsp const struct got_error *err;
2656 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
2657 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
2658 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
2660 15a087fe 2019-02-21 stsp free(s->id2);
2661 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2662 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2663 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2665 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2667 d7a04538 2019-02-21 stsp return err;
2668 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
2669 15a087fe 2019-02-21 stsp free(s->id1);
2670 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
2671 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2672 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
2673 15a087fe 2019-02-21 stsp return NULL;
2676 0cf4efb1 2018-09-29 stsp static const struct got_error *
2677 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2678 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2680 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2681 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2682 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
2683 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
2686 e5a0f69f 2018-08-18 stsp switch (ch) {
2688 1e37a5c2 2019-05-12 jcs case KEY_UP:
2689 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
2690 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2692 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2693 a60a9dc4 2019-05-13 jcs case CTRL('b'):
2694 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
2697 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
2698 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
2699 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2702 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2703 1e37a5c2 2019-05-12 jcs if (!s->eof)
2704 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2706 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
2707 a60a9dc4 2019-05-13 jcs case CTRL('f'):
2709 00ba99a7 2019-05-12 jcs if (s->eof)
2712 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
2713 1e37a5c2 2019-05-12 jcs char *line;
2714 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
2715 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2716 1e37a5c2 2019-05-12 jcs if (line == NULL)
2721 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
2722 1e37a5c2 2019-05-12 jcs s->diff_context--;
2723 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2724 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2728 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2729 1e37a5c2 2019-05-12 jcs s->diff_context++;
2730 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2731 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2736 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2738 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2739 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
2740 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
2741 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2744 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2749 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2753 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2754 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2756 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2757 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2761 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2763 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2765 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2766 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
2768 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
2769 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
2770 1e37a5c2 2019-05-12 jcs update_panels();
2771 1e37a5c2 2019-05-12 jcs doupdate();
2773 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
2774 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
2775 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
2776 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
2780 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2785 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
2786 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2789 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2793 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2794 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2796 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2797 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2803 bcbd79e2 2018-08-19 stsp return err;
2806 4ed7e80c 2018-05-20 stsp static const struct got_error *
2807 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2809 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2810 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2811 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2812 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2813 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2814 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2816 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2818 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2820 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2821 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2822 eb6600df 2019-01-04 stsp NULL) == -1)
2823 26ed57b2 2018-05-19 stsp err(1, "pledge");
2826 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2827 26ed57b2 2018-05-19 stsp switch (ch) {
2829 17020d27 2019-03-07 stsp usage_diff();
2830 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2834 26ed57b2 2018-05-19 stsp argc -= optind;
2835 26ed57b2 2018-05-19 stsp argv += optind;
2837 26ed57b2 2018-05-19 stsp if (argc == 0) {
2838 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2839 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2840 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2841 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2842 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2843 15a94983 2018-12-23 stsp id_str1 = argv[0];
2844 15a94983 2018-12-23 stsp id_str2 = argv[1];
2845 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2846 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2847 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2848 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2849 15a94983 2018-12-23 stsp id_str1 = argv[1];
2850 15a94983 2018-12-23 stsp id_str2 = argv[2];
2852 26ed57b2 2018-05-19 stsp usage_diff();
2854 a915003a 2019-02-05 stsp init_curses();
2856 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2857 eb6600df 2019-01-04 stsp if (error)
2858 eb6600df 2019-01-04 stsp goto done;
2860 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
2861 26ed57b2 2018-05-19 stsp if (error)
2862 26ed57b2 2018-05-19 stsp goto done;
2864 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
2865 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
2866 26ed57b2 2018-05-19 stsp if (error)
2867 26ed57b2 2018-05-19 stsp goto done;
2869 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
2870 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
2871 26ed57b2 2018-05-19 stsp if (error)
2872 26ed57b2 2018-05-19 stsp goto done;
2874 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2875 8b473291 2019-02-21 stsp if (error)
2876 8b473291 2019-02-21 stsp goto done;
2878 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2879 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2880 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2881 ea5e7bb5 2018-08-01 stsp goto done;
2883 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2884 5dc9f4bc 2018-08-04 stsp if (error)
2885 5dc9f4bc 2018-08-04 stsp goto done;
2886 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2888 c02c541e 2019-03-29 stsp free(repo_path);
2890 921be706 2019-06-28 stsp got_repo_close(repo);
2891 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2892 26ed57b2 2018-05-19 stsp return error;
2895 4ed7e80c 2018-05-20 stsp __dead static void
2896 9f7d7167 2018-04-29 stsp usage_blame(void)
2899 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2900 9f7d7167 2018-04-29 stsp getprogname());
2904 84451b3e 2018-07-10 stsp struct tog_blame_line {
2905 84451b3e 2018-07-10 stsp int annotated;
2906 84451b3e 2018-07-10 stsp struct got_object_id *id;
2909 4ed7e80c 2018-05-20 stsp static const struct got_error *
2910 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2911 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2912 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2913 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2915 84451b3e 2018-07-10 stsp const struct got_error *err;
2916 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2917 84451b3e 2018-07-10 stsp char *line;
2918 84451b3e 2018-07-10 stsp size_t len;
2919 84451b3e 2018-07-10 stsp wchar_t *wline;
2920 b700b5d6 2018-07-10 stsp int width, wlimit;
2921 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2922 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2923 ab089a2a 2018-07-12 stsp char *id_str;
2925 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2927 ab089a2a 2018-07-12 stsp return err;
2929 84451b3e 2018-07-10 stsp rewind(f);
2930 f7d12f7e 2018-08-01 stsp werase(view->window);
2932 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
2933 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2934 ab089a2a 2018-07-12 stsp free(id_str);
2935 ab089a2a 2018-07-12 stsp return err;
2938 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2939 ab089a2a 2018-07-12 stsp free(line);
2940 2550e4c3 2018-07-13 stsp line = NULL;
2941 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2942 a3404814 2018-09-02 stsp wstandout(view->window);
2943 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2944 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2945 a3404814 2018-09-02 stsp wstandend(view->window);
2946 2550e4c3 2018-07-13 stsp free(wline);
2947 2550e4c3 2018-07-13 stsp wline = NULL;
2948 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2949 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2951 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2952 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2953 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
2954 ab089a2a 2018-07-12 stsp free(id_str);
2955 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2957 ab089a2a 2018-07-12 stsp free(id_str);
2958 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2959 3f60a8ef 2018-07-10 stsp free(line);
2960 2550e4c3 2018-07-13 stsp line = NULL;
2962 3f60a8ef 2018-07-10 stsp return err;
2963 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2964 2550e4c3 2018-07-13 stsp free(wline);
2965 2550e4c3 2018-07-13 stsp wline = NULL;
2966 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2967 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2970 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2971 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2972 84451b3e 2018-07-10 stsp if (line == NULL) {
2976 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2977 84451b3e 2018-07-10 stsp free(line);
2981 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2982 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2983 84451b3e 2018-07-10 stsp if (err) {
2984 84451b3e 2018-07-10 stsp free(line);
2985 84451b3e 2018-07-10 stsp return err;
2988 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2989 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2991 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2992 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2993 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2994 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2995 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2996 84451b3e 2018-07-10 stsp char *id_str;
2997 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2998 84451b3e 2018-07-10 stsp if (err) {
2999 84451b3e 2018-07-10 stsp free(line);
3000 2550e4c3 2018-07-13 stsp free(wline);
3001 84451b3e 2018-07-10 stsp return err;
3003 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
3004 84451b3e 2018-07-10 stsp free(id_str);
3005 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
3007 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
3008 ee41ec32 2018-07-10 stsp prev_id = NULL;
3011 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3012 b700b5d6 2018-07-10 stsp while (width < wlimit) {
3013 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
3016 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3017 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3018 84451b3e 2018-07-10 stsp if (++nprinted == 1)
3019 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
3020 84451b3e 2018-07-10 stsp free(line);
3021 2550e4c3 2018-07-13 stsp free(wline);
3022 2550e4c3 2018-07-13 stsp wline = NULL;
3024 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
3026 1a57306a 2018-09-02 stsp view_vborder(view);
3028 84451b3e 2018-07-10 stsp return NULL;
3031 84451b3e 2018-07-10 stsp static const struct got_error *
3032 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3034 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
3035 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
3036 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
3037 1a76625f 2018-10-22 stsp int errcode;
3039 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
3040 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3041 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3043 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3044 1a76625f 2018-10-22 stsp if (errcode)
3045 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3047 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3048 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3049 d68a0a7d 2018-07-10 stsp goto done;
3052 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3053 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3055 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3056 d68a0a7d 2018-07-10 stsp if (line->annotated)
3057 d68a0a7d 2018-07-10 stsp goto done;
3059 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3060 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3061 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3062 84451b3e 2018-07-10 stsp goto done;
3064 84451b3e 2018-07-10 stsp line->annotated = 1;
3066 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3067 1a76625f 2018-10-22 stsp if (errcode)
3068 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3069 84451b3e 2018-07-10 stsp return err;
3072 84451b3e 2018-07-10 stsp static void *
3073 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3075 18430de3 2018-07-10 stsp const struct got_error *err;
3076 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3077 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3078 1a76625f 2018-10-22 stsp int errcode;
3080 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3081 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
3083 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3084 1a76625f 2018-10-22 stsp if (errcode)
3085 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3086 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3088 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3089 c9beca56 2018-07-22 stsp ta->repo = NULL;
3090 c9beca56 2018-07-22 stsp *ta->complete = 1;
3092 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3093 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3094 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3096 18430de3 2018-07-10 stsp return (void *)err;
3099 245d91c1 2018-07-12 stsp static struct got_object_id *
3100 15a94983 2018-12-23 stsp get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3101 15a94983 2018-12-23 stsp int selected_line)
3103 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3105 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3106 245d91c1 2018-07-12 stsp if (!line->annotated)
3107 245d91c1 2018-07-12 stsp return NULL;
3109 245d91c1 2018-07-12 stsp return line->id;
3112 b880a918 2018-07-10 stsp static const struct got_error *
3113 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3115 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3118 245d91c1 2018-07-12 stsp if (blame->thread) {
3119 1a76625f 2018-10-22 stsp int errcode;
3120 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3121 1a76625f 2018-10-22 stsp if (errcode)
3122 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3123 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3124 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3125 1a76625f 2018-10-22 stsp if (errcode)
3126 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3127 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3128 1a76625f 2018-10-22 stsp if (errcode)
3129 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3130 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3131 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3132 245d91c1 2018-07-12 stsp err = NULL;
3133 245d91c1 2018-07-12 stsp blame->thread = NULL;
3135 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3136 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3137 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3139 245d91c1 2018-07-12 stsp if (blame->f) {
3140 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3141 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3142 245d91c1 2018-07-12 stsp blame->f = NULL;
3144 57670559 2018-12-23 stsp if (blame->lines) {
3145 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3146 57670559 2018-12-23 stsp free(blame->lines[i].id);
3147 57670559 2018-12-23 stsp free(blame->lines);
3148 57670559 2018-12-23 stsp blame->lines = NULL;
3150 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3151 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3153 245d91c1 2018-07-12 stsp return err;
3156 245d91c1 2018-07-12 stsp static const struct got_error *
3157 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3158 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3159 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3160 245d91c1 2018-07-12 stsp struct got_repository *repo)
3162 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3163 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3164 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3165 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3166 15a94983 2018-12-23 stsp int obj_type;
3168 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3170 15a94983 2018-12-23 stsp return err;
3171 15a94983 2018-12-23 stsp if (obj_id == NULL)
3172 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
3174 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3176 84451b3e 2018-07-10 stsp goto done;
3178 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3179 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3180 84451b3e 2018-07-10 stsp goto done;
3183 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3185 a70480e0 2018-06-23 stsp goto done;
3186 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3187 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3188 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3189 84451b3e 2018-07-10 stsp goto done;
3191 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3192 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
3194 84451b3e 2018-07-10 stsp goto done;
3196 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3197 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3198 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3199 84451b3e 2018-07-10 stsp goto done;
3202 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3204 bd24772e 2018-07-11 stsp goto done;
3206 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3207 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3208 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3209 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3210 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3211 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3212 245d91c1 2018-07-12 stsp goto done;
3214 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3216 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3217 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3218 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3219 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3220 245d91c1 2018-07-12 stsp *blame_complete = 0;
3224 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3225 27d434c2 2018-09-15 stsp free(obj_id);
3227 245d91c1 2018-07-12 stsp stop_blame(blame);
3228 245d91c1 2018-07-12 stsp return err;
3231 245d91c1 2018-07-12 stsp static const struct got_error *
3232 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3233 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3234 8b473291 2019-02-21 stsp struct got_repository *repo)
3236 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3237 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3239 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3241 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3243 7cbe629d 2018-08-04 stsp return err;
3245 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3246 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3247 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3248 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3249 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3250 fb2756b9 2018-08-04 stsp s->path = path;
3251 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
3252 638f9024 2019-05-13 stsp return got_error_from_errno("open_blame_view");
3253 fb2756b9 2018-08-04 stsp s->repo = repo;
3254 8b473291 2019-02-21 stsp s->refs = refs;
3255 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3256 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3258 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3259 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3260 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3261 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
3262 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
3264 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3265 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3266 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3267 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3270 e5a0f69f 2018-08-18 stsp static const struct got_error *
3271 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3273 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3274 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3276 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3277 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3279 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3280 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3281 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3282 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3283 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3286 e5a0f69f 2018-08-18 stsp free(s->path);
3288 e5a0f69f 2018-08-18 stsp return err;
3291 7cbe629d 2018-08-04 stsp static const struct got_error *
3292 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
3294 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3296 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
3297 6c4c42e0 2019-06-24 stsp return NULL;
3300 6c4c42e0 2019-06-24 stsp static int
3301 6c4c42e0 2019-06-24 stsp match_line(const char *line, regex_t *regex)
3303 6c4c42e0 2019-06-24 stsp regmatch_t regmatch;
3305 6c4c42e0 2019-06-24 stsp return regexec(regex, line, 1, ®match, 0) == 0;
3309 6c4c42e0 2019-06-24 stsp static const struct got_error *
3310 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
3312 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3313 6c4c42e0 2019-06-24 stsp int lineno;
3315 6c4c42e0 2019-06-24 stsp if (!view->searching) {
3316 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3317 6c4c42e0 2019-06-24 stsp return NULL;
3320 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3321 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3322 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
3324 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
3326 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3327 6c4c42e0 2019-06-24 stsp lineno = 1;
3329 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3332 6c4c42e0 2019-06-24 stsp while (1) {
3333 6c4c42e0 2019-06-24 stsp char *line = NULL;
3334 6c4c42e0 2019-06-24 stsp off_t offset;
3335 6c4c42e0 2019-06-24 stsp size_t len;
3337 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
3338 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
3339 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3340 6c4c42e0 2019-06-24 stsp free(line);
3344 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3345 6c4c42e0 2019-06-24 stsp lineno = 1;
3347 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3350 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
3351 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3352 6c4c42e0 2019-06-24 stsp free(line);
3353 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
3355 6c4c42e0 2019-06-24 stsp free(line);
3356 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
3357 2246482e 2019-06-25 stsp if (line && match_line(line, &view->regex)) {
3358 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3359 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
3360 6c4c42e0 2019-06-24 stsp free(line);
3363 6c4c42e0 2019-06-24 stsp free(line);
3364 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3370 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3371 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
3372 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
3375 6c4c42e0 2019-06-24 stsp return NULL;
3378 6c4c42e0 2019-06-24 stsp static const struct got_error *
3379 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
3381 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3382 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
3383 2b380cc8 2018-10-24 stsp int errcode;
3385 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
3386 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3387 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
3388 2b380cc8 2018-10-24 stsp if (errcode)
3389 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3392 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3393 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3394 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
3395 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
3397 669b5ffa 2018-10-07 stsp view_vborder(view);
3398 e5a0f69f 2018-08-18 stsp return err;
3401 e5a0f69f 2018-08-18 stsp static const struct got_error *
3402 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3403 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3405 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
3406 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
3407 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3408 669b5ffa 2018-10-07 stsp int begin_x = 0;
3410 e5a0f69f 2018-08-18 stsp switch (ch) {
3412 1e37a5c2 2019-05-12 jcs s->done = 1;
3415 1e37a5c2 2019-05-12 jcs case KEY_UP:
3416 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
3417 1e37a5c2 2019-05-12 jcs s->selected_line--;
3418 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
3419 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3420 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3422 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3423 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
3424 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3427 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3428 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3429 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3431 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3434 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3435 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3436 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3437 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3438 1e37a5c2 2019-05-12 jcs s->selected_line++;
3439 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3440 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3441 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3444 1e37a5c2 2019-05-12 jcs case 'p': {
3445 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3446 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3447 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3448 1e37a5c2 2019-05-12 jcs if (id == NULL)
3450 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3451 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3452 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3453 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3454 1e37a5c2 2019-05-12 jcs int obj_type;
3455 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3456 1e37a5c2 2019-05-12 jcs s->repo, id);
3459 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3460 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3461 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3462 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3465 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3466 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3467 1e37a5c2 2019-05-12 jcs pid->id, s->path);
3468 e5a0f69f 2018-08-18 stsp if (err) {
3469 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
3470 1e37a5c2 2019-05-12 jcs err = NULL;
3471 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3474 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
3476 1e37a5c2 2019-05-12 jcs free(blob_id);
3477 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
3478 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
3479 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3482 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3484 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3486 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
3487 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
3489 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3494 1e37a5c2 2019-05-12 jcs s->done = 1;
3495 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3496 1e37a5c2 2019-05-12 jcs s->done = 0;
3497 1e37a5c2 2019-05-12 jcs if (thread_err)
3499 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3500 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
3501 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3502 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3503 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
3504 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
3509 1e37a5c2 2019-05-12 jcs case 'B': {
3510 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
3511 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
3512 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
3514 1e37a5c2 2019-05-12 jcs s->done = 1;
3515 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3516 1e37a5c2 2019-05-12 jcs s->done = 0;
3517 1e37a5c2 2019-05-12 jcs if (thread_err)
3519 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3520 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
3521 1e37a5c2 2019-05-12 jcs s->blamed_commit =
3522 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
3523 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3524 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3525 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
3526 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
3531 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3532 1e37a5c2 2019-05-12 jcs case '\r': {
3533 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3534 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
3535 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
3536 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3537 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3538 1e37a5c2 2019-05-12 jcs if (id == NULL)
3540 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
3543 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
3544 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
3545 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3546 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3547 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3548 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
3549 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3550 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
3553 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
3554 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
3555 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3557 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3560 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3561 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3564 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
3566 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3569 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
3570 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
3572 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3577 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3579 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3580 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
3581 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
3584 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3585 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
3586 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3587 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3590 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
3591 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
3592 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
3593 1e37a5c2 2019-05-12 jcs view->nlines - 2;
3595 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
3596 1e37a5c2 2019-05-12 jcs s->blame.nlines -
3597 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
3599 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3600 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
3601 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3602 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3608 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
3611 a70480e0 2018-06-23 stsp static const struct got_error *
3612 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
3614 a70480e0 2018-06-23 stsp const struct got_error *error;
3615 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
3616 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3617 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
3618 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3619 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3620 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
3622 e1cd8fed 2018-08-01 stsp struct tog_view *view;
3624 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3626 a70480e0 2018-06-23 stsp #ifndef PROFILE
3627 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3628 8e94dd5b 2019-01-04 stsp NULL) == -1)
3629 a70480e0 2018-06-23 stsp err(1, "pledge");
3632 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3633 a70480e0 2018-06-23 stsp switch (ch) {
3635 a70480e0 2018-06-23 stsp commit_id_str = optarg;
3638 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3639 69069811 2018-08-02 stsp if (repo_path == NULL)
3640 69069811 2018-08-02 stsp err(1, "-r option");
3643 17020d27 2019-03-07 stsp usage_blame();
3644 a70480e0 2018-06-23 stsp /* NOTREACHED */
3648 a70480e0 2018-06-23 stsp argc -= optind;
3649 a70480e0 2018-06-23 stsp argv += optind;
3651 69069811 2018-08-02 stsp if (argc == 1)
3652 69069811 2018-08-02 stsp path = argv[0];
3654 a70480e0 2018-06-23 stsp usage_blame();
3656 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
3657 69069811 2018-08-02 stsp if (cwd == NULL) {
3658 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3659 69069811 2018-08-02 stsp goto done;
3661 69069811 2018-08-02 stsp if (repo_path == NULL) {
3662 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3663 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3664 69069811 2018-08-02 stsp goto done;
3666 eb41ed75 2019-02-05 stsp error = NULL;
3667 eb41ed75 2019-02-05 stsp if (worktree) {
3668 eb41ed75 2019-02-05 stsp repo_path =
3669 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3670 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
3671 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3672 eb41ed75 2019-02-05 stsp if (error)
3673 eb41ed75 2019-02-05 stsp goto done;
3675 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
3676 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
3677 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3678 eb41ed75 2019-02-05 stsp goto done;
3683 a915003a 2019-02-05 stsp init_curses();
3685 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
3686 c02c541e 2019-03-29 stsp if (error != NULL)
3687 8e94dd5b 2019-01-04 stsp goto done;
3689 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3690 c02c541e 2019-03-29 stsp if (error)
3691 92205607 2019-01-04 stsp goto done;
3693 eb41ed75 2019-02-05 stsp if (worktree) {
3694 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3695 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3696 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3697 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3698 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3699 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3700 eb41ed75 2019-02-05 stsp path) == -1) {
3701 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3702 eb41ed75 2019-02-05 stsp goto done;
3704 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3707 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3709 eb41ed75 2019-02-05 stsp if (error)
3710 69069811 2018-08-02 stsp goto done;
3712 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
3713 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
3714 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3715 a70480e0 2018-06-23 stsp if (error != NULL)
3716 66b4983c 2018-06-23 stsp goto done;
3717 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3718 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
3720 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&commit_id, commit_id_str, repo);
3721 f2b6a97d 2019-07-15 stsp if (error) {
3722 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
3723 f2b6a97d 2019-07-15 stsp goto done;
3724 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&commit_id,
3725 f2b6a97d 2019-07-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3728 a19e88aa 2018-06-23 stsp if (error != NULL)
3729 8b473291 2019-02-21 stsp goto done;
3731 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
3732 8b473291 2019-02-21 stsp if (error)
3733 a19e88aa 2018-06-23 stsp goto done;
3735 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3736 e1cd8fed 2018-08-01 stsp if (view == NULL) {
3737 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3738 e1cd8fed 2018-08-01 stsp goto done;
3740 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3741 7cbe629d 2018-08-04 stsp if (error)
3742 7cbe629d 2018-08-04 stsp goto done;
3743 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3745 69069811 2018-08-02 stsp free(repo_path);
3746 69069811 2018-08-02 stsp free(cwd);
3747 a70480e0 2018-06-23 stsp free(commit_id);
3748 eb41ed75 2019-02-05 stsp if (worktree)
3749 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
3751 a70480e0 2018-06-23 stsp got_repo_close(repo);
3752 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3753 a70480e0 2018-06-23 stsp return error;
3756 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3757 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
3758 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
3759 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
3760 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
3761 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
3762 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
3764 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3765 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
3766 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
3767 ffd1d5e5 2018-06-23 stsp int width, n;
3769 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
3771 f7d12f7e 2018-08-01 stsp werase(view->window);
3773 ffd1d5e5 2018-06-23 stsp if (limit == 0)
3774 ffd1d5e5 2018-06-23 stsp return NULL;
3776 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
3778 ffd1d5e5 2018-06-23 stsp return err;
3779 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3780 a3404814 2018-09-02 stsp wstandout(view->window);
3781 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3782 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3783 a3404814 2018-09-02 stsp wstandend(view->window);
3784 2550e4c3 2018-07-13 stsp free(wline);
3785 2550e4c3 2018-07-13 stsp wline = NULL;
3786 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3787 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3788 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3789 ffd1d5e5 2018-06-23 stsp return NULL;
3790 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
3792 ce52c690 2018-06-23 stsp return err;
3793 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3794 2550e4c3 2018-07-13 stsp free(wline);
3795 2550e4c3 2018-07-13 stsp wline = NULL;
3796 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3797 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3798 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3799 ffd1d5e5 2018-06-23 stsp return NULL;
3800 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3801 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
3802 a1eca9bb 2018-06-23 stsp return NULL;
3804 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3805 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
3806 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
3807 0cf4efb1 2018-09-29 stsp if (view->focussed)
3808 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3809 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
3811 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
3812 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
3813 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3814 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3815 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3816 ffd1d5e5 2018-06-23 stsp return NULL;
3820 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
3821 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3824 ffd1d5e5 2018-06-23 stsp while (te) {
3825 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
3827 1d13200f 2018-07-12 stsp if (show_ids) {
3828 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
3830 638f9024 2019-05-13 stsp return got_error_from_errno(
3831 230a42bd 2019-05-11 jcs "got_object_id_str");
3833 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3834 d8355ef1 2019-02-10 stsp te->name, S_ISDIR(te->mode) ? "/" :
3835 d8355ef1 2019-02-10 stsp ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3836 1d13200f 2018-07-12 stsp free(id_str);
3837 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3839 1d13200f 2018-07-12 stsp free(id_str);
3840 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
3841 ffd1d5e5 2018-06-23 stsp if (err) {
3842 ffd1d5e5 2018-06-23 stsp free(line);
3845 ffd1d5e5 2018-06-23 stsp if (n == selected) {
3846 0cf4efb1 2018-09-29 stsp if (view->focussed)
3847 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3848 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
3850 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3851 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3852 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3853 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
3854 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3855 ffd1d5e5 2018-06-23 stsp free(line);
3856 2550e4c3 2018-07-13 stsp free(wline);
3857 2550e4c3 2018-07-13 stsp wline = NULL;
3859 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3860 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
3861 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3863 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3866 ffd1d5e5 2018-06-23 stsp return err;
3869 ffd1d5e5 2018-06-23 stsp static void
3870 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
3871 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
3872 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
3874 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
3877 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
3880 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3881 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
3882 ffd1d5e5 2018-06-23 stsp if (!isroot)
3883 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3887 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
3888 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
3889 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
3890 ffd1d5e5 2018-06-23 stsp prev = te;
3891 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3893 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
3894 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3896 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3897 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3900 768394f3 2019-01-24 stsp static int
3901 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3902 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
3903 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
3905 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
3906 ffd1d5e5 2018-06-23 stsp int n = 0;
3908 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
3909 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3911 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
3912 768394f3 2019-01-24 stsp last = last_displayed_entry;
3913 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
3914 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
3915 768394f3 2019-01-24 stsp if (last) {
3916 768394f3 2019-01-24 stsp *first_displayed_entry = next;
3917 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
3923 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3924 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
3925 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
3927 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
3928 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
3929 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
3931 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
3932 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
3934 ce52c690 2018-06-23 stsp len += strlen(te->name);
3936 ce52c690 2018-06-23 stsp *path = calloc(1, len);
3937 ffd1d5e5 2018-06-23 stsp if (path == NULL)
3938 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
3940 ce52c690 2018-06-23 stsp (*path)[0] = '/';
3941 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
3942 d9765a41 2018-06-23 stsp while (pt) {
3943 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3944 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3945 cb2ebc8a 2018-06-23 stsp goto done;
3947 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
3948 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3949 cb2ebc8a 2018-06-23 stsp goto done;
3951 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3954 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3955 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3956 ce52c690 2018-06-23 stsp goto done;
3960 ce52c690 2018-06-23 stsp if (err) {
3961 ce52c690 2018-06-23 stsp free(*path);
3962 ce52c690 2018-06-23 stsp *path = NULL;
3964 ce52c690 2018-06-23 stsp return err;
3967 ce52c690 2018-06-23 stsp static const struct got_error *
3968 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3969 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3970 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3971 8b473291 2019-02-21 stsp struct got_repository *repo)
3973 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3974 ce52c690 2018-06-23 stsp char *path;
3975 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3977 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3979 ce52c690 2018-06-23 stsp return err;
3981 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3982 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3983 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3985 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
3986 e5a0f69f 2018-08-18 stsp if (err) {
3987 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3988 e5a0f69f 2018-08-18 stsp free(path);
3990 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3991 69efd4c4 2018-07-18 stsp return err;
3994 69efd4c4 2018-07-18 stsp static const struct got_error *
3995 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3996 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3997 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3998 8b473291 2019-02-21 stsp struct got_repository *repo)
4000 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
4001 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
4002 69efd4c4 2018-07-18 stsp char *path;
4004 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4005 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
4006 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
4008 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
4010 69efd4c4 2018-07-18 stsp return err;
4012 d01904d4 2019-06-25 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4014 e5a0f69f 2018-08-18 stsp view_close(log_view);
4016 e5a0f69f 2018-08-18 stsp *new_view = log_view;
4017 cb2ebc8a 2018-06-23 stsp free(path);
4018 cb2ebc8a 2018-06-23 stsp return err;
4021 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4022 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
4023 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4024 8b473291 2019-02-21 stsp struct got_repository *repo)
4026 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4027 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
4028 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4030 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
4032 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
4033 ffd1d5e5 2018-06-23 stsp if (err != NULL)
4034 ffd1d5e5 2018-06-23 stsp goto done;
4036 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4037 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4038 ffd1d5e5 2018-06-23 stsp goto done;
4041 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
4042 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
4043 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4044 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
4045 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
4046 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4047 6484ec90 2018-09-29 stsp goto done;
4049 8b473291 2019-02-21 stsp s->refs = refs;
4050 fb2756b9 2018-08-04 stsp s->repo = repo;
4052 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
4053 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
4054 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
4055 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
4056 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
4058 ad80ab7b 2018-08-04 stsp free(commit_id_str);
4059 6484ec90 2018-09-29 stsp if (err) {
4060 fb2756b9 2018-08-04 stsp free(s->tree_label);
4061 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4063 ad80ab7b 2018-08-04 stsp return err;
4066 e5a0f69f 2018-08-18 stsp static const struct got_error *
4067 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
4069 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4071 fb2756b9 2018-08-04 stsp free(s->tree_label);
4072 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4073 6484ec90 2018-09-29 stsp free(s->commit_id);
4074 6484ec90 2018-09-29 stsp s->commit_id = NULL;
4075 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
4076 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
4077 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
4078 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
4079 ad80ab7b 2018-08-04 stsp free(parent);
4082 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
4083 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
4084 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
4086 7c32bd05 2019-06-22 stsp return NULL;
4089 7c32bd05 2019-06-22 stsp static const struct got_error *
4090 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
4092 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4094 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
4095 7c32bd05 2019-06-22 stsp return NULL;
4098 7c32bd05 2019-06-22 stsp static int
4099 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4101 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
4103 7c32bd05 2019-06-22 stsp return regexec(regex, te->name, 1, ®match, 0) == 0;
4106 7c32bd05 2019-06-22 stsp static const struct got_error *
4107 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
4109 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4110 7c32bd05 2019-06-22 stsp struct got_tree_entry *entry, *te;
4112 7c32bd05 2019-06-22 stsp if (!view->searching) {
4113 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4114 7c32bd05 2019-06-22 stsp return NULL;
4117 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4118 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
4119 7c32bd05 2019-06-22 stsp if (s->selected_entry)
4120 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4122 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4125 7c32bd05 2019-06-22 stsp if (s->selected_entry == NULL) {
4126 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4127 7c32bd05 2019-06-22 stsp entry = te;
4129 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4130 7c32bd05 2019-06-22 stsp entry = te;
4131 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) ==
4132 7c32bd05 2019-06-22 stsp s->selected_entry)
4138 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4139 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4141 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4142 7c32bd05 2019-06-22 stsp entry = te;
4146 7c32bd05 2019-06-22 stsp while (1) {
4147 7c32bd05 2019-06-22 stsp if (entry == NULL) {
4148 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
4149 ac66afb8 2019-06-24 stsp view->search_next_done = 1;
4150 ac66afb8 2019-06-24 stsp return NULL;
4152 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4153 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4155 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4156 7c32bd05 2019-06-22 stsp entry = te;
4160 7c32bd05 2019-06-22 stsp if (match_tree_entry(entry, &view->regex)) {
4161 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4162 7c32bd05 2019-06-22 stsp s->matched_entry = entry;
4166 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4167 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(entry, entry);
4169 7c32bd05 2019-06-22 stsp if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4170 7c32bd05 2019-06-22 stsp entry = NULL;
4172 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4173 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) == entry) {
4174 7c32bd05 2019-06-22 stsp entry = te;
4182 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4183 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
4184 7c32bd05 2019-06-22 stsp s->selected = 0;
4187 e5a0f69f 2018-08-18 stsp return NULL;
4190 ad80ab7b 2018-08-04 stsp static const struct got_error *
4191 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
4193 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
4194 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4195 e5a0f69f 2018-08-18 stsp char *parent_path;
4197 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
4199 e5a0f69f 2018-08-18 stsp return err;
4201 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
4202 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
4203 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4204 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
4205 e5a0f69f 2018-08-18 stsp free(parent_path);
4207 669b5ffa 2018-10-07 stsp view_vborder(view);
4208 e5a0f69f 2018-08-18 stsp return err;
4211 e5a0f69f 2018-08-18 stsp static const struct got_error *
4212 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4213 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4215 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4216 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
4217 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
4218 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
4220 e5a0f69f 2018-08-18 stsp switch (ch) {
4222 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
4225 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
4227 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4228 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4229 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
4230 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
4231 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
4232 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4233 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4235 1e37a5c2 2019-05-12 jcs return err;
4236 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
4238 1e37a5c2 2019-05-12 jcs view_close(log_view);
4241 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
4242 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4244 1e37a5c2 2019-05-12 jcs *new_view = log_view;
4247 1e37a5c2 2019-05-12 jcs case KEY_UP:
4248 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
4249 1e37a5c2 2019-05-12 jcs s->selected--;
4250 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
4253 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
4255 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
4256 1e37a5c2 2019-05-12 jcs s->entries, s->tree == s->root);
4258 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4259 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
4260 1e37a5c2 2019-05-12 jcs MAX(0, view->nlines - 4 - s->selected), s->entries,
4261 1e37a5c2 2019-05-12 jcs s->tree == s->root);
4262 1e37a5c2 2019-05-12 jcs s->selected = 0;
4263 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_FIRST(&s->entries->head) ==
4264 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
4265 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
4268 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4269 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
4270 1e37a5c2 2019-05-12 jcs s->selected++;
4273 00ba99a7 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4274 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
4276 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
4277 1e37a5c2 2019-05-12 jcs s->last_displayed_entry, s->entries);
4279 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4280 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4282 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
4283 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
4284 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4287 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
4288 1e37a5c2 2019-05-12 jcs view->nlines, s->last_displayed_entry, s->entries);
4289 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
4290 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
4291 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
4292 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
4294 1e37a5c2 2019-05-12 jcs ndisplayed++;
4295 1e37a5c2 2019-05-12 jcs te = SIMPLEQ_NEXT(te, entry);
4296 1e37a5c2 2019-05-12 jcs } while (te);
4297 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
4300 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4302 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4303 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4304 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
4305 1e37a5c2 2019-05-12 jcs /* user selected '..' */
4306 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
4308 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
4309 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
4311 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
4312 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
4313 1e37a5c2 2019-05-12 jcs s->entries =
4314 1e37a5c2 2019-05-12 jcs got_object_tree_get_entries(s->tree);
4315 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
4316 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
4317 1e37a5c2 2019-05-12 jcs s->selected_entry =
4318 1e37a5c2 2019-05-12 jcs parent->selected_entry;
4319 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
4320 1e37a5c2 2019-05-12 jcs free(parent);
4321 1e37a5c2 2019-05-12 jcs } else if (S_ISDIR(s->selected_entry->mode)) {
4322 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
4323 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&subtree,
4324 1e37a5c2 2019-05-12 jcs s->repo, s->selected_entry->id);
4327 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
4328 941e9f74 2019-05-21 stsp if (err) {
4329 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
4332 1e37a5c2 2019-05-12 jcs } else if (S_ISREG(s->selected_entry->mode)) {
4333 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
4334 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
4335 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
4337 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
4338 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
4339 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
4342 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
4343 669b5ffa 2018-10-07 stsp err = view_close_child(view);
4345 669b5ffa 2018-10-07 stsp return err;
4346 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
4347 669b5ffa 2018-10-07 stsp if (err) {
4348 1e37a5c2 2019-05-12 jcs view_close(blame_view);
4351 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
4352 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
4354 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
4357 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4358 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
4359 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4365 ffd1d5e5 2018-06-23 stsp return err;
4368 ffd1d5e5 2018-06-23 stsp __dead static void
4369 ffd1d5e5 2018-06-23 stsp usage_tree(void)
4372 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4373 ffd1d5e5 2018-06-23 stsp getprogname());
4377 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4378 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
4380 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
4381 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
4382 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4383 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
4384 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4385 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
4386 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
4387 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
4389 5221c383 2018-08-01 stsp struct tog_view *view;
4391 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4393 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
4394 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4395 d188b9a6 2019-01-04 stsp NULL) == -1)
4396 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
4399 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
4400 ffd1d5e5 2018-06-23 stsp switch (ch) {
4402 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
4405 17020d27 2019-03-07 stsp usage_tree();
4406 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
4410 ffd1d5e5 2018-06-23 stsp argc -= optind;
4411 ffd1d5e5 2018-06-23 stsp argv += optind;
4413 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
4414 52185f70 2019-02-05 stsp struct got_worktree *worktree;
4415 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
4416 52185f70 2019-02-05 stsp if (cwd == NULL)
4417 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
4418 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4419 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4420 52185f70 2019-02-05 stsp goto done;
4421 52185f70 2019-02-05 stsp if (worktree) {
4422 52185f70 2019-02-05 stsp free(cwd);
4423 52185f70 2019-02-05 stsp repo_path =
4424 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4425 52185f70 2019-02-05 stsp got_worktree_close(worktree);
4427 52185f70 2019-02-05 stsp repo_path = cwd;
4428 52185f70 2019-02-05 stsp if (repo_path == NULL) {
4429 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4430 52185f70 2019-02-05 stsp goto done;
4432 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
4433 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
4434 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
4435 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
4437 ffd1d5e5 2018-06-23 stsp usage_log();
4439 a915003a 2019-02-05 stsp init_curses();
4441 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
4442 c02c541e 2019-03-29 stsp if (error != NULL)
4443 52185f70 2019-02-05 stsp goto done;
4445 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4446 c02c541e 2019-03-29 stsp if (error)
4447 52185f70 2019-02-05 stsp goto done;
4449 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
4450 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4452 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4453 f2b6a97d 2019-07-15 stsp if (error) {
4454 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
4455 f2b6a97d 2019-07-15 stsp goto done;
4456 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&commit_id,
4457 f2b6a97d 2019-07-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4460 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4461 ffd1d5e5 2018-06-23 stsp goto done;
4463 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4464 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4465 ffd1d5e5 2018-06-23 stsp goto done;
4467 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
4468 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
4469 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4470 8b473291 2019-02-21 stsp goto done;
4472 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
4473 8b473291 2019-02-21 stsp if (error)
4474 ffd1d5e5 2018-06-23 stsp goto done;
4476 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4477 5221c383 2018-08-01 stsp if (view == NULL) {
4478 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4479 5221c383 2018-08-01 stsp goto done;
4481 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
4482 ad80ab7b 2018-08-04 stsp if (error)
4483 ad80ab7b 2018-08-04 stsp goto done;
4484 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4486 52185f70 2019-02-05 stsp free(repo_path);
4487 ffd1d5e5 2018-06-23 stsp free(commit_id);
4488 ffd1d5e5 2018-06-23 stsp if (commit)
4489 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
4491 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
4493 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
4494 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4495 ffd1d5e5 2018-06-23 stsp return error;
4498 ce5b7c56 2019-07-09 stsp static void
4499 ce5b7c56 2019-07-09 stsp list_commands(void)
4503 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
4504 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
4505 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
4506 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->name);
4508 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
4511 4ed7e80c 2018-05-20 stsp __dead static void
4512 ce5b7c56 2019-07-09 stsp usage(int hflag)
4514 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4515 53ccebc2 2019-07-30 stsp getprogname());
4516 ce5b7c56 2019-07-09 stsp if (hflag)
4517 ce5b7c56 2019-07-09 stsp list_commands();
4521 c2301be8 2018-04-30 stsp static char **
4522 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
4524 c2301be8 2018-04-30 stsp char **argv;
4525 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
4527 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
4528 c2301be8 2018-04-30 stsp if (argv == NULL)
4529 c2301be8 2018-04-30 stsp err(1, "calloc");
4530 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
4531 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
4532 c2301be8 2018-04-30 stsp err(1, "calloc");
4533 c2301be8 2018-04-30 stsp if (arg1) {
4534 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
4535 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
4536 c2301be8 2018-04-30 stsp err(1, "calloc");
4539 c2301be8 2018-04-30 stsp return argv;
4543 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
4545 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
4546 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
4547 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
4548 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
4550 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
4552 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
4553 9f7d7167 2018-04-29 stsp switch (ch) {
4555 9f7d7167 2018-04-29 stsp hflag = 1;
4558 53ccebc2 2019-07-30 stsp Vflag = 1;
4561 ce5b7c56 2019-07-09 stsp usage(hflag);
4562 9f7d7167 2018-04-29 stsp /* NOTREACHED */
4566 9f7d7167 2018-04-29 stsp argc -= optind;
4567 9f7d7167 2018-04-29 stsp argv += optind;
4568 9f7d7167 2018-04-29 stsp optind = 0;
4569 c2301be8 2018-04-30 stsp optreset = 1;
4571 53ccebc2 2019-07-30 stsp if (Vflag) {
4572 53ccebc2 2019-07-30 stsp got_version_print_str();
4576 c2301be8 2018-04-30 stsp if (argc == 0) {
4577 f29d3e89 2018-06-23 stsp if (hflag)
4578 ce5b7c56 2019-07-09 stsp usage(hflag);
4579 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
4580 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
4581 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
4586 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
4587 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
4588 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
4589 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
4590 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
4595 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
4596 d70c3147 2019-07-09 stsp fprintf(stderr, "%s: unknown command '%s'\n",
4597 3642c4c6 2019-07-09 stsp getprogname(), argv[0]);
4598 ce5b7c56 2019-07-09 stsp list_commands();
4603 3642c4c6 2019-07-09 stsp if (hflag)
4604 3642c4c6 2019-07-09 stsp cmd->cmd_usage();
4606 3642c4c6 2019-07-09 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4609 c2301be8 2018-04-30 stsp free(cmd_argv);
4610 9f7d7167 2018-04-29 stsp if (error)
4611 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);