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 1a76625f 2018-10-22 stsp free(s->start_id);
1356 1a76625f 2018-10-22 stsp return err;
1357 1a76625f 2018-10-22 stsp }
1358 1a76625f 2018-10-22 stsp
1359 1a76625f 2018-10-22 stsp static const struct got_error *
1360 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1361 04cc582a 2018-08-01 stsp struct got_repository *repo, const char *path)
1362 80ddbec8 2018-04-29 stsp {
1363 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1364 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1365 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1366 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1367 1a76625f 2018-10-22 stsp int errcode;
1368 80ddbec8 2018-04-29 stsp
1369 fb2756b9 2018-08-04 stsp err = got_repo_map_path(&s->in_repo_path, repo, path);
1370 ecb28ae0 2018-07-16 stsp if (err != NULL)
1371 ecb28ae0 2018-07-16 stsp goto done;
1372 ecb28ae0 2018-07-16 stsp
1373 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1374 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1375 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1376 9ba79e04 2018-06-11 stsp
1377 fb2756b9 2018-08-04 stsp s->repo = repo;
1378 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1379 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1380 5036bf37 2018-09-24 stsp err = got_error_from_errno();
1381 5036bf37 2018-09-24 stsp goto done;
1382 5036bf37 2018-09-24 stsp }
1383 e5a0f69f 2018-08-18 stsp
1384 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1385 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1386 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1387 1a76625f 2018-10-22 stsp
1388 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1389 1a76625f 2018-10-22 stsp if (err)
1390 1a76625f 2018-10-22 stsp goto done;
1391 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1392 1a76625f 2018-10-22 stsp 0, thread_repo);
1393 1a76625f 2018-10-22 stsp if (err)
1394 1a76625f 2018-10-22 stsp goto done;
1395 1a76625f 2018-10-22 stsp
1396 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1397 1a76625f 2018-10-22 stsp if (errcode) {
1398 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1399 1a76625f 2018-10-22 stsp goto done;
1400 1a76625f 2018-10-22 stsp }
1401 1a76625f 2018-10-22 stsp
1402 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1403 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1404 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1405 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1406 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1407 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1408 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1409 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1410 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1411 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1412 1a76625f 2018-10-22 stsp s->thread_args.last_displayed_entry = &s->last_displayed_entry;
1413 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1414 1a76625f 2018-10-22 stsp s->thread_args.selected = &s->selected;
1415 1a76625f 2018-10-22 stsp
1416 1a76625f 2018-10-22 stsp errcode = pthread_create(&s->thread, NULL, log_thread,
1417 1a76625f 2018-10-22 stsp &s->thread_args);
1418 1a76625f 2018-10-22 stsp if (errcode) {
1419 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1420 1a76625f 2018-10-22 stsp goto done;
1421 1a76625f 2018-10-22 stsp }
1422 1a76625f 2018-10-22 stsp
1423 ba4f502b 2018-08-04 stsp done:
1424 1a76625f 2018-10-22 stsp if (err)
1425 1a76625f 2018-10-22 stsp close_log_view(view);
1426 ba4f502b 2018-08-04 stsp return err;
1427 ba4f502b 2018-08-04 stsp }
1428 ba4f502b 2018-08-04 stsp
1429 e5a0f69f 2018-08-18 stsp static const struct got_error *
1430 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1431 ba4f502b 2018-08-04 stsp {
1432 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1433 ba4f502b 2018-08-04 stsp
1434 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1435 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1436 1a76625f 2018-10-22 stsp &s->commits, s->selected, view->nlines,
1437 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1438 e5a0f69f 2018-08-18 stsp }
1439 04cc582a 2018-08-01 stsp
1440 e5a0f69f 2018-08-18 stsp static const struct got_error *
1441 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1442 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1443 e5a0f69f 2018-08-18 stsp {
1444 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1445 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1446 5036bf37 2018-09-24 stsp char *parent_path;
1447 669b5ffa 2018-10-07 stsp struct tog_view *diff_view = NULL, *tree_view = NULL;
1448 669b5ffa 2018-10-07 stsp int begin_x = 0;
1449 80ddbec8 2018-04-29 stsp
1450 e5a0f69f 2018-08-18 stsp switch (ch) {
1451 1a76625f 2018-10-22 stsp case 'q':
1452 1a76625f 2018-10-22 stsp s->quit = 1;
1453 1a76625f 2018-10-22 stsp break;
1454 e5a0f69f 2018-08-18 stsp case 'k':
1455 e5a0f69f 2018-08-18 stsp case KEY_UP:
1456 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1457 e5a0f69f 2018-08-18 stsp s->selected--;
1458 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1459 31120ada 2018-04-30 stsp break;
1460 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1461 e5a0f69f 2018-08-18 stsp &s->commits);
1462 e5a0f69f 2018-08-18 stsp break;
1463 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1464 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1465 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1466 e5a0f69f 2018-08-18 stsp s->selected = 0;
1467 80ddbec8 2018-04-29 stsp break;
1468 e5a0f69f 2018-08-18 stsp }
1469 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1470 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1471 e5a0f69f 2018-08-18 stsp break;
1472 e5a0f69f 2018-08-18 stsp case 'j':
1473 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1474 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1475 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1476 e5a0f69f 2018-08-18 stsp s->selected++;
1477 80ddbec8 2018-04-29 stsp break;
1478 e5a0f69f 2018-08-18 stsp }
1479 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1480 93e45b7c 2018-09-24 stsp &s->last_displayed_entry, &s->commits,
1481 1a76625f 2018-10-22 stsp &s->thread_args.log_complete,
1482 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1483 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1484 e5a0f69f 2018-08-18 stsp break;
1485 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1486 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1487 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1488 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1489 93e45b7c 2018-09-24 stsp view->nlines, &s->last_displayed_entry,
1490 1a76625f 2018-10-22 stsp &s->commits, &s->thread_args.log_complete,
1491 1a76625f 2018-10-22 stsp &s->thread_args.commits_needed,
1492 1a76625f 2018-10-22 stsp &s->thread_args.need_commits);
1493 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1494 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1495 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1496 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1497 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1498 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1499 e5a0f69f 2018-08-18 stsp }
1500 e5a0f69f 2018-08-18 stsp err = NULL;
1501 e5a0f69f 2018-08-18 stsp break;
1502 80ddbec8 2018-04-29 stsp }
1503 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1504 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1505 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1506 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1507 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1508 e5a0f69f 2018-08-18 stsp break;
1509 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1510 e5a0f69f 2018-08-18 stsp case '\r':
1511 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1512 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1513 669b5ffa 2018-10-07 stsp err = open_diff_view_for_commit(&diff_view, begin_x,
1514 2a4718d3 2018-09-29 stsp s->selected_entry->id, s->selected_entry->commit,
1515 e5a0f69f 2018-08-18 stsp s->repo);
1516 bfddd0d9 2018-09-29 stsp if (err)
1517 bfddd0d9 2018-09-29 stsp break;
1518 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1519 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1520 669b5ffa 2018-10-07 stsp if (err)
1521 669b5ffa 2018-10-07 stsp return err;
1522 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
1523 669b5ffa 2018-10-07 stsp if (err) {
1524 669b5ffa 2018-10-07 stsp view_close(diff_view);
1525 669b5ffa 2018-10-07 stsp break;
1526 669b5ffa 2018-10-07 stsp }
1527 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(diff_view)) {
1528 669b5ffa 2018-10-07 stsp *focus_view = diff_view;
1529 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1530 669b5ffa 2018-10-07 stsp }
1531 669b5ffa 2018-10-07 stsp } else
1532 669b5ffa 2018-10-07 stsp *new_view = diff_view;
1533 e5a0f69f 2018-08-18 stsp break;
1534 e5a0f69f 2018-08-18 stsp case 't':
1535 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1536 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1537 669b5ffa 2018-10-07 stsp err = browse_commit(&tree_view, begin_x,
1538 0cf4efb1 2018-09-29 stsp s->selected_entry, s->repo);
1539 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1540 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1541 669b5ffa 2018-10-07 stsp if (err)
1542 669b5ffa 2018-10-07 stsp return err;
1543 669b5ffa 2018-10-07 stsp err = view_set_child(view, tree_view);
1544 669b5ffa 2018-10-07 stsp if (err) {
1545 669b5ffa 2018-10-07 stsp view_close(tree_view);
1546 669b5ffa 2018-10-07 stsp break;
1547 669b5ffa 2018-10-07 stsp }
1548 669b5ffa 2018-10-07 stsp *focus_view = tree_view;
1549 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1550 669b5ffa 2018-10-07 stsp } else
1551 669b5ffa 2018-10-07 stsp *new_view = tree_view;
1552 e5a0f69f 2018-08-18 stsp break;
1553 5036bf37 2018-09-24 stsp case KEY_BACKSPACE:
1554 5036bf37 2018-09-24 stsp if (strcmp(s->in_repo_path, "/") == 0)
1555 5036bf37 2018-09-24 stsp break;
1556 5036bf37 2018-09-24 stsp parent_path = dirname(s->in_repo_path);
1557 5036bf37 2018-09-24 stsp if (parent_path && strcmp(parent_path, ".") != 0) {
1558 5036bf37 2018-09-24 stsp struct tog_view *lv;
1559 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1560 1a76625f 2018-10-22 stsp if (err)
1561 1a76625f 2018-10-22 stsp return err;
1562 0cf4efb1 2018-09-29 stsp lv = view_open(view->nlines, view->ncols,
1563 0cf4efb1 2018-09-29 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
1564 5036bf37 2018-09-24 stsp if (lv == NULL)
1565 5036bf37 2018-09-24 stsp return got_error_from_errno();
1566 5036bf37 2018-09-24 stsp err = open_log_view(lv, s->start_id, s->repo,
1567 5036bf37 2018-09-24 stsp parent_path);
1568 5036bf37 2018-09-24 stsp if (err)
1569 1a76625f 2018-10-22 stsp return err;;
1570 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1571 669b5ffa 2018-10-07 stsp *new_view = lv;
1572 669b5ffa 2018-10-07 stsp else {
1573 669b5ffa 2018-10-07 stsp view_set_child(view->parent, lv);
1574 669b5ffa 2018-10-07 stsp *dead_view = view;
1575 669b5ffa 2018-10-07 stsp }
1576 1a76625f 2018-10-22 stsp return NULL;
1577 5036bf37 2018-09-24 stsp }
1578 5036bf37 2018-09-24 stsp break;
1579 e5a0f69f 2018-08-18 stsp default:
1580 e5a0f69f 2018-08-18 stsp break;
1581 899d86c2 2018-05-10 stsp }
1582 e5a0f69f 2018-08-18 stsp
1583 80ddbec8 2018-04-29 stsp return err;
1584 80ddbec8 2018-04-29 stsp }
1585 80ddbec8 2018-04-29 stsp
1586 4ed7e80c 2018-05-20 stsp static const struct got_error *
1587 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1588 9f7d7167 2018-04-29 stsp {
1589 80ddbec8 2018-04-29 stsp const struct got_error *error;
1590 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1591 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1592 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1593 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1594 80ddbec8 2018-04-29 stsp int ch;
1595 04cc582a 2018-08-01 stsp struct tog_view *view;
1596 80ddbec8 2018-04-29 stsp
1597 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1598 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1599 ad242220 2018-09-08 stsp == -1)
1600 80ddbec8 2018-04-29 stsp err(1, "pledge");
1601 80ddbec8 2018-04-29 stsp #endif
1602 80ddbec8 2018-04-29 stsp
1603 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1604 80ddbec8 2018-04-29 stsp switch (ch) {
1605 80ddbec8 2018-04-29 stsp case 'c':
1606 80ddbec8 2018-04-29 stsp start_commit = optarg;
1607 80ddbec8 2018-04-29 stsp break;
1608 ecb28ae0 2018-07-16 stsp case 'r':
1609 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1610 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1611 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1612 ecb28ae0 2018-07-16 stsp break;
1613 80ddbec8 2018-04-29 stsp default:
1614 80ddbec8 2018-04-29 stsp usage();
1615 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1616 80ddbec8 2018-04-29 stsp }
1617 80ddbec8 2018-04-29 stsp }
1618 80ddbec8 2018-04-29 stsp
1619 80ddbec8 2018-04-29 stsp argc -= optind;
1620 80ddbec8 2018-04-29 stsp argv += optind;
1621 80ddbec8 2018-04-29 stsp
1622 ecb28ae0 2018-07-16 stsp if (argc == 0)
1623 ecb28ae0 2018-07-16 stsp path = strdup("");
1624 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1625 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1626 ecb28ae0 2018-07-16 stsp else
1627 80ddbec8 2018-04-29 stsp usage_log();
1628 ecb28ae0 2018-07-16 stsp if (path == NULL)
1629 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1630 80ddbec8 2018-04-29 stsp
1631 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1632 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1633 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1634 ecb28ae0 2018-07-16 stsp goto done;
1635 ecb28ae0 2018-07-16 stsp }
1636 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1637 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1638 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1639 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1640 ecb28ae0 2018-07-16 stsp goto done;
1641 ecb28ae0 2018-07-16 stsp }
1642 ecb28ae0 2018-07-16 stsp }
1643 ecb28ae0 2018-07-16 stsp
1644 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1645 80ddbec8 2018-04-29 stsp if (error != NULL)
1646 ecb28ae0 2018-07-16 stsp goto done;
1647 80ddbec8 2018-04-29 stsp
1648 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1649 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1650 80ddbec8 2018-04-29 stsp if (error != NULL)
1651 ecb28ae0 2018-07-16 stsp goto done;
1652 80ddbec8 2018-04-29 stsp } else {
1653 80ddbec8 2018-04-29 stsp struct got_object *obj;
1654 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1655 80ddbec8 2018-04-29 stsp if (error == NULL) {
1656 6402fb3c 2018-09-15 stsp start_id = got_object_id_dup(got_object_get_id(obj));
1657 899d86c2 2018-05-10 stsp if (start_id == NULL)
1658 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1659 ecb28ae0 2018-07-16 stsp goto done;
1660 80ddbec8 2018-04-29 stsp }
1661 80ddbec8 2018-04-29 stsp }
1662 80ddbec8 2018-04-29 stsp if (error != NULL)
1663 ecb28ae0 2018-07-16 stsp goto done;
1664 ecb28ae0 2018-07-16 stsp
1665 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1666 04cc582a 2018-08-01 stsp if (view == NULL) {
1667 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1668 04cc582a 2018-08-01 stsp goto done;
1669 04cc582a 2018-08-01 stsp }
1670 ba4f502b 2018-08-04 stsp error = open_log_view(view, start_id, repo, path);
1671 ba4f502b 2018-08-04 stsp if (error)
1672 ba4f502b 2018-08-04 stsp goto done;
1673 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1674 ecb28ae0 2018-07-16 stsp done:
1675 ecb28ae0 2018-07-16 stsp free(repo_path);
1676 ecb28ae0 2018-07-16 stsp free(cwd);
1677 ecb28ae0 2018-07-16 stsp free(path);
1678 899d86c2 2018-05-10 stsp free(start_id);
1679 ecb28ae0 2018-07-16 stsp if (repo)
1680 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1681 80ddbec8 2018-04-29 stsp return error;
1682 9f7d7167 2018-04-29 stsp }
1683 9f7d7167 2018-04-29 stsp
1684 4ed7e80c 2018-05-20 stsp __dead static void
1685 9f7d7167 2018-04-29 stsp usage_diff(void)
1686 9f7d7167 2018-04-29 stsp {
1687 80ddbec8 2018-04-29 stsp endwin();
1688 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1689 9f7d7167 2018-04-29 stsp getprogname());
1690 9f7d7167 2018-04-29 stsp exit(1);
1691 b304db33 2018-05-20 stsp }
1692 b304db33 2018-05-20 stsp
1693 b304db33 2018-05-20 stsp static char *
1694 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1695 b304db33 2018-05-20 stsp {
1696 b304db33 2018-05-20 stsp char *line;
1697 b304db33 2018-05-20 stsp size_t linelen;
1698 b304db33 2018-05-20 stsp size_t lineno;
1699 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1700 b304db33 2018-05-20 stsp
1701 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1702 b304db33 2018-05-20 stsp if (len)
1703 b304db33 2018-05-20 stsp *len = linelen;
1704 b304db33 2018-05-20 stsp return line;
1705 26ed57b2 2018-05-19 stsp }
1706 26ed57b2 2018-05-19 stsp
1707 4ed7e80c 2018-05-20 stsp static const struct got_error *
1708 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1709 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1710 a3404814 2018-09-02 stsp char * header)
1711 26ed57b2 2018-05-19 stsp {
1712 61e69b96 2018-05-20 stsp const struct got_error *err;
1713 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1714 b304db33 2018-05-20 stsp char *line;
1715 b304db33 2018-05-20 stsp size_t len;
1716 61e69b96 2018-05-20 stsp wchar_t *wline;
1717 e0b650dd 2018-05-20 stsp int width;
1718 26ed57b2 2018-05-19 stsp
1719 26ed57b2 2018-05-19 stsp rewind(f);
1720 f7d12f7e 2018-08-01 stsp werase(view->window);
1721 a3404814 2018-09-02 stsp
1722 a3404814 2018-09-02 stsp if (header) {
1723 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1724 a3404814 2018-09-02 stsp if (err) {
1725 a3404814 2018-09-02 stsp return err;
1726 a3404814 2018-09-02 stsp }
1727 a3404814 2018-09-02 stsp
1728 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1729 a3404814 2018-09-02 stsp wstandout(view->window);
1730 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1731 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1732 a3404814 2018-09-02 stsp wstandend(view->window);
1733 a3404814 2018-09-02 stsp if (width < view->ncols)
1734 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1735 26ed57b2 2018-05-19 stsp
1736 a3404814 2018-09-02 stsp if (max_lines <= 1)
1737 a3404814 2018-09-02 stsp return NULL;
1738 a3404814 2018-09-02 stsp max_lines--;
1739 a3404814 2018-09-02 stsp }
1740 a3404814 2018-09-02 stsp
1741 26ed57b2 2018-05-19 stsp *eof = 0;
1742 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1743 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1744 26ed57b2 2018-05-19 stsp if (line == NULL) {
1745 26ed57b2 2018-05-19 stsp *eof = 1;
1746 26ed57b2 2018-05-19 stsp break;
1747 26ed57b2 2018-05-19 stsp }
1748 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1749 26ed57b2 2018-05-19 stsp free(line);
1750 26ed57b2 2018-05-19 stsp continue;
1751 26ed57b2 2018-05-19 stsp }
1752 26ed57b2 2018-05-19 stsp
1753 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1754 61e69b96 2018-05-20 stsp if (err) {
1755 61e69b96 2018-05-20 stsp free(line);
1756 61e69b96 2018-05-20 stsp return err;
1757 61e69b96 2018-05-20 stsp }
1758 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1759 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1760 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1761 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1762 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1763 26ed57b2 2018-05-19 stsp free(line);
1764 2550e4c3 2018-07-13 stsp free(wline);
1765 2550e4c3 2018-07-13 stsp wline = NULL;
1766 26ed57b2 2018-05-19 stsp }
1767 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1768 26ed57b2 2018-05-19 stsp
1769 1a57306a 2018-09-02 stsp view_vborder(view);
1770 26ed57b2 2018-05-19 stsp
1771 26ed57b2 2018-05-19 stsp return NULL;
1772 9f7d7167 2018-04-29 stsp }
1773 9f7d7167 2018-04-29 stsp
1774 4ed7e80c 2018-05-20 stsp static const struct got_error *
1775 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
1776 26ed57b2 2018-05-19 stsp {
1777 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
1778 48ae06ee 2018-10-18 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1779 48ae06ee 2018-10-18 stsp FILE *f = NULL;
1780 26ed57b2 2018-05-19 stsp
1781 48ae06ee 2018-10-18 stsp if (s->id1) {
1782 48ae06ee 2018-10-18 stsp err = got_object_open(&obj1, s->repo, s->id1);
1783 48ae06ee 2018-10-18 stsp if (err)
1784 48ae06ee 2018-10-18 stsp return err;
1785 48ae06ee 2018-10-18 stsp }
1786 26ed57b2 2018-05-19 stsp
1787 48ae06ee 2018-10-18 stsp err = got_object_open(&obj2, s->repo, s->id2);
1788 48ae06ee 2018-10-18 stsp if (err)
1789 48ae06ee 2018-10-18 stsp goto done;
1790 48ae06ee 2018-10-18 stsp
1791 511a516b 2018-05-19 stsp f = got_opentemp();
1792 48ae06ee 2018-10-18 stsp if (f == NULL) {
1793 48ae06ee 2018-10-18 stsp err = got_error_from_errno();
1794 48ae06ee 2018-10-18 stsp goto done;
1795 48ae06ee 2018-10-18 stsp }
1796 48ae06ee 2018-10-18 stsp if (s->f)
1797 48ae06ee 2018-10-18 stsp fclose(s->f);
1798 48ae06ee 2018-10-18 stsp s->f = f;
1799 26ed57b2 2018-05-19 stsp
1800 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1801 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1802 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1803 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1804 26ed57b2 2018-05-19 stsp break;
1805 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1806 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_trees(obj1, obj2, "", "",
1807 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1808 26ed57b2 2018-05-19 stsp break;
1809 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1810 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1811 48ae06ee 2018-10-18 stsp s->repo, f);
1812 26ed57b2 2018-05-19 stsp break;
1813 26ed57b2 2018-05-19 stsp default:
1814 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1815 48ae06ee 2018-10-18 stsp break;
1816 26ed57b2 2018-05-19 stsp }
1817 48ae06ee 2018-10-18 stsp done:
1818 48ae06ee 2018-10-18 stsp if (obj1)
1819 48ae06ee 2018-10-18 stsp got_object_close(obj1);
1820 48ae06ee 2018-10-18 stsp got_object_close(obj2);
1821 48ae06ee 2018-10-18 stsp if (f)
1822 48ae06ee 2018-10-18 stsp fflush(f);
1823 48ae06ee 2018-10-18 stsp return err;
1824 48ae06ee 2018-10-18 stsp }
1825 26ed57b2 2018-05-19 stsp
1826 48ae06ee 2018-10-18 stsp static const struct got_error *
1827 48ae06ee 2018-10-18 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1828 48ae06ee 2018-10-18 stsp struct got_object *obj2, struct got_repository *repo)
1829 48ae06ee 2018-10-18 stsp {
1830 48ae06ee 2018-10-18 stsp const struct got_error *err;
1831 5dc9f4bc 2018-08-04 stsp
1832 48ae06ee 2018-10-18 stsp if (obj1 != NULL && obj2 != NULL &&
1833 48ae06ee 2018-10-18 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1834 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
1835 48ae06ee 2018-10-18 stsp
1836 48ae06ee 2018-10-18 stsp if (obj1) {
1837 48ae06ee 2018-10-18 stsp struct got_object_id *id1;
1838 48ae06ee 2018-10-18 stsp id1 = got_object_id_dup(got_object_get_id(obj1));
1839 48ae06ee 2018-10-18 stsp if (id1 == NULL)
1840 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1841 48ae06ee 2018-10-18 stsp view->state.diff.id1 = id1;
1842 48ae06ee 2018-10-18 stsp } else
1843 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1844 48ae06ee 2018-10-18 stsp
1845 48ae06ee 2018-10-18 stsp view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1846 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
1847 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1848 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1849 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1850 48ae06ee 2018-10-18 stsp }
1851 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
1852 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1853 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1854 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
1855 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
1856 5dc9f4bc 2018-08-04 stsp
1857 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
1858 48ae06ee 2018-10-18 stsp if (err) {
1859 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1860 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1861 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
1862 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
1863 48ae06ee 2018-10-18 stsp return err;
1864 48ae06ee 2018-10-18 stsp }
1865 48ae06ee 2018-10-18 stsp
1866 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
1867 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
1868 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
1869 e5a0f69f 2018-08-18 stsp
1870 5dc9f4bc 2018-08-04 stsp return NULL;
1871 5dc9f4bc 2018-08-04 stsp }
1872 5dc9f4bc 2018-08-04 stsp
1873 e5a0f69f 2018-08-18 stsp static const struct got_error *
1874 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1875 5dc9f4bc 2018-08-04 stsp {
1876 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1877 e5a0f69f 2018-08-18 stsp
1878 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1879 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1880 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
1881 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
1882 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1883 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1884 e5a0f69f 2018-08-18 stsp return err;
1885 5dc9f4bc 2018-08-04 stsp }
1886 5dc9f4bc 2018-08-04 stsp
1887 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1888 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1889 5dc9f4bc 2018-08-04 stsp {
1890 a3404814 2018-09-02 stsp const struct got_error *err;
1891 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1892 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
1893 a3404814 2018-09-02 stsp
1894 a3404814 2018-09-02 stsp if (s->id1) {
1895 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
1896 a3404814 2018-09-02 stsp if (err)
1897 a3404814 2018-09-02 stsp return err;
1898 a3404814 2018-09-02 stsp }
1899 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
1900 a3404814 2018-09-02 stsp if (err)
1901 a3404814 2018-09-02 stsp return err;
1902 26ed57b2 2018-05-19 stsp
1903 a3404814 2018-09-02 stsp if (asprintf(&header, "diff: %s %s",
1904 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1905 a3404814 2018-09-02 stsp err = got_error_from_errno();
1906 a3404814 2018-09-02 stsp free(id_str1);
1907 a3404814 2018-09-02 stsp free(id_str2);
1908 a3404814 2018-09-02 stsp return err;
1909 a3404814 2018-09-02 stsp }
1910 a3404814 2018-09-02 stsp free(id_str1);
1911 a3404814 2018-09-02 stsp free(id_str2);
1912 a3404814 2018-09-02 stsp
1913 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
1914 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
1915 a3404814 2018-09-02 stsp header);
1916 0cf4efb1 2018-09-29 stsp }
1917 0cf4efb1 2018-09-29 stsp
1918 0cf4efb1 2018-09-29 stsp static const struct got_error *
1919 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1920 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1921 e5a0f69f 2018-08-18 stsp {
1922 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1923 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
1924 e5a0f69f 2018-08-18 stsp int i;
1925 e5a0f69f 2018-08-18 stsp
1926 e5a0f69f 2018-08-18 stsp switch (ch) {
1927 e5a0f69f 2018-08-18 stsp case 'k':
1928 e5a0f69f 2018-08-18 stsp case KEY_UP:
1929 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
1930 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1931 26ed57b2 2018-05-19 stsp break;
1932 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1933 e5a0f69f 2018-08-18 stsp i = 0;
1934 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
1935 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
1936 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1937 e5a0f69f 2018-08-18 stsp break;
1938 e5a0f69f 2018-08-18 stsp case 'j':
1939 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1940 e5a0f69f 2018-08-18 stsp if (!s->eof)
1941 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1942 e5a0f69f 2018-08-18 stsp break;
1943 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
1944 e5a0f69f 2018-08-18 stsp case ' ':
1945 e5a0f69f 2018-08-18 stsp i = 0;
1946 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
1947 e5a0f69f 2018-08-18 stsp char *line;
1948 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
1949 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1950 e5a0f69f 2018-08-18 stsp if (line == NULL)
1951 e5a0f69f 2018-08-18 stsp break;
1952 48ae06ee 2018-10-18 stsp }
1953 48ae06ee 2018-10-18 stsp break;
1954 48ae06ee 2018-10-18 stsp case '[':
1955 48ae06ee 2018-10-18 stsp if (s->diff_context > 0) {
1956 48ae06ee 2018-10-18 stsp s->diff_context--;
1957 48ae06ee 2018-10-18 stsp err = create_diff(s);
1958 48ae06ee 2018-10-18 stsp }
1959 48ae06ee 2018-10-18 stsp break;
1960 48ae06ee 2018-10-18 stsp case ']':
1961 4a8520aa 2018-10-18 stsp if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
1962 48ae06ee 2018-10-18 stsp s->diff_context++;
1963 48ae06ee 2018-10-18 stsp err = create_diff(s);
1964 bcbd79e2 2018-08-19 stsp }
1965 bcbd79e2 2018-08-19 stsp break;
1966 e5a0f69f 2018-08-18 stsp default:
1967 e5a0f69f 2018-08-18 stsp break;
1968 26ed57b2 2018-05-19 stsp }
1969 e5a0f69f 2018-08-18 stsp
1970 bcbd79e2 2018-08-19 stsp return err;
1971 26ed57b2 2018-05-19 stsp }
1972 26ed57b2 2018-05-19 stsp
1973 4ed7e80c 2018-05-20 stsp static const struct got_error *
1974 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1975 9f7d7167 2018-04-29 stsp {
1976 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1977 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1978 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1979 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1980 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1981 26ed57b2 2018-05-19 stsp int ch;
1982 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1983 26ed57b2 2018-05-19 stsp
1984 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1985 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1986 ad242220 2018-09-08 stsp == -1)
1987 26ed57b2 2018-05-19 stsp err(1, "pledge");
1988 26ed57b2 2018-05-19 stsp #endif
1989 26ed57b2 2018-05-19 stsp
1990 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1991 26ed57b2 2018-05-19 stsp switch (ch) {
1992 26ed57b2 2018-05-19 stsp default:
1993 26ed57b2 2018-05-19 stsp usage();
1994 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1995 26ed57b2 2018-05-19 stsp }
1996 26ed57b2 2018-05-19 stsp }
1997 26ed57b2 2018-05-19 stsp
1998 26ed57b2 2018-05-19 stsp argc -= optind;
1999 26ed57b2 2018-05-19 stsp argv += optind;
2000 26ed57b2 2018-05-19 stsp
2001 26ed57b2 2018-05-19 stsp if (argc == 0) {
2002 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2003 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2004 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2005 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2006 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2007 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
2008 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
2009 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2010 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2011 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2012 26ed57b2 2018-05-19 stsp return got_error_from_errno();
2013 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
2014 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
2015 26ed57b2 2018-05-19 stsp } else
2016 26ed57b2 2018-05-19 stsp usage_diff();
2017 26ed57b2 2018-05-19 stsp
2018 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
2019 26ed57b2 2018-05-19 stsp free(repo_path);
2020 26ed57b2 2018-05-19 stsp if (error)
2021 26ed57b2 2018-05-19 stsp goto done;
2022 26ed57b2 2018-05-19 stsp
2023 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
2024 26ed57b2 2018-05-19 stsp if (error)
2025 26ed57b2 2018-05-19 stsp goto done;
2026 26ed57b2 2018-05-19 stsp
2027 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
2028 26ed57b2 2018-05-19 stsp if (error)
2029 26ed57b2 2018-05-19 stsp goto done;
2030 26ed57b2 2018-05-19 stsp
2031 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2032 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2033 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
2034 ea5e7bb5 2018-08-01 stsp goto done;
2035 ea5e7bb5 2018-08-01 stsp }
2036 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
2037 5dc9f4bc 2018-08-04 stsp if (error)
2038 5dc9f4bc 2018-08-04 stsp goto done;
2039 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2040 26ed57b2 2018-05-19 stsp done:
2041 26ed57b2 2018-05-19 stsp got_repo_close(repo);
2042 26ed57b2 2018-05-19 stsp if (obj1)
2043 26ed57b2 2018-05-19 stsp got_object_close(obj1);
2044 26ed57b2 2018-05-19 stsp if (obj2)
2045 26ed57b2 2018-05-19 stsp got_object_close(obj2);
2046 26ed57b2 2018-05-19 stsp return error;
2047 9f7d7167 2018-04-29 stsp }
2048 9f7d7167 2018-04-29 stsp
2049 4ed7e80c 2018-05-20 stsp __dead static void
2050 9f7d7167 2018-04-29 stsp usage_blame(void)
2051 9f7d7167 2018-04-29 stsp {
2052 80ddbec8 2018-04-29 stsp endwin();
2053 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2054 9f7d7167 2018-04-29 stsp getprogname());
2055 9f7d7167 2018-04-29 stsp exit(1);
2056 9f7d7167 2018-04-29 stsp }
2057 84451b3e 2018-07-10 stsp
2058 84451b3e 2018-07-10 stsp struct tog_blame_line {
2059 84451b3e 2018-07-10 stsp int annotated;
2060 84451b3e 2018-07-10 stsp struct got_object_id *id;
2061 84451b3e 2018-07-10 stsp };
2062 9f7d7167 2018-04-29 stsp
2063 4ed7e80c 2018-05-20 stsp static const struct got_error *
2064 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2065 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2066 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2067 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2068 84451b3e 2018-07-10 stsp {
2069 84451b3e 2018-07-10 stsp const struct got_error *err;
2070 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2071 84451b3e 2018-07-10 stsp char *line;
2072 84451b3e 2018-07-10 stsp size_t len;
2073 84451b3e 2018-07-10 stsp wchar_t *wline;
2074 b700b5d6 2018-07-10 stsp int width, wlimit;
2075 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2076 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2077 ab089a2a 2018-07-12 stsp char *id_str;
2078 ab089a2a 2018-07-12 stsp
2079 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2080 ab089a2a 2018-07-12 stsp if (err)
2081 ab089a2a 2018-07-12 stsp return err;
2082 84451b3e 2018-07-10 stsp
2083 84451b3e 2018-07-10 stsp rewind(f);
2084 f7d12f7e 2018-08-01 stsp werase(view->window);
2085 84451b3e 2018-07-10 stsp
2086 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
2087 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
2088 ab089a2a 2018-07-12 stsp free(id_str);
2089 ab089a2a 2018-07-12 stsp return err;
2090 ab089a2a 2018-07-12 stsp }
2091 ab089a2a 2018-07-12 stsp
2092 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2093 ab089a2a 2018-07-12 stsp free(line);
2094 2550e4c3 2018-07-13 stsp line = NULL;
2095 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2096 a3404814 2018-09-02 stsp wstandout(view->window);
2097 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2098 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2099 a3404814 2018-09-02 stsp wstandend(view->window);
2100 2550e4c3 2018-07-13 stsp free(wline);
2101 2550e4c3 2018-07-13 stsp wline = NULL;
2102 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2103 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2104 ab089a2a 2018-07-12 stsp
2105 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2106 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2107 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
2108 ab089a2a 2018-07-12 stsp free(id_str);
2109 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
2110 ab089a2a 2018-07-12 stsp }
2111 ab089a2a 2018-07-12 stsp free(id_str);
2112 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2113 3f60a8ef 2018-07-10 stsp free(line);
2114 2550e4c3 2018-07-13 stsp line = NULL;
2115 3f60a8ef 2018-07-10 stsp if (err)
2116 3f60a8ef 2018-07-10 stsp return err;
2117 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2118 2550e4c3 2018-07-13 stsp free(wline);
2119 2550e4c3 2018-07-13 stsp wline = NULL;
2120 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2121 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2122 3f60a8ef 2018-07-10 stsp
2123 84451b3e 2018-07-10 stsp *eof = 0;
2124 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2125 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2126 84451b3e 2018-07-10 stsp if (line == NULL) {
2127 84451b3e 2018-07-10 stsp *eof = 1;
2128 84451b3e 2018-07-10 stsp break;
2129 84451b3e 2018-07-10 stsp }
2130 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2131 84451b3e 2018-07-10 stsp free(line);
2132 84451b3e 2018-07-10 stsp continue;
2133 84451b3e 2018-07-10 stsp }
2134 84451b3e 2018-07-10 stsp
2135 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2136 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2137 84451b3e 2018-07-10 stsp if (err) {
2138 84451b3e 2018-07-10 stsp free(line);
2139 84451b3e 2018-07-10 stsp return err;
2140 84451b3e 2018-07-10 stsp }
2141 84451b3e 2018-07-10 stsp
2142 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2143 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2144 b700b5d6 2018-07-10 stsp
2145 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2146 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2147 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
2148 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
2149 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
2150 84451b3e 2018-07-10 stsp char *id_str;
2151 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
2152 84451b3e 2018-07-10 stsp if (err) {
2153 84451b3e 2018-07-10 stsp free(line);
2154 2550e4c3 2018-07-13 stsp free(wline);
2155 84451b3e 2018-07-10 stsp return err;
2156 84451b3e 2018-07-10 stsp }
2157 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
2158 84451b3e 2018-07-10 stsp free(id_str);
2159 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
2160 ee41ec32 2018-07-10 stsp } else {
2161 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
2162 ee41ec32 2018-07-10 stsp prev_id = NULL;
2163 ee41ec32 2018-07-10 stsp }
2164 84451b3e 2018-07-10 stsp
2165 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2166 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2167 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2168 b700b5d6 2018-07-10 stsp width++;
2169 b700b5d6 2018-07-10 stsp }
2170 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2171 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2172 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2173 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2174 84451b3e 2018-07-10 stsp free(line);
2175 2550e4c3 2018-07-13 stsp free(wline);
2176 2550e4c3 2018-07-13 stsp wline = NULL;
2177 84451b3e 2018-07-10 stsp }
2178 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2179 84451b3e 2018-07-10 stsp
2180 1a57306a 2018-09-02 stsp view_vborder(view);
2181 84451b3e 2018-07-10 stsp
2182 84451b3e 2018-07-10 stsp return NULL;
2183 84451b3e 2018-07-10 stsp }
2184 84451b3e 2018-07-10 stsp
2185 84451b3e 2018-07-10 stsp static const struct got_error *
2186 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2187 84451b3e 2018-07-10 stsp {
2188 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2189 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2190 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2191 1a76625f 2018-10-22 stsp int errcode;
2192 84451b3e 2018-07-10 stsp
2193 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2194 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2195 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2196 84451b3e 2018-07-10 stsp
2197 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2198 1a76625f 2018-10-22 stsp if (errcode)
2199 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2200 84451b3e 2018-07-10 stsp
2201 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2202 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2203 d68a0a7d 2018-07-10 stsp goto done;
2204 d68a0a7d 2018-07-10 stsp }
2205 d68a0a7d 2018-07-10 stsp
2206 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2207 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2208 d68a0a7d 2018-07-10 stsp
2209 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2210 d68a0a7d 2018-07-10 stsp if (line->annotated)
2211 d68a0a7d 2018-07-10 stsp goto done;
2212 d68a0a7d 2018-07-10 stsp
2213 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2214 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2215 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2216 84451b3e 2018-07-10 stsp goto done;
2217 84451b3e 2018-07-10 stsp }
2218 84451b3e 2018-07-10 stsp line->annotated = 1;
2219 84451b3e 2018-07-10 stsp
2220 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
2221 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
2222 e5a0f69f 2018-08-18 stsp a->last_displayed_line, a->eof, a->view->nlines);
2223 84451b3e 2018-07-10 stsp done:
2224 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2225 1a76625f 2018-10-22 stsp if (errcode)
2226 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2227 84451b3e 2018-07-10 stsp return err;
2228 84451b3e 2018-07-10 stsp }
2229 84451b3e 2018-07-10 stsp
2230 84451b3e 2018-07-10 stsp static void *
2231 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2232 84451b3e 2018-07-10 stsp {
2233 18430de3 2018-07-10 stsp const struct got_error *err;
2234 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
2235 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
2236 1a76625f 2018-10-22 stsp int errcode;
2237 18430de3 2018-07-10 stsp
2238 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2239 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
2240 18430de3 2018-07-10 stsp
2241 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2242 1a76625f 2018-10-22 stsp if (errcode)
2243 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
2244 18430de3 2018-07-10 stsp
2245 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
2246 c9beca56 2018-07-22 stsp ta->repo = NULL;
2247 c9beca56 2018-07-22 stsp *ta->complete = 1;
2248 1a76625f 2018-10-22 stsp if (!err) {
2249 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
2250 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
2251 e5a0f69f 2018-08-18 stsp a->first_displayed_line, a->last_displayed_line, a->eof,
2252 f7d12f7e 2018-08-01 stsp a->view->nlines);
2253 1a76625f 2018-10-22 stsp }
2254 18430de3 2018-07-10 stsp
2255 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2256 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2257 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2258 18430de3 2018-07-10 stsp
2259 18430de3 2018-07-10 stsp return (void *)err;
2260 84451b3e 2018-07-10 stsp }
2261 84451b3e 2018-07-10 stsp
2262 245d91c1 2018-07-12 stsp static struct got_object_id *
2263 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
2264 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
2265 245d91c1 2018-07-12 stsp {
2266 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2267 b880a918 2018-07-10 stsp
2268 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2269 245d91c1 2018-07-12 stsp if (!line->annotated)
2270 245d91c1 2018-07-12 stsp return NULL;
2271 245d91c1 2018-07-12 stsp
2272 245d91c1 2018-07-12 stsp return line->id;
2273 245d91c1 2018-07-12 stsp }
2274 245d91c1 2018-07-12 stsp
2275 84451b3e 2018-07-10 stsp static const struct got_error *
2276 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
2277 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
2278 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
2279 b880a918 2018-07-10 stsp {
2280 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
2281 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
2282 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
2283 b880a918 2018-07-10 stsp struct got_object_qid *pid;
2284 b880a918 2018-07-10 stsp
2285 b880a918 2018-07-10 stsp *pobj = NULL;
2286 b880a918 2018-07-10 stsp *obj = NULL;
2287 b880a918 2018-07-10 stsp
2288 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
2289 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
2290 245d91c1 2018-07-12 stsp if (selected_id == NULL)
2291 b880a918 2018-07-10 stsp return NULL;
2292 b880a918 2018-07-10 stsp
2293 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
2294 b880a918 2018-07-10 stsp if (err)
2295 b880a918 2018-07-10 stsp goto done;
2296 b880a918 2018-07-10 stsp
2297 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
2298 b880a918 2018-07-10 stsp if (err)
2299 b880a918 2018-07-10 stsp goto done;
2300 b880a918 2018-07-10 stsp
2301 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
2302 b880a918 2018-07-10 stsp if (pid) {
2303 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
2304 b880a918 2018-07-10 stsp if (err)
2305 b880a918 2018-07-10 stsp goto done;
2306 b880a918 2018-07-10 stsp }
2307 b880a918 2018-07-10 stsp done:
2308 b880a918 2018-07-10 stsp if (commit)
2309 b880a918 2018-07-10 stsp got_object_commit_close(commit);
2310 b880a918 2018-07-10 stsp return err;
2311 b880a918 2018-07-10 stsp }
2312 245d91c1 2018-07-12 stsp
2313 b880a918 2018-07-10 stsp static const struct got_error *
2314 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2315 a70480e0 2018-06-23 stsp {
2316 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2317 245d91c1 2018-07-12 stsp int i;
2318 245d91c1 2018-07-12 stsp
2319 245d91c1 2018-07-12 stsp if (blame->thread) {
2320 1a76625f 2018-10-22 stsp int errcode;
2321 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2322 1a76625f 2018-10-22 stsp if (errcode)
2323 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2324 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
2325 1a76625f 2018-10-22 stsp if (errcode)
2326 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2327 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2328 1a76625f 2018-10-22 stsp if (errcode)
2329 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2330 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2331 245d91c1 2018-07-12 stsp err = NULL;
2332 245d91c1 2018-07-12 stsp blame->thread = NULL;
2333 245d91c1 2018-07-12 stsp }
2334 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2335 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2336 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2337 245d91c1 2018-07-12 stsp }
2338 245d91c1 2018-07-12 stsp if (blame->f) {
2339 245d91c1 2018-07-12 stsp fclose(blame->f);
2340 245d91c1 2018-07-12 stsp blame->f = NULL;
2341 245d91c1 2018-07-12 stsp }
2342 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
2343 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
2344 245d91c1 2018-07-12 stsp free(blame->lines);
2345 245d91c1 2018-07-12 stsp blame->lines = NULL;
2346 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2347 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2348 245d91c1 2018-07-12 stsp
2349 245d91c1 2018-07-12 stsp return err;
2350 245d91c1 2018-07-12 stsp }
2351 245d91c1 2018-07-12 stsp
2352 245d91c1 2018-07-12 stsp static const struct got_error *
2353 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2354 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
2355 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
2356 245d91c1 2018-07-12 stsp struct got_repository *repo)
2357 245d91c1 2018-07-12 stsp {
2358 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2359 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2360 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2361 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2362 27d434c2 2018-09-15 stsp struct got_object *obj = NULL;
2363 1a76625f 2018-10-22 stsp int errcode;
2364 a70480e0 2018-06-23 stsp
2365 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2366 27d434c2 2018-09-15 stsp if (err)
2367 27d434c2 2018-09-15 stsp goto done;
2368 27d434c2 2018-09-15 stsp
2369 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
2370 84451b3e 2018-07-10 stsp if (err)
2371 84451b3e 2018-07-10 stsp goto done;
2372 27d434c2 2018-09-15 stsp
2373 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2374 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2375 84451b3e 2018-07-10 stsp goto done;
2376 84451b3e 2018-07-10 stsp }
2377 a70480e0 2018-06-23 stsp
2378 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
2379 a70480e0 2018-06-23 stsp if (err)
2380 a70480e0 2018-06-23 stsp goto done;
2381 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2382 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2383 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2384 84451b3e 2018-07-10 stsp goto done;
2385 84451b3e 2018-07-10 stsp }
2386 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2387 245d91c1 2018-07-12 stsp blame->f, blob);
2388 84451b3e 2018-07-10 stsp if (err)
2389 84451b3e 2018-07-10 stsp goto done;
2390 a70480e0 2018-06-23 stsp
2391 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2392 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2393 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2394 84451b3e 2018-07-10 stsp goto done;
2395 84451b3e 2018-07-10 stsp }
2396 a70480e0 2018-06-23 stsp
2397 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2398 bd24772e 2018-07-11 stsp if (err)
2399 bd24772e 2018-07-11 stsp goto done;
2400 bd24772e 2018-07-11 stsp
2401 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2402 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2403 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2404 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2405 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2406 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2407 245d91c1 2018-07-12 stsp goto done;
2408 245d91c1 2018-07-12 stsp }
2409 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
2410 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
2411 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
2412 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
2413 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
2414 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2415 e5a0f69f 2018-08-18 stsp blame->cb_args.eof = eof;
2416 245d91c1 2018-07-12 stsp
2417 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2418 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2419 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2420 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2421 245d91c1 2018-07-12 stsp *blame_complete = 0;
2422 245d91c1 2018-07-12 stsp
2423 1a76625f 2018-10-22 stsp errcode = pthread_create(&blame->thread, NULL, blame_thread,
2424 1a76625f 2018-10-22 stsp &blame->thread_args);
2425 1a76625f 2018-10-22 stsp if (errcode) {
2426 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2427 245d91c1 2018-07-12 stsp goto done;
2428 245d91c1 2018-07-12 stsp }
2429 245d91c1 2018-07-12 stsp
2430 245d91c1 2018-07-12 stsp done:
2431 245d91c1 2018-07-12 stsp if (blob)
2432 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2433 27d434c2 2018-09-15 stsp free(obj_id);
2434 245d91c1 2018-07-12 stsp if (obj)
2435 245d91c1 2018-07-12 stsp got_object_close(obj);
2436 245d91c1 2018-07-12 stsp if (err)
2437 245d91c1 2018-07-12 stsp stop_blame(blame);
2438 245d91c1 2018-07-12 stsp return err;
2439 245d91c1 2018-07-12 stsp }
2440 245d91c1 2018-07-12 stsp
2441 245d91c1 2018-07-12 stsp static const struct got_error *
2442 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2443 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2444 245d91c1 2018-07-12 stsp {
2445 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2446 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2447 dbc6a6b6 2018-07-12 stsp
2448 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2449 245d91c1 2018-07-12 stsp
2450 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2451 dbc6a6b6 2018-07-12 stsp if (err)
2452 7cbe629d 2018-08-04 stsp return err;
2453 245d91c1 2018-07-12 stsp
2454 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2455 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2456 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2457 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2458 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2459 fb2756b9 2018-08-04 stsp s->path = path;
2460 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2461 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2462 fb2756b9 2018-08-04 stsp s->repo = repo;
2463 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2464 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2465 7cbe629d 2018-08-04 stsp
2466 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2467 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2468 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2469 e5a0f69f 2018-08-18 stsp
2470 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
2471 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2472 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2473 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2474 7cbe629d 2018-08-04 stsp }
2475 7cbe629d 2018-08-04 stsp
2476 e5a0f69f 2018-08-18 stsp static const struct got_error *
2477 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2478 7cbe629d 2018-08-04 stsp {
2479 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2480 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2481 7cbe629d 2018-08-04 stsp
2482 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2483 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2484 e5a0f69f 2018-08-18 stsp
2485 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2486 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2487 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2488 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2489 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2490 7cbe629d 2018-08-04 stsp }
2491 e5a0f69f 2018-08-18 stsp
2492 e5a0f69f 2018-08-18 stsp free(s->path);
2493 e5a0f69f 2018-08-18 stsp
2494 e5a0f69f 2018-08-18 stsp return err;
2495 7cbe629d 2018-08-04 stsp }
2496 7cbe629d 2018-08-04 stsp
2497 7cbe629d 2018-08-04 stsp static const struct got_error *
2498 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2499 7cbe629d 2018-08-04 stsp {
2500 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2501 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2502 e5a0f69f 2018-08-18 stsp
2503 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2504 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2505 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2506 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2507 e5a0f69f 2018-08-18 stsp
2508 669b5ffa 2018-10-07 stsp view_vborder(view);
2509 e5a0f69f 2018-08-18 stsp return err;
2510 e5a0f69f 2018-08-18 stsp }
2511 e5a0f69f 2018-08-18 stsp
2512 e5a0f69f 2018-08-18 stsp static const struct got_error *
2513 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2514 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2515 e5a0f69f 2018-08-18 stsp {
2516 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2517 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
2518 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2519 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2520 669b5ffa 2018-10-07 stsp int begin_x = 0;
2521 7cbe629d 2018-08-04 stsp
2522 e5a0f69f 2018-08-18 stsp switch (ch) {
2523 e5a0f69f 2018-08-18 stsp case 'q':
2524 e5a0f69f 2018-08-18 stsp s->done = 1;
2525 1a76625f 2018-10-22 stsp break;
2526 e5a0f69f 2018-08-18 stsp case 'k':
2527 e5a0f69f 2018-08-18 stsp case KEY_UP:
2528 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2529 e5a0f69f 2018-08-18 stsp s->selected_line--;
2530 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2531 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2532 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2533 a70480e0 2018-06-23 stsp break;
2534 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2535 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2536 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2537 a70480e0 2018-06-23 stsp break;
2538 e5a0f69f 2018-08-18 stsp }
2539 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2540 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2541 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2542 e5a0f69f 2018-08-18 stsp else
2543 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2544 e5a0f69f 2018-08-18 stsp break;
2545 e5a0f69f 2018-08-18 stsp case 'j':
2546 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2547 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2548 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2549 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2550 e5a0f69f 2018-08-18 stsp s->selected_line++;
2551 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2552 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2553 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2554 e5a0f69f 2018-08-18 stsp break;
2555 e5a0f69f 2018-08-18 stsp case 'b':
2556 e5a0f69f 2018-08-18 stsp case 'p': {
2557 e5a0f69f 2018-08-18 stsp struct got_object_id *id;
2558 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2559 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2560 e5a0f69f 2018-08-18 stsp if (id == NULL || got_object_id_cmp(id,
2561 e5a0f69f 2018-08-18 stsp s->blamed_commit->id) == 0)
2562 a70480e0 2018-06-23 stsp break;
2563 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2564 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2565 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2566 e5a0f69f 2018-08-18 stsp if (err)
2567 a70480e0 2018-06-23 stsp break;
2568 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2569 b700b5d6 2018-07-10 stsp break;
2570 e5a0f69f 2018-08-18 stsp if (ch == 'p' && pobj == NULL)
2571 dbc6a6b6 2018-07-12 stsp break;
2572 e5a0f69f 2018-08-18 stsp s->done = 1;
2573 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2574 e5a0f69f 2018-08-18 stsp s->done = 0;
2575 e5a0f69f 2018-08-18 stsp if (thread_err)
2576 245d91c1 2018-07-12 stsp break;
2577 e5a0f69f 2018-08-18 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
2578 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2579 e5a0f69f 2018-08-18 stsp obj = NULL;
2580 e5a0f69f 2018-08-18 stsp if (pobj) {
2581 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2582 e5a0f69f 2018-08-18 stsp pobj = NULL;
2583 e5a0f69f 2018-08-18 stsp }
2584 6402fb3c 2018-09-15 stsp err = got_object_qid_alloc(&s->blamed_commit, id);
2585 e5a0f69f 2018-08-18 stsp if (err)
2586 e5a0f69f 2018-08-18 stsp goto done;
2587 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2588 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2589 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2590 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2591 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2592 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2593 e5a0f69f 2018-08-18 stsp if (err)
2594 a70480e0 2018-06-23 stsp break;
2595 e5a0f69f 2018-08-18 stsp break;
2596 84451b3e 2018-07-10 stsp }
2597 e5a0f69f 2018-08-18 stsp case 'B': {
2598 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2599 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2600 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2601 e5a0f69f 2018-08-18 stsp break;
2602 e5a0f69f 2018-08-18 stsp s->done = 1;
2603 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2604 e5a0f69f 2018-08-18 stsp s->done = 0;
2605 e5a0f69f 2018-08-18 stsp if (thread_err)
2606 e5a0f69f 2018-08-18 stsp break;
2607 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2608 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2609 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2610 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2611 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2612 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2613 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2614 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2615 e5a0f69f 2018-08-18 stsp if (err)
2616 e5a0f69f 2018-08-18 stsp break;
2617 245d91c1 2018-07-12 stsp break;
2618 e5a0f69f 2018-08-18 stsp }
2619 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2620 e5a0f69f 2018-08-18 stsp case '\r':
2621 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2622 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2623 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2624 e5a0f69f 2018-08-18 stsp if (err)
2625 e5a0f69f 2018-08-18 stsp break;
2626 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2627 e5a0f69f 2018-08-18 stsp break;
2628 669b5ffa 2018-10-07 stsp
2629 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
2630 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
2631 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2632 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2633 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2634 e5a0f69f 2018-08-18 stsp break;
2635 e5a0f69f 2018-08-18 stsp }
2636 669b5ffa 2018-10-07 stsp err = open_diff_view(diff_view, pobj, obj, s->repo);
2637 e5a0f69f 2018-08-18 stsp if (err) {
2638 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2639 e5a0f69f 2018-08-18 stsp break;
2640 e5a0f69f 2018-08-18 stsp }
2641 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
2642 669b5ffa 2018-10-07 stsp err = view_close_child(view);
2643 669b5ffa 2018-10-07 stsp if (err)
2644 669b5ffa 2018-10-07 stsp return err;
2645 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
2646 669b5ffa 2018-10-07 stsp if (err) {
2647 669b5ffa 2018-10-07 stsp view_close(diff_view);
2648 669b5ffa 2018-10-07 stsp break;
2649 669b5ffa 2018-10-07 stsp }
2650 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(diff_view)) {
2651 669b5ffa 2018-10-07 stsp *focus_view = diff_view;
2652 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
2653 669b5ffa 2018-10-07 stsp }
2654 669b5ffa 2018-10-07 stsp } else
2655 669b5ffa 2018-10-07 stsp *new_view = diff_view;
2656 e5a0f69f 2018-08-18 stsp if (pobj) {
2657 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2658 e5a0f69f 2018-08-18 stsp pobj = NULL;
2659 e5a0f69f 2018-08-18 stsp }
2660 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2661 e5a0f69f 2018-08-18 stsp obj = NULL;
2662 e5a0f69f 2018-08-18 stsp if (err)
2663 e5a0f69f 2018-08-18 stsp break;
2664 e5a0f69f 2018-08-18 stsp break;
2665 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2666 e5a0f69f 2018-08-18 stsp case ' ':
2667 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2668 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2669 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2670 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2671 e5a0f69f 2018-08-18 stsp break;
2672 e5a0f69f 2018-08-18 stsp }
2673 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2674 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2675 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2676 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2677 e5a0f69f 2018-08-18 stsp else
2678 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2679 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2680 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2681 e5a0f69f 2018-08-18 stsp break;
2682 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2683 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2684 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2685 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2686 e5a0f69f 2018-08-18 stsp }
2687 e5a0f69f 2018-08-18 stsp break;
2688 e5a0f69f 2018-08-18 stsp default:
2689 e5a0f69f 2018-08-18 stsp break;
2690 a70480e0 2018-06-23 stsp }
2691 a70480e0 2018-06-23 stsp done:
2692 b880a918 2018-07-10 stsp if (pobj)
2693 b880a918 2018-07-10 stsp got_object_close(pobj);
2694 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2695 a70480e0 2018-06-23 stsp }
2696 a70480e0 2018-06-23 stsp
2697 a70480e0 2018-06-23 stsp static const struct got_error *
2698 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2699 9f7d7167 2018-04-29 stsp {
2700 a70480e0 2018-06-23 stsp const struct got_error *error;
2701 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2702 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2703 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2704 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2705 a70480e0 2018-06-23 stsp int ch;
2706 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2707 a70480e0 2018-06-23 stsp
2708 a70480e0 2018-06-23 stsp #ifndef PROFILE
2709 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2710 ad242220 2018-09-08 stsp == -1)
2711 a70480e0 2018-06-23 stsp err(1, "pledge");
2712 a70480e0 2018-06-23 stsp #endif
2713 a70480e0 2018-06-23 stsp
2714 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2715 a70480e0 2018-06-23 stsp switch (ch) {
2716 a70480e0 2018-06-23 stsp case 'c':
2717 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2718 a70480e0 2018-06-23 stsp break;
2719 69069811 2018-08-02 stsp case 'r':
2720 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2721 69069811 2018-08-02 stsp if (repo_path == NULL)
2722 69069811 2018-08-02 stsp err(1, "-r option");
2723 69069811 2018-08-02 stsp break;
2724 a70480e0 2018-06-23 stsp default:
2725 a70480e0 2018-06-23 stsp usage();
2726 a70480e0 2018-06-23 stsp /* NOTREACHED */
2727 a70480e0 2018-06-23 stsp }
2728 a70480e0 2018-06-23 stsp }
2729 a70480e0 2018-06-23 stsp
2730 a70480e0 2018-06-23 stsp argc -= optind;
2731 a70480e0 2018-06-23 stsp argv += optind;
2732 a70480e0 2018-06-23 stsp
2733 69069811 2018-08-02 stsp if (argc == 1)
2734 69069811 2018-08-02 stsp path = argv[0];
2735 69069811 2018-08-02 stsp else
2736 a70480e0 2018-06-23 stsp usage_blame();
2737 69069811 2018-08-02 stsp
2738 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2739 69069811 2018-08-02 stsp if (cwd == NULL) {
2740 69069811 2018-08-02 stsp error = got_error_from_errno();
2741 69069811 2018-08-02 stsp goto done;
2742 69069811 2018-08-02 stsp }
2743 69069811 2018-08-02 stsp if (repo_path == NULL) {
2744 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2745 69069811 2018-08-02 stsp if (repo_path == NULL) {
2746 69069811 2018-08-02 stsp error = got_error_from_errno();
2747 69069811 2018-08-02 stsp goto done;
2748 69069811 2018-08-02 stsp }
2749 69069811 2018-08-02 stsp }
2750 69069811 2018-08-02 stsp
2751 69069811 2018-08-02 stsp
2752 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2753 a70480e0 2018-06-23 stsp if (error != NULL)
2754 66b4983c 2018-06-23 stsp return error;
2755 69069811 2018-08-02 stsp
2756 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2757 69069811 2018-08-02 stsp if (error != NULL)
2758 69069811 2018-08-02 stsp goto done;
2759 a70480e0 2018-06-23 stsp
2760 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2761 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2762 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2763 a70480e0 2018-06-23 stsp if (error != NULL)
2764 66b4983c 2018-06-23 stsp goto done;
2765 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2766 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2767 a70480e0 2018-06-23 stsp } else {
2768 a70480e0 2018-06-23 stsp struct got_object *obj;
2769 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2770 a70480e0 2018-06-23 stsp if (error != NULL)
2771 66b4983c 2018-06-23 stsp goto done;
2772 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
2773 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2774 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2775 a19e88aa 2018-06-23 stsp got_object_close(obj);
2776 a70480e0 2018-06-23 stsp }
2777 a19e88aa 2018-06-23 stsp if (error != NULL)
2778 a19e88aa 2018-06-23 stsp goto done;
2779 a70480e0 2018-06-23 stsp
2780 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2781 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2782 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2783 e1cd8fed 2018-08-01 stsp goto done;
2784 e1cd8fed 2018-08-01 stsp }
2785 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2786 7cbe629d 2018-08-04 stsp if (error)
2787 7cbe629d 2018-08-04 stsp goto done;
2788 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2789 a70480e0 2018-06-23 stsp done:
2790 69069811 2018-08-02 stsp free(repo_path);
2791 69069811 2018-08-02 stsp free(cwd);
2792 a70480e0 2018-06-23 stsp free(commit_id);
2793 a70480e0 2018-06-23 stsp if (repo)
2794 a70480e0 2018-06-23 stsp got_repo_close(repo);
2795 a70480e0 2018-06-23 stsp return error;
2796 ffd1d5e5 2018-06-23 stsp }
2797 ffd1d5e5 2018-06-23 stsp
2798 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2799 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2800 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2801 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2802 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2803 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2804 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2805 ffd1d5e5 2018-06-23 stsp {
2806 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2807 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2808 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2809 ffd1d5e5 2018-06-23 stsp int width, n;
2810 ffd1d5e5 2018-06-23 stsp
2811 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2812 ffd1d5e5 2018-06-23 stsp
2813 f7d12f7e 2018-08-01 stsp werase(view->window);
2814 ffd1d5e5 2018-06-23 stsp
2815 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2816 ffd1d5e5 2018-06-23 stsp return NULL;
2817 ffd1d5e5 2018-06-23 stsp
2818 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2819 ffd1d5e5 2018-06-23 stsp if (err)
2820 ffd1d5e5 2018-06-23 stsp return err;
2821 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2822 a3404814 2018-09-02 stsp wstandout(view->window);
2823 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2824 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2825 a3404814 2018-09-02 stsp wstandend(view->window);
2826 2550e4c3 2018-07-13 stsp free(wline);
2827 2550e4c3 2018-07-13 stsp wline = NULL;
2828 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2829 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2830 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2831 ffd1d5e5 2018-06-23 stsp return NULL;
2832 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2833 ce52c690 2018-06-23 stsp if (err)
2834 ce52c690 2018-06-23 stsp return err;
2835 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2836 2550e4c3 2018-07-13 stsp free(wline);
2837 2550e4c3 2018-07-13 stsp wline = NULL;
2838 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2839 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2840 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2841 ffd1d5e5 2018-06-23 stsp return NULL;
2842 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2843 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2844 a1eca9bb 2018-06-23 stsp return NULL;
2845 ffd1d5e5 2018-06-23 stsp
2846 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2847 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2848 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2849 0cf4efb1 2018-09-29 stsp if (view->focussed)
2850 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2851 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2852 ffd1d5e5 2018-06-23 stsp }
2853 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2854 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
2855 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2856 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2857 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2858 ffd1d5e5 2018-06-23 stsp return NULL;
2859 ffd1d5e5 2018-06-23 stsp n = 1;
2860 ffd1d5e5 2018-06-23 stsp } else {
2861 ffd1d5e5 2018-06-23 stsp n = 0;
2862 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2863 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2864 ffd1d5e5 2018-06-23 stsp }
2865 ffd1d5e5 2018-06-23 stsp
2866 ffd1d5e5 2018-06-23 stsp while (te) {
2867 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2868 1d13200f 2018-07-12 stsp
2869 1d13200f 2018-07-12 stsp if (show_ids) {
2870 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2871 1d13200f 2018-07-12 stsp if (err)
2872 1d13200f 2018-07-12 stsp return got_error_from_errno();
2873 1d13200f 2018-07-12 stsp }
2874 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2875 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2876 1d13200f 2018-07-12 stsp free(id_str);
2877 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2878 1d13200f 2018-07-12 stsp }
2879 1d13200f 2018-07-12 stsp free(id_str);
2880 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2881 ffd1d5e5 2018-06-23 stsp if (err) {
2882 ffd1d5e5 2018-06-23 stsp free(line);
2883 ffd1d5e5 2018-06-23 stsp break;
2884 ffd1d5e5 2018-06-23 stsp }
2885 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2886 0cf4efb1 2018-09-29 stsp if (view->focussed)
2887 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2888 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2889 ffd1d5e5 2018-06-23 stsp }
2890 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2891 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2892 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2893 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
2894 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2895 ffd1d5e5 2018-06-23 stsp free(line);
2896 2550e4c3 2018-07-13 stsp free(wline);
2897 2550e4c3 2018-07-13 stsp wline = NULL;
2898 ffd1d5e5 2018-06-23 stsp n++;
2899 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2900 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2901 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2902 ffd1d5e5 2018-06-23 stsp break;
2903 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2904 ffd1d5e5 2018-06-23 stsp }
2905 ffd1d5e5 2018-06-23 stsp
2906 ffd1d5e5 2018-06-23 stsp return err;
2907 ffd1d5e5 2018-06-23 stsp }
2908 ffd1d5e5 2018-06-23 stsp
2909 ffd1d5e5 2018-06-23 stsp static void
2910 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2911 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2912 ffd1d5e5 2018-06-23 stsp {
2913 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2914 ffd1d5e5 2018-06-23 stsp int i;
2915 ffd1d5e5 2018-06-23 stsp
2916 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2917 ffd1d5e5 2018-06-23 stsp return;
2918 ffd1d5e5 2018-06-23 stsp
2919 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2920 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2921 ffd1d5e5 2018-06-23 stsp if (!isroot)
2922 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2923 ffd1d5e5 2018-06-23 stsp return;
2924 ffd1d5e5 2018-06-23 stsp }
2925 ffd1d5e5 2018-06-23 stsp
2926 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2927 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2928 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2929 ffd1d5e5 2018-06-23 stsp prev = te;
2930 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2931 ffd1d5e5 2018-06-23 stsp }
2932 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2933 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2934 ffd1d5e5 2018-06-23 stsp }
2935 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2936 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2937 ffd1d5e5 2018-06-23 stsp }
2938 ffd1d5e5 2018-06-23 stsp
2939 ffd1d5e5 2018-06-23 stsp static void
2940 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2941 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2942 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2943 ffd1d5e5 2018-06-23 stsp {
2944 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2945 ffd1d5e5 2018-06-23 stsp int n = 0;
2946 ffd1d5e5 2018-06-23 stsp
2947 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2948 ffd1d5e5 2018-06-23 stsp return;
2949 ffd1d5e5 2018-06-23 stsp
2950 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2951 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2952 ffd1d5e5 2018-06-23 stsp else
2953 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2954 ffd1d5e5 2018-06-23 stsp while (next) {
2955 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2956 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2957 ffd1d5e5 2018-06-23 stsp break;
2958 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2959 ffd1d5e5 2018-06-23 stsp }
2960 ffd1d5e5 2018-06-23 stsp }
2961 ffd1d5e5 2018-06-23 stsp
2962 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2963 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2964 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2965 ffd1d5e5 2018-06-23 stsp {
2966 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2967 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2968 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2969 ffd1d5e5 2018-06-23 stsp
2970 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2971 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2972 ce52c690 2018-06-23 stsp if (te)
2973 ce52c690 2018-06-23 stsp len += strlen(te->name);
2974 ce52c690 2018-06-23 stsp
2975 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2976 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2977 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2978 ffd1d5e5 2018-06-23 stsp
2979 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2980 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2981 d9765a41 2018-06-23 stsp while (pt) {
2982 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2983 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2984 cb2ebc8a 2018-06-23 stsp goto done;
2985 cb2ebc8a 2018-06-23 stsp }
2986 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2987 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2988 cb2ebc8a 2018-06-23 stsp goto done;
2989 cb2ebc8a 2018-06-23 stsp }
2990 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2991 ffd1d5e5 2018-06-23 stsp }
2992 ce52c690 2018-06-23 stsp if (te) {
2993 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2994 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2995 ce52c690 2018-06-23 stsp goto done;
2996 ce52c690 2018-06-23 stsp }
2997 cb2ebc8a 2018-06-23 stsp }
2998 ce52c690 2018-06-23 stsp done:
2999 ce52c690 2018-06-23 stsp if (err) {
3000 ce52c690 2018-06-23 stsp free(*path);
3001 ce52c690 2018-06-23 stsp *path = NULL;
3002 ce52c690 2018-06-23 stsp }
3003 ce52c690 2018-06-23 stsp return err;
3004 ce52c690 2018-06-23 stsp }
3005 ce52c690 2018-06-23 stsp
3006 ce52c690 2018-06-23 stsp static const struct got_error *
3007 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3008 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3009 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
3010 ce52c690 2018-06-23 stsp {
3011 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3012 ce52c690 2018-06-23 stsp char *path;
3013 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3014 69efd4c4 2018-07-18 stsp
3015 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3016 ce52c690 2018-06-23 stsp if (err)
3017 ce52c690 2018-06-23 stsp return err;
3018 ffd1d5e5 2018-06-23 stsp
3019 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3020 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3021 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3022 cdf1ee82 2018-08-01 stsp
3023 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
3024 e5a0f69f 2018-08-18 stsp if (err) {
3025 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3026 e5a0f69f 2018-08-18 stsp free(path);
3027 e5a0f69f 2018-08-18 stsp } else
3028 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3029 69efd4c4 2018-07-18 stsp return err;
3030 69efd4c4 2018-07-18 stsp }
3031 69efd4c4 2018-07-18 stsp
3032 69efd4c4 2018-07-18 stsp static const struct got_error *
3033 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3034 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3035 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
3036 69efd4c4 2018-07-18 stsp {
3037 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3038 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3039 69efd4c4 2018-07-18 stsp char *path;
3040 69efd4c4 2018-07-18 stsp
3041 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3042 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3043 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3044 e5a0f69f 2018-08-18 stsp
3045 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3046 69efd4c4 2018-07-18 stsp if (err)
3047 69efd4c4 2018-07-18 stsp return err;
3048 69efd4c4 2018-07-18 stsp
3049 e5a0f69f 2018-08-18 stsp err = open_log_view(log_view, commit_id, repo, path);
3050 ba4f502b 2018-08-04 stsp if (err)
3051 e5a0f69f 2018-08-18 stsp view_close(log_view);
3052 e5a0f69f 2018-08-18 stsp else
3053 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3054 cb2ebc8a 2018-06-23 stsp free(path);
3055 cb2ebc8a 2018-06-23 stsp return err;
3056 ffd1d5e5 2018-06-23 stsp }
3057 ffd1d5e5 2018-06-23 stsp
3058 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3059 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3060 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
3061 ffd1d5e5 2018-06-23 stsp {
3062 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3063 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3064 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3065 ffd1d5e5 2018-06-23 stsp
3066 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3067 ffd1d5e5 2018-06-23 stsp
3068 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3069 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3070 ffd1d5e5 2018-06-23 stsp goto done;
3071 ffd1d5e5 2018-06-23 stsp
3072 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3073 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
3074 ffd1d5e5 2018-06-23 stsp goto done;
3075 ffd1d5e5 2018-06-23 stsp }
3076 ffd1d5e5 2018-06-23 stsp
3077 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3078 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3079 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3080 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3081 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3082 6484ec90 2018-09-29 stsp err = got_error_from_errno();
3083 6484ec90 2018-09-29 stsp goto done;
3084 6484ec90 2018-09-29 stsp }
3085 fb2756b9 2018-08-04 stsp s->repo = repo;
3086 e5a0f69f 2018-08-18 stsp
3087 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
3088 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
3089 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
3090 ad80ab7b 2018-08-04 stsp done:
3091 ad80ab7b 2018-08-04 stsp free(commit_id_str);
3092 6484ec90 2018-09-29 stsp if (err) {
3093 fb2756b9 2018-08-04 stsp free(s->tree_label);
3094 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3095 6484ec90 2018-09-29 stsp }
3096 ad80ab7b 2018-08-04 stsp return err;
3097 ad80ab7b 2018-08-04 stsp }
3098 ad80ab7b 2018-08-04 stsp
3099 e5a0f69f 2018-08-18 stsp static const struct got_error *
3100 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
3101 ad80ab7b 2018-08-04 stsp {
3102 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3103 ad80ab7b 2018-08-04 stsp
3104 fb2756b9 2018-08-04 stsp free(s->tree_label);
3105 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3106 6484ec90 2018-09-29 stsp free(s->commit_id);
3107 6484ec90 2018-09-29 stsp s->commit_id = NULL;
3108 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
3109 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
3110 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
3111 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
3112 ad80ab7b 2018-08-04 stsp free(parent);
3113 ad80ab7b 2018-08-04 stsp
3114 ad80ab7b 2018-08-04 stsp }
3115 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
3116 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
3117 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
3118 e5a0f69f 2018-08-18 stsp
3119 e5a0f69f 2018-08-18 stsp return NULL;
3120 ad80ab7b 2018-08-04 stsp }
3121 ad80ab7b 2018-08-04 stsp
3122 ad80ab7b 2018-08-04 stsp static const struct got_error *
3123 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
3124 ad80ab7b 2018-08-04 stsp {
3125 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
3126 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3127 e5a0f69f 2018-08-18 stsp char *parent_path;
3128 ad80ab7b 2018-08-04 stsp
3129 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
3130 e5a0f69f 2018-08-18 stsp if (err)
3131 e5a0f69f 2018-08-18 stsp return err;
3132 ffd1d5e5 2018-06-23 stsp
3133 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
3134 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
3135 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3136 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
3137 e5a0f69f 2018-08-18 stsp free(parent_path);
3138 669b5ffa 2018-10-07 stsp
3139 669b5ffa 2018-10-07 stsp view_vborder(view);
3140 e5a0f69f 2018-08-18 stsp return err;
3141 e5a0f69f 2018-08-18 stsp }
3142 ce52c690 2018-06-23 stsp
3143 e5a0f69f 2018-08-18 stsp static const struct got_error *
3144 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3145 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3146 e5a0f69f 2018-08-18 stsp {
3147 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3148 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
3149 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
3150 669b5ffa 2018-10-07 stsp int begin_x = 0;
3151 ffd1d5e5 2018-06-23 stsp
3152 e5a0f69f 2018-08-18 stsp switch (ch) {
3153 e5a0f69f 2018-08-18 stsp case 'i':
3154 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
3155 ffd1d5e5 2018-06-23 stsp break;
3156 e5a0f69f 2018-08-18 stsp case 'l':
3157 669b5ffa 2018-10-07 stsp if (!s->selected_entry)
3158 669b5ffa 2018-10-07 stsp break;
3159 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
3160 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
3161 669b5ffa 2018-10-07 stsp err = log_tree_entry(&log_view, begin_x,
3162 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
3163 669b5ffa 2018-10-07 stsp s->commit_id, s->repo);
3164 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3165 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3166 669b5ffa 2018-10-07 stsp if (err)
3167 669b5ffa 2018-10-07 stsp return err;
3168 669b5ffa 2018-10-07 stsp err = view_set_child(view, log_view);
3169 669b5ffa 2018-10-07 stsp if (err) {
3170 669b5ffa 2018-10-07 stsp view_close(log_view);
3171 669b5ffa 2018-10-07 stsp break;
3172 669b5ffa 2018-10-07 stsp }
3173 669b5ffa 2018-10-07 stsp *focus_view = log_view;
3174 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3175 669b5ffa 2018-10-07 stsp } else
3176 669b5ffa 2018-10-07 stsp *new_view = log_view;
3177 e5a0f69f 2018-08-18 stsp break;
3178 e5a0f69f 2018-08-18 stsp case 'k':
3179 e5a0f69f 2018-08-18 stsp case KEY_UP:
3180 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3181 e5a0f69f 2018-08-18 stsp s->selected--;
3182 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3183 ffd1d5e5 2018-06-23 stsp break;
3184 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
3185 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
3186 e5a0f69f 2018-08-18 stsp break;
3187 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
3188 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
3189 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
3190 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
3191 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3192 e5a0f69f 2018-08-18 stsp s->selected = 0;
3193 69efd4c4 2018-07-18 stsp break;
3194 e5a0f69f 2018-08-18 stsp }
3195 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
3196 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
3197 e5a0f69f 2018-08-18 stsp s->tree == s->root);
3198 e5a0f69f 2018-08-18 stsp break;
3199 e5a0f69f 2018-08-18 stsp case 'j':
3200 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
3201 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
3202 e5a0f69f 2018-08-18 stsp s->selected++;
3203 1d13200f 2018-07-12 stsp break;
3204 e5a0f69f 2018-08-18 stsp }
3205 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
3206 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
3207 e5a0f69f 2018-08-18 stsp break;
3208 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
3209 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
3210 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
3211 e5a0f69f 2018-08-18 stsp s->entries);
3212 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
3213 e5a0f69f 2018-08-18 stsp entry))
3214 ffd1d5e5 2018-06-23 stsp break;
3215 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
3216 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
3217 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3218 e5a0f69f 2018-08-18 stsp break;
3219 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
3220 e5a0f69f 2018-08-18 stsp case '\r':
3221 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
3222 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3223 7837eeac 2018-09-24 stsp case KEY_BACKSPACE:
3224 e5a0f69f 2018-08-18 stsp /* user selected '..' */
3225 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
3226 ffd1d5e5 2018-06-23 stsp break;
3227 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
3228 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
3229 e5a0f69f 2018-08-18 stsp entry);
3230 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
3231 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
3232 e5a0f69f 2018-08-18 stsp s->entries =
3233 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3234 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
3235 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
3236 e5a0f69f 2018-08-18 stsp s->selected_entry =
3237 e5a0f69f 2018-08-18 stsp parent->selected_entry;
3238 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
3239 e5a0f69f 2018-08-18 stsp free(parent);
3240 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
3241 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3242 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
3243 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
3244 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
3245 e5a0f69f 2018-08-18 stsp if (err)
3246 ffd1d5e5 2018-06-23 stsp break;
3247 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
3248 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
3249 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
3250 e5a0f69f 2018-08-18 stsp break;
3251 ffd1d5e5 2018-06-23 stsp }
3252 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
3253 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
3254 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
3255 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
3256 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
3257 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3258 e5a0f69f 2018-08-18 stsp s->tree = child;
3259 e5a0f69f 2018-08-18 stsp s->entries =
3260 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3261 e5a0f69f 2018-08-18 stsp s->selected = 0;
3262 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3263 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
3264 669b5ffa 2018-10-07 stsp struct tog_view *blame_view;
3265 669b5ffa 2018-10-07 stsp int begin_x = view_is_parent_view(view) ?
3266 669b5ffa 2018-10-07 stsp view_split_begin_x(view->begin_x) : 0;
3267 669b5ffa 2018-10-07 stsp
3268 669b5ffa 2018-10-07 stsp err = blame_tree_entry(&blame_view, begin_x,
3269 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents, s->commit_id,
3270 669b5ffa 2018-10-07 stsp s->repo);
3271 e5a0f69f 2018-08-18 stsp if (err)
3272 ffd1d5e5 2018-06-23 stsp break;
3273 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3274 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3275 669b5ffa 2018-10-07 stsp if (err)
3276 669b5ffa 2018-10-07 stsp return err;
3277 669b5ffa 2018-10-07 stsp err = view_set_child(view, blame_view);
3278 669b5ffa 2018-10-07 stsp if (err) {
3279 669b5ffa 2018-10-07 stsp view_close(blame_view);
3280 669b5ffa 2018-10-07 stsp break;
3281 669b5ffa 2018-10-07 stsp }
3282 669b5ffa 2018-10-07 stsp *focus_view = blame_view;
3283 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3284 669b5ffa 2018-10-07 stsp } else
3285 669b5ffa 2018-10-07 stsp *new_view = blame_view;
3286 e5a0f69f 2018-08-18 stsp }
3287 e5a0f69f 2018-08-18 stsp break;
3288 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3289 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3290 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3291 e5a0f69f 2018-08-18 stsp break;
3292 e5a0f69f 2018-08-18 stsp default:
3293 e5a0f69f 2018-08-18 stsp break;
3294 ffd1d5e5 2018-06-23 stsp }
3295 e5a0f69f 2018-08-18 stsp
3296 ffd1d5e5 2018-06-23 stsp return err;
3297 9f7d7167 2018-04-29 stsp }
3298 9f7d7167 2018-04-29 stsp
3299 ffd1d5e5 2018-06-23 stsp __dead static void
3300 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3301 ffd1d5e5 2018-06-23 stsp {
3302 ffd1d5e5 2018-06-23 stsp endwin();
3303 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3304 ffd1d5e5 2018-06-23 stsp getprogname());
3305 ffd1d5e5 2018-06-23 stsp exit(1);
3306 ffd1d5e5 2018-06-23 stsp }
3307 ffd1d5e5 2018-06-23 stsp
3308 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3309 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3310 ffd1d5e5 2018-06-23 stsp {
3311 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3312 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3313 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3314 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3315 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3316 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3317 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3318 ffd1d5e5 2018-06-23 stsp int ch;
3319 5221c383 2018-08-01 stsp struct tog_view *view;
3320 ffd1d5e5 2018-06-23 stsp
3321 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3322 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3323 ad242220 2018-09-08 stsp == -1)
3324 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3325 ffd1d5e5 2018-06-23 stsp #endif
3326 ffd1d5e5 2018-06-23 stsp
3327 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3328 ffd1d5e5 2018-06-23 stsp switch (ch) {
3329 ffd1d5e5 2018-06-23 stsp case 'c':
3330 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3331 ffd1d5e5 2018-06-23 stsp break;
3332 ffd1d5e5 2018-06-23 stsp default:
3333 ffd1d5e5 2018-06-23 stsp usage();
3334 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3335 ffd1d5e5 2018-06-23 stsp }
3336 ffd1d5e5 2018-06-23 stsp }
3337 ffd1d5e5 2018-06-23 stsp
3338 ffd1d5e5 2018-06-23 stsp argc -= optind;
3339 ffd1d5e5 2018-06-23 stsp argv += optind;
3340 ffd1d5e5 2018-06-23 stsp
3341 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3342 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
3343 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3344 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3345 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3346 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3347 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3348 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3349 ffd1d5e5 2018-06-23 stsp } else
3350 ffd1d5e5 2018-06-23 stsp usage_log();
3351 ffd1d5e5 2018-06-23 stsp
3352 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3353 ffd1d5e5 2018-06-23 stsp free(repo_path);
3354 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3355 ffd1d5e5 2018-06-23 stsp return error;
3356 ffd1d5e5 2018-06-23 stsp
3357 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
3358 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3359 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3360 ffd1d5e5 2018-06-23 stsp goto done;
3361 ffd1d5e5 2018-06-23 stsp } else {
3362 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
3363 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3364 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
3365 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
3366 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
3367 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
3368 ffd1d5e5 2018-06-23 stsp }
3369 ffd1d5e5 2018-06-23 stsp }
3370 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3371 ffd1d5e5 2018-06-23 stsp goto done;
3372 ffd1d5e5 2018-06-23 stsp
3373 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3374 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3375 ffd1d5e5 2018-06-23 stsp goto done;
3376 ffd1d5e5 2018-06-23 stsp
3377 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3378 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3379 ffd1d5e5 2018-06-23 stsp goto done;
3380 ffd1d5e5 2018-06-23 stsp
3381 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3382 5221c383 2018-08-01 stsp if (view == NULL) {
3383 5221c383 2018-08-01 stsp error = got_error_from_errno();
3384 5221c383 2018-08-01 stsp goto done;
3385 5221c383 2018-08-01 stsp }
3386 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3387 ad80ab7b 2018-08-04 stsp if (error)
3388 ad80ab7b 2018-08-04 stsp goto done;
3389 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3390 ffd1d5e5 2018-06-23 stsp done:
3391 ffd1d5e5 2018-06-23 stsp free(commit_id);
3392 ffd1d5e5 2018-06-23 stsp if (commit)
3393 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3394 ffd1d5e5 2018-06-23 stsp if (tree)
3395 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3396 ffd1d5e5 2018-06-23 stsp if (repo)
3397 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3398 ffd1d5e5 2018-06-23 stsp return error;
3399 ffd1d5e5 2018-06-23 stsp }
3400 5c5136c5 2018-05-20 stsp static void
3401 9f7d7167 2018-04-29 stsp init_curses(void)
3402 9f7d7167 2018-04-29 stsp {
3403 9f7d7167 2018-04-29 stsp initscr();
3404 9f7d7167 2018-04-29 stsp cbreak();
3405 fd823528 2018-10-22 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
3406 9f7d7167 2018-04-29 stsp noecho();
3407 9f7d7167 2018-04-29 stsp nonl();
3408 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3409 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3410 1f475ad8 2018-05-10 stsp curs_set(0);
3411 9f7d7167 2018-04-29 stsp }
3412 9f7d7167 2018-04-29 stsp
3413 4ed7e80c 2018-05-20 stsp __dead static void
3414 9f7d7167 2018-04-29 stsp usage(void)
3415 9f7d7167 2018-04-29 stsp {
3416 9f7d7167 2018-04-29 stsp int i;
3417 9f7d7167 2018-04-29 stsp
3418 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3419 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3420 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3421 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3422 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3423 9f7d7167 2018-04-29 stsp }
3424 9f7d7167 2018-04-29 stsp exit(1);
3425 9f7d7167 2018-04-29 stsp }
3426 9f7d7167 2018-04-29 stsp
3427 c2301be8 2018-04-30 stsp static char **
3428 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3429 c2301be8 2018-04-30 stsp {
3430 c2301be8 2018-04-30 stsp char **argv;
3431 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3432 c2301be8 2018-04-30 stsp
3433 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3434 c2301be8 2018-04-30 stsp if (argv == NULL)
3435 c2301be8 2018-04-30 stsp err(1, "calloc");
3436 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3437 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3438 c2301be8 2018-04-30 stsp err(1, "calloc");
3439 c2301be8 2018-04-30 stsp if (arg1) {
3440 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3441 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3442 c2301be8 2018-04-30 stsp err(1, "calloc");
3443 c2301be8 2018-04-30 stsp }
3444 c2301be8 2018-04-30 stsp
3445 c2301be8 2018-04-30 stsp return argv;
3446 c2301be8 2018-04-30 stsp }
3447 c2301be8 2018-04-30 stsp
3448 9f7d7167 2018-04-29 stsp int
3449 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3450 9f7d7167 2018-04-29 stsp {
3451 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3452 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3453 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3454 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3455 9f7d7167 2018-04-29 stsp
3456 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
3457 9f7d7167 2018-04-29 stsp
3458 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3459 9f7d7167 2018-04-29 stsp switch (ch) {
3460 9f7d7167 2018-04-29 stsp case 'h':
3461 9f7d7167 2018-04-29 stsp hflag = 1;
3462 9f7d7167 2018-04-29 stsp break;
3463 9f7d7167 2018-04-29 stsp default:
3464 9f7d7167 2018-04-29 stsp usage();
3465 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3466 9f7d7167 2018-04-29 stsp }
3467 9f7d7167 2018-04-29 stsp }
3468 9f7d7167 2018-04-29 stsp
3469 9f7d7167 2018-04-29 stsp argc -= optind;
3470 9f7d7167 2018-04-29 stsp argv += optind;
3471 9f7d7167 2018-04-29 stsp optind = 0;
3472 c2301be8 2018-04-30 stsp optreset = 1;
3473 9f7d7167 2018-04-29 stsp
3474 c2301be8 2018-04-30 stsp if (argc == 0) {
3475 f29d3e89 2018-06-23 stsp if (hflag)
3476 f29d3e89 2018-06-23 stsp usage();
3477 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3478 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3479 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3480 c2301be8 2018-04-30 stsp argc = 1;
3481 c2301be8 2018-04-30 stsp } else {
3482 9f7d7167 2018-04-29 stsp int i;
3483 9f7d7167 2018-04-29 stsp
3484 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3485 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3486 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3487 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3488 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3489 9f7d7167 2018-04-29 stsp if (hflag)
3490 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3491 9f7d7167 2018-04-29 stsp break;
3492 9f7d7167 2018-04-29 stsp }
3493 9f7d7167 2018-04-29 stsp }
3494 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3495 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3496 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3497 c2301be8 2018-04-30 stsp if (repo_path) {
3498 c2301be8 2018-04-30 stsp struct got_repository *repo;
3499 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3500 c2301be8 2018-04-30 stsp if (error == NULL)
3501 c2301be8 2018-04-30 stsp got_repo_close(repo);
3502 c2301be8 2018-04-30 stsp } else
3503 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3504 c2301be8 2018-04-30 stsp if (error) {
3505 f29d3e89 2018-06-23 stsp if (hflag) {
3506 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3507 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3508 f29d3e89 2018-06-23 stsp argv[0]);
3509 f29d3e89 2018-06-23 stsp usage();
3510 f29d3e89 2018-06-23 stsp }
3511 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3512 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3513 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3514 ad7de8d9 2018-04-30 stsp free(repo_path);
3515 c2301be8 2018-04-30 stsp return 1;
3516 c2301be8 2018-04-30 stsp }
3517 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3518 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3519 c2301be8 2018-04-30 stsp argc = 2;
3520 c2301be8 2018-04-30 stsp free(repo_path);
3521 9f7d7167 2018-04-29 stsp }
3522 9f7d7167 2018-04-29 stsp }
3523 9f7d7167 2018-04-29 stsp
3524 5c5136c5 2018-05-20 stsp init_curses();
3525 9f7d7167 2018-04-29 stsp
3526 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3527 9f7d7167 2018-04-29 stsp if (error)
3528 9f7d7167 2018-04-29 stsp goto done;
3529 9f7d7167 2018-04-29 stsp done:
3530 9f7d7167 2018-04-29 stsp endwin();
3531 c2301be8 2018-04-30 stsp free(cmd_argv);
3532 9f7d7167 2018-04-29 stsp if (error)
3533 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3534 9f7d7167 2018-04-29 stsp return 0;
3535 9f7d7167 2018-04-29 stsp }