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