Blame


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