Blame


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