Blame


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