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