Blame


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