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