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 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
49 6fb7cd11 2019-08-22 stsp #include "got_cancel.h"
50 6fb7cd11 2019-08-22 stsp #include "got_commit_graph.h"
51 a70480e0 2018-06-23 stsp #include "got_blame.h"
52 c2db6724 2019-01-04 stsp #include "got_privsep.h"
53 1dd54920 2019-05-11 stsp #include "got_path.h"
54 b7165be3 2019-02-05 stsp #include "got_worktree.h"
57 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
64 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
66 9f7d7167 2018-04-29 stsp #ifndef nitems
67 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 9f7d7167 2018-04-29 stsp struct tog_cmd {
71 c2301be8 2018-04-30 stsp const char *name;
72 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
73 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
76 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
77 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
78 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
79 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
80 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
82 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
83 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
84 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
85 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
87 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
88 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
89 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
90 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
91 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
94 d6b05b5b 2018-08-04 stsp enum tog_view_type {
95 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
96 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
97 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
98 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
101 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
103 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
104 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
105 ba4f502b 2018-08-04 stsp struct got_object_id *id;
106 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
109 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
110 ba4f502b 2018-08-04 stsp struct commit_queue {
111 ba4f502b 2018-08-04 stsp int ncommits;
112 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
115 6d17833f 2019-11-08 stsp struct tog_line_color {
116 6d17833f 2019-11-08 stsp SIMPLEQ_ENTRY(tog_line_color) entry;
117 6d17833f 2019-11-08 stsp regex_t regex;
118 6d17833f 2019-11-08 stsp short colorpair;
120 6d17833f 2019-11-08 stsp SIMPLEQ_HEAD(tog_line_colors, tog_line_color);
122 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
123 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
125 15a087fe 2019-02-21 stsp int first_displayed_line;
126 15a087fe 2019-02-21 stsp int last_displayed_line;
128 15a087fe 2019-02-21 stsp int diff_context;
129 15a087fe 2019-02-21 stsp struct got_repository *repo;
130 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
131 6d17833f 2019-11-08 stsp struct tog_line_colors line_colors;
133 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
134 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
137 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
139 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
140 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
141 1a76625f 2018-10-22 stsp int commits_needed;
142 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
143 1a76625f 2018-10-22 stsp struct commit_queue *commits;
144 1a76625f 2018-10-22 stsp const char *in_repo_path;
145 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
146 1a76625f 2018-10-22 stsp struct got_repository *repo;
147 1a76625f 2018-10-22 stsp int log_complete;
148 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
149 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
150 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
151 13add988 2019-10-15 stsp int *searching;
152 13add988 2019-10-15 stsp int *search_next_done;
153 13add988 2019-10-15 stsp regex_t *regex;
156 1a76625f 2018-10-22 stsp struct tog_log_view_state {
157 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
158 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
159 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
160 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
161 b01e7d3b 2018-08-04 stsp int selected;
162 b01e7d3b 2018-08-04 stsp char *in_repo_path;
163 d01904d4 2019-06-25 stsp const char *head_ref_name;
164 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
165 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
166 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
167 1a76625f 2018-10-22 stsp sig_atomic_t quit;
168 1a76625f 2018-10-22 stsp pthread_t thread;
169 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
170 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
171 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
174 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
175 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
176 e9424729 2018-08-04 stsp int nlines;
178 e9424729 2018-08-04 stsp struct tog_view *view;
179 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
183 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
184 e9424729 2018-08-04 stsp const char *path;
185 e9424729 2018-08-04 stsp struct got_repository *repo;
186 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
187 e9424729 2018-08-04 stsp int *complete;
188 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
189 fc06ba56 2019-08-22 stsp void *cancel_arg;
192 e9424729 2018-08-04 stsp struct tog_blame {
194 e9424729 2018-08-04 stsp size_t filesize;
195 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
196 6fcac457 2018-11-19 stsp int nlines;
197 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
198 e9424729 2018-08-04 stsp pthread_t thread;
199 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
200 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
201 e9424729 2018-08-04 stsp const char *path;
204 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
205 7cbe629d 2018-08-04 stsp int first_displayed_line;
206 7cbe629d 2018-08-04 stsp int last_displayed_line;
207 7cbe629d 2018-08-04 stsp int selected_line;
208 7cbe629d 2018-08-04 stsp int blame_complete;
211 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
212 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
213 e5a0f69f 2018-08-18 stsp char *path;
214 7cbe629d 2018-08-04 stsp struct got_repository *repo;
215 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
216 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
217 e9424729 2018-08-04 stsp struct tog_blame blame;
218 6c4c42e0 2019-06-24 stsp int matched_line;
221 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
222 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
223 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
224 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
225 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
226 ad80ab7b 2018-08-04 stsp int selected;
229 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
231 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
232 ad80ab7b 2018-08-04 stsp char *tree_label;
233 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
234 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
235 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
236 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
237 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
238 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
239 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
240 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
241 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
242 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
243 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
244 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
245 c0b01bdb 2019-11-08 stsp struct tog_line_colors line_colors;
249 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
251 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
252 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
253 669b5ffa 2018-10-07 stsp * there is enough screen estate.
255 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
256 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
258 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
259 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
260 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
262 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
263 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
265 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
267 cc3c9aac 2018-08-01 stsp struct tog_view {
268 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
269 26ed57b2 2018-05-19 stsp WINDOW *window;
270 26ed57b2 2018-05-19 stsp PANEL *panel;
271 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
272 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
273 1004088d 2018-09-29 stsp int focussed;
274 669b5ffa 2018-10-07 stsp struct tog_view *parent;
275 669b5ffa 2018-10-07 stsp struct tog_view *child;
276 669b5ffa 2018-10-07 stsp int child_focussed;
278 5dc9f4bc 2018-08-04 stsp /* type-specific state */
279 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
281 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
282 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
283 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
284 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
287 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
288 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
289 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
290 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
292 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
293 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
294 60493ae3 2019-06-20 stsp int searching;
295 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
296 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
297 60493ae3 2019-06-20 stsp int search_next_done;
298 1803e47f 2019-06-22 stsp regex_t regex;
301 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
302 fb872ab2 2019-02-21 stsp struct got_object_id *, struct got_object_id *, struct tog_view *,
303 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
304 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
305 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
306 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
307 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
309 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
310 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *,
311 d01904d4 2019-06-25 stsp struct got_repository *, const char *, const char *, int);
312 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
313 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
314 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
315 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
316 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
317 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
319 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
320 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *, struct got_repository *);
321 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
322 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
323 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
324 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
325 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
326 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
328 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
329 8b473291 2019-02-21 stsp struct got_tree_object *, struct got_object_id *,
330 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
331 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
332 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
333 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
334 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
335 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
336 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
338 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
339 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
341 25791caa 2018-10-24 stsp static void
342 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
344 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
347 83baff54 2019-08-12 stsp static void
348 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
350 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
353 e5a0f69f 2018-08-18 stsp static const struct got_error *
354 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
356 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
358 669b5ffa 2018-10-07 stsp if (view->child) {
359 669b5ffa 2018-10-07 stsp view_close(view->child);
360 669b5ffa 2018-10-07 stsp view->child = NULL;
362 e5a0f69f 2018-08-18 stsp if (view->close)
363 e5a0f69f 2018-08-18 stsp err = view->close(view);
364 ea5e7bb5 2018-08-01 stsp if (view->panel)
365 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
366 ea5e7bb5 2018-08-01 stsp if (view->window)
367 ea5e7bb5 2018-08-01 stsp delwin(view->window);
368 ea5e7bb5 2018-08-01 stsp free(view);
369 e5a0f69f 2018-08-18 stsp return err;
372 ea5e7bb5 2018-08-01 stsp static struct tog_view *
373 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
374 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
376 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
378 ea5e7bb5 2018-08-01 stsp if (view == NULL)
379 ea5e7bb5 2018-08-01 stsp return NULL;
381 d6b05b5b 2018-08-04 stsp view->type = type;
382 f7d12f7e 2018-08-01 stsp view->lines = LINES;
383 f7d12f7e 2018-08-01 stsp view->cols = COLS;
384 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
385 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
386 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
387 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
388 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
389 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
390 96a765a8 2018-08-04 stsp view_close(view);
391 ea5e7bb5 2018-08-01 stsp return NULL;
393 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
394 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
395 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
396 96a765a8 2018-08-04 stsp view_close(view);
397 ea5e7bb5 2018-08-01 stsp return NULL;
400 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
401 ea5e7bb5 2018-08-01 stsp return view;
405 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
407 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
409 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
412 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
414 5c60c32a 2018-10-18 stsp static const struct got_error *
415 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
417 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
419 5c60c32a 2018-10-18 stsp view->begin_y = 0;
420 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
421 5c60c32a 2018-10-18 stsp view->nlines = LINES;
422 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
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;
435 5c60c32a 2018-10-18 stsp static const struct got_error *
436 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
438 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
440 5c60c32a 2018-10-18 stsp view->begin_x = 0;
441 5c60c32a 2018-10-18 stsp view->begin_y = 0;
442 5c60c32a 2018-10-18 stsp view->nlines = LINES;
443 5c60c32a 2018-10-18 stsp view->ncols = COLS;
444 5c60c32a 2018-10-18 stsp view->lines = LINES;
445 5c60c32a 2018-10-18 stsp view->cols = COLS;
446 5c60c32a 2018-10-18 stsp err = view_resize(view);
448 5c60c32a 2018-10-18 stsp return err;
450 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
451 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
453 5c60c32a 2018-10-18 stsp return NULL;
457 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
459 5c60c32a 2018-10-18 stsp return view->parent == NULL;
462 4d8c2215 2018-08-19 stsp static const struct got_error *
463 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
465 f7d12f7e 2018-08-01 stsp int nlines, ncols;
467 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
468 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
470 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
472 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
473 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
475 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
477 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
478 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
479 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
480 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
481 25791caa 2018-10-24 stsp wclear(view->window);
483 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
484 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
485 0cf4efb1 2018-09-29 stsp view->lines = LINES;
486 0cf4efb1 2018-09-29 stsp view->cols = COLS;
488 6e3e5d9c 2018-10-18 stsp if (view->child) {
489 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
490 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
491 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
492 5c60c32a 2018-10-18 stsp if (view->child->focussed)
493 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
495 5c60c32a 2018-10-18 stsp show_panel(view->panel);
497 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
498 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
502 5c60c32a 2018-10-18 stsp return NULL;
505 669b5ffa 2018-10-07 stsp static const struct got_error *
506 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
508 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
510 669b5ffa 2018-10-07 stsp if (view->child == NULL)
511 669b5ffa 2018-10-07 stsp return NULL;
513 669b5ffa 2018-10-07 stsp err = view_close(view->child);
514 669b5ffa 2018-10-07 stsp view->child = NULL;
515 669b5ffa 2018-10-07 stsp return err;
518 669b5ffa 2018-10-07 stsp static const struct got_error *
519 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
521 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
523 669b5ffa 2018-10-07 stsp view->child = child;
524 669b5ffa 2018-10-07 stsp child->parent = view;
525 669b5ffa 2018-10-07 stsp return err;
529 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
531 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
534 34bc9ec9 2019-02-22 stsp static void
535 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
537 25791caa 2018-10-24 stsp int cols, lines;
538 25791caa 2018-10-24 stsp struct winsize size;
540 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
541 25791caa 2018-10-24 stsp cols = 80; /* Default */
542 25791caa 2018-10-24 stsp lines = 24;
544 25791caa 2018-10-24 stsp cols = size.ws_col;
545 25791caa 2018-10-24 stsp lines = size.ws_row;
547 25791caa 2018-10-24 stsp resize_term(lines, cols);
550 2b49a8ae 2019-06-22 stsp static const struct got_error *
551 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
553 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
554 2b49a8ae 2019-06-22 stsp char pattern[1024];
556 7d049c18 2019-08-21 stsp int begin_x = 0;
558 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
559 2b49a8ae 2019-06-22 stsp return NULL;
561 7d049c18 2019-08-21 stsp if (!view_is_parent_view(view))
562 7d049c18 2019-08-21 stsp begin_x = view_split_begin_x(view->begin_x);
563 2b49a8ae 2019-06-22 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1,
564 7d049c18 2019-08-21 stsp begin_x, "/");
565 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
567 2b49a8ae 2019-06-22 stsp nocbreak();
569 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
572 2b49a8ae 2019-06-22 stsp if (ret == ERR)
573 2b49a8ae 2019-06-22 stsp return NULL;
575 2b49a8ae 2019-06-22 stsp if (view->searching) {
576 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
577 2b49a8ae 2019-06-22 stsp view->searching = 0;
580 2b49a8ae 2019-06-22 stsp if (regcomp(&view->regex, pattern,
581 f5daf9b1 2019-06-24 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
582 7c32bd05 2019-06-22 stsp err = view->search_start(view);
584 7c32bd05 2019-06-22 stsp regfree(&view->regex);
585 7c32bd05 2019-06-22 stsp return err;
587 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
588 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
589 2b49a8ae 2019-06-22 stsp view->search_next(view);
592 2b49a8ae 2019-06-22 stsp return NULL;
595 0cf4efb1 2018-09-29 stsp static const struct got_error *
596 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
597 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
598 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
600 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
601 669b5ffa 2018-10-07 stsp struct tog_view *v;
602 1a76625f 2018-10-22 stsp int ch, errcode;
604 e5a0f69f 2018-08-18 stsp *new = NULL;
605 e5a0f69f 2018-08-18 stsp *dead = NULL;
606 0cf4efb1 2018-09-29 stsp *focus = NULL;
608 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
609 60493ae3 2019-06-20 stsp errcode = pthread_mutex_unlock(&tog_mutex);
610 60493ae3 2019-06-20 stsp if (errcode)
611 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
612 60493ae3 2019-06-20 stsp "pthread_mutex_unlock");
613 60493ae3 2019-06-20 stsp pthread_yield();
614 60493ae3 2019-06-20 stsp errcode = pthread_mutex_lock(&tog_mutex);
615 60493ae3 2019-06-20 stsp if (errcode)
616 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
617 60493ae3 2019-06-20 stsp "pthread_mutex_lock");
618 60493ae3 2019-06-20 stsp view->search_next(view);
619 60493ae3 2019-06-20 stsp return NULL;
622 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
623 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
624 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
625 1a76625f 2018-10-22 stsp if (errcode)
626 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
627 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
628 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
629 1a76625f 2018-10-22 stsp if (errcode)
630 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
631 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
633 25791caa 2018-10-24 stsp if (tog_sigwinch_received) {
634 25791caa 2018-10-24 stsp tog_resizeterm();
635 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
636 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
637 25791caa 2018-10-24 stsp err = view_resize(v);
639 25791caa 2018-10-24 stsp return err;
640 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
642 25791caa 2018-10-24 stsp return err;
646 e5a0f69f 2018-08-18 stsp switch (ch) {
650 1e37a5c2 2019-05-12 jcs if (view->child) {
651 1e37a5c2 2019-05-12 jcs *focus = view->child;
652 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
653 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
654 1e37a5c2 2019-05-12 jcs *focus = view->parent;
655 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
659 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
660 1e37a5c2 2019-05-12 jcs *dead = view;
666 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
667 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
669 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
670 669b5ffa 2018-10-07 stsp *focus = view->child;
671 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
672 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
674 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
677 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
678 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
680 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
681 1e37a5c2 2019-05-12 jcs *focus = view;
682 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
683 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
685 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
689 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
690 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
693 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
696 60493ae3 2019-06-20 stsp if (view->search_start)
697 2b49a8ae 2019-06-22 stsp view_search_start(view);
699 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
703 60493ae3 2019-06-20 stsp if (view->search_next && view->searching) {
704 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
705 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
706 60493ae3 2019-06-20 stsp view->search_next_done = 0;
707 60493ae3 2019-06-20 stsp view->search_next(view);
709 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
712 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
716 e5a0f69f 2018-08-18 stsp return err;
720 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
722 0cf4efb1 2018-09-29 stsp PANEL *panel;
723 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
725 669b5ffa 2018-10-07 stsp if (view->parent)
726 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
728 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
729 0cf4efb1 2018-09-29 stsp if (panel == NULL)
732 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
733 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
734 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
738 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
740 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
741 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
743 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
745 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
748 669b5ffa 2018-10-07 stsp return view->focussed;
751 bcbd79e2 2018-08-19 stsp static const struct got_error *
752 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
754 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
755 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
756 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
757 fd823528 2018-10-22 stsp int fast_refresh = 10;
758 1a76625f 2018-10-22 stsp int done = 0, errcode;
760 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
761 1a76625f 2018-10-22 stsp if (errcode)
762 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
764 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
765 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
767 a81bf10d 2018-09-29 stsp main_view = view;
768 1004088d 2018-09-29 stsp view->focussed = 1;
769 878940b7 2018-09-29 stsp err = view->show(view);
771 0cf4efb1 2018-09-29 stsp return err;
772 0cf4efb1 2018-09-29 stsp update_panels();
773 0cf4efb1 2018-09-29 stsp doupdate();
774 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
775 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
776 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
777 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
779 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
780 e4197bf9 2018-08-18 stsp view, &views);
783 e5a0f69f 2018-08-18 stsp if (dead_view) {
784 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
786 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
787 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
788 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
789 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
790 669b5ffa 2018-10-07 stsp prev = view->parent;
792 669b5ffa 2018-10-07 stsp if (dead_view->parent)
793 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
795 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
797 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
798 d01904d4 2019-06-25 stsp if (err || (dead_view == main_view && new_view == NULL))
801 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
802 0cf4efb1 2018-09-29 stsp if (focus_view)
803 0cf4efb1 2018-09-29 stsp view = focus_view;
804 669b5ffa 2018-10-07 stsp else if (prev)
805 669b5ffa 2018-10-07 stsp view = prev;
806 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
807 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
808 0cf4efb1 2018-09-29 stsp tog_view_list_head);
810 0cf4efb1 2018-09-29 stsp view = NULL;
811 669b5ffa 2018-10-07 stsp if (view) {
812 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
813 669b5ffa 2018-10-07 stsp focus_view = view->child;
815 669b5ffa 2018-10-07 stsp focus_view = view;
819 bcbd79e2 2018-08-19 stsp if (new_view) {
820 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
821 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
822 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
823 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
825 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
826 86c66b02 2018-10-18 stsp err = view_close(v);
831 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
832 fed7eaa8 2018-10-24 stsp view = new_view;
833 878940b7 2018-09-29 stsp if (focus_view == NULL)
834 878940b7 2018-09-29 stsp focus_view = new_view;
836 0cf4efb1 2018-09-29 stsp if (focus_view) {
837 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
839 0cf4efb1 2018-09-29 stsp view->focussed = 0;
840 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
841 0cf4efb1 2018-09-29 stsp view = focus_view;
842 878940b7 2018-09-29 stsp if (new_view)
843 878940b7 2018-09-29 stsp show_panel(new_view->panel);
844 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
845 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
847 669b5ffa 2018-10-07 stsp if (view) {
848 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
849 758194b5 2018-10-24 stsp view->focussed = 1;
850 758194b5 2018-10-24 stsp show_panel(view->panel);
851 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
852 758194b5 2018-10-24 stsp show_panel(view->child->panel);
853 1a76625f 2018-10-22 stsp focus_view = view;
855 669b5ffa 2018-10-07 stsp if (view->parent) {
856 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
860 669b5ffa 2018-10-07 stsp err = view->show(view);
863 669b5ffa 2018-10-07 stsp if (view->child) {
864 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
868 1a76625f 2018-10-22 stsp update_panels();
869 1a76625f 2018-10-22 stsp doupdate();
873 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
874 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
875 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
876 e5a0f69f 2018-08-18 stsp view_close(view);
879 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
880 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
881 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
883 e5a0f69f 2018-08-18 stsp return err;
886 4ed7e80c 2018-05-20 stsp __dead static void
887 9f7d7167 2018-04-29 stsp usage_log(void)
890 c70c5802 2018-08-01 stsp fprintf(stderr,
891 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
892 9f7d7167 2018-04-29 stsp getprogname());
896 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
897 80ddbec8 2018-04-29 stsp static const struct got_error *
898 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
900 00dfcb92 2018-06-11 stsp char *vis = NULL;
901 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
903 963b370f 2018-05-20 stsp *ws = NULL;
904 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
905 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
906 00dfcb92 2018-06-11 stsp int vislen;
907 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
908 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
910 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
911 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
913 00dfcb92 2018-06-11 stsp return err;
914 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
915 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
916 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
921 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
922 a7f50699 2018-06-11 stsp if (*ws == NULL) {
923 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
927 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
928 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
933 963b370f 2018-05-20 stsp *ws = NULL;
936 963b370f 2018-05-20 stsp return err;
939 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
940 963b370f 2018-05-20 stsp static const struct got_error *
941 27a741e5 2019-09-11 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
942 27a741e5 2019-09-11 stsp int col_tab_align)
944 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
945 963b370f 2018-05-20 stsp int cols = 0;
946 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
947 963b370f 2018-05-20 stsp size_t wlen;
950 963b370f 2018-05-20 stsp *wlinep = NULL;
951 b700b5d6 2018-07-10 stsp *widthp = 0;
953 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
955 963b370f 2018-05-20 stsp return err;
958 27a741e5 2019-09-11 stsp while (i < wlen) {
959 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
961 27a741e5 2019-09-11 stsp if (width == 0) {
966 27a741e5 2019-09-11 stsp if (width == 1 || width == 2) {
967 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
969 27a741e5 2019-09-11 stsp cols += width;
971 27a741e5 2019-09-11 stsp } else if (width == -1) {
972 27a741e5 2019-09-11 stsp if (wline[i] == L'\t') {
973 27a741e5 2019-09-11 stsp width = TABSIZE -
974 27a741e5 2019-09-11 stsp ((cols + col_tab_align) % TABSIZE);
975 27a741e5 2019-09-11 stsp if (cols + width > wlimit)
977 27a741e5 2019-09-11 stsp cols += width;
981 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
985 963b370f 2018-05-20 stsp wline[i] = L'\0';
986 b700b5d6 2018-07-10 stsp if (widthp)
987 b700b5d6 2018-07-10 stsp *widthp = cols;
990 963b370f 2018-05-20 stsp free(wline);
992 963b370f 2018-05-20 stsp *wlinep = wline;
993 963b370f 2018-05-20 stsp return err;
996 8b473291 2019-02-21 stsp static const struct got_error*
997 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
998 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
1000 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
1001 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
1003 8b473291 2019-02-21 stsp const char *name;
1005 8b473291 2019-02-21 stsp *refs_str = NULL;
1007 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1008 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
1011 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
1012 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1014 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
1015 8b473291 2019-02-21 stsp name += 5;
1016 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1018 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1019 8b473291 2019-02-21 stsp name += 6;
1020 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
1021 8b473291 2019-02-21 stsp name += 8;
1022 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1023 52b5abe1 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1024 5d844a1e 2019-08-13 stsp if (err) {
1025 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1027 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1028 5d844a1e 2019-08-13 stsp err = NULL;
1029 5d844a1e 2019-08-13 stsp tag = NULL;
1032 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1033 52b5abe1 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1035 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1036 52b5abe1 2019-08-13 stsp if (cmp != 0)
1038 8b473291 2019-02-21 stsp s = *refs_str;
1039 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1040 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1041 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1043 8b473291 2019-02-21 stsp *refs_str = NULL;
1049 8b473291 2019-02-21 stsp return err;
1052 963b370f 2018-05-20 stsp static const struct got_error *
1053 27a741e5 2019-09-11 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1054 27a741e5 2019-09-11 stsp int col_tab_align)
1056 5813d178 2019-03-09 stsp char *smallerthan, *at;
1058 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1059 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1060 5813d178 2019-03-09 stsp author = smallerthan + 1;
1061 5813d178 2019-03-09 stsp at = strchr(author, '@');
1063 5813d178 2019-03-09 stsp *at = '\0';
1064 27a741e5 2019-09-11 stsp return format_line(wauthor, author_width, author, limit, col_tab_align);
1067 5813d178 2019-03-09 stsp static const struct got_error *
1068 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1069 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
1070 27a741e5 2019-09-11 stsp const size_t date_display_cols, int author_display_cols)
1072 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1073 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1074 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1075 5813d178 2019-03-09 stsp char *author = NULL;
1076 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1077 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1078 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1079 bb737323 2018-05-20 stsp int col, limit;
1080 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1081 ccb26ccd 2018-11-05 stsp struct tm tm;
1082 45d799e2 2018-12-23 stsp time_t committer_time;
1084 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1085 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1086 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1087 ccb26ccd 2018-11-05 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1088 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1089 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1091 27a741e5 2019-09-11 stsp if (avail <= date_display_cols)
1092 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1094 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1095 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1096 27a741e5 2019-09-11 stsp col = limit;
1097 b39d25c7 2018-07-10 stsp if (col > avail)
1098 b39d25c7 2018-07-10 stsp goto done;
1100 6570a66d 2019-11-08 stsp if (avail >= 120) {
1101 6570a66d 2019-11-08 stsp char *id_str;
1102 6570a66d 2019-11-08 stsp err = got_object_id_str(&id_str, id);
1104 6570a66d 2019-11-08 stsp goto done;
1105 6570a66d 2019-11-08 stsp wprintw(view->window, "%.8s ", id_str);
1106 6570a66d 2019-11-08 stsp free(id_str);
1108 6570a66d 2019-11-08 stsp if (col > avail)
1109 6570a66d 2019-11-08 stsp goto done;
1112 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1113 5813d178 2019-03-09 stsp if (author == NULL) {
1114 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1115 80ddbec8 2018-04-29 stsp goto done;
1117 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &author_width, author, avail - col, col);
1119 bb737323 2018-05-20 stsp goto done;
1120 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1121 bb737323 2018-05-20 stsp col += author_width;
1122 27a741e5 2019-09-11 stsp while (col < avail && author_width < author_display_cols + 2) {
1123 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1125 bb737323 2018-05-20 stsp author_width++;
1127 9c2eaf34 2018-05-20 stsp if (col > avail)
1128 9c2eaf34 2018-05-20 stsp goto done;
1130 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1132 6d9fbc00 2018-04-29 stsp goto done;
1133 bb737323 2018-05-20 stsp logmsg = logmsg0;
1134 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1136 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1137 bb737323 2018-05-20 stsp if (newline)
1138 bb737323 2018-05-20 stsp *newline = '\0';
1139 bb737323 2018-05-20 stsp limit = avail - col;
1140 27a741e5 2019-09-11 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, 0);
1142 bb737323 2018-05-20 stsp goto done;
1143 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1144 bb737323 2018-05-20 stsp col += logmsg_width;
1145 27a741e5 2019-09-11 stsp while (col < avail) {
1146 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1150 80ddbec8 2018-04-29 stsp free(logmsg0);
1151 bb737323 2018-05-20 stsp free(wlogmsg);
1152 5813d178 2019-03-09 stsp free(author);
1153 bb737323 2018-05-20 stsp free(wauthor);
1154 80ddbec8 2018-04-29 stsp free(line);
1155 80ddbec8 2018-04-29 stsp return err;
1158 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1159 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1160 899d86c2 2018-05-10 stsp struct got_object_id *id)
1162 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1164 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1165 80ddbec8 2018-04-29 stsp if (entry == NULL)
1166 899d86c2 2018-05-10 stsp return NULL;
1168 899d86c2 2018-05-10 stsp entry->id = id;
1169 99db9666 2018-05-07 stsp entry->commit = commit;
1170 899d86c2 2018-05-10 stsp return entry;
1173 99db9666 2018-05-07 stsp static void
1174 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1176 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1178 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1179 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1180 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1181 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1182 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1183 99db9666 2018-05-07 stsp free(entry);
1186 99db9666 2018-05-07 stsp static void
1187 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1189 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1190 99db9666 2018-05-07 stsp pop_commit(commits);
1193 c4972b91 2018-05-07 stsp static const struct got_error *
1194 13add988 2019-10-15 stsp match_commit(int *have_match, struct got_object_id *id,
1195 13add988 2019-10-15 stsp struct got_commit_object *commit, regex_t *regex)
1197 13add988 2019-10-15 stsp const struct got_error *err = NULL;
1198 13add988 2019-10-15 stsp regmatch_t regmatch;
1199 13add988 2019-10-15 stsp char *id_str = NULL, *logmsg = NULL;
1201 13add988 2019-10-15 stsp *have_match = 0;
1203 13add988 2019-10-15 stsp err = got_object_id_str(&id_str, id);
1205 13add988 2019-10-15 stsp return err;
1207 13add988 2019-10-15 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
1209 13add988 2019-10-15 stsp goto done;
1211 13add988 2019-10-15 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1212 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1213 13add988 2019-10-15 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1214 13add988 2019-10-15 stsp ®match, 0) == 0 ||
1215 13add988 2019-10-15 stsp regexec(regex, id_str, 1, ®match, 0) == 0 ||
1216 13add988 2019-10-15 stsp regexec(regex, logmsg, 1, ®match, 0) == 0)
1217 13add988 2019-10-15 stsp *have_match = 1;
1219 13add988 2019-10-15 stsp free(id_str);
1220 13add988 2019-10-15 stsp free(logmsg);
1221 13add988 2019-10-15 stsp return err;
1224 13add988 2019-10-15 stsp static const struct got_error *
1225 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1226 13add988 2019-10-15 stsp int minqueue, struct got_repository *repo, const char *path,
1227 13add988 2019-10-15 stsp int *searching, int *search_next_done, regex_t *regex)
1229 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1230 13add988 2019-10-15 stsp int nqueued = 0, have_match = 0;
1233 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1234 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1235 1a76625f 2018-10-22 stsp * while updating the display.
1237 13add988 2019-10-15 stsp while (nqueued < minqueue ||
1238 13add988 2019-10-15 stsp (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1239 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1240 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1241 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1242 1a76625f 2018-10-22 stsp int errcode;
1244 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1245 9ba79e04 2018-06-11 stsp if (err) {
1246 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1248 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
1249 6fb7cd11 2019-08-22 stsp minqueue, repo, NULL, NULL);
1251 ecb28ae0 2018-07-16 stsp return err;
1255 ecb28ae0 2018-07-16 stsp if (id == NULL)
1258 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1261 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1262 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1263 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1267 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1268 1a76625f 2018-10-22 stsp if (errcode) {
1269 13add988 2019-10-15 stsp err = got_error_set_errno(errcode,
1270 13add988 2019-10-15 stsp "pthread_mutex_lock");
1274 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1275 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1276 ecb28ae0 2018-07-16 stsp nqueued++;
1277 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1279 13add988 2019-10-15 stsp if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1280 13add988 2019-10-15 stsp err = match_commit(&have_match, id, commit, regex);
1281 13add988 2019-10-15 stsp if (err) {
1282 13add988 2019-10-15 stsp pthread_mutex_lock(&tog_mutex);
1287 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1288 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1289 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1290 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1292 13add988 2019-10-15 stsp if (have_match)
1296 9ba79e04 2018-06-11 stsp return err;
1299 99db9666 2018-05-07 stsp static const struct got_error *
1300 19e70ad6 2019-05-14 stsp get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1301 19e70ad6 2019-05-14 stsp struct got_repository *repo)
1303 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1304 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1306 9ba79e04 2018-06-11 stsp *head_id = NULL;
1308 19e70ad6 2019-05-14 stsp err = got_ref_open(&head_ref, repo, branch_name, 0);
1310 99db9666 2018-05-07 stsp return err;
1312 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1313 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1314 9ba79e04 2018-06-11 stsp if (err) {
1315 9ba79e04 2018-06-11 stsp *head_id = NULL;
1316 99db9666 2018-05-07 stsp return err;
1319 9ba79e04 2018-06-11 stsp return NULL;
1322 0553a4e3 2018-04-30 stsp static const struct got_error *
1323 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1324 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1325 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1326 8b473291 2019-02-21 stsp struct got_reflist_head *refs, const char *path, int commits_needed)
1328 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1329 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1330 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1331 60493ae3 2019-06-20 stsp int width;
1332 60493ae3 2019-06-20 stsp int ncommits, author_cols = 10;
1333 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1334 8b473291 2019-02-21 stsp char *refs_str = NULL;
1335 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1336 27a741e5 2019-09-11 stsp static const size_t date_display_cols = 9;
1338 e0d42f60 2018-07-22 stsp entry = first;
1339 e0d42f60 2018-07-22 stsp ncommits = 0;
1340 e0d42f60 2018-07-22 stsp while (entry) {
1341 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1342 e0d42f60 2018-07-22 stsp *selected = entry;
1345 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1346 e0d42f60 2018-07-22 stsp ncommits++;
1349 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1350 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1352 ecb28ae0 2018-07-16 stsp return err;
1353 8b473291 2019-02-21 stsp if (refs) {
1354 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, (*selected)->id,
1357 8b473291 2019-02-21 stsp goto done;
1361 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1362 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1364 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1365 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1366 60493ae3 2019-06-20 stsp commits_needed > 0 ?
1367 60493ae3 2019-06-20 stsp (view->searching && view->search_next_done == 0
1368 60493ae3 2019-06-20 stsp ? "searching..." : "loading... ") :
1369 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1370 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1371 8b473291 2019-02-21 stsp goto done;
1374 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1375 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1376 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1377 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1378 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1379 1a76625f 2018-10-22 stsp header = NULL;
1380 1a76625f 2018-10-22 stsp goto done;
1382 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1383 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1384 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1385 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1386 1a76625f 2018-10-22 stsp header = NULL;
1387 1a76625f 2018-10-22 stsp goto done;
1389 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
1391 1a76625f 2018-10-22 stsp goto done;
1393 2814baeb 2018-08-01 stsp werase(view->window);
1395 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1396 a3404814 2018-09-02 stsp wstandout(view->window);
1397 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1398 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1399 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1402 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1403 a3404814 2018-09-02 stsp wstandend(view->window);
1404 ecb28ae0 2018-07-16 stsp free(wline);
1405 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1406 1a76625f 2018-10-22 stsp goto done;
1408 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1409 899d86c2 2018-05-10 stsp entry = first;
1410 5813d178 2019-03-09 stsp ncommits = 0;
1411 5813d178 2019-03-09 stsp while (entry) {
1412 5813d178 2019-03-09 stsp char *author;
1413 5813d178 2019-03-09 stsp wchar_t *wauthor;
1414 5813d178 2019-03-09 stsp int width;
1415 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1417 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1418 5813d178 2019-03-09 stsp if (author == NULL) {
1419 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1420 5813d178 2019-03-09 stsp goto done;
1422 27a741e5 2019-09-11 stsp err = format_author(&wauthor, &width, author, COLS,
1423 27a741e5 2019-09-11 stsp date_display_cols);
1424 5813d178 2019-03-09 stsp if (author_cols < width)
1425 5813d178 2019-03-09 stsp author_cols = width;
1426 5813d178 2019-03-09 stsp free(wauthor);
1427 5813d178 2019-03-09 stsp free(author);
1428 7ca04879 2019-10-19 stsp ncommits++;
1429 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1432 5813d178 2019-03-09 stsp entry = first;
1433 899d86c2 2018-05-10 stsp *last = first;
1434 867c6645 2018-07-10 stsp ncommits = 0;
1435 899d86c2 2018-05-10 stsp while (entry) {
1436 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1438 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1439 2814baeb 2018-08-01 stsp wstandout(view->window);
1440 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1441 27a741e5 2019-09-11 stsp date_display_cols, author_cols);
1442 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1443 2814baeb 2018-08-01 stsp wstandend(view->window);
1445 60493ae3 2019-06-20 stsp goto done;
1446 0553a4e3 2018-04-30 stsp ncommits++;
1447 899d86c2 2018-05-10 stsp *last = entry;
1448 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1451 1a57306a 2018-09-02 stsp view_vborder(view);
1453 1a76625f 2018-10-22 stsp free(id_str);
1454 8b473291 2019-02-21 stsp free(refs_str);
1455 1a76625f 2018-10-22 stsp free(ncommits_str);
1456 1a76625f 2018-10-22 stsp free(header);
1457 80ddbec8 2018-04-29 stsp return err;
1460 07b55e75 2018-05-10 stsp static void
1461 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1462 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1463 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1465 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1466 07b55e75 2018-05-10 stsp int nscrolled = 0;
1468 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1469 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1472 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1473 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1474 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1475 07b55e75 2018-05-10 stsp if (entry) {
1476 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1477 07b55e75 2018-05-10 stsp nscrolled++;
1482 aa075928 2018-05-10 stsp static const struct got_error *
1483 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1484 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1486 5e224a3e 2019-02-22 stsp int errcode;
1487 8a42fca8 2019-02-22 stsp int max_wait = 20;
1489 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1491 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1492 5e224a3e 2019-02-22 stsp if (*log_complete)
1495 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1496 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1497 7aafa0d1 2019-02-22 stsp if (errcode)
1498 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1499 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1500 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1501 7aafa0d1 2019-02-22 stsp if (errcode)
1502 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1503 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1504 7aafa0d1 2019-02-22 stsp pthread_yield();
1505 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1506 7aafa0d1 2019-02-22 stsp if (errcode)
1507 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1508 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1510 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1512 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1513 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1514 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1515 5e224a3e 2019-02-22 stsp * with few commits in history.
1517 7aafa0d1 2019-02-22 stsp return NULL;
1521 5e224a3e 2019-02-22 stsp return NULL;
1524 5e224a3e 2019-02-22 stsp static const struct got_error *
1525 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1526 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1527 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1528 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1529 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1531 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1532 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1533 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1535 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1536 5e224a3e 2019-02-22 stsp return NULL;
1538 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1539 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1541 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1542 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1544 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1545 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1546 5e224a3e 2019-02-22 stsp need_commits);
1548 5e224a3e 2019-02-22 stsp return err;
1552 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1553 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1556 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1558 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1559 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1561 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1562 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1564 dd0a52c1 2018-05-20 stsp return err;
1567 cd0acaa7 2018-05-20 stsp static const struct got_error *
1568 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1569 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1570 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1571 8b473291 2019-02-21 stsp struct got_repository *repo)
1573 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1574 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1575 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1577 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1578 15a94983 2018-12-23 stsp if (diff_view == NULL)
1579 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1581 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1582 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1583 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1584 e5a0f69f 2018-08-18 stsp if (err == NULL)
1585 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1586 cd0acaa7 2018-05-20 stsp return err;
1589 80ddbec8 2018-04-29 stsp static const struct got_error *
1590 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1591 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1593 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1595 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1596 941e9f74 2019-05-21 stsp if (parent == NULL)
1597 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1599 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1600 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1601 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1602 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1603 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1604 941e9f74 2019-05-21 stsp s->tree = subtree;
1605 941e9f74 2019-05-21 stsp s->entries = got_object_tree_get_entries(s->tree);
1606 941e9f74 2019-05-21 stsp s->selected = 0;
1607 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1608 941e9f74 2019-05-21 stsp return NULL;
1612 941e9f74 2019-05-21 stsp static const struct got_error *
1613 941e9f74 2019-05-21 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1614 941e9f74 2019-05-21 stsp struct commit_queue_entry *entry, const char *path,
1615 941e9f74 2019-05-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
1617 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1618 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1619 941e9f74 2019-05-21 stsp struct got_tree_entry *te;
1620 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s;
1621 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1622 b03c880f 2019-05-21 stsp char *slash, *subpath = NULL;
1623 941e9f74 2019-05-21 stsp const char *p;
1625 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1626 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1628 9343a5fb 2018-06-23 stsp return err;
1630 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1631 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1632 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1634 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1635 941e9f74 2019-05-21 stsp if (err) {
1636 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1637 941e9f74 2019-05-21 stsp return err;
1639 941e9f74 2019-05-21 stsp s = &tree_view->state.tree;
1641 941e9f74 2019-05-21 stsp *new_view = tree_view;
1643 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1645 8d0fe45a 2019-08-12 stsp while (p[0] == '/')
1647 941e9f74 2019-05-21 stsp while (*p) {
1648 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1650 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1651 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1652 941e9f74 2019-05-21 stsp if (slash == NULL)
1653 941e9f74 2019-05-21 stsp slash = strchr(p, '\0');
1654 941e9f74 2019-05-21 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1655 941e9f74 2019-05-21 stsp if (strncmp(p, te->name, slash - p) == 0) {
1656 941e9f74 2019-05-21 stsp s->selected_entry = te;
1659 b03c880f 2019-05-21 stsp s->selected++;
1661 941e9f74 2019-05-21 stsp if (s->selected_entry == NULL) {
1662 941e9f74 2019-05-21 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1665 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1666 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1668 67409a31 2019-05-24 stsp if (!S_ISDIR(s->selected_entry->mode)) {
1669 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1670 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1671 67409a31 2019-05-24 stsp s->selected = 0;
1675 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1676 941e9f74 2019-05-21 stsp if (slash)
1677 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1679 941e9f74 2019-05-21 stsp subpath = strdup(path);
1680 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1681 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1685 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1690 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1691 941e9f74 2019-05-21 stsp free(tree_id);
1695 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1696 941e9f74 2019-05-21 stsp if (err) {
1697 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1700 941e9f74 2019-05-21 stsp if (slash == NULL)
1702 941e9f74 2019-05-21 stsp free(subpath);
1703 941e9f74 2019-05-21 stsp subpath = NULL;
1704 941e9f74 2019-05-21 stsp p = slash;
1707 941e9f74 2019-05-21 stsp free(subpath);
1708 1a76625f 2018-10-22 stsp return err;
1711 1a76625f 2018-10-22 stsp static void *
1712 1a76625f 2018-10-22 stsp log_thread(void *arg)
1714 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1715 1a76625f 2018-10-22 stsp int errcode = 0;
1716 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1717 1a76625f 2018-10-22 stsp int done = 0;
1719 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1720 6fb7cd11 2019-08-22 stsp NULL, NULL);
1722 1a76625f 2018-10-22 stsp return (void *)err;
1724 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1725 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1726 13add988 2019-10-15 stsp a->in_repo_path, a->searching, a->search_next_done,
1727 13add988 2019-10-15 stsp a->regex);
1728 1a76625f 2018-10-22 stsp if (err) {
1729 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1730 1a76625f 2018-10-22 stsp return (void *)err;
1731 1a76625f 2018-10-22 stsp err = NULL;
1733 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1734 1a76625f 2018-10-22 stsp a->commits_needed--;
1736 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1737 3abe8080 2019-04-10 stsp if (errcode) {
1738 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1739 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1741 3abe8080 2019-04-10 stsp } else if (*a->quit)
1743 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1744 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1745 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1746 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1750 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1751 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1752 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1753 1a76625f 2018-10-22 stsp &tog_mutex);
1754 1a76625f 2018-10-22 stsp if (errcode)
1755 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1756 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1759 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1760 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1761 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1762 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1764 3abe8080 2019-04-10 stsp a->log_complete = 1;
1765 1a76625f 2018-10-22 stsp return (void *)err;
1768 1a76625f 2018-10-22 stsp static const struct got_error *
1769 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1771 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1772 1a76625f 2018-10-22 stsp int errcode;
1774 1a76625f 2018-10-22 stsp if (s->thread) {
1775 1a76625f 2018-10-22 stsp s->quit = 1;
1776 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1777 1a76625f 2018-10-22 stsp if (errcode)
1778 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1779 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1780 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1781 1a76625f 2018-10-22 stsp if (errcode)
1782 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1783 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1784 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1785 1a76625f 2018-10-22 stsp if (errcode)
1786 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1787 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1788 1a76625f 2018-10-22 stsp if (errcode)
1789 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1790 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1791 1a76625f 2018-10-22 stsp s->thread = NULL;
1794 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1795 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1796 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1798 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1799 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1800 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1803 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1804 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1805 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1808 9343a5fb 2018-06-23 stsp return err;
1811 9343a5fb 2018-06-23 stsp static const struct got_error *
1812 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1814 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1815 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1817 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1818 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1819 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1820 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1821 1a76625f 2018-10-22 stsp free(s->start_id);
1822 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1823 1a76625f 2018-10-22 stsp return err;
1826 1a76625f 2018-10-22 stsp static const struct got_error *
1827 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
1829 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1831 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
1832 96e2b566 2019-07-08 stsp s->search_entry = NULL;
1833 60493ae3 2019-06-20 stsp return NULL;
1836 60493ae3 2019-06-20 stsp static const struct got_error *
1837 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
1839 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
1840 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1841 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
1843 60493ae3 2019-06-20 stsp if (!view->searching) {
1844 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1845 60493ae3 2019-06-20 stsp return NULL;
1848 96e2b566 2019-07-08 stsp if (s->search_entry) {
1849 13add988 2019-10-15 stsp int errcode, ch;
1850 13add988 2019-10-15 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1851 13add988 2019-10-15 stsp if (errcode)
1852 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
1853 13add988 2019-10-15 stsp "pthread_mutex_unlock");
1854 13add988 2019-10-15 stsp ch = wgetch(view->window);
1855 13add988 2019-10-15 stsp errcode = pthread_mutex_lock(&tog_mutex);
1856 13add988 2019-10-15 stsp if (errcode)
1857 13add988 2019-10-15 stsp return got_error_set_errno(errcode,
1858 13add988 2019-10-15 stsp "pthread_mutex_lock");
1859 13add988 2019-10-15 stsp if (ch == KEY_BACKSPACE) {
1860 678cbce5 2019-07-28 stsp view->search_next_done = 1;
1861 678cbce5 2019-07-28 stsp return NULL;
1863 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
1864 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
1866 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
1867 96e2b566 2019-07-08 stsp commit_queue_head, entry);
1868 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
1869 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1870 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
1872 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
1873 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
1875 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1876 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
1878 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1881 60493ae3 2019-06-20 stsp while (1) {
1882 13add988 2019-10-15 stsp int have_match = 0;
1884 60493ae3 2019-06-20 stsp if (entry == NULL) {
1885 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
1886 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
1887 f801134a 2019-06-25 stsp view->search_next_done = 1;
1888 f801134a 2019-06-25 stsp return NULL;
1891 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
1892 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
1893 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
1895 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
1896 57b33b64 2019-07-08 stsp return trigger_log_thread(1,
1897 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
1898 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
1899 f801134a 2019-06-25 stsp &s->thread_args.need_commits);
1902 13add988 2019-10-15 stsp err = match_commit(&have_match, entry->id, entry->commit,
1903 13add988 2019-10-15 stsp &view->regex);
1906 13add988 2019-10-15 stsp if (have_match) {
1907 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1908 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
1912 96e2b566 2019-07-08 stsp s->search_entry = entry;
1913 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1914 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
1916 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1919 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
1920 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
1921 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
1922 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1924 60493ae3 2019-06-20 stsp return err;
1927 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
1928 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1930 60493ae3 2019-06-20 stsp return err;
1935 96e2b566 2019-07-08 stsp s->search_entry = NULL;
1937 60493ae3 2019-06-20 stsp return NULL;
1940 60493ae3 2019-06-20 stsp static const struct got_error *
1941 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1942 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
1943 d01904d4 2019-06-25 stsp const char *head_ref_name, const char *path, int check_disk)
1945 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1946 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1947 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1948 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1949 1a76625f 2018-10-22 stsp int errcode;
1951 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1952 ecb28ae0 2018-07-16 stsp if (err != NULL)
1953 ecb28ae0 2018-07-16 stsp goto done;
1955 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1956 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1957 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1959 8b473291 2019-02-21 stsp s->refs = refs;
1960 fb2756b9 2018-08-04 stsp s->repo = repo;
1961 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
1962 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1963 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1964 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1965 5036bf37 2018-09-24 stsp goto done;
1968 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1969 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1970 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1971 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
1972 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
1974 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
1976 1a76625f 2018-10-22 stsp goto done;
1977 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1978 1a76625f 2018-10-22 stsp 0, thread_repo);
1980 1a76625f 2018-10-22 stsp goto done;
1982 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1983 1a76625f 2018-10-22 stsp if (errcode) {
1984 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
1985 1a76625f 2018-10-22 stsp goto done;
1988 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1989 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1990 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1991 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1992 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1993 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1994 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1995 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1996 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1997 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1998 13add988 2019-10-15 stsp s->thread_args.searching = &view->searching;
1999 13add988 2019-10-15 stsp s->thread_args.search_next_done = &view->search_next_done;
2000 13add988 2019-10-15 stsp s->thread_args.regex = &view->regex;
2003 1a76625f 2018-10-22 stsp close_log_view(view);
2004 ba4f502b 2018-08-04 stsp return err;
2007 e5a0f69f 2018-08-18 stsp static const struct got_error *
2008 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
2010 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
2012 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
2013 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
2014 2b380cc8 2018-10-24 stsp &s->thread_args);
2015 2b380cc8 2018-10-24 stsp if (errcode)
2016 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
2019 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
2020 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
2021 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
2022 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
2025 e5a0f69f 2018-08-18 stsp static const struct got_error *
2026 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2027 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2029 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2030 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
2031 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
2032 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2033 669b5ffa 2018-10-07 stsp int begin_x = 0;
2034 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
2036 e5a0f69f 2018-08-18 stsp switch (ch) {
2038 1e37a5c2 2019-05-12 jcs s->quit = 1;
2041 1e37a5c2 2019-05-12 jcs case KEY_UP:
2044 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2046 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
2047 1e37a5c2 2019-05-12 jcs s->selected--;
2049 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
2050 1144d21a 2019-06-21 stsp &s->commits);
2052 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2053 a4292ac5 2019-05-12 jcs case CTRL('b'):
2054 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2056 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
2057 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
2058 1e37a5c2 2019-05-12 jcs s->selected = 0;
2061 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
2062 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
2065 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2068 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2070 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2071 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2072 1e37a5c2 2019-05-12 jcs s->selected++;
2075 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
2076 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
2077 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
2078 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2079 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2081 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2082 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2083 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2084 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2085 1e37a5c2 2019-05-12 jcs if (first == NULL)
2087 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
2088 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
2089 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
2090 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2091 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2094 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2095 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2096 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2097 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2098 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2099 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2101 1e37a5c2 2019-05-12 jcs err = NULL;
2104 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2105 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2106 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2107 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2108 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2110 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2113 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2115 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2116 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2117 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2118 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2119 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
2122 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2123 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2125 1e37a5c2 2019-05-12 jcs return err;
2126 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2128 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2131 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2132 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2134 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2137 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2139 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2140 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2141 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2142 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2145 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2146 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2148 1e37a5c2 2019-05-12 jcs return err;
2149 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2151 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2154 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2155 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2157 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2159 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2160 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2162 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2163 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2164 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2166 1e37a5c2 2019-05-12 jcs return err;
2167 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2168 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2169 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2170 638f9024 2019-05-13 stsp return got_error_from_errno(
2171 1e37a5c2 2019-05-12 jcs "view_open");
2172 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2173 d01904d4 2019-06-25 stsp s->repo, s->head_ref_name, parent_path, 0);
2175 1e37a5c2 2019-05-12 jcs return err;;
2176 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2177 1e37a5c2 2019-05-12 jcs *new_view = lv;
2179 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2180 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2182 1e37a5c2 2019-05-12 jcs return NULL;
2185 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2186 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2188 d01904d4 2019-06-25 stsp return err;
2189 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2190 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2191 d01904d4 2019-06-25 stsp if (lv == NULL)
2192 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2193 d01904d4 2019-06-25 stsp err = get_head_commit_id(&start_id, s->head_ref_name ?
2194 d01904d4 2019-06-25 stsp s->head_ref_name : GOT_REF_HEAD, s->repo);
2195 ef129c5e 2019-08-03 stsp if (err) {
2196 ef129c5e 2019-08-03 stsp view_close(lv);
2197 d01904d4 2019-06-25 stsp return err;
2199 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2200 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2201 e6b23735 2019-10-04 stsp free(start_id);
2202 e6b23735 2019-10-04 stsp view_close(lv);
2203 e6b23735 2019-10-04 stsp return got_error_from_errno("strdup");
2205 e6b23735 2019-10-04 stsp got_ref_list_free(s->refs);
2206 e6b23735 2019-10-04 stsp err = got_ref_list(s->refs, s->repo, NULL,
2207 e6b23735 2019-10-04 stsp got_ref_cmp_by_name, NULL);
2208 e6b23735 2019-10-04 stsp if (err) {
2209 d01904d4 2019-06-25 stsp free(start_id);
2210 ef129c5e 2019-08-03 stsp view_close(lv);
2211 50284065 2019-10-04 stsp return err;
2213 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2214 d01904d4 2019-06-25 stsp s->head_ref_name, in_repo_path, 0);
2215 ef129c5e 2019-08-03 stsp if (err) {
2216 ef129c5e 2019-08-03 stsp free(start_id);
2217 ef129c5e 2019-08-03 stsp view_close(lv);
2218 d01904d4 2019-06-25 stsp return err;;
2220 d01904d4 2019-06-25 stsp *dead_view = view;
2221 d01904d4 2019-06-25 stsp *new_view = lv;
2227 80ddbec8 2018-04-29 stsp return err;
2230 4ed7e80c 2018-05-20 stsp static const struct got_error *
2231 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2233 c2db6724 2019-01-04 stsp const struct got_error *error;
2235 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2236 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2237 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2239 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2240 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2242 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2243 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2245 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2246 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2248 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2249 c2db6724 2019-01-04 stsp if (error != NULL)
2250 c2db6724 2019-01-04 stsp return error;
2252 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2253 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2255 c2db6724 2019-01-04 stsp return NULL;
2258 a915003a 2019-02-05 stsp static void
2259 a915003a 2019-02-05 stsp init_curses(void)
2261 a915003a 2019-02-05 stsp initscr();
2263 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2266 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2267 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2268 a915003a 2019-02-05 stsp curs_set(0);
2269 6d17833f 2019-11-08 stsp if (getenv("TOG_COLORS") != NULL) {
2270 6d17833f 2019-11-08 stsp start_color();
2271 6d17833f 2019-11-08 stsp use_default_colors();
2273 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2274 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2277 c2db6724 2019-01-04 stsp static const struct got_error *
2278 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2280 80ddbec8 2018-04-29 stsp const struct got_error *error;
2281 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2282 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2283 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2284 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2285 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2286 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2288 04cc582a 2018-08-01 stsp struct tog_view *view;
2290 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2292 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2293 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2294 c2db6724 2019-01-04 stsp NULL) == -1)
2295 80ddbec8 2018-04-29 stsp err(1, "pledge");
2298 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2299 80ddbec8 2018-04-29 stsp switch (ch) {
2301 80ddbec8 2018-04-29 stsp start_commit = optarg;
2304 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2305 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2306 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2310 17020d27 2019-03-07 stsp usage_log();
2311 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2315 80ddbec8 2018-04-29 stsp argc -= optind;
2316 80ddbec8 2018-04-29 stsp argv += optind;
2318 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2319 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2320 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2321 ecb28ae0 2018-07-16 stsp goto done;
2323 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2324 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2325 963f97a1 2019-03-18 stsp goto done;
2326 963f97a1 2019-03-18 stsp error = NULL;
2328 963f97a1 2019-03-18 stsp if (argc == 0) {
2329 963f97a1 2019-03-18 stsp path = strdup("");
2330 963f97a1 2019-03-18 stsp if (path == NULL) {
2331 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2332 ecb28ae0 2018-07-16 stsp goto done;
2334 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2335 963f97a1 2019-03-18 stsp if (worktree) {
2336 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2338 963f97a1 2019-03-18 stsp if (error)
2339 963f97a1 2019-03-18 stsp goto done;
2341 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2342 963f97a1 2019-03-18 stsp if (path == NULL) {
2343 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2344 963f97a1 2019-03-18 stsp goto done;
2348 963f97a1 2019-03-18 stsp usage_log();
2350 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2351 a1fbf39a 2019-08-11 stsp if (worktree)
2352 b9d7675a 2019-08-11 stsp repo_path = strdup(
2353 b9d7675a 2019-08-11 stsp got_worktree_get_repo_path(worktree));
2355 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2357 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2358 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2359 963f97a1 2019-03-18 stsp goto done;
2362 a915003a 2019-02-05 stsp init_curses();
2364 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2365 80ddbec8 2018-04-29 stsp if (error != NULL)
2366 ecb28ae0 2018-07-16 stsp goto done;
2368 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2369 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2370 c02c541e 2019-03-29 stsp if (error)
2371 c02c541e 2019-03-29 stsp goto done;
2373 15a94983 2018-12-23 stsp if (start_commit == NULL)
2374 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&start_id, worktree ?
2375 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2378 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&start_id, start_commit, repo);
2379 f2b6a97d 2019-07-15 stsp if (error) {
2380 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
2381 f2b6a97d 2019-07-15 stsp goto done;
2382 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&start_id,
2383 f2b6a97d 2019-07-15 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2386 80ddbec8 2018-04-29 stsp if (error != NULL)
2387 8b473291 2019-02-21 stsp goto done;
2389 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2390 8b473291 2019-02-21 stsp if (error)
2391 ecb28ae0 2018-07-16 stsp goto done;
2393 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2394 04cc582a 2018-08-01 stsp if (view == NULL) {
2395 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2396 04cc582a 2018-08-01 stsp goto done;
2398 2fc00ff4 2019-08-31 stsp if (worktree) {
2399 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2400 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2401 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2402 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2403 2fc00ff4 2019-08-31 stsp goto done;
2406 2fc00ff4 2019-08-31 stsp error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2408 ba4f502b 2018-08-04 stsp if (error)
2409 ba4f502b 2018-08-04 stsp goto done;
2410 2fc00ff4 2019-08-31 stsp if (worktree) {
2411 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2412 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2413 2fc00ff4 2019-08-31 stsp worktree = NULL;
2415 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2417 ecb28ae0 2018-07-16 stsp free(repo_path);
2418 ecb28ae0 2018-07-16 stsp free(cwd);
2419 ecb28ae0 2018-07-16 stsp free(path);
2420 899d86c2 2018-05-10 stsp free(start_id);
2421 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2423 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2424 ec142235 2019-03-07 stsp if (worktree)
2425 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2426 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2427 80ddbec8 2018-04-29 stsp return error;
2430 4ed7e80c 2018-05-20 stsp __dead static void
2431 9f7d7167 2018-04-29 stsp usage_diff(void)
2434 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2435 9f7d7167 2018-04-29 stsp getprogname());
2439 b304db33 2018-05-20 stsp static char *
2440 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2442 b304db33 2018-05-20 stsp char *line;
2443 b304db33 2018-05-20 stsp size_t linelen;
2444 b304db33 2018-05-20 stsp size_t lineno;
2445 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2447 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2449 b304db33 2018-05-20 stsp *len = linelen;
2450 b304db33 2018-05-20 stsp return line;
2453 6d17833f 2019-11-08 stsp static int
2454 6d17833f 2019-11-08 stsp match_line(const char *line, regex_t *regex)
2456 6d17833f 2019-11-08 stsp regmatch_t regmatch;
2458 6d17833f 2019-11-08 stsp return regexec(regex, line, 1, ®match, 0) == 0;
2461 6d17833f 2019-11-08 stsp struct tog_line_color *
2462 6d17833f 2019-11-08 stsp match_line_color(struct tog_line_colors *colors, const char *line)
2464 6d17833f 2019-11-08 stsp struct tog_line_color *tc = NULL;
2466 6d17833f 2019-11-08 stsp SIMPLEQ_FOREACH(tc, colors, entry) {
2467 6d17833f 2019-11-08 stsp if (match_line(line, &tc->regex))
2468 6d17833f 2019-11-08 stsp return tc;
2471 6d17833f 2019-11-08 stsp return NULL;
2474 4ed7e80c 2018-05-20 stsp static const struct got_error *
2475 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2476 6d17833f 2019-11-08 stsp int *last_displayed_line, int *eof, int max_lines, char *header,
2477 6d17833f 2019-11-08 stsp struct tog_line_colors *colors)
2479 61e69b96 2018-05-20 stsp const struct got_error *err;
2480 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2481 b304db33 2018-05-20 stsp char *line;
2482 6d17833f 2019-11-08 stsp struct tog_line_color *lc;
2483 b304db33 2018-05-20 stsp size_t len;
2484 61e69b96 2018-05-20 stsp wchar_t *wline;
2485 e0b650dd 2018-05-20 stsp int width;
2487 26ed57b2 2018-05-19 stsp rewind(f);
2488 f7d12f7e 2018-08-01 stsp werase(view->window);
2490 a3404814 2018-09-02 stsp if (header) {
2491 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, header, view->ncols, 0);
2492 a3404814 2018-09-02 stsp if (err) {
2493 a3404814 2018-09-02 stsp return err;
2496 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2497 a3404814 2018-09-02 stsp wstandout(view->window);
2498 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2499 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2500 a3404814 2018-09-02 stsp wstandend(view->window);
2501 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2502 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2504 a3404814 2018-09-02 stsp if (max_lines <= 1)
2505 a3404814 2018-09-02 stsp return NULL;
2506 a3404814 2018-09-02 stsp max_lines--;
2510 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2511 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2512 26ed57b2 2018-05-19 stsp if (line == NULL) {
2516 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2517 26ed57b2 2018-05-19 stsp free(line);
2521 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
2522 61e69b96 2018-05-20 stsp if (err) {
2523 61e69b96 2018-05-20 stsp free(line);
2524 61e69b96 2018-05-20 stsp return err;
2527 6d17833f 2019-11-08 stsp lc = match_line_color(colors, line);
2529 6d17833f 2019-11-08 stsp wattr_on(view->window,
2530 6d17833f 2019-11-08 stsp COLOR_PAIR(lc->colorpair), NULL);
2531 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2533 6d17833f 2019-11-08 stsp wattr_off(view->window,
2534 6d17833f 2019-11-08 stsp COLOR_PAIR(lc->colorpair), NULL);
2535 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2536 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2537 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2538 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2539 26ed57b2 2018-05-19 stsp free(line);
2540 2550e4c3 2018-07-13 stsp free(wline);
2541 2550e4c3 2018-07-13 stsp wline = NULL;
2543 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2545 1a57306a 2018-09-02 stsp view_vborder(view);
2547 c3e9aa98 2019-05-13 jcs if (*eof) {
2548 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2549 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2550 c3e9aa98 2019-05-13 jcs nprinted++;
2553 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2555 c3e9aa98 2019-05-13 jcs return err;
2558 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2559 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2560 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2563 26ed57b2 2018-05-19 stsp return NULL;
2566 abd2672a 2018-12-23 stsp static char *
2567 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2569 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2570 09867e48 2019-08-13 stsp char *p, *s;
2572 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2573 09867e48 2019-08-13 stsp if (tm == NULL)
2574 09867e48 2019-08-13 stsp return NULL;
2575 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2576 09867e48 2019-08-13 stsp if (s == NULL)
2577 09867e48 2019-08-13 stsp return NULL;
2578 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2580 abd2672a 2018-12-23 stsp *p = '\0';
2584 4ed7e80c 2018-05-20 stsp static const struct got_error *
2585 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2586 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2588 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2589 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
2590 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2591 5943eee2 2019-08-13 stsp char *id_str = NULL, *logmsg = NULL;
2592 45d799e2 2018-12-23 stsp time_t committer_time;
2593 45d799e2 2018-12-23 stsp const char *author, *committer;
2594 8b473291 2019-02-21 stsp char *refs_str = NULL;
2596 8b473291 2019-02-21 stsp if (refs) {
2597 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
2599 8b473291 2019-02-21 stsp return err;
2602 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2604 abd2672a 2018-12-23 stsp return err;
2606 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2607 15a94983 2018-12-23 stsp if (err) {
2608 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2609 15a94983 2018-12-23 stsp goto done;
2612 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2613 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2614 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2615 abd2672a 2018-12-23 stsp goto done;
2617 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2618 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2619 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2620 abd2672a 2018-12-23 stsp goto done;
2622 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2623 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
2624 09867e48 2019-08-13 stsp if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2625 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2626 abd2672a 2018-12-23 stsp goto done;
2628 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2629 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2630 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2631 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2632 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2633 abd2672a 2018-12-23 stsp goto done;
2635 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2637 5943eee2 2019-08-13 stsp goto done;
2638 5943eee2 2019-08-13 stsp if (fprintf(outfile, "%s\n", logmsg) < 0) {
2639 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2640 abd2672a 2018-12-23 stsp goto done;
2643 abd2672a 2018-12-23 stsp free(id_str);
2644 5943eee2 2019-08-13 stsp free(logmsg);
2645 8b473291 2019-02-21 stsp free(refs_str);
2646 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2647 abd2672a 2018-12-23 stsp return err;
2650 abd2672a 2018-12-23 stsp static const struct got_error *
2651 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2653 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2654 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2655 15a94983 2018-12-23 stsp int obj_type;
2657 511a516b 2018-05-19 stsp f = got_opentemp();
2658 48ae06ee 2018-10-18 stsp if (f == NULL) {
2659 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2660 48ae06ee 2018-10-18 stsp goto done;
2662 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2663 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2664 fb43ecf1 2019-02-11 stsp goto done;
2668 15a94983 2018-12-23 stsp if (s->id1)
2669 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2671 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2673 15a94983 2018-12-23 stsp goto done;
2675 15a94983 2018-12-23 stsp switch (obj_type) {
2676 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2677 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2678 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2680 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2681 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2682 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2684 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2685 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2686 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2687 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2689 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2692 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2693 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2694 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2696 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2697 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2698 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2699 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2700 8b473291 2019-02-21 stsp s->repo, f);
2705 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2707 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2708 63035f9f 2019-10-06 stsp s->diff_context, 0, s->repo, f);
2712 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2716 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2717 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2718 48ae06ee 2018-10-18 stsp return err;
2721 f5215bb9 2019-02-22 stsp static void
2722 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2724 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2725 f5215bb9 2019-02-22 stsp update_panels();
2726 f5215bb9 2019-02-22 stsp doupdate();
2729 6d17833f 2019-11-08 stsp static const struct got_error *
2730 6d17833f 2019-11-08 stsp add_line_color(struct tog_line_colors *colors, const char *pattern,
2731 6d17833f 2019-11-08 stsp int idx, short color)
2733 6d17833f 2019-11-08 stsp const struct got_error *err = NULL;
2734 6d17833f 2019-11-08 stsp struct tog_line_color *lc;
2735 6d17833f 2019-11-08 stsp int regerr = 0;
2737 6d17833f 2019-11-08 stsp init_pair(idx, color, -1);
2739 6d17833f 2019-11-08 stsp lc = calloc(1, sizeof(*lc));
2740 6d17833f 2019-11-08 stsp if (lc == NULL)
2741 6d17833f 2019-11-08 stsp return got_error_from_errno("calloc");
2742 6d17833f 2019-11-08 stsp regerr = regcomp(&lc->regex, pattern,
2743 6d17833f 2019-11-08 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
2744 6d17833f 2019-11-08 stsp if (regerr) {
2745 6d17833f 2019-11-08 stsp static char regerr_msg[512];
2746 6d17833f 2019-11-08 stsp static char err_msg[512];
2747 6d17833f 2019-11-08 stsp regerror(regerr, &lc->regex, regerr_msg,
2748 6d17833f 2019-11-08 stsp sizeof(regerr_msg));
2749 6d17833f 2019-11-08 stsp snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
2750 6d17833f 2019-11-08 stsp regerr_msg);
2751 6d17833f 2019-11-08 stsp err = got_error_msg(GOT_ERR_REGEX, err_msg);
2753 6d17833f 2019-11-08 stsp return err;
2755 6d17833f 2019-11-08 stsp lc->colorpair = idx;
2756 6d17833f 2019-11-08 stsp SIMPLEQ_INSERT_HEAD(colors, lc, entry);
2757 6d17833f 2019-11-08 stsp return NULL;
2760 7169caa2 2019-11-08 stsp static void
2761 6d17833f 2019-11-08 stsp free_line_colors(struct tog_line_colors *colors)
2763 6d17833f 2019-11-08 stsp struct tog_line_color *lc;
2765 6d17833f 2019-11-08 stsp while (!SIMPLEQ_EMPTY(colors)) {
2766 6d17833f 2019-11-08 stsp lc = SIMPLEQ_FIRST(colors);
2767 6d17833f 2019-11-08 stsp SIMPLEQ_REMOVE_HEAD(colors, entry);
2768 6d17833f 2019-11-08 stsp regfree(&lc->regex);
2773 6d17833f 2019-11-08 stsp static int
2774 6d17833f 2019-11-08 stsp default_color_value(const char *envvar)
2776 6d17833f 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
2777 6d17833f 2019-11-08 stsp return COLOR_MAGENTA;
2778 6d17833f 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
2779 6d17833f 2019-11-08 stsp return COLOR_CYAN;
2780 6d17833f 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
2781 6d17833f 2019-11-08 stsp return COLOR_YELLOW;
2782 6d17833f 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
2783 6d17833f 2019-11-08 stsp return COLOR_GREEN;
2784 c0b01bdb 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
2785 c0b01bdb 2019-11-08 stsp return COLOR_MAGENTA;
2786 c0b01bdb 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
2787 c0b01bdb 2019-11-08 stsp return COLOR_CYAN;
2788 c0b01bdb 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
2789 c0b01bdb 2019-11-08 stsp return COLOR_BLUE;
2790 c0b01bdb 2019-11-08 stsp if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
2791 c0b01bdb 2019-11-08 stsp return COLOR_GREEN;
2793 6d17833f 2019-11-08 stsp return -1;
2796 6d17833f 2019-11-08 stsp static int
2797 6d17833f 2019-11-08 stsp get_color_value(const char *envvar)
2799 6d17833f 2019-11-08 stsp const char *val = getenv(envvar);
2801 6d17833f 2019-11-08 stsp if (val == NULL)
2802 6d17833f 2019-11-08 stsp return default_color_value(envvar);
2804 6d17833f 2019-11-08 stsp if (strcasecmp(val, "black") == 0)
2805 6d17833f 2019-11-08 stsp return COLOR_BLACK;
2806 6d17833f 2019-11-08 stsp if (strcasecmp(val, "red") == 0)
2807 6d17833f 2019-11-08 stsp return COLOR_RED;
2808 6d17833f 2019-11-08 stsp if (strcasecmp(val, "green") == 0)
2809 6d17833f 2019-11-08 stsp return COLOR_GREEN;
2810 6d17833f 2019-11-08 stsp if (strcasecmp(val, "yellow") == 0)
2811 6d17833f 2019-11-08 stsp return COLOR_YELLOW;
2812 6d17833f 2019-11-08 stsp if (strcasecmp(val, "blue") == 0)
2813 6d17833f 2019-11-08 stsp return COLOR_BLUE;
2814 6d17833f 2019-11-08 stsp if (strcasecmp(val, "magenta") == 0)
2815 6d17833f 2019-11-08 stsp return COLOR_MAGENTA;
2816 6d17833f 2019-11-08 stsp if (strcasecmp(val, "cyan") == 0)
2817 6d17833f 2019-11-08 stsp return COLOR_CYAN;
2818 6d17833f 2019-11-08 stsp if (strcasecmp(val, "white") == 0)
2819 6d17833f 2019-11-08 stsp return COLOR_WHITE;
2821 6d17833f 2019-11-08 stsp return default_color_value(envvar);
2824 48ae06ee 2018-10-18 stsp static const struct got_error *
2825 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2826 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2827 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2829 48ae06ee 2018-10-18 stsp const struct got_error *err;
2831 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2832 15a94983 2018-12-23 stsp int type1, type2;
2833 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2835 15a94983 2018-12-23 stsp return err;
2836 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2838 15a94983 2018-12-23 stsp return err;
2840 15a94983 2018-12-23 stsp if (type1 != type2)
2841 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2844 15a94983 2018-12-23 stsp if (id1) {
2845 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2846 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2847 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2849 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2851 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2852 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2853 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2854 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2855 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2857 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2858 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2859 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2860 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2861 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2862 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2863 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2864 6d17833f 2019-11-08 stsp SIMPLEQ_INIT(&view->state.diff.line_colors);
2866 6d17833f 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
2867 6d17833f 2019-11-08 stsp err = add_line_color(&view->state.diff.line_colors,
2868 6d17833f 2019-11-08 stsp "^-", 1, get_color_value("TOG_COLOR_DIFF_MINUS"));
2870 6d17833f 2019-11-08 stsp return err;
2871 6d17833f 2019-11-08 stsp err = add_line_color(&view->state.diff.line_colors,
2872 6d17833f 2019-11-08 stsp "^\\+", 2, get_color_value("TOG_COLOR_DIFF_PLUS"));
2873 6d17833f 2019-11-08 stsp if (err) {
2874 6d17833f 2019-11-08 stsp free_line_colors(&view->state.diff.line_colors);
2875 6d17833f 2019-11-08 stsp return err;
2877 6d17833f 2019-11-08 stsp err = add_line_color(&view->state.diff.line_colors,
2879 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2880 6d17833f 2019-11-08 stsp if (err) {
2881 6d17833f 2019-11-08 stsp free_line_colors(&view->state.diff.line_colors);
2882 6d17833f 2019-11-08 stsp return err;
2885 6d17833f 2019-11-08 stsp err = add_line_color(&view->state.diff.line_colors,
2886 6d17833f 2019-11-08 stsp "^(commit|(blob|file) [-+] )", 4,
2887 6d17833f 2019-11-08 stsp get_color_value("TOG_COLOR_DIFF_META"));
2888 6d17833f 2019-11-08 stsp if (err) {
2889 6d17833f 2019-11-08 stsp free_line_colors(&view->state.diff.line_colors);
2890 6d17833f 2019-11-08 stsp return err;
2894 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2895 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2896 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2898 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2899 48ae06ee 2018-10-18 stsp if (err) {
2900 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2901 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2902 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2903 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2904 48ae06ee 2018-10-18 stsp return err;
2907 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2908 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2909 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2911 5dc9f4bc 2018-08-04 stsp return NULL;
2914 e5a0f69f 2018-08-18 stsp static const struct got_error *
2915 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2917 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2919 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2920 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2921 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2922 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2923 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2924 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2925 6d17833f 2019-11-08 stsp free_line_colors(&view->state.diff.line_colors);
2926 e5a0f69f 2018-08-18 stsp return err;
2929 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2930 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2932 a3404814 2018-09-02 stsp const struct got_error *err;
2933 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2934 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2936 a3404814 2018-09-02 stsp if (s->id1) {
2937 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2939 a3404814 2018-09-02 stsp return err;
2941 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2943 a3404814 2018-09-02 stsp return err;
2945 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2946 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2947 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2948 a3404814 2018-09-02 stsp free(id_str1);
2949 a3404814 2018-09-02 stsp free(id_str2);
2950 a3404814 2018-09-02 stsp return err;
2952 a3404814 2018-09-02 stsp free(id_str1);
2953 a3404814 2018-09-02 stsp free(id_str2);
2955 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2956 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2957 6d17833f 2019-11-08 stsp header, &s->line_colors);
2960 15a087fe 2019-02-21 stsp static const struct got_error *
2961 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2962 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2964 d7a04538 2019-02-21 stsp const struct got_error *err;
2965 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
2966 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
2967 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
2969 15a087fe 2019-02-21 stsp free(s->id2);
2970 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2971 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2972 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2974 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2976 d7a04538 2019-02-21 stsp return err;
2977 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
2978 15a087fe 2019-02-21 stsp free(s->id1);
2979 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
2980 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2981 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
2982 15a087fe 2019-02-21 stsp return NULL;
2985 0cf4efb1 2018-09-29 stsp static const struct got_error *
2986 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2987 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2989 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2990 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2991 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
2992 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
2995 e5a0f69f 2018-08-18 stsp switch (ch) {
2997 1e37a5c2 2019-05-12 jcs case KEY_UP:
2998 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
2999 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3001 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3002 a60a9dc4 2019-05-13 jcs case CTRL('b'):
3003 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
3006 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
3007 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3008 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3011 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3012 1e37a5c2 2019-05-12 jcs if (!s->eof)
3013 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3015 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3016 a60a9dc4 2019-05-13 jcs case CTRL('f'):
3018 00ba99a7 2019-05-12 jcs if (s->eof)
3021 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
3022 1e37a5c2 2019-05-12 jcs char *line;
3023 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
3024 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3025 1e37a5c2 2019-05-12 jcs if (line == NULL)
3030 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
3031 1e37a5c2 2019-05-12 jcs s->diff_context--;
3032 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3033 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3037 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3038 1e37a5c2 2019-05-12 jcs s->diff_context++;
3039 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3040 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3045 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3047 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3048 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
3049 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
3050 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3053 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3058 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3062 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3063 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3065 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3066 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3070 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
3072 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
3074 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3075 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
3077 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
3078 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
3079 1e37a5c2 2019-05-12 jcs update_panels();
3080 1e37a5c2 2019-05-12 jcs doupdate();
3082 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
3083 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
3084 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
3085 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
3089 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
3094 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
3095 1e37a5c2 2019-05-12 jcs if (entry == NULL)
3098 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
3102 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3103 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
3105 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
3106 1e37a5c2 2019-05-12 jcs err = create_diff(s);
3112 bcbd79e2 2018-08-19 stsp return err;
3115 4ed7e80c 2018-05-20 stsp static const struct got_error *
3116 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
3118 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
3119 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
3120 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3121 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3122 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
3123 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
3125 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
3127 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3129 26ed57b2 2018-05-19 stsp #ifndef PROFILE
3130 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3131 eb6600df 2019-01-04 stsp NULL) == -1)
3132 26ed57b2 2018-05-19 stsp err(1, "pledge");
3135 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3136 26ed57b2 2018-05-19 stsp switch (ch) {
3138 17020d27 2019-03-07 stsp usage_diff();
3139 26ed57b2 2018-05-19 stsp /* NOTREACHED */
3143 26ed57b2 2018-05-19 stsp argc -= optind;
3144 26ed57b2 2018-05-19 stsp argv += optind;
3146 26ed57b2 2018-05-19 stsp if (argc == 0) {
3147 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
3148 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
3149 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
3150 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
3151 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
3152 15a94983 2018-12-23 stsp id_str1 = argv[0];
3153 15a94983 2018-12-23 stsp id_str2 = argv[1];
3154 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
3155 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
3156 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
3157 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
3158 15a94983 2018-12-23 stsp id_str1 = argv[1];
3159 15a94983 2018-12-23 stsp id_str2 = argv[2];
3161 26ed57b2 2018-05-19 stsp usage_diff();
3163 a915003a 2019-02-05 stsp init_curses();
3165 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3166 eb6600df 2019-01-04 stsp if (error)
3167 eb6600df 2019-01-04 stsp goto done;
3169 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3170 26ed57b2 2018-05-19 stsp if (error)
3171 26ed57b2 2018-05-19 stsp goto done;
3173 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
3174 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3175 26ed57b2 2018-05-19 stsp if (error)
3176 26ed57b2 2018-05-19 stsp goto done;
3178 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
3179 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
3180 26ed57b2 2018-05-19 stsp if (error)
3181 26ed57b2 2018-05-19 stsp goto done;
3183 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3184 8b473291 2019-02-21 stsp if (error)
3185 8b473291 2019-02-21 stsp goto done;
3187 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3188 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
3189 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3190 ea5e7bb5 2018-08-01 stsp goto done;
3192 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3193 5dc9f4bc 2018-08-04 stsp if (error)
3194 5dc9f4bc 2018-08-04 stsp goto done;
3195 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3197 c02c541e 2019-03-29 stsp free(repo_path);
3199 921be706 2019-06-28 stsp got_repo_close(repo);
3200 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3201 26ed57b2 2018-05-19 stsp return error;
3204 4ed7e80c 2018-05-20 stsp __dead static void
3205 9f7d7167 2018-04-29 stsp usage_blame(void)
3208 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3209 9f7d7167 2018-04-29 stsp getprogname());
3213 84451b3e 2018-07-10 stsp struct tog_blame_line {
3214 84451b3e 2018-07-10 stsp int annotated;
3215 84451b3e 2018-07-10 stsp struct got_object_id *id;
3218 4ed7e80c 2018-05-20 stsp static const struct got_error *
3219 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3220 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
3221 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
3222 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
3224 84451b3e 2018-07-10 stsp const struct got_error *err;
3225 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
3226 84451b3e 2018-07-10 stsp char *line;
3227 84451b3e 2018-07-10 stsp size_t len;
3228 84451b3e 2018-07-10 stsp wchar_t *wline;
3229 27a741e5 2019-09-11 stsp int width;
3230 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
3231 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
3232 ab089a2a 2018-07-12 stsp char *id_str;
3234 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
3236 ab089a2a 2018-07-12 stsp return err;
3238 84451b3e 2018-07-10 stsp rewind(f);
3239 f7d12f7e 2018-08-01 stsp werase(view->window);
3241 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3242 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3243 ab089a2a 2018-07-12 stsp free(id_str);
3244 ab089a2a 2018-07-12 stsp return err;
3247 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3248 ab089a2a 2018-07-12 stsp free(line);
3249 2550e4c3 2018-07-13 stsp line = NULL;
3251 1cae65b4 2019-09-22 stsp return err;
3252 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3253 a3404814 2018-09-02 stsp wstandout(view->window);
3254 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3255 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3256 a3404814 2018-09-02 stsp wstandend(view->window);
3257 2550e4c3 2018-07-13 stsp free(wline);
3258 2550e4c3 2018-07-13 stsp wline = NULL;
3259 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3260 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3262 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3263 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
3264 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
3265 ab089a2a 2018-07-12 stsp free(id_str);
3266 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3268 ab089a2a 2018-07-12 stsp free(id_str);
3269 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
3270 3f60a8ef 2018-07-10 stsp free(line);
3271 2550e4c3 2018-07-13 stsp line = NULL;
3273 3f60a8ef 2018-07-10 stsp return err;
3274 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3275 2550e4c3 2018-07-13 stsp free(wline);
3276 2550e4c3 2018-07-13 stsp wline = NULL;
3277 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3278 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3281 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
3282 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
3283 84451b3e 2018-07-10 stsp if (line == NULL) {
3287 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
3288 84451b3e 2018-07-10 stsp free(line);
3292 27a741e5 2019-09-11 stsp if (view->ncols <= 9) {
3293 27a741e5 2019-09-11 stsp width = 9;
3294 27a741e5 2019-09-11 stsp wline = wcsdup(L"");
3295 27a741e5 2019-09-11 stsp if (wline == NULL)
3296 27a741e5 2019-09-11 stsp err = got_error_from_errno("wcsdup");
3298 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line,
3299 27a741e5 2019-09-11 stsp view->ncols - 9, 9);
3300 27a741e5 2019-09-11 stsp width += 9;
3302 84451b3e 2018-07-10 stsp if (err) {
3303 84451b3e 2018-07-10 stsp free(line);
3304 84451b3e 2018-07-10 stsp return err;
3307 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3308 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3310 8d0fe45a 2019-08-12 stsp if (nlines > 0) {
3311 8d0fe45a 2019-08-12 stsp blame_line = &lines[lineno - 1];
3312 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3313 27a741e5 2019-09-11 stsp got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3314 27a741e5 2019-09-11 stsp !(view->focussed &&
3315 27a741e5 2019-09-11 stsp nprinted == selected_line - 1)) {
3316 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3317 27a741e5 2019-09-11 stsp } else if (blame_line->annotated) {
3318 8d0fe45a 2019-08-12 stsp char *id_str;
3319 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3320 8d0fe45a 2019-08-12 stsp if (err) {
3321 8d0fe45a 2019-08-12 stsp free(line);
3322 8d0fe45a 2019-08-12 stsp free(wline);
3323 8d0fe45a 2019-08-12 stsp return err;
3325 27a741e5 2019-09-11 stsp wprintw(view->window, "%.8s", id_str);
3326 8d0fe45a 2019-08-12 stsp free(id_str);
3327 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3329 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3330 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3333 27a741e5 2019-09-11 stsp waddstr(view->window, "........");
3334 ee41ec32 2018-07-10 stsp prev_id = NULL;
3337 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3338 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3339 27a741e5 2019-09-11 stsp waddstr(view->window, " ");
3341 27a741e5 2019-09-11 stsp waddwstr(view->window, wline);
3342 27a741e5 2019-09-11 stsp if (width <= view->ncols - 1)
3343 27a741e5 2019-09-11 stsp waddch(view->window, '\n');
3344 84451b3e 2018-07-10 stsp if (++nprinted == 1)
3345 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
3346 84451b3e 2018-07-10 stsp free(line);
3347 2550e4c3 2018-07-13 stsp free(wline);
3348 2550e4c3 2018-07-13 stsp wline = NULL;
3350 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
3352 1a57306a 2018-09-02 stsp view_vborder(view);
3354 84451b3e 2018-07-10 stsp return NULL;
3357 84451b3e 2018-07-10 stsp static const struct got_error *
3358 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3360 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
3361 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
3362 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
3363 1a76625f 2018-10-22 stsp int errcode;
3365 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
3366 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3367 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3369 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3370 1a76625f 2018-10-22 stsp if (errcode)
3371 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3373 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3374 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3375 d68a0a7d 2018-07-10 stsp goto done;
3378 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3379 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3381 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3382 d68a0a7d 2018-07-10 stsp if (line->annotated)
3383 d68a0a7d 2018-07-10 stsp goto done;
3385 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3386 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3387 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3388 84451b3e 2018-07-10 stsp goto done;
3390 84451b3e 2018-07-10 stsp line->annotated = 1;
3392 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3393 1a76625f 2018-10-22 stsp if (errcode)
3394 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3395 84451b3e 2018-07-10 stsp return err;
3398 84451b3e 2018-07-10 stsp static void *
3399 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3401 18430de3 2018-07-10 stsp const struct got_error *err;
3402 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3403 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3404 1a76625f 2018-10-22 stsp int errcode;
3406 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
3407 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3408 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
3409 fc06ba56 2019-08-22 stsp err = NULL;
3411 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3412 1a76625f 2018-10-22 stsp if (errcode)
3413 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3414 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3416 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3417 c9beca56 2018-07-22 stsp ta->repo = NULL;
3418 c9beca56 2018-07-22 stsp *ta->complete = 1;
3420 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3421 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3422 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3424 18430de3 2018-07-10 stsp return (void *)err;
3427 245d91c1 2018-07-12 stsp static struct got_object_id *
3428 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3429 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
3431 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3433 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
3434 8d0fe45a 2019-08-12 stsp return NULL;
3436 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3437 245d91c1 2018-07-12 stsp if (!line->annotated)
3438 245d91c1 2018-07-12 stsp return NULL;
3440 245d91c1 2018-07-12 stsp return line->id;
3443 b880a918 2018-07-10 stsp static const struct got_error *
3444 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3446 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3449 245d91c1 2018-07-12 stsp if (blame->thread) {
3450 1a76625f 2018-10-22 stsp int errcode;
3451 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3452 1a76625f 2018-10-22 stsp if (errcode)
3453 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3454 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3455 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3456 1a76625f 2018-10-22 stsp if (errcode)
3457 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3458 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3459 1a76625f 2018-10-22 stsp if (errcode)
3460 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3461 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3462 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3463 245d91c1 2018-07-12 stsp err = NULL;
3464 245d91c1 2018-07-12 stsp blame->thread = NULL;
3466 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3467 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3468 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3470 245d91c1 2018-07-12 stsp if (blame->f) {
3471 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3472 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3473 245d91c1 2018-07-12 stsp blame->f = NULL;
3475 57670559 2018-12-23 stsp if (blame->lines) {
3476 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3477 57670559 2018-12-23 stsp free(blame->lines[i].id);
3478 57670559 2018-12-23 stsp free(blame->lines);
3479 57670559 2018-12-23 stsp blame->lines = NULL;
3481 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3482 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3484 245d91c1 2018-07-12 stsp return err;
3487 245d91c1 2018-07-12 stsp static const struct got_error *
3488 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
3490 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
3491 fc06ba56 2019-08-22 stsp int *done = arg;
3492 fc06ba56 2019-08-22 stsp int errcode;
3494 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3495 fc06ba56 2019-08-22 stsp if (errcode)
3496 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3497 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
3499 fc06ba56 2019-08-22 stsp if (*done)
3500 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
3502 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3503 fc06ba56 2019-08-22 stsp if (errcode)
3504 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3505 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
3507 fc06ba56 2019-08-22 stsp return err;
3510 fc06ba56 2019-08-22 stsp static const struct got_error *
3511 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3512 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3513 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3514 245d91c1 2018-07-12 stsp struct got_repository *repo)
3516 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3517 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3518 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3519 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3520 15a94983 2018-12-23 stsp int obj_type;
3522 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3524 15a94983 2018-12-23 stsp return err;
3525 15a94983 2018-12-23 stsp if (obj_id == NULL)
3526 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
3528 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3530 84451b3e 2018-07-10 stsp goto done;
3532 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3533 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3534 84451b3e 2018-07-10 stsp goto done;
3537 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3539 a70480e0 2018-06-23 stsp goto done;
3540 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3541 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3542 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3543 84451b3e 2018-07-10 stsp goto done;
3545 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3546 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
3547 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
3548 84451b3e 2018-07-10 stsp goto done;
3550 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3551 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3552 b02560ec 2019-08-19 stsp blame->nlines--;
3554 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3555 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3556 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3557 84451b3e 2018-07-10 stsp goto done;
3560 c9956ddf 2019-09-08 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3562 bd24772e 2018-07-11 stsp goto done;
3564 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3565 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3566 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3567 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3568 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3569 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3570 245d91c1 2018-07-12 stsp goto done;
3572 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3574 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3575 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3576 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3577 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3578 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
3579 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_arg = done;
3580 245d91c1 2018-07-12 stsp *blame_complete = 0;
3584 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3585 27d434c2 2018-09-15 stsp free(obj_id);
3587 245d91c1 2018-07-12 stsp stop_blame(blame);
3588 245d91c1 2018-07-12 stsp return err;
3591 245d91c1 2018-07-12 stsp static const struct got_error *
3592 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3593 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3594 8b473291 2019-02-21 stsp struct got_repository *repo)
3596 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3597 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3599 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3601 c4843652 2019-08-12 stsp s->path = strdup(path);
3602 c4843652 2019-08-12 stsp if (s->path == NULL)
3603 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
3605 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3606 c4843652 2019-08-12 stsp if (err) {
3607 c4843652 2019-08-12 stsp free(s->path);
3608 7cbe629d 2018-08-04 stsp return err;
3611 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3612 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3613 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3614 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3615 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3616 fb2756b9 2018-08-04 stsp s->repo = repo;
3617 8b473291 2019-02-21 stsp s->refs = refs;
3618 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3619 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3621 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3622 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3623 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3624 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
3625 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
3627 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3628 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3629 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3630 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3633 e5a0f69f 2018-08-18 stsp static const struct got_error *
3634 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3636 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3637 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3639 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3640 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3642 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3643 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3644 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3645 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3646 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3649 e5a0f69f 2018-08-18 stsp free(s->path);
3651 e5a0f69f 2018-08-18 stsp return err;
3654 7cbe629d 2018-08-04 stsp static const struct got_error *
3655 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
3657 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3659 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
3660 6c4c42e0 2019-06-24 stsp return NULL;
3663 6c4c42e0 2019-06-24 stsp static const struct got_error *
3664 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
3666 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3667 6c4c42e0 2019-06-24 stsp int lineno;
3669 6c4c42e0 2019-06-24 stsp if (!view->searching) {
3670 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3671 6c4c42e0 2019-06-24 stsp return NULL;
3674 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3675 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3676 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
3678 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
3680 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3681 6c4c42e0 2019-06-24 stsp lineno = 1;
3683 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3686 6c4c42e0 2019-06-24 stsp while (1) {
3687 6c4c42e0 2019-06-24 stsp char *line = NULL;
3688 6c4c42e0 2019-06-24 stsp off_t offset;
3689 6c4c42e0 2019-06-24 stsp size_t len;
3691 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
3692 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
3693 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3694 6c4c42e0 2019-06-24 stsp free(line);
3698 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3699 6c4c42e0 2019-06-24 stsp lineno = 1;
3701 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3704 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
3705 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3706 6c4c42e0 2019-06-24 stsp free(line);
3707 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
3709 6c4c42e0 2019-06-24 stsp free(line);
3710 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
3711 2246482e 2019-06-25 stsp if (line && match_line(line, &view->regex)) {
3712 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3713 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
3714 6c4c42e0 2019-06-24 stsp free(line);
3717 6c4c42e0 2019-06-24 stsp free(line);
3718 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3724 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3725 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
3726 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
3729 6c4c42e0 2019-06-24 stsp return NULL;
3732 6c4c42e0 2019-06-24 stsp static const struct got_error *
3733 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
3735 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3736 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
3737 2b380cc8 2018-10-24 stsp int errcode;
3739 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
3740 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3741 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
3742 2b380cc8 2018-10-24 stsp if (errcode)
3743 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3745 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
3748 51fe7530 2019-08-19 stsp if (s->blame_complete)
3749 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
3751 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3752 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3753 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
3754 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
3756 669b5ffa 2018-10-07 stsp view_vborder(view);
3757 e5a0f69f 2018-08-18 stsp return err;
3760 e5a0f69f 2018-08-18 stsp static const struct got_error *
3761 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3762 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3764 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
3765 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
3766 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3767 669b5ffa 2018-10-07 stsp int begin_x = 0;
3769 e5a0f69f 2018-08-18 stsp switch (ch) {
3771 1e37a5c2 2019-05-12 jcs s->done = 1;
3774 1e37a5c2 2019-05-12 jcs case KEY_UP:
3775 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
3776 1e37a5c2 2019-05-12 jcs s->selected_line--;
3777 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
3778 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3779 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3781 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3782 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
3783 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3786 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3787 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3788 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3790 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3793 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3794 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3795 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3796 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3797 1e37a5c2 2019-05-12 jcs s->selected_line++;
3798 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3799 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3800 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3803 1e37a5c2 2019-05-12 jcs case 'p': {
3804 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3805 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3806 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3807 1e37a5c2 2019-05-12 jcs if (id == NULL)
3809 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3810 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3811 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3812 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3813 1e37a5c2 2019-05-12 jcs int obj_type;
3814 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3815 1e37a5c2 2019-05-12 jcs s->repo, id);
3818 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3819 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3820 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3821 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3824 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3825 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3826 1e37a5c2 2019-05-12 jcs pid->id, s->path);
3827 e5a0f69f 2018-08-18 stsp if (err) {
3828 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
3829 1e37a5c2 2019-05-12 jcs err = NULL;
3830 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3833 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
3835 1e37a5c2 2019-05-12 jcs free(blob_id);
3836 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
3837 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
3838 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3841 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3843 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3845 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
3846 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
3848 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3853 1e37a5c2 2019-05-12 jcs s->done = 1;
3854 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3855 1e37a5c2 2019-05-12 jcs s->done = 0;
3856 1e37a5c2 2019-05-12 jcs if (thread_err)
3858 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3859 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
3860 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3861 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3862 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
3863 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
3868 1e37a5c2 2019-05-12 jcs case 'B': {
3869 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
3870 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
3871 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
3873 1e37a5c2 2019-05-12 jcs s->done = 1;
3874 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3875 1e37a5c2 2019-05-12 jcs s->done = 0;
3876 1e37a5c2 2019-05-12 jcs if (thread_err)
3878 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3879 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
3880 1e37a5c2 2019-05-12 jcs s->blamed_commit =
3881 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
3882 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3883 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3884 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
3885 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
3890 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3891 1e37a5c2 2019-05-12 jcs case '\r': {
3892 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3893 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
3894 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
3895 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3896 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3897 1e37a5c2 2019-05-12 jcs if (id == NULL)
3899 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
3902 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
3903 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
3904 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3905 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3906 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3907 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
3908 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3909 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
3912 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
3913 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
3914 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3916 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3919 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3920 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3923 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
3925 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3928 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
3929 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
3931 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3936 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3938 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3939 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
3940 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
3943 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3944 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
3945 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3946 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3949 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
3950 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
3951 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
3952 1e37a5c2 2019-05-12 jcs view->nlines - 2;
3954 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
3955 1e37a5c2 2019-05-12 jcs s->blame.nlines -
3956 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
3958 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3959 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
3960 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3961 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3967 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
3970 a70480e0 2018-06-23 stsp static const struct got_error *
3971 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
3973 a70480e0 2018-06-23 stsp const struct got_error *error;
3974 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
3975 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3976 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
3977 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3978 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3979 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
3981 e1cd8fed 2018-08-01 stsp struct tog_view *view;
3983 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3985 a70480e0 2018-06-23 stsp #ifndef PROFILE
3986 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3987 8e94dd5b 2019-01-04 stsp NULL) == -1)
3988 a70480e0 2018-06-23 stsp err(1, "pledge");
3991 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3992 a70480e0 2018-06-23 stsp switch (ch) {
3994 a70480e0 2018-06-23 stsp commit_id_str = optarg;
3997 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3998 69069811 2018-08-02 stsp if (repo_path == NULL)
3999 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4003 17020d27 2019-03-07 stsp usage_blame();
4004 a70480e0 2018-06-23 stsp /* NOTREACHED */
4008 a70480e0 2018-06-23 stsp argc -= optind;
4009 a70480e0 2018-06-23 stsp argv += optind;
4011 69069811 2018-08-02 stsp if (argc == 1)
4012 69069811 2018-08-02 stsp path = argv[0];
4014 a70480e0 2018-06-23 stsp usage_blame();
4016 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
4017 69069811 2018-08-02 stsp if (cwd == NULL) {
4018 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4019 69069811 2018-08-02 stsp goto done;
4021 69069811 2018-08-02 stsp if (repo_path == NULL) {
4022 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4023 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4024 69069811 2018-08-02 stsp goto done;
4026 eb41ed75 2019-02-05 stsp error = NULL;
4027 eb41ed75 2019-02-05 stsp if (worktree) {
4028 eb41ed75 2019-02-05 stsp repo_path =
4029 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4030 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
4031 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4032 eb41ed75 2019-02-05 stsp if (error)
4033 eb41ed75 2019-02-05 stsp goto done;
4035 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
4036 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
4037 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4038 eb41ed75 2019-02-05 stsp goto done;
4043 a915003a 2019-02-05 stsp init_curses();
4045 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4046 c02c541e 2019-03-29 stsp if (error != NULL)
4047 8e94dd5b 2019-01-04 stsp goto done;
4049 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4050 c02c541e 2019-03-29 stsp if (error)
4051 92205607 2019-01-04 stsp goto done;
4053 eb41ed75 2019-02-05 stsp if (worktree) {
4054 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4055 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4056 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4057 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4058 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4059 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4060 eb41ed75 2019-02-05 stsp path) == -1) {
4061 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4062 eb41ed75 2019-02-05 stsp goto done;
4064 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4067 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4069 eb41ed75 2019-02-05 stsp if (error)
4070 69069811 2018-08-02 stsp goto done;
4072 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
4073 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
4074 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4075 a70480e0 2018-06-23 stsp if (error != NULL)
4076 66b4983c 2018-06-23 stsp goto done;
4077 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4078 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
4080 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&commit_id, commit_id_str, repo);
4081 f2b6a97d 2019-07-15 stsp if (error) {
4082 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
4083 f2b6a97d 2019-07-15 stsp goto done;
4084 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&commit_id,
4085 f2b6a97d 2019-07-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
4088 a19e88aa 2018-06-23 stsp if (error != NULL)
4089 8b473291 2019-02-21 stsp goto done;
4091 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4092 8b473291 2019-02-21 stsp if (error)
4093 a19e88aa 2018-06-23 stsp goto done;
4095 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4096 e1cd8fed 2018-08-01 stsp if (view == NULL) {
4097 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4098 e1cd8fed 2018-08-01 stsp goto done;
4100 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4101 7cbe629d 2018-08-04 stsp if (error)
4102 7cbe629d 2018-08-04 stsp goto done;
4103 12314ad4 2019-08-31 stsp if (worktree) {
4104 12314ad4 2019-08-31 stsp /* Release work tree lock. */
4105 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
4106 12314ad4 2019-08-31 stsp worktree = NULL;
4108 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4110 69069811 2018-08-02 stsp free(repo_path);
4111 69069811 2018-08-02 stsp free(cwd);
4112 a70480e0 2018-06-23 stsp free(commit_id);
4113 eb41ed75 2019-02-05 stsp if (worktree)
4114 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
4116 a70480e0 2018-06-23 stsp got_repo_close(repo);
4117 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4118 a70480e0 2018-06-23 stsp return error;
4121 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4122 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
4123 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
4124 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
4125 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
4126 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
4127 c0b01bdb 2019-11-08 stsp const struct got_tree_entries *entries, int selected, int limit,
4128 c0b01bdb 2019-11-08 stsp int isroot, struct tog_line_colors *colors)
4130 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4131 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
4132 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
4133 c0b01bdb 2019-11-08 stsp struct tog_line_color *lc;
4134 ffd1d5e5 2018-06-23 stsp int width, n;
4136 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
4138 f7d12f7e 2018-08-01 stsp werase(view->window);
4140 ffd1d5e5 2018-06-23 stsp if (limit == 0)
4141 ffd1d5e5 2018-06-23 stsp return NULL;
4143 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, label, view->ncols, 0);
4145 ffd1d5e5 2018-06-23 stsp return err;
4146 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4147 a3404814 2018-09-02 stsp wstandout(view->window);
4148 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4149 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
4150 a3404814 2018-09-02 stsp wstandend(view->window);
4151 2550e4c3 2018-07-13 stsp free(wline);
4152 2550e4c3 2018-07-13 stsp wline = NULL;
4153 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4154 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4155 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4156 ffd1d5e5 2018-06-23 stsp return NULL;
4157 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, parent_path, view->ncols, 0);
4159 ce52c690 2018-06-23 stsp return err;
4160 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4161 2550e4c3 2018-07-13 stsp free(wline);
4162 2550e4c3 2018-07-13 stsp wline = NULL;
4163 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4164 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4165 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4166 ffd1d5e5 2018-06-23 stsp return NULL;
4167 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4168 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
4169 a1eca9bb 2018-06-23 stsp return NULL;
4171 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
4172 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
4173 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
4174 0cf4efb1 2018-09-29 stsp if (view->focussed)
4175 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4176 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
4178 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
4179 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
4180 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4181 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4182 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4183 ffd1d5e5 2018-06-23 stsp return NULL;
4187 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
4188 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
4191 ffd1d5e5 2018-06-23 stsp while (te) {
4192 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
4193 848d6979 2019-08-12 stsp const char *modestr = "";
4195 1d13200f 2018-07-12 stsp if (show_ids) {
4196 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
4198 638f9024 2019-05-13 stsp return got_error_from_errno(
4199 230a42bd 2019-05-11 jcs "got_object_id_str");
4201 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4202 63c5ca5d 2019-08-24 stsp modestr = "$";
4203 63c5ca5d 2019-08-24 stsp else if (S_ISLNK(te->mode))
4204 848d6979 2019-08-12 stsp modestr = "@";
4205 848d6979 2019-08-12 stsp else if (S_ISDIR(te->mode))
4206 848d6979 2019-08-12 stsp modestr = "/";
4207 848d6979 2019-08-12 stsp else if (te->mode & S_IXUSR)
4208 848d6979 2019-08-12 stsp modestr = "*";
4209 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4210 848d6979 2019-08-12 stsp te->name, modestr) == -1) {
4211 1d13200f 2018-07-12 stsp free(id_str);
4212 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4214 1d13200f 2018-07-12 stsp free(id_str);
4215 27a741e5 2019-09-11 stsp err = format_line(&wline, &width, line, view->ncols, 0);
4216 ffd1d5e5 2018-06-23 stsp if (err) {
4217 ffd1d5e5 2018-06-23 stsp free(line);
4220 ffd1d5e5 2018-06-23 stsp if (n == selected) {
4221 0cf4efb1 2018-09-29 stsp if (view->focussed)
4222 0cf4efb1 2018-09-29 stsp wstandout(view->window);
4223 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
4225 c0b01bdb 2019-11-08 stsp lc = match_line_color(colors, line);
4227 c0b01bdb 2019-11-08 stsp wattr_on(view->window,
4228 c0b01bdb 2019-11-08 stsp COLOR_PAIR(lc->colorpair), NULL);
4229 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
4231 c0b01bdb 2019-11-08 stsp wattr_off(view->window,
4232 c0b01bdb 2019-11-08 stsp COLOR_PAIR(lc->colorpair), NULL);
4233 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
4234 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
4235 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
4236 f7d12f7e 2018-08-01 stsp wstandend(view->window);
4237 ffd1d5e5 2018-06-23 stsp free(line);
4238 2550e4c3 2018-07-13 stsp free(wline);
4239 2550e4c3 2018-07-13 stsp wline = NULL;
4241 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
4242 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
4243 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
4245 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
4248 ffd1d5e5 2018-06-23 stsp return err;
4251 ffd1d5e5 2018-06-23 stsp static void
4252 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
4253 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
4254 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
4256 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
4259 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
4262 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
4263 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
4264 ffd1d5e5 2018-06-23 stsp if (!isroot)
4265 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4269 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
4270 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
4271 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
4272 ffd1d5e5 2018-06-23 stsp prev = te;
4273 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
4275 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
4276 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
4278 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4279 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4282 768394f3 2019-01-24 stsp static int
4283 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4284 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
4285 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
4287 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4288 ffd1d5e5 2018-06-23 stsp int n = 0;
4290 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
4291 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4293 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
4294 768394f3 2019-01-24 stsp last = last_displayed_entry;
4295 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4296 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
4297 768394f3 2019-01-24 stsp if (last) {
4298 768394f3 2019-01-24 stsp *first_displayed_entry = next;
4299 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
4305 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4306 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4307 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4309 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4310 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4311 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4313 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4314 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
4316 ce52c690 2018-06-23 stsp len += strlen(te->name);
4318 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4319 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4320 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4322 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4323 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4324 d9765a41 2018-06-23 stsp while (pt) {
4325 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4326 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4327 cb2ebc8a 2018-06-23 stsp goto done;
4329 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
4330 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4331 cb2ebc8a 2018-06-23 stsp goto done;
4333 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4336 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
4337 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4338 ce52c690 2018-06-23 stsp goto done;
4342 ce52c690 2018-06-23 stsp if (err) {
4343 ce52c690 2018-06-23 stsp free(*path);
4344 ce52c690 2018-06-23 stsp *path = NULL;
4346 ce52c690 2018-06-23 stsp return err;
4349 ce52c690 2018-06-23 stsp static const struct got_error *
4350 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
4351 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4352 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4353 8b473291 2019-02-21 stsp struct got_repository *repo)
4355 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
4356 ce52c690 2018-06-23 stsp char *path;
4357 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
4359 a0de39f3 2019-08-09 stsp *new_view = NULL;
4361 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
4363 ce52c690 2018-06-23 stsp return err;
4365 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4366 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
4367 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
4368 83ce39e3 2019-08-12 stsp goto done;
4371 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
4372 e5a0f69f 2018-08-18 stsp if (err) {
4373 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
4374 fc06ba56 2019-08-22 stsp err = NULL;
4375 e5a0f69f 2018-08-18 stsp view_close(blame_view);
4377 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
4379 83ce39e3 2019-08-12 stsp free(path);
4380 69efd4c4 2018-07-18 stsp return err;
4383 69efd4c4 2018-07-18 stsp static const struct got_error *
4384 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
4385 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4386 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4387 8b473291 2019-02-21 stsp struct got_repository *repo)
4389 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
4390 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
4391 69efd4c4 2018-07-18 stsp char *path;
4393 a0de39f3 2019-08-09 stsp *new_view = NULL;
4395 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4396 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
4397 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
4399 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
4401 69efd4c4 2018-07-18 stsp return err;
4403 d01904d4 2019-06-25 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4405 e5a0f69f 2018-08-18 stsp view_close(log_view);
4407 e5a0f69f 2018-08-18 stsp *new_view = log_view;
4408 cb2ebc8a 2018-06-23 stsp free(path);
4409 cb2ebc8a 2018-06-23 stsp return err;
4412 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4413 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
4414 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4415 8b473291 2019-02-21 stsp struct got_repository *repo)
4417 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4418 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
4419 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4421 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
4423 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
4424 ffd1d5e5 2018-06-23 stsp if (err != NULL)
4425 ffd1d5e5 2018-06-23 stsp goto done;
4427 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4428 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4429 ffd1d5e5 2018-06-23 stsp goto done;
4432 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
4433 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
4434 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4435 8d0fe45a 2019-08-12 stsp s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4436 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
4437 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
4438 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4439 6484ec90 2018-09-29 stsp goto done;
4441 8b473291 2019-02-21 stsp s->refs = refs;
4442 fb2756b9 2018-08-04 stsp s->repo = repo;
4444 c0b01bdb 2019-11-08 stsp SIMPLEQ_INIT(&s->line_colors);
4446 c0b01bdb 2019-11-08 stsp if (has_colors() && getenv("TOG_COLORS") != NULL) {
4447 c0b01bdb 2019-11-08 stsp err = add_line_color(&s->line_colors,
4448 c0b01bdb 2019-11-08 stsp "\\$$", 1, get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4450 c0b01bdb 2019-11-08 stsp goto done;
4451 c0b01bdb 2019-11-08 stsp err = add_line_color(&s->line_colors,
4452 c0b01bdb 2019-11-08 stsp "@$", 2, get_color_value("TOG_COLOR_TREE_SYMLINK"));
4453 c0b01bdb 2019-11-08 stsp if (err) {
4454 c0b01bdb 2019-11-08 stsp free_line_colors(&s->line_colors);
4455 c0b01bdb 2019-11-08 stsp goto done;
4457 c0b01bdb 2019-11-08 stsp err = add_line_color(&s->line_colors,
4458 c0b01bdb 2019-11-08 stsp "/$", 3, get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4459 c0b01bdb 2019-11-08 stsp if (err) {
4460 c0b01bdb 2019-11-08 stsp free_line_colors(&s->line_colors);
4461 c0b01bdb 2019-11-08 stsp goto done;
4464 c0b01bdb 2019-11-08 stsp err = add_line_color(&s->line_colors,
4465 c0b01bdb 2019-11-08 stsp "\\*$", 4, get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4466 c0b01bdb 2019-11-08 stsp if (err) {
4467 c0b01bdb 2019-11-08 stsp free_line_colors(&s->line_colors);
4468 c0b01bdb 2019-11-08 stsp goto done;
4472 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
4473 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
4474 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
4475 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
4476 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
4478 ad80ab7b 2018-08-04 stsp free(commit_id_str);
4479 6484ec90 2018-09-29 stsp if (err) {
4480 fb2756b9 2018-08-04 stsp free(s->tree_label);
4481 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4483 ad80ab7b 2018-08-04 stsp return err;
4486 e5a0f69f 2018-08-18 stsp static const struct got_error *
4487 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
4489 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4491 c0b01bdb 2019-11-08 stsp free_line_colors(&s->line_colors);
4492 fb2756b9 2018-08-04 stsp free(s->tree_label);
4493 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4494 6484ec90 2018-09-29 stsp free(s->commit_id);
4495 6484ec90 2018-09-29 stsp s->commit_id = NULL;
4496 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
4497 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
4498 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
4499 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
4500 ad80ab7b 2018-08-04 stsp free(parent);
4503 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
4504 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
4505 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
4507 7c32bd05 2019-06-22 stsp return NULL;
4510 7c32bd05 2019-06-22 stsp static const struct got_error *
4511 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
4513 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4515 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
4516 7c32bd05 2019-06-22 stsp return NULL;
4519 7c32bd05 2019-06-22 stsp static int
4520 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4522 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
4524 7c32bd05 2019-06-22 stsp return regexec(regex, te->name, 1, ®match, 0) == 0;
4527 7c32bd05 2019-06-22 stsp static const struct got_error *
4528 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
4530 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4531 a0de39f3 2019-08-09 stsp struct got_tree_entry *entry = NULL, *te;
4533 7c32bd05 2019-06-22 stsp if (!view->searching) {
4534 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4535 7c32bd05 2019-06-22 stsp return NULL;
4538 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4539 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
4540 7c32bd05 2019-06-22 stsp if (s->selected_entry)
4541 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4543 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4546 7c32bd05 2019-06-22 stsp if (s->selected_entry == NULL) {
4547 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4548 7c32bd05 2019-06-22 stsp entry = te;
4550 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4551 7c32bd05 2019-06-22 stsp entry = te;
4552 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) ==
4553 7c32bd05 2019-06-22 stsp s->selected_entry)
4559 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4560 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4562 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4563 7c32bd05 2019-06-22 stsp entry = te;
4567 7c32bd05 2019-06-22 stsp while (1) {
4568 7c32bd05 2019-06-22 stsp if (entry == NULL) {
4569 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
4570 ac66afb8 2019-06-24 stsp view->search_next_done = 1;
4571 ac66afb8 2019-06-24 stsp return NULL;
4573 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4574 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4576 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4577 7c32bd05 2019-06-22 stsp entry = te;
4581 7c32bd05 2019-06-22 stsp if (match_tree_entry(entry, &view->regex)) {
4582 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4583 7c32bd05 2019-06-22 stsp s->matched_entry = entry;
4587 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4588 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(entry, entry);
4590 7c32bd05 2019-06-22 stsp if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4591 7c32bd05 2019-06-22 stsp entry = NULL;
4593 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4594 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) == entry) {
4595 7c32bd05 2019-06-22 stsp entry = te;
4603 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4604 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
4605 7c32bd05 2019-06-22 stsp s->selected = 0;
4608 e5a0f69f 2018-08-18 stsp return NULL;
4611 ad80ab7b 2018-08-04 stsp static const struct got_error *
4612 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
4614 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
4615 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4616 e5a0f69f 2018-08-18 stsp char *parent_path;
4618 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
4620 e5a0f69f 2018-08-18 stsp return err;
4622 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
4623 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
4624 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4625 c0b01bdb 2019-11-08 stsp s->entries, s->selected, view->nlines, s->tree == s->root,
4626 c0b01bdb 2019-11-08 stsp &s->line_colors);
4627 e5a0f69f 2018-08-18 stsp free(parent_path);
4629 669b5ffa 2018-10-07 stsp view_vborder(view);
4630 e5a0f69f 2018-08-18 stsp return err;
4633 e5a0f69f 2018-08-18 stsp static const struct got_error *
4634 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4635 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4637 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4638 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
4639 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
4640 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
4642 e5a0f69f 2018-08-18 stsp switch (ch) {
4644 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
4647 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
4649 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4650 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4651 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
4652 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
4653 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
4654 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4655 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4657 1e37a5c2 2019-05-12 jcs return err;
4658 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
4660 1e37a5c2 2019-05-12 jcs view_close(log_view);
4663 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
4664 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4666 1e37a5c2 2019-05-12 jcs *new_view = log_view;
4669 1e37a5c2 2019-05-12 jcs case KEY_UP:
4670 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
4671 1e37a5c2 2019-05-12 jcs s->selected--;
4672 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
4675 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
4677 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
4678 1e37a5c2 2019-05-12 jcs s->entries, s->tree == s->root);
4680 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4681 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
4682 1e37a5c2 2019-05-12 jcs MAX(0, view->nlines - 4 - s->selected), s->entries,
4683 1e37a5c2 2019-05-12 jcs s->tree == s->root);
4684 1e37a5c2 2019-05-12 jcs s->selected = 0;
4685 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_FIRST(&s->entries->head) ==
4686 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
4687 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
4690 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4691 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
4692 1e37a5c2 2019-05-12 jcs s->selected++;
4695 00ba99a7 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4696 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
4698 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
4699 1e37a5c2 2019-05-12 jcs s->last_displayed_entry, s->entries);
4701 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4702 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4704 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
4705 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
4706 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4709 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
4710 1e37a5c2 2019-05-12 jcs view->nlines, s->last_displayed_entry, s->entries);
4711 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
4712 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
4713 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
4714 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
4716 1e37a5c2 2019-05-12 jcs ndisplayed++;
4717 1e37a5c2 2019-05-12 jcs te = SIMPLEQ_NEXT(te, entry);
4718 1e37a5c2 2019-05-12 jcs } while (te);
4719 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
4722 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4724 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4725 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4726 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
4727 1e37a5c2 2019-05-12 jcs /* user selected '..' */
4728 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
4730 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
4731 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
4733 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
4734 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
4735 1e37a5c2 2019-05-12 jcs s->entries =
4736 1e37a5c2 2019-05-12 jcs got_object_tree_get_entries(s->tree);
4737 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
4738 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
4739 1e37a5c2 2019-05-12 jcs s->selected_entry =
4740 1e37a5c2 2019-05-12 jcs parent->selected_entry;
4741 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
4742 1e37a5c2 2019-05-12 jcs free(parent);
4743 1e37a5c2 2019-05-12 jcs } else if (S_ISDIR(s->selected_entry->mode)) {
4744 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
4745 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&subtree,
4746 1e37a5c2 2019-05-12 jcs s->repo, s->selected_entry->id);
4749 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
4750 941e9f74 2019-05-21 stsp if (err) {
4751 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
4754 1e37a5c2 2019-05-12 jcs } else if (S_ISREG(s->selected_entry->mode)) {
4755 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
4756 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
4757 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
4759 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
4760 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
4761 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
4764 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
4765 669b5ffa 2018-10-07 stsp err = view_close_child(view);
4767 669b5ffa 2018-10-07 stsp return err;
4768 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
4769 669b5ffa 2018-10-07 stsp if (err) {
4770 1e37a5c2 2019-05-12 jcs view_close(blame_view);
4773 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
4774 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
4776 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
4779 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4780 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
4781 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4787 ffd1d5e5 2018-06-23 stsp return err;
4790 ffd1d5e5 2018-06-23 stsp __dead static void
4791 ffd1d5e5 2018-06-23 stsp usage_tree(void)
4794 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4795 ffd1d5e5 2018-06-23 stsp getprogname());
4799 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4800 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
4802 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
4803 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
4804 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4805 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
4806 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4807 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
4808 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
4809 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
4811 5221c383 2018-08-01 stsp struct tog_view *view;
4813 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4815 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
4816 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4817 d188b9a6 2019-01-04 stsp NULL) == -1)
4818 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
4821 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
4822 ffd1d5e5 2018-06-23 stsp switch (ch) {
4824 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
4827 17020d27 2019-03-07 stsp usage_tree();
4828 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
4832 ffd1d5e5 2018-06-23 stsp argc -= optind;
4833 ffd1d5e5 2018-06-23 stsp argv += optind;
4835 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
4836 52185f70 2019-02-05 stsp struct got_worktree *worktree;
4837 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
4838 52185f70 2019-02-05 stsp if (cwd == NULL)
4839 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
4840 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4841 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4842 52185f70 2019-02-05 stsp goto done;
4843 52185f70 2019-02-05 stsp if (worktree) {
4844 52185f70 2019-02-05 stsp free(cwd);
4845 52185f70 2019-02-05 stsp repo_path =
4846 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4847 52185f70 2019-02-05 stsp got_worktree_close(worktree);
4849 52185f70 2019-02-05 stsp repo_path = cwd;
4850 52185f70 2019-02-05 stsp if (repo_path == NULL) {
4851 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4852 52185f70 2019-02-05 stsp goto done;
4854 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
4855 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
4856 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
4857 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
4859 ffd1d5e5 2018-06-23 stsp usage_log();
4861 a915003a 2019-02-05 stsp init_curses();
4863 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4864 c02c541e 2019-03-29 stsp if (error != NULL)
4865 52185f70 2019-02-05 stsp goto done;
4867 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4868 c02c541e 2019-03-29 stsp if (error)
4869 52185f70 2019-02-05 stsp goto done;
4871 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
4872 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4874 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4875 f2b6a97d 2019-07-15 stsp if (error) {
4876 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
4877 f2b6a97d 2019-07-15 stsp goto done;
4878 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&commit_id,
4879 f2b6a97d 2019-07-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4882 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4883 ffd1d5e5 2018-06-23 stsp goto done;
4885 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4886 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4887 ffd1d5e5 2018-06-23 stsp goto done;
4889 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
4890 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
4891 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4892 8b473291 2019-02-21 stsp goto done;
4894 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4895 8b473291 2019-02-21 stsp if (error)
4896 ffd1d5e5 2018-06-23 stsp goto done;
4898 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4899 5221c383 2018-08-01 stsp if (view == NULL) {
4900 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4901 5221c383 2018-08-01 stsp goto done;
4903 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
4904 ad80ab7b 2018-08-04 stsp if (error)
4905 ad80ab7b 2018-08-04 stsp goto done;
4906 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4908 52185f70 2019-02-05 stsp free(repo_path);
4909 ffd1d5e5 2018-06-23 stsp free(commit_id);
4910 ffd1d5e5 2018-06-23 stsp if (commit)
4911 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
4913 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
4915 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
4916 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4917 ffd1d5e5 2018-06-23 stsp return error;
4920 ce5b7c56 2019-07-09 stsp static void
4921 ce5b7c56 2019-07-09 stsp list_commands(void)
4925 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
4926 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
4927 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
4928 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->name);
4930 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
4933 4ed7e80c 2018-05-20 stsp __dead static void
4934 ce5b7c56 2019-07-09 stsp usage(int hflag)
4936 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4937 53ccebc2 2019-07-30 stsp getprogname());
4938 ce5b7c56 2019-07-09 stsp if (hflag)
4939 ce5b7c56 2019-07-09 stsp list_commands();
4943 c2301be8 2018-04-30 stsp static char **
4944 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
4946 c2301be8 2018-04-30 stsp char **argv;
4947 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
4949 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
4950 c2301be8 2018-04-30 stsp if (argv == NULL)
4951 c2301be8 2018-04-30 stsp err(1, "calloc");
4952 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
4953 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
4954 e10c916e 2019-09-15 hiltjo err(1, "strdup");
4955 c2301be8 2018-04-30 stsp if (arg1) {
4956 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
4957 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
4958 e10c916e 2019-09-15 hiltjo err(1, "strdup");
4961 c2301be8 2018-04-30 stsp return argv;
4965 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
4967 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
4968 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
4969 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
4970 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
4972 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
4974 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
4975 9f7d7167 2018-04-29 stsp switch (ch) {
4977 9f7d7167 2018-04-29 stsp hflag = 1;
4980 53ccebc2 2019-07-30 stsp Vflag = 1;
4983 ce5b7c56 2019-07-09 stsp usage(hflag);
4984 9f7d7167 2018-04-29 stsp /* NOTREACHED */
4988 9f7d7167 2018-04-29 stsp argc -= optind;
4989 9f7d7167 2018-04-29 stsp argv += optind;
4990 9f7d7167 2018-04-29 stsp optind = 0;
4991 c2301be8 2018-04-30 stsp optreset = 1;
4993 53ccebc2 2019-07-30 stsp if (Vflag) {
4994 53ccebc2 2019-07-30 stsp got_version_print_str();
4998 c2301be8 2018-04-30 stsp if (argc == 0) {
4999 f29d3e89 2018-06-23 stsp if (hflag)
5000 ce5b7c56 2019-07-09 stsp usage(hflag);
5001 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
5002 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
5003 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
5008 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
5009 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
5010 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
5011 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
5012 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
5017 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
5018 d70c3147 2019-07-09 stsp fprintf(stderr, "%s: unknown command '%s'\n",
5019 3642c4c6 2019-07-09 stsp getprogname(), argv[0]);
5020 ce5b7c56 2019-07-09 stsp list_commands();
5025 3642c4c6 2019-07-09 stsp if (hflag)
5026 3642c4c6 2019-07-09 stsp cmd->cmd_usage();
5028 3642c4c6 2019-07-09 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5031 c2301be8 2018-04-30 stsp free(cmd_argv);
5032 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
5033 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);