Blame


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