Blame


1 9f7d7167 2018-04-29 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 9f7d7167 2018-04-29 stsp *
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.
7 9f7d7167 2018-04-29 stsp *
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.
15 9f7d7167 2018-04-29 stsp */
16 9f7d7167 2018-04-29 stsp
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>
20 80ddbec8 2018-04-29 stsp
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>
40 9f7d7167 2018-04-29 stsp
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"
55 9f7d7167 2018-04-29 stsp
56 881b2d3e 2018-04-30 stsp #ifndef MIN
57 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 881b2d3e 2018-04-30 stsp #endif
59 881b2d3e 2018-04-30 stsp
60 2bd27830 2018-10-22 stsp #ifndef MAX
61 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 2bd27830 2018-10-22 stsp #endif
63 2bd27830 2018-10-22 stsp
64 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
65 2bd27830 2018-10-22 stsp
66 9f7d7167 2018-04-29 stsp #ifndef nitems
67 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 9f7d7167 2018-04-29 stsp #endif
69 9f7d7167 2018-04-29 stsp
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);
74 9f7d7167 2018-04-29 stsp };
75 9f7d7167 2018-04-29 stsp
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);
81 9f7d7167 2018-04-29 stsp
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 *[]);
86 9f7d7167 2018-04-29 stsp
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 },
92 9f7d7167 2018-04-29 stsp };
93 9f7d7167 2018-04-29 stsp
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
99 d6b05b5b 2018-08-04 stsp };
100 c3e9aa98 2019-05-13 jcs
101 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
102 d6b05b5b 2018-08-04 stsp
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;
107 1a76625f 2018-10-22 stsp int idx;
108 ba4f502b 2018-08-04 stsp };
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;
113 15a087fe 2019-02-21 stsp };
114 15a087fe 2019-02-21 stsp
115 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
116 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
117 15a087fe 2019-02-21 stsp FILE *f;
118 15a087fe 2019-02-21 stsp int first_displayed_line;
119 15a087fe 2019-02-21 stsp int last_displayed_line;
120 15a087fe 2019-02-21 stsp int eof;
121 15a087fe 2019-02-21 stsp int diff_context;
122 15a087fe 2019-02-21 stsp struct got_repository *repo;
123 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
124 15a087fe 2019-02-21 stsp
125 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
126 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
127 b01e7d3b 2018-08-04 stsp };
128 b01e7d3b 2018-08-04 stsp
129 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
130 1a76625f 2018-10-22 stsp
131 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
132 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
133 1a76625f 2018-10-22 stsp int commits_needed;
134 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
135 1a76625f 2018-10-22 stsp struct commit_queue *commits;
136 1a76625f 2018-10-22 stsp const char *in_repo_path;
137 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
138 1a76625f 2018-10-22 stsp struct got_repository *repo;
139 1a76625f 2018-10-22 stsp int log_complete;
140 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
141 1a76625f 2018-10-22 stsp struct tog_view *view;
142 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
143 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
144 1a76625f 2018-10-22 stsp };
145 1a76625f 2018-10-22 stsp
146 1a76625f 2018-10-22 stsp struct tog_log_view_state {
147 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
148 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
149 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
150 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
151 b01e7d3b 2018-08-04 stsp int selected;
152 b01e7d3b 2018-08-04 stsp char *in_repo_path;
153 d01904d4 2019-06-25 stsp const char *head_ref_name;
154 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
155 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
156 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
157 1a76625f 2018-10-22 stsp sig_atomic_t quit;
158 1a76625f 2018-10-22 stsp pthread_t thread;
159 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
160 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
161 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
162 ba4f502b 2018-08-04 stsp };
163 ba4f502b 2018-08-04 stsp
164 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
165 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
166 e9424729 2018-08-04 stsp int nlines;
167 e9424729 2018-08-04 stsp
168 e9424729 2018-08-04 stsp struct tog_view *view;
169 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
170 e9424729 2018-08-04 stsp int *quit;
171 e9424729 2018-08-04 stsp };
172 e9424729 2018-08-04 stsp
173 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
174 e9424729 2018-08-04 stsp const char *path;
175 e9424729 2018-08-04 stsp struct got_repository *repo;
176 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
177 e9424729 2018-08-04 stsp int *complete;
178 fc06ba56 2019-08-22 stsp got_cancel_cb cancel_cb;
179 fc06ba56 2019-08-22 stsp void *cancel_arg;
180 e9424729 2018-08-04 stsp };
181 e9424729 2018-08-04 stsp
182 e9424729 2018-08-04 stsp struct tog_blame {
183 e9424729 2018-08-04 stsp FILE *f;
184 e9424729 2018-08-04 stsp size_t filesize;
185 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
186 6fcac457 2018-11-19 stsp int nlines;
187 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
188 e9424729 2018-08-04 stsp pthread_t thread;
189 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
190 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
191 e9424729 2018-08-04 stsp const char *path;
192 e9424729 2018-08-04 stsp };
193 e9424729 2018-08-04 stsp
194 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
195 7cbe629d 2018-08-04 stsp int first_displayed_line;
196 7cbe629d 2018-08-04 stsp int last_displayed_line;
197 7cbe629d 2018-08-04 stsp int selected_line;
198 7cbe629d 2018-08-04 stsp int blame_complete;
199 e5a0f69f 2018-08-18 stsp int eof;
200 e5a0f69f 2018-08-18 stsp int done;
201 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
202 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
203 e5a0f69f 2018-08-18 stsp char *path;
204 7cbe629d 2018-08-04 stsp struct got_repository *repo;
205 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
206 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
207 e9424729 2018-08-04 stsp struct tog_blame blame;
208 6c4c42e0 2019-06-24 stsp int matched_line;
209 ad80ab7b 2018-08-04 stsp };
210 ad80ab7b 2018-08-04 stsp
211 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
212 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
213 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
214 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
215 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
216 ad80ab7b 2018-08-04 stsp int selected;
217 ad80ab7b 2018-08-04 stsp };
218 ad80ab7b 2018-08-04 stsp
219 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
220 ad80ab7b 2018-08-04 stsp
221 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
222 ad80ab7b 2018-08-04 stsp char *tree_label;
223 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
224 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
225 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
226 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
227 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
228 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
229 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
230 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
231 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
232 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
233 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
234 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
235 7cbe629d 2018-08-04 stsp };
236 7cbe629d 2018-08-04 stsp
237 669b5ffa 2018-10-07 stsp /*
238 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
239 669b5ffa 2018-10-07 stsp *
240 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
241 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
242 669b5ffa 2018-10-07 stsp * there is enough screen estate.
243 669b5ffa 2018-10-07 stsp *
244 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
245 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
246 669b5ffa 2018-10-07 stsp *
247 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
248 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
249 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
250 669b5ffa 2018-10-07 stsp *
251 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
252 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
253 669b5ffa 2018-10-07 stsp */
254 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
255 669b5ffa 2018-10-07 stsp
256 cc3c9aac 2018-08-01 stsp struct tog_view {
257 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
258 26ed57b2 2018-05-19 stsp WINDOW *window;
259 26ed57b2 2018-05-19 stsp PANEL *panel;
260 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
261 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
262 1004088d 2018-09-29 stsp int focussed;
263 669b5ffa 2018-10-07 stsp struct tog_view *parent;
264 669b5ffa 2018-10-07 stsp struct tog_view *child;
265 669b5ffa 2018-10-07 stsp int child_focussed;
266 5dc9f4bc 2018-08-04 stsp
267 5dc9f4bc 2018-08-04 stsp /* type-specific state */
268 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
269 5dc9f4bc 2018-08-04 stsp union {
270 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
271 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
272 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
273 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
274 5dc9f4bc 2018-08-04 stsp } state;
275 e5a0f69f 2018-08-18 stsp
276 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
277 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
278 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
279 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
280 60493ae3 2019-06-20 stsp
281 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
282 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
283 60493ae3 2019-06-20 stsp int searching;
284 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
285 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
286 60493ae3 2019-06-20 stsp int search_next_done;
287 1803e47f 2019-06-22 stsp regex_t regex;
288 cc3c9aac 2018-08-01 stsp };
289 cd0acaa7 2018-05-20 stsp
290 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
291 fb872ab2 2019-02-21 stsp struct got_object_id *, struct got_object_id *, struct tog_view *,
292 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
293 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
294 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
295 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
296 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
297 e5a0f69f 2018-08-18 stsp
298 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
299 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *,
300 d01904d4 2019-06-25 stsp struct got_repository *, const char *, const char *, int);
301 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
302 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
303 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
304 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
305 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
306 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
307 e5a0f69f 2018-08-18 stsp
308 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
309 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *, struct got_repository *);
310 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
311 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
312 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
313 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
314 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
315 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
316 e5a0f69f 2018-08-18 stsp
317 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
318 8b473291 2019-02-21 stsp struct got_tree_object *, struct got_object_id *,
319 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
320 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
321 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
322 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
323 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
324 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
325 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
326 25791caa 2018-10-24 stsp
327 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
328 83baff54 2019-08-12 stsp static volatile sig_atomic_t tog_sigpipe_received;
329 25791caa 2018-10-24 stsp
330 25791caa 2018-10-24 stsp static void
331 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
332 25791caa 2018-10-24 stsp {
333 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
334 83baff54 2019-08-12 stsp }
335 83baff54 2019-08-12 stsp
336 83baff54 2019-08-12 stsp static void
337 83baff54 2019-08-12 stsp tog_sigpipe(int signo)
338 83baff54 2019-08-12 stsp {
339 83baff54 2019-08-12 stsp tog_sigpipe_received = 1;
340 25791caa 2018-10-24 stsp }
341 26ed57b2 2018-05-19 stsp
342 e5a0f69f 2018-08-18 stsp static const struct got_error *
343 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
344 ea5e7bb5 2018-08-01 stsp {
345 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
346 e5a0f69f 2018-08-18 stsp
347 669b5ffa 2018-10-07 stsp if (view->child) {
348 669b5ffa 2018-10-07 stsp view_close(view->child);
349 669b5ffa 2018-10-07 stsp view->child = NULL;
350 669b5ffa 2018-10-07 stsp }
351 e5a0f69f 2018-08-18 stsp if (view->close)
352 e5a0f69f 2018-08-18 stsp err = view->close(view);
353 ea5e7bb5 2018-08-01 stsp if (view->panel)
354 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
355 ea5e7bb5 2018-08-01 stsp if (view->window)
356 ea5e7bb5 2018-08-01 stsp delwin(view->window);
357 ea5e7bb5 2018-08-01 stsp free(view);
358 e5a0f69f 2018-08-18 stsp return err;
359 ea5e7bb5 2018-08-01 stsp }
360 ea5e7bb5 2018-08-01 stsp
361 ea5e7bb5 2018-08-01 stsp static struct tog_view *
362 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
363 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
364 ea5e7bb5 2018-08-01 stsp {
365 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
366 ea5e7bb5 2018-08-01 stsp
367 ea5e7bb5 2018-08-01 stsp if (view == NULL)
368 ea5e7bb5 2018-08-01 stsp return NULL;
369 ea5e7bb5 2018-08-01 stsp
370 d6b05b5b 2018-08-04 stsp view->type = type;
371 f7d12f7e 2018-08-01 stsp view->lines = LINES;
372 f7d12f7e 2018-08-01 stsp view->cols = COLS;
373 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
374 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
375 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
376 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
377 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
378 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
379 96a765a8 2018-08-04 stsp view_close(view);
380 ea5e7bb5 2018-08-01 stsp return NULL;
381 ea5e7bb5 2018-08-01 stsp }
382 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
383 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
384 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
385 96a765a8 2018-08-04 stsp view_close(view);
386 ea5e7bb5 2018-08-01 stsp return NULL;
387 ea5e7bb5 2018-08-01 stsp }
388 ea5e7bb5 2018-08-01 stsp
389 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
390 ea5e7bb5 2018-08-01 stsp return view;
391 cdf1ee82 2018-08-01 stsp }
392 cdf1ee82 2018-08-01 stsp
393 0cf4efb1 2018-09-29 stsp static int
394 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
395 0cf4efb1 2018-09-29 stsp {
396 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
397 0cf4efb1 2018-09-29 stsp return 0;
398 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
399 5c60c32a 2018-10-18 stsp }
400 5c60c32a 2018-10-18 stsp
401 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
402 5c60c32a 2018-10-18 stsp
403 5c60c32a 2018-10-18 stsp static const struct got_error *
404 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
405 5c60c32a 2018-10-18 stsp {
406 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
407 5c60c32a 2018-10-18 stsp
408 5c60c32a 2018-10-18 stsp view->begin_y = 0;
409 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
410 5c60c32a 2018-10-18 stsp view->nlines = LINES;
411 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
412 5c60c32a 2018-10-18 stsp view->lines = LINES;
413 5c60c32a 2018-10-18 stsp view->cols = COLS;
414 5c60c32a 2018-10-18 stsp err = view_resize(view);
415 5c60c32a 2018-10-18 stsp if (err)
416 5c60c32a 2018-10-18 stsp return err;
417 5c60c32a 2018-10-18 stsp
418 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
419 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
420 5c60c32a 2018-10-18 stsp
421 5c60c32a 2018-10-18 stsp return NULL;
422 5c60c32a 2018-10-18 stsp }
423 5c60c32a 2018-10-18 stsp
424 5c60c32a 2018-10-18 stsp static const struct got_error *
425 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
426 5c60c32a 2018-10-18 stsp {
427 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
428 5c60c32a 2018-10-18 stsp
429 5c60c32a 2018-10-18 stsp view->begin_x = 0;
430 5c60c32a 2018-10-18 stsp view->begin_y = 0;
431 5c60c32a 2018-10-18 stsp view->nlines = LINES;
432 5c60c32a 2018-10-18 stsp view->ncols = COLS;
433 5c60c32a 2018-10-18 stsp view->lines = LINES;
434 5c60c32a 2018-10-18 stsp view->cols = COLS;
435 5c60c32a 2018-10-18 stsp err = view_resize(view);
436 5c60c32a 2018-10-18 stsp if (err)
437 5c60c32a 2018-10-18 stsp return err;
438 5c60c32a 2018-10-18 stsp
439 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
440 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
441 5c60c32a 2018-10-18 stsp
442 5c60c32a 2018-10-18 stsp return NULL;
443 0cf4efb1 2018-09-29 stsp }
444 0cf4efb1 2018-09-29 stsp
445 5c60c32a 2018-10-18 stsp static int
446 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
447 5c60c32a 2018-10-18 stsp {
448 5c60c32a 2018-10-18 stsp return view->parent == NULL;
449 5c60c32a 2018-10-18 stsp }
450 5c60c32a 2018-10-18 stsp
451 4d8c2215 2018-08-19 stsp static const struct got_error *
452 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
453 f7d12f7e 2018-08-01 stsp {
454 f7d12f7e 2018-08-01 stsp int nlines, ncols;
455 f7d12f7e 2018-08-01 stsp
456 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
457 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
458 0cf4efb1 2018-09-29 stsp else
459 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
460 f7d12f7e 2018-08-01 stsp
461 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
462 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
463 0cf4efb1 2018-09-29 stsp else
464 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
465 f7d12f7e 2018-08-01 stsp
466 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
467 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
468 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
469 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
470 25791caa 2018-10-24 stsp wclear(view->window);
471 f7d12f7e 2018-08-01 stsp
472 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
473 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
474 0cf4efb1 2018-09-29 stsp view->lines = LINES;
475 0cf4efb1 2018-09-29 stsp view->cols = COLS;
476 6d0fee91 2018-08-01 stsp
477 6e3e5d9c 2018-10-18 stsp if (view->child) {
478 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
479 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
480 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
481 5c60c32a 2018-10-18 stsp if (view->child->focussed)
482 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
483 5c60c32a 2018-10-18 stsp else
484 5c60c32a 2018-10-18 stsp show_panel(view->panel);
485 5c60c32a 2018-10-18 stsp } else {
486 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
487 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
488 5c60c32a 2018-10-18 stsp }
489 5c60c32a 2018-10-18 stsp }
490 669b5ffa 2018-10-07 stsp
491 5c60c32a 2018-10-18 stsp return NULL;
492 669b5ffa 2018-10-07 stsp }
493 669b5ffa 2018-10-07 stsp
494 669b5ffa 2018-10-07 stsp static const struct got_error *
495 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
496 669b5ffa 2018-10-07 stsp {
497 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
498 669b5ffa 2018-10-07 stsp
499 669b5ffa 2018-10-07 stsp if (view->child == NULL)
500 669b5ffa 2018-10-07 stsp return NULL;
501 669b5ffa 2018-10-07 stsp
502 669b5ffa 2018-10-07 stsp err = view_close(view->child);
503 669b5ffa 2018-10-07 stsp view->child = NULL;
504 669b5ffa 2018-10-07 stsp return err;
505 669b5ffa 2018-10-07 stsp }
506 669b5ffa 2018-10-07 stsp
507 669b5ffa 2018-10-07 stsp static const struct got_error *
508 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
509 669b5ffa 2018-10-07 stsp {
510 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
511 669b5ffa 2018-10-07 stsp
512 669b5ffa 2018-10-07 stsp view->child = child;
513 669b5ffa 2018-10-07 stsp child->parent = view;
514 669b5ffa 2018-10-07 stsp return err;
515 bfddd0d9 2018-09-29 stsp }
516 bfddd0d9 2018-09-29 stsp
517 bfddd0d9 2018-09-29 stsp static int
518 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
519 bfddd0d9 2018-09-29 stsp {
520 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
521 34bc9ec9 2019-02-22 stsp }
522 34bc9ec9 2019-02-22 stsp
523 34bc9ec9 2019-02-22 stsp static void
524 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
525 25791caa 2018-10-24 stsp {
526 25791caa 2018-10-24 stsp int cols, lines;
527 25791caa 2018-10-24 stsp struct winsize size;
528 25791caa 2018-10-24 stsp
529 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
530 25791caa 2018-10-24 stsp cols = 80; /* Default */
531 25791caa 2018-10-24 stsp lines = 24;
532 25791caa 2018-10-24 stsp } else {
533 25791caa 2018-10-24 stsp cols = size.ws_col;
534 25791caa 2018-10-24 stsp lines = size.ws_row;
535 25791caa 2018-10-24 stsp }
536 25791caa 2018-10-24 stsp resize_term(lines, cols);
537 2b49a8ae 2019-06-22 stsp }
538 2b49a8ae 2019-06-22 stsp
539 2b49a8ae 2019-06-22 stsp static const struct got_error *
540 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
541 2b49a8ae 2019-06-22 stsp {
542 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
543 2b49a8ae 2019-06-22 stsp char pattern[1024];
544 2b49a8ae 2019-06-22 stsp int ret;
545 7d049c18 2019-08-21 stsp int begin_x = 0;
546 2b49a8ae 2019-06-22 stsp
547 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
548 2b49a8ae 2019-06-22 stsp return NULL;
549 2b49a8ae 2019-06-22 stsp
550 7d049c18 2019-08-21 stsp if (!view_is_parent_view(view))
551 7d049c18 2019-08-21 stsp begin_x = view_split_begin_x(view->begin_x);
552 2b49a8ae 2019-06-22 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1,
553 7d049c18 2019-08-21 stsp begin_x, "/");
554 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
555 2b49a8ae 2019-06-22 stsp
556 2b49a8ae 2019-06-22 stsp nocbreak();
557 2b49a8ae 2019-06-22 stsp echo();
558 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
559 2b49a8ae 2019-06-22 stsp cbreak();
560 2b49a8ae 2019-06-22 stsp noecho();
561 2b49a8ae 2019-06-22 stsp if (ret == ERR)
562 2b49a8ae 2019-06-22 stsp return NULL;
563 2b49a8ae 2019-06-22 stsp
564 2b49a8ae 2019-06-22 stsp if (view->searching) {
565 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
566 2b49a8ae 2019-06-22 stsp view->searching = 0;
567 2b49a8ae 2019-06-22 stsp }
568 2b49a8ae 2019-06-22 stsp
569 2b49a8ae 2019-06-22 stsp if (regcomp(&view->regex, pattern,
570 f5daf9b1 2019-06-24 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
571 7c32bd05 2019-06-22 stsp err = view->search_start(view);
572 7c32bd05 2019-06-22 stsp if (err) {
573 7c32bd05 2019-06-22 stsp regfree(&view->regex);
574 7c32bd05 2019-06-22 stsp return err;
575 7c32bd05 2019-06-22 stsp }
576 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
577 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
578 2b49a8ae 2019-06-22 stsp view->search_next(view);
579 2b49a8ae 2019-06-22 stsp }
580 2b49a8ae 2019-06-22 stsp
581 2b49a8ae 2019-06-22 stsp return NULL;
582 0cf4efb1 2018-09-29 stsp }
583 6d0fee91 2018-08-01 stsp
584 0cf4efb1 2018-09-29 stsp static const struct got_error *
585 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
586 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
587 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
588 e5a0f69f 2018-08-18 stsp {
589 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
590 669b5ffa 2018-10-07 stsp struct tog_view *v;
591 1a76625f 2018-10-22 stsp int ch, errcode;
592 e5a0f69f 2018-08-18 stsp
593 e5a0f69f 2018-08-18 stsp *new = NULL;
594 e5a0f69f 2018-08-18 stsp *dead = NULL;
595 0cf4efb1 2018-09-29 stsp *focus = NULL;
596 e5a0f69f 2018-08-18 stsp
597 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
598 60493ae3 2019-06-20 stsp errcode = pthread_mutex_unlock(&tog_mutex);
599 60493ae3 2019-06-20 stsp if (errcode)
600 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
601 60493ae3 2019-06-20 stsp "pthread_mutex_unlock");
602 60493ae3 2019-06-20 stsp pthread_yield();
603 60493ae3 2019-06-20 stsp errcode = pthread_mutex_lock(&tog_mutex);
604 60493ae3 2019-06-20 stsp if (errcode)
605 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
606 60493ae3 2019-06-20 stsp "pthread_mutex_lock");
607 60493ae3 2019-06-20 stsp view->search_next(view);
608 60493ae3 2019-06-20 stsp return NULL;
609 60493ae3 2019-06-20 stsp }
610 60493ae3 2019-06-20 stsp
611 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
612 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
613 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
614 1a76625f 2018-10-22 stsp if (errcode)
615 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
616 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
617 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
618 1a76625f 2018-10-22 stsp if (errcode)
619 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
620 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
621 25791caa 2018-10-24 stsp
622 25791caa 2018-10-24 stsp if (tog_sigwinch_received) {
623 25791caa 2018-10-24 stsp tog_resizeterm();
624 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
625 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
626 25791caa 2018-10-24 stsp err = view_resize(v);
627 25791caa 2018-10-24 stsp if (err)
628 25791caa 2018-10-24 stsp return err;
629 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
630 25791caa 2018-10-24 stsp if (err)
631 25791caa 2018-10-24 stsp return err;
632 25791caa 2018-10-24 stsp }
633 25791caa 2018-10-24 stsp }
634 25791caa 2018-10-24 stsp
635 e5a0f69f 2018-08-18 stsp switch (ch) {
636 1e37a5c2 2019-05-12 jcs case ERR:
637 1e37a5c2 2019-05-12 jcs break;
638 1e37a5c2 2019-05-12 jcs case '\t':
639 1e37a5c2 2019-05-12 jcs if (view->child) {
640 1e37a5c2 2019-05-12 jcs *focus = view->child;
641 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
642 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
643 1e37a5c2 2019-05-12 jcs *focus = view->parent;
644 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
645 1e37a5c2 2019-05-12 jcs }
646 1e37a5c2 2019-05-12 jcs break;
647 1e37a5c2 2019-05-12 jcs case 'q':
648 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
649 1e37a5c2 2019-05-12 jcs *dead = view;
650 1e37a5c2 2019-05-12 jcs break;
651 1e37a5c2 2019-05-12 jcs case 'Q':
652 1e37a5c2 2019-05-12 jcs *done = 1;
653 1e37a5c2 2019-05-12 jcs break;
654 1e37a5c2 2019-05-12 jcs case 'f':
655 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
656 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
657 1e37a5c2 2019-05-12 jcs break;
658 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
659 669b5ffa 2018-10-07 stsp *focus = view->child;
660 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
661 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
662 1e37a5c2 2019-05-12 jcs } else
663 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
664 1e37a5c2 2019-05-12 jcs if (err)
665 1e37a5c2 2019-05-12 jcs break;
666 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
667 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
668 1e37a5c2 2019-05-12 jcs } else {
669 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
670 1e37a5c2 2019-05-12 jcs *focus = view;
671 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
672 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
673 1e37a5c2 2019-05-12 jcs } else {
674 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
675 669b5ffa 2018-10-07 stsp }
676 1e37a5c2 2019-05-12 jcs if (err)
677 1e37a5c2 2019-05-12 jcs break;
678 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
679 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
680 1e37a5c2 2019-05-12 jcs }
681 1e37a5c2 2019-05-12 jcs break;
682 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
683 60493ae3 2019-06-20 stsp break;
684 60493ae3 2019-06-20 stsp case '/':
685 60493ae3 2019-06-20 stsp if (view->search_start)
686 2b49a8ae 2019-06-22 stsp view_search_start(view);
687 60493ae3 2019-06-20 stsp else
688 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
689 1e37a5c2 2019-05-12 jcs break;
690 b1bf1435 2019-06-21 stsp case 'N':
691 60493ae3 2019-06-20 stsp case 'n':
692 60493ae3 2019-06-20 stsp if (view->search_next && view->searching) {
693 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
694 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
695 60493ae3 2019-06-20 stsp view->search_next_done = 0;
696 60493ae3 2019-06-20 stsp view->search_next(view);
697 60493ae3 2019-06-20 stsp } else
698 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
699 60493ae3 2019-06-20 stsp break;
700 1e37a5c2 2019-05-12 jcs default:
701 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
702 1e37a5c2 2019-05-12 jcs break;
703 e5a0f69f 2018-08-18 stsp }
704 e5a0f69f 2018-08-18 stsp
705 e5a0f69f 2018-08-18 stsp return err;
706 e5a0f69f 2018-08-18 stsp }
707 e5a0f69f 2018-08-18 stsp
708 1a57306a 2018-09-02 stsp void
709 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
710 1a57306a 2018-09-02 stsp {
711 0cf4efb1 2018-09-29 stsp PANEL *panel;
712 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
713 0cf4efb1 2018-09-29 stsp
714 669b5ffa 2018-10-07 stsp if (view->parent)
715 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
716 669b5ffa 2018-10-07 stsp
717 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
718 0cf4efb1 2018-09-29 stsp if (panel == NULL)
719 1a57306a 2018-09-02 stsp return;
720 1a57306a 2018-09-02 stsp
721 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
722 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
723 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
724 bcbd79e2 2018-08-19 stsp }
725 bcbd79e2 2018-08-19 stsp
726 a3404814 2018-09-02 stsp int
727 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
728 a3404814 2018-09-02 stsp {
729 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
730 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
731 669b5ffa 2018-10-07 stsp return 0;
732 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
733 669b5ffa 2018-10-07 stsp return 0;
734 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
735 a3404814 2018-09-02 stsp return 0;
736 a3404814 2018-09-02 stsp
737 669b5ffa 2018-10-07 stsp return view->focussed;
738 a3404814 2018-09-02 stsp }
739 a3404814 2018-09-02 stsp
740 bcbd79e2 2018-08-19 stsp static const struct got_error *
741 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
742 e5a0f69f 2018-08-18 stsp {
743 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
744 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
745 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
746 fd823528 2018-10-22 stsp int fast_refresh = 10;
747 1a76625f 2018-10-22 stsp int done = 0, errcode;
748 e5a0f69f 2018-08-18 stsp
749 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
750 1a76625f 2018-10-22 stsp if (errcode)
751 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
752 1a76625f 2018-10-22 stsp
753 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
754 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
755 e5a0f69f 2018-08-18 stsp
756 a81bf10d 2018-09-29 stsp main_view = view;
757 1004088d 2018-09-29 stsp view->focussed = 1;
758 878940b7 2018-09-29 stsp err = view->show(view);
759 0cf4efb1 2018-09-29 stsp if (err)
760 0cf4efb1 2018-09-29 stsp return err;
761 0cf4efb1 2018-09-29 stsp update_panels();
762 0cf4efb1 2018-09-29 stsp doupdate();
763 83baff54 2019-08-12 stsp while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
764 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
765 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
766 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
767 fd823528 2018-10-22 stsp
768 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
769 e4197bf9 2018-08-18 stsp view, &views);
770 e5a0f69f 2018-08-18 stsp if (err)
771 e5a0f69f 2018-08-18 stsp break;
772 e5a0f69f 2018-08-18 stsp if (dead_view) {
773 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
774 669b5ffa 2018-10-07 stsp
775 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
776 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
777 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
778 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
779 669b5ffa 2018-10-07 stsp prev = view->parent;
780 669b5ffa 2018-10-07 stsp
781 669b5ffa 2018-10-07 stsp if (dead_view->parent)
782 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
783 669b5ffa 2018-10-07 stsp else
784 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
785 669b5ffa 2018-10-07 stsp
786 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
787 d01904d4 2019-06-25 stsp if (err || (dead_view == main_view && new_view == NULL))
788 e5a0f69f 2018-08-18 stsp goto done;
789 669b5ffa 2018-10-07 stsp
790 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
791 0cf4efb1 2018-09-29 stsp if (focus_view)
792 0cf4efb1 2018-09-29 stsp view = focus_view;
793 669b5ffa 2018-10-07 stsp else if (prev)
794 669b5ffa 2018-10-07 stsp view = prev;
795 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
796 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
797 0cf4efb1 2018-09-29 stsp tog_view_list_head);
798 669b5ffa 2018-10-07 stsp else
799 0cf4efb1 2018-09-29 stsp view = NULL;
800 669b5ffa 2018-10-07 stsp if (view) {
801 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
802 669b5ffa 2018-10-07 stsp focus_view = view->child;
803 669b5ffa 2018-10-07 stsp else
804 669b5ffa 2018-10-07 stsp focus_view = view;
805 669b5ffa 2018-10-07 stsp }
806 0cf4efb1 2018-09-29 stsp }
807 e5a0f69f 2018-08-18 stsp }
808 bcbd79e2 2018-08-19 stsp if (new_view) {
809 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
810 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
811 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
812 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
813 86c66b02 2018-10-18 stsp continue;
814 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
815 86c66b02 2018-10-18 stsp err = view_close(v);
816 86c66b02 2018-10-18 stsp if (err)
817 86c66b02 2018-10-18 stsp goto done;
818 86c66b02 2018-10-18 stsp break;
819 86c66b02 2018-10-18 stsp }
820 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
821 fed7eaa8 2018-10-24 stsp view = new_view;
822 878940b7 2018-09-29 stsp if (focus_view == NULL)
823 878940b7 2018-09-29 stsp focus_view = new_view;
824 1a76625f 2018-10-22 stsp }
825 0cf4efb1 2018-09-29 stsp if (focus_view) {
826 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
827 669b5ffa 2018-10-07 stsp if (view)
828 0cf4efb1 2018-09-29 stsp view->focussed = 0;
829 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
830 0cf4efb1 2018-09-29 stsp view = focus_view;
831 878940b7 2018-09-29 stsp if (new_view)
832 878940b7 2018-09-29 stsp show_panel(new_view->panel);
833 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
834 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
835 bcbd79e2 2018-08-19 stsp }
836 669b5ffa 2018-10-07 stsp if (view) {
837 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
838 758194b5 2018-10-24 stsp view->focussed = 1;
839 758194b5 2018-10-24 stsp show_panel(view->panel);
840 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
841 758194b5 2018-10-24 stsp show_panel(view->child->panel);
842 1a76625f 2018-10-22 stsp focus_view = view;
843 1a76625f 2018-10-22 stsp }
844 669b5ffa 2018-10-07 stsp if (view->parent) {
845 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
846 669b5ffa 2018-10-07 stsp if (err)
847 1a76625f 2018-10-22 stsp goto done;
848 669b5ffa 2018-10-07 stsp }
849 669b5ffa 2018-10-07 stsp err = view->show(view);
850 0cf4efb1 2018-09-29 stsp if (err)
851 1a76625f 2018-10-22 stsp goto done;
852 669b5ffa 2018-10-07 stsp if (view->child) {
853 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
854 669b5ffa 2018-10-07 stsp if (err)
855 1a76625f 2018-10-22 stsp goto done;
856 669b5ffa 2018-10-07 stsp }
857 1a76625f 2018-10-22 stsp update_panels();
858 1a76625f 2018-10-22 stsp doupdate();
859 0cf4efb1 2018-09-29 stsp }
860 e5a0f69f 2018-08-18 stsp }
861 e5a0f69f 2018-08-18 stsp done:
862 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
863 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
864 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
865 e5a0f69f 2018-08-18 stsp view_close(view);
866 e5a0f69f 2018-08-18 stsp }
867 1a76625f 2018-10-22 stsp
868 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
869 963ecf2a 2019-08-12 stsp if (errcode && err == NULL)
870 963ecf2a 2019-08-12 stsp err = got_error_set_errno(errcode, "pthread_mutex_unlock");
871 1a76625f 2018-10-22 stsp
872 e5a0f69f 2018-08-18 stsp return err;
873 ea5e7bb5 2018-08-01 stsp }
874 ea5e7bb5 2018-08-01 stsp
875 4ed7e80c 2018-05-20 stsp __dead static void
876 9f7d7167 2018-04-29 stsp usage_log(void)
877 9f7d7167 2018-04-29 stsp {
878 80ddbec8 2018-04-29 stsp endwin();
879 c70c5802 2018-08-01 stsp fprintf(stderr,
880 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
881 9f7d7167 2018-04-29 stsp getprogname());
882 9f7d7167 2018-04-29 stsp exit(1);
883 80ddbec8 2018-04-29 stsp }
884 80ddbec8 2018-04-29 stsp
885 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
886 80ddbec8 2018-04-29 stsp static const struct got_error *
887 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
888 963b370f 2018-05-20 stsp {
889 00dfcb92 2018-06-11 stsp char *vis = NULL;
890 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
891 963b370f 2018-05-20 stsp
892 963b370f 2018-05-20 stsp *ws = NULL;
893 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
894 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
895 00dfcb92 2018-06-11 stsp int vislen;
896 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
897 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
898 00dfcb92 2018-06-11 stsp
899 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
900 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
901 00dfcb92 2018-06-11 stsp if (err)
902 00dfcb92 2018-06-11 stsp return err;
903 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
904 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
905 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
906 a7f50699 2018-06-11 stsp goto done;
907 a7f50699 2018-06-11 stsp }
908 00dfcb92 2018-06-11 stsp }
909 963b370f 2018-05-20 stsp
910 fd9f4a2d 2019-08-28 hiltjo *ws = calloc(*wlen + 1, sizeof(**ws));
911 a7f50699 2018-06-11 stsp if (*ws == NULL) {
912 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
913 a7f50699 2018-06-11 stsp goto done;
914 a7f50699 2018-06-11 stsp }
915 963b370f 2018-05-20 stsp
916 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
917 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
918 a7f50699 2018-06-11 stsp done:
919 00dfcb92 2018-06-11 stsp free(vis);
920 963b370f 2018-05-20 stsp if (err) {
921 963b370f 2018-05-20 stsp free(*ws);
922 963b370f 2018-05-20 stsp *ws = NULL;
923 963b370f 2018-05-20 stsp *wlen = 0;
924 963b370f 2018-05-20 stsp }
925 963b370f 2018-05-20 stsp return err;
926 963b370f 2018-05-20 stsp }
927 963b370f 2018-05-20 stsp
928 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
929 963b370f 2018-05-20 stsp static const struct got_error *
930 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
931 963b370f 2018-05-20 stsp {
932 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
933 963b370f 2018-05-20 stsp int cols = 0;
934 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
935 963b370f 2018-05-20 stsp size_t wlen;
936 963b370f 2018-05-20 stsp int i;
937 963b370f 2018-05-20 stsp
938 963b370f 2018-05-20 stsp *wlinep = NULL;
939 b700b5d6 2018-07-10 stsp *widthp = 0;
940 963b370f 2018-05-20 stsp
941 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
942 963b370f 2018-05-20 stsp if (err)
943 963b370f 2018-05-20 stsp return err;
944 963b370f 2018-05-20 stsp
945 963b370f 2018-05-20 stsp i = 0;
946 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
947 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
948 963b370f 2018-05-20 stsp switch (width) {
949 963b370f 2018-05-20 stsp case 0:
950 b700b5d6 2018-07-10 stsp i++;
951 963b370f 2018-05-20 stsp break;
952 963b370f 2018-05-20 stsp case 1:
953 963b370f 2018-05-20 stsp case 2:
954 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
955 b700b5d6 2018-07-10 stsp cols += width;
956 3c1f04f1 2018-09-13 stsp i++;
957 963b370f 2018-05-20 stsp break;
958 963b370f 2018-05-20 stsp case -1:
959 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
960 67ceb59d 2019-03-03 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
961 b700b5d6 2018-07-10 stsp i++;
962 963b370f 2018-05-20 stsp break;
963 963b370f 2018-05-20 stsp default:
964 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
965 963b370f 2018-05-20 stsp goto done;
966 963b370f 2018-05-20 stsp }
967 963b370f 2018-05-20 stsp }
968 963b370f 2018-05-20 stsp wline[i] = L'\0';
969 b700b5d6 2018-07-10 stsp if (widthp)
970 b700b5d6 2018-07-10 stsp *widthp = cols;
971 963b370f 2018-05-20 stsp done:
972 963b370f 2018-05-20 stsp if (err)
973 963b370f 2018-05-20 stsp free(wline);
974 963b370f 2018-05-20 stsp else
975 963b370f 2018-05-20 stsp *wlinep = wline;
976 963b370f 2018-05-20 stsp return err;
977 963b370f 2018-05-20 stsp }
978 963b370f 2018-05-20 stsp
979 8b473291 2019-02-21 stsp static const struct got_error*
980 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
981 52b5abe1 2019-08-13 stsp struct got_object_id *id, struct got_repository *repo)
982 8b473291 2019-02-21 stsp {
983 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
984 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
985 8b473291 2019-02-21 stsp char *s;
986 8b473291 2019-02-21 stsp const char *name;
987 8b473291 2019-02-21 stsp
988 8b473291 2019-02-21 stsp *refs_str = NULL;
989 8b473291 2019-02-21 stsp
990 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
991 52b5abe1 2019-08-13 stsp struct got_tag_object *tag = NULL;
992 52b5abe1 2019-08-13 stsp int cmp;
993 52b5abe1 2019-08-13 stsp
994 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
995 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
996 8b473291 2019-02-21 stsp continue;
997 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
998 8b473291 2019-02-21 stsp name += 5;
999 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1000 7143d404 2019-03-12 stsp continue;
1001 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
1002 8b473291 2019-02-21 stsp name += 6;
1003 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
1004 8b473291 2019-02-21 stsp name += 8;
1005 52b5abe1 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1006 52b5abe1 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1007 5d844a1e 2019-08-13 stsp if (err) {
1008 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1009 5d844a1e 2019-08-13 stsp break;
1010 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1011 5d844a1e 2019-08-13 stsp err = NULL;
1012 5d844a1e 2019-08-13 stsp tag = NULL;
1013 5d844a1e 2019-08-13 stsp }
1014 52b5abe1 2019-08-13 stsp }
1015 52b5abe1 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1016 52b5abe1 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1017 52b5abe1 2019-08-13 stsp if (tag)
1018 52b5abe1 2019-08-13 stsp got_object_tag_close(tag);
1019 52b5abe1 2019-08-13 stsp if (cmp != 0)
1020 52b5abe1 2019-08-13 stsp continue;
1021 8b473291 2019-02-21 stsp s = *refs_str;
1022 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
1023 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
1024 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1025 8b473291 2019-02-21 stsp free(s);
1026 8b473291 2019-02-21 stsp *refs_str = NULL;
1027 8b473291 2019-02-21 stsp break;
1028 8b473291 2019-02-21 stsp }
1029 8b473291 2019-02-21 stsp free(s);
1030 8b473291 2019-02-21 stsp }
1031 8b473291 2019-02-21 stsp
1032 8b473291 2019-02-21 stsp return err;
1033 8b473291 2019-02-21 stsp }
1034 8b473291 2019-02-21 stsp
1035 963b370f 2018-05-20 stsp static const struct got_error *
1036 5813d178 2019-03-09 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1037 5813d178 2019-03-09 stsp {
1038 5813d178 2019-03-09 stsp char *smallerthan, *at;
1039 5813d178 2019-03-09 stsp
1040 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1041 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1042 5813d178 2019-03-09 stsp author = smallerthan + 1;
1043 5813d178 2019-03-09 stsp at = strchr(author, '@');
1044 5813d178 2019-03-09 stsp if (at)
1045 5813d178 2019-03-09 stsp *at = '\0';
1046 5813d178 2019-03-09 stsp return format_line(wauthor, author_width, author, limit);
1047 5813d178 2019-03-09 stsp }
1048 5813d178 2019-03-09 stsp
1049 5813d178 2019-03-09 stsp static const struct got_error *
1050 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1051 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
1052 5813d178 2019-03-09 stsp int author_display_cols)
1053 80ddbec8 2018-04-29 stsp {
1054 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1055 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1056 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1057 5813d178 2019-03-09 stsp char *author = NULL;
1058 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1059 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1060 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1061 bb737323 2018-05-20 stsp int col, limit;
1062 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
1063 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1064 ccb26ccd 2018-11-05 stsp struct tm tm;
1065 45d799e2 2018-12-23 stsp time_t committer_time;
1066 80ddbec8 2018-04-29 stsp
1067 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1068 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1069 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1070 ccb26ccd 2018-11-05 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1071 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1072 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1073 b39d25c7 2018-07-10 stsp
1074 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
1075 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1076 b39d25c7 2018-07-10 stsp else
1077 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1078 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1079 b39d25c7 2018-07-10 stsp col = limit + 1;
1080 b39d25c7 2018-07-10 stsp if (col > avail)
1081 b39d25c7 2018-07-10 stsp goto done;
1082 b39d25c7 2018-07-10 stsp
1083 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1084 5813d178 2019-03-09 stsp if (author == NULL) {
1085 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1086 80ddbec8 2018-04-29 stsp goto done;
1087 80ddbec8 2018-04-29 stsp }
1088 5813d178 2019-03-09 stsp err = format_author(&wauthor, &author_width, author, avail - col);
1089 bb737323 2018-05-20 stsp if (err)
1090 bb737323 2018-05-20 stsp goto done;
1091 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1092 bb737323 2018-05-20 stsp col += author_width;
1093 5813d178 2019-03-09 stsp while (col <= avail && author_width < author_display_cols + 2) {
1094 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1095 bb737323 2018-05-20 stsp col++;
1096 bb737323 2018-05-20 stsp author_width++;
1097 bb737323 2018-05-20 stsp }
1098 9c2eaf34 2018-05-20 stsp if (col > avail)
1099 9c2eaf34 2018-05-20 stsp goto done;
1100 80ddbec8 2018-04-29 stsp
1101 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1102 5943eee2 2019-08-13 stsp if (err)
1103 6d9fbc00 2018-04-29 stsp goto done;
1104 bb737323 2018-05-20 stsp logmsg = logmsg0;
1105 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1106 bb737323 2018-05-20 stsp logmsg++;
1107 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1108 bb737323 2018-05-20 stsp if (newline)
1109 bb737323 2018-05-20 stsp *newline = '\0';
1110 bb737323 2018-05-20 stsp limit = avail - col;
1111 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1112 bb737323 2018-05-20 stsp if (err)
1113 bb737323 2018-05-20 stsp goto done;
1114 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1115 bb737323 2018-05-20 stsp col += logmsg_width;
1116 bb737323 2018-05-20 stsp while (col <= avail) {
1117 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1118 bb737323 2018-05-20 stsp col++;
1119 881b2d3e 2018-04-30 stsp }
1120 80ddbec8 2018-04-29 stsp done:
1121 80ddbec8 2018-04-29 stsp free(logmsg0);
1122 bb737323 2018-05-20 stsp free(wlogmsg);
1123 5813d178 2019-03-09 stsp free(author);
1124 bb737323 2018-05-20 stsp free(wauthor);
1125 80ddbec8 2018-04-29 stsp free(line);
1126 80ddbec8 2018-04-29 stsp return err;
1127 80ddbec8 2018-04-29 stsp }
1128 26ed57b2 2018-05-19 stsp
1129 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1130 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1131 899d86c2 2018-05-10 stsp struct got_object_id *id)
1132 80ddbec8 2018-04-29 stsp {
1133 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1134 80ddbec8 2018-04-29 stsp
1135 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1136 80ddbec8 2018-04-29 stsp if (entry == NULL)
1137 899d86c2 2018-05-10 stsp return NULL;
1138 99db9666 2018-05-07 stsp
1139 899d86c2 2018-05-10 stsp entry->id = id;
1140 99db9666 2018-05-07 stsp entry->commit = commit;
1141 899d86c2 2018-05-10 stsp return entry;
1142 99db9666 2018-05-07 stsp }
1143 80ddbec8 2018-04-29 stsp
1144 99db9666 2018-05-07 stsp static void
1145 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1146 99db9666 2018-05-07 stsp {
1147 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1148 99db9666 2018-05-07 stsp
1149 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1150 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1151 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1152 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1153 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1154 99db9666 2018-05-07 stsp free(entry);
1155 99db9666 2018-05-07 stsp }
1156 99db9666 2018-05-07 stsp
1157 99db9666 2018-05-07 stsp static void
1158 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1159 99db9666 2018-05-07 stsp {
1160 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1161 99db9666 2018-05-07 stsp pop_commit(commits);
1162 c4972b91 2018-05-07 stsp }
1163 c4972b91 2018-05-07 stsp
1164 c4972b91 2018-05-07 stsp static const struct got_error *
1165 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1166 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
1167 c4972b91 2018-05-07 stsp {
1168 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1169 93e45b7c 2018-09-24 stsp int nqueued = 0;
1170 9ba79e04 2018-06-11 stsp
1171 1a76625f 2018-10-22 stsp /*
1172 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1173 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1174 1a76625f 2018-10-22 stsp * while updating the display.
1175 1a76625f 2018-10-22 stsp */
1176 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
1177 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1178 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1179 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1180 1a76625f 2018-10-22 stsp int errcode;
1181 899d86c2 2018-05-10 stsp
1182 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1183 9ba79e04 2018-06-11 stsp if (err) {
1184 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1185 93e45b7c 2018-09-24 stsp break;
1186 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
1187 6fb7cd11 2019-08-22 stsp minqueue, repo, NULL, NULL);
1188 ecb28ae0 2018-07-16 stsp if (err)
1189 ecb28ae0 2018-07-16 stsp return err;
1190 ecb28ae0 2018-07-16 stsp continue;
1191 9ba79e04 2018-06-11 stsp }
1192 93e45b7c 2018-09-24 stsp
1193 ecb28ae0 2018-07-16 stsp if (id == NULL)
1194 ecb28ae0 2018-07-16 stsp break;
1195 899d86c2 2018-05-10 stsp
1196 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1197 9ba79e04 2018-06-11 stsp if (err)
1198 9ba79e04 2018-06-11 stsp break;
1199 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1200 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1201 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1202 9ba79e04 2018-06-11 stsp break;
1203 9ba79e04 2018-06-11 stsp }
1204 93e45b7c 2018-09-24 stsp
1205 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1206 1a76625f 2018-10-22 stsp if (errcode) {
1207 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_lock");
1208 1a76625f 2018-10-22 stsp break;
1209 1a76625f 2018-10-22 stsp }
1210 1a76625f 2018-10-22 stsp
1211 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1212 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1213 ecb28ae0 2018-07-16 stsp nqueued++;
1214 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1215 1a76625f 2018-10-22 stsp
1216 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1217 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1218 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1219 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1220 899d86c2 2018-05-10 stsp }
1221 899d86c2 2018-05-10 stsp
1222 9ba79e04 2018-06-11 stsp return err;
1223 99db9666 2018-05-07 stsp }
1224 99db9666 2018-05-07 stsp
1225 99db9666 2018-05-07 stsp static const struct got_error *
1226 19e70ad6 2019-05-14 stsp get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1227 19e70ad6 2019-05-14 stsp struct got_repository *repo)
1228 99db9666 2018-05-07 stsp {
1229 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1230 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1231 99db9666 2018-05-07 stsp
1232 9ba79e04 2018-06-11 stsp *head_id = NULL;
1233 899d86c2 2018-05-10 stsp
1234 19e70ad6 2019-05-14 stsp err = got_ref_open(&head_ref, repo, branch_name, 0);
1235 99db9666 2018-05-07 stsp if (err)
1236 99db9666 2018-05-07 stsp return err;
1237 99db9666 2018-05-07 stsp
1238 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1239 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1240 9ba79e04 2018-06-11 stsp if (err) {
1241 9ba79e04 2018-06-11 stsp *head_id = NULL;
1242 99db9666 2018-05-07 stsp return err;
1243 0553a4e3 2018-04-30 stsp }
1244 80ddbec8 2018-04-29 stsp
1245 9ba79e04 2018-06-11 stsp return NULL;
1246 0553a4e3 2018-04-30 stsp }
1247 0553a4e3 2018-04-30 stsp
1248 0553a4e3 2018-04-30 stsp static const struct got_error *
1249 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1250 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1251 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1252 8b473291 2019-02-21 stsp struct got_reflist_head *refs, const char *path, int commits_needed)
1253 0553a4e3 2018-04-30 stsp {
1254 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1255 52b5abe1 2019-08-13 stsp struct tog_log_view_state *s = &view->state.log;
1256 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1257 60493ae3 2019-06-20 stsp int width;
1258 60493ae3 2019-06-20 stsp int ncommits, author_cols = 10;
1259 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1260 8b473291 2019-02-21 stsp char *refs_str = NULL;
1261 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1262 0553a4e3 2018-04-30 stsp
1263 e0d42f60 2018-07-22 stsp entry = first;
1264 e0d42f60 2018-07-22 stsp ncommits = 0;
1265 e0d42f60 2018-07-22 stsp while (entry) {
1266 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1267 e0d42f60 2018-07-22 stsp *selected = entry;
1268 e0d42f60 2018-07-22 stsp break;
1269 e0d42f60 2018-07-22 stsp }
1270 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1271 e0d42f60 2018-07-22 stsp ncommits++;
1272 e0d42f60 2018-07-22 stsp }
1273 e0d42f60 2018-07-22 stsp
1274 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1275 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1276 1a76625f 2018-10-22 stsp if (err)
1277 ecb28ae0 2018-07-16 stsp return err;
1278 8b473291 2019-02-21 stsp if (refs) {
1279 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, (*selected)->id,
1280 52b5abe1 2019-08-13 stsp s->repo);
1281 8b473291 2019-02-21 stsp if (err)
1282 8b473291 2019-02-21 stsp goto done;
1283 8b473291 2019-02-21 stsp }
1284 867c6645 2018-07-10 stsp }
1285 359bfafd 2019-02-22 stsp
1286 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1287 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1288 1a76625f 2018-10-22 stsp
1289 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1290 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1291 60493ae3 2019-06-20 stsp commits_needed > 0 ?
1292 60493ae3 2019-06-20 stsp (view->searching && view->search_next_done == 0
1293 60493ae3 2019-06-20 stsp ? "searching..." : "loading... ") :
1294 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1295 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1296 8b473291 2019-02-21 stsp goto done;
1297 8b473291 2019-02-21 stsp }
1298 1a76625f 2018-10-22 stsp
1299 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1300 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1301 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1302 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1303 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1304 1a76625f 2018-10-22 stsp header = NULL;
1305 1a76625f 2018-10-22 stsp goto done;
1306 1a76625f 2018-10-22 stsp }
1307 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1308 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1309 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1310 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1311 1a76625f 2018-10-22 stsp header = NULL;
1312 1a76625f 2018-10-22 stsp goto done;
1313 ecb28ae0 2018-07-16 stsp }
1314 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1315 1a76625f 2018-10-22 stsp if (err)
1316 1a76625f 2018-10-22 stsp goto done;
1317 867c6645 2018-07-10 stsp
1318 2814baeb 2018-08-01 stsp werase(view->window);
1319 867c6645 2018-07-10 stsp
1320 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1321 a3404814 2018-09-02 stsp wstandout(view->window);
1322 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1323 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1324 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1325 1a76625f 2018-10-22 stsp width++;
1326 1a76625f 2018-10-22 stsp }
1327 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1328 a3404814 2018-09-02 stsp wstandend(view->window);
1329 ecb28ae0 2018-07-16 stsp free(wline);
1330 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1331 1a76625f 2018-10-22 stsp goto done;
1332 0553a4e3 2018-04-30 stsp
1333 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1334 899d86c2 2018-05-10 stsp entry = first;
1335 5813d178 2019-03-09 stsp ncommits = 0;
1336 5813d178 2019-03-09 stsp while (entry) {
1337 5813d178 2019-03-09 stsp char *author;
1338 5813d178 2019-03-09 stsp wchar_t *wauthor;
1339 5813d178 2019-03-09 stsp int width;
1340 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1341 5813d178 2019-03-09 stsp break;
1342 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1343 5813d178 2019-03-09 stsp if (author == NULL) {
1344 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1345 5813d178 2019-03-09 stsp goto done;
1346 5813d178 2019-03-09 stsp }
1347 5813d178 2019-03-09 stsp err = format_author(&wauthor, &width, author, COLS);
1348 5813d178 2019-03-09 stsp if (author_cols < width)
1349 5813d178 2019-03-09 stsp author_cols = width;
1350 5813d178 2019-03-09 stsp free(wauthor);
1351 5813d178 2019-03-09 stsp free(author);
1352 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1353 5813d178 2019-03-09 stsp }
1354 5813d178 2019-03-09 stsp
1355 5813d178 2019-03-09 stsp entry = first;
1356 899d86c2 2018-05-10 stsp *last = first;
1357 867c6645 2018-07-10 stsp ncommits = 0;
1358 899d86c2 2018-05-10 stsp while (entry) {
1359 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1360 899d86c2 2018-05-10 stsp break;
1361 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1362 2814baeb 2018-08-01 stsp wstandout(view->window);
1363 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1364 5813d178 2019-03-09 stsp author_cols);
1365 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1366 2814baeb 2018-08-01 stsp wstandend(view->window);
1367 0553a4e3 2018-04-30 stsp if (err)
1368 60493ae3 2019-06-20 stsp goto done;
1369 0553a4e3 2018-04-30 stsp ncommits++;
1370 899d86c2 2018-05-10 stsp *last = entry;
1371 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1372 80ddbec8 2018-04-29 stsp }
1373 80ddbec8 2018-04-29 stsp
1374 1a57306a 2018-09-02 stsp view_vborder(view);
1375 1a76625f 2018-10-22 stsp done:
1376 1a76625f 2018-10-22 stsp free(id_str);
1377 8b473291 2019-02-21 stsp free(refs_str);
1378 1a76625f 2018-10-22 stsp free(ncommits_str);
1379 1a76625f 2018-10-22 stsp free(header);
1380 80ddbec8 2018-04-29 stsp return err;
1381 9f7d7167 2018-04-29 stsp }
1382 07b55e75 2018-05-10 stsp
1383 07b55e75 2018-05-10 stsp static void
1384 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1385 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1386 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1387 07b55e75 2018-05-10 stsp {
1388 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1389 07b55e75 2018-05-10 stsp int nscrolled = 0;
1390 07b55e75 2018-05-10 stsp
1391 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1392 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1393 07b55e75 2018-05-10 stsp return;
1394 9f7d7167 2018-04-29 stsp
1395 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1396 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1397 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1398 07b55e75 2018-05-10 stsp if (entry) {
1399 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1400 07b55e75 2018-05-10 stsp nscrolled++;
1401 07b55e75 2018-05-10 stsp }
1402 07b55e75 2018-05-10 stsp }
1403 aa075928 2018-05-10 stsp }
1404 aa075928 2018-05-10 stsp
1405 aa075928 2018-05-10 stsp static const struct got_error *
1406 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1407 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1408 aa075928 2018-05-10 stsp {
1409 5e224a3e 2019-02-22 stsp int errcode;
1410 8a42fca8 2019-02-22 stsp int max_wait = 20;
1411 8a42fca8 2019-02-22 stsp
1412 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1413 aa075928 2018-05-10 stsp
1414 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1415 5e224a3e 2019-02-22 stsp if (*log_complete)
1416 5e224a3e 2019-02-22 stsp break;
1417 b295e71b 2019-02-22 stsp
1418 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1419 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1420 7aafa0d1 2019-02-22 stsp if (errcode)
1421 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1422 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1423 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1424 7aafa0d1 2019-02-22 stsp if (errcode)
1425 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1426 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1427 7aafa0d1 2019-02-22 stsp pthread_yield();
1428 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1429 7aafa0d1 2019-02-22 stsp if (errcode)
1430 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1431 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1432 5e224a3e 2019-02-22 stsp
1433 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1434 5e224a3e 2019-02-22 stsp /*
1435 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1436 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1437 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1438 5e224a3e 2019-02-22 stsp * with few commits in history.
1439 5e224a3e 2019-02-22 stsp */
1440 7aafa0d1 2019-02-22 stsp return NULL;
1441 7aafa0d1 2019-02-22 stsp }
1442 5e224a3e 2019-02-22 stsp }
1443 5e224a3e 2019-02-22 stsp
1444 5e224a3e 2019-02-22 stsp return NULL;
1445 5e224a3e 2019-02-22 stsp }
1446 5e224a3e 2019-02-22 stsp
1447 5e224a3e 2019-02-22 stsp static const struct got_error *
1448 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1449 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1450 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1451 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1452 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1453 5e224a3e 2019-02-22 stsp {
1454 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1455 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1456 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1457 5e224a3e 2019-02-22 stsp
1458 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1459 5e224a3e 2019-02-22 stsp return NULL;
1460 5e224a3e 2019-02-22 stsp
1461 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1462 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1463 08ebd0a9 2019-02-22 stsp /*
1464 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1465 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1466 08ebd0a9 2019-02-22 stsp */
1467 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1468 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1469 5e224a3e 2019-02-22 stsp need_commits);
1470 5e224a3e 2019-02-22 stsp if (err)
1471 5e224a3e 2019-02-22 stsp return err;
1472 7aafa0d1 2019-02-22 stsp }
1473 b295e71b 2019-02-22 stsp
1474 7aafa0d1 2019-02-22 stsp do {
1475 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1476 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1477 88048b54 2019-02-21 stsp break;
1478 88048b54 2019-02-21 stsp
1479 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1480 aa075928 2018-05-10 stsp
1481 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1482 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1483 dd0a52c1 2018-05-20 stsp break;
1484 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1485 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1486 aa075928 2018-05-10 stsp
1487 dd0a52c1 2018-05-20 stsp return err;
1488 07b55e75 2018-05-10 stsp }
1489 4a7f7875 2018-05-10 stsp
1490 cd0acaa7 2018-05-20 stsp static const struct got_error *
1491 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1492 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1493 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1494 8b473291 2019-02-21 stsp struct got_repository *repo)
1495 cd0acaa7 2018-05-20 stsp {
1496 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1497 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1498 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1499 cd0acaa7 2018-05-20 stsp
1500 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1501 15a94983 2018-12-23 stsp if (diff_view == NULL)
1502 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1503 ea5e7bb5 2018-08-01 stsp
1504 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1505 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1506 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1507 e5a0f69f 2018-08-18 stsp if (err == NULL)
1508 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1509 cd0acaa7 2018-05-20 stsp return err;
1510 4a7f7875 2018-05-10 stsp }
1511 4a7f7875 2018-05-10 stsp
1512 80ddbec8 2018-04-29 stsp static const struct got_error *
1513 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1514 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1515 9343a5fb 2018-06-23 stsp {
1516 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1517 941e9f74 2019-05-21 stsp
1518 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1519 941e9f74 2019-05-21 stsp if (parent == NULL)
1520 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1521 941e9f74 2019-05-21 stsp
1522 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1523 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1524 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1525 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1526 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1527 941e9f74 2019-05-21 stsp s->tree = subtree;
1528 941e9f74 2019-05-21 stsp s->entries = got_object_tree_get_entries(s->tree);
1529 941e9f74 2019-05-21 stsp s->selected = 0;
1530 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1531 941e9f74 2019-05-21 stsp return NULL;
1532 941e9f74 2019-05-21 stsp }
1533 941e9f74 2019-05-21 stsp
1534 941e9f74 2019-05-21 stsp
1535 941e9f74 2019-05-21 stsp static const struct got_error *
1536 941e9f74 2019-05-21 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1537 941e9f74 2019-05-21 stsp struct commit_queue_entry *entry, const char *path,
1538 941e9f74 2019-05-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
1539 941e9f74 2019-05-21 stsp {
1540 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1541 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1542 941e9f74 2019-05-21 stsp struct got_tree_entry *te;
1543 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s;
1544 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1545 b03c880f 2019-05-21 stsp char *slash, *subpath = NULL;
1546 941e9f74 2019-05-21 stsp const char *p;
1547 9343a5fb 2018-06-23 stsp
1548 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1549 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1550 9343a5fb 2018-06-23 stsp if (err)
1551 9343a5fb 2018-06-23 stsp return err;
1552 9343a5fb 2018-06-23 stsp
1553 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1554 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1555 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1556 e5a0f69f 2018-08-18 stsp
1557 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1558 941e9f74 2019-05-21 stsp if (err) {
1559 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1560 941e9f74 2019-05-21 stsp return err;
1561 941e9f74 2019-05-21 stsp }
1562 941e9f74 2019-05-21 stsp s = &tree_view->state.tree;
1563 941e9f74 2019-05-21 stsp
1564 941e9f74 2019-05-21 stsp *new_view = tree_view;
1565 941e9f74 2019-05-21 stsp
1566 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1567 941e9f74 2019-05-21 stsp p = path;
1568 8d0fe45a 2019-08-12 stsp while (p[0] == '/')
1569 8d0fe45a 2019-08-12 stsp p++;
1570 941e9f74 2019-05-21 stsp while (*p) {
1571 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1572 941e9f74 2019-05-21 stsp
1573 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1574 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1575 941e9f74 2019-05-21 stsp if (slash == NULL)
1576 941e9f74 2019-05-21 stsp slash = strchr(p, '\0');
1577 941e9f74 2019-05-21 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1578 941e9f74 2019-05-21 stsp if (strncmp(p, te->name, slash - p) == 0) {
1579 941e9f74 2019-05-21 stsp s->selected_entry = te;
1580 941e9f74 2019-05-21 stsp break;
1581 941e9f74 2019-05-21 stsp }
1582 b03c880f 2019-05-21 stsp s->selected++;
1583 941e9f74 2019-05-21 stsp }
1584 941e9f74 2019-05-21 stsp if (s->selected_entry == NULL) {
1585 941e9f74 2019-05-21 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1586 941e9f74 2019-05-21 stsp break;
1587 941e9f74 2019-05-21 stsp }
1588 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1589 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1590 941e9f74 2019-05-21 stsp
1591 67409a31 2019-05-24 stsp if (!S_ISDIR(s->selected_entry->mode)) {
1592 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1593 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1594 67409a31 2019-05-24 stsp s->selected = 0;
1595 b03c880f 2019-05-21 stsp break;
1596 67409a31 2019-05-24 stsp }
1597 b03c880f 2019-05-21 stsp
1598 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1599 941e9f74 2019-05-21 stsp if (slash)
1600 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1601 941e9f74 2019-05-21 stsp else
1602 941e9f74 2019-05-21 stsp subpath = strdup(path);
1603 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1604 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1605 941e9f74 2019-05-21 stsp break;
1606 941e9f74 2019-05-21 stsp }
1607 941e9f74 2019-05-21 stsp
1608 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1609 941e9f74 2019-05-21 stsp subpath);
1610 941e9f74 2019-05-21 stsp if (err)
1611 941e9f74 2019-05-21 stsp break;
1612 941e9f74 2019-05-21 stsp
1613 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1614 941e9f74 2019-05-21 stsp free(tree_id);
1615 941e9f74 2019-05-21 stsp if (err)
1616 941e9f74 2019-05-21 stsp break;
1617 941e9f74 2019-05-21 stsp
1618 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1619 941e9f74 2019-05-21 stsp if (err) {
1620 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1621 941e9f74 2019-05-21 stsp break;
1622 941e9f74 2019-05-21 stsp }
1623 941e9f74 2019-05-21 stsp if (slash == NULL)
1624 941e9f74 2019-05-21 stsp break;
1625 941e9f74 2019-05-21 stsp free(subpath);
1626 941e9f74 2019-05-21 stsp subpath = NULL;
1627 941e9f74 2019-05-21 stsp p = slash;
1628 941e9f74 2019-05-21 stsp }
1629 941e9f74 2019-05-21 stsp
1630 941e9f74 2019-05-21 stsp free(subpath);
1631 1a76625f 2018-10-22 stsp return err;
1632 1a76625f 2018-10-22 stsp }
1633 1a76625f 2018-10-22 stsp
1634 1a76625f 2018-10-22 stsp static void *
1635 1a76625f 2018-10-22 stsp log_thread(void *arg)
1636 1a76625f 2018-10-22 stsp {
1637 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1638 1a76625f 2018-10-22 stsp int errcode = 0;
1639 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1640 1a76625f 2018-10-22 stsp int done = 0;
1641 1a76625f 2018-10-22 stsp
1642 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1643 6fb7cd11 2019-08-22 stsp NULL, NULL);
1644 1a76625f 2018-10-22 stsp if (err)
1645 1a76625f 2018-10-22 stsp return (void *)err;
1646 1a76625f 2018-10-22 stsp
1647 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1648 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1649 1a76625f 2018-10-22 stsp a->in_repo_path);
1650 1a76625f 2018-10-22 stsp if (err) {
1651 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1652 1a76625f 2018-10-22 stsp return (void *)err;
1653 1a76625f 2018-10-22 stsp err = NULL;
1654 1a76625f 2018-10-22 stsp done = 1;
1655 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1656 1a76625f 2018-10-22 stsp a->commits_needed--;
1657 1a76625f 2018-10-22 stsp
1658 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1659 3abe8080 2019-04-10 stsp if (errcode) {
1660 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1661 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1662 3abe8080 2019-04-10 stsp break;
1663 3abe8080 2019-04-10 stsp } else if (*a->quit)
1664 1a76625f 2018-10-22 stsp done = 1;
1665 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1666 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1667 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1668 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1669 1a76625f 2018-10-22 stsp }
1670 1a76625f 2018-10-22 stsp
1671 1a76625f 2018-10-22 stsp if (done)
1672 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1673 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1674 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1675 1a76625f 2018-10-22 stsp &tog_mutex);
1676 1a76625f 2018-10-22 stsp if (errcode)
1677 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1678 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1679 1a76625f 2018-10-22 stsp }
1680 1a76625f 2018-10-22 stsp
1681 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1682 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1683 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1684 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1685 1a76625f 2018-10-22 stsp }
1686 3abe8080 2019-04-10 stsp a->log_complete = 1;
1687 1a76625f 2018-10-22 stsp return (void *)err;
1688 1a76625f 2018-10-22 stsp }
1689 1a76625f 2018-10-22 stsp
1690 1a76625f 2018-10-22 stsp static const struct got_error *
1691 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1692 1a76625f 2018-10-22 stsp {
1693 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1694 1a76625f 2018-10-22 stsp int errcode;
1695 1a76625f 2018-10-22 stsp
1696 1a76625f 2018-10-22 stsp if (s->thread) {
1697 1a76625f 2018-10-22 stsp s->quit = 1;
1698 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1699 1a76625f 2018-10-22 stsp if (errcode)
1700 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1701 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1702 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1703 1a76625f 2018-10-22 stsp if (errcode)
1704 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1705 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1706 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1707 1a76625f 2018-10-22 stsp if (errcode)
1708 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1709 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1710 1a76625f 2018-10-22 stsp if (errcode)
1711 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1712 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1713 1a76625f 2018-10-22 stsp s->thread = NULL;
1714 1a76625f 2018-10-22 stsp }
1715 1a76625f 2018-10-22 stsp
1716 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1717 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1718 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1719 1a76625f 2018-10-22 stsp
1720 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1721 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1722 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1723 1a76625f 2018-10-22 stsp }
1724 1a76625f 2018-10-22 stsp
1725 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1726 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1727 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1728 1a76625f 2018-10-22 stsp }
1729 1a76625f 2018-10-22 stsp
1730 9343a5fb 2018-06-23 stsp return err;
1731 9343a5fb 2018-06-23 stsp }
1732 9343a5fb 2018-06-23 stsp
1733 9343a5fb 2018-06-23 stsp static const struct got_error *
1734 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1735 1a76625f 2018-10-22 stsp {
1736 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1737 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1738 1a76625f 2018-10-22 stsp
1739 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1740 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1741 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1742 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1743 1a76625f 2018-10-22 stsp free(s->start_id);
1744 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1745 1a76625f 2018-10-22 stsp return err;
1746 1a76625f 2018-10-22 stsp }
1747 1a76625f 2018-10-22 stsp
1748 1a76625f 2018-10-22 stsp static const struct got_error *
1749 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
1750 60493ae3 2019-06-20 stsp {
1751 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1752 60493ae3 2019-06-20 stsp
1753 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
1754 96e2b566 2019-07-08 stsp s->search_entry = NULL;
1755 60493ae3 2019-06-20 stsp return NULL;
1756 60493ae3 2019-06-20 stsp }
1757 60493ae3 2019-06-20 stsp
1758 60493ae3 2019-06-20 stsp static int
1759 df0b3d8a 2019-06-28 stsp match_commit(struct got_commit_object *commit, const char *id_str,
1760 5943eee2 2019-08-13 stsp const char *logmsg, regex_t *regex)
1761 60493ae3 2019-06-20 stsp {
1762 60493ae3 2019-06-20 stsp regmatch_t regmatch;
1763 60493ae3 2019-06-20 stsp
1764 60493ae3 2019-06-20 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1765 60493ae3 2019-06-20 stsp &regmatch, 0) == 0 ||
1766 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1767 60493ae3 2019-06-20 stsp &regmatch, 0) == 0 ||
1768 5943eee2 2019-08-13 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1769 5943eee2 2019-08-13 stsp regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1770 60493ae3 2019-06-20 stsp return 1;
1771 60493ae3 2019-06-20 stsp
1772 60493ae3 2019-06-20 stsp return 0;
1773 60493ae3 2019-06-20 stsp }
1774 60493ae3 2019-06-20 stsp
1775 60493ae3 2019-06-20 stsp static const struct got_error *
1776 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
1777 60493ae3 2019-06-20 stsp {
1778 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
1779 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1780 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
1781 60493ae3 2019-06-20 stsp
1782 60493ae3 2019-06-20 stsp if (!view->searching) {
1783 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1784 60493ae3 2019-06-20 stsp return NULL;
1785 60493ae3 2019-06-20 stsp }
1786 60493ae3 2019-06-20 stsp
1787 96e2b566 2019-07-08 stsp if (s->search_entry) {
1788 678cbce5 2019-07-28 stsp if (wgetch(view->window) == KEY_BACKSPACE) {
1789 678cbce5 2019-07-28 stsp view->search_next_done = 1;
1790 678cbce5 2019-07-28 stsp return NULL;
1791 678cbce5 2019-07-28 stsp }
1792 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
1793 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
1794 96e2b566 2019-07-08 stsp else
1795 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
1796 96e2b566 2019-07-08 stsp commit_queue_head, entry);
1797 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
1798 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1799 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
1800 b1bf1435 2019-06-21 stsp else
1801 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
1802 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
1803 20be8d96 2019-06-21 stsp } else {
1804 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1805 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
1806 20be8d96 2019-06-21 stsp else
1807 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1808 20be8d96 2019-06-21 stsp }
1809 60493ae3 2019-06-20 stsp
1810 60493ae3 2019-06-20 stsp while (1) {
1811 5943eee2 2019-08-13 stsp char *id_str, *logmsg;
1812 60493ae3 2019-06-20 stsp if (entry == NULL) {
1813 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
1814 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
1815 f801134a 2019-06-25 stsp view->search_next_done = 1;
1816 f801134a 2019-06-25 stsp return NULL;
1817 60493ae3 2019-06-20 stsp }
1818 96e2b566 2019-07-08 stsp /*
1819 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
1820 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
1821 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
1822 96e2b566 2019-07-08 stsp */
1823 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
1824 57b33b64 2019-07-08 stsp return trigger_log_thread(1,
1825 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
1826 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
1827 f801134a 2019-06-25 stsp &s->thread_args.need_commits);
1828 60493ae3 2019-06-20 stsp }
1829 60493ae3 2019-06-20 stsp
1830 df0b3d8a 2019-06-28 stsp err = got_object_id_str(&id_str, entry->id);
1831 df0b3d8a 2019-06-28 stsp if (err)
1832 df0b3d8a 2019-06-28 stsp return err;
1833 df0b3d8a 2019-06-28 stsp
1834 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, entry->commit);
1835 5943eee2 2019-08-13 stsp if (err)
1836 5943eee2 2019-08-13 stsp return err;
1837 5943eee2 2019-08-13 stsp if (match_commit(entry->commit, id_str, logmsg, &view->regex)) {
1838 5943eee2 2019-08-13 stsp free(logmsg);
1839 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1840 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
1841 df0b3d8a 2019-06-28 stsp free(id_str);
1842 60493ae3 2019-06-20 stsp break;
1843 60493ae3 2019-06-20 stsp }
1844 5943eee2 2019-08-13 stsp free(logmsg);
1845 df0b3d8a 2019-06-28 stsp free(id_str);
1846 96e2b566 2019-07-08 stsp s->search_entry = entry;
1847 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1848 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
1849 b1bf1435 2019-06-21 stsp else
1850 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1851 60493ae3 2019-06-20 stsp }
1852 60493ae3 2019-06-20 stsp
1853 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
1854 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
1855 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
1856 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1857 60493ae3 2019-06-20 stsp if (err)
1858 60493ae3 2019-06-20 stsp return err;
1859 ead14cbe 2019-06-21 stsp cur++;
1860 ead14cbe 2019-06-21 stsp }
1861 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
1862 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1863 60493ae3 2019-06-20 stsp if (err)
1864 60493ae3 2019-06-20 stsp return err;
1865 ead14cbe 2019-06-21 stsp cur--;
1866 60493ae3 2019-06-20 stsp }
1867 60493ae3 2019-06-20 stsp }
1868 60493ae3 2019-06-20 stsp
1869 96e2b566 2019-07-08 stsp s->search_entry = NULL;
1870 96e2b566 2019-07-08 stsp
1871 60493ae3 2019-06-20 stsp return NULL;
1872 60493ae3 2019-06-20 stsp }
1873 60493ae3 2019-06-20 stsp
1874 60493ae3 2019-06-20 stsp static const struct got_error *
1875 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1876 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
1877 d01904d4 2019-06-25 stsp const char *head_ref_name, const char *path, int check_disk)
1878 80ddbec8 2018-04-29 stsp {
1879 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1880 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1881 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1882 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1883 1a76625f 2018-10-22 stsp int errcode;
1884 80ddbec8 2018-04-29 stsp
1885 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1886 ecb28ae0 2018-07-16 stsp if (err != NULL)
1887 ecb28ae0 2018-07-16 stsp goto done;
1888 ecb28ae0 2018-07-16 stsp
1889 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1890 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1891 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1892 9ba79e04 2018-06-11 stsp
1893 8b473291 2019-02-21 stsp s->refs = refs;
1894 fb2756b9 2018-08-04 stsp s->repo = repo;
1895 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
1896 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1897 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1898 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1899 5036bf37 2018-09-24 stsp goto done;
1900 5036bf37 2018-09-24 stsp }
1901 e5a0f69f 2018-08-18 stsp
1902 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1903 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1904 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1905 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
1906 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
1907 1a76625f 2018-10-22 stsp
1908 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1909 1a76625f 2018-10-22 stsp if (err)
1910 1a76625f 2018-10-22 stsp goto done;
1911 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1912 1a76625f 2018-10-22 stsp 0, thread_repo);
1913 1a76625f 2018-10-22 stsp if (err)
1914 1a76625f 2018-10-22 stsp goto done;
1915 1a76625f 2018-10-22 stsp
1916 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1917 1a76625f 2018-10-22 stsp if (errcode) {
1918 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
1919 1a76625f 2018-10-22 stsp goto done;
1920 1a76625f 2018-10-22 stsp }
1921 1a76625f 2018-10-22 stsp
1922 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1923 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1924 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1925 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1926 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1927 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1928 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1929 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1930 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1931 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1932 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1933 ba4f502b 2018-08-04 stsp done:
1934 1a76625f 2018-10-22 stsp if (err)
1935 1a76625f 2018-10-22 stsp close_log_view(view);
1936 ba4f502b 2018-08-04 stsp return err;
1937 ba4f502b 2018-08-04 stsp }
1938 ba4f502b 2018-08-04 stsp
1939 e5a0f69f 2018-08-18 stsp static const struct got_error *
1940 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1941 ba4f502b 2018-08-04 stsp {
1942 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1943 ba4f502b 2018-08-04 stsp
1944 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1945 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1946 2b380cc8 2018-10-24 stsp &s->thread_args);
1947 2b380cc8 2018-10-24 stsp if (errcode)
1948 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
1949 2b380cc8 2018-10-24 stsp }
1950 2b380cc8 2018-10-24 stsp
1951 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1952 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1953 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
1954 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1955 e5a0f69f 2018-08-18 stsp }
1956 04cc582a 2018-08-01 stsp
1957 e5a0f69f 2018-08-18 stsp static const struct got_error *
1958 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1959 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1960 e5a0f69f 2018-08-18 stsp {
1961 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1962 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1963 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
1964 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1965 669b5ffa 2018-10-07 stsp int begin_x = 0;
1966 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
1967 80ddbec8 2018-04-29 stsp
1968 e5a0f69f 2018-08-18 stsp switch (ch) {
1969 1e37a5c2 2019-05-12 jcs case 'q':
1970 1e37a5c2 2019-05-12 jcs s->quit = 1;
1971 1e37a5c2 2019-05-12 jcs break;
1972 1e37a5c2 2019-05-12 jcs case 'k':
1973 1e37a5c2 2019-05-12 jcs case KEY_UP:
1974 1e37a5c2 2019-05-12 jcs case '<':
1975 1e37a5c2 2019-05-12 jcs case ',':
1976 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1977 1a76625f 2018-10-22 stsp break;
1978 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
1979 1e37a5c2 2019-05-12 jcs s->selected--;
1980 1144d21a 2019-06-21 stsp else
1981 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
1982 1144d21a 2019-06-21 stsp &s->commits);
1983 1e37a5c2 2019-05-12 jcs break;
1984 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
1985 a4292ac5 2019-05-12 jcs case CTRL('b'):
1986 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1987 e5a0f69f 2018-08-18 stsp break;
1988 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
1989 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
1990 1e37a5c2 2019-05-12 jcs s->selected = 0;
1991 e5a0f69f 2018-08-18 stsp break;
1992 1e37a5c2 2019-05-12 jcs }
1993 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
1994 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
1995 1e37a5c2 2019-05-12 jcs break;
1996 1e37a5c2 2019-05-12 jcs case 'j':
1997 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
1998 1e37a5c2 2019-05-12 jcs case '>':
1999 1e37a5c2 2019-05-12 jcs case '.':
2000 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
2001 e5a0f69f 2018-08-18 stsp break;
2002 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
2003 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2004 1e37a5c2 2019-05-12 jcs s->selected++;
2005 1e37a5c2 2019-05-12 jcs break;
2006 80ddbec8 2018-04-29 stsp }
2007 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
2008 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
2009 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
2010 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2011 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2012 1e37a5c2 2019-05-12 jcs break;
2013 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
2014 a4292ac5 2019-05-12 jcs case CTRL('f'): {
2015 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
2016 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
2017 1e37a5c2 2019-05-12 jcs if (first == NULL)
2018 e5a0f69f 2018-08-18 stsp break;
2019 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
2020 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
2021 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
2022 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
2023 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
2024 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
2025 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
2026 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2027 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2028 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2029 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2030 1e37a5c2 2019-05-12 jcs }
2031 1e37a5c2 2019-05-12 jcs err = NULL;
2032 1e37a5c2 2019-05-12 jcs break;
2033 1e37a5c2 2019-05-12 jcs }
2034 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2035 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2036 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2037 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2038 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2039 1e37a5c2 2019-05-12 jcs break;
2040 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2041 87c7274c 2019-05-12 jcs case ' ':
2042 1e37a5c2 2019-05-12 jcs case '\r':
2043 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2044 e5a0f69f 2018-08-18 stsp break;
2045 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2046 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2047 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2048 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2049 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
2050 1e37a5c2 2019-05-12 jcs if (err)
2051 1e37a5c2 2019-05-12 jcs break;
2052 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2053 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2054 f7013a22 2018-10-24 stsp if (err)
2055 1e37a5c2 2019-05-12 jcs return err;
2056 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2057 1e37a5c2 2019-05-12 jcs if (err) {
2058 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2059 f7013a22 2018-10-24 stsp break;
2060 5036bf37 2018-09-24 stsp }
2061 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2062 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2063 1e37a5c2 2019-05-12 jcs } else
2064 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2065 1e37a5c2 2019-05-12 jcs break;
2066 1e37a5c2 2019-05-12 jcs case 't':
2067 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2068 5036bf37 2018-09-24 stsp break;
2069 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2070 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2071 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2072 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2073 1e37a5c2 2019-05-12 jcs if (err)
2074 e5a0f69f 2018-08-18 stsp break;
2075 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2076 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2077 1e37a5c2 2019-05-12 jcs if (err)
2078 1e37a5c2 2019-05-12 jcs return err;
2079 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2080 1e37a5c2 2019-05-12 jcs if (err) {
2081 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2082 1e37a5c2 2019-05-12 jcs break;
2083 1e37a5c2 2019-05-12 jcs }
2084 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2085 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2086 1e37a5c2 2019-05-12 jcs } else
2087 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2088 1e37a5c2 2019-05-12 jcs break;
2089 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2090 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2091 1e37a5c2 2019-05-12 jcs break;
2092 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2093 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2094 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2095 1e37a5c2 2019-05-12 jcs if (err)
2096 1e37a5c2 2019-05-12 jcs return err;
2097 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2098 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2099 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2100 638f9024 2019-05-13 stsp return got_error_from_errno(
2101 1e37a5c2 2019-05-12 jcs "view_open");
2102 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2103 d01904d4 2019-06-25 stsp s->repo, s->head_ref_name, parent_path, 0);
2104 1e37a5c2 2019-05-12 jcs if (err)
2105 1e37a5c2 2019-05-12 jcs return err;;
2106 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2107 1e37a5c2 2019-05-12 jcs *new_view = lv;
2108 1e37a5c2 2019-05-12 jcs else {
2109 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2110 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2111 1e37a5c2 2019-05-12 jcs }
2112 1e37a5c2 2019-05-12 jcs return NULL;
2113 1e37a5c2 2019-05-12 jcs }
2114 1e37a5c2 2019-05-12 jcs break;
2115 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2116 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2117 d01904d4 2019-06-25 stsp if (err)
2118 d01904d4 2019-06-25 stsp return err;
2119 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2120 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2121 d01904d4 2019-06-25 stsp if (lv == NULL)
2122 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2123 d01904d4 2019-06-25 stsp err = get_head_commit_id(&start_id, s->head_ref_name ?
2124 d01904d4 2019-06-25 stsp s->head_ref_name : GOT_REF_HEAD, s->repo);
2125 ef129c5e 2019-08-03 stsp if (err) {
2126 ef129c5e 2019-08-03 stsp view_close(lv);
2127 d01904d4 2019-06-25 stsp return err;
2128 ef129c5e 2019-08-03 stsp }
2129 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2130 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2131 d01904d4 2019-06-25 stsp free(start_id);
2132 ef129c5e 2019-08-03 stsp view_close(lv);
2133 d01904d4 2019-06-25 stsp return got_error_from_errno("strdup");
2134 d01904d4 2019-06-25 stsp }
2135 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2136 d01904d4 2019-06-25 stsp s->head_ref_name, in_repo_path, 0);
2137 ef129c5e 2019-08-03 stsp if (err) {
2138 ef129c5e 2019-08-03 stsp free(start_id);
2139 ef129c5e 2019-08-03 stsp view_close(lv);
2140 d01904d4 2019-06-25 stsp return err;;
2141 ef129c5e 2019-08-03 stsp }
2142 d01904d4 2019-06-25 stsp *dead_view = view;
2143 d01904d4 2019-06-25 stsp *new_view = lv;
2144 d01904d4 2019-06-25 stsp break;
2145 1e37a5c2 2019-05-12 jcs default:
2146 1e37a5c2 2019-05-12 jcs break;
2147 899d86c2 2018-05-10 stsp }
2148 e5a0f69f 2018-08-18 stsp
2149 80ddbec8 2018-04-29 stsp return err;
2150 80ddbec8 2018-04-29 stsp }
2151 80ddbec8 2018-04-29 stsp
2152 4ed7e80c 2018-05-20 stsp static const struct got_error *
2153 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2154 c2db6724 2019-01-04 stsp {
2155 c2db6724 2019-01-04 stsp const struct got_error *error;
2156 c2db6724 2019-01-04 stsp
2157 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2158 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2159 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2160 37c06ea4 2019-07-15 stsp #endif
2161 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2162 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2163 c2db6724 2019-01-04 stsp
2164 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2165 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2166 c2db6724 2019-01-04 stsp
2167 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2168 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2169 c2db6724 2019-01-04 stsp
2170 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2171 c2db6724 2019-01-04 stsp if (error != NULL)
2172 c2db6724 2019-01-04 stsp return error;
2173 c2db6724 2019-01-04 stsp
2174 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2175 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2176 c2db6724 2019-01-04 stsp
2177 c2db6724 2019-01-04 stsp return NULL;
2178 c2db6724 2019-01-04 stsp }
2179 c2db6724 2019-01-04 stsp
2180 a915003a 2019-02-05 stsp static void
2181 a915003a 2019-02-05 stsp init_curses(void)
2182 a915003a 2019-02-05 stsp {
2183 a915003a 2019-02-05 stsp initscr();
2184 a915003a 2019-02-05 stsp cbreak();
2185 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2186 a915003a 2019-02-05 stsp noecho();
2187 a915003a 2019-02-05 stsp nonl();
2188 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2189 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2190 a915003a 2019-02-05 stsp curs_set(0);
2191 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2192 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2193 a915003a 2019-02-05 stsp }
2194 a915003a 2019-02-05 stsp
2195 c2db6724 2019-01-04 stsp static const struct got_error *
2196 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2197 9f7d7167 2018-04-29 stsp {
2198 80ddbec8 2018-04-29 stsp const struct got_error *error;
2199 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2200 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2201 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2202 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2203 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2204 2fc00ff4 2019-08-31 stsp char *start_commit = NULL, *head_ref_name = NULL;
2205 80ddbec8 2018-04-29 stsp int ch;
2206 04cc582a 2018-08-01 stsp struct tog_view *view;
2207 a55555f2 2019-03-28 stsp
2208 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2209 80ddbec8 2018-04-29 stsp
2210 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2211 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2212 c2db6724 2019-01-04 stsp NULL) == -1)
2213 80ddbec8 2018-04-29 stsp err(1, "pledge");
2214 80ddbec8 2018-04-29 stsp #endif
2215 80ddbec8 2018-04-29 stsp
2216 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2217 80ddbec8 2018-04-29 stsp switch (ch) {
2218 80ddbec8 2018-04-29 stsp case 'c':
2219 80ddbec8 2018-04-29 stsp start_commit = optarg;
2220 80ddbec8 2018-04-29 stsp break;
2221 ecb28ae0 2018-07-16 stsp case 'r':
2222 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2223 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2224 ecb28ae0 2018-07-16 stsp err(1, "-r option");
2225 ecb28ae0 2018-07-16 stsp break;
2226 80ddbec8 2018-04-29 stsp default:
2227 17020d27 2019-03-07 stsp usage_log();
2228 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2229 80ddbec8 2018-04-29 stsp }
2230 80ddbec8 2018-04-29 stsp }
2231 80ddbec8 2018-04-29 stsp
2232 80ddbec8 2018-04-29 stsp argc -= optind;
2233 80ddbec8 2018-04-29 stsp argv += optind;
2234 80ddbec8 2018-04-29 stsp
2235 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2236 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2237 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2238 ecb28ae0 2018-07-16 stsp goto done;
2239 ecb28ae0 2018-07-16 stsp }
2240 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2241 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2242 963f97a1 2019-03-18 stsp goto done;
2243 963f97a1 2019-03-18 stsp error = NULL;
2244 963f97a1 2019-03-18 stsp
2245 963f97a1 2019-03-18 stsp if (argc == 0) {
2246 963f97a1 2019-03-18 stsp path = strdup("");
2247 963f97a1 2019-03-18 stsp if (path == NULL) {
2248 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2249 ecb28ae0 2018-07-16 stsp goto done;
2250 ecb28ae0 2018-07-16 stsp }
2251 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2252 963f97a1 2019-03-18 stsp if (worktree) {
2253 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2254 963f97a1 2019-03-18 stsp argv[0]);
2255 963f97a1 2019-03-18 stsp if (error)
2256 963f97a1 2019-03-18 stsp goto done;
2257 963f97a1 2019-03-18 stsp } else {
2258 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2259 963f97a1 2019-03-18 stsp if (path == NULL) {
2260 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2261 963f97a1 2019-03-18 stsp goto done;
2262 963f97a1 2019-03-18 stsp }
2263 963f97a1 2019-03-18 stsp }
2264 963f97a1 2019-03-18 stsp } else
2265 963f97a1 2019-03-18 stsp usage_log();
2266 963f97a1 2019-03-18 stsp
2267 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2268 a1fbf39a 2019-08-11 stsp if (worktree)
2269 b9d7675a 2019-08-11 stsp repo_path = strdup(
2270 b9d7675a 2019-08-11 stsp got_worktree_get_repo_path(worktree));
2271 a1fbf39a 2019-08-11 stsp else
2272 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2273 a1fbf39a 2019-08-11 stsp }
2274 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2275 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2276 963f97a1 2019-03-18 stsp goto done;
2277 ecb28ae0 2018-07-16 stsp }
2278 c2db6724 2019-01-04 stsp
2279 a915003a 2019-02-05 stsp init_curses();
2280 ecb28ae0 2018-07-16 stsp
2281 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
2282 80ddbec8 2018-04-29 stsp if (error != NULL)
2283 ecb28ae0 2018-07-16 stsp goto done;
2284 80ddbec8 2018-04-29 stsp
2285 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2286 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2287 c02c541e 2019-03-29 stsp if (error)
2288 c02c541e 2019-03-29 stsp goto done;
2289 c02c541e 2019-03-29 stsp
2290 15a94983 2018-12-23 stsp if (start_commit == NULL)
2291 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&start_id, worktree ?
2292 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2293 19e70ad6 2019-05-14 stsp repo);
2294 f2b6a97d 2019-07-15 stsp else {
2295 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&start_id, start_commit, repo);
2296 f2b6a97d 2019-07-15 stsp if (error) {
2297 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
2298 f2b6a97d 2019-07-15 stsp goto done;
2299 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&start_id,
2300 f2b6a97d 2019-07-15 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2301 f2b6a97d 2019-07-15 stsp }
2302 f2b6a97d 2019-07-15 stsp }
2303 80ddbec8 2018-04-29 stsp if (error != NULL)
2304 8b473291 2019-02-21 stsp goto done;
2305 8b473291 2019-02-21 stsp
2306 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2307 8b473291 2019-02-21 stsp if (error)
2308 ecb28ae0 2018-07-16 stsp goto done;
2309 ecb28ae0 2018-07-16 stsp
2310 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2311 04cc582a 2018-08-01 stsp if (view == NULL) {
2312 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2313 04cc582a 2018-08-01 stsp goto done;
2314 04cc582a 2018-08-01 stsp }
2315 2fc00ff4 2019-08-31 stsp if (worktree) {
2316 2fc00ff4 2019-08-31 stsp head_ref_name = strdup(
2317 2fc00ff4 2019-08-31 stsp got_worktree_get_head_ref_name(worktree));
2318 2fc00ff4 2019-08-31 stsp if (head_ref_name == NULL) {
2319 2fc00ff4 2019-08-31 stsp error = got_error_from_errno("strdup");
2320 2fc00ff4 2019-08-31 stsp goto done;
2321 2fc00ff4 2019-08-31 stsp }
2322 2fc00ff4 2019-08-31 stsp }
2323 2fc00ff4 2019-08-31 stsp error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2324 2fc00ff4 2019-08-31 stsp path, 1);
2325 ba4f502b 2018-08-04 stsp if (error)
2326 ba4f502b 2018-08-04 stsp goto done;
2327 2fc00ff4 2019-08-31 stsp if (worktree) {
2328 2fc00ff4 2019-08-31 stsp /* Release work tree lock. */
2329 2fc00ff4 2019-08-31 stsp got_worktree_close(worktree);
2330 2fc00ff4 2019-08-31 stsp worktree = NULL;
2331 2fc00ff4 2019-08-31 stsp }
2332 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2333 ecb28ae0 2018-07-16 stsp done:
2334 ecb28ae0 2018-07-16 stsp free(repo_path);
2335 ecb28ae0 2018-07-16 stsp free(cwd);
2336 ecb28ae0 2018-07-16 stsp free(path);
2337 899d86c2 2018-05-10 stsp free(start_id);
2338 2fc00ff4 2019-08-31 stsp free(head_ref_name);
2339 ecb28ae0 2018-07-16 stsp if (repo)
2340 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2341 ec142235 2019-03-07 stsp if (worktree)
2342 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2343 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2344 80ddbec8 2018-04-29 stsp return error;
2345 9f7d7167 2018-04-29 stsp }
2346 9f7d7167 2018-04-29 stsp
2347 4ed7e80c 2018-05-20 stsp __dead static void
2348 9f7d7167 2018-04-29 stsp usage_diff(void)
2349 9f7d7167 2018-04-29 stsp {
2350 80ddbec8 2018-04-29 stsp endwin();
2351 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2352 9f7d7167 2018-04-29 stsp getprogname());
2353 9f7d7167 2018-04-29 stsp exit(1);
2354 b304db33 2018-05-20 stsp }
2355 b304db33 2018-05-20 stsp
2356 b304db33 2018-05-20 stsp static char *
2357 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2358 b304db33 2018-05-20 stsp {
2359 b304db33 2018-05-20 stsp char *line;
2360 b304db33 2018-05-20 stsp size_t linelen;
2361 b304db33 2018-05-20 stsp size_t lineno;
2362 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2363 b304db33 2018-05-20 stsp
2364 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2365 b304db33 2018-05-20 stsp if (len)
2366 b304db33 2018-05-20 stsp *len = linelen;
2367 b304db33 2018-05-20 stsp return line;
2368 26ed57b2 2018-05-19 stsp }
2369 26ed57b2 2018-05-19 stsp
2370 4ed7e80c 2018-05-20 stsp static const struct got_error *
2371 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2372 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
2373 c3e9aa98 2019-05-13 jcs char *header)
2374 26ed57b2 2018-05-19 stsp {
2375 61e69b96 2018-05-20 stsp const struct got_error *err;
2376 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2377 b304db33 2018-05-20 stsp char *line;
2378 b304db33 2018-05-20 stsp size_t len;
2379 61e69b96 2018-05-20 stsp wchar_t *wline;
2380 e0b650dd 2018-05-20 stsp int width;
2381 26ed57b2 2018-05-19 stsp
2382 26ed57b2 2018-05-19 stsp rewind(f);
2383 f7d12f7e 2018-08-01 stsp werase(view->window);
2384 a3404814 2018-09-02 stsp
2385 a3404814 2018-09-02 stsp if (header) {
2386 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
2387 a3404814 2018-09-02 stsp if (err) {
2388 a3404814 2018-09-02 stsp return err;
2389 a3404814 2018-09-02 stsp }
2390 a3404814 2018-09-02 stsp
2391 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2392 a3404814 2018-09-02 stsp wstandout(view->window);
2393 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2394 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2395 a3404814 2018-09-02 stsp wstandend(view->window);
2396 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2397 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2398 26ed57b2 2018-05-19 stsp
2399 a3404814 2018-09-02 stsp if (max_lines <= 1)
2400 a3404814 2018-09-02 stsp return NULL;
2401 a3404814 2018-09-02 stsp max_lines--;
2402 a3404814 2018-09-02 stsp }
2403 a3404814 2018-09-02 stsp
2404 26ed57b2 2018-05-19 stsp *eof = 0;
2405 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2406 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2407 26ed57b2 2018-05-19 stsp if (line == NULL) {
2408 26ed57b2 2018-05-19 stsp *eof = 1;
2409 26ed57b2 2018-05-19 stsp break;
2410 26ed57b2 2018-05-19 stsp }
2411 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2412 26ed57b2 2018-05-19 stsp free(line);
2413 26ed57b2 2018-05-19 stsp continue;
2414 26ed57b2 2018-05-19 stsp }
2415 26ed57b2 2018-05-19 stsp
2416 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2417 61e69b96 2018-05-20 stsp if (err) {
2418 61e69b96 2018-05-20 stsp free(line);
2419 61e69b96 2018-05-20 stsp return err;
2420 61e69b96 2018-05-20 stsp }
2421 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2422 239f6369 2019-09-08 stsp if (width <= view->ncols - 1)
2423 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2424 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2425 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2426 26ed57b2 2018-05-19 stsp free(line);
2427 2550e4c3 2018-07-13 stsp free(wline);
2428 2550e4c3 2018-07-13 stsp wline = NULL;
2429 26ed57b2 2018-05-19 stsp }
2430 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2431 26ed57b2 2018-05-19 stsp
2432 1a57306a 2018-09-02 stsp view_vborder(view);
2433 c3e9aa98 2019-05-13 jcs
2434 c3e9aa98 2019-05-13 jcs if (*eof) {
2435 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2436 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2437 c3e9aa98 2019-05-13 jcs nprinted++;
2438 c3e9aa98 2019-05-13 jcs }
2439 c3e9aa98 2019-05-13 jcs
2440 c3e9aa98 2019-05-13 jcs err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2441 c3e9aa98 2019-05-13 jcs if (err) {
2442 c3e9aa98 2019-05-13 jcs return err;
2443 c3e9aa98 2019-05-13 jcs }
2444 26ed57b2 2018-05-19 stsp
2445 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2446 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2447 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2448 c3e9aa98 2019-05-13 jcs }
2449 c3e9aa98 2019-05-13 jcs
2450 26ed57b2 2018-05-19 stsp return NULL;
2451 abd2672a 2018-12-23 stsp }
2452 abd2672a 2018-12-23 stsp
2453 abd2672a 2018-12-23 stsp static char *
2454 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2455 abd2672a 2018-12-23 stsp {
2456 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2457 09867e48 2019-08-13 stsp char *p, *s;
2458 09867e48 2019-08-13 stsp
2459 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2460 09867e48 2019-08-13 stsp if (tm == NULL)
2461 09867e48 2019-08-13 stsp return NULL;
2462 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2463 09867e48 2019-08-13 stsp if (s == NULL)
2464 09867e48 2019-08-13 stsp return NULL;
2465 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2466 abd2672a 2018-12-23 stsp if (p)
2467 abd2672a 2018-12-23 stsp *p = '\0';
2468 abd2672a 2018-12-23 stsp return s;
2469 9f7d7167 2018-04-29 stsp }
2470 9f7d7167 2018-04-29 stsp
2471 4ed7e80c 2018-05-20 stsp static const struct got_error *
2472 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2473 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2474 abd2672a 2018-12-23 stsp {
2475 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2476 09867e48 2019-08-13 stsp char datebuf[26], *datestr;
2477 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2478 5943eee2 2019-08-13 stsp char *id_str = NULL, *logmsg = NULL;
2479 45d799e2 2018-12-23 stsp time_t committer_time;
2480 45d799e2 2018-12-23 stsp const char *author, *committer;
2481 8b473291 2019-02-21 stsp char *refs_str = NULL;
2482 abd2672a 2018-12-23 stsp
2483 8b473291 2019-02-21 stsp if (refs) {
2484 52b5abe1 2019-08-13 stsp err = build_refs_str(&refs_str, refs, commit_id, repo);
2485 8b473291 2019-02-21 stsp if (err)
2486 8b473291 2019-02-21 stsp return err;
2487 8b473291 2019-02-21 stsp }
2488 8b473291 2019-02-21 stsp
2489 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2490 abd2672a 2018-12-23 stsp if (err)
2491 abd2672a 2018-12-23 stsp return err;
2492 abd2672a 2018-12-23 stsp
2493 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2494 15a94983 2018-12-23 stsp if (err) {
2495 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2496 15a94983 2018-12-23 stsp goto done;
2497 15a94983 2018-12-23 stsp }
2498 abd2672a 2018-12-23 stsp
2499 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2500 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2501 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2502 abd2672a 2018-12-23 stsp goto done;
2503 abd2672a 2018-12-23 stsp }
2504 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2505 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2506 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2507 abd2672a 2018-12-23 stsp goto done;
2508 abd2672a 2018-12-23 stsp }
2509 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2510 09867e48 2019-08-13 stsp datestr = get_datestr(&committer_time, datebuf);
2511 09867e48 2019-08-13 stsp if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2512 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2513 abd2672a 2018-12-23 stsp goto done;
2514 abd2672a 2018-12-23 stsp }
2515 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2516 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2517 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2518 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2519 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2520 abd2672a 2018-12-23 stsp goto done;
2521 abd2672a 2018-12-23 stsp }
2522 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
2523 5943eee2 2019-08-13 stsp if (err)
2524 5943eee2 2019-08-13 stsp goto done;
2525 5943eee2 2019-08-13 stsp if (fprintf(outfile, "%s\n", logmsg) < 0) {
2526 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2527 abd2672a 2018-12-23 stsp goto done;
2528 abd2672a 2018-12-23 stsp }
2529 abd2672a 2018-12-23 stsp done:
2530 abd2672a 2018-12-23 stsp free(id_str);
2531 5943eee2 2019-08-13 stsp free(logmsg);
2532 8b473291 2019-02-21 stsp free(refs_str);
2533 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2534 abd2672a 2018-12-23 stsp return err;
2535 abd2672a 2018-12-23 stsp }
2536 abd2672a 2018-12-23 stsp
2537 abd2672a 2018-12-23 stsp static const struct got_error *
2538 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2539 26ed57b2 2018-05-19 stsp {
2540 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2541 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2542 15a94983 2018-12-23 stsp int obj_type;
2543 26ed57b2 2018-05-19 stsp
2544 511a516b 2018-05-19 stsp f = got_opentemp();
2545 48ae06ee 2018-10-18 stsp if (f == NULL) {
2546 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2547 48ae06ee 2018-10-18 stsp goto done;
2548 48ae06ee 2018-10-18 stsp }
2549 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2550 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2551 fb43ecf1 2019-02-11 stsp goto done;
2552 fb43ecf1 2019-02-11 stsp }
2553 48ae06ee 2018-10-18 stsp s->f = f;
2554 26ed57b2 2018-05-19 stsp
2555 15a94983 2018-12-23 stsp if (s->id1)
2556 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2557 15a94983 2018-12-23 stsp else
2558 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2559 15a94983 2018-12-23 stsp if (err)
2560 15a94983 2018-12-23 stsp goto done;
2561 15a94983 2018-12-23 stsp
2562 15a94983 2018-12-23 stsp switch (obj_type) {
2563 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2564 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2565 54156555 2018-12-24 stsp s->diff_context, s->repo, f);
2566 26ed57b2 2018-05-19 stsp break;
2567 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2568 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2569 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
2570 26ed57b2 2018-05-19 stsp break;
2571 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2572 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2573 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2574 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2575 abd2672a 2018-12-23 stsp
2576 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2577 abd2672a 2018-12-23 stsp if (err)
2578 abd2672a 2018-12-23 stsp break;
2579 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2580 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2581 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2582 15a087fe 2019-02-21 stsp else {
2583 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2584 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2585 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2586 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2587 8b473291 2019-02-21 stsp s->repo, f);
2588 15a087fe 2019-02-21 stsp break;
2589 15a087fe 2019-02-21 stsp }
2590 abd2672a 2018-12-23 stsp }
2591 abd2672a 2018-12-23 stsp }
2592 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2593 abd2672a 2018-12-23 stsp
2594 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2595 15a94983 2018-12-23 stsp s->diff_context, s->repo, f);
2596 26ed57b2 2018-05-19 stsp break;
2597 abd2672a 2018-12-23 stsp }
2598 26ed57b2 2018-05-19 stsp default:
2599 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2600 48ae06ee 2018-10-18 stsp break;
2601 26ed57b2 2018-05-19 stsp }
2602 48ae06ee 2018-10-18 stsp done:
2603 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2604 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2605 48ae06ee 2018-10-18 stsp return err;
2606 48ae06ee 2018-10-18 stsp }
2607 26ed57b2 2018-05-19 stsp
2608 f5215bb9 2019-02-22 stsp static void
2609 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2610 f5215bb9 2019-02-22 stsp {
2611 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2612 f5215bb9 2019-02-22 stsp update_panels();
2613 f5215bb9 2019-02-22 stsp doupdate();
2614 f5215bb9 2019-02-22 stsp }
2615 f5215bb9 2019-02-22 stsp
2616 48ae06ee 2018-10-18 stsp static const struct got_error *
2617 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2618 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2619 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2620 48ae06ee 2018-10-18 stsp {
2621 48ae06ee 2018-10-18 stsp const struct got_error *err;
2622 5dc9f4bc 2018-08-04 stsp
2623 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2624 15a94983 2018-12-23 stsp int type1, type2;
2625 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2626 15a94983 2018-12-23 stsp if (err)
2627 15a94983 2018-12-23 stsp return err;
2628 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2629 15a94983 2018-12-23 stsp if (err)
2630 15a94983 2018-12-23 stsp return err;
2631 15a94983 2018-12-23 stsp
2632 15a94983 2018-12-23 stsp if (type1 != type2)
2633 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2634 15a94983 2018-12-23 stsp }
2635 48ae06ee 2018-10-18 stsp
2636 15a94983 2018-12-23 stsp if (id1) {
2637 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2638 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2639 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2640 48ae06ee 2018-10-18 stsp } else
2641 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2642 48ae06ee 2018-10-18 stsp
2643 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2644 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2645 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2646 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2647 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2648 48ae06ee 2018-10-18 stsp }
2649 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2650 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2651 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2652 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2653 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2654 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2655 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2656 5dc9f4bc 2018-08-04 stsp
2657 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2658 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2659 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2660 f5215bb9 2019-02-22 stsp
2661 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2662 48ae06ee 2018-10-18 stsp if (err) {
2663 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2664 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2665 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2666 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2667 48ae06ee 2018-10-18 stsp return err;
2668 48ae06ee 2018-10-18 stsp }
2669 48ae06ee 2018-10-18 stsp
2670 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2671 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2672 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2673 e5a0f69f 2018-08-18 stsp
2674 5dc9f4bc 2018-08-04 stsp return NULL;
2675 5dc9f4bc 2018-08-04 stsp }
2676 5dc9f4bc 2018-08-04 stsp
2677 e5a0f69f 2018-08-18 stsp static const struct got_error *
2678 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2679 5dc9f4bc 2018-08-04 stsp {
2680 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2681 e5a0f69f 2018-08-18 stsp
2682 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2683 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2684 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2685 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2686 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2687 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2688 e5a0f69f 2018-08-18 stsp return err;
2689 5dc9f4bc 2018-08-04 stsp }
2690 5dc9f4bc 2018-08-04 stsp
2691 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2692 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2693 5dc9f4bc 2018-08-04 stsp {
2694 a3404814 2018-09-02 stsp const struct got_error *err;
2695 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2696 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2697 a3404814 2018-09-02 stsp
2698 a3404814 2018-09-02 stsp if (s->id1) {
2699 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2700 a3404814 2018-09-02 stsp if (err)
2701 a3404814 2018-09-02 stsp return err;
2702 a3404814 2018-09-02 stsp }
2703 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2704 a3404814 2018-09-02 stsp if (err)
2705 a3404814 2018-09-02 stsp return err;
2706 26ed57b2 2018-05-19 stsp
2707 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2708 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2709 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2710 a3404814 2018-09-02 stsp free(id_str1);
2711 a3404814 2018-09-02 stsp free(id_str2);
2712 a3404814 2018-09-02 stsp return err;
2713 a3404814 2018-09-02 stsp }
2714 a3404814 2018-09-02 stsp free(id_str1);
2715 a3404814 2018-09-02 stsp free(id_str2);
2716 a3404814 2018-09-02 stsp
2717 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2718 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2719 a3404814 2018-09-02 stsp header);
2720 15a087fe 2019-02-21 stsp }
2721 15a087fe 2019-02-21 stsp
2722 15a087fe 2019-02-21 stsp static const struct got_error *
2723 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2724 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2725 15a087fe 2019-02-21 stsp {
2726 d7a04538 2019-02-21 stsp const struct got_error *err;
2727 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
2728 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
2729 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
2730 15a087fe 2019-02-21 stsp
2731 15a087fe 2019-02-21 stsp free(s->id2);
2732 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2733 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2734 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2735 15a087fe 2019-02-21 stsp
2736 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2737 d7a04538 2019-02-21 stsp if (err)
2738 d7a04538 2019-02-21 stsp return err;
2739 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
2740 15a087fe 2019-02-21 stsp free(s->id1);
2741 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
2742 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2743 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
2744 15a087fe 2019-02-21 stsp return NULL;
2745 0cf4efb1 2018-09-29 stsp }
2746 0cf4efb1 2018-09-29 stsp
2747 0cf4efb1 2018-09-29 stsp static const struct got_error *
2748 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2749 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2750 e5a0f69f 2018-08-18 stsp {
2751 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2752 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2753 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
2754 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
2755 e5a0f69f 2018-08-18 stsp int i;
2756 e5a0f69f 2018-08-18 stsp
2757 e5a0f69f 2018-08-18 stsp switch (ch) {
2758 1e37a5c2 2019-05-12 jcs case 'k':
2759 1e37a5c2 2019-05-12 jcs case KEY_UP:
2760 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
2761 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2762 1e37a5c2 2019-05-12 jcs break;
2763 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2764 a60a9dc4 2019-05-13 jcs case CTRL('b'):
2765 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
2766 26ed57b2 2018-05-19 stsp break;
2767 1e37a5c2 2019-05-12 jcs i = 0;
2768 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
2769 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
2770 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2771 1e37a5c2 2019-05-12 jcs break;
2772 1e37a5c2 2019-05-12 jcs case 'j':
2773 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2774 1e37a5c2 2019-05-12 jcs if (!s->eof)
2775 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2776 1e37a5c2 2019-05-12 jcs break;
2777 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
2778 a60a9dc4 2019-05-13 jcs case CTRL('f'):
2779 1e37a5c2 2019-05-12 jcs case ' ':
2780 00ba99a7 2019-05-12 jcs if (s->eof)
2781 1e37a5c2 2019-05-12 jcs break;
2782 1e37a5c2 2019-05-12 jcs i = 0;
2783 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
2784 1e37a5c2 2019-05-12 jcs char *line;
2785 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
2786 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2787 1e37a5c2 2019-05-12 jcs if (line == NULL)
2788 34bc9ec9 2019-02-22 stsp break;
2789 1e37a5c2 2019-05-12 jcs }
2790 1e37a5c2 2019-05-12 jcs break;
2791 1e37a5c2 2019-05-12 jcs case '[':
2792 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
2793 1e37a5c2 2019-05-12 jcs s->diff_context--;
2794 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2795 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2796 1e37a5c2 2019-05-12 jcs }
2797 1e37a5c2 2019-05-12 jcs break;
2798 1e37a5c2 2019-05-12 jcs case ']':
2799 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2800 1e37a5c2 2019-05-12 jcs s->diff_context++;
2801 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2802 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2803 1e37a5c2 2019-05-12 jcs }
2804 1e37a5c2 2019-05-12 jcs break;
2805 1e37a5c2 2019-05-12 jcs case '<':
2806 1e37a5c2 2019-05-12 jcs case ',':
2807 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2808 48ae06ee 2018-10-18 stsp break;
2809 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2810 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
2811 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
2812 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2813 48ae06ee 2018-10-18 stsp break;
2814 6524637e 2019-02-21 stsp
2815 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2816 1e37a5c2 2019-05-12 jcs KEY_UP);
2817 1e37a5c2 2019-05-12 jcs if (err)
2818 1e37a5c2 2019-05-12 jcs break;
2819 15a087fe 2019-02-21 stsp
2820 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2821 1e37a5c2 2019-05-12 jcs if (err)
2822 1e37a5c2 2019-05-12 jcs break;
2823 15a087fe 2019-02-21 stsp
2824 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2825 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2826 15a087fe 2019-02-21 stsp
2827 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2828 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2829 1e37a5c2 2019-05-12 jcs break;
2830 1e37a5c2 2019-05-12 jcs case '>':
2831 1e37a5c2 2019-05-12 jcs case '.':
2832 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2833 15a087fe 2019-02-21 stsp break;
2834 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2835 5e224a3e 2019-02-22 stsp
2836 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2837 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
2838 5e224a3e 2019-02-22 stsp
2839 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
2840 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
2841 1e37a5c2 2019-05-12 jcs update_panels();
2842 1e37a5c2 2019-05-12 jcs doupdate();
2843 6e73b0d6 2019-02-22 stsp
2844 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
2845 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
2846 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
2847 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
2848 fb872ab2 2019-02-21 stsp if (err)
2849 fb872ab2 2019-02-21 stsp break;
2850 1e37a5c2 2019-05-12 jcs }
2851 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2852 1e37a5c2 2019-05-12 jcs KEY_DOWN);
2853 1e37a5c2 2019-05-12 jcs if (err)
2854 1e37a5c2 2019-05-12 jcs break;
2855 15a087fe 2019-02-21 stsp
2856 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
2857 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2858 1e37a5c2 2019-05-12 jcs break;
2859 15a087fe 2019-02-21 stsp
2860 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2861 1e37a5c2 2019-05-12 jcs if (err)
2862 1e37a5c2 2019-05-12 jcs break;
2863 15a087fe 2019-02-21 stsp
2864 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2865 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2866 1e37a5c2 2019-05-12 jcs
2867 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2868 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2869 1e37a5c2 2019-05-12 jcs break;
2870 1e37a5c2 2019-05-12 jcs default:
2871 1e37a5c2 2019-05-12 jcs break;
2872 26ed57b2 2018-05-19 stsp }
2873 e5a0f69f 2018-08-18 stsp
2874 bcbd79e2 2018-08-19 stsp return err;
2875 26ed57b2 2018-05-19 stsp }
2876 26ed57b2 2018-05-19 stsp
2877 4ed7e80c 2018-05-20 stsp static const struct got_error *
2878 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2879 9f7d7167 2018-04-29 stsp {
2880 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2881 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2882 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2883 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2884 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2885 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2886 26ed57b2 2018-05-19 stsp int ch;
2887 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2888 70ac5f84 2019-03-28 stsp
2889 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2890 26ed57b2 2018-05-19 stsp
2891 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2892 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2893 eb6600df 2019-01-04 stsp NULL) == -1)
2894 26ed57b2 2018-05-19 stsp err(1, "pledge");
2895 26ed57b2 2018-05-19 stsp #endif
2896 26ed57b2 2018-05-19 stsp
2897 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2898 26ed57b2 2018-05-19 stsp switch (ch) {
2899 26ed57b2 2018-05-19 stsp default:
2900 17020d27 2019-03-07 stsp usage_diff();
2901 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2902 26ed57b2 2018-05-19 stsp }
2903 26ed57b2 2018-05-19 stsp }
2904 26ed57b2 2018-05-19 stsp
2905 26ed57b2 2018-05-19 stsp argc -= optind;
2906 26ed57b2 2018-05-19 stsp argv += optind;
2907 26ed57b2 2018-05-19 stsp
2908 26ed57b2 2018-05-19 stsp if (argc == 0) {
2909 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2910 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2911 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2912 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2913 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2914 15a94983 2018-12-23 stsp id_str1 = argv[0];
2915 15a94983 2018-12-23 stsp id_str2 = argv[1];
2916 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2917 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2918 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2919 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2920 15a94983 2018-12-23 stsp id_str1 = argv[1];
2921 15a94983 2018-12-23 stsp id_str2 = argv[2];
2922 26ed57b2 2018-05-19 stsp } else
2923 26ed57b2 2018-05-19 stsp usage_diff();
2924 a915003a 2019-02-05 stsp
2925 a915003a 2019-02-05 stsp init_curses();
2926 eb6600df 2019-01-04 stsp
2927 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2928 eb6600df 2019-01-04 stsp if (error)
2929 eb6600df 2019-01-04 stsp goto done;
2930 26ed57b2 2018-05-19 stsp
2931 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
2932 26ed57b2 2018-05-19 stsp if (error)
2933 26ed57b2 2018-05-19 stsp goto done;
2934 26ed57b2 2018-05-19 stsp
2935 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
2936 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
2937 26ed57b2 2018-05-19 stsp if (error)
2938 26ed57b2 2018-05-19 stsp goto done;
2939 26ed57b2 2018-05-19 stsp
2940 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
2941 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
2942 26ed57b2 2018-05-19 stsp if (error)
2943 26ed57b2 2018-05-19 stsp goto done;
2944 26ed57b2 2018-05-19 stsp
2945 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2946 8b473291 2019-02-21 stsp if (error)
2947 8b473291 2019-02-21 stsp goto done;
2948 8b473291 2019-02-21 stsp
2949 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2950 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2951 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2952 ea5e7bb5 2018-08-01 stsp goto done;
2953 ea5e7bb5 2018-08-01 stsp }
2954 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2955 5dc9f4bc 2018-08-04 stsp if (error)
2956 5dc9f4bc 2018-08-04 stsp goto done;
2957 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2958 26ed57b2 2018-05-19 stsp done:
2959 c02c541e 2019-03-29 stsp free(repo_path);
2960 921be706 2019-06-28 stsp if (repo)
2961 921be706 2019-06-28 stsp got_repo_close(repo);
2962 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2963 26ed57b2 2018-05-19 stsp return error;
2964 9f7d7167 2018-04-29 stsp }
2965 9f7d7167 2018-04-29 stsp
2966 4ed7e80c 2018-05-20 stsp __dead static void
2967 9f7d7167 2018-04-29 stsp usage_blame(void)
2968 9f7d7167 2018-04-29 stsp {
2969 80ddbec8 2018-04-29 stsp endwin();
2970 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2971 9f7d7167 2018-04-29 stsp getprogname());
2972 9f7d7167 2018-04-29 stsp exit(1);
2973 9f7d7167 2018-04-29 stsp }
2974 84451b3e 2018-07-10 stsp
2975 84451b3e 2018-07-10 stsp struct tog_blame_line {
2976 84451b3e 2018-07-10 stsp int annotated;
2977 84451b3e 2018-07-10 stsp struct got_object_id *id;
2978 84451b3e 2018-07-10 stsp };
2979 9f7d7167 2018-04-29 stsp
2980 4ed7e80c 2018-05-20 stsp static const struct got_error *
2981 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2982 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2983 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2984 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2985 84451b3e 2018-07-10 stsp {
2986 84451b3e 2018-07-10 stsp const struct got_error *err;
2987 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2988 84451b3e 2018-07-10 stsp char *line;
2989 84451b3e 2018-07-10 stsp size_t len;
2990 84451b3e 2018-07-10 stsp wchar_t *wline;
2991 b700b5d6 2018-07-10 stsp int width, wlimit;
2992 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2993 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2994 ab089a2a 2018-07-12 stsp char *id_str;
2995 ab089a2a 2018-07-12 stsp
2996 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2997 ab089a2a 2018-07-12 stsp if (err)
2998 ab089a2a 2018-07-12 stsp return err;
2999 84451b3e 2018-07-10 stsp
3000 84451b3e 2018-07-10 stsp rewind(f);
3001 f7d12f7e 2018-08-01 stsp werase(view->window);
3002 84451b3e 2018-07-10 stsp
3003 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
3004 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3005 ab089a2a 2018-07-12 stsp free(id_str);
3006 ab089a2a 2018-07-12 stsp return err;
3007 ab089a2a 2018-07-12 stsp }
3008 ab089a2a 2018-07-12 stsp
3009 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
3010 ab089a2a 2018-07-12 stsp free(line);
3011 2550e4c3 2018-07-13 stsp line = NULL;
3012 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3013 a3404814 2018-09-02 stsp wstandout(view->window);
3014 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3015 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3016 a3404814 2018-09-02 stsp wstandend(view->window);
3017 2550e4c3 2018-07-13 stsp free(wline);
3018 2550e4c3 2018-07-13 stsp wline = NULL;
3019 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3020 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3021 ab089a2a 2018-07-12 stsp
3022 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
3023 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
3024 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
3025 ab089a2a 2018-07-12 stsp free(id_str);
3026 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3027 ab089a2a 2018-07-12 stsp }
3028 ab089a2a 2018-07-12 stsp free(id_str);
3029 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
3030 3f60a8ef 2018-07-10 stsp free(line);
3031 2550e4c3 2018-07-13 stsp line = NULL;
3032 3f60a8ef 2018-07-10 stsp if (err)
3033 3f60a8ef 2018-07-10 stsp return err;
3034 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3035 2550e4c3 2018-07-13 stsp free(wline);
3036 2550e4c3 2018-07-13 stsp wline = NULL;
3037 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3038 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3039 3f60a8ef 2018-07-10 stsp
3040 84451b3e 2018-07-10 stsp *eof = 0;
3041 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
3042 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
3043 84451b3e 2018-07-10 stsp if (line == NULL) {
3044 84451b3e 2018-07-10 stsp *eof = 1;
3045 84451b3e 2018-07-10 stsp break;
3046 84451b3e 2018-07-10 stsp }
3047 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
3048 84451b3e 2018-07-10 stsp free(line);
3049 84451b3e 2018-07-10 stsp continue;
3050 84451b3e 2018-07-10 stsp }
3051 84451b3e 2018-07-10 stsp
3052 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
3053 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
3054 84451b3e 2018-07-10 stsp if (err) {
3055 84451b3e 2018-07-10 stsp free(line);
3056 84451b3e 2018-07-10 stsp return err;
3057 84451b3e 2018-07-10 stsp }
3058 84451b3e 2018-07-10 stsp
3059 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3060 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3061 b700b5d6 2018-07-10 stsp
3062 8d0fe45a 2019-08-12 stsp if (nlines > 0) {
3063 8d0fe45a 2019-08-12 stsp blame_line = &lines[lineno - 1];
3064 8d0fe45a 2019-08-12 stsp if (blame_line->annotated && prev_id &&
3065 8d0fe45a 2019-08-12 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
3066 8d0fe45a 2019-08-12 stsp waddstr(view->window, " ");
3067 8d0fe45a 2019-08-12 stsp else if (blame_line->annotated) {
3068 8d0fe45a 2019-08-12 stsp char *id_str;
3069 8d0fe45a 2019-08-12 stsp err = got_object_id_str(&id_str, blame_line->id);
3070 8d0fe45a 2019-08-12 stsp if (err) {
3071 8d0fe45a 2019-08-12 stsp free(line);
3072 8d0fe45a 2019-08-12 stsp free(wline);
3073 8d0fe45a 2019-08-12 stsp return err;
3074 8d0fe45a 2019-08-12 stsp }
3075 8d0fe45a 2019-08-12 stsp wprintw(view->window, "%.8s ", id_str);
3076 8d0fe45a 2019-08-12 stsp free(id_str);
3077 8d0fe45a 2019-08-12 stsp prev_id = blame_line->id;
3078 8d0fe45a 2019-08-12 stsp } else {
3079 8d0fe45a 2019-08-12 stsp waddstr(view->window, "........ ");
3080 8d0fe45a 2019-08-12 stsp prev_id = NULL;
3081 84451b3e 2018-07-10 stsp }
3082 ee41ec32 2018-07-10 stsp } else {
3083 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
3084 ee41ec32 2018-07-10 stsp prev_id = NULL;
3085 ee41ec32 2018-07-10 stsp }
3086 84451b3e 2018-07-10 stsp
3087 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3088 b700b5d6 2018-07-10 stsp while (width < wlimit) {
3089 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
3090 b700b5d6 2018-07-10 stsp width++;
3091 b700b5d6 2018-07-10 stsp }
3092 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3093 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3094 84451b3e 2018-07-10 stsp if (++nprinted == 1)
3095 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
3096 84451b3e 2018-07-10 stsp free(line);
3097 2550e4c3 2018-07-13 stsp free(wline);
3098 2550e4c3 2018-07-13 stsp wline = NULL;
3099 84451b3e 2018-07-10 stsp }
3100 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
3101 84451b3e 2018-07-10 stsp
3102 1a57306a 2018-09-02 stsp view_vborder(view);
3103 84451b3e 2018-07-10 stsp
3104 84451b3e 2018-07-10 stsp return NULL;
3105 84451b3e 2018-07-10 stsp }
3106 84451b3e 2018-07-10 stsp
3107 84451b3e 2018-07-10 stsp static const struct got_error *
3108 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3109 84451b3e 2018-07-10 stsp {
3110 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
3111 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
3112 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
3113 1a76625f 2018-10-22 stsp int errcode;
3114 84451b3e 2018-07-10 stsp
3115 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
3116 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3117 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3118 84451b3e 2018-07-10 stsp
3119 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3120 1a76625f 2018-10-22 stsp if (errcode)
3121 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3122 84451b3e 2018-07-10 stsp
3123 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3124 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3125 d68a0a7d 2018-07-10 stsp goto done;
3126 d68a0a7d 2018-07-10 stsp }
3127 d68a0a7d 2018-07-10 stsp
3128 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3129 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3130 d68a0a7d 2018-07-10 stsp
3131 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3132 d68a0a7d 2018-07-10 stsp if (line->annotated)
3133 d68a0a7d 2018-07-10 stsp goto done;
3134 d68a0a7d 2018-07-10 stsp
3135 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3136 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3137 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3138 84451b3e 2018-07-10 stsp goto done;
3139 84451b3e 2018-07-10 stsp }
3140 84451b3e 2018-07-10 stsp line->annotated = 1;
3141 84451b3e 2018-07-10 stsp done:
3142 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3143 1a76625f 2018-10-22 stsp if (errcode)
3144 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3145 84451b3e 2018-07-10 stsp return err;
3146 84451b3e 2018-07-10 stsp }
3147 84451b3e 2018-07-10 stsp
3148 84451b3e 2018-07-10 stsp static void *
3149 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3150 84451b3e 2018-07-10 stsp {
3151 18430de3 2018-07-10 stsp const struct got_error *err;
3152 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3153 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3154 1a76625f 2018-10-22 stsp int errcode;
3155 18430de3 2018-07-10 stsp
3156 0d8ff7d5 2019-08-14 stsp err = got_blame(ta->path, a->commit_id, ta->repo,
3157 fc06ba56 2019-08-22 stsp blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3158 fc06ba56 2019-08-22 stsp if (err && err->code == GOT_ERR_CANCELLED)
3159 fc06ba56 2019-08-22 stsp err = NULL;
3160 18430de3 2018-07-10 stsp
3161 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3162 1a76625f 2018-10-22 stsp if (errcode)
3163 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3164 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3165 18430de3 2018-07-10 stsp
3166 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3167 c9beca56 2018-07-22 stsp ta->repo = NULL;
3168 c9beca56 2018-07-22 stsp *ta->complete = 1;
3169 18430de3 2018-07-10 stsp
3170 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3171 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3172 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3173 18430de3 2018-07-10 stsp
3174 18430de3 2018-07-10 stsp return (void *)err;
3175 84451b3e 2018-07-10 stsp }
3176 84451b3e 2018-07-10 stsp
3177 245d91c1 2018-07-12 stsp static struct got_object_id *
3178 8d0fe45a 2019-08-12 stsp get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3179 8d0fe45a 2019-08-12 stsp int first_displayed_line, int selected_line)
3180 245d91c1 2018-07-12 stsp {
3181 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3182 8d0fe45a 2019-08-12 stsp
3183 8d0fe45a 2019-08-12 stsp if (nlines <= 0)
3184 8d0fe45a 2019-08-12 stsp return NULL;
3185 b880a918 2018-07-10 stsp
3186 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3187 245d91c1 2018-07-12 stsp if (!line->annotated)
3188 245d91c1 2018-07-12 stsp return NULL;
3189 245d91c1 2018-07-12 stsp
3190 245d91c1 2018-07-12 stsp return line->id;
3191 b880a918 2018-07-10 stsp }
3192 245d91c1 2018-07-12 stsp
3193 b880a918 2018-07-10 stsp static const struct got_error *
3194 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3195 a70480e0 2018-06-23 stsp {
3196 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3197 245d91c1 2018-07-12 stsp int i;
3198 245d91c1 2018-07-12 stsp
3199 245d91c1 2018-07-12 stsp if (blame->thread) {
3200 1a76625f 2018-10-22 stsp int errcode;
3201 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3202 1a76625f 2018-10-22 stsp if (errcode)
3203 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3204 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3205 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3206 1a76625f 2018-10-22 stsp if (errcode)
3207 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3208 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3209 1a76625f 2018-10-22 stsp if (errcode)
3210 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3211 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3212 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3213 245d91c1 2018-07-12 stsp err = NULL;
3214 245d91c1 2018-07-12 stsp blame->thread = NULL;
3215 245d91c1 2018-07-12 stsp }
3216 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3217 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3218 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3219 245d91c1 2018-07-12 stsp }
3220 245d91c1 2018-07-12 stsp if (blame->f) {
3221 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3222 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3223 245d91c1 2018-07-12 stsp blame->f = NULL;
3224 245d91c1 2018-07-12 stsp }
3225 57670559 2018-12-23 stsp if (blame->lines) {
3226 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3227 57670559 2018-12-23 stsp free(blame->lines[i].id);
3228 57670559 2018-12-23 stsp free(blame->lines);
3229 57670559 2018-12-23 stsp blame->lines = NULL;
3230 57670559 2018-12-23 stsp }
3231 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3232 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3233 245d91c1 2018-07-12 stsp
3234 245d91c1 2018-07-12 stsp return err;
3235 245d91c1 2018-07-12 stsp }
3236 245d91c1 2018-07-12 stsp
3237 245d91c1 2018-07-12 stsp static const struct got_error *
3238 fc06ba56 2019-08-22 stsp cancel_blame_view(void *arg)
3239 fc06ba56 2019-08-22 stsp {
3240 fc06ba56 2019-08-22 stsp const struct got_error *err = NULL;
3241 fc06ba56 2019-08-22 stsp int *done = arg;
3242 fc06ba56 2019-08-22 stsp int errcode;
3243 fc06ba56 2019-08-22 stsp
3244 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3245 fc06ba56 2019-08-22 stsp if (errcode)
3246 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3247 fc06ba56 2019-08-22 stsp "pthread_mutex_unlock");
3248 fc06ba56 2019-08-22 stsp
3249 fc06ba56 2019-08-22 stsp if (*done)
3250 fc06ba56 2019-08-22 stsp err = got_error(GOT_ERR_CANCELLED);
3251 fc06ba56 2019-08-22 stsp
3252 fc06ba56 2019-08-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3253 fc06ba56 2019-08-22 stsp if (errcode)
3254 fc06ba56 2019-08-22 stsp return got_error_set_errno(errcode,
3255 fc06ba56 2019-08-22 stsp "pthread_mutex_lock");
3256 fc06ba56 2019-08-22 stsp
3257 fc06ba56 2019-08-22 stsp return err;
3258 fc06ba56 2019-08-22 stsp }
3259 fc06ba56 2019-08-22 stsp
3260 fc06ba56 2019-08-22 stsp static const struct got_error *
3261 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3262 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3263 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3264 245d91c1 2018-07-12 stsp struct got_repository *repo)
3265 245d91c1 2018-07-12 stsp {
3266 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3267 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3268 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3269 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3270 15a94983 2018-12-23 stsp int obj_type;
3271 a70480e0 2018-06-23 stsp
3272 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3273 27d434c2 2018-09-15 stsp if (err)
3274 15a94983 2018-12-23 stsp return err;
3275 15a94983 2018-12-23 stsp if (obj_id == NULL)
3276 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
3277 27d434c2 2018-09-15 stsp
3278 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3279 84451b3e 2018-07-10 stsp if (err)
3280 84451b3e 2018-07-10 stsp goto done;
3281 27d434c2 2018-09-15 stsp
3282 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3283 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3284 84451b3e 2018-07-10 stsp goto done;
3285 84451b3e 2018-07-10 stsp }
3286 a70480e0 2018-06-23 stsp
3287 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3288 a70480e0 2018-06-23 stsp if (err)
3289 a70480e0 2018-06-23 stsp goto done;
3290 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3291 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3292 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3293 84451b3e 2018-07-10 stsp goto done;
3294 84451b3e 2018-07-10 stsp }
3295 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3296 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
3297 b02560ec 2019-08-19 stsp if (err || blame->nlines == 0)
3298 84451b3e 2018-07-10 stsp goto done;
3299 b02560ec 2019-08-19 stsp
3300 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3301 b02560ec 2019-08-19 stsp if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3302 b02560ec 2019-08-19 stsp blame->nlines--;
3303 a70480e0 2018-06-23 stsp
3304 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3305 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3306 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3307 84451b3e 2018-07-10 stsp goto done;
3308 84451b3e 2018-07-10 stsp }
3309 a70480e0 2018-06-23 stsp
3310 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3311 bd24772e 2018-07-11 stsp if (err)
3312 bd24772e 2018-07-11 stsp goto done;
3313 bd24772e 2018-07-11 stsp
3314 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3315 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3316 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3317 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3318 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3319 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3320 245d91c1 2018-07-12 stsp goto done;
3321 245d91c1 2018-07-12 stsp }
3322 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3323 245d91c1 2018-07-12 stsp
3324 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3325 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3326 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3327 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3328 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_cb = cancel_blame_view;
3329 fc06ba56 2019-08-22 stsp blame->thread_args.cancel_arg = done;
3330 245d91c1 2018-07-12 stsp *blame_complete = 0;
3331 245d91c1 2018-07-12 stsp
3332 245d91c1 2018-07-12 stsp done:
3333 245d91c1 2018-07-12 stsp if (blob)
3334 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3335 27d434c2 2018-09-15 stsp free(obj_id);
3336 245d91c1 2018-07-12 stsp if (err)
3337 245d91c1 2018-07-12 stsp stop_blame(blame);
3338 245d91c1 2018-07-12 stsp return err;
3339 245d91c1 2018-07-12 stsp }
3340 245d91c1 2018-07-12 stsp
3341 245d91c1 2018-07-12 stsp static const struct got_error *
3342 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3343 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3344 8b473291 2019-02-21 stsp struct got_repository *repo)
3345 245d91c1 2018-07-12 stsp {
3346 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3347 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3348 dbc6a6b6 2018-07-12 stsp
3349 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3350 245d91c1 2018-07-12 stsp
3351 c4843652 2019-08-12 stsp s->path = strdup(path);
3352 c4843652 2019-08-12 stsp if (s->path == NULL)
3353 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
3354 c4843652 2019-08-12 stsp
3355 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3356 c4843652 2019-08-12 stsp if (err) {
3357 c4843652 2019-08-12 stsp free(s->path);
3358 7cbe629d 2018-08-04 stsp return err;
3359 c4843652 2019-08-12 stsp }
3360 245d91c1 2018-07-12 stsp
3361 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3362 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3363 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3364 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3365 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3366 fb2756b9 2018-08-04 stsp s->repo = repo;
3367 8b473291 2019-02-21 stsp s->refs = refs;
3368 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3369 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3370 7cbe629d 2018-08-04 stsp
3371 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3372 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3373 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3374 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
3375 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
3376 e5a0f69f 2018-08-18 stsp
3377 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3378 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3379 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3380 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3381 7cbe629d 2018-08-04 stsp }
3382 7cbe629d 2018-08-04 stsp
3383 e5a0f69f 2018-08-18 stsp static const struct got_error *
3384 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3385 7cbe629d 2018-08-04 stsp {
3386 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3387 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3388 7cbe629d 2018-08-04 stsp
3389 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3390 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3391 e5a0f69f 2018-08-18 stsp
3392 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3393 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3394 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3395 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3396 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3397 7cbe629d 2018-08-04 stsp }
3398 e5a0f69f 2018-08-18 stsp
3399 e5a0f69f 2018-08-18 stsp free(s->path);
3400 e5a0f69f 2018-08-18 stsp
3401 e5a0f69f 2018-08-18 stsp return err;
3402 7cbe629d 2018-08-04 stsp }
3403 7cbe629d 2018-08-04 stsp
3404 7cbe629d 2018-08-04 stsp static const struct got_error *
3405 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
3406 6c4c42e0 2019-06-24 stsp {
3407 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3408 6c4c42e0 2019-06-24 stsp
3409 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
3410 6c4c42e0 2019-06-24 stsp return NULL;
3411 6c4c42e0 2019-06-24 stsp }
3412 6c4c42e0 2019-06-24 stsp
3413 6c4c42e0 2019-06-24 stsp static int
3414 6c4c42e0 2019-06-24 stsp match_line(const char *line, regex_t *regex)
3415 6c4c42e0 2019-06-24 stsp {
3416 6c4c42e0 2019-06-24 stsp regmatch_t regmatch;
3417 6c4c42e0 2019-06-24 stsp
3418 6c4c42e0 2019-06-24 stsp return regexec(regex, line, 1, &regmatch, 0) == 0;
3419 6c4c42e0 2019-06-24 stsp }
3420 6c4c42e0 2019-06-24 stsp
3421 6c4c42e0 2019-06-24 stsp
3422 6c4c42e0 2019-06-24 stsp static const struct got_error *
3423 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
3424 6c4c42e0 2019-06-24 stsp {
3425 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3426 6c4c42e0 2019-06-24 stsp int lineno;
3427 6c4c42e0 2019-06-24 stsp
3428 6c4c42e0 2019-06-24 stsp if (!view->searching) {
3429 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3430 6c4c42e0 2019-06-24 stsp return NULL;
3431 6c4c42e0 2019-06-24 stsp }
3432 6c4c42e0 2019-06-24 stsp
3433 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3434 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3435 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
3436 6c4c42e0 2019-06-24 stsp else
3437 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
3438 6c4c42e0 2019-06-24 stsp } else {
3439 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3440 6c4c42e0 2019-06-24 stsp lineno = 1;
3441 6c4c42e0 2019-06-24 stsp else
3442 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3443 6c4c42e0 2019-06-24 stsp }
3444 6c4c42e0 2019-06-24 stsp
3445 6c4c42e0 2019-06-24 stsp while (1) {
3446 6c4c42e0 2019-06-24 stsp char *line = NULL;
3447 6c4c42e0 2019-06-24 stsp off_t offset;
3448 6c4c42e0 2019-06-24 stsp size_t len;
3449 6c4c42e0 2019-06-24 stsp
3450 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
3451 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
3452 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3453 6c4c42e0 2019-06-24 stsp free(line);
3454 6c4c42e0 2019-06-24 stsp break;
3455 6c4c42e0 2019-06-24 stsp }
3456 2246482e 2019-06-25 stsp
3457 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3458 6c4c42e0 2019-06-24 stsp lineno = 1;
3459 6c4c42e0 2019-06-24 stsp else
3460 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3461 6c4c42e0 2019-06-24 stsp }
3462 6c4c42e0 2019-06-24 stsp
3463 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
3464 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3465 6c4c42e0 2019-06-24 stsp free(line);
3466 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
3467 6c4c42e0 2019-06-24 stsp }
3468 6c4c42e0 2019-06-24 stsp free(line);
3469 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
3470 2246482e 2019-06-25 stsp if (line && match_line(line, &view->regex)) {
3471 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3472 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
3473 6c4c42e0 2019-06-24 stsp free(line);
3474 6c4c42e0 2019-06-24 stsp break;
3475 6c4c42e0 2019-06-24 stsp }
3476 6c4c42e0 2019-06-24 stsp free(line);
3477 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3478 6c4c42e0 2019-06-24 stsp lineno++;
3479 6c4c42e0 2019-06-24 stsp else
3480 6c4c42e0 2019-06-24 stsp lineno--;
3481 6c4c42e0 2019-06-24 stsp }
3482 6c4c42e0 2019-06-24 stsp
3483 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3484 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
3485 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
3486 6c4c42e0 2019-06-24 stsp }
3487 6c4c42e0 2019-06-24 stsp
3488 6c4c42e0 2019-06-24 stsp return NULL;
3489 6c4c42e0 2019-06-24 stsp }
3490 6c4c42e0 2019-06-24 stsp
3491 6c4c42e0 2019-06-24 stsp static const struct got_error *
3492 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
3493 7cbe629d 2018-08-04 stsp {
3494 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3495 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
3496 2b380cc8 2018-10-24 stsp int errcode;
3497 2b380cc8 2018-10-24 stsp
3498 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
3499 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3500 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
3501 2b380cc8 2018-10-24 stsp if (errcode)
3502 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3503 51fe7530 2019-08-19 stsp
3504 51fe7530 2019-08-19 stsp halfdelay(1); /* fast refresh while annotating */
3505 2b380cc8 2018-10-24 stsp }
3506 e5a0f69f 2018-08-18 stsp
3507 51fe7530 2019-08-19 stsp if (s->blame_complete)
3508 51fe7530 2019-08-19 stsp halfdelay(10); /* disable fast refresh */
3509 51fe7530 2019-08-19 stsp
3510 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3511 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3512 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
3513 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
3514 e5a0f69f 2018-08-18 stsp
3515 669b5ffa 2018-10-07 stsp view_vborder(view);
3516 e5a0f69f 2018-08-18 stsp return err;
3517 e5a0f69f 2018-08-18 stsp }
3518 e5a0f69f 2018-08-18 stsp
3519 e5a0f69f 2018-08-18 stsp static const struct got_error *
3520 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3521 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3522 e5a0f69f 2018-08-18 stsp {
3523 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
3524 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
3525 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3526 669b5ffa 2018-10-07 stsp int begin_x = 0;
3527 7cbe629d 2018-08-04 stsp
3528 e5a0f69f 2018-08-18 stsp switch (ch) {
3529 1e37a5c2 2019-05-12 jcs case 'q':
3530 1e37a5c2 2019-05-12 jcs s->done = 1;
3531 1e37a5c2 2019-05-12 jcs break;
3532 1e37a5c2 2019-05-12 jcs case 'k':
3533 1e37a5c2 2019-05-12 jcs case KEY_UP:
3534 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
3535 1e37a5c2 2019-05-12 jcs s->selected_line--;
3536 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
3537 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3538 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3539 1e37a5c2 2019-05-12 jcs break;
3540 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3541 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
3542 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3543 e5a0f69f 2018-08-18 stsp break;
3544 1e37a5c2 2019-05-12 jcs }
3545 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3546 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3547 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3548 1e37a5c2 2019-05-12 jcs else
3549 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3550 1e37a5c2 2019-05-12 jcs break;
3551 1e37a5c2 2019-05-12 jcs case 'j':
3552 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3553 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3554 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3555 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3556 1e37a5c2 2019-05-12 jcs s->selected_line++;
3557 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3558 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3559 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3560 1e37a5c2 2019-05-12 jcs break;
3561 1e37a5c2 2019-05-12 jcs case 'b':
3562 1e37a5c2 2019-05-12 jcs case 'p': {
3563 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3564 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3565 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3566 1e37a5c2 2019-05-12 jcs if (id == NULL)
3567 e5a0f69f 2018-08-18 stsp break;
3568 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3569 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3570 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3571 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3572 1e37a5c2 2019-05-12 jcs int obj_type;
3573 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3574 1e37a5c2 2019-05-12 jcs s->repo, id);
3575 e5a0f69f 2018-08-18 stsp if (err)
3576 e5a0f69f 2018-08-18 stsp break;
3577 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3578 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3579 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3580 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3581 e5a0f69f 2018-08-18 stsp break;
3582 e5a0f69f 2018-08-18 stsp }
3583 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3584 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3585 1e37a5c2 2019-05-12 jcs pid->id, s->path);
3586 e5a0f69f 2018-08-18 stsp if (err) {
3587 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
3588 1e37a5c2 2019-05-12 jcs err = NULL;
3589 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3590 e5a0f69f 2018-08-18 stsp break;
3591 e5a0f69f 2018-08-18 stsp }
3592 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
3593 1e37a5c2 2019-05-12 jcs blob_id);
3594 1e37a5c2 2019-05-12 jcs free(blob_id);
3595 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
3596 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
3597 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3598 e5a0f69f 2018-08-18 stsp break;
3599 1e37a5c2 2019-05-12 jcs }
3600 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3601 1e37a5c2 2019-05-12 jcs pid->id);
3602 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3603 1e37a5c2 2019-05-12 jcs } else {
3604 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
3605 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
3606 1e37a5c2 2019-05-12 jcs break;
3607 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3608 1e37a5c2 2019-05-12 jcs id);
3609 1e37a5c2 2019-05-12 jcs }
3610 1e37a5c2 2019-05-12 jcs if (err)
3611 e5a0f69f 2018-08-18 stsp break;
3612 1e37a5c2 2019-05-12 jcs s->done = 1;
3613 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3614 1e37a5c2 2019-05-12 jcs s->done = 0;
3615 1e37a5c2 2019-05-12 jcs if (thread_err)
3616 1e37a5c2 2019-05-12 jcs break;
3617 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3618 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
3619 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3620 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3621 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
3622 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
3623 1e37a5c2 2019-05-12 jcs if (err)
3624 1e37a5c2 2019-05-12 jcs break;
3625 1e37a5c2 2019-05-12 jcs break;
3626 1e37a5c2 2019-05-12 jcs }
3627 1e37a5c2 2019-05-12 jcs case 'B': {
3628 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
3629 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
3630 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
3631 1e37a5c2 2019-05-12 jcs break;
3632 1e37a5c2 2019-05-12 jcs s->done = 1;
3633 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3634 1e37a5c2 2019-05-12 jcs s->done = 0;
3635 1e37a5c2 2019-05-12 jcs if (thread_err)
3636 1e37a5c2 2019-05-12 jcs break;
3637 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3638 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
3639 1e37a5c2 2019-05-12 jcs s->blamed_commit =
3640 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
3641 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3642 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3643 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
3644 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
3645 1e37a5c2 2019-05-12 jcs if (err)
3646 1e37a5c2 2019-05-12 jcs break;
3647 1e37a5c2 2019-05-12 jcs break;
3648 1e37a5c2 2019-05-12 jcs }
3649 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3650 1e37a5c2 2019-05-12 jcs case '\r': {
3651 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3652 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
3653 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
3654 8d0fe45a 2019-08-12 stsp id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3655 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3656 1e37a5c2 2019-05-12 jcs if (id == NULL)
3657 1e37a5c2 2019-05-12 jcs break;
3658 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
3659 1e37a5c2 2019-05-12 jcs if (err)
3660 1e37a5c2 2019-05-12 jcs break;
3661 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
3662 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
3663 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3664 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3665 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3666 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
3667 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3668 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
3669 1e37a5c2 2019-05-12 jcs break;
3670 15a94983 2018-12-23 stsp }
3671 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
3672 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
3673 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3674 1e37a5c2 2019-05-12 jcs if (err) {
3675 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3676 1e37a5c2 2019-05-12 jcs break;
3677 1e37a5c2 2019-05-12 jcs }
3678 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3679 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3680 1e37a5c2 2019-05-12 jcs if (err)
3681 34bc9ec9 2019-02-22 stsp break;
3682 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
3683 1e37a5c2 2019-05-12 jcs if (err) {
3684 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3685 e5a0f69f 2018-08-18 stsp break;
3686 e5a0f69f 2018-08-18 stsp }
3687 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
3688 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
3689 1e37a5c2 2019-05-12 jcs } else
3690 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3691 1e37a5c2 2019-05-12 jcs if (err)
3692 e5a0f69f 2018-08-18 stsp break;
3693 1e37a5c2 2019-05-12 jcs break;
3694 1e37a5c2 2019-05-12 jcs }
3695 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3696 1e37a5c2 2019-05-12 jcs case ' ':
3697 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3698 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
3699 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
3700 e5a0f69f 2018-08-18 stsp break;
3701 1e37a5c2 2019-05-12 jcs }
3702 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3703 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
3704 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3705 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3706 e5a0f69f 2018-08-18 stsp break;
3707 1e37a5c2 2019-05-12 jcs }
3708 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
3709 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
3710 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
3711 1e37a5c2 2019-05-12 jcs view->nlines - 2;
3712 1e37a5c2 2019-05-12 jcs else
3713 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
3714 1e37a5c2 2019-05-12 jcs s->blame.nlines -
3715 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
3716 1e37a5c2 2019-05-12 jcs break;
3717 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3718 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
3719 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3720 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3721 1e37a5c2 2019-05-12 jcs }
3722 1e37a5c2 2019-05-12 jcs break;
3723 1e37a5c2 2019-05-12 jcs default:
3724 1e37a5c2 2019-05-12 jcs break;
3725 a70480e0 2018-06-23 stsp }
3726 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
3727 a70480e0 2018-06-23 stsp }
3728 a70480e0 2018-06-23 stsp
3729 a70480e0 2018-06-23 stsp static const struct got_error *
3730 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
3731 9f7d7167 2018-04-29 stsp {
3732 a70480e0 2018-06-23 stsp const struct got_error *error;
3733 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
3734 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3735 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
3736 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3737 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3738 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
3739 a70480e0 2018-06-23 stsp int ch;
3740 e1cd8fed 2018-08-01 stsp struct tog_view *view;
3741 a70480e0 2018-06-23 stsp
3742 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3743 70ac5f84 2019-03-28 stsp
3744 a70480e0 2018-06-23 stsp #ifndef PROFILE
3745 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3746 8e94dd5b 2019-01-04 stsp NULL) == -1)
3747 a70480e0 2018-06-23 stsp err(1, "pledge");
3748 a70480e0 2018-06-23 stsp #endif
3749 a70480e0 2018-06-23 stsp
3750 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3751 a70480e0 2018-06-23 stsp switch (ch) {
3752 a70480e0 2018-06-23 stsp case 'c':
3753 a70480e0 2018-06-23 stsp commit_id_str = optarg;
3754 a70480e0 2018-06-23 stsp break;
3755 69069811 2018-08-02 stsp case 'r':
3756 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3757 69069811 2018-08-02 stsp if (repo_path == NULL)
3758 69069811 2018-08-02 stsp err(1, "-r option");
3759 69069811 2018-08-02 stsp break;
3760 a70480e0 2018-06-23 stsp default:
3761 17020d27 2019-03-07 stsp usage_blame();
3762 a70480e0 2018-06-23 stsp /* NOTREACHED */
3763 a70480e0 2018-06-23 stsp }
3764 a70480e0 2018-06-23 stsp }
3765 a70480e0 2018-06-23 stsp
3766 a70480e0 2018-06-23 stsp argc -= optind;
3767 a70480e0 2018-06-23 stsp argv += optind;
3768 a70480e0 2018-06-23 stsp
3769 69069811 2018-08-02 stsp if (argc == 1)
3770 69069811 2018-08-02 stsp path = argv[0];
3771 69069811 2018-08-02 stsp else
3772 a70480e0 2018-06-23 stsp usage_blame();
3773 69069811 2018-08-02 stsp
3774 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
3775 69069811 2018-08-02 stsp if (cwd == NULL) {
3776 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3777 69069811 2018-08-02 stsp goto done;
3778 69069811 2018-08-02 stsp }
3779 69069811 2018-08-02 stsp if (repo_path == NULL) {
3780 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3781 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3782 69069811 2018-08-02 stsp goto done;
3783 eb41ed75 2019-02-05 stsp else
3784 eb41ed75 2019-02-05 stsp error = NULL;
3785 eb41ed75 2019-02-05 stsp if (worktree) {
3786 eb41ed75 2019-02-05 stsp repo_path =
3787 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3788 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
3789 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3790 eb41ed75 2019-02-05 stsp if (error)
3791 eb41ed75 2019-02-05 stsp goto done;
3792 eb41ed75 2019-02-05 stsp } else {
3793 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
3794 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
3795 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3796 eb41ed75 2019-02-05 stsp goto done;
3797 eb41ed75 2019-02-05 stsp }
3798 69069811 2018-08-02 stsp }
3799 69069811 2018-08-02 stsp }
3800 a915003a 2019-02-05 stsp
3801 a915003a 2019-02-05 stsp init_curses();
3802 69069811 2018-08-02 stsp
3803 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
3804 c02c541e 2019-03-29 stsp if (error != NULL)
3805 8e94dd5b 2019-01-04 stsp goto done;
3806 69069811 2018-08-02 stsp
3807 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3808 c02c541e 2019-03-29 stsp if (error)
3809 92205607 2019-01-04 stsp goto done;
3810 69069811 2018-08-02 stsp
3811 eb41ed75 2019-02-05 stsp if (worktree) {
3812 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3813 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3814 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3815 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3816 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3817 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3818 eb41ed75 2019-02-05 stsp path) == -1) {
3819 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3820 eb41ed75 2019-02-05 stsp goto done;
3821 eb41ed75 2019-02-05 stsp }
3822 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3823 eb41ed75 2019-02-05 stsp free(p);
3824 eb41ed75 2019-02-05 stsp } else {
3825 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3826 eb41ed75 2019-02-05 stsp }
3827 eb41ed75 2019-02-05 stsp if (error)
3828 69069811 2018-08-02 stsp goto done;
3829 a70480e0 2018-06-23 stsp
3830 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
3831 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
3832 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3833 a70480e0 2018-06-23 stsp if (error != NULL)
3834 66b4983c 2018-06-23 stsp goto done;
3835 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3836 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
3837 a70480e0 2018-06-23 stsp } else {
3838 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&commit_id, commit_id_str, repo);
3839 f2b6a97d 2019-07-15 stsp if (error) {
3840 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
3841 f2b6a97d 2019-07-15 stsp goto done;
3842 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&commit_id,
3843 f2b6a97d 2019-07-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3844 f2b6a97d 2019-07-15 stsp }
3845 a70480e0 2018-06-23 stsp }
3846 a19e88aa 2018-06-23 stsp if (error != NULL)
3847 8b473291 2019-02-21 stsp goto done;
3848 8b473291 2019-02-21 stsp
3849 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3850 8b473291 2019-02-21 stsp if (error)
3851 a19e88aa 2018-06-23 stsp goto done;
3852 a70480e0 2018-06-23 stsp
3853 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3854 e1cd8fed 2018-08-01 stsp if (view == NULL) {
3855 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3856 e1cd8fed 2018-08-01 stsp goto done;
3857 e1cd8fed 2018-08-01 stsp }
3858 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3859 7cbe629d 2018-08-04 stsp if (error)
3860 7cbe629d 2018-08-04 stsp goto done;
3861 12314ad4 2019-08-31 stsp if (worktree) {
3862 12314ad4 2019-08-31 stsp /* Release work tree lock. */
3863 12314ad4 2019-08-31 stsp got_worktree_close(worktree);
3864 12314ad4 2019-08-31 stsp worktree = NULL;
3865 12314ad4 2019-08-31 stsp }
3866 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3867 a70480e0 2018-06-23 stsp done:
3868 69069811 2018-08-02 stsp free(repo_path);
3869 69069811 2018-08-02 stsp free(cwd);
3870 a70480e0 2018-06-23 stsp free(commit_id);
3871 eb41ed75 2019-02-05 stsp if (worktree)
3872 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
3873 a70480e0 2018-06-23 stsp if (repo)
3874 a70480e0 2018-06-23 stsp got_repo_close(repo);
3875 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3876 a70480e0 2018-06-23 stsp return error;
3877 ffd1d5e5 2018-06-23 stsp }
3878 ffd1d5e5 2018-06-23 stsp
3879 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3880 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
3881 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
3882 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
3883 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
3884 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
3885 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
3886 ffd1d5e5 2018-06-23 stsp {
3887 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3888 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
3889 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
3890 ffd1d5e5 2018-06-23 stsp int width, n;
3891 ffd1d5e5 2018-06-23 stsp
3892 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
3893 ffd1d5e5 2018-06-23 stsp
3894 f7d12f7e 2018-08-01 stsp werase(view->window);
3895 ffd1d5e5 2018-06-23 stsp
3896 ffd1d5e5 2018-06-23 stsp if (limit == 0)
3897 ffd1d5e5 2018-06-23 stsp return NULL;
3898 ffd1d5e5 2018-06-23 stsp
3899 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
3900 ffd1d5e5 2018-06-23 stsp if (err)
3901 ffd1d5e5 2018-06-23 stsp return err;
3902 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3903 a3404814 2018-09-02 stsp wstandout(view->window);
3904 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3905 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3906 a3404814 2018-09-02 stsp wstandend(view->window);
3907 2550e4c3 2018-07-13 stsp free(wline);
3908 2550e4c3 2018-07-13 stsp wline = NULL;
3909 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3910 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3911 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3912 ffd1d5e5 2018-06-23 stsp return NULL;
3913 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
3914 ce52c690 2018-06-23 stsp if (err)
3915 ce52c690 2018-06-23 stsp return err;
3916 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3917 2550e4c3 2018-07-13 stsp free(wline);
3918 2550e4c3 2018-07-13 stsp wline = NULL;
3919 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3920 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3921 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3922 ffd1d5e5 2018-06-23 stsp return NULL;
3923 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3924 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
3925 a1eca9bb 2018-06-23 stsp return NULL;
3926 ffd1d5e5 2018-06-23 stsp
3927 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3928 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
3929 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
3930 0cf4efb1 2018-09-29 stsp if (view->focussed)
3931 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3932 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
3933 ffd1d5e5 2018-06-23 stsp }
3934 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
3935 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
3936 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3937 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3938 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3939 ffd1d5e5 2018-06-23 stsp return NULL;
3940 ffd1d5e5 2018-06-23 stsp n = 1;
3941 ffd1d5e5 2018-06-23 stsp } else {
3942 ffd1d5e5 2018-06-23 stsp n = 0;
3943 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
3944 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3945 ffd1d5e5 2018-06-23 stsp }
3946 ffd1d5e5 2018-06-23 stsp
3947 ffd1d5e5 2018-06-23 stsp while (te) {
3948 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
3949 848d6979 2019-08-12 stsp const char *modestr = "";
3950 1d13200f 2018-07-12 stsp
3951 1d13200f 2018-07-12 stsp if (show_ids) {
3952 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
3953 1d13200f 2018-07-12 stsp if (err)
3954 638f9024 2019-05-13 stsp return got_error_from_errno(
3955 230a42bd 2019-05-11 jcs "got_object_id_str");
3956 1d13200f 2018-07-12 stsp }
3957 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
3958 63c5ca5d 2019-08-24 stsp modestr = "$";
3959 63c5ca5d 2019-08-24 stsp else if (S_ISLNK(te->mode))
3960 848d6979 2019-08-12 stsp modestr = "@";
3961 848d6979 2019-08-12 stsp else if (S_ISDIR(te->mode))
3962 848d6979 2019-08-12 stsp modestr = "/";
3963 848d6979 2019-08-12 stsp else if (te->mode & S_IXUSR)
3964 848d6979 2019-08-12 stsp modestr = "*";
3965 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3966 848d6979 2019-08-12 stsp te->name, modestr) == -1) {
3967 1d13200f 2018-07-12 stsp free(id_str);
3968 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3969 1d13200f 2018-07-12 stsp }
3970 1d13200f 2018-07-12 stsp free(id_str);
3971 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
3972 ffd1d5e5 2018-06-23 stsp if (err) {
3973 ffd1d5e5 2018-06-23 stsp free(line);
3974 ffd1d5e5 2018-06-23 stsp break;
3975 ffd1d5e5 2018-06-23 stsp }
3976 ffd1d5e5 2018-06-23 stsp if (n == selected) {
3977 0cf4efb1 2018-09-29 stsp if (view->focussed)
3978 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3979 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
3980 ffd1d5e5 2018-06-23 stsp }
3981 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3982 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3983 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3984 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
3985 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3986 ffd1d5e5 2018-06-23 stsp free(line);
3987 2550e4c3 2018-07-13 stsp free(wline);
3988 2550e4c3 2018-07-13 stsp wline = NULL;
3989 ffd1d5e5 2018-06-23 stsp n++;
3990 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3991 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
3992 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3993 ffd1d5e5 2018-06-23 stsp break;
3994 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3995 ffd1d5e5 2018-06-23 stsp }
3996 ffd1d5e5 2018-06-23 stsp
3997 ffd1d5e5 2018-06-23 stsp return err;
3998 ffd1d5e5 2018-06-23 stsp }
3999 ffd1d5e5 2018-06-23 stsp
4000 ffd1d5e5 2018-06-23 stsp static void
4001 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
4002 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
4003 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
4004 ffd1d5e5 2018-06-23 stsp {
4005 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
4006 ffd1d5e5 2018-06-23 stsp int i;
4007 ffd1d5e5 2018-06-23 stsp
4008 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
4009 ffd1d5e5 2018-06-23 stsp return;
4010 ffd1d5e5 2018-06-23 stsp
4011 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
4012 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
4013 ffd1d5e5 2018-06-23 stsp if (!isroot)
4014 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4015 ffd1d5e5 2018-06-23 stsp return;
4016 ffd1d5e5 2018-06-23 stsp }
4017 ffd1d5e5 2018-06-23 stsp
4018 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
4019 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
4020 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
4021 ffd1d5e5 2018-06-23 stsp prev = te;
4022 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
4023 ffd1d5e5 2018-06-23 stsp }
4024 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
4025 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
4026 ffd1d5e5 2018-06-23 stsp }
4027 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4028 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
4029 ffd1d5e5 2018-06-23 stsp }
4030 ffd1d5e5 2018-06-23 stsp
4031 768394f3 2019-01-24 stsp static int
4032 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4033 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
4034 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
4035 ffd1d5e5 2018-06-23 stsp {
4036 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
4037 ffd1d5e5 2018-06-23 stsp int n = 0;
4038 ffd1d5e5 2018-06-23 stsp
4039 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
4040 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4041 ffd1d5e5 2018-06-23 stsp else
4042 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
4043 768394f3 2019-01-24 stsp last = last_displayed_entry;
4044 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
4045 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
4046 768394f3 2019-01-24 stsp if (last) {
4047 768394f3 2019-01-24 stsp *first_displayed_entry = next;
4048 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
4049 768394f3 2019-01-24 stsp }
4050 ffd1d5e5 2018-06-23 stsp }
4051 768394f3 2019-01-24 stsp return n;
4052 ffd1d5e5 2018-06-23 stsp }
4053 ffd1d5e5 2018-06-23 stsp
4054 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4055 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
4056 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
4057 ffd1d5e5 2018-06-23 stsp {
4058 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
4059 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
4060 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
4061 ffd1d5e5 2018-06-23 stsp
4062 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
4063 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
4064 ce52c690 2018-06-23 stsp if (te)
4065 ce52c690 2018-06-23 stsp len += strlen(te->name);
4066 ce52c690 2018-06-23 stsp
4067 ce52c690 2018-06-23 stsp *path = calloc(1, len);
4068 ffd1d5e5 2018-06-23 stsp if (path == NULL)
4069 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
4070 ffd1d5e5 2018-06-23 stsp
4071 ce52c690 2018-06-23 stsp (*path)[0] = '/';
4072 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
4073 d9765a41 2018-06-23 stsp while (pt) {
4074 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4075 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4076 cb2ebc8a 2018-06-23 stsp goto done;
4077 cb2ebc8a 2018-06-23 stsp }
4078 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
4079 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4080 cb2ebc8a 2018-06-23 stsp goto done;
4081 cb2ebc8a 2018-06-23 stsp }
4082 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4083 ffd1d5e5 2018-06-23 stsp }
4084 ce52c690 2018-06-23 stsp if (te) {
4085 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
4086 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
4087 ce52c690 2018-06-23 stsp goto done;
4088 ce52c690 2018-06-23 stsp }
4089 cb2ebc8a 2018-06-23 stsp }
4090 ce52c690 2018-06-23 stsp done:
4091 ce52c690 2018-06-23 stsp if (err) {
4092 ce52c690 2018-06-23 stsp free(*path);
4093 ce52c690 2018-06-23 stsp *path = NULL;
4094 ce52c690 2018-06-23 stsp }
4095 ce52c690 2018-06-23 stsp return err;
4096 ce52c690 2018-06-23 stsp }
4097 ce52c690 2018-06-23 stsp
4098 ce52c690 2018-06-23 stsp static const struct got_error *
4099 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
4100 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4101 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4102 8b473291 2019-02-21 stsp struct got_repository *repo)
4103 ce52c690 2018-06-23 stsp {
4104 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
4105 ce52c690 2018-06-23 stsp char *path;
4106 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
4107 a0de39f3 2019-08-09 stsp
4108 a0de39f3 2019-08-09 stsp *new_view = NULL;
4109 69efd4c4 2018-07-18 stsp
4110 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
4111 ce52c690 2018-06-23 stsp if (err)
4112 ce52c690 2018-06-23 stsp return err;
4113 ffd1d5e5 2018-06-23 stsp
4114 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4115 83ce39e3 2019-08-12 stsp if (blame_view == NULL) {
4116 83ce39e3 2019-08-12 stsp err = got_error_from_errno("view_open");
4117 83ce39e3 2019-08-12 stsp goto done;
4118 83ce39e3 2019-08-12 stsp }
4119 cdf1ee82 2018-08-01 stsp
4120 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
4121 e5a0f69f 2018-08-18 stsp if (err) {
4122 fc06ba56 2019-08-22 stsp if (err->code == GOT_ERR_CANCELLED)
4123 fc06ba56 2019-08-22 stsp err = NULL;
4124 e5a0f69f 2018-08-18 stsp view_close(blame_view);
4125 e5a0f69f 2018-08-18 stsp } else
4126 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
4127 83ce39e3 2019-08-12 stsp done:
4128 83ce39e3 2019-08-12 stsp free(path);
4129 69efd4c4 2018-07-18 stsp return err;
4130 69efd4c4 2018-07-18 stsp }
4131 69efd4c4 2018-07-18 stsp
4132 69efd4c4 2018-07-18 stsp static const struct got_error *
4133 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
4134 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4135 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4136 8b473291 2019-02-21 stsp struct got_repository *repo)
4137 69efd4c4 2018-07-18 stsp {
4138 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
4139 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
4140 69efd4c4 2018-07-18 stsp char *path;
4141 a0de39f3 2019-08-09 stsp
4142 a0de39f3 2019-08-09 stsp *new_view = NULL;
4143 69efd4c4 2018-07-18 stsp
4144 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4145 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
4146 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
4147 e5a0f69f 2018-08-18 stsp
4148 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
4149 69efd4c4 2018-07-18 stsp if (err)
4150 69efd4c4 2018-07-18 stsp return err;
4151 69efd4c4 2018-07-18 stsp
4152 d01904d4 2019-06-25 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4153 ba4f502b 2018-08-04 stsp if (err)
4154 e5a0f69f 2018-08-18 stsp view_close(log_view);
4155 e5a0f69f 2018-08-18 stsp else
4156 e5a0f69f 2018-08-18 stsp *new_view = log_view;
4157 cb2ebc8a 2018-06-23 stsp free(path);
4158 cb2ebc8a 2018-06-23 stsp return err;
4159 ffd1d5e5 2018-06-23 stsp }
4160 ffd1d5e5 2018-06-23 stsp
4161 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4162 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
4163 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4164 8b473291 2019-02-21 stsp struct got_repository *repo)
4165 ffd1d5e5 2018-06-23 stsp {
4166 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4167 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
4168 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4169 ffd1d5e5 2018-06-23 stsp
4170 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
4171 ffd1d5e5 2018-06-23 stsp
4172 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
4173 ffd1d5e5 2018-06-23 stsp if (err != NULL)
4174 ffd1d5e5 2018-06-23 stsp goto done;
4175 ffd1d5e5 2018-06-23 stsp
4176 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4177 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4178 ffd1d5e5 2018-06-23 stsp goto done;
4179 ffd1d5e5 2018-06-23 stsp }
4180 ffd1d5e5 2018-06-23 stsp
4181 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
4182 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
4183 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4184 8d0fe45a 2019-08-12 stsp s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4185 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
4186 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
4187 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4188 6484ec90 2018-09-29 stsp goto done;
4189 6484ec90 2018-09-29 stsp }
4190 8b473291 2019-02-21 stsp s->refs = refs;
4191 fb2756b9 2018-08-04 stsp s->repo = repo;
4192 e5a0f69f 2018-08-18 stsp
4193 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
4194 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
4195 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
4196 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
4197 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
4198 ad80ab7b 2018-08-04 stsp done:
4199 ad80ab7b 2018-08-04 stsp free(commit_id_str);
4200 6484ec90 2018-09-29 stsp if (err) {
4201 fb2756b9 2018-08-04 stsp free(s->tree_label);
4202 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4203 6484ec90 2018-09-29 stsp }
4204 ad80ab7b 2018-08-04 stsp return err;
4205 ad80ab7b 2018-08-04 stsp }
4206 ad80ab7b 2018-08-04 stsp
4207 e5a0f69f 2018-08-18 stsp static const struct got_error *
4208 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
4209 ad80ab7b 2018-08-04 stsp {
4210 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4211 ad80ab7b 2018-08-04 stsp
4212 fb2756b9 2018-08-04 stsp free(s->tree_label);
4213 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4214 6484ec90 2018-09-29 stsp free(s->commit_id);
4215 6484ec90 2018-09-29 stsp s->commit_id = NULL;
4216 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
4217 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
4218 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
4219 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
4220 ad80ab7b 2018-08-04 stsp free(parent);
4221 ad80ab7b 2018-08-04 stsp
4222 ad80ab7b 2018-08-04 stsp }
4223 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
4224 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
4225 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
4226 7c32bd05 2019-06-22 stsp
4227 7c32bd05 2019-06-22 stsp return NULL;
4228 7c32bd05 2019-06-22 stsp }
4229 7c32bd05 2019-06-22 stsp
4230 7c32bd05 2019-06-22 stsp static const struct got_error *
4231 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
4232 7c32bd05 2019-06-22 stsp {
4233 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4234 7c32bd05 2019-06-22 stsp
4235 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
4236 7c32bd05 2019-06-22 stsp return NULL;
4237 7c32bd05 2019-06-22 stsp }
4238 7c32bd05 2019-06-22 stsp
4239 7c32bd05 2019-06-22 stsp static int
4240 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4241 7c32bd05 2019-06-22 stsp {
4242 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
4243 7c32bd05 2019-06-22 stsp
4244 7c32bd05 2019-06-22 stsp return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4245 7c32bd05 2019-06-22 stsp }
4246 7c32bd05 2019-06-22 stsp
4247 7c32bd05 2019-06-22 stsp static const struct got_error *
4248 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
4249 7c32bd05 2019-06-22 stsp {
4250 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4251 a0de39f3 2019-08-09 stsp struct got_tree_entry *entry = NULL, *te;
4252 7c32bd05 2019-06-22 stsp
4253 7c32bd05 2019-06-22 stsp if (!view->searching) {
4254 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4255 7c32bd05 2019-06-22 stsp return NULL;
4256 7c32bd05 2019-06-22 stsp }
4257 7c32bd05 2019-06-22 stsp
4258 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4259 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
4260 7c32bd05 2019-06-22 stsp if (s->selected_entry)
4261 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4262 7c32bd05 2019-06-22 stsp else
4263 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4264 7c32bd05 2019-06-22 stsp }
4265 7c32bd05 2019-06-22 stsp else {
4266 7c32bd05 2019-06-22 stsp if (s->selected_entry == NULL) {
4267 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4268 7c32bd05 2019-06-22 stsp entry = te;
4269 7c32bd05 2019-06-22 stsp } else {
4270 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4271 7c32bd05 2019-06-22 stsp entry = te;
4272 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) ==
4273 7c32bd05 2019-06-22 stsp s->selected_entry)
4274 7c32bd05 2019-06-22 stsp break;
4275 7c32bd05 2019-06-22 stsp }
4276 7c32bd05 2019-06-22 stsp }
4277 7c32bd05 2019-06-22 stsp }
4278 7c32bd05 2019-06-22 stsp } else {
4279 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4280 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4281 7c32bd05 2019-06-22 stsp else {
4282 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4283 7c32bd05 2019-06-22 stsp entry = te;
4284 7c32bd05 2019-06-22 stsp }
4285 7c32bd05 2019-06-22 stsp }
4286 7c32bd05 2019-06-22 stsp
4287 7c32bd05 2019-06-22 stsp while (1) {
4288 7c32bd05 2019-06-22 stsp if (entry == NULL) {
4289 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
4290 ac66afb8 2019-06-24 stsp view->search_next_done = 1;
4291 ac66afb8 2019-06-24 stsp return NULL;
4292 ac66afb8 2019-06-24 stsp }
4293 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4294 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4295 7c32bd05 2019-06-22 stsp else {
4296 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4297 7c32bd05 2019-06-22 stsp entry = te;
4298 7c32bd05 2019-06-22 stsp }
4299 7c32bd05 2019-06-22 stsp }
4300 7c32bd05 2019-06-22 stsp
4301 7c32bd05 2019-06-22 stsp if (match_tree_entry(entry, &view->regex)) {
4302 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4303 7c32bd05 2019-06-22 stsp s->matched_entry = entry;
4304 7c32bd05 2019-06-22 stsp break;
4305 7c32bd05 2019-06-22 stsp }
4306 7c32bd05 2019-06-22 stsp
4307 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4308 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(entry, entry);
4309 7c32bd05 2019-06-22 stsp else {
4310 7c32bd05 2019-06-22 stsp if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4311 7c32bd05 2019-06-22 stsp entry = NULL;
4312 7c32bd05 2019-06-22 stsp else {
4313 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4314 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) == entry) {
4315 7c32bd05 2019-06-22 stsp entry = te;
4316 7c32bd05 2019-06-22 stsp break;
4317 7c32bd05 2019-06-22 stsp }
4318 7c32bd05 2019-06-22 stsp }
4319 7c32bd05 2019-06-22 stsp }
4320 7c32bd05 2019-06-22 stsp }
4321 7c32bd05 2019-06-22 stsp }
4322 e5a0f69f 2018-08-18 stsp
4323 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4324 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
4325 7c32bd05 2019-06-22 stsp s->selected = 0;
4326 7c32bd05 2019-06-22 stsp }
4327 7c32bd05 2019-06-22 stsp
4328 e5a0f69f 2018-08-18 stsp return NULL;
4329 ad80ab7b 2018-08-04 stsp }
4330 ad80ab7b 2018-08-04 stsp
4331 ad80ab7b 2018-08-04 stsp static const struct got_error *
4332 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
4333 ad80ab7b 2018-08-04 stsp {
4334 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
4335 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4336 e5a0f69f 2018-08-18 stsp char *parent_path;
4337 ad80ab7b 2018-08-04 stsp
4338 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
4339 e5a0f69f 2018-08-18 stsp if (err)
4340 e5a0f69f 2018-08-18 stsp return err;
4341 ffd1d5e5 2018-06-23 stsp
4342 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
4343 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
4344 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4345 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
4346 e5a0f69f 2018-08-18 stsp free(parent_path);
4347 669b5ffa 2018-10-07 stsp
4348 669b5ffa 2018-10-07 stsp view_vborder(view);
4349 e5a0f69f 2018-08-18 stsp return err;
4350 e5a0f69f 2018-08-18 stsp }
4351 ce52c690 2018-06-23 stsp
4352 e5a0f69f 2018-08-18 stsp static const struct got_error *
4353 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4354 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4355 e5a0f69f 2018-08-18 stsp {
4356 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4357 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
4358 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
4359 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
4360 ffd1d5e5 2018-06-23 stsp
4361 e5a0f69f 2018-08-18 stsp switch (ch) {
4362 1e37a5c2 2019-05-12 jcs case 'i':
4363 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
4364 1e37a5c2 2019-05-12 jcs break;
4365 1e37a5c2 2019-05-12 jcs case 'l':
4366 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
4367 ffd1d5e5 2018-06-23 stsp break;
4368 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4369 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4370 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
4371 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
4372 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
4373 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4374 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4375 1e37a5c2 2019-05-12 jcs if (err)
4376 1e37a5c2 2019-05-12 jcs return err;
4377 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
4378 1e37a5c2 2019-05-12 jcs if (err) {
4379 1e37a5c2 2019-05-12 jcs view_close(log_view);
4380 669b5ffa 2018-10-07 stsp break;
4381 1e37a5c2 2019-05-12 jcs }
4382 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
4383 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4384 1e37a5c2 2019-05-12 jcs } else
4385 1e37a5c2 2019-05-12 jcs *new_view = log_view;
4386 1e37a5c2 2019-05-12 jcs break;
4387 1e37a5c2 2019-05-12 jcs case 'k':
4388 1e37a5c2 2019-05-12 jcs case KEY_UP:
4389 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
4390 1e37a5c2 2019-05-12 jcs s->selected--;
4391 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
4392 1e37a5c2 2019-05-12 jcs break;
4393 1e37a5c2 2019-05-12 jcs }
4394 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
4395 1e37a5c2 2019-05-12 jcs break;
4396 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
4397 1e37a5c2 2019-05-12 jcs s->entries, s->tree == s->root);
4398 1e37a5c2 2019-05-12 jcs break;
4399 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4400 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
4401 1e37a5c2 2019-05-12 jcs MAX(0, view->nlines - 4 - s->selected), s->entries,
4402 1e37a5c2 2019-05-12 jcs s->tree == s->root);
4403 1e37a5c2 2019-05-12 jcs s->selected = 0;
4404 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_FIRST(&s->entries->head) ==
4405 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
4406 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
4407 1e37a5c2 2019-05-12 jcs break;
4408 1e37a5c2 2019-05-12 jcs case 'j':
4409 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4410 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
4411 1e37a5c2 2019-05-12 jcs s->selected++;
4412 1e37a5c2 2019-05-12 jcs break;
4413 1e37a5c2 2019-05-12 jcs }
4414 00ba99a7 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4415 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
4416 1e37a5c2 2019-05-12 jcs break;
4417 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
4418 1e37a5c2 2019-05-12 jcs s->last_displayed_entry, s->entries);
4419 1e37a5c2 2019-05-12 jcs break;
4420 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4421 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4422 1e37a5c2 2019-05-12 jcs == NULL) {
4423 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
4424 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
4425 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4426 1e37a5c2 2019-05-12 jcs break;
4427 1e37a5c2 2019-05-12 jcs }
4428 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
4429 1e37a5c2 2019-05-12 jcs view->nlines, s->last_displayed_entry, s->entries);
4430 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
4431 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
4432 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
4433 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
4434 1e37a5c2 2019-05-12 jcs do {
4435 1e37a5c2 2019-05-12 jcs ndisplayed++;
4436 1e37a5c2 2019-05-12 jcs te = SIMPLEQ_NEXT(te, entry);
4437 1e37a5c2 2019-05-12 jcs } while (te);
4438 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
4439 1e37a5c2 2019-05-12 jcs }
4440 1e37a5c2 2019-05-12 jcs break;
4441 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4442 1e37a5c2 2019-05-12 jcs case '\r':
4443 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4444 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4445 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
4446 1e37a5c2 2019-05-12 jcs /* user selected '..' */
4447 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
4448 1e37a5c2 2019-05-12 jcs break;
4449 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
4450 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
4451 1e37a5c2 2019-05-12 jcs entry);
4452 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
4453 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
4454 1e37a5c2 2019-05-12 jcs s->entries =
4455 1e37a5c2 2019-05-12 jcs got_object_tree_get_entries(s->tree);
4456 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
4457 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
4458 1e37a5c2 2019-05-12 jcs s->selected_entry =
4459 1e37a5c2 2019-05-12 jcs parent->selected_entry;
4460 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
4461 1e37a5c2 2019-05-12 jcs free(parent);
4462 1e37a5c2 2019-05-12 jcs } else if (S_ISDIR(s->selected_entry->mode)) {
4463 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
4464 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&subtree,
4465 1e37a5c2 2019-05-12 jcs s->repo, s->selected_entry->id);
4466 1e37a5c2 2019-05-12 jcs if (err)
4467 1e37a5c2 2019-05-12 jcs break;
4468 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
4469 941e9f74 2019-05-21 stsp if (err) {
4470 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
4471 1e37a5c2 2019-05-12 jcs break;
4472 1e37a5c2 2019-05-12 jcs }
4473 1e37a5c2 2019-05-12 jcs } else if (S_ISREG(s->selected_entry->mode)) {
4474 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
4475 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
4476 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
4477 1e37a5c2 2019-05-12 jcs
4478 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
4479 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
4480 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
4481 1e37a5c2 2019-05-12 jcs if (err)
4482 1e37a5c2 2019-05-12 jcs break;
4483 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
4484 669b5ffa 2018-10-07 stsp err = view_close_child(view);
4485 669b5ffa 2018-10-07 stsp if (err)
4486 669b5ffa 2018-10-07 stsp return err;
4487 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
4488 669b5ffa 2018-10-07 stsp if (err) {
4489 1e37a5c2 2019-05-12 jcs view_close(blame_view);
4490 669b5ffa 2018-10-07 stsp break;
4491 669b5ffa 2018-10-07 stsp }
4492 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
4493 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
4494 669b5ffa 2018-10-07 stsp } else
4495 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
4496 1e37a5c2 2019-05-12 jcs }
4497 1e37a5c2 2019-05-12 jcs break;
4498 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4499 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
4500 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4501 1e37a5c2 2019-05-12 jcs break;
4502 1e37a5c2 2019-05-12 jcs default:
4503 1e37a5c2 2019-05-12 jcs break;
4504 ffd1d5e5 2018-06-23 stsp }
4505 e5a0f69f 2018-08-18 stsp
4506 ffd1d5e5 2018-06-23 stsp return err;
4507 9f7d7167 2018-04-29 stsp }
4508 9f7d7167 2018-04-29 stsp
4509 ffd1d5e5 2018-06-23 stsp __dead static void
4510 ffd1d5e5 2018-06-23 stsp usage_tree(void)
4511 ffd1d5e5 2018-06-23 stsp {
4512 ffd1d5e5 2018-06-23 stsp endwin();
4513 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4514 ffd1d5e5 2018-06-23 stsp getprogname());
4515 ffd1d5e5 2018-06-23 stsp exit(1);
4516 ffd1d5e5 2018-06-23 stsp }
4517 ffd1d5e5 2018-06-23 stsp
4518 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4519 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
4520 ffd1d5e5 2018-06-23 stsp {
4521 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
4522 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
4523 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4524 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
4525 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4526 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
4527 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
4528 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
4529 ffd1d5e5 2018-06-23 stsp int ch;
4530 5221c383 2018-08-01 stsp struct tog_view *view;
4531 70ac5f84 2019-03-28 stsp
4532 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4533 ffd1d5e5 2018-06-23 stsp
4534 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
4535 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4536 d188b9a6 2019-01-04 stsp NULL) == -1)
4537 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
4538 ffd1d5e5 2018-06-23 stsp #endif
4539 ffd1d5e5 2018-06-23 stsp
4540 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
4541 ffd1d5e5 2018-06-23 stsp switch (ch) {
4542 ffd1d5e5 2018-06-23 stsp case 'c':
4543 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
4544 ffd1d5e5 2018-06-23 stsp break;
4545 ffd1d5e5 2018-06-23 stsp default:
4546 17020d27 2019-03-07 stsp usage_tree();
4547 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
4548 ffd1d5e5 2018-06-23 stsp }
4549 ffd1d5e5 2018-06-23 stsp }
4550 ffd1d5e5 2018-06-23 stsp
4551 ffd1d5e5 2018-06-23 stsp argc -= optind;
4552 ffd1d5e5 2018-06-23 stsp argv += optind;
4553 ffd1d5e5 2018-06-23 stsp
4554 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
4555 52185f70 2019-02-05 stsp struct got_worktree *worktree;
4556 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
4557 52185f70 2019-02-05 stsp if (cwd == NULL)
4558 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
4559 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4560 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4561 52185f70 2019-02-05 stsp goto done;
4562 52185f70 2019-02-05 stsp if (worktree) {
4563 52185f70 2019-02-05 stsp free(cwd);
4564 52185f70 2019-02-05 stsp repo_path =
4565 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4566 52185f70 2019-02-05 stsp got_worktree_close(worktree);
4567 52185f70 2019-02-05 stsp } else
4568 52185f70 2019-02-05 stsp repo_path = cwd;
4569 52185f70 2019-02-05 stsp if (repo_path == NULL) {
4570 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4571 52185f70 2019-02-05 stsp goto done;
4572 52185f70 2019-02-05 stsp }
4573 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
4574 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
4575 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
4576 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
4577 ffd1d5e5 2018-06-23 stsp } else
4578 ffd1d5e5 2018-06-23 stsp usage_log();
4579 a915003a 2019-02-05 stsp
4580 a915003a 2019-02-05 stsp init_curses();
4581 ffd1d5e5 2018-06-23 stsp
4582 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
4583 c02c541e 2019-03-29 stsp if (error != NULL)
4584 52185f70 2019-02-05 stsp goto done;
4585 d188b9a6 2019-01-04 stsp
4586 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4587 c02c541e 2019-03-29 stsp if (error)
4588 52185f70 2019-02-05 stsp goto done;
4589 ffd1d5e5 2018-06-23 stsp
4590 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
4591 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4592 f2b6a97d 2019-07-15 stsp else {
4593 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4594 f2b6a97d 2019-07-15 stsp if (error) {
4595 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
4596 f2b6a97d 2019-07-15 stsp goto done;
4597 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&commit_id,
4598 f2b6a97d 2019-07-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4599 f2b6a97d 2019-07-15 stsp }
4600 f2b6a97d 2019-07-15 stsp }
4601 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4602 ffd1d5e5 2018-06-23 stsp goto done;
4603 ffd1d5e5 2018-06-23 stsp
4604 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4605 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4606 ffd1d5e5 2018-06-23 stsp goto done;
4607 ffd1d5e5 2018-06-23 stsp
4608 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
4609 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
4610 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4611 8b473291 2019-02-21 stsp goto done;
4612 8b473291 2019-02-21 stsp
4613 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4614 8b473291 2019-02-21 stsp if (error)
4615 ffd1d5e5 2018-06-23 stsp goto done;
4616 ffd1d5e5 2018-06-23 stsp
4617 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4618 5221c383 2018-08-01 stsp if (view == NULL) {
4619 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4620 5221c383 2018-08-01 stsp goto done;
4621 5221c383 2018-08-01 stsp }
4622 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
4623 ad80ab7b 2018-08-04 stsp if (error)
4624 ad80ab7b 2018-08-04 stsp goto done;
4625 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4626 ffd1d5e5 2018-06-23 stsp done:
4627 52185f70 2019-02-05 stsp free(repo_path);
4628 ffd1d5e5 2018-06-23 stsp free(commit_id);
4629 ffd1d5e5 2018-06-23 stsp if (commit)
4630 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
4631 ffd1d5e5 2018-06-23 stsp if (tree)
4632 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
4633 ffd1d5e5 2018-06-23 stsp if (repo)
4634 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
4635 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4636 ffd1d5e5 2018-06-23 stsp return error;
4637 9f7d7167 2018-04-29 stsp }
4638 ce5b7c56 2019-07-09 stsp
4639 ce5b7c56 2019-07-09 stsp static void
4640 ce5b7c56 2019-07-09 stsp list_commands(void)
4641 ce5b7c56 2019-07-09 stsp {
4642 ce5b7c56 2019-07-09 stsp int i;
4643 9f7d7167 2018-04-29 stsp
4644 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
4645 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
4646 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
4647 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->name);
4648 ce5b7c56 2019-07-09 stsp }
4649 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
4650 ce5b7c56 2019-07-09 stsp }
4651 ce5b7c56 2019-07-09 stsp
4652 4ed7e80c 2018-05-20 stsp __dead static void
4653 ce5b7c56 2019-07-09 stsp usage(int hflag)
4654 9f7d7167 2018-04-29 stsp {
4655 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4656 53ccebc2 2019-07-30 stsp getprogname());
4657 ce5b7c56 2019-07-09 stsp if (hflag)
4658 ce5b7c56 2019-07-09 stsp list_commands();
4659 9f7d7167 2018-04-29 stsp exit(1);
4660 9f7d7167 2018-04-29 stsp }
4661 9f7d7167 2018-04-29 stsp
4662 c2301be8 2018-04-30 stsp static char **
4663 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
4664 c2301be8 2018-04-30 stsp {
4665 c2301be8 2018-04-30 stsp char **argv;
4666 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
4667 c2301be8 2018-04-30 stsp
4668 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
4669 c2301be8 2018-04-30 stsp if (argv == NULL)
4670 c2301be8 2018-04-30 stsp err(1, "calloc");
4671 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
4672 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
4673 c2301be8 2018-04-30 stsp err(1, "calloc");
4674 c2301be8 2018-04-30 stsp if (arg1) {
4675 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
4676 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
4677 c2301be8 2018-04-30 stsp err(1, "calloc");
4678 c2301be8 2018-04-30 stsp }
4679 c2301be8 2018-04-30 stsp
4680 c2301be8 2018-04-30 stsp return argv;
4681 c2301be8 2018-04-30 stsp }
4682 c2301be8 2018-04-30 stsp
4683 9f7d7167 2018-04-29 stsp int
4684 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
4685 9f7d7167 2018-04-29 stsp {
4686 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
4687 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
4688 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
4689 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
4690 9f7d7167 2018-04-29 stsp
4691 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
4692 9f7d7167 2018-04-29 stsp
4693 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
4694 9f7d7167 2018-04-29 stsp switch (ch) {
4695 9f7d7167 2018-04-29 stsp case 'h':
4696 9f7d7167 2018-04-29 stsp hflag = 1;
4697 9f7d7167 2018-04-29 stsp break;
4698 53ccebc2 2019-07-30 stsp case 'V':
4699 53ccebc2 2019-07-30 stsp Vflag = 1;
4700 53ccebc2 2019-07-30 stsp break;
4701 9f7d7167 2018-04-29 stsp default:
4702 ce5b7c56 2019-07-09 stsp usage(hflag);
4703 9f7d7167 2018-04-29 stsp /* NOTREACHED */
4704 9f7d7167 2018-04-29 stsp }
4705 9f7d7167 2018-04-29 stsp }
4706 9f7d7167 2018-04-29 stsp
4707 9f7d7167 2018-04-29 stsp argc -= optind;
4708 9f7d7167 2018-04-29 stsp argv += optind;
4709 9f7d7167 2018-04-29 stsp optind = 0;
4710 c2301be8 2018-04-30 stsp optreset = 1;
4711 9f7d7167 2018-04-29 stsp
4712 53ccebc2 2019-07-30 stsp if (Vflag) {
4713 53ccebc2 2019-07-30 stsp got_version_print_str();
4714 53ccebc2 2019-07-30 stsp return 1;
4715 53ccebc2 2019-07-30 stsp }
4716 53ccebc2 2019-07-30 stsp
4717 c2301be8 2018-04-30 stsp if (argc == 0) {
4718 f29d3e89 2018-06-23 stsp if (hflag)
4719 ce5b7c56 2019-07-09 stsp usage(hflag);
4720 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
4721 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
4722 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
4723 c2301be8 2018-04-30 stsp argc = 1;
4724 c2301be8 2018-04-30 stsp } else {
4725 9f7d7167 2018-04-29 stsp int i;
4726 9f7d7167 2018-04-29 stsp
4727 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
4728 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
4729 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
4730 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
4731 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
4732 9f7d7167 2018-04-29 stsp break;
4733 9f7d7167 2018-04-29 stsp }
4734 9f7d7167 2018-04-29 stsp }
4735 3642c4c6 2019-07-09 stsp
4736 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
4737 d70c3147 2019-07-09 stsp fprintf(stderr, "%s: unknown command '%s'\n",
4738 3642c4c6 2019-07-09 stsp getprogname(), argv[0]);
4739 ce5b7c56 2019-07-09 stsp list_commands();
4740 3642c4c6 2019-07-09 stsp return 1;
4741 9f7d7167 2018-04-29 stsp }
4742 9f7d7167 2018-04-29 stsp }
4743 9f7d7167 2018-04-29 stsp
4744 3642c4c6 2019-07-09 stsp if (hflag)
4745 3642c4c6 2019-07-09 stsp cmd->cmd_usage();
4746 3642c4c6 2019-07-09 stsp else
4747 3642c4c6 2019-07-09 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4748 3642c4c6 2019-07-09 stsp
4749 9f7d7167 2018-04-29 stsp endwin();
4750 c2301be8 2018-04-30 stsp free(cmd_argv);
4751 fc06ba56 2019-08-22 stsp if (error && error->code != GOT_ERR_CANCELLED)
4752 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4753 9f7d7167 2018-04-29 stsp return 0;
4754 9f7d7167 2018-04-29 stsp }