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