Blame


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