Blame


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