Blame


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