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 941e9f74 2019-05-21 stsp while (*p) {
1546 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1547 941e9f74 2019-05-21 stsp
1548 941e9f74 2019-05-21 stsp while (p[0] == '/')
1549 941e9f74 2019-05-21 stsp p++;
1550 941e9f74 2019-05-21 stsp
1551 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1552 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1553 941e9f74 2019-05-21 stsp if (slash == NULL)
1554 941e9f74 2019-05-21 stsp slash = strchr(p, '\0');
1555 941e9f74 2019-05-21 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1556 941e9f74 2019-05-21 stsp if (strncmp(p, te->name, slash - p) == 0) {
1557 941e9f74 2019-05-21 stsp s->selected_entry = te;
1558 941e9f74 2019-05-21 stsp break;
1559 941e9f74 2019-05-21 stsp }
1560 b03c880f 2019-05-21 stsp s->selected++;
1561 941e9f74 2019-05-21 stsp }
1562 941e9f74 2019-05-21 stsp if (s->selected_entry == NULL) {
1563 941e9f74 2019-05-21 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1564 941e9f74 2019-05-21 stsp break;
1565 941e9f74 2019-05-21 stsp }
1566 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1567 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1568 941e9f74 2019-05-21 stsp
1569 67409a31 2019-05-24 stsp if (!S_ISDIR(s->selected_entry->mode)) {
1570 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1571 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1572 67409a31 2019-05-24 stsp s->selected = 0;
1573 b03c880f 2019-05-21 stsp break;
1574 67409a31 2019-05-24 stsp }
1575 b03c880f 2019-05-21 stsp
1576 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1577 941e9f74 2019-05-21 stsp if (slash)
1578 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1579 941e9f74 2019-05-21 stsp else
1580 941e9f74 2019-05-21 stsp subpath = strdup(path);
1581 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1582 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1583 941e9f74 2019-05-21 stsp break;
1584 941e9f74 2019-05-21 stsp }
1585 941e9f74 2019-05-21 stsp
1586 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1587 941e9f74 2019-05-21 stsp subpath);
1588 941e9f74 2019-05-21 stsp if (err)
1589 941e9f74 2019-05-21 stsp break;
1590 941e9f74 2019-05-21 stsp
1591 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1592 941e9f74 2019-05-21 stsp free(tree_id);
1593 941e9f74 2019-05-21 stsp if (err)
1594 941e9f74 2019-05-21 stsp break;
1595 941e9f74 2019-05-21 stsp
1596 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1597 941e9f74 2019-05-21 stsp if (err) {
1598 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1599 941e9f74 2019-05-21 stsp break;
1600 941e9f74 2019-05-21 stsp }
1601 941e9f74 2019-05-21 stsp if (slash == NULL)
1602 941e9f74 2019-05-21 stsp break;
1603 941e9f74 2019-05-21 stsp free(subpath);
1604 941e9f74 2019-05-21 stsp subpath = NULL;
1605 941e9f74 2019-05-21 stsp p = slash;
1606 941e9f74 2019-05-21 stsp }
1607 941e9f74 2019-05-21 stsp
1608 941e9f74 2019-05-21 stsp free(subpath);
1609 1a76625f 2018-10-22 stsp return err;
1610 1a76625f 2018-10-22 stsp }
1611 1a76625f 2018-10-22 stsp
1612 1a76625f 2018-10-22 stsp static void *
1613 1a76625f 2018-10-22 stsp log_thread(void *arg)
1614 1a76625f 2018-10-22 stsp {
1615 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1616 1a76625f 2018-10-22 stsp int errcode = 0;
1617 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1618 1a76625f 2018-10-22 stsp int done = 0;
1619 1a76625f 2018-10-22 stsp
1620 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1621 1a76625f 2018-10-22 stsp if (err)
1622 1a76625f 2018-10-22 stsp return (void *)err;
1623 1a76625f 2018-10-22 stsp
1624 d264cece 2019-08-12 stsp while (!done && !err && !tog_sigpipe_received) {
1625 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1626 1a76625f 2018-10-22 stsp a->in_repo_path);
1627 1a76625f 2018-10-22 stsp if (err) {
1628 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1629 1a76625f 2018-10-22 stsp return (void *)err;
1630 1a76625f 2018-10-22 stsp err = NULL;
1631 1a76625f 2018-10-22 stsp done = 1;
1632 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1633 1a76625f 2018-10-22 stsp a->commits_needed--;
1634 1a76625f 2018-10-22 stsp
1635 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1636 3abe8080 2019-04-10 stsp if (errcode) {
1637 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1638 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1639 3abe8080 2019-04-10 stsp break;
1640 3abe8080 2019-04-10 stsp } else if (*a->quit)
1641 1a76625f 2018-10-22 stsp done = 1;
1642 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1643 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1644 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1645 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1646 1a76625f 2018-10-22 stsp }
1647 1a76625f 2018-10-22 stsp
1648 1a76625f 2018-10-22 stsp if (done)
1649 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1650 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1651 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1652 1a76625f 2018-10-22 stsp &tog_mutex);
1653 1a76625f 2018-10-22 stsp if (errcode)
1654 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1655 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1656 1a76625f 2018-10-22 stsp }
1657 1a76625f 2018-10-22 stsp
1658 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1659 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1660 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1661 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1662 1a76625f 2018-10-22 stsp }
1663 3abe8080 2019-04-10 stsp a->log_complete = 1;
1664 1a76625f 2018-10-22 stsp return (void *)err;
1665 1a76625f 2018-10-22 stsp }
1666 1a76625f 2018-10-22 stsp
1667 1a76625f 2018-10-22 stsp static const struct got_error *
1668 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1669 1a76625f 2018-10-22 stsp {
1670 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1671 1a76625f 2018-10-22 stsp int errcode;
1672 1a76625f 2018-10-22 stsp
1673 1a76625f 2018-10-22 stsp if (s->thread) {
1674 1a76625f 2018-10-22 stsp s->quit = 1;
1675 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1676 1a76625f 2018-10-22 stsp if (errcode)
1677 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1678 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1679 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1680 1a76625f 2018-10-22 stsp if (errcode)
1681 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1682 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1683 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1684 1a76625f 2018-10-22 stsp if (errcode)
1685 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1686 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1687 1a76625f 2018-10-22 stsp if (errcode)
1688 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1689 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1690 1a76625f 2018-10-22 stsp s->thread = NULL;
1691 1a76625f 2018-10-22 stsp }
1692 1a76625f 2018-10-22 stsp
1693 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1694 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1695 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1696 1a76625f 2018-10-22 stsp
1697 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1698 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1699 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1700 1a76625f 2018-10-22 stsp }
1701 1a76625f 2018-10-22 stsp
1702 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1703 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1704 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1705 1a76625f 2018-10-22 stsp }
1706 1a76625f 2018-10-22 stsp
1707 9343a5fb 2018-06-23 stsp return err;
1708 9343a5fb 2018-06-23 stsp }
1709 9343a5fb 2018-06-23 stsp
1710 9343a5fb 2018-06-23 stsp static const struct got_error *
1711 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1712 1a76625f 2018-10-22 stsp {
1713 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1714 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1715 1a76625f 2018-10-22 stsp
1716 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1717 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1718 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1719 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1720 1a76625f 2018-10-22 stsp free(s->start_id);
1721 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1722 1a76625f 2018-10-22 stsp return err;
1723 1a76625f 2018-10-22 stsp }
1724 1a76625f 2018-10-22 stsp
1725 1a76625f 2018-10-22 stsp static const struct got_error *
1726 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
1727 60493ae3 2019-06-20 stsp {
1728 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1729 60493ae3 2019-06-20 stsp
1730 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
1731 96e2b566 2019-07-08 stsp s->search_entry = NULL;
1732 60493ae3 2019-06-20 stsp return NULL;
1733 60493ae3 2019-06-20 stsp }
1734 60493ae3 2019-06-20 stsp
1735 60493ae3 2019-06-20 stsp static int
1736 df0b3d8a 2019-06-28 stsp match_commit(struct got_commit_object *commit, const char *id_str,
1737 df0b3d8a 2019-06-28 stsp regex_t *regex)
1738 60493ae3 2019-06-20 stsp {
1739 60493ae3 2019-06-20 stsp regmatch_t regmatch;
1740 60493ae3 2019-06-20 stsp
1741 60493ae3 2019-06-20 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1742 60493ae3 2019-06-20 stsp &regmatch, 0) == 0 ||
1743 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1744 60493ae3 2019-06-20 stsp &regmatch, 0) == 0 ||
1745 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_logmsg(commit), 1,
1746 df0b3d8a 2019-06-28 stsp &regmatch, 0) == 0 ||
1747 df0b3d8a 2019-06-28 stsp regexec(regex, id_str, 1, &regmatch, 0) == 0)
1748 60493ae3 2019-06-20 stsp return 1;
1749 60493ae3 2019-06-20 stsp
1750 60493ae3 2019-06-20 stsp return 0;
1751 60493ae3 2019-06-20 stsp }
1752 60493ae3 2019-06-20 stsp
1753 60493ae3 2019-06-20 stsp static const struct got_error *
1754 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
1755 60493ae3 2019-06-20 stsp {
1756 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
1757 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1758 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
1759 60493ae3 2019-06-20 stsp
1760 60493ae3 2019-06-20 stsp if (!view->searching) {
1761 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1762 60493ae3 2019-06-20 stsp return NULL;
1763 60493ae3 2019-06-20 stsp }
1764 60493ae3 2019-06-20 stsp
1765 96e2b566 2019-07-08 stsp if (s->search_entry) {
1766 678cbce5 2019-07-28 stsp if (wgetch(view->window) == KEY_BACKSPACE) {
1767 678cbce5 2019-07-28 stsp view->search_next_done = 1;
1768 678cbce5 2019-07-28 stsp return NULL;
1769 678cbce5 2019-07-28 stsp }
1770 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
1771 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
1772 96e2b566 2019-07-08 stsp else
1773 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
1774 96e2b566 2019-07-08 stsp commit_queue_head, entry);
1775 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
1776 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1777 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
1778 b1bf1435 2019-06-21 stsp else
1779 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
1780 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
1781 20be8d96 2019-06-21 stsp } else {
1782 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1783 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
1784 20be8d96 2019-06-21 stsp else
1785 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1786 20be8d96 2019-06-21 stsp }
1787 60493ae3 2019-06-20 stsp
1788 60493ae3 2019-06-20 stsp while (1) {
1789 df0b3d8a 2019-06-28 stsp char *id_str;
1790 60493ae3 2019-06-20 stsp if (entry == NULL) {
1791 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
1792 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
1793 f801134a 2019-06-25 stsp view->search_next_done = 1;
1794 f801134a 2019-06-25 stsp return NULL;
1795 60493ae3 2019-06-20 stsp }
1796 96e2b566 2019-07-08 stsp /*
1797 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
1798 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
1799 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
1800 96e2b566 2019-07-08 stsp */
1801 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
1802 57b33b64 2019-07-08 stsp return trigger_log_thread(1,
1803 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
1804 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
1805 f801134a 2019-06-25 stsp &s->thread_args.need_commits);
1806 60493ae3 2019-06-20 stsp }
1807 60493ae3 2019-06-20 stsp
1808 df0b3d8a 2019-06-28 stsp err = got_object_id_str(&id_str, entry->id);
1809 df0b3d8a 2019-06-28 stsp if (err)
1810 df0b3d8a 2019-06-28 stsp return err;
1811 df0b3d8a 2019-06-28 stsp
1812 df0b3d8a 2019-06-28 stsp if (match_commit(entry->commit, id_str, &view->regex)) {
1813 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1814 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
1815 df0b3d8a 2019-06-28 stsp free(id_str);
1816 60493ae3 2019-06-20 stsp break;
1817 60493ae3 2019-06-20 stsp }
1818 df0b3d8a 2019-06-28 stsp free(id_str);
1819 96e2b566 2019-07-08 stsp s->search_entry = entry;
1820 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1821 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
1822 b1bf1435 2019-06-21 stsp else
1823 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1824 60493ae3 2019-06-20 stsp }
1825 60493ae3 2019-06-20 stsp
1826 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
1827 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
1828 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
1829 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1830 60493ae3 2019-06-20 stsp if (err)
1831 60493ae3 2019-06-20 stsp return err;
1832 ead14cbe 2019-06-21 stsp cur++;
1833 ead14cbe 2019-06-21 stsp }
1834 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
1835 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1836 60493ae3 2019-06-20 stsp if (err)
1837 60493ae3 2019-06-20 stsp return err;
1838 ead14cbe 2019-06-21 stsp cur--;
1839 60493ae3 2019-06-20 stsp }
1840 60493ae3 2019-06-20 stsp }
1841 60493ae3 2019-06-20 stsp
1842 96e2b566 2019-07-08 stsp s->search_entry = NULL;
1843 96e2b566 2019-07-08 stsp
1844 60493ae3 2019-06-20 stsp return NULL;
1845 60493ae3 2019-06-20 stsp }
1846 60493ae3 2019-06-20 stsp
1847 60493ae3 2019-06-20 stsp static const struct got_error *
1848 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1849 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
1850 d01904d4 2019-06-25 stsp const char *head_ref_name, const char *path, int check_disk)
1851 80ddbec8 2018-04-29 stsp {
1852 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1853 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1854 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1855 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1856 1a76625f 2018-10-22 stsp int errcode;
1857 80ddbec8 2018-04-29 stsp
1858 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1859 ecb28ae0 2018-07-16 stsp if (err != NULL)
1860 ecb28ae0 2018-07-16 stsp goto done;
1861 ecb28ae0 2018-07-16 stsp
1862 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1863 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1864 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1865 9ba79e04 2018-06-11 stsp
1866 8b473291 2019-02-21 stsp s->refs = refs;
1867 fb2756b9 2018-08-04 stsp s->repo = repo;
1868 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
1869 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1870 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1871 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1872 5036bf37 2018-09-24 stsp goto done;
1873 5036bf37 2018-09-24 stsp }
1874 e5a0f69f 2018-08-18 stsp
1875 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1876 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1877 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1878 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
1879 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
1880 1a76625f 2018-10-22 stsp
1881 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1882 1a76625f 2018-10-22 stsp if (err)
1883 1a76625f 2018-10-22 stsp goto done;
1884 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1885 1a76625f 2018-10-22 stsp 0, thread_repo);
1886 1a76625f 2018-10-22 stsp if (err)
1887 1a76625f 2018-10-22 stsp goto done;
1888 1a76625f 2018-10-22 stsp
1889 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1890 1a76625f 2018-10-22 stsp if (errcode) {
1891 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
1892 1a76625f 2018-10-22 stsp goto done;
1893 1a76625f 2018-10-22 stsp }
1894 1a76625f 2018-10-22 stsp
1895 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1896 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1897 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1898 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1899 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1900 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1901 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1902 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1903 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1904 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1905 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1906 ba4f502b 2018-08-04 stsp done:
1907 1a76625f 2018-10-22 stsp if (err)
1908 1a76625f 2018-10-22 stsp close_log_view(view);
1909 ba4f502b 2018-08-04 stsp return err;
1910 ba4f502b 2018-08-04 stsp }
1911 ba4f502b 2018-08-04 stsp
1912 e5a0f69f 2018-08-18 stsp static const struct got_error *
1913 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1914 ba4f502b 2018-08-04 stsp {
1915 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1916 ba4f502b 2018-08-04 stsp
1917 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1918 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1919 2b380cc8 2018-10-24 stsp &s->thread_args);
1920 2b380cc8 2018-10-24 stsp if (errcode)
1921 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
1922 2b380cc8 2018-10-24 stsp }
1923 2b380cc8 2018-10-24 stsp
1924 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1925 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1926 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
1927 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1928 e5a0f69f 2018-08-18 stsp }
1929 04cc582a 2018-08-01 stsp
1930 e5a0f69f 2018-08-18 stsp static const struct got_error *
1931 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1932 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1933 e5a0f69f 2018-08-18 stsp {
1934 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1935 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1936 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
1937 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1938 669b5ffa 2018-10-07 stsp int begin_x = 0;
1939 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
1940 80ddbec8 2018-04-29 stsp
1941 e5a0f69f 2018-08-18 stsp switch (ch) {
1942 1e37a5c2 2019-05-12 jcs case 'q':
1943 1e37a5c2 2019-05-12 jcs s->quit = 1;
1944 1e37a5c2 2019-05-12 jcs break;
1945 1e37a5c2 2019-05-12 jcs case 'k':
1946 1e37a5c2 2019-05-12 jcs case KEY_UP:
1947 1e37a5c2 2019-05-12 jcs case '<':
1948 1e37a5c2 2019-05-12 jcs case ',':
1949 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1950 1a76625f 2018-10-22 stsp break;
1951 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
1952 1e37a5c2 2019-05-12 jcs s->selected--;
1953 1144d21a 2019-06-21 stsp else
1954 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
1955 1144d21a 2019-06-21 stsp &s->commits);
1956 1e37a5c2 2019-05-12 jcs break;
1957 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
1958 a4292ac5 2019-05-12 jcs case CTRL('b'):
1959 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1960 e5a0f69f 2018-08-18 stsp break;
1961 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
1962 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
1963 1e37a5c2 2019-05-12 jcs s->selected = 0;
1964 e5a0f69f 2018-08-18 stsp break;
1965 1e37a5c2 2019-05-12 jcs }
1966 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
1967 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
1968 1e37a5c2 2019-05-12 jcs break;
1969 1e37a5c2 2019-05-12 jcs case 'j':
1970 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
1971 1e37a5c2 2019-05-12 jcs case '>':
1972 1e37a5c2 2019-05-12 jcs case '.':
1973 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1974 e5a0f69f 2018-08-18 stsp break;
1975 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
1976 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1977 1e37a5c2 2019-05-12 jcs s->selected++;
1978 1e37a5c2 2019-05-12 jcs break;
1979 80ddbec8 2018-04-29 stsp }
1980 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
1981 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
1982 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
1983 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1984 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1985 1e37a5c2 2019-05-12 jcs break;
1986 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
1987 a4292ac5 2019-05-12 jcs case CTRL('f'): {
1988 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
1989 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
1990 1e37a5c2 2019-05-12 jcs if (first == NULL)
1991 e5a0f69f 2018-08-18 stsp break;
1992 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
1993 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
1994 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
1995 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1996 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1997 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
1998 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
1999 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
2000 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
2001 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
2002 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
2003 1e37a5c2 2019-05-12 jcs }
2004 1e37a5c2 2019-05-12 jcs err = NULL;
2005 1e37a5c2 2019-05-12 jcs break;
2006 1e37a5c2 2019-05-12 jcs }
2007 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2008 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2009 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2010 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2011 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2012 1e37a5c2 2019-05-12 jcs break;
2013 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2014 87c7274c 2019-05-12 jcs case ' ':
2015 1e37a5c2 2019-05-12 jcs case '\r':
2016 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2017 e5a0f69f 2018-08-18 stsp break;
2018 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2019 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2020 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2021 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2022 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
2023 1e37a5c2 2019-05-12 jcs if (err)
2024 1e37a5c2 2019-05-12 jcs break;
2025 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2026 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2027 f7013a22 2018-10-24 stsp if (err)
2028 1e37a5c2 2019-05-12 jcs return err;
2029 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2030 1e37a5c2 2019-05-12 jcs if (err) {
2031 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2032 f7013a22 2018-10-24 stsp break;
2033 5036bf37 2018-09-24 stsp }
2034 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2035 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2036 1e37a5c2 2019-05-12 jcs } else
2037 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2038 1e37a5c2 2019-05-12 jcs break;
2039 1e37a5c2 2019-05-12 jcs case 't':
2040 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2041 5036bf37 2018-09-24 stsp break;
2042 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2043 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2044 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2045 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2046 1e37a5c2 2019-05-12 jcs if (err)
2047 e5a0f69f 2018-08-18 stsp break;
2048 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2049 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2050 1e37a5c2 2019-05-12 jcs if (err)
2051 1e37a5c2 2019-05-12 jcs return err;
2052 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2053 1e37a5c2 2019-05-12 jcs if (err) {
2054 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2055 1e37a5c2 2019-05-12 jcs break;
2056 1e37a5c2 2019-05-12 jcs }
2057 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2058 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2059 1e37a5c2 2019-05-12 jcs } else
2060 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2061 1e37a5c2 2019-05-12 jcs break;
2062 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2063 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2064 1e37a5c2 2019-05-12 jcs break;
2065 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2066 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2067 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2068 1e37a5c2 2019-05-12 jcs if (err)
2069 1e37a5c2 2019-05-12 jcs return err;
2070 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2071 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2072 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2073 638f9024 2019-05-13 stsp return got_error_from_errno(
2074 1e37a5c2 2019-05-12 jcs "view_open");
2075 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2076 d01904d4 2019-06-25 stsp s->repo, s->head_ref_name, parent_path, 0);
2077 1e37a5c2 2019-05-12 jcs if (err)
2078 1e37a5c2 2019-05-12 jcs return err;;
2079 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2080 1e37a5c2 2019-05-12 jcs *new_view = lv;
2081 1e37a5c2 2019-05-12 jcs else {
2082 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2083 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2084 1e37a5c2 2019-05-12 jcs }
2085 1e37a5c2 2019-05-12 jcs return NULL;
2086 1e37a5c2 2019-05-12 jcs }
2087 1e37a5c2 2019-05-12 jcs break;
2088 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2089 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2090 d01904d4 2019-06-25 stsp if (err)
2091 d01904d4 2019-06-25 stsp return err;
2092 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2093 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2094 d01904d4 2019-06-25 stsp if (lv == NULL)
2095 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2096 d01904d4 2019-06-25 stsp err = get_head_commit_id(&start_id, s->head_ref_name ?
2097 d01904d4 2019-06-25 stsp s->head_ref_name : GOT_REF_HEAD, s->repo);
2098 ef129c5e 2019-08-03 stsp if (err) {
2099 ef129c5e 2019-08-03 stsp view_close(lv);
2100 d01904d4 2019-06-25 stsp return err;
2101 ef129c5e 2019-08-03 stsp }
2102 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2103 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2104 d01904d4 2019-06-25 stsp free(start_id);
2105 ef129c5e 2019-08-03 stsp view_close(lv);
2106 d01904d4 2019-06-25 stsp return got_error_from_errno("strdup");
2107 d01904d4 2019-06-25 stsp }
2108 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2109 d01904d4 2019-06-25 stsp s->head_ref_name, in_repo_path, 0);
2110 ef129c5e 2019-08-03 stsp if (err) {
2111 ef129c5e 2019-08-03 stsp free(start_id);
2112 ef129c5e 2019-08-03 stsp view_close(lv);
2113 d01904d4 2019-06-25 stsp return err;;
2114 ef129c5e 2019-08-03 stsp }
2115 d01904d4 2019-06-25 stsp *dead_view = view;
2116 d01904d4 2019-06-25 stsp *new_view = lv;
2117 d01904d4 2019-06-25 stsp break;
2118 1e37a5c2 2019-05-12 jcs default:
2119 1e37a5c2 2019-05-12 jcs break;
2120 899d86c2 2018-05-10 stsp }
2121 e5a0f69f 2018-08-18 stsp
2122 80ddbec8 2018-04-29 stsp return err;
2123 80ddbec8 2018-04-29 stsp }
2124 80ddbec8 2018-04-29 stsp
2125 4ed7e80c 2018-05-20 stsp static const struct got_error *
2126 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2127 c2db6724 2019-01-04 stsp {
2128 c2db6724 2019-01-04 stsp const struct got_error *error;
2129 c2db6724 2019-01-04 stsp
2130 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2131 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2132 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2133 37c06ea4 2019-07-15 stsp #endif
2134 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2135 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2136 c2db6724 2019-01-04 stsp
2137 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2138 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2139 c2db6724 2019-01-04 stsp
2140 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2141 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2142 c2db6724 2019-01-04 stsp
2143 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2144 c2db6724 2019-01-04 stsp if (error != NULL)
2145 c2db6724 2019-01-04 stsp return error;
2146 c2db6724 2019-01-04 stsp
2147 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2148 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2149 c2db6724 2019-01-04 stsp
2150 c2db6724 2019-01-04 stsp return NULL;
2151 c2db6724 2019-01-04 stsp }
2152 c2db6724 2019-01-04 stsp
2153 a915003a 2019-02-05 stsp static void
2154 a915003a 2019-02-05 stsp init_curses(void)
2155 a915003a 2019-02-05 stsp {
2156 a915003a 2019-02-05 stsp initscr();
2157 a915003a 2019-02-05 stsp cbreak();
2158 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2159 a915003a 2019-02-05 stsp noecho();
2160 a915003a 2019-02-05 stsp nonl();
2161 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2162 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2163 a915003a 2019-02-05 stsp curs_set(0);
2164 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2165 83baff54 2019-08-12 stsp signal(SIGPIPE, tog_sigpipe);
2166 a915003a 2019-02-05 stsp }
2167 a915003a 2019-02-05 stsp
2168 c2db6724 2019-01-04 stsp static const struct got_error *
2169 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2170 9f7d7167 2018-04-29 stsp {
2171 80ddbec8 2018-04-29 stsp const struct got_error *error;
2172 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2173 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2174 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2175 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2176 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2177 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
2178 80ddbec8 2018-04-29 stsp int ch;
2179 04cc582a 2018-08-01 stsp struct tog_view *view;
2180 a55555f2 2019-03-28 stsp
2181 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2182 80ddbec8 2018-04-29 stsp
2183 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2184 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2185 c2db6724 2019-01-04 stsp NULL) == -1)
2186 80ddbec8 2018-04-29 stsp err(1, "pledge");
2187 80ddbec8 2018-04-29 stsp #endif
2188 80ddbec8 2018-04-29 stsp
2189 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2190 80ddbec8 2018-04-29 stsp switch (ch) {
2191 80ddbec8 2018-04-29 stsp case 'c':
2192 80ddbec8 2018-04-29 stsp start_commit = optarg;
2193 80ddbec8 2018-04-29 stsp break;
2194 ecb28ae0 2018-07-16 stsp case 'r':
2195 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2196 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2197 ecb28ae0 2018-07-16 stsp err(1, "-r option");
2198 ecb28ae0 2018-07-16 stsp break;
2199 80ddbec8 2018-04-29 stsp default:
2200 17020d27 2019-03-07 stsp usage_log();
2201 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2202 80ddbec8 2018-04-29 stsp }
2203 80ddbec8 2018-04-29 stsp }
2204 80ddbec8 2018-04-29 stsp
2205 80ddbec8 2018-04-29 stsp argc -= optind;
2206 80ddbec8 2018-04-29 stsp argv += optind;
2207 80ddbec8 2018-04-29 stsp
2208 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2209 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2210 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2211 ecb28ae0 2018-07-16 stsp goto done;
2212 ecb28ae0 2018-07-16 stsp }
2213 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2214 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2215 963f97a1 2019-03-18 stsp goto done;
2216 963f97a1 2019-03-18 stsp error = NULL;
2217 963f97a1 2019-03-18 stsp
2218 963f97a1 2019-03-18 stsp if (argc == 0) {
2219 963f97a1 2019-03-18 stsp path = strdup("");
2220 963f97a1 2019-03-18 stsp if (path == NULL) {
2221 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2222 ecb28ae0 2018-07-16 stsp goto done;
2223 ecb28ae0 2018-07-16 stsp }
2224 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2225 963f97a1 2019-03-18 stsp if (worktree) {
2226 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2227 963f97a1 2019-03-18 stsp argv[0]);
2228 963f97a1 2019-03-18 stsp if (error)
2229 963f97a1 2019-03-18 stsp goto done;
2230 963f97a1 2019-03-18 stsp } else {
2231 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2232 963f97a1 2019-03-18 stsp if (path == NULL) {
2233 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2234 963f97a1 2019-03-18 stsp goto done;
2235 963f97a1 2019-03-18 stsp }
2236 963f97a1 2019-03-18 stsp }
2237 963f97a1 2019-03-18 stsp } else
2238 963f97a1 2019-03-18 stsp usage_log();
2239 963f97a1 2019-03-18 stsp
2240 a1fbf39a 2019-08-11 stsp if (repo_path == NULL) {
2241 a1fbf39a 2019-08-11 stsp if (worktree)
2242 b9d7675a 2019-08-11 stsp repo_path = strdup(
2243 b9d7675a 2019-08-11 stsp got_worktree_get_repo_path(worktree));
2244 a1fbf39a 2019-08-11 stsp else
2245 a1fbf39a 2019-08-11 stsp repo_path = strdup(cwd);
2246 a1fbf39a 2019-08-11 stsp }
2247 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2248 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2249 963f97a1 2019-03-18 stsp goto done;
2250 ecb28ae0 2018-07-16 stsp }
2251 c2db6724 2019-01-04 stsp
2252 a915003a 2019-02-05 stsp init_curses();
2253 ecb28ae0 2018-07-16 stsp
2254 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
2255 80ddbec8 2018-04-29 stsp if (error != NULL)
2256 ecb28ae0 2018-07-16 stsp goto done;
2257 80ddbec8 2018-04-29 stsp
2258 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2259 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2260 c02c541e 2019-03-29 stsp if (error)
2261 c02c541e 2019-03-29 stsp goto done;
2262 c02c541e 2019-03-29 stsp
2263 15a94983 2018-12-23 stsp if (start_commit == NULL)
2264 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&start_id, worktree ?
2265 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2266 19e70ad6 2019-05-14 stsp repo);
2267 f2b6a97d 2019-07-15 stsp else {
2268 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&start_id, start_commit, repo);
2269 f2b6a97d 2019-07-15 stsp if (error) {
2270 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
2271 f2b6a97d 2019-07-15 stsp goto done;
2272 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&start_id,
2273 f2b6a97d 2019-07-15 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2274 f2b6a97d 2019-07-15 stsp }
2275 f2b6a97d 2019-07-15 stsp }
2276 80ddbec8 2018-04-29 stsp if (error != NULL)
2277 8b473291 2019-02-21 stsp goto done;
2278 8b473291 2019-02-21 stsp
2279 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2280 8b473291 2019-02-21 stsp if (error)
2281 ecb28ae0 2018-07-16 stsp goto done;
2282 ecb28ae0 2018-07-16 stsp
2283 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2284 04cc582a 2018-08-01 stsp if (view == NULL) {
2285 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2286 04cc582a 2018-08-01 stsp goto done;
2287 04cc582a 2018-08-01 stsp }
2288 d01904d4 2019-06-25 stsp error = open_log_view(view, start_id, &refs, repo, worktree ?
2289 d01904d4 2019-06-25 stsp got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2290 ba4f502b 2018-08-04 stsp if (error)
2291 ba4f502b 2018-08-04 stsp goto done;
2292 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2293 ecb28ae0 2018-07-16 stsp done:
2294 ecb28ae0 2018-07-16 stsp free(repo_path);
2295 ecb28ae0 2018-07-16 stsp free(cwd);
2296 ecb28ae0 2018-07-16 stsp free(path);
2297 899d86c2 2018-05-10 stsp free(start_id);
2298 ecb28ae0 2018-07-16 stsp if (repo)
2299 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2300 ec142235 2019-03-07 stsp if (worktree)
2301 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2302 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2303 80ddbec8 2018-04-29 stsp return error;
2304 9f7d7167 2018-04-29 stsp }
2305 9f7d7167 2018-04-29 stsp
2306 4ed7e80c 2018-05-20 stsp __dead static void
2307 9f7d7167 2018-04-29 stsp usage_diff(void)
2308 9f7d7167 2018-04-29 stsp {
2309 80ddbec8 2018-04-29 stsp endwin();
2310 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2311 9f7d7167 2018-04-29 stsp getprogname());
2312 9f7d7167 2018-04-29 stsp exit(1);
2313 b304db33 2018-05-20 stsp }
2314 b304db33 2018-05-20 stsp
2315 b304db33 2018-05-20 stsp static char *
2316 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2317 b304db33 2018-05-20 stsp {
2318 b304db33 2018-05-20 stsp char *line;
2319 b304db33 2018-05-20 stsp size_t linelen;
2320 b304db33 2018-05-20 stsp size_t lineno;
2321 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2322 b304db33 2018-05-20 stsp
2323 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2324 b304db33 2018-05-20 stsp if (len)
2325 b304db33 2018-05-20 stsp *len = linelen;
2326 b304db33 2018-05-20 stsp return line;
2327 26ed57b2 2018-05-19 stsp }
2328 26ed57b2 2018-05-19 stsp
2329 4ed7e80c 2018-05-20 stsp static const struct got_error *
2330 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2331 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
2332 c3e9aa98 2019-05-13 jcs char *header)
2333 26ed57b2 2018-05-19 stsp {
2334 61e69b96 2018-05-20 stsp const struct got_error *err;
2335 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2336 b304db33 2018-05-20 stsp char *line;
2337 b304db33 2018-05-20 stsp size_t len;
2338 61e69b96 2018-05-20 stsp wchar_t *wline;
2339 e0b650dd 2018-05-20 stsp int width;
2340 26ed57b2 2018-05-19 stsp
2341 26ed57b2 2018-05-19 stsp rewind(f);
2342 f7d12f7e 2018-08-01 stsp werase(view->window);
2343 a3404814 2018-09-02 stsp
2344 a3404814 2018-09-02 stsp if (header) {
2345 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
2346 a3404814 2018-09-02 stsp if (err) {
2347 a3404814 2018-09-02 stsp return err;
2348 a3404814 2018-09-02 stsp }
2349 a3404814 2018-09-02 stsp
2350 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2351 a3404814 2018-09-02 stsp wstandout(view->window);
2352 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2353 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2354 a3404814 2018-09-02 stsp wstandend(view->window);
2355 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2356 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2357 26ed57b2 2018-05-19 stsp
2358 a3404814 2018-09-02 stsp if (max_lines <= 1)
2359 a3404814 2018-09-02 stsp return NULL;
2360 a3404814 2018-09-02 stsp max_lines--;
2361 a3404814 2018-09-02 stsp }
2362 a3404814 2018-09-02 stsp
2363 26ed57b2 2018-05-19 stsp *eof = 0;
2364 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2365 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2366 26ed57b2 2018-05-19 stsp if (line == NULL) {
2367 26ed57b2 2018-05-19 stsp *eof = 1;
2368 26ed57b2 2018-05-19 stsp break;
2369 26ed57b2 2018-05-19 stsp }
2370 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2371 26ed57b2 2018-05-19 stsp free(line);
2372 26ed57b2 2018-05-19 stsp continue;
2373 26ed57b2 2018-05-19 stsp }
2374 26ed57b2 2018-05-19 stsp
2375 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2376 61e69b96 2018-05-20 stsp if (err) {
2377 61e69b96 2018-05-20 stsp free(line);
2378 61e69b96 2018-05-20 stsp return err;
2379 61e69b96 2018-05-20 stsp }
2380 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2381 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2382 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2383 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2384 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2385 26ed57b2 2018-05-19 stsp free(line);
2386 2550e4c3 2018-07-13 stsp free(wline);
2387 2550e4c3 2018-07-13 stsp wline = NULL;
2388 26ed57b2 2018-05-19 stsp }
2389 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2390 26ed57b2 2018-05-19 stsp
2391 1a57306a 2018-09-02 stsp view_vborder(view);
2392 c3e9aa98 2019-05-13 jcs
2393 c3e9aa98 2019-05-13 jcs if (*eof) {
2394 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2395 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2396 c3e9aa98 2019-05-13 jcs nprinted++;
2397 c3e9aa98 2019-05-13 jcs }
2398 c3e9aa98 2019-05-13 jcs
2399 c3e9aa98 2019-05-13 jcs err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2400 c3e9aa98 2019-05-13 jcs if (err) {
2401 c3e9aa98 2019-05-13 jcs return err;
2402 c3e9aa98 2019-05-13 jcs }
2403 26ed57b2 2018-05-19 stsp
2404 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2405 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2406 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2407 c3e9aa98 2019-05-13 jcs }
2408 c3e9aa98 2019-05-13 jcs
2409 26ed57b2 2018-05-19 stsp return NULL;
2410 abd2672a 2018-12-23 stsp }
2411 abd2672a 2018-12-23 stsp
2412 abd2672a 2018-12-23 stsp static char *
2413 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2414 abd2672a 2018-12-23 stsp {
2415 abd2672a 2018-12-23 stsp char *p, *s = ctime_r(time, datebuf);
2416 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2417 abd2672a 2018-12-23 stsp if (p)
2418 abd2672a 2018-12-23 stsp *p = '\0';
2419 abd2672a 2018-12-23 stsp return s;
2420 9f7d7167 2018-04-29 stsp }
2421 9f7d7167 2018-04-29 stsp
2422 4ed7e80c 2018-05-20 stsp static const struct got_error *
2423 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2424 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2425 abd2672a 2018-12-23 stsp {
2426 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2427 abd2672a 2018-12-23 stsp char datebuf[26];
2428 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2429 15a94983 2018-12-23 stsp char *id_str = NULL;
2430 45d799e2 2018-12-23 stsp time_t committer_time;
2431 45d799e2 2018-12-23 stsp const char *author, *committer;
2432 8b473291 2019-02-21 stsp char *refs_str = NULL;
2433 abd2672a 2018-12-23 stsp
2434 8b473291 2019-02-21 stsp if (refs) {
2435 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, commit_id);
2436 8b473291 2019-02-21 stsp if (err)
2437 8b473291 2019-02-21 stsp return err;
2438 8b473291 2019-02-21 stsp }
2439 8b473291 2019-02-21 stsp
2440 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2441 abd2672a 2018-12-23 stsp if (err)
2442 abd2672a 2018-12-23 stsp return err;
2443 abd2672a 2018-12-23 stsp
2444 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2445 15a94983 2018-12-23 stsp if (err) {
2446 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2447 15a94983 2018-12-23 stsp goto done;
2448 15a94983 2018-12-23 stsp }
2449 abd2672a 2018-12-23 stsp
2450 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2451 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2452 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2453 abd2672a 2018-12-23 stsp goto done;
2454 abd2672a 2018-12-23 stsp }
2455 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2456 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2457 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2458 abd2672a 2018-12-23 stsp goto done;
2459 abd2672a 2018-12-23 stsp }
2460 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2461 abd2672a 2018-12-23 stsp if (fprintf(outfile, "date: %s UTC\n",
2462 45d799e2 2018-12-23 stsp get_datestr(&committer_time, datebuf)) < 0) {
2463 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2464 abd2672a 2018-12-23 stsp goto done;
2465 abd2672a 2018-12-23 stsp }
2466 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2467 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2468 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2469 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2470 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2471 abd2672a 2018-12-23 stsp goto done;
2472 abd2672a 2018-12-23 stsp }
2473 45d799e2 2018-12-23 stsp if (fprintf(outfile, "%s\n",
2474 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(commit)) < 0) {
2475 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2476 abd2672a 2018-12-23 stsp goto done;
2477 abd2672a 2018-12-23 stsp }
2478 abd2672a 2018-12-23 stsp done:
2479 abd2672a 2018-12-23 stsp free(id_str);
2480 8b473291 2019-02-21 stsp free(refs_str);
2481 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2482 abd2672a 2018-12-23 stsp return err;
2483 abd2672a 2018-12-23 stsp }
2484 abd2672a 2018-12-23 stsp
2485 abd2672a 2018-12-23 stsp static const struct got_error *
2486 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2487 26ed57b2 2018-05-19 stsp {
2488 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2489 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2490 15a94983 2018-12-23 stsp int obj_type;
2491 26ed57b2 2018-05-19 stsp
2492 511a516b 2018-05-19 stsp f = got_opentemp();
2493 48ae06ee 2018-10-18 stsp if (f == NULL) {
2494 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2495 48ae06ee 2018-10-18 stsp goto done;
2496 48ae06ee 2018-10-18 stsp }
2497 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2498 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2499 fb43ecf1 2019-02-11 stsp goto done;
2500 fb43ecf1 2019-02-11 stsp }
2501 48ae06ee 2018-10-18 stsp s->f = f;
2502 26ed57b2 2018-05-19 stsp
2503 15a94983 2018-12-23 stsp if (s->id1)
2504 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2505 15a94983 2018-12-23 stsp else
2506 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2507 15a94983 2018-12-23 stsp if (err)
2508 15a94983 2018-12-23 stsp goto done;
2509 15a94983 2018-12-23 stsp
2510 15a94983 2018-12-23 stsp switch (obj_type) {
2511 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2512 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2513 54156555 2018-12-24 stsp s->diff_context, s->repo, f);
2514 26ed57b2 2018-05-19 stsp break;
2515 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2516 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2517 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
2518 26ed57b2 2018-05-19 stsp break;
2519 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2520 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2521 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2522 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2523 abd2672a 2018-12-23 stsp
2524 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2525 abd2672a 2018-12-23 stsp if (err)
2526 abd2672a 2018-12-23 stsp break;
2527 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2528 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2529 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2530 15a087fe 2019-02-21 stsp else {
2531 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2532 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2533 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2534 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2535 8b473291 2019-02-21 stsp s->repo, f);
2536 15a087fe 2019-02-21 stsp break;
2537 15a087fe 2019-02-21 stsp }
2538 abd2672a 2018-12-23 stsp }
2539 abd2672a 2018-12-23 stsp }
2540 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2541 abd2672a 2018-12-23 stsp
2542 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2543 15a94983 2018-12-23 stsp s->diff_context, s->repo, f);
2544 26ed57b2 2018-05-19 stsp break;
2545 abd2672a 2018-12-23 stsp }
2546 26ed57b2 2018-05-19 stsp default:
2547 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2548 48ae06ee 2018-10-18 stsp break;
2549 26ed57b2 2018-05-19 stsp }
2550 48ae06ee 2018-10-18 stsp done:
2551 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2552 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2553 48ae06ee 2018-10-18 stsp return err;
2554 48ae06ee 2018-10-18 stsp }
2555 26ed57b2 2018-05-19 stsp
2556 f5215bb9 2019-02-22 stsp static void
2557 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2558 f5215bb9 2019-02-22 stsp {
2559 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2560 f5215bb9 2019-02-22 stsp update_panels();
2561 f5215bb9 2019-02-22 stsp doupdate();
2562 f5215bb9 2019-02-22 stsp }
2563 f5215bb9 2019-02-22 stsp
2564 48ae06ee 2018-10-18 stsp static const struct got_error *
2565 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2566 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2567 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2568 48ae06ee 2018-10-18 stsp {
2569 48ae06ee 2018-10-18 stsp const struct got_error *err;
2570 5dc9f4bc 2018-08-04 stsp
2571 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2572 15a94983 2018-12-23 stsp int type1, type2;
2573 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2574 15a94983 2018-12-23 stsp if (err)
2575 15a94983 2018-12-23 stsp return err;
2576 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2577 15a94983 2018-12-23 stsp if (err)
2578 15a94983 2018-12-23 stsp return err;
2579 15a94983 2018-12-23 stsp
2580 15a94983 2018-12-23 stsp if (type1 != type2)
2581 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2582 15a94983 2018-12-23 stsp }
2583 48ae06ee 2018-10-18 stsp
2584 15a94983 2018-12-23 stsp if (id1) {
2585 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2586 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2587 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2588 48ae06ee 2018-10-18 stsp } else
2589 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2590 48ae06ee 2018-10-18 stsp
2591 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2592 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2593 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2594 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2595 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2596 48ae06ee 2018-10-18 stsp }
2597 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2598 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2599 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2600 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2601 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2602 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2603 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2604 5dc9f4bc 2018-08-04 stsp
2605 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2606 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2607 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2608 f5215bb9 2019-02-22 stsp
2609 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2610 48ae06ee 2018-10-18 stsp if (err) {
2611 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2612 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2613 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2614 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2615 48ae06ee 2018-10-18 stsp return err;
2616 48ae06ee 2018-10-18 stsp }
2617 48ae06ee 2018-10-18 stsp
2618 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2619 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2620 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2621 e5a0f69f 2018-08-18 stsp
2622 5dc9f4bc 2018-08-04 stsp return NULL;
2623 5dc9f4bc 2018-08-04 stsp }
2624 5dc9f4bc 2018-08-04 stsp
2625 e5a0f69f 2018-08-18 stsp static const struct got_error *
2626 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2627 5dc9f4bc 2018-08-04 stsp {
2628 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2629 e5a0f69f 2018-08-18 stsp
2630 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2631 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2632 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2633 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2634 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2635 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2636 e5a0f69f 2018-08-18 stsp return err;
2637 5dc9f4bc 2018-08-04 stsp }
2638 5dc9f4bc 2018-08-04 stsp
2639 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2640 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2641 5dc9f4bc 2018-08-04 stsp {
2642 a3404814 2018-09-02 stsp const struct got_error *err;
2643 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2644 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2645 a3404814 2018-09-02 stsp
2646 a3404814 2018-09-02 stsp if (s->id1) {
2647 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2648 a3404814 2018-09-02 stsp if (err)
2649 a3404814 2018-09-02 stsp return err;
2650 a3404814 2018-09-02 stsp }
2651 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2652 a3404814 2018-09-02 stsp if (err)
2653 a3404814 2018-09-02 stsp return err;
2654 26ed57b2 2018-05-19 stsp
2655 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2656 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2657 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2658 a3404814 2018-09-02 stsp free(id_str1);
2659 a3404814 2018-09-02 stsp free(id_str2);
2660 a3404814 2018-09-02 stsp return err;
2661 a3404814 2018-09-02 stsp }
2662 a3404814 2018-09-02 stsp free(id_str1);
2663 a3404814 2018-09-02 stsp free(id_str2);
2664 a3404814 2018-09-02 stsp
2665 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2666 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2667 a3404814 2018-09-02 stsp header);
2668 15a087fe 2019-02-21 stsp }
2669 15a087fe 2019-02-21 stsp
2670 15a087fe 2019-02-21 stsp static const struct got_error *
2671 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2672 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2673 15a087fe 2019-02-21 stsp {
2674 d7a04538 2019-02-21 stsp const struct got_error *err;
2675 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
2676 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
2677 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
2678 15a087fe 2019-02-21 stsp
2679 15a087fe 2019-02-21 stsp free(s->id2);
2680 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2681 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2682 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2683 15a087fe 2019-02-21 stsp
2684 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2685 d7a04538 2019-02-21 stsp if (err)
2686 d7a04538 2019-02-21 stsp return err;
2687 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
2688 15a087fe 2019-02-21 stsp free(s->id1);
2689 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
2690 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2691 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
2692 15a087fe 2019-02-21 stsp return NULL;
2693 0cf4efb1 2018-09-29 stsp }
2694 0cf4efb1 2018-09-29 stsp
2695 0cf4efb1 2018-09-29 stsp static const struct got_error *
2696 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2697 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2698 e5a0f69f 2018-08-18 stsp {
2699 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2700 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2701 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
2702 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
2703 e5a0f69f 2018-08-18 stsp int i;
2704 e5a0f69f 2018-08-18 stsp
2705 e5a0f69f 2018-08-18 stsp switch (ch) {
2706 1e37a5c2 2019-05-12 jcs case 'k':
2707 1e37a5c2 2019-05-12 jcs case KEY_UP:
2708 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
2709 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2710 1e37a5c2 2019-05-12 jcs break;
2711 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2712 a60a9dc4 2019-05-13 jcs case CTRL('b'):
2713 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
2714 26ed57b2 2018-05-19 stsp break;
2715 1e37a5c2 2019-05-12 jcs i = 0;
2716 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
2717 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
2718 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2719 1e37a5c2 2019-05-12 jcs break;
2720 1e37a5c2 2019-05-12 jcs case 'j':
2721 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2722 1e37a5c2 2019-05-12 jcs if (!s->eof)
2723 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2724 1e37a5c2 2019-05-12 jcs break;
2725 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
2726 a60a9dc4 2019-05-13 jcs case CTRL('f'):
2727 1e37a5c2 2019-05-12 jcs case ' ':
2728 00ba99a7 2019-05-12 jcs if (s->eof)
2729 1e37a5c2 2019-05-12 jcs break;
2730 1e37a5c2 2019-05-12 jcs i = 0;
2731 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
2732 1e37a5c2 2019-05-12 jcs char *line;
2733 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
2734 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2735 1e37a5c2 2019-05-12 jcs if (line == NULL)
2736 34bc9ec9 2019-02-22 stsp break;
2737 1e37a5c2 2019-05-12 jcs }
2738 1e37a5c2 2019-05-12 jcs break;
2739 1e37a5c2 2019-05-12 jcs case '[':
2740 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
2741 1e37a5c2 2019-05-12 jcs s->diff_context--;
2742 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2743 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2744 1e37a5c2 2019-05-12 jcs }
2745 1e37a5c2 2019-05-12 jcs break;
2746 1e37a5c2 2019-05-12 jcs case ']':
2747 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2748 1e37a5c2 2019-05-12 jcs s->diff_context++;
2749 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2750 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2751 1e37a5c2 2019-05-12 jcs }
2752 1e37a5c2 2019-05-12 jcs break;
2753 1e37a5c2 2019-05-12 jcs case '<':
2754 1e37a5c2 2019-05-12 jcs case ',':
2755 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2756 48ae06ee 2018-10-18 stsp break;
2757 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2758 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
2759 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
2760 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2761 48ae06ee 2018-10-18 stsp break;
2762 6524637e 2019-02-21 stsp
2763 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2764 1e37a5c2 2019-05-12 jcs KEY_UP);
2765 1e37a5c2 2019-05-12 jcs if (err)
2766 1e37a5c2 2019-05-12 jcs break;
2767 15a087fe 2019-02-21 stsp
2768 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2769 1e37a5c2 2019-05-12 jcs if (err)
2770 1e37a5c2 2019-05-12 jcs break;
2771 15a087fe 2019-02-21 stsp
2772 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2773 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2774 15a087fe 2019-02-21 stsp
2775 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2776 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2777 1e37a5c2 2019-05-12 jcs break;
2778 1e37a5c2 2019-05-12 jcs case '>':
2779 1e37a5c2 2019-05-12 jcs case '.':
2780 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2781 15a087fe 2019-02-21 stsp break;
2782 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2783 5e224a3e 2019-02-22 stsp
2784 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2785 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
2786 5e224a3e 2019-02-22 stsp
2787 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
2788 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
2789 1e37a5c2 2019-05-12 jcs update_panels();
2790 1e37a5c2 2019-05-12 jcs doupdate();
2791 6e73b0d6 2019-02-22 stsp
2792 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
2793 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
2794 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
2795 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
2796 fb872ab2 2019-02-21 stsp if (err)
2797 fb872ab2 2019-02-21 stsp break;
2798 1e37a5c2 2019-05-12 jcs }
2799 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2800 1e37a5c2 2019-05-12 jcs KEY_DOWN);
2801 1e37a5c2 2019-05-12 jcs if (err)
2802 1e37a5c2 2019-05-12 jcs break;
2803 15a087fe 2019-02-21 stsp
2804 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
2805 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2806 1e37a5c2 2019-05-12 jcs break;
2807 15a087fe 2019-02-21 stsp
2808 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2809 1e37a5c2 2019-05-12 jcs if (err)
2810 1e37a5c2 2019-05-12 jcs break;
2811 15a087fe 2019-02-21 stsp
2812 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2813 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2814 1e37a5c2 2019-05-12 jcs
2815 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2816 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2817 1e37a5c2 2019-05-12 jcs break;
2818 1e37a5c2 2019-05-12 jcs default:
2819 1e37a5c2 2019-05-12 jcs break;
2820 26ed57b2 2018-05-19 stsp }
2821 e5a0f69f 2018-08-18 stsp
2822 bcbd79e2 2018-08-19 stsp return err;
2823 26ed57b2 2018-05-19 stsp }
2824 26ed57b2 2018-05-19 stsp
2825 4ed7e80c 2018-05-20 stsp static const struct got_error *
2826 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2827 9f7d7167 2018-04-29 stsp {
2828 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2829 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2830 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2831 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2832 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2833 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2834 26ed57b2 2018-05-19 stsp int ch;
2835 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2836 70ac5f84 2019-03-28 stsp
2837 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2838 26ed57b2 2018-05-19 stsp
2839 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2840 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2841 eb6600df 2019-01-04 stsp NULL) == -1)
2842 26ed57b2 2018-05-19 stsp err(1, "pledge");
2843 26ed57b2 2018-05-19 stsp #endif
2844 26ed57b2 2018-05-19 stsp
2845 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2846 26ed57b2 2018-05-19 stsp switch (ch) {
2847 26ed57b2 2018-05-19 stsp default:
2848 17020d27 2019-03-07 stsp usage_diff();
2849 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2850 26ed57b2 2018-05-19 stsp }
2851 26ed57b2 2018-05-19 stsp }
2852 26ed57b2 2018-05-19 stsp
2853 26ed57b2 2018-05-19 stsp argc -= optind;
2854 26ed57b2 2018-05-19 stsp argv += optind;
2855 26ed57b2 2018-05-19 stsp
2856 26ed57b2 2018-05-19 stsp if (argc == 0) {
2857 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2858 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2859 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2860 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2861 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2862 15a94983 2018-12-23 stsp id_str1 = argv[0];
2863 15a94983 2018-12-23 stsp id_str2 = argv[1];
2864 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2865 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2866 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2867 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2868 15a94983 2018-12-23 stsp id_str1 = argv[1];
2869 15a94983 2018-12-23 stsp id_str2 = argv[2];
2870 26ed57b2 2018-05-19 stsp } else
2871 26ed57b2 2018-05-19 stsp usage_diff();
2872 a915003a 2019-02-05 stsp
2873 a915003a 2019-02-05 stsp init_curses();
2874 eb6600df 2019-01-04 stsp
2875 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2876 eb6600df 2019-01-04 stsp if (error)
2877 eb6600df 2019-01-04 stsp goto done;
2878 26ed57b2 2018-05-19 stsp
2879 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
2880 26ed57b2 2018-05-19 stsp if (error)
2881 26ed57b2 2018-05-19 stsp goto done;
2882 26ed57b2 2018-05-19 stsp
2883 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
2884 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
2885 26ed57b2 2018-05-19 stsp if (error)
2886 26ed57b2 2018-05-19 stsp goto done;
2887 26ed57b2 2018-05-19 stsp
2888 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
2889 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
2890 26ed57b2 2018-05-19 stsp if (error)
2891 26ed57b2 2018-05-19 stsp goto done;
2892 26ed57b2 2018-05-19 stsp
2893 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2894 8b473291 2019-02-21 stsp if (error)
2895 8b473291 2019-02-21 stsp goto done;
2896 8b473291 2019-02-21 stsp
2897 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2898 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2899 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2900 ea5e7bb5 2018-08-01 stsp goto done;
2901 ea5e7bb5 2018-08-01 stsp }
2902 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2903 5dc9f4bc 2018-08-04 stsp if (error)
2904 5dc9f4bc 2018-08-04 stsp goto done;
2905 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2906 26ed57b2 2018-05-19 stsp done:
2907 c02c541e 2019-03-29 stsp free(repo_path);
2908 921be706 2019-06-28 stsp if (repo)
2909 921be706 2019-06-28 stsp got_repo_close(repo);
2910 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2911 26ed57b2 2018-05-19 stsp return error;
2912 9f7d7167 2018-04-29 stsp }
2913 9f7d7167 2018-04-29 stsp
2914 4ed7e80c 2018-05-20 stsp __dead static void
2915 9f7d7167 2018-04-29 stsp usage_blame(void)
2916 9f7d7167 2018-04-29 stsp {
2917 80ddbec8 2018-04-29 stsp endwin();
2918 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2919 9f7d7167 2018-04-29 stsp getprogname());
2920 9f7d7167 2018-04-29 stsp exit(1);
2921 9f7d7167 2018-04-29 stsp }
2922 84451b3e 2018-07-10 stsp
2923 84451b3e 2018-07-10 stsp struct tog_blame_line {
2924 84451b3e 2018-07-10 stsp int annotated;
2925 84451b3e 2018-07-10 stsp struct got_object_id *id;
2926 84451b3e 2018-07-10 stsp };
2927 9f7d7167 2018-04-29 stsp
2928 4ed7e80c 2018-05-20 stsp static const struct got_error *
2929 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2930 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2931 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2932 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2933 84451b3e 2018-07-10 stsp {
2934 84451b3e 2018-07-10 stsp const struct got_error *err;
2935 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2936 84451b3e 2018-07-10 stsp char *line;
2937 84451b3e 2018-07-10 stsp size_t len;
2938 84451b3e 2018-07-10 stsp wchar_t *wline;
2939 b700b5d6 2018-07-10 stsp int width, wlimit;
2940 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2941 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2942 ab089a2a 2018-07-12 stsp char *id_str;
2943 ab089a2a 2018-07-12 stsp
2944 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2945 ab089a2a 2018-07-12 stsp if (err)
2946 ab089a2a 2018-07-12 stsp return err;
2947 84451b3e 2018-07-10 stsp
2948 84451b3e 2018-07-10 stsp rewind(f);
2949 f7d12f7e 2018-08-01 stsp werase(view->window);
2950 84451b3e 2018-07-10 stsp
2951 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
2952 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2953 ab089a2a 2018-07-12 stsp free(id_str);
2954 ab089a2a 2018-07-12 stsp return err;
2955 ab089a2a 2018-07-12 stsp }
2956 ab089a2a 2018-07-12 stsp
2957 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2958 ab089a2a 2018-07-12 stsp free(line);
2959 2550e4c3 2018-07-13 stsp line = NULL;
2960 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2961 a3404814 2018-09-02 stsp wstandout(view->window);
2962 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2963 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2964 a3404814 2018-09-02 stsp wstandend(view->window);
2965 2550e4c3 2018-07-13 stsp free(wline);
2966 2550e4c3 2018-07-13 stsp wline = NULL;
2967 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2968 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2969 ab089a2a 2018-07-12 stsp
2970 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2971 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2972 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
2973 ab089a2a 2018-07-12 stsp free(id_str);
2974 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2975 ab089a2a 2018-07-12 stsp }
2976 ab089a2a 2018-07-12 stsp free(id_str);
2977 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2978 3f60a8ef 2018-07-10 stsp free(line);
2979 2550e4c3 2018-07-13 stsp line = NULL;
2980 3f60a8ef 2018-07-10 stsp if (err)
2981 3f60a8ef 2018-07-10 stsp return err;
2982 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2983 2550e4c3 2018-07-13 stsp free(wline);
2984 2550e4c3 2018-07-13 stsp wline = NULL;
2985 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2986 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2987 3f60a8ef 2018-07-10 stsp
2988 84451b3e 2018-07-10 stsp *eof = 0;
2989 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2990 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2991 84451b3e 2018-07-10 stsp if (line == NULL) {
2992 84451b3e 2018-07-10 stsp *eof = 1;
2993 84451b3e 2018-07-10 stsp break;
2994 84451b3e 2018-07-10 stsp }
2995 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2996 84451b3e 2018-07-10 stsp free(line);
2997 84451b3e 2018-07-10 stsp continue;
2998 84451b3e 2018-07-10 stsp }
2999 84451b3e 2018-07-10 stsp
3000 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
3001 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
3002 84451b3e 2018-07-10 stsp if (err) {
3003 84451b3e 2018-07-10 stsp free(line);
3004 84451b3e 2018-07-10 stsp return err;
3005 84451b3e 2018-07-10 stsp }
3006 84451b3e 2018-07-10 stsp
3007 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3008 f7d12f7e 2018-08-01 stsp wstandout(view->window);
3009 b700b5d6 2018-07-10 stsp
3010 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
3011 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
3012 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
3013 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
3014 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
3015 84451b3e 2018-07-10 stsp char *id_str;
3016 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
3017 84451b3e 2018-07-10 stsp if (err) {
3018 84451b3e 2018-07-10 stsp free(line);
3019 2550e4c3 2018-07-13 stsp free(wline);
3020 84451b3e 2018-07-10 stsp return err;
3021 84451b3e 2018-07-10 stsp }
3022 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
3023 84451b3e 2018-07-10 stsp free(id_str);
3024 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
3025 ee41ec32 2018-07-10 stsp } else {
3026 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
3027 ee41ec32 2018-07-10 stsp prev_id = NULL;
3028 ee41ec32 2018-07-10 stsp }
3029 84451b3e 2018-07-10 stsp
3030 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3031 b700b5d6 2018-07-10 stsp while (width < wlimit) {
3032 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
3033 b700b5d6 2018-07-10 stsp width++;
3034 b700b5d6 2018-07-10 stsp }
3035 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3036 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3037 84451b3e 2018-07-10 stsp if (++nprinted == 1)
3038 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
3039 84451b3e 2018-07-10 stsp free(line);
3040 2550e4c3 2018-07-13 stsp free(wline);
3041 2550e4c3 2018-07-13 stsp wline = NULL;
3042 84451b3e 2018-07-10 stsp }
3043 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
3044 84451b3e 2018-07-10 stsp
3045 1a57306a 2018-09-02 stsp view_vborder(view);
3046 84451b3e 2018-07-10 stsp
3047 84451b3e 2018-07-10 stsp return NULL;
3048 84451b3e 2018-07-10 stsp }
3049 84451b3e 2018-07-10 stsp
3050 84451b3e 2018-07-10 stsp static const struct got_error *
3051 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3052 84451b3e 2018-07-10 stsp {
3053 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
3054 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
3055 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
3056 1a76625f 2018-10-22 stsp int errcode;
3057 84451b3e 2018-07-10 stsp
3058 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
3059 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3060 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3061 84451b3e 2018-07-10 stsp
3062 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3063 1a76625f 2018-10-22 stsp if (errcode)
3064 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3065 84451b3e 2018-07-10 stsp
3066 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3067 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3068 d68a0a7d 2018-07-10 stsp goto done;
3069 d68a0a7d 2018-07-10 stsp }
3070 d68a0a7d 2018-07-10 stsp
3071 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3072 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3073 d68a0a7d 2018-07-10 stsp
3074 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3075 d68a0a7d 2018-07-10 stsp if (line->annotated)
3076 d68a0a7d 2018-07-10 stsp goto done;
3077 d68a0a7d 2018-07-10 stsp
3078 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3079 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3080 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3081 84451b3e 2018-07-10 stsp goto done;
3082 84451b3e 2018-07-10 stsp }
3083 84451b3e 2018-07-10 stsp line->annotated = 1;
3084 84451b3e 2018-07-10 stsp done:
3085 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3086 1a76625f 2018-10-22 stsp if (errcode)
3087 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3088 84451b3e 2018-07-10 stsp return err;
3089 84451b3e 2018-07-10 stsp }
3090 84451b3e 2018-07-10 stsp
3091 84451b3e 2018-07-10 stsp static void *
3092 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3093 84451b3e 2018-07-10 stsp {
3094 18430de3 2018-07-10 stsp const struct got_error *err;
3095 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3096 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3097 1a76625f 2018-10-22 stsp int errcode;
3098 18430de3 2018-07-10 stsp
3099 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3100 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
3101 18430de3 2018-07-10 stsp
3102 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3103 1a76625f 2018-10-22 stsp if (errcode)
3104 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3105 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3106 18430de3 2018-07-10 stsp
3107 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3108 c9beca56 2018-07-22 stsp ta->repo = NULL;
3109 c9beca56 2018-07-22 stsp *ta->complete = 1;
3110 18430de3 2018-07-10 stsp
3111 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3112 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3113 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3114 18430de3 2018-07-10 stsp
3115 18430de3 2018-07-10 stsp return (void *)err;
3116 84451b3e 2018-07-10 stsp }
3117 84451b3e 2018-07-10 stsp
3118 245d91c1 2018-07-12 stsp static struct got_object_id *
3119 15a94983 2018-12-23 stsp get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3120 15a94983 2018-12-23 stsp int selected_line)
3121 245d91c1 2018-07-12 stsp {
3122 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
3123 b880a918 2018-07-10 stsp
3124 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
3125 245d91c1 2018-07-12 stsp if (!line->annotated)
3126 245d91c1 2018-07-12 stsp return NULL;
3127 245d91c1 2018-07-12 stsp
3128 245d91c1 2018-07-12 stsp return line->id;
3129 b880a918 2018-07-10 stsp }
3130 245d91c1 2018-07-12 stsp
3131 b880a918 2018-07-10 stsp static const struct got_error *
3132 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
3133 a70480e0 2018-06-23 stsp {
3134 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
3135 245d91c1 2018-07-12 stsp int i;
3136 245d91c1 2018-07-12 stsp
3137 245d91c1 2018-07-12 stsp if (blame->thread) {
3138 1a76625f 2018-10-22 stsp int errcode;
3139 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3140 1a76625f 2018-10-22 stsp if (errcode)
3141 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
3142 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
3143 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
3144 1a76625f 2018-10-22 stsp if (errcode)
3145 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
3146 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&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_lock");
3150 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
3151 245d91c1 2018-07-12 stsp err = NULL;
3152 245d91c1 2018-07-12 stsp blame->thread = NULL;
3153 245d91c1 2018-07-12 stsp }
3154 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
3155 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
3156 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
3157 245d91c1 2018-07-12 stsp }
3158 245d91c1 2018-07-12 stsp if (blame->f) {
3159 fb43ecf1 2019-02-11 stsp if (fclose(blame->f) != 0 && err == NULL)
3160 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3161 245d91c1 2018-07-12 stsp blame->f = NULL;
3162 245d91c1 2018-07-12 stsp }
3163 57670559 2018-12-23 stsp if (blame->lines) {
3164 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
3165 57670559 2018-12-23 stsp free(blame->lines[i].id);
3166 57670559 2018-12-23 stsp free(blame->lines);
3167 57670559 2018-12-23 stsp blame->lines = NULL;
3168 57670559 2018-12-23 stsp }
3169 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
3170 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
3171 245d91c1 2018-07-12 stsp
3172 245d91c1 2018-07-12 stsp return err;
3173 245d91c1 2018-07-12 stsp }
3174 245d91c1 2018-07-12 stsp
3175 245d91c1 2018-07-12 stsp static const struct got_error *
3176 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3177 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
3178 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
3179 245d91c1 2018-07-12 stsp struct got_repository *repo)
3180 245d91c1 2018-07-12 stsp {
3181 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
3182 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
3183 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
3184 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
3185 15a94983 2018-12-23 stsp int obj_type;
3186 a70480e0 2018-06-23 stsp
3187 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3188 27d434c2 2018-09-15 stsp if (err)
3189 15a94983 2018-12-23 stsp return err;
3190 15a94983 2018-12-23 stsp if (obj_id == NULL)
3191 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
3192 27d434c2 2018-09-15 stsp
3193 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
3194 84451b3e 2018-07-10 stsp if (err)
3195 84451b3e 2018-07-10 stsp goto done;
3196 27d434c2 2018-09-15 stsp
3197 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3198 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3199 84451b3e 2018-07-10 stsp goto done;
3200 84451b3e 2018-07-10 stsp }
3201 a70480e0 2018-06-23 stsp
3202 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3203 a70480e0 2018-06-23 stsp if (err)
3204 a70480e0 2018-06-23 stsp goto done;
3205 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
3206 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
3207 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
3208 84451b3e 2018-07-10 stsp goto done;
3209 84451b3e 2018-07-10 stsp }
3210 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3211 6c4c42e0 2019-06-24 stsp &blame->line_offsets, blame->f, blob);
3212 84451b3e 2018-07-10 stsp if (err)
3213 84451b3e 2018-07-10 stsp goto done;
3214 a70480e0 2018-06-23 stsp
3215 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3216 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
3217 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
3218 84451b3e 2018-07-10 stsp goto done;
3219 84451b3e 2018-07-10 stsp }
3220 a70480e0 2018-06-23 stsp
3221 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3222 bd24772e 2018-07-11 stsp if (err)
3223 bd24772e 2018-07-11 stsp goto done;
3224 bd24772e 2018-07-11 stsp
3225 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
3226 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
3227 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
3228 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
3229 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
3230 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3231 245d91c1 2018-07-12 stsp goto done;
3232 245d91c1 2018-07-12 stsp }
3233 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
3234 245d91c1 2018-07-12 stsp
3235 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
3236 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
3237 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
3238 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
3239 245d91c1 2018-07-12 stsp *blame_complete = 0;
3240 245d91c1 2018-07-12 stsp
3241 245d91c1 2018-07-12 stsp done:
3242 245d91c1 2018-07-12 stsp if (blob)
3243 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
3244 27d434c2 2018-09-15 stsp free(obj_id);
3245 245d91c1 2018-07-12 stsp if (err)
3246 245d91c1 2018-07-12 stsp stop_blame(blame);
3247 245d91c1 2018-07-12 stsp return err;
3248 245d91c1 2018-07-12 stsp }
3249 245d91c1 2018-07-12 stsp
3250 245d91c1 2018-07-12 stsp static const struct got_error *
3251 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
3252 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3253 8b473291 2019-02-21 stsp struct got_repository *repo)
3254 245d91c1 2018-07-12 stsp {
3255 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
3256 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3257 dbc6a6b6 2018-07-12 stsp
3258 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
3259 245d91c1 2018-07-12 stsp
3260 c4843652 2019-08-12 stsp s->path = strdup(path);
3261 c4843652 2019-08-12 stsp if (s->path == NULL)
3262 c4843652 2019-08-12 stsp return got_error_from_errno("strdup");
3263 c4843652 2019-08-12 stsp
3264 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3265 c4843652 2019-08-12 stsp if (err) {
3266 c4843652 2019-08-12 stsp free(s->path);
3267 7cbe629d 2018-08-04 stsp return err;
3268 c4843652 2019-08-12 stsp }
3269 245d91c1 2018-07-12 stsp
3270 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3271 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
3272 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
3273 fb2756b9 2018-08-04 stsp s->selected_line = 1;
3274 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
3275 fb2756b9 2018-08-04 stsp s->repo = repo;
3276 8b473291 2019-02-21 stsp s->refs = refs;
3277 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
3278 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
3279 7cbe629d 2018-08-04 stsp
3280 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
3281 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
3282 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
3283 6c4c42e0 2019-06-24 stsp view->search_start = search_start_blame_view;
3284 6c4c42e0 2019-06-24 stsp view->search_next = search_next_blame_view;
3285 e5a0f69f 2018-08-18 stsp
3286 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
3287 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
3288 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
3289 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
3290 7cbe629d 2018-08-04 stsp }
3291 7cbe629d 2018-08-04 stsp
3292 e5a0f69f 2018-08-18 stsp static const struct got_error *
3293 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
3294 7cbe629d 2018-08-04 stsp {
3295 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3296 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3297 7cbe629d 2018-08-04 stsp
3298 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
3299 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
3300 e5a0f69f 2018-08-18 stsp
3301 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3302 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
3303 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3304 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3305 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
3306 7cbe629d 2018-08-04 stsp }
3307 e5a0f69f 2018-08-18 stsp
3308 e5a0f69f 2018-08-18 stsp free(s->path);
3309 e5a0f69f 2018-08-18 stsp
3310 e5a0f69f 2018-08-18 stsp return err;
3311 7cbe629d 2018-08-04 stsp }
3312 7cbe629d 2018-08-04 stsp
3313 7cbe629d 2018-08-04 stsp static const struct got_error *
3314 6c4c42e0 2019-06-24 stsp search_start_blame_view(struct tog_view *view)
3315 6c4c42e0 2019-06-24 stsp {
3316 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3317 6c4c42e0 2019-06-24 stsp
3318 6c4c42e0 2019-06-24 stsp s->matched_line = 0;
3319 6c4c42e0 2019-06-24 stsp return NULL;
3320 6c4c42e0 2019-06-24 stsp }
3321 6c4c42e0 2019-06-24 stsp
3322 6c4c42e0 2019-06-24 stsp static int
3323 6c4c42e0 2019-06-24 stsp match_line(const char *line, regex_t *regex)
3324 6c4c42e0 2019-06-24 stsp {
3325 6c4c42e0 2019-06-24 stsp regmatch_t regmatch;
3326 6c4c42e0 2019-06-24 stsp
3327 6c4c42e0 2019-06-24 stsp return regexec(regex, line, 1, &regmatch, 0) == 0;
3328 6c4c42e0 2019-06-24 stsp }
3329 6c4c42e0 2019-06-24 stsp
3330 6c4c42e0 2019-06-24 stsp
3331 6c4c42e0 2019-06-24 stsp static const struct got_error *
3332 6c4c42e0 2019-06-24 stsp search_next_blame_view(struct tog_view *view)
3333 6c4c42e0 2019-06-24 stsp {
3334 6c4c42e0 2019-06-24 stsp struct tog_blame_view_state *s = &view->state.blame;
3335 6c4c42e0 2019-06-24 stsp int lineno;
3336 6c4c42e0 2019-06-24 stsp
3337 6c4c42e0 2019-06-24 stsp if (!view->searching) {
3338 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3339 6c4c42e0 2019-06-24 stsp return NULL;
3340 6c4c42e0 2019-06-24 stsp }
3341 6c4c42e0 2019-06-24 stsp
3342 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3343 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3344 2246482e 2019-06-25 stsp lineno = s->matched_line + 1;
3345 6c4c42e0 2019-06-24 stsp else
3346 2246482e 2019-06-25 stsp lineno = s->matched_line - 1;
3347 6c4c42e0 2019-06-24 stsp } else {
3348 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3349 6c4c42e0 2019-06-24 stsp lineno = 1;
3350 6c4c42e0 2019-06-24 stsp else
3351 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3352 6c4c42e0 2019-06-24 stsp }
3353 6c4c42e0 2019-06-24 stsp
3354 6c4c42e0 2019-06-24 stsp while (1) {
3355 6c4c42e0 2019-06-24 stsp char *line = NULL;
3356 6c4c42e0 2019-06-24 stsp off_t offset;
3357 6c4c42e0 2019-06-24 stsp size_t len;
3358 6c4c42e0 2019-06-24 stsp
3359 6c4c42e0 2019-06-24 stsp if (lineno <= 0 || lineno > s->blame.nlines) {
3360 6c4c42e0 2019-06-24 stsp if (s->matched_line == 0) {
3361 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3362 6c4c42e0 2019-06-24 stsp free(line);
3363 6c4c42e0 2019-06-24 stsp break;
3364 6c4c42e0 2019-06-24 stsp }
3365 2246482e 2019-06-25 stsp
3366 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3367 6c4c42e0 2019-06-24 stsp lineno = 1;
3368 6c4c42e0 2019-06-24 stsp else
3369 6c4c42e0 2019-06-24 stsp lineno = s->blame.nlines;
3370 6c4c42e0 2019-06-24 stsp }
3371 6c4c42e0 2019-06-24 stsp
3372 6c4c42e0 2019-06-24 stsp offset = s->blame.line_offsets[lineno - 1];
3373 6c4c42e0 2019-06-24 stsp if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3374 6c4c42e0 2019-06-24 stsp free(line);
3375 6c4c42e0 2019-06-24 stsp return got_error_from_errno("fseeko");
3376 6c4c42e0 2019-06-24 stsp }
3377 6c4c42e0 2019-06-24 stsp free(line);
3378 6c4c42e0 2019-06-24 stsp line = parse_next_line(s->blame.f, &len);
3379 2246482e 2019-06-25 stsp if (line && match_line(line, &view->regex)) {
3380 6c4c42e0 2019-06-24 stsp view->search_next_done = 1;
3381 6c4c42e0 2019-06-24 stsp s->matched_line = lineno;
3382 6c4c42e0 2019-06-24 stsp free(line);
3383 6c4c42e0 2019-06-24 stsp break;
3384 6c4c42e0 2019-06-24 stsp }
3385 6c4c42e0 2019-06-24 stsp free(line);
3386 6c4c42e0 2019-06-24 stsp if (view->searching == TOG_SEARCH_FORWARD)
3387 6c4c42e0 2019-06-24 stsp lineno++;
3388 6c4c42e0 2019-06-24 stsp else
3389 6c4c42e0 2019-06-24 stsp lineno--;
3390 6c4c42e0 2019-06-24 stsp }
3391 6c4c42e0 2019-06-24 stsp
3392 6c4c42e0 2019-06-24 stsp if (s->matched_line) {
3393 6c4c42e0 2019-06-24 stsp s->first_displayed_line = s->matched_line;
3394 6c4c42e0 2019-06-24 stsp s->selected_line = 1;
3395 6c4c42e0 2019-06-24 stsp }
3396 6c4c42e0 2019-06-24 stsp
3397 6c4c42e0 2019-06-24 stsp return NULL;
3398 6c4c42e0 2019-06-24 stsp }
3399 6c4c42e0 2019-06-24 stsp
3400 6c4c42e0 2019-06-24 stsp static const struct got_error *
3401 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
3402 7cbe629d 2018-08-04 stsp {
3403 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3404 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
3405 2b380cc8 2018-10-24 stsp int errcode;
3406 2b380cc8 2018-10-24 stsp
3407 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
3408 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3409 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
3410 2b380cc8 2018-10-24 stsp if (errcode)
3411 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
3412 2b380cc8 2018-10-24 stsp }
3413 e5a0f69f 2018-08-18 stsp
3414 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3415 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3416 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
3417 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
3418 e5a0f69f 2018-08-18 stsp
3419 669b5ffa 2018-10-07 stsp view_vborder(view);
3420 e5a0f69f 2018-08-18 stsp return err;
3421 e5a0f69f 2018-08-18 stsp }
3422 e5a0f69f 2018-08-18 stsp
3423 e5a0f69f 2018-08-18 stsp static const struct got_error *
3424 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3425 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3426 e5a0f69f 2018-08-18 stsp {
3427 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
3428 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
3429 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
3430 669b5ffa 2018-10-07 stsp int begin_x = 0;
3431 7cbe629d 2018-08-04 stsp
3432 e5a0f69f 2018-08-18 stsp switch (ch) {
3433 1e37a5c2 2019-05-12 jcs case 'q':
3434 1e37a5c2 2019-05-12 jcs s->done = 1;
3435 1e37a5c2 2019-05-12 jcs break;
3436 1e37a5c2 2019-05-12 jcs case 'k':
3437 1e37a5c2 2019-05-12 jcs case KEY_UP:
3438 1e37a5c2 2019-05-12 jcs if (s->selected_line > 1)
3439 1e37a5c2 2019-05-12 jcs s->selected_line--;
3440 1e37a5c2 2019-05-12 jcs else if (s->selected_line == 1 &&
3441 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
3442 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
3443 1e37a5c2 2019-05-12 jcs break;
3444 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
3445 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line == 1) {
3446 1e37a5c2 2019-05-12 jcs s->selected_line = 1;
3447 e5a0f69f 2018-08-18 stsp break;
3448 1e37a5c2 2019-05-12 jcs }
3449 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > view->nlines - 2)
3450 1e37a5c2 2019-05-12 jcs s->first_displayed_line -=
3451 1e37a5c2 2019-05-12 jcs (view->nlines - 2);
3452 1e37a5c2 2019-05-12 jcs else
3453 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
3454 1e37a5c2 2019-05-12 jcs break;
3455 1e37a5c2 2019-05-12 jcs case 'j':
3456 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
3457 1e37a5c2 2019-05-12 jcs if (s->selected_line < view->nlines - 2 &&
3458 1e37a5c2 2019-05-12 jcs s->first_displayed_line +
3459 1e37a5c2 2019-05-12 jcs s->selected_line <= s->blame.nlines)
3460 1e37a5c2 2019-05-12 jcs s->selected_line++;
3461 1e37a5c2 2019-05-12 jcs else if (s->last_displayed_line <
3462 1e37a5c2 2019-05-12 jcs s->blame.nlines)
3463 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
3464 1e37a5c2 2019-05-12 jcs break;
3465 1e37a5c2 2019-05-12 jcs case 'b':
3466 1e37a5c2 2019-05-12 jcs case 'p': {
3467 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3468 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3469 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3470 1e37a5c2 2019-05-12 jcs if (id == NULL)
3471 e5a0f69f 2018-08-18 stsp break;
3472 1e37a5c2 2019-05-12 jcs if (ch == 'p') {
3473 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit;
3474 15a94983 2018-12-23 stsp struct got_object_qid *pid;
3475 1e37a5c2 2019-05-12 jcs struct got_object_id *blob_id = NULL;
3476 1e37a5c2 2019-05-12 jcs int obj_type;
3477 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit,
3478 1e37a5c2 2019-05-12 jcs s->repo, id);
3479 e5a0f69f 2018-08-18 stsp if (err)
3480 e5a0f69f 2018-08-18 stsp break;
3481 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
3482 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
3483 1e37a5c2 2019-05-12 jcs if (pid == NULL) {
3484 15a94983 2018-12-23 stsp got_object_commit_close(commit);
3485 e5a0f69f 2018-08-18 stsp break;
3486 e5a0f69f 2018-08-18 stsp }
3487 1e37a5c2 2019-05-12 jcs /* Check if path history ends here. */
3488 1e37a5c2 2019-05-12 jcs err = got_object_id_by_path(&blob_id, s->repo,
3489 1e37a5c2 2019-05-12 jcs pid->id, s->path);
3490 e5a0f69f 2018-08-18 stsp if (err) {
3491 1e37a5c2 2019-05-12 jcs if (err->code == GOT_ERR_NO_TREE_ENTRY)
3492 1e37a5c2 2019-05-12 jcs err = NULL;
3493 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3494 e5a0f69f 2018-08-18 stsp break;
3495 e5a0f69f 2018-08-18 stsp }
3496 1e37a5c2 2019-05-12 jcs err = got_object_get_type(&obj_type, s->repo,
3497 1e37a5c2 2019-05-12 jcs blob_id);
3498 1e37a5c2 2019-05-12 jcs free(blob_id);
3499 1e37a5c2 2019-05-12 jcs /* Can't blame non-blob type objects. */
3500 1e37a5c2 2019-05-12 jcs if (obj_type != GOT_OBJ_TYPE_BLOB) {
3501 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3502 e5a0f69f 2018-08-18 stsp break;
3503 1e37a5c2 2019-05-12 jcs }
3504 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3505 1e37a5c2 2019-05-12 jcs pid->id);
3506 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3507 1e37a5c2 2019-05-12 jcs } else {
3508 1e37a5c2 2019-05-12 jcs if (got_object_id_cmp(id,
3509 1e37a5c2 2019-05-12 jcs s->blamed_commit->id) == 0)
3510 1e37a5c2 2019-05-12 jcs break;
3511 1e37a5c2 2019-05-12 jcs err = got_object_qid_alloc(&s->blamed_commit,
3512 1e37a5c2 2019-05-12 jcs id);
3513 1e37a5c2 2019-05-12 jcs }
3514 1e37a5c2 2019-05-12 jcs if (err)
3515 e5a0f69f 2018-08-18 stsp break;
3516 1e37a5c2 2019-05-12 jcs s->done = 1;
3517 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3518 1e37a5c2 2019-05-12 jcs s->done = 0;
3519 1e37a5c2 2019-05-12 jcs if (thread_err)
3520 1e37a5c2 2019-05-12 jcs break;
3521 1e37a5c2 2019-05-12 jcs SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3522 1e37a5c2 2019-05-12 jcs s->blamed_commit, entry);
3523 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3524 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3525 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof,
3526 1e37a5c2 2019-05-12 jcs s->path, s->blamed_commit->id, s->repo);
3527 1e37a5c2 2019-05-12 jcs if (err)
3528 1e37a5c2 2019-05-12 jcs break;
3529 1e37a5c2 2019-05-12 jcs break;
3530 1e37a5c2 2019-05-12 jcs }
3531 1e37a5c2 2019-05-12 jcs case 'B': {
3532 1e37a5c2 2019-05-12 jcs struct got_object_qid *first;
3533 1e37a5c2 2019-05-12 jcs first = SIMPLEQ_FIRST(&s->blamed_commits);
3534 1e37a5c2 2019-05-12 jcs if (!got_object_id_cmp(first->id, s->commit_id))
3535 1e37a5c2 2019-05-12 jcs break;
3536 1e37a5c2 2019-05-12 jcs s->done = 1;
3537 1e37a5c2 2019-05-12 jcs thread_err = stop_blame(&s->blame);
3538 1e37a5c2 2019-05-12 jcs s->done = 0;
3539 1e37a5c2 2019-05-12 jcs if (thread_err)
3540 1e37a5c2 2019-05-12 jcs break;
3541 1e37a5c2 2019-05-12 jcs SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3542 1e37a5c2 2019-05-12 jcs got_object_qid_free(s->blamed_commit);
3543 1e37a5c2 2019-05-12 jcs s->blamed_commit =
3544 1e37a5c2 2019-05-12 jcs SIMPLEQ_FIRST(&s->blamed_commits);
3545 1e37a5c2 2019-05-12 jcs err = run_blame(&s->blame, view, &s->blame_complete,
3546 1e37a5c2 2019-05-12 jcs &s->first_displayed_line, &s->last_displayed_line,
3547 1e37a5c2 2019-05-12 jcs &s->selected_line, &s->done, &s->eof, s->path,
3548 1e37a5c2 2019-05-12 jcs s->blamed_commit->id, s->repo);
3549 1e37a5c2 2019-05-12 jcs if (err)
3550 1e37a5c2 2019-05-12 jcs break;
3551 1e37a5c2 2019-05-12 jcs break;
3552 1e37a5c2 2019-05-12 jcs }
3553 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
3554 1e37a5c2 2019-05-12 jcs case '\r': {
3555 1e37a5c2 2019-05-12 jcs struct got_object_id *id = NULL;
3556 1e37a5c2 2019-05-12 jcs struct got_object_qid *pid;
3557 1e37a5c2 2019-05-12 jcs struct got_commit_object *commit = NULL;
3558 1e37a5c2 2019-05-12 jcs id = get_selected_commit_id(s->blame.lines,
3559 1e37a5c2 2019-05-12 jcs s->first_displayed_line, s->selected_line);
3560 1e37a5c2 2019-05-12 jcs if (id == NULL)
3561 1e37a5c2 2019-05-12 jcs break;
3562 1e37a5c2 2019-05-12 jcs err = got_object_open_as_commit(&commit, s->repo, id);
3563 1e37a5c2 2019-05-12 jcs if (err)
3564 1e37a5c2 2019-05-12 jcs break;
3565 1e37a5c2 2019-05-12 jcs pid = SIMPLEQ_FIRST(
3566 1e37a5c2 2019-05-12 jcs got_object_commit_get_parent_ids(commit));
3567 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
3568 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
3569 1e37a5c2 2019-05-12 jcs diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3570 1e37a5c2 2019-05-12 jcs if (diff_view == NULL) {
3571 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3572 638f9024 2019-05-13 stsp err = got_error_from_errno("view_open");
3573 1e37a5c2 2019-05-12 jcs break;
3574 15a94983 2018-12-23 stsp }
3575 1e37a5c2 2019-05-12 jcs err = open_diff_view(diff_view, pid ? pid->id : NULL,
3576 1e37a5c2 2019-05-12 jcs id, NULL, s->refs, s->repo);
3577 1e37a5c2 2019-05-12 jcs got_object_commit_close(commit);
3578 1e37a5c2 2019-05-12 jcs if (err) {
3579 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3580 1e37a5c2 2019-05-12 jcs break;
3581 1e37a5c2 2019-05-12 jcs }
3582 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
3583 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
3584 1e37a5c2 2019-05-12 jcs if (err)
3585 34bc9ec9 2019-02-22 stsp break;
3586 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
3587 1e37a5c2 2019-05-12 jcs if (err) {
3588 1e37a5c2 2019-05-12 jcs view_close(diff_view);
3589 e5a0f69f 2018-08-18 stsp break;
3590 e5a0f69f 2018-08-18 stsp }
3591 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
3592 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
3593 1e37a5c2 2019-05-12 jcs } else
3594 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
3595 1e37a5c2 2019-05-12 jcs if (err)
3596 e5a0f69f 2018-08-18 stsp break;
3597 1e37a5c2 2019-05-12 jcs break;
3598 1e37a5c2 2019-05-12 jcs }
3599 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
3600 1e37a5c2 2019-05-12 jcs case ' ':
3601 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3602 1e37a5c2 2019-05-12 jcs s->selected_line >= MIN(s->blame.nlines,
3603 00ba99a7 2019-05-12 jcs view->nlines - 2)) {
3604 e5a0f69f 2018-08-18 stsp break;
3605 1e37a5c2 2019-05-12 jcs }
3606 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line >= s->blame.nlines &&
3607 1e37a5c2 2019-05-12 jcs s->selected_line < view->nlines - 2) {
3608 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3609 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3610 e5a0f69f 2018-08-18 stsp break;
3611 1e37a5c2 2019-05-12 jcs }
3612 1e37a5c2 2019-05-12 jcs if (s->last_displayed_line + view->nlines - 2
3613 1e37a5c2 2019-05-12 jcs <= s->blame.nlines)
3614 1e37a5c2 2019-05-12 jcs s->first_displayed_line +=
3615 1e37a5c2 2019-05-12 jcs view->nlines - 2;
3616 1e37a5c2 2019-05-12 jcs else
3617 1e37a5c2 2019-05-12 jcs s->first_displayed_line =
3618 1e37a5c2 2019-05-12 jcs s->blame.nlines -
3619 1e37a5c2 2019-05-12 jcs (view->nlines - 3);
3620 1e37a5c2 2019-05-12 jcs break;
3621 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
3622 1e37a5c2 2019-05-12 jcs if (s->selected_line > view->nlines - 2) {
3623 1e37a5c2 2019-05-12 jcs s->selected_line = MIN(s->blame.nlines,
3624 1e37a5c2 2019-05-12 jcs view->nlines - 2);
3625 1e37a5c2 2019-05-12 jcs }
3626 1e37a5c2 2019-05-12 jcs break;
3627 1e37a5c2 2019-05-12 jcs default:
3628 1e37a5c2 2019-05-12 jcs break;
3629 a70480e0 2018-06-23 stsp }
3630 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
3631 a70480e0 2018-06-23 stsp }
3632 a70480e0 2018-06-23 stsp
3633 a70480e0 2018-06-23 stsp static const struct got_error *
3634 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
3635 9f7d7167 2018-04-29 stsp {
3636 a70480e0 2018-06-23 stsp const struct got_error *error;
3637 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
3638 8b473291 2019-02-21 stsp struct got_reflist_head refs;
3639 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
3640 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3641 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3642 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
3643 a70480e0 2018-06-23 stsp int ch;
3644 e1cd8fed 2018-08-01 stsp struct tog_view *view;
3645 a70480e0 2018-06-23 stsp
3646 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
3647 70ac5f84 2019-03-28 stsp
3648 a70480e0 2018-06-23 stsp #ifndef PROFILE
3649 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3650 8e94dd5b 2019-01-04 stsp NULL) == -1)
3651 a70480e0 2018-06-23 stsp err(1, "pledge");
3652 a70480e0 2018-06-23 stsp #endif
3653 a70480e0 2018-06-23 stsp
3654 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3655 a70480e0 2018-06-23 stsp switch (ch) {
3656 a70480e0 2018-06-23 stsp case 'c':
3657 a70480e0 2018-06-23 stsp commit_id_str = optarg;
3658 a70480e0 2018-06-23 stsp break;
3659 69069811 2018-08-02 stsp case 'r':
3660 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3661 69069811 2018-08-02 stsp if (repo_path == NULL)
3662 69069811 2018-08-02 stsp err(1, "-r option");
3663 69069811 2018-08-02 stsp break;
3664 a70480e0 2018-06-23 stsp default:
3665 17020d27 2019-03-07 stsp usage_blame();
3666 a70480e0 2018-06-23 stsp /* NOTREACHED */
3667 a70480e0 2018-06-23 stsp }
3668 a70480e0 2018-06-23 stsp }
3669 a70480e0 2018-06-23 stsp
3670 a70480e0 2018-06-23 stsp argc -= optind;
3671 a70480e0 2018-06-23 stsp argv += optind;
3672 a70480e0 2018-06-23 stsp
3673 69069811 2018-08-02 stsp if (argc == 1)
3674 69069811 2018-08-02 stsp path = argv[0];
3675 69069811 2018-08-02 stsp else
3676 a70480e0 2018-06-23 stsp usage_blame();
3677 69069811 2018-08-02 stsp
3678 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
3679 69069811 2018-08-02 stsp if (cwd == NULL) {
3680 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3681 69069811 2018-08-02 stsp goto done;
3682 69069811 2018-08-02 stsp }
3683 69069811 2018-08-02 stsp if (repo_path == NULL) {
3684 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3685 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3686 69069811 2018-08-02 stsp goto done;
3687 eb41ed75 2019-02-05 stsp else
3688 eb41ed75 2019-02-05 stsp error = NULL;
3689 eb41ed75 2019-02-05 stsp if (worktree) {
3690 eb41ed75 2019-02-05 stsp repo_path =
3691 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3692 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
3693 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3694 eb41ed75 2019-02-05 stsp if (error)
3695 eb41ed75 2019-02-05 stsp goto done;
3696 eb41ed75 2019-02-05 stsp } else {
3697 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
3698 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
3699 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3700 eb41ed75 2019-02-05 stsp goto done;
3701 eb41ed75 2019-02-05 stsp }
3702 69069811 2018-08-02 stsp }
3703 69069811 2018-08-02 stsp }
3704 a915003a 2019-02-05 stsp
3705 a915003a 2019-02-05 stsp init_curses();
3706 69069811 2018-08-02 stsp
3707 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
3708 c02c541e 2019-03-29 stsp if (error != NULL)
3709 8e94dd5b 2019-01-04 stsp goto done;
3710 69069811 2018-08-02 stsp
3711 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
3712 c02c541e 2019-03-29 stsp if (error)
3713 92205607 2019-01-04 stsp goto done;
3714 69069811 2018-08-02 stsp
3715 eb41ed75 2019-02-05 stsp if (worktree) {
3716 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3717 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3718 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3719 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3720 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3721 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3722 eb41ed75 2019-02-05 stsp path) == -1) {
3723 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3724 eb41ed75 2019-02-05 stsp goto done;
3725 eb41ed75 2019-02-05 stsp }
3726 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3727 eb41ed75 2019-02-05 stsp free(p);
3728 eb41ed75 2019-02-05 stsp } else {
3729 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3730 eb41ed75 2019-02-05 stsp }
3731 eb41ed75 2019-02-05 stsp if (error)
3732 69069811 2018-08-02 stsp goto done;
3733 a70480e0 2018-06-23 stsp
3734 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
3735 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
3736 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3737 a70480e0 2018-06-23 stsp if (error != NULL)
3738 66b4983c 2018-06-23 stsp goto done;
3739 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3740 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
3741 a70480e0 2018-06-23 stsp } else {
3742 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&commit_id, commit_id_str, repo);
3743 f2b6a97d 2019-07-15 stsp if (error) {
3744 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
3745 f2b6a97d 2019-07-15 stsp goto done;
3746 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&commit_id,
3747 f2b6a97d 2019-07-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3748 f2b6a97d 2019-07-15 stsp }
3749 a70480e0 2018-06-23 stsp }
3750 a19e88aa 2018-06-23 stsp if (error != NULL)
3751 8b473291 2019-02-21 stsp goto done;
3752 8b473291 2019-02-21 stsp
3753 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
3754 8b473291 2019-02-21 stsp if (error)
3755 a19e88aa 2018-06-23 stsp goto done;
3756 a70480e0 2018-06-23 stsp
3757 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3758 e1cd8fed 2018-08-01 stsp if (view == NULL) {
3759 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
3760 e1cd8fed 2018-08-01 stsp goto done;
3761 e1cd8fed 2018-08-01 stsp }
3762 8b473291 2019-02-21 stsp error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3763 7cbe629d 2018-08-04 stsp if (error)
3764 7cbe629d 2018-08-04 stsp goto done;
3765 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3766 a70480e0 2018-06-23 stsp done:
3767 69069811 2018-08-02 stsp free(repo_path);
3768 69069811 2018-08-02 stsp free(cwd);
3769 a70480e0 2018-06-23 stsp free(commit_id);
3770 eb41ed75 2019-02-05 stsp if (worktree)
3771 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
3772 a70480e0 2018-06-23 stsp if (repo)
3773 a70480e0 2018-06-23 stsp got_repo_close(repo);
3774 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3775 a70480e0 2018-06-23 stsp return error;
3776 ffd1d5e5 2018-06-23 stsp }
3777 ffd1d5e5 2018-06-23 stsp
3778 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3779 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
3780 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
3781 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
3782 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
3783 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
3784 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
3785 ffd1d5e5 2018-06-23 stsp {
3786 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3787 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
3788 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
3789 ffd1d5e5 2018-06-23 stsp int width, n;
3790 ffd1d5e5 2018-06-23 stsp
3791 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
3792 ffd1d5e5 2018-06-23 stsp
3793 f7d12f7e 2018-08-01 stsp werase(view->window);
3794 ffd1d5e5 2018-06-23 stsp
3795 ffd1d5e5 2018-06-23 stsp if (limit == 0)
3796 ffd1d5e5 2018-06-23 stsp return NULL;
3797 ffd1d5e5 2018-06-23 stsp
3798 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
3799 ffd1d5e5 2018-06-23 stsp if (err)
3800 ffd1d5e5 2018-06-23 stsp return err;
3801 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3802 a3404814 2018-09-02 stsp wstandout(view->window);
3803 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3804 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
3805 a3404814 2018-09-02 stsp wstandend(view->window);
3806 2550e4c3 2018-07-13 stsp free(wline);
3807 2550e4c3 2018-07-13 stsp wline = NULL;
3808 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3809 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3810 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3811 ffd1d5e5 2018-06-23 stsp return NULL;
3812 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
3813 ce52c690 2018-06-23 stsp if (err)
3814 ce52c690 2018-06-23 stsp return err;
3815 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3816 2550e4c3 2018-07-13 stsp free(wline);
3817 2550e4c3 2018-07-13 stsp wline = NULL;
3818 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3819 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3820 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3821 ffd1d5e5 2018-06-23 stsp return NULL;
3822 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3823 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
3824 a1eca9bb 2018-06-23 stsp return NULL;
3825 ffd1d5e5 2018-06-23 stsp
3826 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3827 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
3828 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
3829 0cf4efb1 2018-09-29 stsp if (view->focussed)
3830 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3831 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
3832 ffd1d5e5 2018-06-23 stsp }
3833 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
3834 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
3835 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3836 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3837 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3838 ffd1d5e5 2018-06-23 stsp return NULL;
3839 ffd1d5e5 2018-06-23 stsp n = 1;
3840 ffd1d5e5 2018-06-23 stsp } else {
3841 ffd1d5e5 2018-06-23 stsp n = 0;
3842 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
3843 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3844 ffd1d5e5 2018-06-23 stsp }
3845 ffd1d5e5 2018-06-23 stsp
3846 ffd1d5e5 2018-06-23 stsp while (te) {
3847 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
3848 1d13200f 2018-07-12 stsp
3849 1d13200f 2018-07-12 stsp if (show_ids) {
3850 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
3851 1d13200f 2018-07-12 stsp if (err)
3852 638f9024 2019-05-13 stsp return got_error_from_errno(
3853 230a42bd 2019-05-11 jcs "got_object_id_str");
3854 1d13200f 2018-07-12 stsp }
3855 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3856 d8355ef1 2019-02-10 stsp te->name, S_ISDIR(te->mode) ? "/" :
3857 d8355ef1 2019-02-10 stsp ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3858 1d13200f 2018-07-12 stsp free(id_str);
3859 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3860 1d13200f 2018-07-12 stsp }
3861 1d13200f 2018-07-12 stsp free(id_str);
3862 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
3863 ffd1d5e5 2018-06-23 stsp if (err) {
3864 ffd1d5e5 2018-06-23 stsp free(line);
3865 ffd1d5e5 2018-06-23 stsp break;
3866 ffd1d5e5 2018-06-23 stsp }
3867 ffd1d5e5 2018-06-23 stsp if (n == selected) {
3868 0cf4efb1 2018-09-29 stsp if (view->focussed)
3869 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3870 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
3871 ffd1d5e5 2018-06-23 stsp }
3872 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3873 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
3874 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3875 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
3876 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3877 ffd1d5e5 2018-06-23 stsp free(line);
3878 2550e4c3 2018-07-13 stsp free(wline);
3879 2550e4c3 2018-07-13 stsp wline = NULL;
3880 ffd1d5e5 2018-06-23 stsp n++;
3881 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3882 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
3883 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3884 ffd1d5e5 2018-06-23 stsp break;
3885 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3886 ffd1d5e5 2018-06-23 stsp }
3887 ffd1d5e5 2018-06-23 stsp
3888 ffd1d5e5 2018-06-23 stsp return err;
3889 ffd1d5e5 2018-06-23 stsp }
3890 ffd1d5e5 2018-06-23 stsp
3891 ffd1d5e5 2018-06-23 stsp static void
3892 34bc9ec9 2019-02-22 stsp tree_scroll_up(struct tog_view *view,
3893 34bc9ec9 2019-02-22 stsp struct got_tree_entry **first_displayed_entry, int maxscroll,
3894 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
3895 ffd1d5e5 2018-06-23 stsp {
3896 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
3897 ffd1d5e5 2018-06-23 stsp int i;
3898 ffd1d5e5 2018-06-23 stsp
3899 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == NULL)
3900 ffd1d5e5 2018-06-23 stsp return;
3901 ffd1d5e5 2018-06-23 stsp
3902 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3903 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
3904 ffd1d5e5 2018-06-23 stsp if (!isroot)
3905 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3906 ffd1d5e5 2018-06-23 stsp return;
3907 ffd1d5e5 2018-06-23 stsp }
3908 ffd1d5e5 2018-06-23 stsp
3909 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
3910 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
3911 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
3912 ffd1d5e5 2018-06-23 stsp prev = te;
3913 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3914 ffd1d5e5 2018-06-23 stsp }
3915 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
3916 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3917 ffd1d5e5 2018-06-23 stsp }
3918 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3919 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3920 ffd1d5e5 2018-06-23 stsp }
3921 ffd1d5e5 2018-06-23 stsp
3922 768394f3 2019-01-24 stsp static int
3923 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3924 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
3925 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
3926 ffd1d5e5 2018-06-23 stsp {
3927 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
3928 ffd1d5e5 2018-06-23 stsp int n = 0;
3929 ffd1d5e5 2018-06-23 stsp
3930 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
3931 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3932 ffd1d5e5 2018-06-23 stsp else
3933 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
3934 768394f3 2019-01-24 stsp last = last_displayed_entry;
3935 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
3936 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
3937 768394f3 2019-01-24 stsp if (last) {
3938 768394f3 2019-01-24 stsp *first_displayed_entry = next;
3939 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
3940 768394f3 2019-01-24 stsp }
3941 ffd1d5e5 2018-06-23 stsp }
3942 768394f3 2019-01-24 stsp return n;
3943 ffd1d5e5 2018-06-23 stsp }
3944 ffd1d5e5 2018-06-23 stsp
3945 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3946 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
3947 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
3948 ffd1d5e5 2018-06-23 stsp {
3949 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
3950 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
3951 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
3952 ffd1d5e5 2018-06-23 stsp
3953 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
3954 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
3955 ce52c690 2018-06-23 stsp if (te)
3956 ce52c690 2018-06-23 stsp len += strlen(te->name);
3957 ce52c690 2018-06-23 stsp
3958 ce52c690 2018-06-23 stsp *path = calloc(1, len);
3959 ffd1d5e5 2018-06-23 stsp if (path == NULL)
3960 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
3961 ffd1d5e5 2018-06-23 stsp
3962 ce52c690 2018-06-23 stsp (*path)[0] = '/';
3963 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
3964 d9765a41 2018-06-23 stsp while (pt) {
3965 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3966 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3967 cb2ebc8a 2018-06-23 stsp goto done;
3968 cb2ebc8a 2018-06-23 stsp }
3969 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
3970 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3971 cb2ebc8a 2018-06-23 stsp goto done;
3972 cb2ebc8a 2018-06-23 stsp }
3973 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3974 ffd1d5e5 2018-06-23 stsp }
3975 ce52c690 2018-06-23 stsp if (te) {
3976 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3977 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3978 ce52c690 2018-06-23 stsp goto done;
3979 ce52c690 2018-06-23 stsp }
3980 cb2ebc8a 2018-06-23 stsp }
3981 ce52c690 2018-06-23 stsp done:
3982 ce52c690 2018-06-23 stsp if (err) {
3983 ce52c690 2018-06-23 stsp free(*path);
3984 ce52c690 2018-06-23 stsp *path = NULL;
3985 ce52c690 2018-06-23 stsp }
3986 ce52c690 2018-06-23 stsp return err;
3987 ce52c690 2018-06-23 stsp }
3988 ce52c690 2018-06-23 stsp
3989 ce52c690 2018-06-23 stsp static const struct got_error *
3990 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3991 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3992 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
3993 8b473291 2019-02-21 stsp struct got_repository *repo)
3994 ce52c690 2018-06-23 stsp {
3995 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3996 ce52c690 2018-06-23 stsp char *path;
3997 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3998 a0de39f3 2019-08-09 stsp
3999 a0de39f3 2019-08-09 stsp *new_view = NULL;
4000 69efd4c4 2018-07-18 stsp
4001 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
4002 ce52c690 2018-06-23 stsp if (err)
4003 ce52c690 2018-06-23 stsp return err;
4004 ffd1d5e5 2018-06-23 stsp
4005 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4006 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
4007 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
4008 cdf1ee82 2018-08-01 stsp
4009 8b473291 2019-02-21 stsp err = open_blame_view(blame_view, path, commit_id, refs, repo);
4010 e5a0f69f 2018-08-18 stsp if (err) {
4011 e5a0f69f 2018-08-18 stsp view_close(blame_view);
4012 e5a0f69f 2018-08-18 stsp free(path);
4013 e5a0f69f 2018-08-18 stsp } else
4014 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
4015 69efd4c4 2018-07-18 stsp return err;
4016 69efd4c4 2018-07-18 stsp }
4017 69efd4c4 2018-07-18 stsp
4018 69efd4c4 2018-07-18 stsp static const struct got_error *
4019 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
4020 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
4021 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4022 8b473291 2019-02-21 stsp struct got_repository *repo)
4023 69efd4c4 2018-07-18 stsp {
4024 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
4025 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
4026 69efd4c4 2018-07-18 stsp char *path;
4027 a0de39f3 2019-08-09 stsp
4028 a0de39f3 2019-08-09 stsp *new_view = NULL;
4029 69efd4c4 2018-07-18 stsp
4030 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4031 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
4032 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
4033 e5a0f69f 2018-08-18 stsp
4034 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
4035 69efd4c4 2018-07-18 stsp if (err)
4036 69efd4c4 2018-07-18 stsp return err;
4037 69efd4c4 2018-07-18 stsp
4038 d01904d4 2019-06-25 stsp err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4039 ba4f502b 2018-08-04 stsp if (err)
4040 e5a0f69f 2018-08-18 stsp view_close(log_view);
4041 e5a0f69f 2018-08-18 stsp else
4042 e5a0f69f 2018-08-18 stsp *new_view = log_view;
4043 cb2ebc8a 2018-06-23 stsp free(path);
4044 cb2ebc8a 2018-06-23 stsp return err;
4045 ffd1d5e5 2018-06-23 stsp }
4046 ffd1d5e5 2018-06-23 stsp
4047 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4048 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
4049 8b473291 2019-02-21 stsp struct got_object_id *commit_id, struct got_reflist_head *refs,
4050 8b473291 2019-02-21 stsp struct got_repository *repo)
4051 ffd1d5e5 2018-06-23 stsp {
4052 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
4053 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
4054 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4055 ffd1d5e5 2018-06-23 stsp
4056 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
4057 ffd1d5e5 2018-06-23 stsp
4058 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
4059 ffd1d5e5 2018-06-23 stsp if (err != NULL)
4060 ffd1d5e5 2018-06-23 stsp goto done;
4061 ffd1d5e5 2018-06-23 stsp
4062 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4063 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4064 ffd1d5e5 2018-06-23 stsp goto done;
4065 ffd1d5e5 2018-06-23 stsp }
4066 ffd1d5e5 2018-06-23 stsp
4067 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
4068 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
4069 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4070 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
4071 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
4072 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
4073 6484ec90 2018-09-29 stsp goto done;
4074 6484ec90 2018-09-29 stsp }
4075 8b473291 2019-02-21 stsp s->refs = refs;
4076 fb2756b9 2018-08-04 stsp s->repo = repo;
4077 e5a0f69f 2018-08-18 stsp
4078 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
4079 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
4080 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
4081 7c32bd05 2019-06-22 stsp view->search_start = search_start_tree_view;
4082 7c32bd05 2019-06-22 stsp view->search_next = search_next_tree_view;
4083 ad80ab7b 2018-08-04 stsp done:
4084 ad80ab7b 2018-08-04 stsp free(commit_id_str);
4085 6484ec90 2018-09-29 stsp if (err) {
4086 fb2756b9 2018-08-04 stsp free(s->tree_label);
4087 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4088 6484ec90 2018-09-29 stsp }
4089 ad80ab7b 2018-08-04 stsp return err;
4090 ad80ab7b 2018-08-04 stsp }
4091 ad80ab7b 2018-08-04 stsp
4092 e5a0f69f 2018-08-18 stsp static const struct got_error *
4093 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
4094 ad80ab7b 2018-08-04 stsp {
4095 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4096 ad80ab7b 2018-08-04 stsp
4097 fb2756b9 2018-08-04 stsp free(s->tree_label);
4098 6484ec90 2018-09-29 stsp s->tree_label = NULL;
4099 6484ec90 2018-09-29 stsp free(s->commit_id);
4100 6484ec90 2018-09-29 stsp s->commit_id = NULL;
4101 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
4102 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
4103 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
4104 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
4105 ad80ab7b 2018-08-04 stsp free(parent);
4106 ad80ab7b 2018-08-04 stsp
4107 ad80ab7b 2018-08-04 stsp }
4108 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
4109 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
4110 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
4111 7c32bd05 2019-06-22 stsp
4112 7c32bd05 2019-06-22 stsp return NULL;
4113 7c32bd05 2019-06-22 stsp }
4114 7c32bd05 2019-06-22 stsp
4115 7c32bd05 2019-06-22 stsp static const struct got_error *
4116 7c32bd05 2019-06-22 stsp search_start_tree_view(struct tog_view *view)
4117 7c32bd05 2019-06-22 stsp {
4118 7c32bd05 2019-06-22 stsp struct tog_tree_view_state *s = &view->state.tree;
4119 7c32bd05 2019-06-22 stsp
4120 7c32bd05 2019-06-22 stsp s->matched_entry = NULL;
4121 7c32bd05 2019-06-22 stsp return NULL;
4122 7c32bd05 2019-06-22 stsp }
4123 7c32bd05 2019-06-22 stsp
4124 7c32bd05 2019-06-22 stsp static int
4125 7c32bd05 2019-06-22 stsp match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4126 7c32bd05 2019-06-22 stsp {
4127 7c32bd05 2019-06-22 stsp regmatch_t regmatch;
4128 7c32bd05 2019-06-22 stsp
4129 7c32bd05 2019-06-22 stsp return regexec(regex, te->name, 1, &regmatch, 0) == 0;
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_next_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 a0de39f3 2019-08-09 stsp struct got_tree_entry *entry = NULL, *te;
4137 7c32bd05 2019-06-22 stsp
4138 7c32bd05 2019-06-22 stsp if (!view->searching) {
4139 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4140 7c32bd05 2019-06-22 stsp return NULL;
4141 7c32bd05 2019-06-22 stsp }
4142 7c32bd05 2019-06-22 stsp
4143 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4144 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD) {
4145 7c32bd05 2019-06-22 stsp if (s->selected_entry)
4146 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4147 7c32bd05 2019-06-22 stsp else
4148 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4149 7c32bd05 2019-06-22 stsp }
4150 7c32bd05 2019-06-22 stsp else {
4151 7c32bd05 2019-06-22 stsp if (s->selected_entry == NULL) {
4152 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4153 7c32bd05 2019-06-22 stsp entry = te;
4154 7c32bd05 2019-06-22 stsp } else {
4155 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4156 7c32bd05 2019-06-22 stsp entry = te;
4157 7c32bd05 2019-06-22 stsp if (SIMPLEQ_NEXT(te, entry) ==
4158 7c32bd05 2019-06-22 stsp s->selected_entry)
4159 7c32bd05 2019-06-22 stsp break;
4160 7c32bd05 2019-06-22 stsp }
4161 7c32bd05 2019-06-22 stsp }
4162 7c32bd05 2019-06-22 stsp }
4163 7c32bd05 2019-06-22 stsp } else {
4164 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4165 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4166 7c32bd05 2019-06-22 stsp else {
4167 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4168 7c32bd05 2019-06-22 stsp entry = te;
4169 7c32bd05 2019-06-22 stsp }
4170 7c32bd05 2019-06-22 stsp }
4171 7c32bd05 2019-06-22 stsp
4172 7c32bd05 2019-06-22 stsp while (1) {
4173 7c32bd05 2019-06-22 stsp if (entry == NULL) {
4174 ac66afb8 2019-06-24 stsp if (s->matched_entry == NULL) {
4175 ac66afb8 2019-06-24 stsp view->search_next_done = 1;
4176 ac66afb8 2019-06-24 stsp return NULL;
4177 ac66afb8 2019-06-24 stsp }
4178 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4179 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_FIRST(&s->entries->head);
4180 7c32bd05 2019-06-22 stsp else {
4181 7c32bd05 2019-06-22 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4182 7c32bd05 2019-06-22 stsp entry = te;
4183 7c32bd05 2019-06-22 stsp }
4184 7c32bd05 2019-06-22 stsp }
4185 7c32bd05 2019-06-22 stsp
4186 7c32bd05 2019-06-22 stsp if (match_tree_entry(entry, &view->regex)) {
4187 7c32bd05 2019-06-22 stsp view->search_next_done = 1;
4188 7c32bd05 2019-06-22 stsp s->matched_entry = entry;
4189 7c32bd05 2019-06-22 stsp break;
4190 7c32bd05 2019-06-22 stsp }
4191 7c32bd05 2019-06-22 stsp
4192 7c32bd05 2019-06-22 stsp if (view->searching == TOG_SEARCH_FORWARD)
4193 7c32bd05 2019-06-22 stsp entry = SIMPLEQ_NEXT(entry, entry);
4194 7c32bd05 2019-06-22 stsp else {
4195 7c32bd05 2019-06-22 stsp if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4196 7c32bd05 2019-06-22 stsp entry = NULL;
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 if (SIMPLEQ_NEXT(te, entry) == entry) {
4200 7c32bd05 2019-06-22 stsp entry = te;
4201 7c32bd05 2019-06-22 stsp break;
4202 7c32bd05 2019-06-22 stsp }
4203 7c32bd05 2019-06-22 stsp }
4204 7c32bd05 2019-06-22 stsp }
4205 7c32bd05 2019-06-22 stsp }
4206 7c32bd05 2019-06-22 stsp }
4207 e5a0f69f 2018-08-18 stsp
4208 7c32bd05 2019-06-22 stsp if (s->matched_entry) {
4209 7c32bd05 2019-06-22 stsp s->first_displayed_entry = s->matched_entry;
4210 7c32bd05 2019-06-22 stsp s->selected = 0;
4211 7c32bd05 2019-06-22 stsp }
4212 7c32bd05 2019-06-22 stsp
4213 e5a0f69f 2018-08-18 stsp return NULL;
4214 ad80ab7b 2018-08-04 stsp }
4215 ad80ab7b 2018-08-04 stsp
4216 ad80ab7b 2018-08-04 stsp static const struct got_error *
4217 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
4218 ad80ab7b 2018-08-04 stsp {
4219 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
4220 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
4221 e5a0f69f 2018-08-18 stsp char *parent_path;
4222 ad80ab7b 2018-08-04 stsp
4223 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
4224 e5a0f69f 2018-08-18 stsp if (err)
4225 e5a0f69f 2018-08-18 stsp return err;
4226 ffd1d5e5 2018-06-23 stsp
4227 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
4228 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
4229 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4230 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
4231 e5a0f69f 2018-08-18 stsp free(parent_path);
4232 669b5ffa 2018-10-07 stsp
4233 669b5ffa 2018-10-07 stsp view_vborder(view);
4234 e5a0f69f 2018-08-18 stsp return err;
4235 e5a0f69f 2018-08-18 stsp }
4236 ce52c690 2018-06-23 stsp
4237 e5a0f69f 2018-08-18 stsp static const struct got_error *
4238 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4239 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
4240 e5a0f69f 2018-08-18 stsp {
4241 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
4242 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
4243 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
4244 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
4245 ffd1d5e5 2018-06-23 stsp
4246 e5a0f69f 2018-08-18 stsp switch (ch) {
4247 1e37a5c2 2019-05-12 jcs case 'i':
4248 1e37a5c2 2019-05-12 jcs s->show_ids = !s->show_ids;
4249 1e37a5c2 2019-05-12 jcs break;
4250 1e37a5c2 2019-05-12 jcs case 'l':
4251 1e37a5c2 2019-05-12 jcs if (!s->selected_entry)
4252 ffd1d5e5 2018-06-23 stsp break;
4253 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
4254 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
4255 1e37a5c2 2019-05-12 jcs err = log_tree_entry(&log_view, begin_x,
4256 1e37a5c2 2019-05-12 jcs s->selected_entry, &s->parents,
4257 1e37a5c2 2019-05-12 jcs s->commit_id, s->refs, s->repo);
4258 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
4259 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
4260 1e37a5c2 2019-05-12 jcs if (err)
4261 1e37a5c2 2019-05-12 jcs return err;
4262 1e37a5c2 2019-05-12 jcs err = view_set_child(view, log_view);
4263 1e37a5c2 2019-05-12 jcs if (err) {
4264 1e37a5c2 2019-05-12 jcs view_close(log_view);
4265 669b5ffa 2018-10-07 stsp break;
4266 1e37a5c2 2019-05-12 jcs }
4267 1e37a5c2 2019-05-12 jcs *focus_view = log_view;
4268 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
4269 1e37a5c2 2019-05-12 jcs } else
4270 1e37a5c2 2019-05-12 jcs *new_view = log_view;
4271 1e37a5c2 2019-05-12 jcs break;
4272 1e37a5c2 2019-05-12 jcs case 'k':
4273 1e37a5c2 2019-05-12 jcs case KEY_UP:
4274 1e37a5c2 2019-05-12 jcs if (s->selected > 0) {
4275 1e37a5c2 2019-05-12 jcs s->selected--;
4276 1e37a5c2 2019-05-12 jcs if (s->selected == 0)
4277 1e37a5c2 2019-05-12 jcs break;
4278 1e37a5c2 2019-05-12 jcs }
4279 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
4280 1e37a5c2 2019-05-12 jcs break;
4281 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry, 1,
4282 1e37a5c2 2019-05-12 jcs s->entries, s->tree == s->root);
4283 1e37a5c2 2019-05-12 jcs break;
4284 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
4285 1e37a5c2 2019-05-12 jcs tree_scroll_up(view, &s->first_displayed_entry,
4286 1e37a5c2 2019-05-12 jcs MAX(0, view->nlines - 4 - s->selected), s->entries,
4287 1e37a5c2 2019-05-12 jcs s->tree == s->root);
4288 1e37a5c2 2019-05-12 jcs s->selected = 0;
4289 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_FIRST(&s->entries->head) ==
4290 1e37a5c2 2019-05-12 jcs s->first_displayed_entry && s->tree != s->root)
4291 1e37a5c2 2019-05-12 jcs s->first_displayed_entry = NULL;
4292 1e37a5c2 2019-05-12 jcs break;
4293 1e37a5c2 2019-05-12 jcs case 'j':
4294 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
4295 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1) {
4296 1e37a5c2 2019-05-12 jcs s->selected++;
4297 1e37a5c2 2019-05-12 jcs break;
4298 1e37a5c2 2019-05-12 jcs }
4299 00ba99a7 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4300 1e37a5c2 2019-05-12 jcs /* can't scroll any further */
4301 1e37a5c2 2019-05-12 jcs break;
4302 1e37a5c2 2019-05-12 jcs tree_scroll_down(&s->first_displayed_entry, 1,
4303 1e37a5c2 2019-05-12 jcs s->last_displayed_entry, s->entries);
4304 1e37a5c2 2019-05-12 jcs break;
4305 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
4306 1e37a5c2 2019-05-12 jcs if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4307 1e37a5c2 2019-05-12 jcs == NULL) {
4308 1e37a5c2 2019-05-12 jcs /* can't scroll any further; move cursor down */
4309 1e37a5c2 2019-05-12 jcs if (s->selected < s->ndisplayed - 1)
4310 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4311 1e37a5c2 2019-05-12 jcs break;
4312 1e37a5c2 2019-05-12 jcs }
4313 1e37a5c2 2019-05-12 jcs nscrolled = tree_scroll_down(&s->first_displayed_entry,
4314 1e37a5c2 2019-05-12 jcs view->nlines, s->last_displayed_entry, s->entries);
4315 1e37a5c2 2019-05-12 jcs if (nscrolled < view->nlines) {
4316 1e37a5c2 2019-05-12 jcs int ndisplayed = 0;
4317 1e37a5c2 2019-05-12 jcs struct got_tree_entry *te;
4318 1e37a5c2 2019-05-12 jcs te = s->first_displayed_entry;
4319 1e37a5c2 2019-05-12 jcs do {
4320 1e37a5c2 2019-05-12 jcs ndisplayed++;
4321 1e37a5c2 2019-05-12 jcs te = SIMPLEQ_NEXT(te, entry);
4322 1e37a5c2 2019-05-12 jcs } while (te);
4323 1e37a5c2 2019-05-12 jcs s->selected = ndisplayed - 1;
4324 1e37a5c2 2019-05-12 jcs }
4325 1e37a5c2 2019-05-12 jcs break;
4326 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
4327 1e37a5c2 2019-05-12 jcs case '\r':
4328 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
4329 6f10d58e 2019-05-12 stsp if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4330 6f10d58e 2019-05-12 stsp struct tog_parent_tree *parent;
4331 1e37a5c2 2019-05-12 jcs /* user selected '..' */
4332 1e37a5c2 2019-05-12 jcs if (s->tree == s->root)
4333 1e37a5c2 2019-05-12 jcs break;
4334 1e37a5c2 2019-05-12 jcs parent = TAILQ_FIRST(&s->parents);
4335 1e37a5c2 2019-05-12 jcs TAILQ_REMOVE(&s->parents, parent,
4336 1e37a5c2 2019-05-12 jcs entry);
4337 1e37a5c2 2019-05-12 jcs got_object_tree_close(s->tree);
4338 1e37a5c2 2019-05-12 jcs s->tree = parent->tree;
4339 1e37a5c2 2019-05-12 jcs s->entries =
4340 1e37a5c2 2019-05-12 jcs got_object_tree_get_entries(s->tree);
4341 1e37a5c2 2019-05-12 jcs s->first_displayed_entry =
4342 1e37a5c2 2019-05-12 jcs parent->first_displayed_entry;
4343 1e37a5c2 2019-05-12 jcs s->selected_entry =
4344 1e37a5c2 2019-05-12 jcs parent->selected_entry;
4345 1e37a5c2 2019-05-12 jcs s->selected = parent->selected;
4346 1e37a5c2 2019-05-12 jcs free(parent);
4347 1e37a5c2 2019-05-12 jcs } else if (S_ISDIR(s->selected_entry->mode)) {
4348 941e9f74 2019-05-21 stsp struct got_tree_object *subtree;
4349 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&subtree,
4350 1e37a5c2 2019-05-12 jcs s->repo, s->selected_entry->id);
4351 1e37a5c2 2019-05-12 jcs if (err)
4352 1e37a5c2 2019-05-12 jcs break;
4353 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(subtree, s);
4354 941e9f74 2019-05-21 stsp if (err) {
4355 941e9f74 2019-05-21 stsp got_object_tree_close(subtree);
4356 1e37a5c2 2019-05-12 jcs break;
4357 1e37a5c2 2019-05-12 jcs }
4358 1e37a5c2 2019-05-12 jcs } else if (S_ISREG(s->selected_entry->mode)) {
4359 1e37a5c2 2019-05-12 jcs struct tog_view *blame_view;
4360 1e37a5c2 2019-05-12 jcs int begin_x = view_is_parent_view(view) ?
4361 1e37a5c2 2019-05-12 jcs view_split_begin_x(view->begin_x) : 0;
4362 1e37a5c2 2019-05-12 jcs
4363 1e37a5c2 2019-05-12 jcs err = blame_tree_entry(&blame_view, begin_x,
4364 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
4365 8b473291 2019-02-21 stsp s->commit_id, s->refs, s->repo);
4366 1e37a5c2 2019-05-12 jcs if (err)
4367 1e37a5c2 2019-05-12 jcs break;
4368 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
4369 669b5ffa 2018-10-07 stsp err = view_close_child(view);
4370 669b5ffa 2018-10-07 stsp if (err)
4371 669b5ffa 2018-10-07 stsp return err;
4372 1e37a5c2 2019-05-12 jcs err = view_set_child(view, blame_view);
4373 669b5ffa 2018-10-07 stsp if (err) {
4374 1e37a5c2 2019-05-12 jcs view_close(blame_view);
4375 669b5ffa 2018-10-07 stsp break;
4376 669b5ffa 2018-10-07 stsp }
4377 1e37a5c2 2019-05-12 jcs *focus_view = blame_view;
4378 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
4379 669b5ffa 2018-10-07 stsp } else
4380 1e37a5c2 2019-05-12 jcs *new_view = blame_view;
4381 1e37a5c2 2019-05-12 jcs }
4382 1e37a5c2 2019-05-12 jcs break;
4383 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
4384 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines)
4385 1e37a5c2 2019-05-12 jcs s->selected = s->ndisplayed - 1;
4386 1e37a5c2 2019-05-12 jcs break;
4387 1e37a5c2 2019-05-12 jcs default:
4388 1e37a5c2 2019-05-12 jcs break;
4389 ffd1d5e5 2018-06-23 stsp }
4390 e5a0f69f 2018-08-18 stsp
4391 ffd1d5e5 2018-06-23 stsp return err;
4392 9f7d7167 2018-04-29 stsp }
4393 9f7d7167 2018-04-29 stsp
4394 ffd1d5e5 2018-06-23 stsp __dead static void
4395 ffd1d5e5 2018-06-23 stsp usage_tree(void)
4396 ffd1d5e5 2018-06-23 stsp {
4397 ffd1d5e5 2018-06-23 stsp endwin();
4398 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4399 ffd1d5e5 2018-06-23 stsp getprogname());
4400 ffd1d5e5 2018-06-23 stsp exit(1);
4401 ffd1d5e5 2018-06-23 stsp }
4402 ffd1d5e5 2018-06-23 stsp
4403 ffd1d5e5 2018-06-23 stsp static const struct got_error *
4404 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
4405 ffd1d5e5 2018-06-23 stsp {
4406 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
4407 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
4408 8b473291 2019-02-21 stsp struct got_reflist_head refs;
4409 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
4410 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
4411 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
4412 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
4413 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
4414 ffd1d5e5 2018-06-23 stsp int ch;
4415 5221c383 2018-08-01 stsp struct tog_view *view;
4416 70ac5f84 2019-03-28 stsp
4417 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
4418 ffd1d5e5 2018-06-23 stsp
4419 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
4420 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4421 d188b9a6 2019-01-04 stsp NULL) == -1)
4422 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
4423 ffd1d5e5 2018-06-23 stsp #endif
4424 ffd1d5e5 2018-06-23 stsp
4425 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
4426 ffd1d5e5 2018-06-23 stsp switch (ch) {
4427 ffd1d5e5 2018-06-23 stsp case 'c':
4428 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
4429 ffd1d5e5 2018-06-23 stsp break;
4430 ffd1d5e5 2018-06-23 stsp default:
4431 17020d27 2019-03-07 stsp usage_tree();
4432 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
4433 ffd1d5e5 2018-06-23 stsp }
4434 ffd1d5e5 2018-06-23 stsp }
4435 ffd1d5e5 2018-06-23 stsp
4436 ffd1d5e5 2018-06-23 stsp argc -= optind;
4437 ffd1d5e5 2018-06-23 stsp argv += optind;
4438 ffd1d5e5 2018-06-23 stsp
4439 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
4440 52185f70 2019-02-05 stsp struct got_worktree *worktree;
4441 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
4442 52185f70 2019-02-05 stsp if (cwd == NULL)
4443 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
4444 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4445 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4446 52185f70 2019-02-05 stsp goto done;
4447 52185f70 2019-02-05 stsp if (worktree) {
4448 52185f70 2019-02-05 stsp free(cwd);
4449 52185f70 2019-02-05 stsp repo_path =
4450 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4451 52185f70 2019-02-05 stsp got_worktree_close(worktree);
4452 52185f70 2019-02-05 stsp } else
4453 52185f70 2019-02-05 stsp repo_path = cwd;
4454 52185f70 2019-02-05 stsp if (repo_path == NULL) {
4455 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4456 52185f70 2019-02-05 stsp goto done;
4457 52185f70 2019-02-05 stsp }
4458 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
4459 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
4460 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
4461 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
4462 ffd1d5e5 2018-06-23 stsp } else
4463 ffd1d5e5 2018-06-23 stsp usage_log();
4464 a915003a 2019-02-05 stsp
4465 a915003a 2019-02-05 stsp init_curses();
4466 ffd1d5e5 2018-06-23 stsp
4467 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
4468 c02c541e 2019-03-29 stsp if (error != NULL)
4469 52185f70 2019-02-05 stsp goto done;
4470 d188b9a6 2019-01-04 stsp
4471 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
4472 c02c541e 2019-03-29 stsp if (error)
4473 52185f70 2019-02-05 stsp goto done;
4474 ffd1d5e5 2018-06-23 stsp
4475 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
4476 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4477 f2b6a97d 2019-07-15 stsp else {
4478 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4479 f2b6a97d 2019-07-15 stsp if (error) {
4480 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
4481 f2b6a97d 2019-07-15 stsp goto done;
4482 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&commit_id,
4483 f2b6a97d 2019-07-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4484 f2b6a97d 2019-07-15 stsp }
4485 f2b6a97d 2019-07-15 stsp }
4486 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4487 ffd1d5e5 2018-06-23 stsp goto done;
4488 ffd1d5e5 2018-06-23 stsp
4489 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4490 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4491 ffd1d5e5 2018-06-23 stsp goto done;
4492 ffd1d5e5 2018-06-23 stsp
4493 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
4494 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
4495 ffd1d5e5 2018-06-23 stsp if (error != NULL)
4496 8b473291 2019-02-21 stsp goto done;
4497 8b473291 2019-02-21 stsp
4498 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
4499 8b473291 2019-02-21 stsp if (error)
4500 ffd1d5e5 2018-06-23 stsp goto done;
4501 ffd1d5e5 2018-06-23 stsp
4502 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4503 5221c383 2018-08-01 stsp if (view == NULL) {
4504 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
4505 5221c383 2018-08-01 stsp goto done;
4506 5221c383 2018-08-01 stsp }
4507 8b473291 2019-02-21 stsp error = open_tree_view(view, tree, commit_id, &refs, repo);
4508 ad80ab7b 2018-08-04 stsp if (error)
4509 ad80ab7b 2018-08-04 stsp goto done;
4510 e5a0f69f 2018-08-18 stsp error = view_loop(view);
4511 ffd1d5e5 2018-06-23 stsp done:
4512 52185f70 2019-02-05 stsp free(repo_path);
4513 ffd1d5e5 2018-06-23 stsp free(commit_id);
4514 ffd1d5e5 2018-06-23 stsp if (commit)
4515 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
4516 ffd1d5e5 2018-06-23 stsp if (tree)
4517 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
4518 ffd1d5e5 2018-06-23 stsp if (repo)
4519 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
4520 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4521 ffd1d5e5 2018-06-23 stsp return error;
4522 9f7d7167 2018-04-29 stsp }
4523 ce5b7c56 2019-07-09 stsp
4524 ce5b7c56 2019-07-09 stsp static void
4525 ce5b7c56 2019-07-09 stsp list_commands(void)
4526 ce5b7c56 2019-07-09 stsp {
4527 ce5b7c56 2019-07-09 stsp int i;
4528 9f7d7167 2018-04-29 stsp
4529 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
4530 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(tog_commands); i++) {
4531 ce5b7c56 2019-07-09 stsp struct tog_cmd *cmd = &tog_commands[i];
4532 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->name);
4533 ce5b7c56 2019-07-09 stsp }
4534 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
4535 ce5b7c56 2019-07-09 stsp }
4536 ce5b7c56 2019-07-09 stsp
4537 4ed7e80c 2018-05-20 stsp __dead static void
4538 ce5b7c56 2019-07-09 stsp usage(int hflag)
4539 9f7d7167 2018-04-29 stsp {
4540 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4541 53ccebc2 2019-07-30 stsp getprogname());
4542 ce5b7c56 2019-07-09 stsp if (hflag)
4543 ce5b7c56 2019-07-09 stsp list_commands();
4544 9f7d7167 2018-04-29 stsp exit(1);
4545 9f7d7167 2018-04-29 stsp }
4546 9f7d7167 2018-04-29 stsp
4547 c2301be8 2018-04-30 stsp static char **
4548 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
4549 c2301be8 2018-04-30 stsp {
4550 c2301be8 2018-04-30 stsp char **argv;
4551 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
4552 c2301be8 2018-04-30 stsp
4553 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
4554 c2301be8 2018-04-30 stsp if (argv == NULL)
4555 c2301be8 2018-04-30 stsp err(1, "calloc");
4556 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
4557 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
4558 c2301be8 2018-04-30 stsp err(1, "calloc");
4559 c2301be8 2018-04-30 stsp if (arg1) {
4560 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
4561 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
4562 c2301be8 2018-04-30 stsp err(1, "calloc");
4563 c2301be8 2018-04-30 stsp }
4564 c2301be8 2018-04-30 stsp
4565 c2301be8 2018-04-30 stsp return argv;
4566 c2301be8 2018-04-30 stsp }
4567 c2301be8 2018-04-30 stsp
4568 9f7d7167 2018-04-29 stsp int
4569 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
4570 9f7d7167 2018-04-29 stsp {
4571 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
4572 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
4573 53ccebc2 2019-07-30 stsp int ch, hflag = 0, Vflag = 0;
4574 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
4575 9f7d7167 2018-04-29 stsp
4576 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
4577 9f7d7167 2018-04-29 stsp
4578 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
4579 9f7d7167 2018-04-29 stsp switch (ch) {
4580 9f7d7167 2018-04-29 stsp case 'h':
4581 9f7d7167 2018-04-29 stsp hflag = 1;
4582 9f7d7167 2018-04-29 stsp break;
4583 53ccebc2 2019-07-30 stsp case 'V':
4584 53ccebc2 2019-07-30 stsp Vflag = 1;
4585 53ccebc2 2019-07-30 stsp break;
4586 9f7d7167 2018-04-29 stsp default:
4587 ce5b7c56 2019-07-09 stsp usage(hflag);
4588 9f7d7167 2018-04-29 stsp /* NOTREACHED */
4589 9f7d7167 2018-04-29 stsp }
4590 9f7d7167 2018-04-29 stsp }
4591 9f7d7167 2018-04-29 stsp
4592 9f7d7167 2018-04-29 stsp argc -= optind;
4593 9f7d7167 2018-04-29 stsp argv += optind;
4594 9f7d7167 2018-04-29 stsp optind = 0;
4595 c2301be8 2018-04-30 stsp optreset = 1;
4596 9f7d7167 2018-04-29 stsp
4597 53ccebc2 2019-07-30 stsp if (Vflag) {
4598 53ccebc2 2019-07-30 stsp got_version_print_str();
4599 53ccebc2 2019-07-30 stsp return 1;
4600 53ccebc2 2019-07-30 stsp }
4601 53ccebc2 2019-07-30 stsp
4602 c2301be8 2018-04-30 stsp if (argc == 0) {
4603 f29d3e89 2018-06-23 stsp if (hflag)
4604 ce5b7c56 2019-07-09 stsp usage(hflag);
4605 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
4606 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
4607 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
4608 c2301be8 2018-04-30 stsp argc = 1;
4609 c2301be8 2018-04-30 stsp } else {
4610 9f7d7167 2018-04-29 stsp int i;
4611 9f7d7167 2018-04-29 stsp
4612 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
4613 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
4614 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
4615 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
4616 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
4617 9f7d7167 2018-04-29 stsp break;
4618 9f7d7167 2018-04-29 stsp }
4619 9f7d7167 2018-04-29 stsp }
4620 3642c4c6 2019-07-09 stsp
4621 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
4622 d70c3147 2019-07-09 stsp fprintf(stderr, "%s: unknown command '%s'\n",
4623 3642c4c6 2019-07-09 stsp getprogname(), argv[0]);
4624 ce5b7c56 2019-07-09 stsp list_commands();
4625 3642c4c6 2019-07-09 stsp return 1;
4626 9f7d7167 2018-04-29 stsp }
4627 9f7d7167 2018-04-29 stsp }
4628 9f7d7167 2018-04-29 stsp
4629 3642c4c6 2019-07-09 stsp if (hflag)
4630 3642c4c6 2019-07-09 stsp cmd->cmd_usage();
4631 3642c4c6 2019-07-09 stsp else
4632 3642c4c6 2019-07-09 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4633 3642c4c6 2019-07-09 stsp
4634 9f7d7167 2018-04-29 stsp endwin();
4635 c2301be8 2018-04-30 stsp free(cmd_argv);
4636 9f7d7167 2018-04-29 stsp if (error)
4637 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4638 9f7d7167 2018-04-29 stsp return 0;
4639 9f7d7167 2018-04-29 stsp }