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 60493ae3 2019-06-20 stsp match_commit(struct got_commit_object *commit, regex_t *regex)
1727 60493ae3 2019-06-20 stsp {
1728 60493ae3 2019-06-20 stsp regmatch_t regmatch;
1729 60493ae3 2019-06-20 stsp
1730 60493ae3 2019-06-20 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1731 60493ae3 2019-06-20 stsp &regmatch, 0) == 0 ||
1732 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1733 60493ae3 2019-06-20 stsp &regmatch, 0) == 0 ||
1734 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_logmsg(commit), 1,
1735 60493ae3 2019-06-20 stsp &regmatch, 0) == 0)
1736 60493ae3 2019-06-20 stsp return 1;
1737 60493ae3 2019-06-20 stsp
1738 60493ae3 2019-06-20 stsp return 0;
1739 60493ae3 2019-06-20 stsp }
1740 60493ae3 2019-06-20 stsp
1741 60493ae3 2019-06-20 stsp static const struct got_error *
1742 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
1743 60493ae3 2019-06-20 stsp {
1744 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
1745 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1746 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
1747 60493ae3 2019-06-20 stsp
1748 60493ae3 2019-06-20 stsp if (!view->searching) {
1749 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1750 60493ae3 2019-06-20 stsp return NULL;
1751 60493ae3 2019-06-20 stsp }
1752 60493ae3 2019-06-20 stsp
1753 b1bf1435 2019-06-21 stsp if (s->matched_entry) {
1754 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1755 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
1756 b1bf1435 2019-06-21 stsp else
1757 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
1758 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
1759 20be8d96 2019-06-21 stsp } else {
1760 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1761 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
1762 20be8d96 2019-06-21 stsp else
1763 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1764 20be8d96 2019-06-21 stsp }
1765 60493ae3 2019-06-20 stsp
1766 60493ae3 2019-06-20 stsp while (1) {
1767 60493ae3 2019-06-20 stsp if (entry == NULL) {
1768 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
1769 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
1770 f801134a 2019-06-25 stsp view->search_next_done = 1;
1771 f801134a 2019-06-25 stsp return NULL;
1772 60493ae3 2019-06-20 stsp }
1773 f801134a 2019-06-25 stsp s->thread_args.commits_needed = 1;
1774 f801134a 2019-06-25 stsp return trigger_log_thread(0,
1775 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
1776 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
1777 f801134a 2019-06-25 stsp &s->thread_args.need_commits);
1778 60493ae3 2019-06-20 stsp }
1779 60493ae3 2019-06-20 stsp
1780 1803e47f 2019-06-22 stsp if (match_commit(entry->commit, &view->regex)) {
1781 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1782 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
1783 60493ae3 2019-06-20 stsp break;
1784 60493ae3 2019-06-20 stsp }
1785 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1786 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
1787 b1bf1435 2019-06-21 stsp else
1788 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1789 60493ae3 2019-06-20 stsp }
1790 60493ae3 2019-06-20 stsp
1791 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
1792 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
1793 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
1794 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1795 60493ae3 2019-06-20 stsp if (err)
1796 60493ae3 2019-06-20 stsp return err;
1797 ead14cbe 2019-06-21 stsp cur++;
1798 ead14cbe 2019-06-21 stsp }
1799 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
1800 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1801 60493ae3 2019-06-20 stsp if (err)
1802 60493ae3 2019-06-20 stsp return err;
1803 ead14cbe 2019-06-21 stsp cur--;
1804 60493ae3 2019-06-20 stsp }
1805 60493ae3 2019-06-20 stsp }
1806 60493ae3 2019-06-20 stsp
1807 60493ae3 2019-06-20 stsp return NULL;
1808 60493ae3 2019-06-20 stsp }
1809 60493ae3 2019-06-20 stsp
1810 60493ae3 2019-06-20 stsp static const struct got_error *
1811 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1812 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
1813 d01904d4 2019-06-25 stsp const char *head_ref_name, const char *path, int check_disk)
1814 80ddbec8 2018-04-29 stsp {
1815 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1816 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1817 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1818 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1819 1a76625f 2018-10-22 stsp int errcode;
1820 80ddbec8 2018-04-29 stsp
1821 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1822 ecb28ae0 2018-07-16 stsp if (err != NULL)
1823 ecb28ae0 2018-07-16 stsp goto done;
1824 ecb28ae0 2018-07-16 stsp
1825 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1826 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1827 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1828 9ba79e04 2018-06-11 stsp
1829 8b473291 2019-02-21 stsp s->refs = refs;
1830 fb2756b9 2018-08-04 stsp s->repo = repo;
1831 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
1832 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1833 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1834 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1835 5036bf37 2018-09-24 stsp goto done;
1836 5036bf37 2018-09-24 stsp }
1837 e5a0f69f 2018-08-18 stsp
1838 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1839 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1840 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1841 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
1842 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
1843 1a76625f 2018-10-22 stsp
1844 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1845 1a76625f 2018-10-22 stsp if (err)
1846 1a76625f 2018-10-22 stsp goto done;
1847 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1848 1a76625f 2018-10-22 stsp 0, thread_repo);
1849 1a76625f 2018-10-22 stsp if (err)
1850 1a76625f 2018-10-22 stsp goto done;
1851 1a76625f 2018-10-22 stsp
1852 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1853 1a76625f 2018-10-22 stsp if (errcode) {
1854 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
1855 1a76625f 2018-10-22 stsp goto done;
1856 1a76625f 2018-10-22 stsp }
1857 1a76625f 2018-10-22 stsp
1858 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1859 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1860 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1861 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1862 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1863 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1864 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1865 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1866 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1867 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1868 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1869 ba4f502b 2018-08-04 stsp done:
1870 1a76625f 2018-10-22 stsp if (err)
1871 1a76625f 2018-10-22 stsp close_log_view(view);
1872 ba4f502b 2018-08-04 stsp return err;
1873 ba4f502b 2018-08-04 stsp }
1874 ba4f502b 2018-08-04 stsp
1875 e5a0f69f 2018-08-18 stsp static const struct got_error *
1876 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1877 ba4f502b 2018-08-04 stsp {
1878 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1879 ba4f502b 2018-08-04 stsp
1880 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1881 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1882 2b380cc8 2018-10-24 stsp &s->thread_args);
1883 2b380cc8 2018-10-24 stsp if (errcode)
1884 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
1885 2b380cc8 2018-10-24 stsp }
1886 2b380cc8 2018-10-24 stsp
1887 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1888 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1889 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
1890 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1891 e5a0f69f 2018-08-18 stsp }
1892 04cc582a 2018-08-01 stsp
1893 e5a0f69f 2018-08-18 stsp static const struct got_error *
1894 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1895 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1896 e5a0f69f 2018-08-18 stsp {
1897 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1898 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1899 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
1900 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1901 669b5ffa 2018-10-07 stsp int begin_x = 0;
1902 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
1903 80ddbec8 2018-04-29 stsp
1904 e5a0f69f 2018-08-18 stsp switch (ch) {
1905 1e37a5c2 2019-05-12 jcs case 'q':
1906 1e37a5c2 2019-05-12 jcs s->quit = 1;
1907 1e37a5c2 2019-05-12 jcs break;
1908 1e37a5c2 2019-05-12 jcs case 'k':
1909 1e37a5c2 2019-05-12 jcs case KEY_UP:
1910 1e37a5c2 2019-05-12 jcs case '<':
1911 1e37a5c2 2019-05-12 jcs case ',':
1912 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1913 1a76625f 2018-10-22 stsp break;
1914 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
1915 1e37a5c2 2019-05-12 jcs s->selected--;
1916 1144d21a 2019-06-21 stsp else
1917 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
1918 1144d21a 2019-06-21 stsp &s->commits);
1919 1e37a5c2 2019-05-12 jcs break;
1920 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
1921 a4292ac5 2019-05-12 jcs case CTRL('b'):
1922 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1923 e5a0f69f 2018-08-18 stsp break;
1924 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
1925 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
1926 1e37a5c2 2019-05-12 jcs s->selected = 0;
1927 e5a0f69f 2018-08-18 stsp break;
1928 1e37a5c2 2019-05-12 jcs }
1929 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
1930 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
1931 1e37a5c2 2019-05-12 jcs break;
1932 1e37a5c2 2019-05-12 jcs case 'j':
1933 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
1934 1e37a5c2 2019-05-12 jcs case '>':
1935 1e37a5c2 2019-05-12 jcs case '.':
1936 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1937 e5a0f69f 2018-08-18 stsp break;
1938 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
1939 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1940 1e37a5c2 2019-05-12 jcs s->selected++;
1941 1e37a5c2 2019-05-12 jcs break;
1942 80ddbec8 2018-04-29 stsp }
1943 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
1944 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
1945 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
1946 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1947 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1948 1e37a5c2 2019-05-12 jcs break;
1949 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
1950 a4292ac5 2019-05-12 jcs case CTRL('f'): {
1951 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
1952 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
1953 1e37a5c2 2019-05-12 jcs if (first == NULL)
1954 e5a0f69f 2018-08-18 stsp break;
1955 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
1956 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
1957 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
1958 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1959 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1960 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
1961 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
1962 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1963 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
1964 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
1965 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
1966 1e37a5c2 2019-05-12 jcs }
1967 1e37a5c2 2019-05-12 jcs err = NULL;
1968 1e37a5c2 2019-05-12 jcs break;
1969 1e37a5c2 2019-05-12 jcs }
1970 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
1971 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
1972 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
1973 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
1974 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
1975 1e37a5c2 2019-05-12 jcs break;
1976 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
1977 87c7274c 2019-05-12 jcs case ' ':
1978 1e37a5c2 2019-05-12 jcs case '\r':
1979 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
1980 e5a0f69f 2018-08-18 stsp break;
1981 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
1982 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
1983 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
1984 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
1985 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
1986 1e37a5c2 2019-05-12 jcs if (err)
1987 1e37a5c2 2019-05-12 jcs break;
1988 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
1989 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
1990 f7013a22 2018-10-24 stsp if (err)
1991 1e37a5c2 2019-05-12 jcs return err;
1992 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
1993 1e37a5c2 2019-05-12 jcs if (err) {
1994 1e37a5c2 2019-05-12 jcs view_close(diff_view);
1995 f7013a22 2018-10-24 stsp break;
1996 5036bf37 2018-09-24 stsp }
1997 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
1998 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
1999 1e37a5c2 2019-05-12 jcs } else
2000 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2001 1e37a5c2 2019-05-12 jcs break;
2002 1e37a5c2 2019-05-12 jcs case 't':
2003 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2004 5036bf37 2018-09-24 stsp break;
2005 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2006 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2007 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2008 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2009 1e37a5c2 2019-05-12 jcs if (err)
2010 e5a0f69f 2018-08-18 stsp break;
2011 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2012 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2013 1e37a5c2 2019-05-12 jcs if (err)
2014 1e37a5c2 2019-05-12 jcs return err;
2015 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2016 1e37a5c2 2019-05-12 jcs if (err) {
2017 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2018 1e37a5c2 2019-05-12 jcs break;
2019 1e37a5c2 2019-05-12 jcs }
2020 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2021 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2022 1e37a5c2 2019-05-12 jcs } else
2023 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2024 1e37a5c2 2019-05-12 jcs break;
2025 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2026 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2027 1e37a5c2 2019-05-12 jcs break;
2028 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2029 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2030 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2031 1e37a5c2 2019-05-12 jcs if (err)
2032 1e37a5c2 2019-05-12 jcs return err;
2033 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2034 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2035 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2036 638f9024 2019-05-13 stsp return got_error_from_errno(
2037 1e37a5c2 2019-05-12 jcs "view_open");
2038 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2039 d01904d4 2019-06-25 stsp s->repo, s->head_ref_name, parent_path, 0);
2040 1e37a5c2 2019-05-12 jcs if (err)
2041 1e37a5c2 2019-05-12 jcs return err;;
2042 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2043 1e37a5c2 2019-05-12 jcs *new_view = lv;
2044 1e37a5c2 2019-05-12 jcs else {
2045 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2046 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2047 1e37a5c2 2019-05-12 jcs }
2048 1e37a5c2 2019-05-12 jcs return NULL;
2049 1e37a5c2 2019-05-12 jcs }
2050 1e37a5c2 2019-05-12 jcs break;
2051 d01904d4 2019-06-25 stsp case 'r':
2052 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2053 d01904d4 2019-06-25 stsp if (err)
2054 d01904d4 2019-06-25 stsp return err;
2055 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2056 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2057 d01904d4 2019-06-25 stsp if (lv == NULL)
2058 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2059 d01904d4 2019-06-25 stsp err = get_head_commit_id(&start_id, s->head_ref_name ?
2060 d01904d4 2019-06-25 stsp s->head_ref_name : GOT_REF_HEAD, s->repo);
2061 d01904d4 2019-06-25 stsp if (err)
2062 d01904d4 2019-06-25 stsp return err;
2063 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2064 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2065 d01904d4 2019-06-25 stsp free(start_id);
2066 d01904d4 2019-06-25 stsp return got_error_from_errno("strdup");
2067 d01904d4 2019-06-25 stsp }
2068 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2069 d01904d4 2019-06-25 stsp s->head_ref_name, in_repo_path, 0);
2070 d01904d4 2019-06-25 stsp if (err)
2071 d01904d4 2019-06-25 stsp return err;;
2072 d01904d4 2019-06-25 stsp *dead_view = view;
2073 d01904d4 2019-06-25 stsp *new_view = lv;
2074 d01904d4 2019-06-25 stsp break;
2075 1e37a5c2 2019-05-12 jcs default:
2076 1e37a5c2 2019-05-12 jcs break;
2077 899d86c2 2018-05-10 stsp }
2078 e5a0f69f 2018-08-18 stsp
2079 80ddbec8 2018-04-29 stsp return err;
2080 80ddbec8 2018-04-29 stsp }
2081 80ddbec8 2018-04-29 stsp
2082 4ed7e80c 2018-05-20 stsp static const struct got_error *
2083 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2084 c2db6724 2019-01-04 stsp {
2085 c2db6724 2019-01-04 stsp const struct got_error *error;
2086 c2db6724 2019-01-04 stsp
2087 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2088 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2089 c2db6724 2019-01-04 stsp
2090 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2091 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2092 c2db6724 2019-01-04 stsp
2093 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2094 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2095 c2db6724 2019-01-04 stsp
2096 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2097 c2db6724 2019-01-04 stsp if (error != NULL)
2098 c2db6724 2019-01-04 stsp return error;
2099 c2db6724 2019-01-04 stsp
2100 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2101 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2102 c2db6724 2019-01-04 stsp
2103 c2db6724 2019-01-04 stsp return NULL;
2104 c2db6724 2019-01-04 stsp }
2105 c2db6724 2019-01-04 stsp
2106 a915003a 2019-02-05 stsp static void
2107 a915003a 2019-02-05 stsp init_curses(void)
2108 a915003a 2019-02-05 stsp {
2109 a915003a 2019-02-05 stsp initscr();
2110 a915003a 2019-02-05 stsp cbreak();
2111 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2112 a915003a 2019-02-05 stsp noecho();
2113 a915003a 2019-02-05 stsp nonl();
2114 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2115 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2116 a915003a 2019-02-05 stsp curs_set(0);
2117 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2118 a915003a 2019-02-05 stsp }
2119 a915003a 2019-02-05 stsp
2120 c2db6724 2019-01-04 stsp static const struct got_error *
2121 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2122 9f7d7167 2018-04-29 stsp {
2123 80ddbec8 2018-04-29 stsp const struct got_error *error;
2124 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2125 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2126 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2127 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2128 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2129 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
2130 80ddbec8 2018-04-29 stsp int ch;
2131 04cc582a 2018-08-01 stsp struct tog_view *view;
2132 a55555f2 2019-03-28 stsp
2133 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2134 80ddbec8 2018-04-29 stsp
2135 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2136 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2137 c2db6724 2019-01-04 stsp NULL) == -1)
2138 80ddbec8 2018-04-29 stsp err(1, "pledge");
2139 80ddbec8 2018-04-29 stsp #endif
2140 80ddbec8 2018-04-29 stsp
2141 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2142 80ddbec8 2018-04-29 stsp switch (ch) {
2143 80ddbec8 2018-04-29 stsp case 'c':
2144 80ddbec8 2018-04-29 stsp start_commit = optarg;
2145 80ddbec8 2018-04-29 stsp break;
2146 ecb28ae0 2018-07-16 stsp case 'r':
2147 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2148 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2149 ecb28ae0 2018-07-16 stsp err(1, "-r option");
2150 ecb28ae0 2018-07-16 stsp break;
2151 80ddbec8 2018-04-29 stsp default:
2152 17020d27 2019-03-07 stsp usage_log();
2153 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2154 80ddbec8 2018-04-29 stsp }
2155 80ddbec8 2018-04-29 stsp }
2156 80ddbec8 2018-04-29 stsp
2157 80ddbec8 2018-04-29 stsp argc -= optind;
2158 80ddbec8 2018-04-29 stsp argv += optind;
2159 80ddbec8 2018-04-29 stsp
2160 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2161 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2162 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2163 ecb28ae0 2018-07-16 stsp goto done;
2164 ecb28ae0 2018-07-16 stsp }
2165 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2166 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2167 963f97a1 2019-03-18 stsp goto done;
2168 963f97a1 2019-03-18 stsp error = NULL;
2169 963f97a1 2019-03-18 stsp
2170 963f97a1 2019-03-18 stsp if (argc == 0) {
2171 963f97a1 2019-03-18 stsp path = strdup("");
2172 963f97a1 2019-03-18 stsp if (path == NULL) {
2173 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2174 ecb28ae0 2018-07-16 stsp goto done;
2175 ecb28ae0 2018-07-16 stsp }
2176 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2177 963f97a1 2019-03-18 stsp if (worktree) {
2178 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2179 963f97a1 2019-03-18 stsp argv[0]);
2180 963f97a1 2019-03-18 stsp if (error)
2181 963f97a1 2019-03-18 stsp goto done;
2182 963f97a1 2019-03-18 stsp } else {
2183 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2184 963f97a1 2019-03-18 stsp if (path == NULL) {
2185 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2186 963f97a1 2019-03-18 stsp goto done;
2187 963f97a1 2019-03-18 stsp }
2188 963f97a1 2019-03-18 stsp }
2189 963f97a1 2019-03-18 stsp } else
2190 963f97a1 2019-03-18 stsp usage_log();
2191 963f97a1 2019-03-18 stsp
2192 963f97a1 2019-03-18 stsp repo_path = worktree ?
2193 963f97a1 2019-03-18 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2194 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2195 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2196 963f97a1 2019-03-18 stsp goto done;
2197 ecb28ae0 2018-07-16 stsp }
2198 c2db6724 2019-01-04 stsp
2199 a915003a 2019-02-05 stsp init_curses();
2200 ecb28ae0 2018-07-16 stsp
2201 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
2202 80ddbec8 2018-04-29 stsp if (error != NULL)
2203 ecb28ae0 2018-07-16 stsp goto done;
2204 80ddbec8 2018-04-29 stsp
2205 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2206 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2207 c02c541e 2019-03-29 stsp if (error)
2208 c02c541e 2019-03-29 stsp goto done;
2209 c02c541e 2019-03-29 stsp
2210 15a94983 2018-12-23 stsp if (start_commit == NULL)
2211 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&start_id, worktree ?
2212 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2213 19e70ad6 2019-05-14 stsp repo);
2214 15a94983 2018-12-23 stsp else
2215 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&start_id, repo,
2216 15a94983 2018-12-23 stsp start_commit);
2217 80ddbec8 2018-04-29 stsp if (error != NULL)
2218 8b473291 2019-02-21 stsp goto done;
2219 8b473291 2019-02-21 stsp
2220 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2221 8b473291 2019-02-21 stsp if (error)
2222 ecb28ae0 2018-07-16 stsp goto done;
2223 ecb28ae0 2018-07-16 stsp
2224 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2225 04cc582a 2018-08-01 stsp if (view == NULL) {
2226 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2227 04cc582a 2018-08-01 stsp goto done;
2228 04cc582a 2018-08-01 stsp }
2229 d01904d4 2019-06-25 stsp error = open_log_view(view, start_id, &refs, repo, worktree ?
2230 d01904d4 2019-06-25 stsp got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2231 ba4f502b 2018-08-04 stsp if (error)
2232 ba4f502b 2018-08-04 stsp goto done;
2233 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2234 ecb28ae0 2018-07-16 stsp done:
2235 ecb28ae0 2018-07-16 stsp free(repo_path);
2236 ecb28ae0 2018-07-16 stsp free(cwd);
2237 ecb28ae0 2018-07-16 stsp free(path);
2238 899d86c2 2018-05-10 stsp free(start_id);
2239 ecb28ae0 2018-07-16 stsp if (repo)
2240 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2241 ec142235 2019-03-07 stsp if (worktree)
2242 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2243 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2244 80ddbec8 2018-04-29 stsp return error;
2245 9f7d7167 2018-04-29 stsp }
2246 9f7d7167 2018-04-29 stsp
2247 4ed7e80c 2018-05-20 stsp __dead static void
2248 9f7d7167 2018-04-29 stsp usage_diff(void)
2249 9f7d7167 2018-04-29 stsp {
2250 80ddbec8 2018-04-29 stsp endwin();
2251 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2252 9f7d7167 2018-04-29 stsp getprogname());
2253 9f7d7167 2018-04-29 stsp exit(1);
2254 b304db33 2018-05-20 stsp }
2255 b304db33 2018-05-20 stsp
2256 b304db33 2018-05-20 stsp static char *
2257 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2258 b304db33 2018-05-20 stsp {
2259 b304db33 2018-05-20 stsp char *line;
2260 b304db33 2018-05-20 stsp size_t linelen;
2261 b304db33 2018-05-20 stsp size_t lineno;
2262 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2263 b304db33 2018-05-20 stsp
2264 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2265 b304db33 2018-05-20 stsp if (len)
2266 b304db33 2018-05-20 stsp *len = linelen;
2267 b304db33 2018-05-20 stsp return line;
2268 26ed57b2 2018-05-19 stsp }
2269 26ed57b2 2018-05-19 stsp
2270 4ed7e80c 2018-05-20 stsp static const struct got_error *
2271 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2272 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
2273 c3e9aa98 2019-05-13 jcs char *header)
2274 26ed57b2 2018-05-19 stsp {
2275 61e69b96 2018-05-20 stsp const struct got_error *err;
2276 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2277 b304db33 2018-05-20 stsp char *line;
2278 b304db33 2018-05-20 stsp size_t len;
2279 61e69b96 2018-05-20 stsp wchar_t *wline;
2280 e0b650dd 2018-05-20 stsp int width;
2281 26ed57b2 2018-05-19 stsp
2282 26ed57b2 2018-05-19 stsp rewind(f);
2283 f7d12f7e 2018-08-01 stsp werase(view->window);
2284 a3404814 2018-09-02 stsp
2285 a3404814 2018-09-02 stsp if (header) {
2286 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
2287 a3404814 2018-09-02 stsp if (err) {
2288 a3404814 2018-09-02 stsp return err;
2289 a3404814 2018-09-02 stsp }
2290 a3404814 2018-09-02 stsp
2291 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2292 a3404814 2018-09-02 stsp wstandout(view->window);
2293 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2294 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2295 a3404814 2018-09-02 stsp wstandend(view->window);
2296 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2297 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2298 26ed57b2 2018-05-19 stsp
2299 a3404814 2018-09-02 stsp if (max_lines <= 1)
2300 a3404814 2018-09-02 stsp return NULL;
2301 a3404814 2018-09-02 stsp max_lines--;
2302 a3404814 2018-09-02 stsp }
2303 a3404814 2018-09-02 stsp
2304 26ed57b2 2018-05-19 stsp *eof = 0;
2305 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2306 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2307 26ed57b2 2018-05-19 stsp if (line == NULL) {
2308 26ed57b2 2018-05-19 stsp *eof = 1;
2309 26ed57b2 2018-05-19 stsp break;
2310 26ed57b2 2018-05-19 stsp }
2311 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2312 26ed57b2 2018-05-19 stsp free(line);
2313 26ed57b2 2018-05-19 stsp continue;
2314 26ed57b2 2018-05-19 stsp }
2315 26ed57b2 2018-05-19 stsp
2316 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2317 61e69b96 2018-05-20 stsp if (err) {
2318 61e69b96 2018-05-20 stsp free(line);
2319 61e69b96 2018-05-20 stsp return err;
2320 61e69b96 2018-05-20 stsp }
2321 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2322 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2323 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2324 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2325 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2326 26ed57b2 2018-05-19 stsp free(line);
2327 2550e4c3 2018-07-13 stsp free(wline);
2328 2550e4c3 2018-07-13 stsp wline = NULL;
2329 26ed57b2 2018-05-19 stsp }
2330 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2331 26ed57b2 2018-05-19 stsp
2332 1a57306a 2018-09-02 stsp view_vborder(view);
2333 c3e9aa98 2019-05-13 jcs
2334 c3e9aa98 2019-05-13 jcs if (*eof) {
2335 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2336 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2337 c3e9aa98 2019-05-13 jcs nprinted++;
2338 c3e9aa98 2019-05-13 jcs }
2339 c3e9aa98 2019-05-13 jcs
2340 c3e9aa98 2019-05-13 jcs err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2341 c3e9aa98 2019-05-13 jcs if (err) {
2342 c3e9aa98 2019-05-13 jcs return err;
2343 c3e9aa98 2019-05-13 jcs }
2344 26ed57b2 2018-05-19 stsp
2345 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2346 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2347 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2348 c3e9aa98 2019-05-13 jcs }
2349 c3e9aa98 2019-05-13 jcs
2350 26ed57b2 2018-05-19 stsp return NULL;
2351 abd2672a 2018-12-23 stsp }
2352 abd2672a 2018-12-23 stsp
2353 abd2672a 2018-12-23 stsp static char *
2354 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2355 abd2672a 2018-12-23 stsp {
2356 abd2672a 2018-12-23 stsp char *p, *s = ctime_r(time, datebuf);
2357 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2358 abd2672a 2018-12-23 stsp if (p)
2359 abd2672a 2018-12-23 stsp *p = '\0';
2360 abd2672a 2018-12-23 stsp return s;
2361 9f7d7167 2018-04-29 stsp }
2362 9f7d7167 2018-04-29 stsp
2363 4ed7e80c 2018-05-20 stsp static const struct got_error *
2364 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2365 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2366 abd2672a 2018-12-23 stsp {
2367 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2368 abd2672a 2018-12-23 stsp char datebuf[26];
2369 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2370 15a94983 2018-12-23 stsp char *id_str = NULL;
2371 45d799e2 2018-12-23 stsp time_t committer_time;
2372 45d799e2 2018-12-23 stsp const char *author, *committer;
2373 8b473291 2019-02-21 stsp char *refs_str = NULL;
2374 abd2672a 2018-12-23 stsp
2375 8b473291 2019-02-21 stsp if (refs) {
2376 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, commit_id);
2377 8b473291 2019-02-21 stsp if (err)
2378 8b473291 2019-02-21 stsp return err;
2379 8b473291 2019-02-21 stsp }
2380 8b473291 2019-02-21 stsp
2381 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2382 abd2672a 2018-12-23 stsp if (err)
2383 abd2672a 2018-12-23 stsp return err;
2384 abd2672a 2018-12-23 stsp
2385 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2386 15a94983 2018-12-23 stsp if (err) {
2387 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2388 15a94983 2018-12-23 stsp goto done;
2389 15a94983 2018-12-23 stsp }
2390 abd2672a 2018-12-23 stsp
2391 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2392 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2393 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2394 abd2672a 2018-12-23 stsp goto done;
2395 abd2672a 2018-12-23 stsp }
2396 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2397 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2398 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2399 abd2672a 2018-12-23 stsp goto done;
2400 abd2672a 2018-12-23 stsp }
2401 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2402 abd2672a 2018-12-23 stsp if (fprintf(outfile, "date: %s UTC\n",
2403 45d799e2 2018-12-23 stsp get_datestr(&committer_time, datebuf)) < 0) {
2404 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2405 abd2672a 2018-12-23 stsp goto done;
2406 abd2672a 2018-12-23 stsp }
2407 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2408 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2409 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2410 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2411 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2412 abd2672a 2018-12-23 stsp goto done;
2413 abd2672a 2018-12-23 stsp }
2414 45d799e2 2018-12-23 stsp if (fprintf(outfile, "%s\n",
2415 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(commit)) < 0) {
2416 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2417 abd2672a 2018-12-23 stsp goto done;
2418 abd2672a 2018-12-23 stsp }
2419 abd2672a 2018-12-23 stsp done:
2420 abd2672a 2018-12-23 stsp free(id_str);
2421 8b473291 2019-02-21 stsp free(refs_str);
2422 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2423 abd2672a 2018-12-23 stsp return err;
2424 abd2672a 2018-12-23 stsp }
2425 abd2672a 2018-12-23 stsp
2426 abd2672a 2018-12-23 stsp static const struct got_error *
2427 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2428 26ed57b2 2018-05-19 stsp {
2429 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2430 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2431 15a94983 2018-12-23 stsp int obj_type;
2432 26ed57b2 2018-05-19 stsp
2433 511a516b 2018-05-19 stsp f = got_opentemp();
2434 48ae06ee 2018-10-18 stsp if (f == NULL) {
2435 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2436 48ae06ee 2018-10-18 stsp goto done;
2437 48ae06ee 2018-10-18 stsp }
2438 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2439 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2440 fb43ecf1 2019-02-11 stsp goto done;
2441 fb43ecf1 2019-02-11 stsp }
2442 48ae06ee 2018-10-18 stsp s->f = f;
2443 26ed57b2 2018-05-19 stsp
2444 15a94983 2018-12-23 stsp if (s->id1)
2445 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2446 15a94983 2018-12-23 stsp else
2447 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2448 15a94983 2018-12-23 stsp if (err)
2449 15a94983 2018-12-23 stsp goto done;
2450 15a94983 2018-12-23 stsp
2451 15a94983 2018-12-23 stsp switch (obj_type) {
2452 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2453 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2454 54156555 2018-12-24 stsp s->diff_context, s->repo, f);
2455 26ed57b2 2018-05-19 stsp break;
2456 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2457 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2458 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
2459 26ed57b2 2018-05-19 stsp break;
2460 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2461 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2462 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2463 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2464 abd2672a 2018-12-23 stsp
2465 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2466 abd2672a 2018-12-23 stsp if (err)
2467 abd2672a 2018-12-23 stsp break;
2468 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2469 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2470 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2471 15a087fe 2019-02-21 stsp else {
2472 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2473 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2474 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2475 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2476 8b473291 2019-02-21 stsp s->repo, f);
2477 15a087fe 2019-02-21 stsp break;
2478 15a087fe 2019-02-21 stsp }
2479 abd2672a 2018-12-23 stsp }
2480 abd2672a 2018-12-23 stsp }
2481 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2482 abd2672a 2018-12-23 stsp
2483 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2484 15a94983 2018-12-23 stsp s->diff_context, s->repo, f);
2485 26ed57b2 2018-05-19 stsp break;
2486 abd2672a 2018-12-23 stsp }
2487 26ed57b2 2018-05-19 stsp default:
2488 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2489 48ae06ee 2018-10-18 stsp break;
2490 26ed57b2 2018-05-19 stsp }
2491 48ae06ee 2018-10-18 stsp done:
2492 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2493 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2494 48ae06ee 2018-10-18 stsp return err;
2495 48ae06ee 2018-10-18 stsp }
2496 26ed57b2 2018-05-19 stsp
2497 f5215bb9 2019-02-22 stsp static void
2498 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2499 f5215bb9 2019-02-22 stsp {
2500 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2501 f5215bb9 2019-02-22 stsp update_panels();
2502 f5215bb9 2019-02-22 stsp doupdate();
2503 f5215bb9 2019-02-22 stsp }
2504 f5215bb9 2019-02-22 stsp
2505 48ae06ee 2018-10-18 stsp static const struct got_error *
2506 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2507 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2508 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2509 48ae06ee 2018-10-18 stsp {
2510 48ae06ee 2018-10-18 stsp const struct got_error *err;
2511 5dc9f4bc 2018-08-04 stsp
2512 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2513 15a94983 2018-12-23 stsp int type1, type2;
2514 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2515 15a94983 2018-12-23 stsp if (err)
2516 15a94983 2018-12-23 stsp return err;
2517 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2518 15a94983 2018-12-23 stsp if (err)
2519 15a94983 2018-12-23 stsp return err;
2520 15a94983 2018-12-23 stsp
2521 15a94983 2018-12-23 stsp if (type1 != type2)
2522 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2523 15a94983 2018-12-23 stsp }
2524 48ae06ee 2018-10-18 stsp
2525 15a94983 2018-12-23 stsp if (id1) {
2526 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2527 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2528 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2529 48ae06ee 2018-10-18 stsp } else
2530 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2531 48ae06ee 2018-10-18 stsp
2532 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2533 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2534 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2535 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2536 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2537 48ae06ee 2018-10-18 stsp }
2538 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2539 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2540 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2541 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2542 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2543 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2544 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2545 5dc9f4bc 2018-08-04 stsp
2546 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2547 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2548 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2549 f5215bb9 2019-02-22 stsp
2550 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2551 48ae06ee 2018-10-18 stsp if (err) {
2552 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2553 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2554 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2555 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2556 48ae06ee 2018-10-18 stsp return err;
2557 48ae06ee 2018-10-18 stsp }
2558 48ae06ee 2018-10-18 stsp
2559 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2560 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2561 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2562 e5a0f69f 2018-08-18 stsp
2563 5dc9f4bc 2018-08-04 stsp return NULL;
2564 5dc9f4bc 2018-08-04 stsp }
2565 5dc9f4bc 2018-08-04 stsp
2566 e5a0f69f 2018-08-18 stsp static const struct got_error *
2567 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2568 5dc9f4bc 2018-08-04 stsp {
2569 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2570 e5a0f69f 2018-08-18 stsp
2571 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2572 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2573 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2574 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2575 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2576 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2577 e5a0f69f 2018-08-18 stsp return err;
2578 5dc9f4bc 2018-08-04 stsp }
2579 5dc9f4bc 2018-08-04 stsp
2580 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2581 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2582 5dc9f4bc 2018-08-04 stsp {
2583 a3404814 2018-09-02 stsp const struct got_error *err;
2584 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2585 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2586 a3404814 2018-09-02 stsp
2587 a3404814 2018-09-02 stsp if (s->id1) {
2588 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2589 a3404814 2018-09-02 stsp if (err)
2590 a3404814 2018-09-02 stsp return err;
2591 a3404814 2018-09-02 stsp }
2592 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2593 a3404814 2018-09-02 stsp if (err)
2594 a3404814 2018-09-02 stsp return err;
2595 26ed57b2 2018-05-19 stsp
2596 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2597 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2598 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2599 a3404814 2018-09-02 stsp free(id_str1);
2600 a3404814 2018-09-02 stsp free(id_str2);
2601 a3404814 2018-09-02 stsp return err;
2602 a3404814 2018-09-02 stsp }
2603 a3404814 2018-09-02 stsp free(id_str1);
2604 a3404814 2018-09-02 stsp free(id_str2);
2605 a3404814 2018-09-02 stsp
2606 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2607 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2608 a3404814 2018-09-02 stsp header);
2609 15a087fe 2019-02-21 stsp }
2610 15a087fe 2019-02-21 stsp
2611 15a087fe 2019-02-21 stsp static const struct got_error *
2612 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2613 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2614 15a087fe 2019-02-21 stsp {
2615 d7a04538 2019-02-21 stsp const struct got_error *err;
2616 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
2617 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
2618 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
2619 15a087fe 2019-02-21 stsp
2620 15a087fe 2019-02-21 stsp free(s->id2);
2621 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2622 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2623 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2624 15a087fe 2019-02-21 stsp
2625 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2626 d7a04538 2019-02-21 stsp if (err)
2627 d7a04538 2019-02-21 stsp return err;
2628 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
2629 15a087fe 2019-02-21 stsp free(s->id1);
2630 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
2631 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2632 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
2633 15a087fe 2019-02-21 stsp return NULL;
2634 0cf4efb1 2018-09-29 stsp }
2635 0cf4efb1 2018-09-29 stsp
2636 0cf4efb1 2018-09-29 stsp static const struct got_error *
2637 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2638 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2639 e5a0f69f 2018-08-18 stsp {
2640 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2641 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2642 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
2643 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
2644 e5a0f69f 2018-08-18 stsp int i;
2645 e5a0f69f 2018-08-18 stsp
2646 e5a0f69f 2018-08-18 stsp switch (ch) {
2647 1e37a5c2 2019-05-12 jcs case 'k':
2648 1e37a5c2 2019-05-12 jcs case KEY_UP:
2649 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
2650 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2651 1e37a5c2 2019-05-12 jcs break;
2652 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2653 a60a9dc4 2019-05-13 jcs case CTRL('b'):
2654 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
2655 26ed57b2 2018-05-19 stsp break;
2656 1e37a5c2 2019-05-12 jcs i = 0;
2657 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
2658 1e37a5c2 2019-05-12 jcs 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 'j':
2662 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2663 1e37a5c2 2019-05-12 jcs if (!s->eof)
2664 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2665 1e37a5c2 2019-05-12 jcs break;
2666 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
2667 a60a9dc4 2019-05-13 jcs case CTRL('f'):
2668 1e37a5c2 2019-05-12 jcs case ' ':
2669 00ba99a7 2019-05-12 jcs if (s->eof)
2670 1e37a5c2 2019-05-12 jcs break;
2671 1e37a5c2 2019-05-12 jcs i = 0;
2672 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
2673 1e37a5c2 2019-05-12 jcs char *line;
2674 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
2675 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2676 1e37a5c2 2019-05-12 jcs if (line == NULL)
2677 34bc9ec9 2019-02-22 stsp break;
2678 1e37a5c2 2019-05-12 jcs }
2679 1e37a5c2 2019-05-12 jcs break;
2680 1e37a5c2 2019-05-12 jcs case '[':
2681 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
2682 1e37a5c2 2019-05-12 jcs s->diff_context--;
2683 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2684 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2685 1e37a5c2 2019-05-12 jcs }
2686 1e37a5c2 2019-05-12 jcs break;
2687 1e37a5c2 2019-05-12 jcs case ']':
2688 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2689 1e37a5c2 2019-05-12 jcs s->diff_context++;
2690 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2691 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2692 1e37a5c2 2019-05-12 jcs }
2693 1e37a5c2 2019-05-12 jcs break;
2694 1e37a5c2 2019-05-12 jcs case '<':
2695 1e37a5c2 2019-05-12 jcs case ',':
2696 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2697 48ae06ee 2018-10-18 stsp break;
2698 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2699 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
2700 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
2701 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2702 48ae06ee 2018-10-18 stsp break;
2703 6524637e 2019-02-21 stsp
2704 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2705 1e37a5c2 2019-05-12 jcs KEY_UP);
2706 1e37a5c2 2019-05-12 jcs if (err)
2707 1e37a5c2 2019-05-12 jcs break;
2708 15a087fe 2019-02-21 stsp
2709 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2710 1e37a5c2 2019-05-12 jcs if (err)
2711 1e37a5c2 2019-05-12 jcs break;
2712 15a087fe 2019-02-21 stsp
2713 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2714 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2715 15a087fe 2019-02-21 stsp
2716 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2717 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2718 1e37a5c2 2019-05-12 jcs break;
2719 1e37a5c2 2019-05-12 jcs case '>':
2720 1e37a5c2 2019-05-12 jcs case '.':
2721 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2722 15a087fe 2019-02-21 stsp break;
2723 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2724 5e224a3e 2019-02-22 stsp
2725 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2726 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
2727 5e224a3e 2019-02-22 stsp
2728 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
2729 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
2730 1e37a5c2 2019-05-12 jcs update_panels();
2731 1e37a5c2 2019-05-12 jcs doupdate();
2732 6e73b0d6 2019-02-22 stsp
2733 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
2734 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
2735 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
2736 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
2737 fb872ab2 2019-02-21 stsp if (err)
2738 fb872ab2 2019-02-21 stsp break;
2739 1e37a5c2 2019-05-12 jcs }
2740 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2741 1e37a5c2 2019-05-12 jcs KEY_DOWN);
2742 1e37a5c2 2019-05-12 jcs if (err)
2743 1e37a5c2 2019-05-12 jcs break;
2744 15a087fe 2019-02-21 stsp
2745 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
2746 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2747 1e37a5c2 2019-05-12 jcs break;
2748 15a087fe 2019-02-21 stsp
2749 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2750 1e37a5c2 2019-05-12 jcs if (err)
2751 1e37a5c2 2019-05-12 jcs break;
2752 15a087fe 2019-02-21 stsp
2753 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2754 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2755 1e37a5c2 2019-05-12 jcs
2756 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2757 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2758 1e37a5c2 2019-05-12 jcs break;
2759 1e37a5c2 2019-05-12 jcs default:
2760 1e37a5c2 2019-05-12 jcs break;
2761 26ed57b2 2018-05-19 stsp }
2762 e5a0f69f 2018-08-18 stsp
2763 bcbd79e2 2018-08-19 stsp return err;
2764 26ed57b2 2018-05-19 stsp }
2765 26ed57b2 2018-05-19 stsp
2766 4ed7e80c 2018-05-20 stsp static const struct got_error *
2767 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2768 9f7d7167 2018-04-29 stsp {
2769 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2770 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2771 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2772 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2773 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2774 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2775 26ed57b2 2018-05-19 stsp int ch;
2776 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2777 70ac5f84 2019-03-28 stsp
2778 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2779 26ed57b2 2018-05-19 stsp
2780 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2781 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2782 eb6600df 2019-01-04 stsp NULL) == -1)
2783 26ed57b2 2018-05-19 stsp err(1, "pledge");
2784 26ed57b2 2018-05-19 stsp #endif
2785 26ed57b2 2018-05-19 stsp
2786 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2787 26ed57b2 2018-05-19 stsp switch (ch) {
2788 26ed57b2 2018-05-19 stsp default:
2789 17020d27 2019-03-07 stsp usage_diff();
2790 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2791 26ed57b2 2018-05-19 stsp }
2792 26ed57b2 2018-05-19 stsp }
2793 26ed57b2 2018-05-19 stsp
2794 26ed57b2 2018-05-19 stsp argc -= optind;
2795 26ed57b2 2018-05-19 stsp argv += optind;
2796 26ed57b2 2018-05-19 stsp
2797 26ed57b2 2018-05-19 stsp if (argc == 0) {
2798 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2799 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2800 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2801 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2802 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2803 15a94983 2018-12-23 stsp id_str1 = argv[0];
2804 15a94983 2018-12-23 stsp id_str2 = argv[1];
2805 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2806 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2807 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2808 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2809 15a94983 2018-12-23 stsp id_str1 = argv[1];
2810 15a94983 2018-12-23 stsp id_str2 = argv[2];
2811 26ed57b2 2018-05-19 stsp } else
2812 26ed57b2 2018-05-19 stsp usage_diff();
2813 a915003a 2019-02-05 stsp
2814 a915003a 2019-02-05 stsp init_curses();
2815 eb6600df 2019-01-04 stsp
2816 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2817 eb6600df 2019-01-04 stsp if (error)
2818 eb6600df 2019-01-04 stsp goto done;
2819 26ed57b2 2018-05-19 stsp
2820 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
2821 26ed57b2 2018-05-19 stsp if (error)
2822 26ed57b2 2018-05-19 stsp goto done;
2823 26ed57b2 2018-05-19 stsp
2824 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
2825 26ed57b2 2018-05-19 stsp if (error)
2826 26ed57b2 2018-05-19 stsp goto done;
2827 26ed57b2 2018-05-19 stsp
2828 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
2829 26ed57b2 2018-05-19 stsp if (error)
2830 26ed57b2 2018-05-19 stsp goto done;
2831 26ed57b2 2018-05-19 stsp
2832 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2833 8b473291 2019-02-21 stsp if (error)
2834 8b473291 2019-02-21 stsp goto done;
2835 8b473291 2019-02-21 stsp
2836 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2837 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2838 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2839 ea5e7bb5 2018-08-01 stsp goto done;
2840 ea5e7bb5 2018-08-01 stsp }
2841 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2842 5dc9f4bc 2018-08-04 stsp if (error)
2843 5dc9f4bc 2018-08-04 stsp goto done;
2844 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2845 26ed57b2 2018-05-19 stsp done:
2846 c02c541e 2019-03-29 stsp free(repo_path);
2847 26ed57b2 2018-05-19 stsp got_repo_close(repo);
2848 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2849 26ed57b2 2018-05-19 stsp return error;
2850 9f7d7167 2018-04-29 stsp }
2851 9f7d7167 2018-04-29 stsp
2852 4ed7e80c 2018-05-20 stsp __dead static void
2853 9f7d7167 2018-04-29 stsp usage_blame(void)
2854 9f7d7167 2018-04-29 stsp {
2855 80ddbec8 2018-04-29 stsp endwin();
2856 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2857 9f7d7167 2018-04-29 stsp getprogname());
2858 9f7d7167 2018-04-29 stsp exit(1);
2859 9f7d7167 2018-04-29 stsp }
2860 84451b3e 2018-07-10 stsp
2861 84451b3e 2018-07-10 stsp struct tog_blame_line {
2862 84451b3e 2018-07-10 stsp int annotated;
2863 84451b3e 2018-07-10 stsp struct got_object_id *id;
2864 84451b3e 2018-07-10 stsp };
2865 9f7d7167 2018-04-29 stsp
2866 4ed7e80c 2018-05-20 stsp static const struct got_error *
2867 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2868 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2869 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2870 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2871 84451b3e 2018-07-10 stsp {
2872 84451b3e 2018-07-10 stsp const struct got_error *err;
2873 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2874 84451b3e 2018-07-10 stsp char *line;
2875 84451b3e 2018-07-10 stsp size_t len;
2876 84451b3e 2018-07-10 stsp wchar_t *wline;
2877 b700b5d6 2018-07-10 stsp int width, wlimit;
2878 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2879 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2880 ab089a2a 2018-07-12 stsp char *id_str;
2881 ab089a2a 2018-07-12 stsp
2882 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2883 ab089a2a 2018-07-12 stsp if (err)
2884 ab089a2a 2018-07-12 stsp return err;
2885 84451b3e 2018-07-10 stsp
2886 84451b3e 2018-07-10 stsp rewind(f);
2887 f7d12f7e 2018-08-01 stsp werase(view->window);
2888 84451b3e 2018-07-10 stsp
2889 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
2890 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2891 ab089a2a 2018-07-12 stsp free(id_str);
2892 ab089a2a 2018-07-12 stsp return err;
2893 ab089a2a 2018-07-12 stsp }
2894 ab089a2a 2018-07-12 stsp
2895 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2896 ab089a2a 2018-07-12 stsp free(line);
2897 2550e4c3 2018-07-13 stsp line = NULL;
2898 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2899 a3404814 2018-09-02 stsp wstandout(view->window);
2900 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2901 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2902 a3404814 2018-09-02 stsp wstandend(view->window);
2903 2550e4c3 2018-07-13 stsp free(wline);
2904 2550e4c3 2018-07-13 stsp wline = NULL;
2905 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2906 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2907 ab089a2a 2018-07-12 stsp
2908 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2909 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2910 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
2911 ab089a2a 2018-07-12 stsp free(id_str);
2912 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2913 ab089a2a 2018-07-12 stsp }
2914 ab089a2a 2018-07-12 stsp free(id_str);
2915 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2916 3f60a8ef 2018-07-10 stsp free(line);
2917 2550e4c3 2018-07-13 stsp line = NULL;
2918 3f60a8ef 2018-07-10 stsp if (err)
2919 3f60a8ef 2018-07-10 stsp return err;
2920 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2921 2550e4c3 2018-07-13 stsp free(wline);
2922 2550e4c3 2018-07-13 stsp wline = NULL;
2923 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2924 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2925 3f60a8ef 2018-07-10 stsp
2926 84451b3e 2018-07-10 stsp *eof = 0;
2927 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2928 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2929 84451b3e 2018-07-10 stsp if (line == NULL) {
2930 84451b3e 2018-07-10 stsp *eof = 1;
2931 84451b3e 2018-07-10 stsp break;
2932 84451b3e 2018-07-10 stsp }
2933 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2934 84451b3e 2018-07-10 stsp free(line);
2935 84451b3e 2018-07-10 stsp continue;
2936 84451b3e 2018-07-10 stsp }
2937 84451b3e 2018-07-10 stsp
2938 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2939 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2940 84451b3e 2018-07-10 stsp if (err) {
2941 84451b3e 2018-07-10 stsp free(line);
2942 84451b3e 2018-07-10 stsp return err;
2943 84451b3e 2018-07-10 stsp }
2944 84451b3e 2018-07-10 stsp
2945 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2946 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2947 b700b5d6 2018-07-10 stsp
2948 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2949 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2950 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2951 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2952 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2953 84451b3e 2018-07-10 stsp char *id_str;
2954 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2955 84451b3e 2018-07-10 stsp if (err) {
2956 84451b3e 2018-07-10 stsp free(line);
2957 2550e4c3 2018-07-13 stsp free(wline);
2958 84451b3e 2018-07-10 stsp return err;
2959 84451b3e 2018-07-10 stsp }
2960 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2961 84451b3e 2018-07-10 stsp free(id_str);
2962 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2963 ee41ec32 2018-07-10 stsp } else {
2964 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2965 ee41ec32 2018-07-10 stsp prev_id = NULL;
2966 ee41ec32 2018-07-10 stsp }
2967 84451b3e 2018-07-10 stsp
2968 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2969 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2970 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2971 b700b5d6 2018-07-10 stsp width++;
2972 b700b5d6 2018-07-10 stsp }
2973 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2974 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2975 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2976 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2977 84451b3e 2018-07-10 stsp free(line);
2978 2550e4c3 2018-07-13 stsp free(wline);
2979 2550e4c3 2018-07-13 stsp wline = NULL;
2980 84451b3e 2018-07-10 stsp }
2981 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2982 84451b3e 2018-07-10 stsp
2983 1a57306a 2018-09-02 stsp view_vborder(view);
2984 84451b3e 2018-07-10 stsp
2985 84451b3e 2018-07-10 stsp return NULL;
2986 84451b3e 2018-07-10 stsp }
2987 84451b3e 2018-07-10 stsp
2988 84451b3e 2018-07-10 stsp static const struct got_error *
2989 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2990 84451b3e 2018-07-10 stsp {
2991 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2992 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2993 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2994 1a76625f 2018-10-22 stsp int errcode;
2995 84451b3e 2018-07-10 stsp
2996 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2997 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2998 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2999 84451b3e 2018-07-10 stsp
3000 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3001 1a76625f 2018-10-22 stsp if (errcode)
3002 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3003 84451b3e 2018-07-10 stsp
3004 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3005 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3006 d68a0a7d 2018-07-10 stsp goto done;
3007 d68a0a7d 2018-07-10 stsp }
3008 d68a0a7d 2018-07-10 stsp
3009 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3010 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3011 d68a0a7d 2018-07-10 stsp
3012 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3013 d68a0a7d 2018-07-10 stsp if (line->annotated)
3014 d68a0a7d 2018-07-10 stsp goto done;
3015 d68a0a7d 2018-07-10 stsp
3016 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3017 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3018 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3019 84451b3e 2018-07-10 stsp goto done;
3020 84451b3e 2018-07-10 stsp }
3021 84451b3e 2018-07-10 stsp line->annotated = 1;
3022 84451b3e 2018-07-10 stsp done:
3023 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3024 1a76625f 2018-10-22 stsp if (errcode)
3025 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3026 84451b3e 2018-07-10 stsp return err;
3027 84451b3e 2018-07-10 stsp }
3028 84451b3e 2018-07-10 stsp
3029 84451b3e 2018-07-10 stsp static void *
3030 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3031 84451b3e 2018-07-10 stsp {
3032 18430de3 2018-07-10 stsp const struct got_error *err;
3033 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3034 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3035 1a76625f 2018-10-22 stsp int errcode;
3036 18430de3 2018-07-10 stsp
3037 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3038 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
3039 18430de3 2018-07-10 stsp
3040 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3041 1a76625f 2018-10-22 stsp if (errcode)
3042 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3043 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3044 18430de3 2018-07-10 stsp
3045 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3046 c9beca56 2018-07-22 stsp ta->repo = NULL;
3047 c9beca56 2018-07-22 stsp *ta->complete = 1;
3048 18430de3 2018-07-10 stsp
3049 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3050 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3051 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3052 18430de3 2018-07-10 stsp
3053 18430de3 2018-07-10 stsp return (void *)err;
3054 84451b3e 2018-07-10 stsp }
3055 84451b3e 2018-07-10 stsp
3056 245d91c1 2018-07-12 stsp static struct got_object_id *
3057 15a94983 2018-12-23 stsp get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3058 15a94983 2018-12-23 stsp int selected_line)
3059 245d91c1 2018-07-12 stsp {
3060 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3061 b880a918 2018-07-10 stsp
3062 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3063 245d91c1 2018-07-12 stsp if (!line->annotated)
3064 245d91c1 2018-07-12 stsp return NULL;
3065 245d91c1 2018-07-12 stsp
3066 245d91c1 2018-07-12 stsp return line->id;
3067 b880a918 2018-07-10 stsp }
3068 245d91c1 2018-07-12 stsp
3069 b880a918 2018-07-10 stsp static const struct got_error *
3070 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3071 a70480e0 2018-06-23 stsp {
3072 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3073 245d91c1 2018-07-12 stsp int i;
3074 245d91c1 2018-07-12 stsp
3075 245d91c1 2018-07-12 stsp if (blame->thread) {
3076 1a76625f 2018-10-22 stsp int errcode;
3077 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3078 1a76625f 2018-10-22 stsp if (errcode)
3079 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3080 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3081 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3082 1a76625f 2018-10-22 stsp if (errcode)
3083 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3084 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3085 1a76625f 2018-10-22 stsp if (errcode)
3086 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3087 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3088 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3089 245d91c1 2018-07-12 stsp err = NULL;
3090 245d91c1 2018-07-12 stsp blame->thread = NULL;
3091 245d91c1 2018-07-12 stsp }
3092 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3093 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3094 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3095 245d91c1 2018-07-12 stsp }
3096 245d91c1 2018-07-12 stsp if (blame->f) {
3097 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3098 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3099 245d91c1 2018-07-12 stsp blame->f = NULL;
3100 245d91c1 2018-07-12 stsp }
3101 57670559 2018-12-23 stsp if (blame->lines) {
3102 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3103 57670559 2018-12-23 stsp free(blame->lines[i].id);
3104 57670559 2018-12-23 stsp free(blame->lines);
3105 57670559 2018-12-23 stsp blame->lines = NULL;
3106 57670559 2018-12-23 stsp }
3107 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3108 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3109 245d91c1 2018-07-12 stsp
3110 245d91c1 2018-07-12 stsp return err;
3111 245d91c1 2018-07-12 stsp }
3112 245d91c1 2018-07-12 stsp
3113 245d91c1 2018-07-12 stsp static const struct got_error *
3114 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3115 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3116 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3117 245d91c1 2018-07-12 stsp struct got_repository *repo)
3118 245d91c1 2018-07-12 stsp {
3119 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3120 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3121 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3122 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3123 15a94983 2018-12-23 stsp int obj_type;
3124 a70480e0 2018-06-23 stsp
3125 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3126 27d434c2 2018-09-15 stsp if (err)
3127 15a94983 2018-12-23 stsp return err;
3128 15a94983 2018-12-23 stsp if (obj_id == NULL)
3129 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
3130 27d434c2 2018-09-15 stsp
3131 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3132 84451b3e 2018-07-10 stsp if (err)
3133 84451b3e 2018-07-10 stsp goto done;
3134 27d434c2 2018-09-15 stsp
3135 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3136 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3137 84451b3e 2018-07-10 stsp goto done;
3138 84451b3e 2018-07-10 stsp }
3139 a70480e0 2018-06-23 stsp
3140 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3141 a70480e0 2018-06-23 stsp if (err)
3142 a70480e0 2018-06-23 stsp goto done;
3143 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3144 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3145 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3146 84451b3e 2018-07-10 stsp goto done;
3147 84451b3e 2018-07-10 stsp }
3148 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3149 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
3150 84451b3e 2018-07-10 stsp if (err)
3151 84451b3e 2018-07-10 stsp goto done;
3152 a70480e0 2018-06-23 stsp
3153 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3154 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3155 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3156 84451b3e 2018-07-10 stsp goto done;
3157 84451b3e 2018-07-10 stsp }
3158 a70480e0 2018-06-23 stsp
3159 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3160 bd24772e 2018-07-11 stsp if (err)
3161 bd24772e 2018-07-11 stsp goto done;
3162 bd24772e 2018-07-11 stsp
3163 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3164 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3165 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3166 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3167 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3168 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3169 245d91c1 2018-07-12 stsp goto done;
3170 245d91c1 2018-07-12 stsp }
3171 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3172 245d91c1 2018-07-12 stsp
3173 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3174 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3175 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3176 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3177 245d91c1 2018-07-12 stsp *blame_complete = 0;
3178 245d91c1 2018-07-12 stsp
3179 245d91c1 2018-07-12 stsp done:
3180 245d91c1 2018-07-12 stsp if (blob)
3181 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3182 27d434c2 2018-09-15 stsp free(obj_id);
3183 245d91c1 2018-07-12 stsp if (err)
3184 245d91c1 2018-07-12 stsp stop_blame(blame);
3185 245d91c1 2018-07-12 stsp return err;
3186 245d91c1 2018-07-12 stsp }
3187 245d91c1 2018-07-12 stsp
3188 245d91c1 2018-07-12 stsp static const struct got_error *
3189 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3190 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3191 8b473291 2019-02-21 stsp struct got_repository *repo)
3192 245d91c1 2018-07-12 stsp {
3193 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3194 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3195 dbc6a6b6 2018-07-12 stsp
3196 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3197 245d91c1 2018-07-12 stsp
3198 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3199 dbc6a6b6 2018-07-12 stsp if (err)
3200 7cbe629d 2018-08-04 stsp return err;
3201 245d91c1 2018-07-12 stsp
3202 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3203 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3204 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3205 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3206 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3207 fb2756b9 2018-08-04 stsp s->path = path;
3208 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
3209 638f9024 2019-05-13 stsp return got_error_from_errno("open_blame_view");
3210 fb2756b9 2018-08-04 stsp s->repo = repo;
3211 8b473291 2019-02-21 stsp s->refs = refs;
3212 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3213 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3214 7cbe629d 2018-08-04 stsp
3215 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3216 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3217 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3218 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
3219 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
3220 e5a0f69f 2018-08-18 stsp
3221 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3222 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3223 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3224 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3225 7cbe629d 2018-08-04 stsp }
3226 7cbe629d 2018-08-04 stsp
3227 e5a0f69f 2018-08-18 stsp static const struct got_error *
3228 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3229 7cbe629d 2018-08-04 stsp {
3230 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3231 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3232 7cbe629d 2018-08-04 stsp
3233 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3234 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3235 e5a0f69f 2018-08-18 stsp
3236 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3237 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3238 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3239 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3240 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3241 7cbe629d 2018-08-04 stsp }
3242 e5a0f69f 2018-08-18 stsp
3243 e5a0f69f 2018-08-18 stsp free(s->path);
3244 e5a0f69f 2018-08-18 stsp
3245 e5a0f69f 2018-08-18 stsp return err;
3246 7cbe629d 2018-08-04 stsp }
3247 7cbe629d 2018-08-04 stsp
3248 7cbe629d 2018-08-04 stsp static const struct got_error *
3249 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
3250 6c4c42e0 2019-06-24 stsp {
3251 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3252 6c4c42e0 2019-06-24 stsp
3253 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
3254 6c4c42e0 2019-06-24 stsp return NULL;
3255 6c4c42e0 2019-06-24 stsp }
3256 6c4c42e0 2019-06-24 stsp
3257 6c4c42e0 2019-06-24 stsp static int
3258 6c4c42e0 2019-06-24 stsp match_line(const char *line, regex_t *regex)
3259 6c4c42e0 2019-06-24 stsp {
3260 6c4c42e0 2019-06-24 stsp regmatch_t regmatch;
3261 6c4c42e0 2019-06-24 stsp
3262 6c4c42e0 2019-06-24 stsp return regexec(regex, line, 1, &regmatch, 0) == 0;
3263 6c4c42e0 2019-06-24 stsp }
3264 6c4c42e0 2019-06-24 stsp
3265 6c4c42e0 2019-06-24 stsp
3266 6c4c42e0 2019-06-24 stsp static const struct got_error *
3267 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
3268 6c4c42e0 2019-06-24 stsp {
3269 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3270 6c4c42e0 2019-06-24 stsp int lineno;
3271 6c4c42e0 2019-06-24 stsp
3272 6c4c42e0 2019-06-24 stsp if (!view->searching) {
3273 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3274 6c4c42e0 2019-06-24 stsp return NULL;
3275 6c4c42e0 2019-06-24 stsp }
3276 6c4c42e0 2019-06-24 stsp
3277 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3278 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3279 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
3280 6c4c42e0 2019-06-24 stsp else
3281 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
3282 6c4c42e0 2019-06-24 stsp } else {
3283 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3284 6c4c42e0 2019-06-24 stsp lineno = 1;
3285 6c4c42e0 2019-06-24 stsp else
3286 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3287 6c4c42e0 2019-06-24 stsp }
3288 6c4c42e0 2019-06-24 stsp
3289 6c4c42e0 2019-06-24 stsp while (1) {
3290 6c4c42e0 2019-06-24 stsp char *line = NULL;
3291 6c4c42e0 2019-06-24 stsp off_t offset;
3292 6c4c42e0 2019-06-24 stsp size_t len;
3293 6c4c42e0 2019-06-24 stsp
3294 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
3295 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
3296 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3297 6c4c42e0 2019-06-24 stsp free(line);
3298 6c4c42e0 2019-06-24 stsp break;
3299 6c4c42e0 2019-06-24 stsp }
3300 2246482e 2019-06-25 stsp
3301 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3302 6c4c42e0 2019-06-24 stsp lineno = 1;
3303 6c4c42e0 2019-06-24 stsp else
3304 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3305 6c4c42e0 2019-06-24 stsp }
3306 6c4c42e0 2019-06-24 stsp
3307 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
3308 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3309 6c4c42e0 2019-06-24 stsp free(line);
3310 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
3311 6c4c42e0 2019-06-24 stsp }
3312 6c4c42e0 2019-06-24 stsp free(line);
3313 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
3314 2246482e 2019-06-25 stsp if (line && match_line(line, &view->regex)) {
3315 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3316 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
3317 6c4c42e0 2019-06-24 stsp free(line);
3318 6c4c42e0 2019-06-24 stsp break;
3319 6c4c42e0 2019-06-24 stsp }
3320 6c4c42e0 2019-06-24 stsp free(line);
3321 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3322 6c4c42e0 2019-06-24 stsp lineno++;
3323 6c4c42e0 2019-06-24 stsp else
3324 6c4c42e0 2019-06-24 stsp lineno--;
3325 6c4c42e0 2019-06-24 stsp }
3326 6c4c42e0 2019-06-24 stsp
3327 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3328 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
3329 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
3330 6c4c42e0 2019-06-24 stsp }
3331 6c4c42e0 2019-06-24 stsp
3332 6c4c42e0 2019-06-24 stsp return NULL;
3333 6c4c42e0 2019-06-24 stsp }
3334 6c4c42e0 2019-06-24 stsp
3335 6c4c42e0 2019-06-24 stsp static const struct got_error *
3336 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
3337 7cbe629d 2018-08-04 stsp {
3338 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3339 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
3340 2b380cc8 2018-10-24 stsp int errcode;
3341 2b380cc8 2018-10-24 stsp
3342 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
3343 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3344 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
3345 2b380cc8 2018-10-24 stsp if (errcode)
3346 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3347 2b380cc8 2018-10-24 stsp }
3348 e5a0f69f 2018-08-18 stsp
3349 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3350 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3351 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
3352 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
3353 e5a0f69f 2018-08-18 stsp
3354 669b5ffa 2018-10-07 stsp view_vborder(view);
3355 e5a0f69f 2018-08-18 stsp return err;
3356 e5a0f69f 2018-08-18 stsp }
3357 e5a0f69f 2018-08-18 stsp
3358 e5a0f69f 2018-08-18 stsp static const struct got_error *
3359 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3360 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3361 e5a0f69f 2018-08-18 stsp {
3362 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
3363 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
3364 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3365 669b5ffa 2018-10-07 stsp int begin_x = 0;
3366 7cbe629d 2018-08-04 stsp
3367 e5a0f69f 2018-08-18 stsp switch (ch) {
3368 1e37a5c2 2019-05-12 jcs case 'q':
3369 1e37a5c2 2019-05-12 jcs s->done = 1;
3370 1e37a5c2 2019-05-12 jcs break;
3371 1e37a5c2 2019-05-12 jcs case 'k':
3372 1e37a5c2 2019-05-12 jcs case KEY_UP:
3373 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
3374 1e37a5c2 2019-05-12 jcs s->selected_line--;
3375 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
3376 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3377 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3378 1e37a5c2 2019-05-12 jcs break;
3379 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3380 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
3381 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3382 e5a0f69f 2018-08-18 stsp break;
3383 1e37a5c2 2019-05-12 jcs }
3384 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3385 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3386 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3387 1e37a5c2 2019-05-12 jcs else
3388 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3389 1e37a5c2 2019-05-12 jcs break;
3390 1e37a5c2 2019-05-12 jcs case 'j':
3391 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3392 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3393 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3394 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3395 1e37a5c2 2019-05-12 jcs s->selected_line++;
3396 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3397 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3398 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3399 1e37a5c2 2019-05-12 jcs break;
3400 1e37a5c2 2019-05-12 jcs case 'b':
3401 1e37a5c2 2019-05-12 jcs case 'p': {
3402 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3403 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3404 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3405 1e37a5c2 2019-05-12 jcs if (id == NULL)
3406 e5a0f69f 2018-08-18 stsp break;
3407 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3408 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3409 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3410 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3411 1e37a5c2 2019-05-12 jcs int obj_type;
3412 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3413 1e37a5c2 2019-05-12 jcs s->repo, id);
3414 e5a0f69f 2018-08-18 stsp if (err)
3415 e5a0f69f 2018-08-18 stsp break;
3416 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3417 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3418 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3419 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3420 e5a0f69f 2018-08-18 stsp break;
3421 e5a0f69f 2018-08-18 stsp }
3422 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3423 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3424 1e37a5c2 2019-05-12 jcs pid->id, s->path);
3425 e5a0f69f 2018-08-18 stsp if (err) {
3426 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
3427 1e37a5c2 2019-05-12 jcs err = NULL;
3428 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3429 e5a0f69f 2018-08-18 stsp break;
3430 e5a0f69f 2018-08-18 stsp }
3431 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
3432 1e37a5c2 2019-05-12 jcs blob_id);
3433 1e37a5c2 2019-05-12 jcs free(blob_id);
3434 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
3435 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
3436 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3437 e5a0f69f 2018-08-18 stsp break;
3438 1e37a5c2 2019-05-12 jcs }
3439 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3440 1e37a5c2 2019-05-12 jcs pid->id);
3441 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3442 1e37a5c2 2019-05-12 jcs } else {
3443 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
3444 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
3445 1e37a5c2 2019-05-12 jcs break;
3446 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3447 1e37a5c2 2019-05-12 jcs id);
3448 1e37a5c2 2019-05-12 jcs }
3449 1e37a5c2 2019-05-12 jcs if (err)
3450 e5a0f69f 2018-08-18 stsp break;
3451 1e37a5c2 2019-05-12 jcs s->done = 1;
3452 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3453 1e37a5c2 2019-05-12 jcs s->done = 0;
3454 1e37a5c2 2019-05-12 jcs if (thread_err)
3455 1e37a5c2 2019-05-12 jcs break;
3456 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3457 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
3458 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3459 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3460 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
3461 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
3462 1e37a5c2 2019-05-12 jcs if (err)
3463 1e37a5c2 2019-05-12 jcs break;
3464 1e37a5c2 2019-05-12 jcs break;
3465 1e37a5c2 2019-05-12 jcs }
3466 1e37a5c2 2019-05-12 jcs case 'B': {
3467 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
3468 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
3469 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
3470 1e37a5c2 2019-05-12 jcs break;
3471 1e37a5c2 2019-05-12 jcs s->done = 1;
3472 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3473 1e37a5c2 2019-05-12 jcs s->done = 0;
3474 1e37a5c2 2019-05-12 jcs if (thread_err)
3475 1e37a5c2 2019-05-12 jcs break;
3476 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3477 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
3478 1e37a5c2 2019-05-12 jcs s->blamed_commit =
3479 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
3480 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3481 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3482 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
3483 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
3484 1e37a5c2 2019-05-12 jcs if (err)
3485 1e37a5c2 2019-05-12 jcs break;
3486 1e37a5c2 2019-05-12 jcs break;
3487 1e37a5c2 2019-05-12 jcs }
3488 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3489 1e37a5c2 2019-05-12 jcs case '\r': {
3490 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3491 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
3492 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
3493 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3494 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3495 1e37a5c2 2019-05-12 jcs if (id == NULL)
3496 1e37a5c2 2019-05-12 jcs break;
3497 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
3498 1e37a5c2 2019-05-12 jcs if (err)
3499 1e37a5c2 2019-05-12 jcs break;
3500 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
3501 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
3502 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3503 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3504 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3505 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
3506 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3507 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
3508 1e37a5c2 2019-05-12 jcs break;
3509 15a94983 2018-12-23 stsp }
3510 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
3511 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
3512 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3513 1e37a5c2 2019-05-12 jcs if (err) {
3514 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3515 1e37a5c2 2019-05-12 jcs break;
3516 1e37a5c2 2019-05-12 jcs }
3517 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3518 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3519 1e37a5c2 2019-05-12 jcs if (err)
3520 34bc9ec9 2019-02-22 stsp break;
3521 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
3522 1e37a5c2 2019-05-12 jcs if (err) {
3523 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3524 e5a0f69f 2018-08-18 stsp break;
3525 e5a0f69f 2018-08-18 stsp }
3526 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
3527 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
3528 1e37a5c2 2019-05-12 jcs } else
3529 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3530 1e37a5c2 2019-05-12 jcs if (err)
3531 e5a0f69f 2018-08-18 stsp break;
3532 1e37a5c2 2019-05-12 jcs break;
3533 1e37a5c2 2019-05-12 jcs }
3534 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3535 1e37a5c2 2019-05-12 jcs case ' ':
3536 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3537 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
3538 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
3539 e5a0f69f 2018-08-18 stsp break;
3540 1e37a5c2 2019-05-12 jcs }
3541 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3542 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
3543 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3544 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3545 e5a0f69f 2018-08-18 stsp break;
3546 1e37a5c2 2019-05-12 jcs }
3547 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
3548 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
3549 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
3550 1e37a5c2 2019-05-12 jcs view->nlines - 2;
3551 1e37a5c2 2019-05-12 jcs else
3552 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
3553 1e37a5c2 2019-05-12 jcs s->blame.nlines -
3554 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
3555 1e37a5c2 2019-05-12 jcs break;
3556 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3557 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
3558 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3559 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3560 1e37a5c2 2019-05-12 jcs }
3561 1e37a5c2 2019-05-12 jcs break;
3562 1e37a5c2 2019-05-12 jcs default:
3563 1e37a5c2 2019-05-12 jcs break;
3564 a70480e0 2018-06-23 stsp }
3565 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
3566 a70480e0 2018-06-23 stsp }
3567 a70480e0 2018-06-23 stsp
3568 a70480e0 2018-06-23 stsp static const struct got_error *
3569 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
3570 9f7d7167 2018-04-29 stsp {
3571 a70480e0 2018-06-23 stsp const struct got_error *error;
3572 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
3573 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3574 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
3575 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3576 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3577 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
3578 a70480e0 2018-06-23 stsp int ch;
3579 e1cd8fed 2018-08-01 stsp struct tog_view *view;
3580 a70480e0 2018-06-23 stsp
3581 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3582 70ac5f84 2019-03-28 stsp
3583 a70480e0 2018-06-23 stsp #ifndef PROFILE
3584 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3585 8e94dd5b 2019-01-04 stsp NULL) == -1)
3586 a70480e0 2018-06-23 stsp err(1, "pledge");
3587 a70480e0 2018-06-23 stsp #endif
3588 a70480e0 2018-06-23 stsp
3589 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3590 a70480e0 2018-06-23 stsp switch (ch) {
3591 a70480e0 2018-06-23 stsp case 'c':
3592 a70480e0 2018-06-23 stsp commit_id_str = optarg;
3593 a70480e0 2018-06-23 stsp break;
3594 69069811 2018-08-02 stsp case 'r':
3595 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3596 69069811 2018-08-02 stsp if (repo_path == NULL)
3597 69069811 2018-08-02 stsp err(1, "-r option");
3598 69069811 2018-08-02 stsp break;
3599 a70480e0 2018-06-23 stsp default:
3600 17020d27 2019-03-07 stsp usage_blame();
3601 a70480e0 2018-06-23 stsp /* NOTREACHED */
3602 a70480e0 2018-06-23 stsp }
3603 a70480e0 2018-06-23 stsp }
3604 a70480e0 2018-06-23 stsp
3605 a70480e0 2018-06-23 stsp argc -= optind;
3606 a70480e0 2018-06-23 stsp argv += optind;
3607 a70480e0 2018-06-23 stsp
3608 69069811 2018-08-02 stsp if (argc == 1)
3609 69069811 2018-08-02 stsp path = argv[0];
3610 69069811 2018-08-02 stsp else
3611 a70480e0 2018-06-23 stsp usage_blame();
3612 69069811 2018-08-02 stsp
3613 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
3614 69069811 2018-08-02 stsp if (cwd == NULL) {
3615 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3616 69069811 2018-08-02 stsp goto done;
3617 69069811 2018-08-02 stsp }
3618 69069811 2018-08-02 stsp if (repo_path == NULL) {
3619 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3620 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3621 69069811 2018-08-02 stsp goto done;
3622 eb41ed75 2019-02-05 stsp else
3623 eb41ed75 2019-02-05 stsp error = NULL;
3624 eb41ed75 2019-02-05 stsp if (worktree) {
3625 eb41ed75 2019-02-05 stsp repo_path =
3626 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3627 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
3628 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3629 eb41ed75 2019-02-05 stsp if (error)
3630 eb41ed75 2019-02-05 stsp goto done;
3631 eb41ed75 2019-02-05 stsp } else {
3632 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
3633 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
3634 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3635 eb41ed75 2019-02-05 stsp goto done;
3636 eb41ed75 2019-02-05 stsp }
3637 69069811 2018-08-02 stsp }
3638 69069811 2018-08-02 stsp }
3639 a915003a 2019-02-05 stsp
3640 a915003a 2019-02-05 stsp init_curses();
3641 69069811 2018-08-02 stsp
3642 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
3643 c02c541e 2019-03-29 stsp if (error != NULL)
3644 8e94dd5b 2019-01-04 stsp goto done;
3645 69069811 2018-08-02 stsp
3646 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3647 c02c541e 2019-03-29 stsp if (error)
3648 92205607 2019-01-04 stsp goto done;
3649 69069811 2018-08-02 stsp
3650 eb41ed75 2019-02-05 stsp if (worktree) {
3651 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3652 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3653 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3654 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3655 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3656 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3657 eb41ed75 2019-02-05 stsp path) == -1) {
3658 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3659 eb41ed75 2019-02-05 stsp goto done;
3660 eb41ed75 2019-02-05 stsp }
3661 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3662 eb41ed75 2019-02-05 stsp free(p);
3663 eb41ed75 2019-02-05 stsp } else {
3664 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3665 eb41ed75 2019-02-05 stsp }
3666 eb41ed75 2019-02-05 stsp if (error)
3667 69069811 2018-08-02 stsp goto done;
3668 a70480e0 2018-06-23 stsp
3669 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
3670 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
3671 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3672 a70480e0 2018-06-23 stsp if (error != NULL)
3673 66b4983c 2018-06-23 stsp goto done;
3674 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3675 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
3676 a70480e0 2018-06-23 stsp } else {
3677 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
3678 15a94983 2018-12-23 stsp commit_id_str);
3679 a70480e0 2018-06-23 stsp }
3680 a19e88aa 2018-06-23 stsp if (error != NULL)
3681 8b473291 2019-02-21 stsp goto done;
3682 8b473291 2019-02-21 stsp
3683 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
3684 8b473291 2019-02-21 stsp if (error)
3685 a19e88aa 2018-06-23 stsp goto done;
3686 a70480e0 2018-06-23 stsp
3687 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3688 e1cd8fed 2018-08-01 stsp if (view == NULL) {
3689 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3690 e1cd8fed 2018-08-01 stsp goto done;
3691 e1cd8fed 2018-08-01 stsp }
3692 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3693 7cbe629d 2018-08-04 stsp if (error)
3694 7cbe629d 2018-08-04 stsp goto done;
3695 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3696 a70480e0 2018-06-23 stsp done:
3697 69069811 2018-08-02 stsp free(repo_path);
3698 69069811 2018-08-02 stsp free(cwd);
3699 a70480e0 2018-06-23 stsp free(commit_id);
3700 eb41ed75 2019-02-05 stsp if (worktree)
3701 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
3702 a70480e0 2018-06-23 stsp if (repo)
3703 a70480e0 2018-06-23 stsp got_repo_close(repo);
3704 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3705 a70480e0 2018-06-23 stsp return error;
3706 ffd1d5e5 2018-06-23 stsp }
3707 ffd1d5e5 2018-06-23 stsp
3708 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3709 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
3710 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
3711 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
3712 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
3713 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
3714 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
3715 ffd1d5e5 2018-06-23 stsp {
3716 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3717 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
3718 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
3719 ffd1d5e5 2018-06-23 stsp int width, n;
3720 ffd1d5e5 2018-06-23 stsp
3721 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
3722 ffd1d5e5 2018-06-23 stsp
3723 f7d12f7e 2018-08-01 stsp werase(view->window);
3724 ffd1d5e5 2018-06-23 stsp
3725 ffd1d5e5 2018-06-23 stsp if (limit == 0)
3726 ffd1d5e5 2018-06-23 stsp return NULL;
3727 ffd1d5e5 2018-06-23 stsp
3728 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
3729 ffd1d5e5 2018-06-23 stsp if (err)
3730 ffd1d5e5 2018-06-23 stsp return err;
3731 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3732 a3404814 2018-09-02 stsp wstandout(view->window);
3733 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3734 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3735 a3404814 2018-09-02 stsp wstandend(view->window);
3736 2550e4c3 2018-07-13 stsp free(wline);
3737 2550e4c3 2018-07-13 stsp wline = NULL;
3738 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3739 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3740 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3741 ffd1d5e5 2018-06-23 stsp return NULL;
3742 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
3743 ce52c690 2018-06-23 stsp if (err)
3744 ce52c690 2018-06-23 stsp return err;
3745 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3746 2550e4c3 2018-07-13 stsp free(wline);
3747 2550e4c3 2018-07-13 stsp wline = NULL;
3748 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3749 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3750 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3751 ffd1d5e5 2018-06-23 stsp return NULL;
3752 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3753 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
3754 a1eca9bb 2018-06-23 stsp return NULL;
3755 ffd1d5e5 2018-06-23 stsp
3756 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3757 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
3758 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
3759 0cf4efb1 2018-09-29 stsp if (view->focussed)
3760 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3761 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
3762 ffd1d5e5 2018-06-23 stsp }
3763 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
3764 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
3765 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3766 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3767 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3768 ffd1d5e5 2018-06-23 stsp return NULL;
3769 ffd1d5e5 2018-06-23 stsp n = 1;
3770 ffd1d5e5 2018-06-23 stsp } else {
3771 ffd1d5e5 2018-06-23 stsp n = 0;
3772 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
3773 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3774 ffd1d5e5 2018-06-23 stsp }
3775 ffd1d5e5 2018-06-23 stsp
3776 ffd1d5e5 2018-06-23 stsp while (te) {
3777 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
3778 1d13200f 2018-07-12 stsp
3779 1d13200f 2018-07-12 stsp if (show_ids) {
3780 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
3781 1d13200f 2018-07-12 stsp if (err)
3782 638f9024 2019-05-13 stsp return got_error_from_errno(
3783 230a42bd 2019-05-11 jcs "got_object_id_str");
3784 1d13200f 2018-07-12 stsp }
3785 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3786 d8355ef1 2019-02-10 stsp te->name, S_ISDIR(te->mode) ? "/" :
3787 d8355ef1 2019-02-10 stsp ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3788 1d13200f 2018-07-12 stsp free(id_str);
3789 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3790 1d13200f 2018-07-12 stsp }
3791 1d13200f 2018-07-12 stsp free(id_str);
3792 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
3793 ffd1d5e5 2018-06-23 stsp if (err) {
3794 ffd1d5e5 2018-06-23 stsp free(line);
3795 ffd1d5e5 2018-06-23 stsp break;
3796 ffd1d5e5 2018-06-23 stsp }
3797 ffd1d5e5 2018-06-23 stsp if (n == selected) {
3798 0cf4efb1 2018-09-29 stsp if (view->focussed)
3799 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3800 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
3801 ffd1d5e5 2018-06-23 stsp }
3802 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3803 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3804 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3805 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
3806 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3807 ffd1d5e5 2018-06-23 stsp free(line);
3808 2550e4c3 2018-07-13 stsp free(wline);
3809 2550e4c3 2018-07-13 stsp wline = NULL;
3810 ffd1d5e5 2018-06-23 stsp n++;
3811 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3812 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
3813 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3814 ffd1d5e5 2018-06-23 stsp break;
3815 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3816 ffd1d5e5 2018-06-23 stsp }
3817 ffd1d5e5 2018-06-23 stsp
3818 ffd1d5e5 2018-06-23 stsp return err;
3819 ffd1d5e5 2018-06-23 stsp }
3820 ffd1d5e5 2018-06-23 stsp
3821 ffd1d5e5 2018-06-23 stsp static void
3822 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
3823 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
3824 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
3825 ffd1d5e5 2018-06-23 stsp {
3826 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
3827 ffd1d5e5 2018-06-23 stsp int i;
3828 ffd1d5e5 2018-06-23 stsp
3829 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
3830 ffd1d5e5 2018-06-23 stsp return;
3831 ffd1d5e5 2018-06-23 stsp
3832 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3833 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
3834 ffd1d5e5 2018-06-23 stsp if (!isroot)
3835 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3836 ffd1d5e5 2018-06-23 stsp return;
3837 ffd1d5e5 2018-06-23 stsp }
3838 ffd1d5e5 2018-06-23 stsp
3839 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
3840 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
3841 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
3842 ffd1d5e5 2018-06-23 stsp prev = te;
3843 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3844 ffd1d5e5 2018-06-23 stsp }
3845 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
3846 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3847 ffd1d5e5 2018-06-23 stsp }
3848 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3849 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3850 ffd1d5e5 2018-06-23 stsp }
3851 ffd1d5e5 2018-06-23 stsp
3852 768394f3 2019-01-24 stsp static int
3853 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3854 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
3855 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
3856 ffd1d5e5 2018-06-23 stsp {
3857 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
3858 ffd1d5e5 2018-06-23 stsp int n = 0;
3859 ffd1d5e5 2018-06-23 stsp
3860 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
3861 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3862 ffd1d5e5 2018-06-23 stsp else
3863 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
3864 768394f3 2019-01-24 stsp last = last_displayed_entry;
3865 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
3866 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
3867 768394f3 2019-01-24 stsp if (last) {
3868 768394f3 2019-01-24 stsp *first_displayed_entry = next;
3869 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
3870 768394f3 2019-01-24 stsp }
3871 ffd1d5e5 2018-06-23 stsp }
3872 768394f3 2019-01-24 stsp return n;
3873 ffd1d5e5 2018-06-23 stsp }
3874 ffd1d5e5 2018-06-23 stsp
3875 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3876 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
3877 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
3878 ffd1d5e5 2018-06-23 stsp {
3879 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
3880 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
3881 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
3882 ffd1d5e5 2018-06-23 stsp
3883 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
3884 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
3885 ce52c690 2018-06-23 stsp if (te)
3886 ce52c690 2018-06-23 stsp len += strlen(te->name);
3887 ce52c690 2018-06-23 stsp
3888 ce52c690 2018-06-23 stsp *path = calloc(1, len);
3889 ffd1d5e5 2018-06-23 stsp if (path == NULL)
3890 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
3891 ffd1d5e5 2018-06-23 stsp
3892 ce52c690 2018-06-23 stsp (*path)[0] = '/';
3893 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
3894 d9765a41 2018-06-23 stsp while (pt) {
3895 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3896 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3897 cb2ebc8a 2018-06-23 stsp goto done;
3898 cb2ebc8a 2018-06-23 stsp }
3899 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
3900 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3901 cb2ebc8a 2018-06-23 stsp goto done;
3902 cb2ebc8a 2018-06-23 stsp }
3903 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3904 ffd1d5e5 2018-06-23 stsp }
3905 ce52c690 2018-06-23 stsp if (te) {
3906 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3907 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3908 ce52c690 2018-06-23 stsp goto done;
3909 ce52c690 2018-06-23 stsp }
3910 cb2ebc8a 2018-06-23 stsp }
3911 ce52c690 2018-06-23 stsp done:
3912 ce52c690 2018-06-23 stsp if (err) {
3913 ce52c690 2018-06-23 stsp free(*path);
3914 ce52c690 2018-06-23 stsp *path = NULL;
3915 ce52c690 2018-06-23 stsp }
3916 ce52c690 2018-06-23 stsp return err;
3917 ce52c690 2018-06-23 stsp }
3918 ce52c690 2018-06-23 stsp
3919 ce52c690 2018-06-23 stsp static const struct got_error *
3920 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3921 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3922 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3923 8b473291 2019-02-21 stsp struct got_repository *repo)
3924 ce52c690 2018-06-23 stsp {
3925 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3926 ce52c690 2018-06-23 stsp char *path;
3927 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3928 69efd4c4 2018-07-18 stsp
3929 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3930 ce52c690 2018-06-23 stsp if (err)
3931 ce52c690 2018-06-23 stsp return err;
3932 ffd1d5e5 2018-06-23 stsp
3933 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3934 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3935 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3936 cdf1ee82 2018-08-01 stsp
3937 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
3938 e5a0f69f 2018-08-18 stsp if (err) {
3939 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3940 e5a0f69f 2018-08-18 stsp free(path);
3941 e5a0f69f 2018-08-18 stsp } else
3942 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3943 69efd4c4 2018-07-18 stsp return err;
3944 69efd4c4 2018-07-18 stsp }
3945 69efd4c4 2018-07-18 stsp
3946 69efd4c4 2018-07-18 stsp static const struct got_error *
3947 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3948 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3949 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3950 8b473291 2019-02-21 stsp struct got_repository *repo)
3951 69efd4c4 2018-07-18 stsp {
3952 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3953 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3954 69efd4c4 2018-07-18 stsp char *path;
3955 69efd4c4 2018-07-18 stsp
3956 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3957 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3958 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
3959 e5a0f69f 2018-08-18 stsp
3960 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3961 69efd4c4 2018-07-18 stsp if (err)
3962 69efd4c4 2018-07-18 stsp return err;
3963 69efd4c4 2018-07-18 stsp
3964 d01904d4 2019-06-25 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
3965 ba4f502b 2018-08-04 stsp if (err)
3966 e5a0f69f 2018-08-18 stsp view_close(log_view);
3967 e5a0f69f 2018-08-18 stsp else
3968 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3969 cb2ebc8a 2018-06-23 stsp free(path);
3970 cb2ebc8a 2018-06-23 stsp return err;
3971 ffd1d5e5 2018-06-23 stsp }
3972 ffd1d5e5 2018-06-23 stsp
3973 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3974 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3975 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3976 8b473291 2019-02-21 stsp struct got_repository *repo)
3977 ffd1d5e5 2018-06-23 stsp {
3978 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3979 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3980 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3981 ffd1d5e5 2018-06-23 stsp
3982 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3983 ffd1d5e5 2018-06-23 stsp
3984 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3985 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3986 ffd1d5e5 2018-06-23 stsp goto done;
3987 ffd1d5e5 2018-06-23 stsp
3988 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3989 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3990 ffd1d5e5 2018-06-23 stsp goto done;
3991 ffd1d5e5 2018-06-23 stsp }
3992 ffd1d5e5 2018-06-23 stsp
3993 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3994 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3995 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3996 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3997 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3998 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3999 6484ec90 2018-09-29 stsp goto done;
4000 6484ec90 2018-09-29 stsp }
4001 8b473291 2019-02-21 stsp s->refs = refs;
4002 fb2756b9 2018-08-04 stsp s->repo = repo;
4003 e5a0f69f 2018-08-18 stsp
4004 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
4005 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
4006 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
4007 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
4008 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
4009 ad80ab7b 2018-08-04 stsp done:
4010 ad80ab7b 2018-08-04 stsp free(commit_id_str);
4011 6484ec90 2018-09-29 stsp if (err) {
4012 fb2756b9 2018-08-04 stsp free(s->tree_label);
4013 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4014 6484ec90 2018-09-29 stsp }
4015 ad80ab7b 2018-08-04 stsp return err;
4016 ad80ab7b 2018-08-04 stsp }
4017 ad80ab7b 2018-08-04 stsp
4018 e5a0f69f 2018-08-18 stsp static const struct got_error *
4019 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
4020 ad80ab7b 2018-08-04 stsp {
4021 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4022 ad80ab7b 2018-08-04 stsp
4023 fb2756b9 2018-08-04 stsp free(s->tree_label);
4024 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4025 6484ec90 2018-09-29 stsp free(s->commit_id);
4026 6484ec90 2018-09-29 stsp s->commit_id = NULL;
4027 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
4028 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
4029 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
4030 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
4031 ad80ab7b 2018-08-04 stsp free(parent);
4032 ad80ab7b 2018-08-04 stsp
4033 ad80ab7b 2018-08-04 stsp }
4034 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
4035 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
4036 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
4037 7c32bd05 2019-06-22 stsp
4038 7c32bd05 2019-06-22 stsp return NULL;
4039 7c32bd05 2019-06-22 stsp }
4040 7c32bd05 2019-06-22 stsp
4041 7c32bd05 2019-06-22 stsp static const struct got_error *
4042 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
4043 7c32bd05 2019-06-22 stsp {
4044 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4045 7c32bd05 2019-06-22 stsp
4046 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
4047 7c32bd05 2019-06-22 stsp return NULL;
4048 7c32bd05 2019-06-22 stsp }
4049 7c32bd05 2019-06-22 stsp
4050 7c32bd05 2019-06-22 stsp static int
4051 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4052 7c32bd05 2019-06-22 stsp {
4053 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
4054 7c32bd05 2019-06-22 stsp
4055 7c32bd05 2019-06-22 stsp return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4056 7c32bd05 2019-06-22 stsp }
4057 7c32bd05 2019-06-22 stsp
4058 7c32bd05 2019-06-22 stsp static const struct got_error *
4059 7c32bd05 2019-06-22 stsp search_next_tree_view(struct tog_view *view)
4060 7c32bd05 2019-06-22 stsp {
4061 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4062 7c32bd05 2019-06-22 stsp struct got_tree_entry *entry, *te;
4063 7c32bd05 2019-06-22 stsp
4064 7c32bd05 2019-06-22 stsp if (!view->searching) {
4065 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4066 7c32bd05 2019-06-22 stsp return NULL;
4067 7c32bd05 2019-06-22 stsp }
4068 7c32bd05 2019-06-22 stsp
4069 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4070 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
4071 7c32bd05 2019-06-22 stsp if (s->selected_entry)
4072 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4073 7c32bd05 2019-06-22 stsp else
4074 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4075 7c32bd05 2019-06-22 stsp }
4076 7c32bd05 2019-06-22 stsp else {
4077 7c32bd05 2019-06-22 stsp if (s->selected_entry == NULL) {
4078 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4079 7c32bd05 2019-06-22 stsp entry = te;
4080 7c32bd05 2019-06-22 stsp } else {
4081 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4082 7c32bd05 2019-06-22 stsp entry = te;
4083 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) ==
4084 7c32bd05 2019-06-22 stsp s->selected_entry)
4085 7c32bd05 2019-06-22 stsp break;
4086 7c32bd05 2019-06-22 stsp }
4087 7c32bd05 2019-06-22 stsp }
4088 7c32bd05 2019-06-22 stsp }
4089 7c32bd05 2019-06-22 stsp } else {
4090 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4091 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
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 }
4096 7c32bd05 2019-06-22 stsp }
4097 7c32bd05 2019-06-22 stsp
4098 7c32bd05 2019-06-22 stsp while (1) {
4099 7c32bd05 2019-06-22 stsp if (entry == NULL) {
4100 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
4101 ac66afb8 2019-06-24 stsp view->search_next_done = 1;
4102 ac66afb8 2019-06-24 stsp return NULL;
4103 ac66afb8 2019-06-24 stsp }
4104 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4105 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4106 7c32bd05 2019-06-22 stsp else {
4107 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4108 7c32bd05 2019-06-22 stsp entry = te;
4109 7c32bd05 2019-06-22 stsp }
4110 7c32bd05 2019-06-22 stsp }
4111 7c32bd05 2019-06-22 stsp
4112 7c32bd05 2019-06-22 stsp if (match_tree_entry(entry, &view->regex)) {
4113 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4114 7c32bd05 2019-06-22 stsp s->matched_entry = entry;
4115 7c32bd05 2019-06-22 stsp break;
4116 7c32bd05 2019-06-22 stsp }
4117 7c32bd05 2019-06-22 stsp
4118 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4119 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(entry, entry);
4120 7c32bd05 2019-06-22 stsp else {
4121 7c32bd05 2019-06-22 stsp if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4122 7c32bd05 2019-06-22 stsp entry = NULL;
4123 7c32bd05 2019-06-22 stsp else {
4124 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4125 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) == entry) {
4126 7c32bd05 2019-06-22 stsp entry = te;
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 }
4131 7c32bd05 2019-06-22 stsp }
4132 7c32bd05 2019-06-22 stsp }
4133 e5a0f69f 2018-08-18 stsp
4134 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4135 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
4136 7c32bd05 2019-06-22 stsp s->selected = 0;
4137 7c32bd05 2019-06-22 stsp }
4138 7c32bd05 2019-06-22 stsp
4139 e5a0f69f 2018-08-18 stsp return NULL;
4140 ad80ab7b 2018-08-04 stsp }
4141 ad80ab7b 2018-08-04 stsp
4142 ad80ab7b 2018-08-04 stsp static const struct got_error *
4143 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
4144 ad80ab7b 2018-08-04 stsp {
4145 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
4146 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4147 e5a0f69f 2018-08-18 stsp char *parent_path;
4148 ad80ab7b 2018-08-04 stsp
4149 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
4150 e5a0f69f 2018-08-18 stsp if (err)
4151 e5a0f69f 2018-08-18 stsp return err;
4152 ffd1d5e5 2018-06-23 stsp
4153 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
4154 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
4155 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4156 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
4157 e5a0f69f 2018-08-18 stsp free(parent_path);
4158 669b5ffa 2018-10-07 stsp
4159 669b5ffa 2018-10-07 stsp view_vborder(view);
4160 e5a0f69f 2018-08-18 stsp return err;
4161 e5a0f69f 2018-08-18 stsp }
4162 ce52c690 2018-06-23 stsp
4163 e5a0f69f 2018-08-18 stsp static const struct got_error *
4164 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4165 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4166 e5a0f69f 2018-08-18 stsp {
4167 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4168 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
4169 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
4170 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
4171 ffd1d5e5 2018-06-23 stsp
4172 e5a0f69f 2018-08-18 stsp switch (ch) {
4173 1e37a5c2 2019-05-12 jcs case 'i':
4174 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
4175 1e37a5c2 2019-05-12 jcs break;
4176 1e37a5c2 2019-05-12 jcs case 'l':
4177 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
4178 ffd1d5e5 2018-06-23 stsp break;
4179 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4180 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4181 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
4182 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
4183 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
4184 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4185 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4186 1e37a5c2 2019-05-12 jcs if (err)
4187 1e37a5c2 2019-05-12 jcs return err;
4188 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
4189 1e37a5c2 2019-05-12 jcs if (err) {
4190 1e37a5c2 2019-05-12 jcs view_close(log_view);
4191 669b5ffa 2018-10-07 stsp break;
4192 1e37a5c2 2019-05-12 jcs }
4193 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
4194 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4195 1e37a5c2 2019-05-12 jcs } else
4196 1e37a5c2 2019-05-12 jcs *new_view = log_view;
4197 1e37a5c2 2019-05-12 jcs break;
4198 1e37a5c2 2019-05-12 jcs case 'k':
4199 1e37a5c2 2019-05-12 jcs case KEY_UP:
4200 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
4201 1e37a5c2 2019-05-12 jcs s->selected--;
4202 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
4203 1e37a5c2 2019-05-12 jcs break;
4204 1e37a5c2 2019-05-12 jcs }
4205 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
4206 1e37a5c2 2019-05-12 jcs break;
4207 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
4208 1e37a5c2 2019-05-12 jcs s->entries, s->tree == s->root);
4209 1e37a5c2 2019-05-12 jcs break;
4210 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4211 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
4212 1e37a5c2 2019-05-12 jcs MAX(0, view->nlines - 4 - s->selected), s->entries,
4213 1e37a5c2 2019-05-12 jcs s->tree == s->root);
4214 1e37a5c2 2019-05-12 jcs s->selected = 0;
4215 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_FIRST(&s->entries->head) ==
4216 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
4217 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
4218 1e37a5c2 2019-05-12 jcs break;
4219 1e37a5c2 2019-05-12 jcs case 'j':
4220 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4221 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
4222 1e37a5c2 2019-05-12 jcs s->selected++;
4223 1e37a5c2 2019-05-12 jcs break;
4224 1e37a5c2 2019-05-12 jcs }
4225 00ba99a7 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4226 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
4227 1e37a5c2 2019-05-12 jcs break;
4228 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
4229 1e37a5c2 2019-05-12 jcs s->last_displayed_entry, s->entries);
4230 1e37a5c2 2019-05-12 jcs break;
4231 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4232 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4233 1e37a5c2 2019-05-12 jcs == NULL) {
4234 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
4235 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
4236 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4237 1e37a5c2 2019-05-12 jcs break;
4238 1e37a5c2 2019-05-12 jcs }
4239 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
4240 1e37a5c2 2019-05-12 jcs view->nlines, s->last_displayed_entry, s->entries);
4241 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
4242 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
4243 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
4244 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
4245 1e37a5c2 2019-05-12 jcs do {
4246 1e37a5c2 2019-05-12 jcs ndisplayed++;
4247 1e37a5c2 2019-05-12 jcs te = SIMPLEQ_NEXT(te, entry);
4248 1e37a5c2 2019-05-12 jcs } while (te);
4249 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
4250 1e37a5c2 2019-05-12 jcs }
4251 1e37a5c2 2019-05-12 jcs break;
4252 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4253 1e37a5c2 2019-05-12 jcs case '\r':
4254 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4255 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4256 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
4257 1e37a5c2 2019-05-12 jcs /* user selected '..' */
4258 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
4259 1e37a5c2 2019-05-12 jcs break;
4260 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
4261 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
4262 1e37a5c2 2019-05-12 jcs entry);
4263 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
4264 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
4265 1e37a5c2 2019-05-12 jcs s->entries =
4266 1e37a5c2 2019-05-12 jcs got_object_tree_get_entries(s->tree);
4267 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
4268 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
4269 1e37a5c2 2019-05-12 jcs s->selected_entry =
4270 1e37a5c2 2019-05-12 jcs parent->selected_entry;
4271 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
4272 1e37a5c2 2019-05-12 jcs free(parent);
4273 1e37a5c2 2019-05-12 jcs } else if (S_ISDIR(s->selected_entry->mode)) {
4274 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
4275 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&subtree,
4276 1e37a5c2 2019-05-12 jcs s->repo, s->selected_entry->id);
4277 1e37a5c2 2019-05-12 jcs if (err)
4278 1e37a5c2 2019-05-12 jcs break;
4279 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
4280 941e9f74 2019-05-21 stsp if (err) {
4281 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
4282 1e37a5c2 2019-05-12 jcs break;
4283 1e37a5c2 2019-05-12 jcs }
4284 1e37a5c2 2019-05-12 jcs } else if (S_ISREG(s->selected_entry->mode)) {
4285 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
4286 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
4287 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
4288 1e37a5c2 2019-05-12 jcs
4289 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
4290 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
4291 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
4292 1e37a5c2 2019-05-12 jcs if (err)
4293 1e37a5c2 2019-05-12 jcs break;
4294 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
4295 669b5ffa 2018-10-07 stsp err = view_close_child(view);
4296 669b5ffa 2018-10-07 stsp if (err)
4297 669b5ffa 2018-10-07 stsp return err;
4298 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
4299 669b5ffa 2018-10-07 stsp if (err) {
4300 1e37a5c2 2019-05-12 jcs view_close(blame_view);
4301 669b5ffa 2018-10-07 stsp break;
4302 669b5ffa 2018-10-07 stsp }
4303 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
4304 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
4305 669b5ffa 2018-10-07 stsp } else
4306 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
4307 1e37a5c2 2019-05-12 jcs }
4308 1e37a5c2 2019-05-12 jcs break;
4309 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4310 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
4311 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4312 1e37a5c2 2019-05-12 jcs break;
4313 1e37a5c2 2019-05-12 jcs default:
4314 1e37a5c2 2019-05-12 jcs break;
4315 ffd1d5e5 2018-06-23 stsp }
4316 e5a0f69f 2018-08-18 stsp
4317 ffd1d5e5 2018-06-23 stsp return err;
4318 9f7d7167 2018-04-29 stsp }
4319 9f7d7167 2018-04-29 stsp
4320 ffd1d5e5 2018-06-23 stsp __dead static void
4321 ffd1d5e5 2018-06-23 stsp usage_tree(void)
4322 ffd1d5e5 2018-06-23 stsp {
4323 ffd1d5e5 2018-06-23 stsp endwin();
4324 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4325 ffd1d5e5 2018-06-23 stsp getprogname());
4326 ffd1d5e5 2018-06-23 stsp exit(1);
4327 ffd1d5e5 2018-06-23 stsp }
4328 ffd1d5e5 2018-06-23 stsp
4329 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4330 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
4331 ffd1d5e5 2018-06-23 stsp {
4332 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
4333 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
4334 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4335 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
4336 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4337 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
4338 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
4339 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
4340 ffd1d5e5 2018-06-23 stsp int ch;
4341 5221c383 2018-08-01 stsp struct tog_view *view;
4342 70ac5f84 2019-03-28 stsp
4343 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4344 ffd1d5e5 2018-06-23 stsp
4345 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
4346 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4347 d188b9a6 2019-01-04 stsp NULL) == -1)
4348 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
4349 ffd1d5e5 2018-06-23 stsp #endif
4350 ffd1d5e5 2018-06-23 stsp
4351 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
4352 ffd1d5e5 2018-06-23 stsp switch (ch) {
4353 ffd1d5e5 2018-06-23 stsp case 'c':
4354 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
4355 ffd1d5e5 2018-06-23 stsp break;
4356 ffd1d5e5 2018-06-23 stsp default:
4357 17020d27 2019-03-07 stsp usage_tree();
4358 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
4359 ffd1d5e5 2018-06-23 stsp }
4360 ffd1d5e5 2018-06-23 stsp }
4361 ffd1d5e5 2018-06-23 stsp
4362 ffd1d5e5 2018-06-23 stsp argc -= optind;
4363 ffd1d5e5 2018-06-23 stsp argv += optind;
4364 ffd1d5e5 2018-06-23 stsp
4365 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
4366 52185f70 2019-02-05 stsp struct got_worktree *worktree;
4367 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
4368 52185f70 2019-02-05 stsp if (cwd == NULL)
4369 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
4370 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4371 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4372 52185f70 2019-02-05 stsp goto done;
4373 52185f70 2019-02-05 stsp if (worktree) {
4374 52185f70 2019-02-05 stsp free(cwd);
4375 52185f70 2019-02-05 stsp repo_path =
4376 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4377 52185f70 2019-02-05 stsp got_worktree_close(worktree);
4378 52185f70 2019-02-05 stsp } else
4379 52185f70 2019-02-05 stsp repo_path = cwd;
4380 52185f70 2019-02-05 stsp if (repo_path == NULL) {
4381 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4382 52185f70 2019-02-05 stsp goto done;
4383 52185f70 2019-02-05 stsp }
4384 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
4385 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
4386 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
4387 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
4388 ffd1d5e5 2018-06-23 stsp } else
4389 ffd1d5e5 2018-06-23 stsp usage_log();
4390 a915003a 2019-02-05 stsp
4391 a915003a 2019-02-05 stsp init_curses();
4392 ffd1d5e5 2018-06-23 stsp
4393 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
4394 c02c541e 2019-03-29 stsp if (error != NULL)
4395 52185f70 2019-02-05 stsp goto done;
4396 d188b9a6 2019-01-04 stsp
4397 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4398 c02c541e 2019-03-29 stsp if (error)
4399 52185f70 2019-02-05 stsp goto done;
4400 ffd1d5e5 2018-06-23 stsp
4401 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
4402 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4403 15a94983 2018-12-23 stsp else
4404 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
4405 15a94983 2018-12-23 stsp commit_id_arg);
4406 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4407 ffd1d5e5 2018-06-23 stsp goto done;
4408 ffd1d5e5 2018-06-23 stsp
4409 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4410 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4411 ffd1d5e5 2018-06-23 stsp goto done;
4412 ffd1d5e5 2018-06-23 stsp
4413 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
4414 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
4415 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4416 8b473291 2019-02-21 stsp goto done;
4417 8b473291 2019-02-21 stsp
4418 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
4419 8b473291 2019-02-21 stsp if (error)
4420 ffd1d5e5 2018-06-23 stsp goto done;
4421 ffd1d5e5 2018-06-23 stsp
4422 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4423 5221c383 2018-08-01 stsp if (view == NULL) {
4424 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4425 5221c383 2018-08-01 stsp goto done;
4426 5221c383 2018-08-01 stsp }
4427 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
4428 ad80ab7b 2018-08-04 stsp if (error)
4429 ad80ab7b 2018-08-04 stsp goto done;
4430 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4431 ffd1d5e5 2018-06-23 stsp done:
4432 52185f70 2019-02-05 stsp free(repo_path);
4433 ffd1d5e5 2018-06-23 stsp free(commit_id);
4434 ffd1d5e5 2018-06-23 stsp if (commit)
4435 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
4436 ffd1d5e5 2018-06-23 stsp if (tree)
4437 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
4438 ffd1d5e5 2018-06-23 stsp if (repo)
4439 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
4440 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4441 ffd1d5e5 2018-06-23 stsp return error;
4442 9f7d7167 2018-04-29 stsp }
4443 9f7d7167 2018-04-29 stsp
4444 4ed7e80c 2018-05-20 stsp __dead static void
4445 9f7d7167 2018-04-29 stsp usage(void)
4446 9f7d7167 2018-04-29 stsp {
4447 5e070240 2019-06-22 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n", getprogname());
4448 9f7d7167 2018-04-29 stsp exit(1);
4449 9f7d7167 2018-04-29 stsp }
4450 9f7d7167 2018-04-29 stsp
4451 c2301be8 2018-04-30 stsp static char **
4452 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
4453 c2301be8 2018-04-30 stsp {
4454 c2301be8 2018-04-30 stsp char **argv;
4455 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
4456 c2301be8 2018-04-30 stsp
4457 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
4458 c2301be8 2018-04-30 stsp if (argv == NULL)
4459 c2301be8 2018-04-30 stsp err(1, "calloc");
4460 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
4461 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
4462 c2301be8 2018-04-30 stsp err(1, "calloc");
4463 c2301be8 2018-04-30 stsp if (arg1) {
4464 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
4465 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
4466 c2301be8 2018-04-30 stsp err(1, "calloc");
4467 c2301be8 2018-04-30 stsp }
4468 c2301be8 2018-04-30 stsp
4469 c2301be8 2018-04-30 stsp return argv;
4470 c2301be8 2018-04-30 stsp }
4471 c2301be8 2018-04-30 stsp
4472 9f7d7167 2018-04-29 stsp int
4473 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
4474 9f7d7167 2018-04-29 stsp {
4475 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
4476 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
4477 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
4478 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
4479 9f7d7167 2018-04-29 stsp
4480 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
4481 9f7d7167 2018-04-29 stsp
4482 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
4483 9f7d7167 2018-04-29 stsp switch (ch) {
4484 9f7d7167 2018-04-29 stsp case 'h':
4485 9f7d7167 2018-04-29 stsp hflag = 1;
4486 9f7d7167 2018-04-29 stsp break;
4487 9f7d7167 2018-04-29 stsp default:
4488 9f7d7167 2018-04-29 stsp usage();
4489 9f7d7167 2018-04-29 stsp /* NOTREACHED */
4490 9f7d7167 2018-04-29 stsp }
4491 9f7d7167 2018-04-29 stsp }
4492 9f7d7167 2018-04-29 stsp
4493 9f7d7167 2018-04-29 stsp argc -= optind;
4494 9f7d7167 2018-04-29 stsp argv += optind;
4495 9f7d7167 2018-04-29 stsp optind = 0;
4496 c2301be8 2018-04-30 stsp optreset = 1;
4497 9f7d7167 2018-04-29 stsp
4498 c2301be8 2018-04-30 stsp if (argc == 0) {
4499 f29d3e89 2018-06-23 stsp if (hflag)
4500 f29d3e89 2018-06-23 stsp usage();
4501 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
4502 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
4503 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
4504 c2301be8 2018-04-30 stsp argc = 1;
4505 c2301be8 2018-04-30 stsp } else {
4506 9f7d7167 2018-04-29 stsp int i;
4507 9f7d7167 2018-04-29 stsp
4508 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
4509 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
4510 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
4511 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
4512 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
4513 9f7d7167 2018-04-29 stsp if (hflag)
4514 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
4515 9f7d7167 2018-04-29 stsp break;
4516 9f7d7167 2018-04-29 stsp }
4517 9f7d7167 2018-04-29 stsp }
4518 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
4519 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
4520 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
4521 c2301be8 2018-04-30 stsp if (repo_path) {
4522 c2301be8 2018-04-30 stsp struct got_repository *repo;
4523 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
4524 c2301be8 2018-04-30 stsp if (error == NULL)
4525 c2301be8 2018-04-30 stsp got_repo_close(repo);
4526 c2301be8 2018-04-30 stsp } else
4527 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath",
4528 230a42bd 2019-05-11 jcs argv[0]);
4529 c2301be8 2018-04-30 stsp if (error) {
4530 f29d3e89 2018-06-23 stsp if (hflag) {
4531 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
4532 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
4533 f29d3e89 2018-06-23 stsp argv[0]);
4534 f29d3e89 2018-06-23 stsp usage();
4535 f29d3e89 2018-06-23 stsp }
4536 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
4537 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
4538 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
4539 ad7de8d9 2018-04-30 stsp free(repo_path);
4540 c2301be8 2018-04-30 stsp return 1;
4541 c2301be8 2018-04-30 stsp }
4542 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
4543 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
4544 c2301be8 2018-04-30 stsp argc = 2;
4545 c2301be8 2018-04-30 stsp free(repo_path);
4546 9f7d7167 2018-04-29 stsp }
4547 9f7d7167 2018-04-29 stsp }
4548 9f7d7167 2018-04-29 stsp
4549 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4550 9f7d7167 2018-04-29 stsp if (error)
4551 9f7d7167 2018-04-29 stsp goto done;
4552 9f7d7167 2018-04-29 stsp done:
4553 9f7d7167 2018-04-29 stsp endwin();
4554 c2301be8 2018-04-30 stsp free(cmd_argv);
4555 9f7d7167 2018-04-29 stsp if (error)
4556 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4557 9f7d7167 2018-04-29 stsp return 0;
4558 9f7d7167 2018-04-29 stsp }