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