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