Blame


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