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