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