Blame


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