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