Blame


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