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