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