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