Blame


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