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