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