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 a915003a 2019-02-05 stsp static void
1640 a915003a 2019-02-05 stsp init_curses(void)
1641 a915003a 2019-02-05 stsp {
1642 a915003a 2019-02-05 stsp initscr();
1643 a915003a 2019-02-05 stsp cbreak();
1644 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
1645 a915003a 2019-02-05 stsp noecho();
1646 a915003a 2019-02-05 stsp nonl();
1647 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
1648 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
1649 a915003a 2019-02-05 stsp curs_set(0);
1650 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
1651 a915003a 2019-02-05 stsp }
1652 a915003a 2019-02-05 stsp
1653 c2db6724 2019-01-04 stsp static const struct got_error *
1654 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1655 9f7d7167 2018-04-29 stsp {
1656 80ddbec8 2018-04-29 stsp const struct got_error *error;
1657 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1658 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1659 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1660 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1661 80ddbec8 2018-04-29 stsp int ch;
1662 04cc582a 2018-08-01 stsp struct tog_view *view;
1663 80ddbec8 2018-04-29 stsp
1664 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1665 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1666 c2db6724 2019-01-04 stsp NULL) == -1)
1667 80ddbec8 2018-04-29 stsp err(1, "pledge");
1668 80ddbec8 2018-04-29 stsp #endif
1669 80ddbec8 2018-04-29 stsp
1670 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1671 80ddbec8 2018-04-29 stsp switch (ch) {
1672 80ddbec8 2018-04-29 stsp case 'c':
1673 80ddbec8 2018-04-29 stsp start_commit = optarg;
1674 80ddbec8 2018-04-29 stsp break;
1675 ecb28ae0 2018-07-16 stsp case 'r':
1676 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1677 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1678 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1679 ecb28ae0 2018-07-16 stsp break;
1680 80ddbec8 2018-04-29 stsp default:
1681 80ddbec8 2018-04-29 stsp usage();
1682 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1683 80ddbec8 2018-04-29 stsp }
1684 80ddbec8 2018-04-29 stsp }
1685 80ddbec8 2018-04-29 stsp
1686 80ddbec8 2018-04-29 stsp argc -= optind;
1687 80ddbec8 2018-04-29 stsp argv += optind;
1688 80ddbec8 2018-04-29 stsp
1689 ecb28ae0 2018-07-16 stsp if (argc == 0)
1690 ecb28ae0 2018-07-16 stsp path = strdup("");
1691 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1692 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1693 ecb28ae0 2018-07-16 stsp else
1694 80ddbec8 2018-04-29 stsp usage_log();
1695 ecb28ae0 2018-07-16 stsp if (path == NULL)
1696 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1697 80ddbec8 2018-04-29 stsp
1698 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1699 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1700 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1701 ecb28ae0 2018-07-16 stsp goto done;
1702 ecb28ae0 2018-07-16 stsp }
1703 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1704 b7165be3 2019-02-05 stsp struct got_worktree *worktree;
1705 b7165be3 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1706 b7165be3 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1707 b7165be3 2019-02-05 stsp goto done;
1708 b7165be3 2019-02-05 stsp if (worktree) {
1709 b7165be3 2019-02-05 stsp repo_path =
1710 b7165be3 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1711 b7165be3 2019-02-05 stsp got_worktree_close(worktree);
1712 b7165be3 2019-02-05 stsp } else
1713 b7165be3 2019-02-05 stsp repo_path = strdup(cwd);
1714 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1715 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1716 ecb28ae0 2018-07-16 stsp goto done;
1717 ecb28ae0 2018-07-16 stsp }
1718 ecb28ae0 2018-07-16 stsp }
1719 c2db6724 2019-01-04 stsp
1720 a915003a 2019-02-05 stsp init_curses();
1721 a915003a 2019-02-05 stsp
1722 c2db6724 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
1723 c2db6724 2019-01-04 stsp if (error)
1724 c2db6724 2019-01-04 stsp goto done;
1725 ecb28ae0 2018-07-16 stsp
1726 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1727 80ddbec8 2018-04-29 stsp if (error != NULL)
1728 ecb28ae0 2018-07-16 stsp goto done;
1729 80ddbec8 2018-04-29 stsp
1730 15a94983 2018-12-23 stsp if (start_commit == NULL)
1731 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1732 15a94983 2018-12-23 stsp else
1733 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&start_id, repo,
1734 15a94983 2018-12-23 stsp start_commit);
1735 80ddbec8 2018-04-29 stsp if (error != NULL)
1736 ecb28ae0 2018-07-16 stsp goto done;
1737 ecb28ae0 2018-07-16 stsp
1738 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1739 04cc582a 2018-08-01 stsp if (view == NULL) {
1740 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1741 04cc582a 2018-08-01 stsp goto done;
1742 04cc582a 2018-08-01 stsp }
1743 23721109 2018-10-22 stsp error = open_log_view(view, start_id, repo, path, 1);
1744 ba4f502b 2018-08-04 stsp if (error)
1745 ba4f502b 2018-08-04 stsp goto done;
1746 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1747 ecb28ae0 2018-07-16 stsp done:
1748 ecb28ae0 2018-07-16 stsp free(repo_path);
1749 ecb28ae0 2018-07-16 stsp free(cwd);
1750 ecb28ae0 2018-07-16 stsp free(path);
1751 899d86c2 2018-05-10 stsp free(start_id);
1752 ecb28ae0 2018-07-16 stsp if (repo)
1753 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1754 80ddbec8 2018-04-29 stsp return error;
1755 9f7d7167 2018-04-29 stsp }
1756 9f7d7167 2018-04-29 stsp
1757 4ed7e80c 2018-05-20 stsp __dead static void
1758 9f7d7167 2018-04-29 stsp usage_diff(void)
1759 9f7d7167 2018-04-29 stsp {
1760 80ddbec8 2018-04-29 stsp endwin();
1761 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1762 9f7d7167 2018-04-29 stsp getprogname());
1763 9f7d7167 2018-04-29 stsp exit(1);
1764 b304db33 2018-05-20 stsp }
1765 b304db33 2018-05-20 stsp
1766 b304db33 2018-05-20 stsp static char *
1767 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1768 b304db33 2018-05-20 stsp {
1769 b304db33 2018-05-20 stsp char *line;
1770 b304db33 2018-05-20 stsp size_t linelen;
1771 b304db33 2018-05-20 stsp size_t lineno;
1772 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1773 b304db33 2018-05-20 stsp
1774 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1775 b304db33 2018-05-20 stsp if (len)
1776 b304db33 2018-05-20 stsp *len = linelen;
1777 b304db33 2018-05-20 stsp return line;
1778 26ed57b2 2018-05-19 stsp }
1779 26ed57b2 2018-05-19 stsp
1780 4ed7e80c 2018-05-20 stsp static const struct got_error *
1781 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1782 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1783 a3404814 2018-09-02 stsp char * header)
1784 26ed57b2 2018-05-19 stsp {
1785 61e69b96 2018-05-20 stsp const struct got_error *err;
1786 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1787 b304db33 2018-05-20 stsp char *line;
1788 b304db33 2018-05-20 stsp size_t len;
1789 61e69b96 2018-05-20 stsp wchar_t *wline;
1790 e0b650dd 2018-05-20 stsp int width;
1791 26ed57b2 2018-05-19 stsp
1792 26ed57b2 2018-05-19 stsp rewind(f);
1793 f7d12f7e 2018-08-01 stsp werase(view->window);
1794 a3404814 2018-09-02 stsp
1795 a3404814 2018-09-02 stsp if (header) {
1796 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1797 a3404814 2018-09-02 stsp if (err) {
1798 a3404814 2018-09-02 stsp return err;
1799 a3404814 2018-09-02 stsp }
1800 a3404814 2018-09-02 stsp
1801 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1802 a3404814 2018-09-02 stsp wstandout(view->window);
1803 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1804 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1805 a3404814 2018-09-02 stsp wstandend(view->window);
1806 a3404814 2018-09-02 stsp if (width < view->ncols)
1807 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1808 26ed57b2 2018-05-19 stsp
1809 a3404814 2018-09-02 stsp if (max_lines <= 1)
1810 a3404814 2018-09-02 stsp return NULL;
1811 a3404814 2018-09-02 stsp max_lines--;
1812 a3404814 2018-09-02 stsp }
1813 a3404814 2018-09-02 stsp
1814 26ed57b2 2018-05-19 stsp *eof = 0;
1815 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1816 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1817 26ed57b2 2018-05-19 stsp if (line == NULL) {
1818 26ed57b2 2018-05-19 stsp *eof = 1;
1819 26ed57b2 2018-05-19 stsp break;
1820 26ed57b2 2018-05-19 stsp }
1821 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1822 26ed57b2 2018-05-19 stsp free(line);
1823 26ed57b2 2018-05-19 stsp continue;
1824 26ed57b2 2018-05-19 stsp }
1825 26ed57b2 2018-05-19 stsp
1826 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1827 61e69b96 2018-05-20 stsp if (err) {
1828 61e69b96 2018-05-20 stsp free(line);
1829 61e69b96 2018-05-20 stsp return err;
1830 61e69b96 2018-05-20 stsp }
1831 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1832 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1833 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1834 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1835 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1836 26ed57b2 2018-05-19 stsp free(line);
1837 2550e4c3 2018-07-13 stsp free(wline);
1838 2550e4c3 2018-07-13 stsp wline = NULL;
1839 26ed57b2 2018-05-19 stsp }
1840 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1841 26ed57b2 2018-05-19 stsp
1842 1a57306a 2018-09-02 stsp view_vborder(view);
1843 26ed57b2 2018-05-19 stsp
1844 26ed57b2 2018-05-19 stsp return NULL;
1845 abd2672a 2018-12-23 stsp }
1846 abd2672a 2018-12-23 stsp
1847 abd2672a 2018-12-23 stsp static char *
1848 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
1849 abd2672a 2018-12-23 stsp {
1850 abd2672a 2018-12-23 stsp char *p, *s = ctime_r(time, datebuf);
1851 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
1852 abd2672a 2018-12-23 stsp if (p)
1853 abd2672a 2018-12-23 stsp *p = '\0';
1854 abd2672a 2018-12-23 stsp return s;
1855 9f7d7167 2018-04-29 stsp }
1856 9f7d7167 2018-04-29 stsp
1857 4ed7e80c 2018-05-20 stsp static const struct got_error *
1858 15a94983 2018-12-23 stsp write_commit_info(struct got_object_id *commit_id, struct got_repository *repo,
1859 abd2672a 2018-12-23 stsp FILE *outfile)
1860 abd2672a 2018-12-23 stsp {
1861 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
1862 abd2672a 2018-12-23 stsp char datebuf[26];
1863 15a94983 2018-12-23 stsp struct got_commit_object *commit;
1864 15a94983 2018-12-23 stsp char *id_str = NULL;
1865 45d799e2 2018-12-23 stsp time_t committer_time;
1866 45d799e2 2018-12-23 stsp const char *author, *committer;
1867 abd2672a 2018-12-23 stsp
1868 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1869 abd2672a 2018-12-23 stsp if (err)
1870 abd2672a 2018-12-23 stsp return err;
1871 abd2672a 2018-12-23 stsp
1872 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
1873 15a94983 2018-12-23 stsp if (err) {
1874 15a94983 2018-12-23 stsp err = got_error_from_errno();
1875 15a94983 2018-12-23 stsp goto done;
1876 15a94983 2018-12-23 stsp }
1877 abd2672a 2018-12-23 stsp
1878 c1124f18 2018-12-23 stsp if (fprintf(outfile, "commit %s\n", id_str) < 0) {
1879 abd2672a 2018-12-23 stsp err = got_error_from_errno();
1880 abd2672a 2018-12-23 stsp goto done;
1881 abd2672a 2018-12-23 stsp }
1882 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
1883 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
1884 abd2672a 2018-12-23 stsp err = got_error_from_errno();
1885 abd2672a 2018-12-23 stsp goto done;
1886 abd2672a 2018-12-23 stsp }
1887 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1888 abd2672a 2018-12-23 stsp if (fprintf(outfile, "date: %s UTC\n",
1889 45d799e2 2018-12-23 stsp get_datestr(&committer_time, datebuf)) < 0) {
1890 abd2672a 2018-12-23 stsp err = got_error_from_errno();
1891 abd2672a 2018-12-23 stsp goto done;
1892 abd2672a 2018-12-23 stsp }
1893 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1894 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1895 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
1896 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
1897 abd2672a 2018-12-23 stsp err = got_error_from_errno();
1898 abd2672a 2018-12-23 stsp goto done;
1899 abd2672a 2018-12-23 stsp }
1900 45d799e2 2018-12-23 stsp if (fprintf(outfile, "%s\n",
1901 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(commit)) < 0) {
1902 abd2672a 2018-12-23 stsp err = got_error_from_errno();
1903 abd2672a 2018-12-23 stsp goto done;
1904 abd2672a 2018-12-23 stsp }
1905 abd2672a 2018-12-23 stsp done:
1906 abd2672a 2018-12-23 stsp free(id_str);
1907 15a94983 2018-12-23 stsp got_object_commit_close(commit);
1908 abd2672a 2018-12-23 stsp return err;
1909 abd2672a 2018-12-23 stsp }
1910 abd2672a 2018-12-23 stsp
1911 abd2672a 2018-12-23 stsp static const struct got_error *
1912 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
1913 26ed57b2 2018-05-19 stsp {
1914 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
1915 48ae06ee 2018-10-18 stsp FILE *f = NULL;
1916 15a94983 2018-12-23 stsp int obj_type;
1917 26ed57b2 2018-05-19 stsp
1918 511a516b 2018-05-19 stsp f = got_opentemp();
1919 48ae06ee 2018-10-18 stsp if (f == NULL) {
1920 48ae06ee 2018-10-18 stsp err = got_error_from_errno();
1921 48ae06ee 2018-10-18 stsp goto done;
1922 48ae06ee 2018-10-18 stsp }
1923 48ae06ee 2018-10-18 stsp if (s->f)
1924 48ae06ee 2018-10-18 stsp fclose(s->f);
1925 48ae06ee 2018-10-18 stsp s->f = f;
1926 26ed57b2 2018-05-19 stsp
1927 15a94983 2018-12-23 stsp if (s->id1)
1928 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
1929 15a94983 2018-12-23 stsp else
1930 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
1931 15a94983 2018-12-23 stsp if (err)
1932 15a94983 2018-12-23 stsp goto done;
1933 15a94983 2018-12-23 stsp
1934 15a94983 2018-12-23 stsp switch (obj_type) {
1935 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1936 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
1937 54156555 2018-12-24 stsp s->diff_context, s->repo, f);
1938 26ed57b2 2018-05-19 stsp break;
1939 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1940 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
1941 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1942 26ed57b2 2018-05-19 stsp break;
1943 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
1944 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1945 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
1946 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
1947 abd2672a 2018-12-23 stsp
1948 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
1949 abd2672a 2018-12-23 stsp if (err)
1950 abd2672a 2018-12-23 stsp break;
1951 abd2672a 2018-12-23 stsp /* Show commit info if we're diffing to a parent commit. */
1952 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
1953 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
1954 15a94983 2018-12-23 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
1955 15a94983 2018-12-23 stsp write_commit_info(s->id2, s->repo, f);
1956 abd2672a 2018-12-23 stsp break;
1957 abd2672a 2018-12-23 stsp }
1958 abd2672a 2018-12-23 stsp }
1959 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
1960 abd2672a 2018-12-23 stsp
1961 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
1962 15a94983 2018-12-23 stsp s->diff_context, s->repo, f);
1963 26ed57b2 2018-05-19 stsp break;
1964 abd2672a 2018-12-23 stsp }
1965 26ed57b2 2018-05-19 stsp default:
1966 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1967 48ae06ee 2018-10-18 stsp break;
1968 26ed57b2 2018-05-19 stsp }
1969 48ae06ee 2018-10-18 stsp done:
1970 48ae06ee 2018-10-18 stsp if (f)
1971 48ae06ee 2018-10-18 stsp fflush(f);
1972 48ae06ee 2018-10-18 stsp return err;
1973 48ae06ee 2018-10-18 stsp }
1974 26ed57b2 2018-05-19 stsp
1975 48ae06ee 2018-10-18 stsp static const struct got_error *
1976 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
1977 15a94983 2018-12-23 stsp struct got_object_id *id2, struct got_repository *repo)
1978 48ae06ee 2018-10-18 stsp {
1979 48ae06ee 2018-10-18 stsp const struct got_error *err;
1980 5dc9f4bc 2018-08-04 stsp
1981 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
1982 15a94983 2018-12-23 stsp int type1, type2;
1983 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
1984 15a94983 2018-12-23 stsp if (err)
1985 15a94983 2018-12-23 stsp return err;
1986 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
1987 15a94983 2018-12-23 stsp if (err)
1988 15a94983 2018-12-23 stsp return err;
1989 15a94983 2018-12-23 stsp
1990 15a94983 2018-12-23 stsp if (type1 != type2)
1991 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
1992 15a94983 2018-12-23 stsp }
1993 48ae06ee 2018-10-18 stsp
1994 15a94983 2018-12-23 stsp if (id1) {
1995 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
1996 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
1997 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1998 48ae06ee 2018-10-18 stsp } else
1999 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2000 48ae06ee 2018-10-18 stsp
2001 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2002 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2003 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2004 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2005 48ae06ee 2018-10-18 stsp return got_error_from_errno();
2006 48ae06ee 2018-10-18 stsp }
2007 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2008 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2009 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2010 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2011 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2012 5dc9f4bc 2018-08-04 stsp
2013 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2014 48ae06ee 2018-10-18 stsp if (err) {
2015 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2016 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2017 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2018 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2019 48ae06ee 2018-10-18 stsp return err;
2020 48ae06ee 2018-10-18 stsp }
2021 48ae06ee 2018-10-18 stsp
2022 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2023 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2024 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2025 e5a0f69f 2018-08-18 stsp
2026 5dc9f4bc 2018-08-04 stsp return NULL;
2027 5dc9f4bc 2018-08-04 stsp }
2028 5dc9f4bc 2018-08-04 stsp
2029 e5a0f69f 2018-08-18 stsp static const struct got_error *
2030 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2031 5dc9f4bc 2018-08-04 stsp {
2032 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2033 e5a0f69f 2018-08-18 stsp
2034 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2035 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2036 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2037 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2038 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2039 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2040 e5a0f69f 2018-08-18 stsp return err;
2041 5dc9f4bc 2018-08-04 stsp }
2042 5dc9f4bc 2018-08-04 stsp
2043 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2044 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2045 5dc9f4bc 2018-08-04 stsp {
2046 a3404814 2018-09-02 stsp const struct got_error *err;
2047 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2048 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2049 a3404814 2018-09-02 stsp
2050 a3404814 2018-09-02 stsp if (s->id1) {
2051 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2052 a3404814 2018-09-02 stsp if (err)
2053 a3404814 2018-09-02 stsp return err;
2054 a3404814 2018-09-02 stsp }
2055 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2056 a3404814 2018-09-02 stsp if (err)
2057 a3404814 2018-09-02 stsp return err;
2058 26ed57b2 2018-05-19 stsp
2059 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2060 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2061 a3404814 2018-09-02 stsp err = got_error_from_errno();
2062 a3404814 2018-09-02 stsp free(id_str1);
2063 a3404814 2018-09-02 stsp free(id_str2);
2064 a3404814 2018-09-02 stsp return err;
2065 a3404814 2018-09-02 stsp }
2066 a3404814 2018-09-02 stsp free(id_str1);
2067 a3404814 2018-09-02 stsp free(id_str2);
2068 a3404814 2018-09-02 stsp
2069 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2070 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2071 a3404814 2018-09-02 stsp header);
2072 0cf4efb1 2018-09-29 stsp }
2073 0cf4efb1 2018-09-29 stsp
2074 0cf4efb1 2018-09-29 stsp static const struct got_error *
2075 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2076 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2077 e5a0f69f 2018-08-18 stsp {
2078 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2079 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2080 e5a0f69f 2018-08-18 stsp int i;
2081 e5a0f69f 2018-08-18 stsp
2082 e5a0f69f 2018-08-18 stsp switch (ch) {
2083 e5a0f69f 2018-08-18 stsp case 'k':
2084 e5a0f69f 2018-08-18 stsp case KEY_UP:
2085 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
2086 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2087 26ed57b2 2018-05-19 stsp break;
2088 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2089 e5a0f69f 2018-08-18 stsp i = 0;
2090 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
2091 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2092 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2093 e5a0f69f 2018-08-18 stsp break;
2094 e5a0f69f 2018-08-18 stsp case 'j':
2095 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2096 e5a0f69f 2018-08-18 stsp if (!s->eof)
2097 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2098 e5a0f69f 2018-08-18 stsp break;
2099 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2100 e5a0f69f 2018-08-18 stsp case ' ':
2101 e5a0f69f 2018-08-18 stsp i = 0;
2102 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
2103 e5a0f69f 2018-08-18 stsp char *line;
2104 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
2105 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2106 e5a0f69f 2018-08-18 stsp if (line == NULL)
2107 e5a0f69f 2018-08-18 stsp break;
2108 48ae06ee 2018-10-18 stsp }
2109 48ae06ee 2018-10-18 stsp break;
2110 48ae06ee 2018-10-18 stsp case '[':
2111 48ae06ee 2018-10-18 stsp if (s->diff_context > 0) {
2112 48ae06ee 2018-10-18 stsp s->diff_context--;
2113 48ae06ee 2018-10-18 stsp err = create_diff(s);
2114 48ae06ee 2018-10-18 stsp }
2115 48ae06ee 2018-10-18 stsp break;
2116 48ae06ee 2018-10-18 stsp case ']':
2117 4a8520aa 2018-10-18 stsp if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2118 48ae06ee 2018-10-18 stsp s->diff_context++;
2119 48ae06ee 2018-10-18 stsp err = create_diff(s);
2120 bcbd79e2 2018-08-19 stsp }
2121 bcbd79e2 2018-08-19 stsp break;
2122 e5a0f69f 2018-08-18 stsp default:
2123 e5a0f69f 2018-08-18 stsp break;
2124 26ed57b2 2018-05-19 stsp }
2125 e5a0f69f 2018-08-18 stsp
2126 bcbd79e2 2018-08-19 stsp return err;
2127 26ed57b2 2018-05-19 stsp }
2128 26ed57b2 2018-05-19 stsp
2129 4ed7e80c 2018-05-20 stsp static const struct got_error *
2130 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2131 9f7d7167 2018-04-29 stsp {
2132 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2133 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2134 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2135 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2136 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2137 26ed57b2 2018-05-19 stsp int ch;
2138 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2139 26ed57b2 2018-05-19 stsp
2140 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2141 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2142 eb6600df 2019-01-04 stsp NULL) == -1)
2143 26ed57b2 2018-05-19 stsp err(1, "pledge");
2144 26ed57b2 2018-05-19 stsp #endif
2145 26ed57b2 2018-05-19 stsp
2146 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2147 26ed57b2 2018-05-19 stsp switch (ch) {
2148 26ed57b2 2018-05-19 stsp default:
2149 26ed57b2 2018-05-19 stsp usage();
2150 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2151 26ed57b2 2018-05-19 stsp }
2152 26ed57b2 2018-05-19 stsp }
2153 26ed57b2 2018-05-19 stsp
2154 26ed57b2 2018-05-19 stsp argc -= optind;
2155 26ed57b2 2018-05-19 stsp argv += optind;
2156 26ed57b2 2018-05-19 stsp
2157 26ed57b2 2018-05-19 stsp if (argc == 0) {
2158 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2159 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2160 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2161 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2162 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2163 15a94983 2018-12-23 stsp id_str1 = argv[0];
2164 15a94983 2018-12-23 stsp id_str2 = argv[1];
2165 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2166 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2167 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2168 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2169 15a94983 2018-12-23 stsp id_str1 = argv[1];
2170 15a94983 2018-12-23 stsp id_str2 = argv[2];
2171 26ed57b2 2018-05-19 stsp } else
2172 26ed57b2 2018-05-19 stsp usage_diff();
2173 a915003a 2019-02-05 stsp
2174 a915003a 2019-02-05 stsp init_curses();
2175 eb6600df 2019-01-04 stsp
2176 eb6600df 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
2177 eb6600df 2019-01-04 stsp if (error)
2178 eb6600df 2019-01-04 stsp goto done;
2179 26ed57b2 2018-05-19 stsp
2180 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
2181 26ed57b2 2018-05-19 stsp free(repo_path);
2182 26ed57b2 2018-05-19 stsp if (error)
2183 26ed57b2 2018-05-19 stsp goto done;
2184 26ed57b2 2018-05-19 stsp
2185 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
2186 26ed57b2 2018-05-19 stsp if (error)
2187 26ed57b2 2018-05-19 stsp goto done;
2188 26ed57b2 2018-05-19 stsp
2189 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
2190 26ed57b2 2018-05-19 stsp if (error)
2191 26ed57b2 2018-05-19 stsp goto done;
2192 26ed57b2 2018-05-19 stsp
2193 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2194 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2195 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
2196 ea5e7bb5 2018-08-01 stsp goto done;
2197 ea5e7bb5 2018-08-01 stsp }
2198 15a94983 2018-12-23 stsp error = open_diff_view(view, id1, id2, repo);
2199 5dc9f4bc 2018-08-04 stsp if (error)
2200 5dc9f4bc 2018-08-04 stsp goto done;
2201 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2202 26ed57b2 2018-05-19 stsp done:
2203 26ed57b2 2018-05-19 stsp got_repo_close(repo);
2204 26ed57b2 2018-05-19 stsp return error;
2205 9f7d7167 2018-04-29 stsp }
2206 9f7d7167 2018-04-29 stsp
2207 4ed7e80c 2018-05-20 stsp __dead static void
2208 9f7d7167 2018-04-29 stsp usage_blame(void)
2209 9f7d7167 2018-04-29 stsp {
2210 80ddbec8 2018-04-29 stsp endwin();
2211 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2212 9f7d7167 2018-04-29 stsp getprogname());
2213 9f7d7167 2018-04-29 stsp exit(1);
2214 9f7d7167 2018-04-29 stsp }
2215 84451b3e 2018-07-10 stsp
2216 84451b3e 2018-07-10 stsp struct tog_blame_line {
2217 84451b3e 2018-07-10 stsp int annotated;
2218 84451b3e 2018-07-10 stsp struct got_object_id *id;
2219 84451b3e 2018-07-10 stsp };
2220 9f7d7167 2018-04-29 stsp
2221 4ed7e80c 2018-05-20 stsp static const struct got_error *
2222 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2223 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2224 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2225 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2226 84451b3e 2018-07-10 stsp {
2227 84451b3e 2018-07-10 stsp const struct got_error *err;
2228 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2229 84451b3e 2018-07-10 stsp char *line;
2230 84451b3e 2018-07-10 stsp size_t len;
2231 84451b3e 2018-07-10 stsp wchar_t *wline;
2232 b700b5d6 2018-07-10 stsp int width, wlimit;
2233 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2234 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2235 ab089a2a 2018-07-12 stsp char *id_str;
2236 ab089a2a 2018-07-12 stsp
2237 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2238 ab089a2a 2018-07-12 stsp if (err)
2239 ab089a2a 2018-07-12 stsp return err;
2240 84451b3e 2018-07-10 stsp
2241 84451b3e 2018-07-10 stsp rewind(f);
2242 f7d12f7e 2018-08-01 stsp werase(view->window);
2243 84451b3e 2018-07-10 stsp
2244 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
2245 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
2246 ab089a2a 2018-07-12 stsp free(id_str);
2247 ab089a2a 2018-07-12 stsp return err;
2248 ab089a2a 2018-07-12 stsp }
2249 ab089a2a 2018-07-12 stsp
2250 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2251 ab089a2a 2018-07-12 stsp free(line);
2252 2550e4c3 2018-07-13 stsp line = NULL;
2253 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2254 a3404814 2018-09-02 stsp wstandout(view->window);
2255 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2256 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2257 a3404814 2018-09-02 stsp wstandend(view->window);
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 ab089a2a 2018-07-12 stsp
2263 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2264 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2265 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
2266 ab089a2a 2018-07-12 stsp free(id_str);
2267 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
2268 ab089a2a 2018-07-12 stsp }
2269 ab089a2a 2018-07-12 stsp free(id_str);
2270 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2271 3f60a8ef 2018-07-10 stsp free(line);
2272 2550e4c3 2018-07-13 stsp line = NULL;
2273 3f60a8ef 2018-07-10 stsp if (err)
2274 3f60a8ef 2018-07-10 stsp return err;
2275 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2276 2550e4c3 2018-07-13 stsp free(wline);
2277 2550e4c3 2018-07-13 stsp wline = NULL;
2278 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2279 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2280 3f60a8ef 2018-07-10 stsp
2281 84451b3e 2018-07-10 stsp *eof = 0;
2282 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2283 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2284 84451b3e 2018-07-10 stsp if (line == NULL) {
2285 84451b3e 2018-07-10 stsp *eof = 1;
2286 84451b3e 2018-07-10 stsp break;
2287 84451b3e 2018-07-10 stsp }
2288 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2289 84451b3e 2018-07-10 stsp free(line);
2290 84451b3e 2018-07-10 stsp continue;
2291 84451b3e 2018-07-10 stsp }
2292 84451b3e 2018-07-10 stsp
2293 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2294 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2295 84451b3e 2018-07-10 stsp if (err) {
2296 84451b3e 2018-07-10 stsp free(line);
2297 84451b3e 2018-07-10 stsp return err;
2298 84451b3e 2018-07-10 stsp }
2299 84451b3e 2018-07-10 stsp
2300 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2301 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2302 b700b5d6 2018-07-10 stsp
2303 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2304 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2305 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2306 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2307 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2308 84451b3e 2018-07-10 stsp char *id_str;
2309 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2310 84451b3e 2018-07-10 stsp if (err) {
2311 84451b3e 2018-07-10 stsp free(line);
2312 2550e4c3 2018-07-13 stsp free(wline);
2313 84451b3e 2018-07-10 stsp return err;
2314 84451b3e 2018-07-10 stsp }
2315 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2316 84451b3e 2018-07-10 stsp free(id_str);
2317 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2318 ee41ec32 2018-07-10 stsp } else {
2319 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2320 ee41ec32 2018-07-10 stsp prev_id = NULL;
2321 ee41ec32 2018-07-10 stsp }
2322 84451b3e 2018-07-10 stsp
2323 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2324 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2325 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2326 b700b5d6 2018-07-10 stsp width++;
2327 b700b5d6 2018-07-10 stsp }
2328 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2329 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2330 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2331 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2332 84451b3e 2018-07-10 stsp free(line);
2333 2550e4c3 2018-07-13 stsp free(wline);
2334 2550e4c3 2018-07-13 stsp wline = NULL;
2335 84451b3e 2018-07-10 stsp }
2336 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2337 84451b3e 2018-07-10 stsp
2338 1a57306a 2018-09-02 stsp view_vborder(view);
2339 84451b3e 2018-07-10 stsp
2340 84451b3e 2018-07-10 stsp return NULL;
2341 84451b3e 2018-07-10 stsp }
2342 84451b3e 2018-07-10 stsp
2343 84451b3e 2018-07-10 stsp static const struct got_error *
2344 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2345 84451b3e 2018-07-10 stsp {
2346 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2347 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2348 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2349 1a76625f 2018-10-22 stsp int errcode;
2350 84451b3e 2018-07-10 stsp
2351 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2352 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2353 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2354 84451b3e 2018-07-10 stsp
2355 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2356 1a76625f 2018-10-22 stsp if (errcode)
2357 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2358 84451b3e 2018-07-10 stsp
2359 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2360 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2361 d68a0a7d 2018-07-10 stsp goto done;
2362 d68a0a7d 2018-07-10 stsp }
2363 d68a0a7d 2018-07-10 stsp
2364 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2365 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2366 d68a0a7d 2018-07-10 stsp
2367 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2368 d68a0a7d 2018-07-10 stsp if (line->annotated)
2369 d68a0a7d 2018-07-10 stsp goto done;
2370 d68a0a7d 2018-07-10 stsp
2371 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2372 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2373 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2374 84451b3e 2018-07-10 stsp goto done;
2375 84451b3e 2018-07-10 stsp }
2376 84451b3e 2018-07-10 stsp line->annotated = 1;
2377 84451b3e 2018-07-10 stsp done:
2378 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2379 1a76625f 2018-10-22 stsp if (errcode)
2380 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2381 84451b3e 2018-07-10 stsp return err;
2382 84451b3e 2018-07-10 stsp }
2383 84451b3e 2018-07-10 stsp
2384 84451b3e 2018-07-10 stsp static void *
2385 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2386 84451b3e 2018-07-10 stsp {
2387 18430de3 2018-07-10 stsp const struct got_error *err;
2388 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
2389 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
2390 1a76625f 2018-10-22 stsp int errcode;
2391 18430de3 2018-07-10 stsp
2392 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2393 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
2394 18430de3 2018-07-10 stsp
2395 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2396 1a76625f 2018-10-22 stsp if (errcode)
2397 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
2398 18430de3 2018-07-10 stsp
2399 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
2400 c9beca56 2018-07-22 stsp ta->repo = NULL;
2401 c9beca56 2018-07-22 stsp *ta->complete = 1;
2402 18430de3 2018-07-10 stsp
2403 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2404 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2405 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2406 18430de3 2018-07-10 stsp
2407 18430de3 2018-07-10 stsp return (void *)err;
2408 84451b3e 2018-07-10 stsp }
2409 84451b3e 2018-07-10 stsp
2410 245d91c1 2018-07-12 stsp static struct got_object_id *
2411 15a94983 2018-12-23 stsp get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2412 15a94983 2018-12-23 stsp int selected_line)
2413 245d91c1 2018-07-12 stsp {
2414 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2415 b880a918 2018-07-10 stsp
2416 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2417 245d91c1 2018-07-12 stsp if (!line->annotated)
2418 245d91c1 2018-07-12 stsp return NULL;
2419 245d91c1 2018-07-12 stsp
2420 245d91c1 2018-07-12 stsp return line->id;
2421 b880a918 2018-07-10 stsp }
2422 245d91c1 2018-07-12 stsp
2423 b880a918 2018-07-10 stsp static const struct got_error *
2424 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2425 a70480e0 2018-06-23 stsp {
2426 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2427 245d91c1 2018-07-12 stsp int i;
2428 245d91c1 2018-07-12 stsp
2429 245d91c1 2018-07-12 stsp if (blame->thread) {
2430 1a76625f 2018-10-22 stsp int errcode;
2431 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2432 1a76625f 2018-10-22 stsp if (errcode)
2433 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2434 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
2435 1a76625f 2018-10-22 stsp if (errcode)
2436 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2437 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2438 1a76625f 2018-10-22 stsp if (errcode)
2439 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2440 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2441 245d91c1 2018-07-12 stsp err = NULL;
2442 245d91c1 2018-07-12 stsp blame->thread = NULL;
2443 245d91c1 2018-07-12 stsp }
2444 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2445 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2446 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2447 245d91c1 2018-07-12 stsp }
2448 245d91c1 2018-07-12 stsp if (blame->f) {
2449 245d91c1 2018-07-12 stsp fclose(blame->f);
2450 245d91c1 2018-07-12 stsp blame->f = NULL;
2451 245d91c1 2018-07-12 stsp }
2452 57670559 2018-12-23 stsp if (blame->lines) {
2453 57670559 2018-12-23 stsp for (i = 0; i < blame->nlines; i++)
2454 57670559 2018-12-23 stsp free(blame->lines[i].id);
2455 57670559 2018-12-23 stsp free(blame->lines);
2456 57670559 2018-12-23 stsp blame->lines = NULL;
2457 57670559 2018-12-23 stsp }
2458 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2459 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2460 245d91c1 2018-07-12 stsp
2461 245d91c1 2018-07-12 stsp return err;
2462 245d91c1 2018-07-12 stsp }
2463 245d91c1 2018-07-12 stsp
2464 245d91c1 2018-07-12 stsp static const struct got_error *
2465 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2466 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
2467 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
2468 245d91c1 2018-07-12 stsp struct got_repository *repo)
2469 245d91c1 2018-07-12 stsp {
2470 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2471 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2472 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2473 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2474 15a94983 2018-12-23 stsp int obj_type;
2475 a70480e0 2018-06-23 stsp
2476 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2477 27d434c2 2018-09-15 stsp if (err)
2478 15a94983 2018-12-23 stsp return err;
2479 15a94983 2018-12-23 stsp if (obj_id == NULL)
2480 15a94983 2018-12-23 stsp return got_error(GOT_ERR_NO_OBJ);
2481 27d434c2 2018-09-15 stsp
2482 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, repo, obj_id);
2483 84451b3e 2018-07-10 stsp if (err)
2484 84451b3e 2018-07-10 stsp goto done;
2485 27d434c2 2018-09-15 stsp
2486 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2487 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2488 84451b3e 2018-07-10 stsp goto done;
2489 84451b3e 2018-07-10 stsp }
2490 a70480e0 2018-06-23 stsp
2491 15a94983 2018-12-23 stsp err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2492 a70480e0 2018-06-23 stsp if (err)
2493 a70480e0 2018-06-23 stsp goto done;
2494 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2495 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2496 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2497 84451b3e 2018-07-10 stsp goto done;
2498 84451b3e 2018-07-10 stsp }
2499 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2500 245d91c1 2018-07-12 stsp blame->f, blob);
2501 84451b3e 2018-07-10 stsp if (err)
2502 84451b3e 2018-07-10 stsp goto done;
2503 a70480e0 2018-06-23 stsp
2504 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2505 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2506 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2507 84451b3e 2018-07-10 stsp goto done;
2508 84451b3e 2018-07-10 stsp }
2509 a70480e0 2018-06-23 stsp
2510 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2511 bd24772e 2018-07-11 stsp if (err)
2512 bd24772e 2018-07-11 stsp goto done;
2513 bd24772e 2018-07-11 stsp
2514 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2515 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2516 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2517 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2518 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2519 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2520 245d91c1 2018-07-12 stsp goto done;
2521 245d91c1 2018-07-12 stsp }
2522 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2523 245d91c1 2018-07-12 stsp
2524 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2525 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2526 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2527 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2528 245d91c1 2018-07-12 stsp *blame_complete = 0;
2529 245d91c1 2018-07-12 stsp
2530 245d91c1 2018-07-12 stsp done:
2531 245d91c1 2018-07-12 stsp if (blob)
2532 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2533 27d434c2 2018-09-15 stsp free(obj_id);
2534 245d91c1 2018-07-12 stsp if (err)
2535 245d91c1 2018-07-12 stsp stop_blame(blame);
2536 245d91c1 2018-07-12 stsp return err;
2537 245d91c1 2018-07-12 stsp }
2538 245d91c1 2018-07-12 stsp
2539 245d91c1 2018-07-12 stsp static const struct got_error *
2540 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2541 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2542 245d91c1 2018-07-12 stsp {
2543 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2544 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2545 dbc6a6b6 2018-07-12 stsp
2546 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2547 245d91c1 2018-07-12 stsp
2548 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2549 dbc6a6b6 2018-07-12 stsp if (err)
2550 7cbe629d 2018-08-04 stsp return err;
2551 245d91c1 2018-07-12 stsp
2552 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2553 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2554 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2555 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2556 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2557 fb2756b9 2018-08-04 stsp s->path = path;
2558 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2559 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2560 fb2756b9 2018-08-04 stsp s->repo = repo;
2561 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2562 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2563 7cbe629d 2018-08-04 stsp
2564 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2565 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2566 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2567 e5a0f69f 2018-08-18 stsp
2568 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
2569 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2570 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2571 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2572 7cbe629d 2018-08-04 stsp }
2573 7cbe629d 2018-08-04 stsp
2574 e5a0f69f 2018-08-18 stsp static const struct got_error *
2575 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2576 7cbe629d 2018-08-04 stsp {
2577 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2578 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2579 7cbe629d 2018-08-04 stsp
2580 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2581 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2582 e5a0f69f 2018-08-18 stsp
2583 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2584 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2585 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2586 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2587 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2588 7cbe629d 2018-08-04 stsp }
2589 e5a0f69f 2018-08-18 stsp
2590 e5a0f69f 2018-08-18 stsp free(s->path);
2591 e5a0f69f 2018-08-18 stsp
2592 e5a0f69f 2018-08-18 stsp return err;
2593 7cbe629d 2018-08-04 stsp }
2594 7cbe629d 2018-08-04 stsp
2595 7cbe629d 2018-08-04 stsp static const struct got_error *
2596 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2597 7cbe629d 2018-08-04 stsp {
2598 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2599 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2600 2b380cc8 2018-10-24 stsp int errcode;
2601 2b380cc8 2018-10-24 stsp
2602 2b380cc8 2018-10-24 stsp if (s->blame.thread == NULL) {
2603 2b380cc8 2018-10-24 stsp errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2604 2b380cc8 2018-10-24 stsp &s->blame.thread_args);
2605 2b380cc8 2018-10-24 stsp if (errcode)
2606 2b380cc8 2018-10-24 stsp return got_error_set_errno(errcode);
2607 2b380cc8 2018-10-24 stsp }
2608 e5a0f69f 2018-08-18 stsp
2609 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2610 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2611 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2612 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2613 e5a0f69f 2018-08-18 stsp
2614 669b5ffa 2018-10-07 stsp view_vborder(view);
2615 e5a0f69f 2018-08-18 stsp return err;
2616 e5a0f69f 2018-08-18 stsp }
2617 e5a0f69f 2018-08-18 stsp
2618 e5a0f69f 2018-08-18 stsp static const struct got_error *
2619 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2620 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2621 e5a0f69f 2018-08-18 stsp {
2622 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2623 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2624 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2625 669b5ffa 2018-10-07 stsp int begin_x = 0;
2626 7cbe629d 2018-08-04 stsp
2627 e5a0f69f 2018-08-18 stsp switch (ch) {
2628 e5a0f69f 2018-08-18 stsp case 'q':
2629 e5a0f69f 2018-08-18 stsp s->done = 1;
2630 1a76625f 2018-10-22 stsp break;
2631 e5a0f69f 2018-08-18 stsp case 'k':
2632 e5a0f69f 2018-08-18 stsp case KEY_UP:
2633 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2634 e5a0f69f 2018-08-18 stsp s->selected_line--;
2635 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2636 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2637 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2638 a70480e0 2018-06-23 stsp break;
2639 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2640 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2641 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2642 a70480e0 2018-06-23 stsp break;
2643 e5a0f69f 2018-08-18 stsp }
2644 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2645 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2646 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2647 e5a0f69f 2018-08-18 stsp else
2648 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2649 e5a0f69f 2018-08-18 stsp break;
2650 e5a0f69f 2018-08-18 stsp case 'j':
2651 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2652 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2653 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2654 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2655 e5a0f69f 2018-08-18 stsp s->selected_line++;
2656 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2657 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2658 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2659 e5a0f69f 2018-08-18 stsp break;
2660 e5a0f69f 2018-08-18 stsp case 'b':
2661 e5a0f69f 2018-08-18 stsp case 'p': {
2662 15a94983 2018-12-23 stsp struct got_object_id *id = NULL;
2663 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2664 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2665 15a94983 2018-12-23 stsp if (id == NULL)
2666 a70480e0 2018-06-23 stsp break;
2667 15a94983 2018-12-23 stsp if (ch == 'p') {
2668 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2669 15a94983 2018-12-23 stsp struct got_object_qid *pid;
2670 15a94983 2018-12-23 stsp struct got_object_id *blob_id = NULL;
2671 15a94983 2018-12-23 stsp int obj_type;
2672 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit,
2673 15a94983 2018-12-23 stsp s->repo, id);
2674 15a94983 2018-12-23 stsp if (err)
2675 15a94983 2018-12-23 stsp break;
2676 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
2677 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
2678 15a94983 2018-12-23 stsp if (pid == NULL) {
2679 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2680 15a94983 2018-12-23 stsp break;
2681 15a94983 2018-12-23 stsp }
2682 15a94983 2018-12-23 stsp /* Check if path history ends here. */
2683 15a94983 2018-12-23 stsp err = got_object_id_by_path(&blob_id, s->repo,
2684 15a94983 2018-12-23 stsp pid->id, s->path);
2685 15a94983 2018-12-23 stsp if (err) {
2686 15a94983 2018-12-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY)
2687 15a94983 2018-12-23 stsp err = NULL;
2688 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2689 15a94983 2018-12-23 stsp break;
2690 15a94983 2018-12-23 stsp }
2691 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo,
2692 15a94983 2018-12-23 stsp blob_id);
2693 15a94983 2018-12-23 stsp free(blob_id);
2694 15a94983 2018-12-23 stsp /* Can't blame non-blob type objects. */
2695 15a94983 2018-12-23 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2696 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2697 15a94983 2018-12-23 stsp break;
2698 15a94983 2018-12-23 stsp }
2699 15a94983 2018-12-23 stsp err = got_object_qid_alloc(&s->blamed_commit,
2700 15a94983 2018-12-23 stsp pid->id);
2701 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2702 15a94983 2018-12-23 stsp } else {
2703 15a94983 2018-12-23 stsp if (got_object_id_cmp(id,
2704 15a94983 2018-12-23 stsp s->blamed_commit->id) == 0)
2705 15a94983 2018-12-23 stsp break;
2706 15a94983 2018-12-23 stsp err = got_object_qid_alloc(&s->blamed_commit,
2707 15a94983 2018-12-23 stsp id);
2708 15a94983 2018-12-23 stsp }
2709 e5a0f69f 2018-08-18 stsp if (err)
2710 a70480e0 2018-06-23 stsp break;
2711 e5a0f69f 2018-08-18 stsp s->done = 1;
2712 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2713 e5a0f69f 2018-08-18 stsp s->done = 0;
2714 e5a0f69f 2018-08-18 stsp if (thread_err)
2715 245d91c1 2018-07-12 stsp break;
2716 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2717 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2718 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2719 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2720 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2721 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2722 e5a0f69f 2018-08-18 stsp if (err)
2723 a70480e0 2018-06-23 stsp break;
2724 e5a0f69f 2018-08-18 stsp break;
2725 84451b3e 2018-07-10 stsp }
2726 e5a0f69f 2018-08-18 stsp case 'B': {
2727 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2728 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2729 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2730 e5a0f69f 2018-08-18 stsp break;
2731 e5a0f69f 2018-08-18 stsp s->done = 1;
2732 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2733 e5a0f69f 2018-08-18 stsp s->done = 0;
2734 e5a0f69f 2018-08-18 stsp if (thread_err)
2735 e5a0f69f 2018-08-18 stsp break;
2736 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2737 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2738 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2739 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2740 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2741 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2742 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2743 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2744 e5a0f69f 2018-08-18 stsp if (err)
2745 e5a0f69f 2018-08-18 stsp break;
2746 245d91c1 2018-07-12 stsp break;
2747 e5a0f69f 2018-08-18 stsp }
2748 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2749 15a94983 2018-12-23 stsp case '\r': {
2750 15a94983 2018-12-23 stsp struct got_object_id *id = NULL;
2751 15a94983 2018-12-23 stsp struct got_object_qid *pid;
2752 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2753 15a94983 2018-12-23 stsp id = get_selected_commit_id(s->blame.lines,
2754 15a94983 2018-12-23 stsp s->first_displayed_line, s->selected_line);
2755 3e9926ea 2019-01-04 stsp if (id == NULL)
2756 15a94983 2018-12-23 stsp break;
2757 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, s->repo, id);
2758 e5a0f69f 2018-08-18 stsp if (err)
2759 e5a0f69f 2018-08-18 stsp break;
2760 15a94983 2018-12-23 stsp pid = SIMPLEQ_FIRST(
2761 15a94983 2018-12-23 stsp got_object_commit_get_parent_ids(commit));
2762 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
2763 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
2764 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2765 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2766 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2767 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2768 e5a0f69f 2018-08-18 stsp break;
2769 e5a0f69f 2018-08-18 stsp }
2770 15a94983 2018-12-23 stsp err = open_diff_view(diff_view, pid ? pid->id : NULL,
2771 15a94983 2018-12-23 stsp id, s->repo);
2772 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2773 e5a0f69f 2018-08-18 stsp if (err) {
2774 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2775 e5a0f69f 2018-08-18 stsp break;
2776 e5a0f69f 2018-08-18 stsp }
2777 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
2778 669b5ffa 2018-10-07 stsp err = view_close_child(view);
2779 669b5ffa 2018-10-07 stsp if (err)
2780 15a94983 2018-12-23 stsp break;
2781 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
2782 669b5ffa 2018-10-07 stsp if (err) {
2783 669b5ffa 2018-10-07 stsp view_close(diff_view);
2784 669b5ffa 2018-10-07 stsp break;
2785 669b5ffa 2018-10-07 stsp }
2786 51c68690 2018-11-07 stsp *focus_view = diff_view;
2787 51c68690 2018-11-07 stsp view->child_focussed = 1;
2788 669b5ffa 2018-10-07 stsp } else
2789 669b5ffa 2018-10-07 stsp *new_view = diff_view;
2790 e5a0f69f 2018-08-18 stsp if (err)
2791 e5a0f69f 2018-08-18 stsp break;
2792 e5a0f69f 2018-08-18 stsp break;
2793 15a94983 2018-12-23 stsp }
2794 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2795 e5a0f69f 2018-08-18 stsp case ' ':
2796 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2797 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2798 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2799 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2800 e5a0f69f 2018-08-18 stsp break;
2801 e5a0f69f 2018-08-18 stsp }
2802 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2803 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2804 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2805 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2806 e5a0f69f 2018-08-18 stsp else
2807 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2808 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2809 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2810 e5a0f69f 2018-08-18 stsp break;
2811 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2812 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2813 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2814 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2815 e5a0f69f 2018-08-18 stsp }
2816 e5a0f69f 2018-08-18 stsp break;
2817 e5a0f69f 2018-08-18 stsp default:
2818 e5a0f69f 2018-08-18 stsp break;
2819 a70480e0 2018-06-23 stsp }
2820 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2821 a70480e0 2018-06-23 stsp }
2822 a70480e0 2018-06-23 stsp
2823 a70480e0 2018-06-23 stsp static const struct got_error *
2824 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2825 9f7d7167 2018-04-29 stsp {
2826 a70480e0 2018-06-23 stsp const struct got_error *error;
2827 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2828 eb41ed75 2019-02-05 stsp struct got_worktree *worktree = NULL;
2829 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2830 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2831 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2832 a70480e0 2018-06-23 stsp int ch;
2833 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2834 a70480e0 2018-06-23 stsp
2835 a70480e0 2018-06-23 stsp #ifndef PROFILE
2836 8e94dd5b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2837 8e94dd5b 2019-01-04 stsp NULL) == -1)
2838 a70480e0 2018-06-23 stsp err(1, "pledge");
2839 a70480e0 2018-06-23 stsp #endif
2840 a70480e0 2018-06-23 stsp
2841 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2842 a70480e0 2018-06-23 stsp switch (ch) {
2843 a70480e0 2018-06-23 stsp case 'c':
2844 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2845 a70480e0 2018-06-23 stsp break;
2846 69069811 2018-08-02 stsp case 'r':
2847 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2848 69069811 2018-08-02 stsp if (repo_path == NULL)
2849 69069811 2018-08-02 stsp err(1, "-r option");
2850 69069811 2018-08-02 stsp break;
2851 a70480e0 2018-06-23 stsp default:
2852 a70480e0 2018-06-23 stsp usage();
2853 a70480e0 2018-06-23 stsp /* NOTREACHED */
2854 a70480e0 2018-06-23 stsp }
2855 a70480e0 2018-06-23 stsp }
2856 a70480e0 2018-06-23 stsp
2857 a70480e0 2018-06-23 stsp argc -= optind;
2858 a70480e0 2018-06-23 stsp argv += optind;
2859 a70480e0 2018-06-23 stsp
2860 69069811 2018-08-02 stsp if (argc == 1)
2861 69069811 2018-08-02 stsp path = argv[0];
2862 69069811 2018-08-02 stsp else
2863 a70480e0 2018-06-23 stsp usage_blame();
2864 69069811 2018-08-02 stsp
2865 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2866 69069811 2018-08-02 stsp if (cwd == NULL) {
2867 69069811 2018-08-02 stsp error = got_error_from_errno();
2868 69069811 2018-08-02 stsp goto done;
2869 69069811 2018-08-02 stsp }
2870 69069811 2018-08-02 stsp if (repo_path == NULL) {
2871 eb41ed75 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2872 eb41ed75 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2873 69069811 2018-08-02 stsp goto done;
2874 eb41ed75 2019-02-05 stsp else
2875 eb41ed75 2019-02-05 stsp error = NULL;
2876 eb41ed75 2019-02-05 stsp if (worktree) {
2877 eb41ed75 2019-02-05 stsp repo_path =
2878 eb41ed75 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2879 eb41ed75 2019-02-05 stsp if (repo_path == NULL)
2880 eb41ed75 2019-02-05 stsp error = got_error_from_errno();
2881 eb41ed75 2019-02-05 stsp if (error)
2882 eb41ed75 2019-02-05 stsp goto done;
2883 eb41ed75 2019-02-05 stsp } else {
2884 eb41ed75 2019-02-05 stsp repo_path = strdup(cwd);
2885 eb41ed75 2019-02-05 stsp if (repo_path == NULL) {
2886 eb41ed75 2019-02-05 stsp error = got_error_from_errno();
2887 eb41ed75 2019-02-05 stsp goto done;
2888 eb41ed75 2019-02-05 stsp }
2889 69069811 2018-08-02 stsp }
2890 69069811 2018-08-02 stsp }
2891 a915003a 2019-02-05 stsp
2892 a915003a 2019-02-05 stsp init_curses();
2893 69069811 2018-08-02 stsp
2894 8e94dd5b 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
2895 8e94dd5b 2019-01-04 stsp if (error)
2896 8e94dd5b 2019-01-04 stsp goto done;
2897 69069811 2018-08-02 stsp
2898 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2899 a70480e0 2018-06-23 stsp if (error != NULL)
2900 92205607 2019-01-04 stsp goto done;
2901 69069811 2018-08-02 stsp
2902 eb41ed75 2019-02-05 stsp if (worktree) {
2903 eb41ed75 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2904 eb41ed75 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2905 eb41ed75 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2906 eb41ed75 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2907 eb41ed75 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2908 eb41ed75 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2909 eb41ed75 2019-02-05 stsp path) == -1) {
2910 eb41ed75 2019-02-05 stsp error = got_error_from_errno();
2911 eb41ed75 2019-02-05 stsp goto done;
2912 eb41ed75 2019-02-05 stsp }
2913 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2914 eb41ed75 2019-02-05 stsp free(p);
2915 eb41ed75 2019-02-05 stsp } else {
2916 eb41ed75 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2917 eb41ed75 2019-02-05 stsp }
2918 eb41ed75 2019-02-05 stsp if (error)
2919 69069811 2018-08-02 stsp goto done;
2920 a70480e0 2018-06-23 stsp
2921 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2922 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2923 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2924 a70480e0 2018-06-23 stsp if (error != NULL)
2925 66b4983c 2018-06-23 stsp goto done;
2926 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2927 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2928 a70480e0 2018-06-23 stsp } else {
2929 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
2930 15a94983 2018-12-23 stsp commit_id_str);
2931 a70480e0 2018-06-23 stsp }
2932 a19e88aa 2018-06-23 stsp if (error != NULL)
2933 a19e88aa 2018-06-23 stsp goto done;
2934 a70480e0 2018-06-23 stsp
2935 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2936 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2937 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2938 e1cd8fed 2018-08-01 stsp goto done;
2939 e1cd8fed 2018-08-01 stsp }
2940 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2941 7cbe629d 2018-08-04 stsp if (error)
2942 7cbe629d 2018-08-04 stsp goto done;
2943 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2944 a70480e0 2018-06-23 stsp done:
2945 69069811 2018-08-02 stsp free(repo_path);
2946 69069811 2018-08-02 stsp free(cwd);
2947 a70480e0 2018-06-23 stsp free(commit_id);
2948 eb41ed75 2019-02-05 stsp if (worktree)
2949 eb41ed75 2019-02-05 stsp got_worktree_close(worktree);
2950 a70480e0 2018-06-23 stsp if (repo)
2951 a70480e0 2018-06-23 stsp got_repo_close(repo);
2952 a70480e0 2018-06-23 stsp return error;
2953 ffd1d5e5 2018-06-23 stsp }
2954 ffd1d5e5 2018-06-23 stsp
2955 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2956 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2957 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2958 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2959 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2960 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2961 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2962 ffd1d5e5 2018-06-23 stsp {
2963 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2964 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2965 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2966 ffd1d5e5 2018-06-23 stsp int width, n;
2967 ffd1d5e5 2018-06-23 stsp
2968 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2969 ffd1d5e5 2018-06-23 stsp
2970 f7d12f7e 2018-08-01 stsp werase(view->window);
2971 ffd1d5e5 2018-06-23 stsp
2972 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2973 ffd1d5e5 2018-06-23 stsp return NULL;
2974 ffd1d5e5 2018-06-23 stsp
2975 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2976 ffd1d5e5 2018-06-23 stsp if (err)
2977 ffd1d5e5 2018-06-23 stsp return err;
2978 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2979 a3404814 2018-09-02 stsp wstandout(view->window);
2980 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2981 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2982 a3404814 2018-09-02 stsp wstandend(view->window);
2983 2550e4c3 2018-07-13 stsp free(wline);
2984 2550e4c3 2018-07-13 stsp wline = NULL;
2985 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2986 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2987 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2988 ffd1d5e5 2018-06-23 stsp return NULL;
2989 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2990 ce52c690 2018-06-23 stsp if (err)
2991 ce52c690 2018-06-23 stsp return err;
2992 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2993 2550e4c3 2018-07-13 stsp free(wline);
2994 2550e4c3 2018-07-13 stsp wline = NULL;
2995 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2996 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2997 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2998 ffd1d5e5 2018-06-23 stsp return NULL;
2999 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3000 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
3001 a1eca9bb 2018-06-23 stsp return NULL;
3002 ffd1d5e5 2018-06-23 stsp
3003 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3004 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
3005 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
3006 0cf4efb1 2018-09-29 stsp if (view->focussed)
3007 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3008 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
3009 ffd1d5e5 2018-06-23 stsp }
3010 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
3011 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
3012 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3013 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3014 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3015 ffd1d5e5 2018-06-23 stsp return NULL;
3016 ffd1d5e5 2018-06-23 stsp n = 1;
3017 ffd1d5e5 2018-06-23 stsp } else {
3018 ffd1d5e5 2018-06-23 stsp n = 0;
3019 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
3020 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3021 ffd1d5e5 2018-06-23 stsp }
3022 ffd1d5e5 2018-06-23 stsp
3023 ffd1d5e5 2018-06-23 stsp while (te) {
3024 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
3025 1d13200f 2018-07-12 stsp
3026 1d13200f 2018-07-12 stsp if (show_ids) {
3027 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
3028 1d13200f 2018-07-12 stsp if (err)
3029 1d13200f 2018-07-12 stsp return got_error_from_errno();
3030 1d13200f 2018-07-12 stsp }
3031 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3032 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
3033 1d13200f 2018-07-12 stsp free(id_str);
3034 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3035 1d13200f 2018-07-12 stsp }
3036 1d13200f 2018-07-12 stsp free(id_str);
3037 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
3038 ffd1d5e5 2018-06-23 stsp if (err) {
3039 ffd1d5e5 2018-06-23 stsp free(line);
3040 ffd1d5e5 2018-06-23 stsp break;
3041 ffd1d5e5 2018-06-23 stsp }
3042 ffd1d5e5 2018-06-23 stsp if (n == selected) {
3043 0cf4efb1 2018-09-29 stsp if (view->focussed)
3044 0cf4efb1 2018-09-29 stsp wstandout(view->window);
3045 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
3046 ffd1d5e5 2018-06-23 stsp }
3047 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3048 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
3049 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
3050 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
3051 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3052 ffd1d5e5 2018-06-23 stsp free(line);
3053 2550e4c3 2018-07-13 stsp free(wline);
3054 2550e4c3 2018-07-13 stsp wline = NULL;
3055 ffd1d5e5 2018-06-23 stsp n++;
3056 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
3057 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
3058 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
3059 ffd1d5e5 2018-06-23 stsp break;
3060 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3061 ffd1d5e5 2018-06-23 stsp }
3062 ffd1d5e5 2018-06-23 stsp
3063 ffd1d5e5 2018-06-23 stsp return err;
3064 ffd1d5e5 2018-06-23 stsp }
3065 ffd1d5e5 2018-06-23 stsp
3066 ffd1d5e5 2018-06-23 stsp static void
3067 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
3068 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
3069 ffd1d5e5 2018-06-23 stsp {
3070 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
3071 ffd1d5e5 2018-06-23 stsp int i;
3072 ffd1d5e5 2018-06-23 stsp
3073 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
3074 ffd1d5e5 2018-06-23 stsp return;
3075 ffd1d5e5 2018-06-23 stsp
3076 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3077 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
3078 ffd1d5e5 2018-06-23 stsp if (!isroot)
3079 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3080 ffd1d5e5 2018-06-23 stsp return;
3081 ffd1d5e5 2018-06-23 stsp }
3082 ffd1d5e5 2018-06-23 stsp
3083 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
3084 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
3085 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
3086 ffd1d5e5 2018-06-23 stsp prev = te;
3087 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
3088 ffd1d5e5 2018-06-23 stsp }
3089 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
3090 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
3091 ffd1d5e5 2018-06-23 stsp }
3092 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3093 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
3094 ffd1d5e5 2018-06-23 stsp }
3095 ffd1d5e5 2018-06-23 stsp
3096 768394f3 2019-01-24 stsp static int
3097 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3098 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
3099 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
3100 ffd1d5e5 2018-06-23 stsp {
3101 768394f3 2019-01-24 stsp struct got_tree_entry *next, *last;
3102 ffd1d5e5 2018-06-23 stsp int n = 0;
3103 ffd1d5e5 2018-06-23 stsp
3104 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
3105 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3106 ffd1d5e5 2018-06-23 stsp else
3107 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
3108 768394f3 2019-01-24 stsp last = last_displayed_entry;
3109 768394f3 2019-01-24 stsp while (next && last && n++ < maxscroll) {
3110 768394f3 2019-01-24 stsp last = SIMPLEQ_NEXT(last, entry);
3111 768394f3 2019-01-24 stsp if (last) {
3112 768394f3 2019-01-24 stsp *first_displayed_entry = next;
3113 768394f3 2019-01-24 stsp next = SIMPLEQ_NEXT(next, entry);
3114 768394f3 2019-01-24 stsp }
3115 ffd1d5e5 2018-06-23 stsp }
3116 768394f3 2019-01-24 stsp return n;
3117 ffd1d5e5 2018-06-23 stsp }
3118 ffd1d5e5 2018-06-23 stsp
3119 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3120 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
3121 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
3122 ffd1d5e5 2018-06-23 stsp {
3123 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
3124 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
3125 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
3126 ffd1d5e5 2018-06-23 stsp
3127 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
3128 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
3129 ce52c690 2018-06-23 stsp if (te)
3130 ce52c690 2018-06-23 stsp len += strlen(te->name);
3131 ce52c690 2018-06-23 stsp
3132 ce52c690 2018-06-23 stsp *path = calloc(1, len);
3133 ffd1d5e5 2018-06-23 stsp if (path == NULL)
3134 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3135 ffd1d5e5 2018-06-23 stsp
3136 ce52c690 2018-06-23 stsp (*path)[0] = '/';
3137 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
3138 d9765a41 2018-06-23 stsp while (pt) {
3139 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3140 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3141 cb2ebc8a 2018-06-23 stsp goto done;
3142 cb2ebc8a 2018-06-23 stsp }
3143 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
3144 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3145 cb2ebc8a 2018-06-23 stsp goto done;
3146 cb2ebc8a 2018-06-23 stsp }
3147 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3148 ffd1d5e5 2018-06-23 stsp }
3149 ce52c690 2018-06-23 stsp if (te) {
3150 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3151 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3152 ce52c690 2018-06-23 stsp goto done;
3153 ce52c690 2018-06-23 stsp }
3154 cb2ebc8a 2018-06-23 stsp }
3155 ce52c690 2018-06-23 stsp done:
3156 ce52c690 2018-06-23 stsp if (err) {
3157 ce52c690 2018-06-23 stsp free(*path);
3158 ce52c690 2018-06-23 stsp *path = NULL;
3159 ce52c690 2018-06-23 stsp }
3160 ce52c690 2018-06-23 stsp return err;
3161 ce52c690 2018-06-23 stsp }
3162 ce52c690 2018-06-23 stsp
3163 ce52c690 2018-06-23 stsp static const struct got_error *
3164 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3165 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3166 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
3167 ce52c690 2018-06-23 stsp {
3168 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3169 ce52c690 2018-06-23 stsp char *path;
3170 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3171 69efd4c4 2018-07-18 stsp
3172 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3173 ce52c690 2018-06-23 stsp if (err)
3174 ce52c690 2018-06-23 stsp return err;
3175 ffd1d5e5 2018-06-23 stsp
3176 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3177 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3178 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3179 cdf1ee82 2018-08-01 stsp
3180 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
3181 e5a0f69f 2018-08-18 stsp if (err) {
3182 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3183 e5a0f69f 2018-08-18 stsp free(path);
3184 e5a0f69f 2018-08-18 stsp } else
3185 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3186 69efd4c4 2018-07-18 stsp return err;
3187 69efd4c4 2018-07-18 stsp }
3188 69efd4c4 2018-07-18 stsp
3189 69efd4c4 2018-07-18 stsp static const struct got_error *
3190 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3191 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3192 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
3193 69efd4c4 2018-07-18 stsp {
3194 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3195 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3196 69efd4c4 2018-07-18 stsp char *path;
3197 69efd4c4 2018-07-18 stsp
3198 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3199 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3200 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3201 e5a0f69f 2018-08-18 stsp
3202 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3203 69efd4c4 2018-07-18 stsp if (err)
3204 69efd4c4 2018-07-18 stsp return err;
3205 69efd4c4 2018-07-18 stsp
3206 23721109 2018-10-22 stsp err = open_log_view(log_view, commit_id, repo, path, 0);
3207 ba4f502b 2018-08-04 stsp if (err)
3208 e5a0f69f 2018-08-18 stsp view_close(log_view);
3209 e5a0f69f 2018-08-18 stsp else
3210 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3211 cb2ebc8a 2018-06-23 stsp free(path);
3212 cb2ebc8a 2018-06-23 stsp return err;
3213 ffd1d5e5 2018-06-23 stsp }
3214 ffd1d5e5 2018-06-23 stsp
3215 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3216 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3217 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
3218 ffd1d5e5 2018-06-23 stsp {
3219 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3220 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3221 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3222 ffd1d5e5 2018-06-23 stsp
3223 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3224 ffd1d5e5 2018-06-23 stsp
3225 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3226 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3227 ffd1d5e5 2018-06-23 stsp goto done;
3228 ffd1d5e5 2018-06-23 stsp
3229 c1124f18 2018-12-23 stsp if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3230 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
3231 ffd1d5e5 2018-06-23 stsp goto done;
3232 ffd1d5e5 2018-06-23 stsp }
3233 ffd1d5e5 2018-06-23 stsp
3234 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3235 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3236 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3237 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3238 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3239 6484ec90 2018-09-29 stsp err = got_error_from_errno();
3240 6484ec90 2018-09-29 stsp goto done;
3241 6484ec90 2018-09-29 stsp }
3242 fb2756b9 2018-08-04 stsp s->repo = repo;
3243 e5a0f69f 2018-08-18 stsp
3244 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
3245 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
3246 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
3247 ad80ab7b 2018-08-04 stsp done:
3248 ad80ab7b 2018-08-04 stsp free(commit_id_str);
3249 6484ec90 2018-09-29 stsp if (err) {
3250 fb2756b9 2018-08-04 stsp free(s->tree_label);
3251 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3252 6484ec90 2018-09-29 stsp }
3253 ad80ab7b 2018-08-04 stsp return err;
3254 ad80ab7b 2018-08-04 stsp }
3255 ad80ab7b 2018-08-04 stsp
3256 e5a0f69f 2018-08-18 stsp static const struct got_error *
3257 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
3258 ad80ab7b 2018-08-04 stsp {
3259 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3260 ad80ab7b 2018-08-04 stsp
3261 fb2756b9 2018-08-04 stsp free(s->tree_label);
3262 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3263 6484ec90 2018-09-29 stsp free(s->commit_id);
3264 6484ec90 2018-09-29 stsp s->commit_id = NULL;
3265 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
3266 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
3267 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
3268 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
3269 ad80ab7b 2018-08-04 stsp free(parent);
3270 ad80ab7b 2018-08-04 stsp
3271 ad80ab7b 2018-08-04 stsp }
3272 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
3273 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
3274 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
3275 e5a0f69f 2018-08-18 stsp
3276 e5a0f69f 2018-08-18 stsp return NULL;
3277 ad80ab7b 2018-08-04 stsp }
3278 ad80ab7b 2018-08-04 stsp
3279 ad80ab7b 2018-08-04 stsp static const struct got_error *
3280 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
3281 ad80ab7b 2018-08-04 stsp {
3282 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
3283 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3284 e5a0f69f 2018-08-18 stsp char *parent_path;
3285 ad80ab7b 2018-08-04 stsp
3286 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
3287 e5a0f69f 2018-08-18 stsp if (err)
3288 e5a0f69f 2018-08-18 stsp return err;
3289 ffd1d5e5 2018-06-23 stsp
3290 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
3291 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
3292 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3293 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
3294 e5a0f69f 2018-08-18 stsp free(parent_path);
3295 669b5ffa 2018-10-07 stsp
3296 669b5ffa 2018-10-07 stsp view_vborder(view);
3297 e5a0f69f 2018-08-18 stsp return err;
3298 e5a0f69f 2018-08-18 stsp }
3299 ce52c690 2018-06-23 stsp
3300 e5a0f69f 2018-08-18 stsp static const struct got_error *
3301 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3302 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3303 e5a0f69f 2018-08-18 stsp {
3304 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3305 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
3306 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
3307 768394f3 2019-01-24 stsp int begin_x = 0, nscrolled;
3308 ffd1d5e5 2018-06-23 stsp
3309 e5a0f69f 2018-08-18 stsp switch (ch) {
3310 e5a0f69f 2018-08-18 stsp case 'i':
3311 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
3312 ffd1d5e5 2018-06-23 stsp break;
3313 e5a0f69f 2018-08-18 stsp case 'l':
3314 669b5ffa 2018-10-07 stsp if (!s->selected_entry)
3315 669b5ffa 2018-10-07 stsp break;
3316 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
3317 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
3318 669b5ffa 2018-10-07 stsp err = log_tree_entry(&log_view, begin_x,
3319 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
3320 669b5ffa 2018-10-07 stsp s->commit_id, s->repo);
3321 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3322 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3323 669b5ffa 2018-10-07 stsp if (err)
3324 669b5ffa 2018-10-07 stsp return err;
3325 669b5ffa 2018-10-07 stsp err = view_set_child(view, log_view);
3326 669b5ffa 2018-10-07 stsp if (err) {
3327 669b5ffa 2018-10-07 stsp view_close(log_view);
3328 669b5ffa 2018-10-07 stsp break;
3329 669b5ffa 2018-10-07 stsp }
3330 669b5ffa 2018-10-07 stsp *focus_view = log_view;
3331 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3332 669b5ffa 2018-10-07 stsp } else
3333 669b5ffa 2018-10-07 stsp *new_view = log_view;
3334 e5a0f69f 2018-08-18 stsp break;
3335 e5a0f69f 2018-08-18 stsp case 'k':
3336 e5a0f69f 2018-08-18 stsp case KEY_UP:
3337 bf979164 2019-01-24 stsp if (s->selected > 0) {
3338 e5a0f69f 2018-08-18 stsp s->selected--;
3339 bf979164 2019-01-24 stsp if (s->selected == 0)
3340 bf979164 2019-01-24 stsp break;
3341 bf979164 2019-01-24 stsp }
3342 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3343 ffd1d5e5 2018-06-23 stsp break;
3344 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
3345 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
3346 e5a0f69f 2018-08-18 stsp break;
3347 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
3348 67cc7991 2019-01-24 stsp tree_scroll_up(&s->first_displayed_entry,
3349 67cc7991 2019-01-24 stsp MAX(0, view->nlines - 4 - s->selected), s->entries,
3350 67cc7991 2019-01-24 stsp s->tree == s->root);
3351 8a0ead39 2018-11-03 stsp s->selected = 0;
3352 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
3353 67cc7991 2019-01-24 stsp s->first_displayed_entry && s->tree != s->root)
3354 67cc7991 2019-01-24 stsp s->first_displayed_entry = NULL;
3355 e5a0f69f 2018-08-18 stsp break;
3356 e5a0f69f 2018-08-18 stsp case 'j':
3357 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
3358 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
3359 e5a0f69f 2018-08-18 stsp s->selected++;
3360 1d13200f 2018-07-12 stsp break;
3361 e5a0f69f 2018-08-18 stsp }
3362 768394f3 2019-01-24 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3363 768394f3 2019-01-24 stsp == NULL) {
3364 768394f3 2019-01-24 stsp /* can't scroll any further */
3365 768394f3 2019-01-24 stsp break;
3366 768394f3 2019-01-24 stsp }
3367 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
3368 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
3369 e5a0f69f 2018-08-18 stsp break;
3370 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
3371 768394f3 2019-01-24 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3372 768394f3 2019-01-24 stsp == NULL) {
3373 768394f3 2019-01-24 stsp /* can't scroll any further; move cursor down */
3374 768394f3 2019-01-24 stsp if (s->selected < s->ndisplayed - 1)
3375 768394f3 2019-01-24 stsp s->selected = s->ndisplayed - 1;
3376 ffd1d5e5 2018-06-23 stsp break;
3377 768394f3 2019-01-24 stsp }
3378 768394f3 2019-01-24 stsp nscrolled = tree_scroll_down(&s->first_displayed_entry,
3379 768394f3 2019-01-24 stsp view->nlines, s->last_displayed_entry, s->entries);
3380 768394f3 2019-01-24 stsp if (nscrolled < view->nlines) {
3381 768394f3 2019-01-24 stsp int ndisplayed = 0;
3382 768394f3 2019-01-24 stsp struct got_tree_entry *te;
3383 768394f3 2019-01-24 stsp te = s->first_displayed_entry;
3384 768394f3 2019-01-24 stsp do {
3385 768394f3 2019-01-24 stsp ndisplayed++;
3386 768394f3 2019-01-24 stsp te = SIMPLEQ_NEXT(te, entry);
3387 768394f3 2019-01-24 stsp } while (te);
3388 768394f3 2019-01-24 stsp s->selected = ndisplayed - 1;
3389 768394f3 2019-01-24 stsp }
3390 e5a0f69f 2018-08-18 stsp break;
3391 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
3392 e5a0f69f 2018-08-18 stsp case '\r':
3393 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
3394 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3395 7837eeac 2018-09-24 stsp case KEY_BACKSPACE:
3396 e5a0f69f 2018-08-18 stsp /* user selected '..' */
3397 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
3398 ffd1d5e5 2018-06-23 stsp break;
3399 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
3400 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
3401 e5a0f69f 2018-08-18 stsp entry);
3402 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
3403 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
3404 e5a0f69f 2018-08-18 stsp s->entries =
3405 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3406 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
3407 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
3408 e5a0f69f 2018-08-18 stsp s->selected_entry =
3409 e5a0f69f 2018-08-18 stsp parent->selected_entry;
3410 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
3411 e5a0f69f 2018-08-18 stsp free(parent);
3412 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
3413 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3414 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
3415 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
3416 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
3417 e5a0f69f 2018-08-18 stsp if (err)
3418 ffd1d5e5 2018-06-23 stsp break;
3419 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
3420 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
3421 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
3422 e5a0f69f 2018-08-18 stsp break;
3423 ffd1d5e5 2018-06-23 stsp }
3424 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
3425 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
3426 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
3427 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
3428 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
3429 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3430 e5a0f69f 2018-08-18 stsp s->tree = child;
3431 e5a0f69f 2018-08-18 stsp s->entries =
3432 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3433 e5a0f69f 2018-08-18 stsp s->selected = 0;
3434 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3435 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
3436 669b5ffa 2018-10-07 stsp struct tog_view *blame_view;
3437 669b5ffa 2018-10-07 stsp int begin_x = view_is_parent_view(view) ?
3438 669b5ffa 2018-10-07 stsp view_split_begin_x(view->begin_x) : 0;
3439 669b5ffa 2018-10-07 stsp
3440 669b5ffa 2018-10-07 stsp err = blame_tree_entry(&blame_view, begin_x,
3441 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents, s->commit_id,
3442 669b5ffa 2018-10-07 stsp s->repo);
3443 e5a0f69f 2018-08-18 stsp if (err)
3444 ffd1d5e5 2018-06-23 stsp break;
3445 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3446 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3447 669b5ffa 2018-10-07 stsp if (err)
3448 669b5ffa 2018-10-07 stsp return err;
3449 669b5ffa 2018-10-07 stsp err = view_set_child(view, blame_view);
3450 669b5ffa 2018-10-07 stsp if (err) {
3451 669b5ffa 2018-10-07 stsp view_close(blame_view);
3452 669b5ffa 2018-10-07 stsp break;
3453 669b5ffa 2018-10-07 stsp }
3454 669b5ffa 2018-10-07 stsp *focus_view = blame_view;
3455 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3456 669b5ffa 2018-10-07 stsp } else
3457 669b5ffa 2018-10-07 stsp *new_view = blame_view;
3458 e5a0f69f 2018-08-18 stsp }
3459 e5a0f69f 2018-08-18 stsp break;
3460 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3461 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3462 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3463 e5a0f69f 2018-08-18 stsp break;
3464 e5a0f69f 2018-08-18 stsp default:
3465 e5a0f69f 2018-08-18 stsp break;
3466 ffd1d5e5 2018-06-23 stsp }
3467 e5a0f69f 2018-08-18 stsp
3468 ffd1d5e5 2018-06-23 stsp return err;
3469 9f7d7167 2018-04-29 stsp }
3470 9f7d7167 2018-04-29 stsp
3471 ffd1d5e5 2018-06-23 stsp __dead static void
3472 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3473 ffd1d5e5 2018-06-23 stsp {
3474 ffd1d5e5 2018-06-23 stsp endwin();
3475 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3476 ffd1d5e5 2018-06-23 stsp getprogname());
3477 ffd1d5e5 2018-06-23 stsp exit(1);
3478 ffd1d5e5 2018-06-23 stsp }
3479 ffd1d5e5 2018-06-23 stsp
3480 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3481 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3482 ffd1d5e5 2018-06-23 stsp {
3483 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3484 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3485 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3486 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3487 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3488 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3489 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3490 ffd1d5e5 2018-06-23 stsp int ch;
3491 5221c383 2018-08-01 stsp struct tog_view *view;
3492 ffd1d5e5 2018-06-23 stsp
3493 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3494 d188b9a6 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3495 d188b9a6 2019-01-04 stsp NULL) == -1)
3496 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3497 ffd1d5e5 2018-06-23 stsp #endif
3498 ffd1d5e5 2018-06-23 stsp
3499 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3500 ffd1d5e5 2018-06-23 stsp switch (ch) {
3501 ffd1d5e5 2018-06-23 stsp case 'c':
3502 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3503 ffd1d5e5 2018-06-23 stsp break;
3504 ffd1d5e5 2018-06-23 stsp default:
3505 ffd1d5e5 2018-06-23 stsp usage();
3506 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3507 ffd1d5e5 2018-06-23 stsp }
3508 ffd1d5e5 2018-06-23 stsp }
3509 ffd1d5e5 2018-06-23 stsp
3510 ffd1d5e5 2018-06-23 stsp argc -= optind;
3511 ffd1d5e5 2018-06-23 stsp argv += optind;
3512 ffd1d5e5 2018-06-23 stsp
3513 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3514 52185f70 2019-02-05 stsp struct got_worktree *worktree;
3515 52185f70 2019-02-05 stsp char *cwd = getcwd(NULL, 0);
3516 52185f70 2019-02-05 stsp if (cwd == NULL)
3517 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3518 52185f70 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3519 52185f70 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3520 52185f70 2019-02-05 stsp goto done;
3521 52185f70 2019-02-05 stsp if (worktree) {
3522 52185f70 2019-02-05 stsp free(cwd);
3523 52185f70 2019-02-05 stsp repo_path =
3524 52185f70 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3525 52185f70 2019-02-05 stsp got_worktree_close(worktree);
3526 52185f70 2019-02-05 stsp } else
3527 52185f70 2019-02-05 stsp repo_path = cwd;
3528 52185f70 2019-02-05 stsp if (repo_path == NULL) {
3529 52185f70 2019-02-05 stsp error = got_error_from_errno();
3530 52185f70 2019-02-05 stsp goto done;
3531 52185f70 2019-02-05 stsp }
3532 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3533 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3534 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3535 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3536 ffd1d5e5 2018-06-23 stsp } else
3537 ffd1d5e5 2018-06-23 stsp usage_log();
3538 a915003a 2019-02-05 stsp
3539 a915003a 2019-02-05 stsp init_curses();
3540 ffd1d5e5 2018-06-23 stsp
3541 d188b9a6 2019-01-04 stsp error = apply_unveil(repo_path, NULL);
3542 d188b9a6 2019-01-04 stsp if (error)
3543 52185f70 2019-02-05 stsp goto done;
3544 d188b9a6 2019-01-04 stsp
3545 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3546 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3547 52185f70 2019-02-05 stsp goto done;
3548 ffd1d5e5 2018-06-23 stsp
3549 15a94983 2018-12-23 stsp if (commit_id_arg == NULL)
3550 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3551 15a94983 2018-12-23 stsp else
3552 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
3553 15a94983 2018-12-23 stsp commit_id_arg);
3554 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3555 ffd1d5e5 2018-06-23 stsp goto done;
3556 ffd1d5e5 2018-06-23 stsp
3557 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3558 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3559 ffd1d5e5 2018-06-23 stsp goto done;
3560 ffd1d5e5 2018-06-23 stsp
3561 45d799e2 2018-12-23 stsp error = got_object_open_as_tree(&tree, repo,
3562 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
3563 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3564 ffd1d5e5 2018-06-23 stsp goto done;
3565 ffd1d5e5 2018-06-23 stsp
3566 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3567 5221c383 2018-08-01 stsp if (view == NULL) {
3568 5221c383 2018-08-01 stsp error = got_error_from_errno();
3569 5221c383 2018-08-01 stsp goto done;
3570 5221c383 2018-08-01 stsp }
3571 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3572 ad80ab7b 2018-08-04 stsp if (error)
3573 ad80ab7b 2018-08-04 stsp goto done;
3574 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3575 ffd1d5e5 2018-06-23 stsp done:
3576 52185f70 2019-02-05 stsp free(repo_path);
3577 ffd1d5e5 2018-06-23 stsp free(commit_id);
3578 ffd1d5e5 2018-06-23 stsp if (commit)
3579 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3580 ffd1d5e5 2018-06-23 stsp if (tree)
3581 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3582 ffd1d5e5 2018-06-23 stsp if (repo)
3583 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3584 ffd1d5e5 2018-06-23 stsp return error;
3585 9f7d7167 2018-04-29 stsp }
3586 9f7d7167 2018-04-29 stsp
3587 4ed7e80c 2018-05-20 stsp __dead static void
3588 9f7d7167 2018-04-29 stsp usage(void)
3589 9f7d7167 2018-04-29 stsp {
3590 9f7d7167 2018-04-29 stsp int i;
3591 9f7d7167 2018-04-29 stsp
3592 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3593 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3594 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3595 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3596 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3597 9f7d7167 2018-04-29 stsp }
3598 9f7d7167 2018-04-29 stsp exit(1);
3599 9f7d7167 2018-04-29 stsp }
3600 9f7d7167 2018-04-29 stsp
3601 c2301be8 2018-04-30 stsp static char **
3602 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3603 c2301be8 2018-04-30 stsp {
3604 c2301be8 2018-04-30 stsp char **argv;
3605 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3606 c2301be8 2018-04-30 stsp
3607 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3608 c2301be8 2018-04-30 stsp if (argv == NULL)
3609 c2301be8 2018-04-30 stsp err(1, "calloc");
3610 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3611 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3612 c2301be8 2018-04-30 stsp err(1, "calloc");
3613 c2301be8 2018-04-30 stsp if (arg1) {
3614 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3615 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3616 c2301be8 2018-04-30 stsp err(1, "calloc");
3617 c2301be8 2018-04-30 stsp }
3618 c2301be8 2018-04-30 stsp
3619 c2301be8 2018-04-30 stsp return argv;
3620 c2301be8 2018-04-30 stsp }
3621 c2301be8 2018-04-30 stsp
3622 9f7d7167 2018-04-29 stsp int
3623 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3624 9f7d7167 2018-04-29 stsp {
3625 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3626 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3627 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3628 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3629 9f7d7167 2018-04-29 stsp
3630 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
3631 9f7d7167 2018-04-29 stsp
3632 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3633 9f7d7167 2018-04-29 stsp switch (ch) {
3634 9f7d7167 2018-04-29 stsp case 'h':
3635 9f7d7167 2018-04-29 stsp hflag = 1;
3636 9f7d7167 2018-04-29 stsp break;
3637 9f7d7167 2018-04-29 stsp default:
3638 9f7d7167 2018-04-29 stsp usage();
3639 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3640 9f7d7167 2018-04-29 stsp }
3641 9f7d7167 2018-04-29 stsp }
3642 9f7d7167 2018-04-29 stsp
3643 9f7d7167 2018-04-29 stsp argc -= optind;
3644 9f7d7167 2018-04-29 stsp argv += optind;
3645 9f7d7167 2018-04-29 stsp optind = 0;
3646 c2301be8 2018-04-30 stsp optreset = 1;
3647 9f7d7167 2018-04-29 stsp
3648 c2301be8 2018-04-30 stsp if (argc == 0) {
3649 f29d3e89 2018-06-23 stsp if (hflag)
3650 f29d3e89 2018-06-23 stsp usage();
3651 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3652 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3653 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3654 c2301be8 2018-04-30 stsp argc = 1;
3655 c2301be8 2018-04-30 stsp } else {
3656 9f7d7167 2018-04-29 stsp int i;
3657 9f7d7167 2018-04-29 stsp
3658 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3659 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3660 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3661 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3662 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3663 9f7d7167 2018-04-29 stsp if (hflag)
3664 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3665 9f7d7167 2018-04-29 stsp break;
3666 9f7d7167 2018-04-29 stsp }
3667 9f7d7167 2018-04-29 stsp }
3668 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3669 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3670 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3671 c2301be8 2018-04-30 stsp if (repo_path) {
3672 c2301be8 2018-04-30 stsp struct got_repository *repo;
3673 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3674 c2301be8 2018-04-30 stsp if (error == NULL)
3675 c2301be8 2018-04-30 stsp got_repo_close(repo);
3676 c2301be8 2018-04-30 stsp } else
3677 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3678 c2301be8 2018-04-30 stsp if (error) {
3679 f29d3e89 2018-06-23 stsp if (hflag) {
3680 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3681 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3682 f29d3e89 2018-06-23 stsp argv[0]);
3683 f29d3e89 2018-06-23 stsp usage();
3684 f29d3e89 2018-06-23 stsp }
3685 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3686 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3687 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3688 ad7de8d9 2018-04-30 stsp free(repo_path);
3689 c2301be8 2018-04-30 stsp return 1;
3690 c2301be8 2018-04-30 stsp }
3691 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3692 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3693 c2301be8 2018-04-30 stsp argc = 2;
3694 c2301be8 2018-04-30 stsp free(repo_path);
3695 9f7d7167 2018-04-29 stsp }
3696 9f7d7167 2018-04-29 stsp }
3697 9f7d7167 2018-04-29 stsp
3698 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3699 9f7d7167 2018-04-29 stsp if (error)
3700 9f7d7167 2018-04-29 stsp goto done;
3701 9f7d7167 2018-04-29 stsp done:
3702 9f7d7167 2018-04-29 stsp endwin();
3703 c2301be8 2018-04-30 stsp free(cmd_argv);
3704 9f7d7167 2018-04-29 stsp if (error)
3705 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3706 9f7d7167 2018-04-29 stsp return 0;
3707 9f7d7167 2018-04-29 stsp }