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