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