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