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 1a76625f 2018-10-22 stsp int done = 0, errcode;
599 e5a0f69f 2018-08-18 stsp
600 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
601 1a76625f 2018-10-22 stsp if (errcode)
602 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
603 1a76625f 2018-10-22 stsp
604 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
605 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
606 e5a0f69f 2018-08-18 stsp
607 a81bf10d 2018-09-29 stsp main_view = view;
608 1004088d 2018-09-29 stsp view->focussed = 1;
609 878940b7 2018-09-29 stsp err = view->show(view);
610 0cf4efb1 2018-09-29 stsp if (err)
611 0cf4efb1 2018-09-29 stsp return err;
612 0cf4efb1 2018-09-29 stsp update_panels();
613 0cf4efb1 2018-09-29 stsp doupdate();
614 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
615 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
616 e4197bf9 2018-08-18 stsp view, &views);
617 e5a0f69f 2018-08-18 stsp if (err)
618 e5a0f69f 2018-08-18 stsp break;
619 e5a0f69f 2018-08-18 stsp if (dead_view) {
620 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
621 669b5ffa 2018-10-07 stsp
622 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
623 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
624 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
625 669b5ffa 2018-10-07 stsp else
626 669b5ffa 2018-10-07 stsp prev = view->parent;
627 669b5ffa 2018-10-07 stsp
628 669b5ffa 2018-10-07 stsp if (dead_view->parent)
629 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
630 669b5ffa 2018-10-07 stsp else
631 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
632 669b5ffa 2018-10-07 stsp
633 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
634 a81bf10d 2018-09-29 stsp if (err || dead_view == main_view)
635 e5a0f69f 2018-08-18 stsp goto done;
636 669b5ffa 2018-10-07 stsp
637 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
638 0cf4efb1 2018-09-29 stsp if (focus_view)
639 0cf4efb1 2018-09-29 stsp view = focus_view;
640 669b5ffa 2018-10-07 stsp else if (prev)
641 669b5ffa 2018-10-07 stsp view = prev;
642 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
643 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
644 0cf4efb1 2018-09-29 stsp tog_view_list_head);
645 669b5ffa 2018-10-07 stsp else
646 0cf4efb1 2018-09-29 stsp view = NULL;
647 669b5ffa 2018-10-07 stsp if (view) {
648 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
649 669b5ffa 2018-10-07 stsp focus_view = view->child;
650 669b5ffa 2018-10-07 stsp else
651 669b5ffa 2018-10-07 stsp focus_view = view;
652 669b5ffa 2018-10-07 stsp }
653 0cf4efb1 2018-09-29 stsp }
654 e5a0f69f 2018-08-18 stsp }
655 bcbd79e2 2018-08-19 stsp if (new_view) {
656 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
657 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
658 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
659 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
660 86c66b02 2018-10-18 stsp continue;
661 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
662 86c66b02 2018-10-18 stsp err = view_close(v);
663 86c66b02 2018-10-18 stsp if (err)
664 86c66b02 2018-10-18 stsp goto done;
665 86c66b02 2018-10-18 stsp if (v == view)
666 86c66b02 2018-10-18 stsp view = new_view;
667 86c66b02 2018-10-18 stsp break;
668 86c66b02 2018-10-18 stsp }
669 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
670 878940b7 2018-09-29 stsp if (focus_view == NULL)
671 878940b7 2018-09-29 stsp focus_view = new_view;
672 1a76625f 2018-10-22 stsp }
673 0cf4efb1 2018-09-29 stsp if (focus_view) {
674 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
675 669b5ffa 2018-10-07 stsp if (view)
676 0cf4efb1 2018-09-29 stsp view->focussed = 0;
677 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
678 0cf4efb1 2018-09-29 stsp view = focus_view;
679 878940b7 2018-09-29 stsp if (new_view)
680 878940b7 2018-09-29 stsp show_panel(new_view->panel);
681 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
682 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
683 bcbd79e2 2018-08-19 stsp }
684 669b5ffa 2018-10-07 stsp if (view) {
685 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
686 1a76625f 2018-10-22 stsp focus_view = view;
687 1a76625f 2018-10-22 stsp focus_view->focussed = 1;
688 1a76625f 2018-10-22 stsp }
689 669b5ffa 2018-10-07 stsp if (view->parent) {
690 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
691 669b5ffa 2018-10-07 stsp if (err)
692 1a76625f 2018-10-22 stsp goto done;
693 669b5ffa 2018-10-07 stsp }
694 669b5ffa 2018-10-07 stsp err = view->show(view);
695 0cf4efb1 2018-09-29 stsp if (err)
696 1a76625f 2018-10-22 stsp goto done;
697 669b5ffa 2018-10-07 stsp if (view->child) {
698 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
699 669b5ffa 2018-10-07 stsp if (err)
700 1a76625f 2018-10-22 stsp goto done;
701 669b5ffa 2018-10-07 stsp }
702 1a76625f 2018-10-22 stsp update_panels();
703 1a76625f 2018-10-22 stsp doupdate();
704 0cf4efb1 2018-09-29 stsp }
705 e5a0f69f 2018-08-18 stsp }
706 e5a0f69f 2018-08-18 stsp done:
707 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
708 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
709 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
710 e5a0f69f 2018-08-18 stsp view_close(view);
711 e5a0f69f 2018-08-18 stsp }
712 1a76625f 2018-10-22 stsp
713 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
714 1a76625f 2018-10-22 stsp if (errcode)
715 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
716 1a76625f 2018-10-22 stsp
717 e5a0f69f 2018-08-18 stsp return err;
718 ea5e7bb5 2018-08-01 stsp }
719 ea5e7bb5 2018-08-01 stsp
720 4ed7e80c 2018-05-20 stsp __dead static void
721 9f7d7167 2018-04-29 stsp usage_log(void)
722 9f7d7167 2018-04-29 stsp {
723 80ddbec8 2018-04-29 stsp endwin();
724 c70c5802 2018-08-01 stsp fprintf(stderr,
725 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
726 9f7d7167 2018-04-29 stsp getprogname());
727 9f7d7167 2018-04-29 stsp exit(1);
728 80ddbec8 2018-04-29 stsp }
729 80ddbec8 2018-04-29 stsp
730 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
731 80ddbec8 2018-04-29 stsp static const struct got_error *
732 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
733 963b370f 2018-05-20 stsp {
734 00dfcb92 2018-06-11 stsp char *vis = NULL;
735 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
736 963b370f 2018-05-20 stsp
737 963b370f 2018-05-20 stsp *ws = NULL;
738 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
739 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
740 00dfcb92 2018-06-11 stsp int vislen;
741 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
742 00dfcb92 2018-06-11 stsp return got_error_from_errno();
743 00dfcb92 2018-06-11 stsp
744 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
745 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
746 00dfcb92 2018-06-11 stsp if (err)
747 00dfcb92 2018-06-11 stsp return err;
748 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
749 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
750 a7f50699 2018-06-11 stsp err = got_error_from_errno(); /* give up */
751 a7f50699 2018-06-11 stsp goto done;
752 a7f50699 2018-06-11 stsp }
753 00dfcb92 2018-06-11 stsp }
754 963b370f 2018-05-20 stsp
755 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
756 a7f50699 2018-06-11 stsp if (*ws == NULL) {
757 a7f50699 2018-06-11 stsp err = got_error_from_errno();
758 a7f50699 2018-06-11 stsp goto done;
759 a7f50699 2018-06-11 stsp }
760 963b370f 2018-05-20 stsp
761 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
762 963b370f 2018-05-20 stsp err = got_error_from_errno();
763 a7f50699 2018-06-11 stsp done:
764 00dfcb92 2018-06-11 stsp free(vis);
765 963b370f 2018-05-20 stsp if (err) {
766 963b370f 2018-05-20 stsp free(*ws);
767 963b370f 2018-05-20 stsp *ws = NULL;
768 963b370f 2018-05-20 stsp *wlen = 0;
769 963b370f 2018-05-20 stsp }
770 963b370f 2018-05-20 stsp return err;
771 963b370f 2018-05-20 stsp }
772 963b370f 2018-05-20 stsp
773 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
774 963b370f 2018-05-20 stsp static const struct got_error *
775 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
776 963b370f 2018-05-20 stsp {
777 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
778 963b370f 2018-05-20 stsp int cols = 0;
779 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
780 963b370f 2018-05-20 stsp size_t wlen;
781 963b370f 2018-05-20 stsp int i;
782 963b370f 2018-05-20 stsp
783 963b370f 2018-05-20 stsp *wlinep = NULL;
784 b700b5d6 2018-07-10 stsp *widthp = 0;
785 963b370f 2018-05-20 stsp
786 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
787 963b370f 2018-05-20 stsp if (err)
788 963b370f 2018-05-20 stsp return err;
789 963b370f 2018-05-20 stsp
790 963b370f 2018-05-20 stsp i = 0;
791 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
792 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
793 963b370f 2018-05-20 stsp switch (width) {
794 963b370f 2018-05-20 stsp case 0:
795 b700b5d6 2018-07-10 stsp i++;
796 963b370f 2018-05-20 stsp break;
797 963b370f 2018-05-20 stsp case 1:
798 963b370f 2018-05-20 stsp case 2:
799 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
800 b700b5d6 2018-07-10 stsp cols += width;
801 3c1f04f1 2018-09-13 stsp i++;
802 963b370f 2018-05-20 stsp break;
803 963b370f 2018-05-20 stsp case -1:
804 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
805 b700b5d6 2018-07-10 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
806 b700b5d6 2018-07-10 stsp i++;
807 963b370f 2018-05-20 stsp break;
808 963b370f 2018-05-20 stsp default:
809 963b370f 2018-05-20 stsp err = got_error_from_errno();
810 963b370f 2018-05-20 stsp goto done;
811 963b370f 2018-05-20 stsp }
812 963b370f 2018-05-20 stsp }
813 963b370f 2018-05-20 stsp wline[i] = L'\0';
814 b700b5d6 2018-07-10 stsp if (widthp)
815 b700b5d6 2018-07-10 stsp *widthp = cols;
816 963b370f 2018-05-20 stsp done:
817 963b370f 2018-05-20 stsp if (err)
818 963b370f 2018-05-20 stsp free(wline);
819 963b370f 2018-05-20 stsp else
820 963b370f 2018-05-20 stsp *wlinep = wline;
821 963b370f 2018-05-20 stsp return err;
822 963b370f 2018-05-20 stsp }
823 963b370f 2018-05-20 stsp
824 963b370f 2018-05-20 stsp static const struct got_error *
825 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
826 2814baeb 2018-08-01 stsp struct got_object_id *id)
827 80ddbec8 2018-04-29 stsp {
828 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
829 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
830 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
831 6d9fbc00 2018-04-29 stsp char *author0 = NULL, *author = NULL;
832 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
833 bb737323 2018-05-20 stsp int author_width, logmsg_width;
834 6d9fbc00 2018-04-29 stsp char *newline, *smallerthan;
835 80ddbec8 2018-04-29 stsp char *line = NULL;
836 bb737323 2018-05-20 stsp int col, limit;
837 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
838 bb737323 2018-05-20 stsp static const size_t author_display_cols = 16;
839 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
840 80ddbec8 2018-04-29 stsp
841 c70c5802 2018-08-01 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
842 c70c5802 2018-08-01 stsp &commit->tm_committer) >= sizeof(datebuf))
843 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
844 b39d25c7 2018-07-10 stsp
845 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
846 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
847 b39d25c7 2018-07-10 stsp else
848 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
849 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
850 b39d25c7 2018-07-10 stsp col = limit + 1;
851 b39d25c7 2018-07-10 stsp if (col > avail)
852 b39d25c7 2018-07-10 stsp goto done;
853 b39d25c7 2018-07-10 stsp
854 6d9fbc00 2018-04-29 stsp author0 = strdup(commit->author);
855 6d9fbc00 2018-04-29 stsp if (author0 == NULL) {
856 80ddbec8 2018-04-29 stsp err = got_error_from_errno();
857 80ddbec8 2018-04-29 stsp goto done;
858 80ddbec8 2018-04-29 stsp }
859 6d9fbc00 2018-04-29 stsp author = author0;
860 6d9fbc00 2018-04-29 stsp smallerthan = strchr(author, '<');
861 6d9fbc00 2018-04-29 stsp if (smallerthan)
862 6d9fbc00 2018-04-29 stsp *smallerthan = '\0';
863 6d9fbc00 2018-04-29 stsp else {
864 6d9fbc00 2018-04-29 stsp char *at = strchr(author, '@');
865 6d9fbc00 2018-04-29 stsp if (at)
866 6d9fbc00 2018-04-29 stsp *at = '\0';
867 6d9fbc00 2018-04-29 stsp }
868 b39d25c7 2018-07-10 stsp limit = avail - col;
869 bb737323 2018-05-20 stsp err = format_line(&wauthor, &author_width, author, limit);
870 bb737323 2018-05-20 stsp if (err)
871 bb737323 2018-05-20 stsp goto done;
872 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
873 bb737323 2018-05-20 stsp col += author_width;
874 9c2eaf34 2018-05-20 stsp while (col <= avail && author_width < author_display_cols + 1) {
875 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
876 bb737323 2018-05-20 stsp col++;
877 bb737323 2018-05-20 stsp author_width++;
878 bb737323 2018-05-20 stsp }
879 9c2eaf34 2018-05-20 stsp if (col > avail)
880 9c2eaf34 2018-05-20 stsp goto done;
881 80ddbec8 2018-04-29 stsp
882 bb737323 2018-05-20 stsp logmsg0 = strdup(commit->logmsg);
883 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
884 6d9fbc00 2018-04-29 stsp err = got_error_from_errno();
885 6d9fbc00 2018-04-29 stsp goto done;
886 6d9fbc00 2018-04-29 stsp }
887 bb737323 2018-05-20 stsp logmsg = logmsg0;
888 bb737323 2018-05-20 stsp while (*logmsg == '\n')
889 bb737323 2018-05-20 stsp logmsg++;
890 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
891 bb737323 2018-05-20 stsp if (newline)
892 bb737323 2018-05-20 stsp *newline = '\0';
893 bb737323 2018-05-20 stsp limit = avail - col;
894 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
895 bb737323 2018-05-20 stsp if (err)
896 bb737323 2018-05-20 stsp goto done;
897 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
898 bb737323 2018-05-20 stsp col += logmsg_width;
899 bb737323 2018-05-20 stsp while (col <= avail) {
900 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
901 bb737323 2018-05-20 stsp col++;
902 881b2d3e 2018-04-30 stsp }
903 80ddbec8 2018-04-29 stsp done:
904 80ddbec8 2018-04-29 stsp free(logmsg0);
905 bb737323 2018-05-20 stsp free(wlogmsg);
906 6d9fbc00 2018-04-29 stsp free(author0);
907 bb737323 2018-05-20 stsp free(wauthor);
908 80ddbec8 2018-04-29 stsp free(line);
909 80ddbec8 2018-04-29 stsp return err;
910 80ddbec8 2018-04-29 stsp }
911 26ed57b2 2018-05-19 stsp
912 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
913 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
914 899d86c2 2018-05-10 stsp struct got_object_id *id)
915 80ddbec8 2018-04-29 stsp {
916 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
917 80ddbec8 2018-04-29 stsp
918 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
919 80ddbec8 2018-04-29 stsp if (entry == NULL)
920 899d86c2 2018-05-10 stsp return NULL;
921 99db9666 2018-05-07 stsp
922 899d86c2 2018-05-10 stsp entry->id = id;
923 99db9666 2018-05-07 stsp entry->commit = commit;
924 899d86c2 2018-05-10 stsp return entry;
925 99db9666 2018-05-07 stsp }
926 80ddbec8 2018-04-29 stsp
927 99db9666 2018-05-07 stsp static void
928 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
929 99db9666 2018-05-07 stsp {
930 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
931 99db9666 2018-05-07 stsp
932 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
933 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
934 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
935 ecb28ae0 2018-07-16 stsp commits->ncommits--;
936 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
937 99db9666 2018-05-07 stsp free(entry);
938 99db9666 2018-05-07 stsp }
939 99db9666 2018-05-07 stsp
940 99db9666 2018-05-07 stsp static void
941 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
942 99db9666 2018-05-07 stsp {
943 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
944 99db9666 2018-05-07 stsp pop_commit(commits);
945 c4972b91 2018-05-07 stsp }
946 c4972b91 2018-05-07 stsp
947 c4972b91 2018-05-07 stsp static const struct got_error *
948 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
949 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
950 c4972b91 2018-05-07 stsp {
951 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
952 93e45b7c 2018-09-24 stsp int nqueued = 0;
953 9ba79e04 2018-06-11 stsp
954 1a76625f 2018-10-22 stsp /*
955 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
956 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
957 1a76625f 2018-10-22 stsp * while updating the display.
958 1a76625f 2018-10-22 stsp */
959 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
960 93e45b7c 2018-09-24 stsp struct got_object_id *id;
961 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
962 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
963 1a76625f 2018-10-22 stsp int errcode;
964 899d86c2 2018-05-10 stsp
965 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
966 9ba79e04 2018-06-11 stsp if (err) {
967 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
968 93e45b7c 2018-09-24 stsp break;
969 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
970 93e45b7c 2018-09-24 stsp minqueue, repo);
971 ecb28ae0 2018-07-16 stsp if (err)
972 ecb28ae0 2018-07-16 stsp return err;
973 ecb28ae0 2018-07-16 stsp continue;
974 9ba79e04 2018-06-11 stsp }
975 93e45b7c 2018-09-24 stsp
976 ecb28ae0 2018-07-16 stsp if (id == NULL)
977 ecb28ae0 2018-07-16 stsp break;
978 899d86c2 2018-05-10 stsp
979 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
980 9ba79e04 2018-06-11 stsp if (err)
981 9ba79e04 2018-06-11 stsp break;
982 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
983 9ba79e04 2018-06-11 stsp if (entry == NULL) {
984 9ba79e04 2018-06-11 stsp err = got_error_from_errno();
985 9ba79e04 2018-06-11 stsp break;
986 9ba79e04 2018-06-11 stsp }
987 93e45b7c 2018-09-24 stsp
988 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
989 1a76625f 2018-10-22 stsp if (errcode) {
990 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
991 1a76625f 2018-10-22 stsp break;
992 1a76625f 2018-10-22 stsp }
993 1a76625f 2018-10-22 stsp
994 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
995 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
996 ecb28ae0 2018-07-16 stsp nqueued++;
997 ecb28ae0 2018-07-16 stsp commits->ncommits++;
998 1a76625f 2018-10-22 stsp
999 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1000 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1001 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
1002 899d86c2 2018-05-10 stsp }
1003 899d86c2 2018-05-10 stsp
1004 9ba79e04 2018-06-11 stsp return err;
1005 99db9666 2018-05-07 stsp }
1006 99db9666 2018-05-07 stsp
1007 99db9666 2018-05-07 stsp static const struct got_error *
1008 9ba79e04 2018-06-11 stsp get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1009 99db9666 2018-05-07 stsp {
1010 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1011 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1012 99db9666 2018-05-07 stsp
1013 9ba79e04 2018-06-11 stsp *head_id = NULL;
1014 899d86c2 2018-05-10 stsp
1015 9ba79e04 2018-06-11 stsp err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1016 99db9666 2018-05-07 stsp if (err)
1017 99db9666 2018-05-07 stsp return err;
1018 99db9666 2018-05-07 stsp
1019 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1020 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1021 9ba79e04 2018-06-11 stsp if (err) {
1022 9ba79e04 2018-06-11 stsp *head_id = NULL;
1023 99db9666 2018-05-07 stsp return err;
1024 0553a4e3 2018-04-30 stsp }
1025 80ddbec8 2018-04-29 stsp
1026 9ba79e04 2018-06-11 stsp return NULL;
1027 0553a4e3 2018-04-30 stsp }
1028 0553a4e3 2018-04-30 stsp
1029 0553a4e3 2018-04-30 stsp static const struct got_error *
1030 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1031 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1032 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1033 1a76625f 2018-10-22 stsp const char *path, int commits_needed)
1034 0553a4e3 2018-04-30 stsp {
1035 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1036 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1037 ecb28ae0 2018-07-16 stsp int ncommits, width;
1038 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1039 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1040 0553a4e3 2018-04-30 stsp
1041 e0d42f60 2018-07-22 stsp entry = first;
1042 e0d42f60 2018-07-22 stsp ncommits = 0;
1043 e0d42f60 2018-07-22 stsp while (entry) {
1044 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1045 e0d42f60 2018-07-22 stsp *selected = entry;
1046 e0d42f60 2018-07-22 stsp break;
1047 e0d42f60 2018-07-22 stsp }
1048 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1049 e0d42f60 2018-07-22 stsp ncommits++;
1050 e0d42f60 2018-07-22 stsp }
1051 e0d42f60 2018-07-22 stsp
1052 1a76625f 2018-10-22 stsp if (*selected) {
1053 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1054 1a76625f 2018-10-22 stsp if (err)
1055 ecb28ae0 2018-07-16 stsp return err;
1056 867c6645 2018-07-10 stsp }
1057 1a76625f 2018-10-22 stsp
1058 1a76625f 2018-10-22 stsp if (asprintf(&ncommits_str, " [%d/%d]%s ",
1059 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1060 1a76625f 2018-10-22 stsp commits_needed == 0 ? "" : " loading...") == -1)
1061 1a76625f 2018-10-22 stsp return got_error_from_errno();
1062 1a76625f 2018-10-22 stsp
1063 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1064 1a76625f 2018-10-22 stsp if (asprintf(&header, "commit: %s %s%s",
1065 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1066 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1067 1a76625f 2018-10-22 stsp err = got_error_from_errno();
1068 1a76625f 2018-10-22 stsp header = NULL;
1069 1a76625f 2018-10-22 stsp goto done;
1070 1a76625f 2018-10-22 stsp }
1071 1a76625f 2018-10-22 stsp } else if (asprintf(&header, "commit: %s%s",
1072 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1073 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1074 1a76625f 2018-10-22 stsp err = got_error_from_errno();
1075 1a76625f 2018-10-22 stsp header = NULL;
1076 1a76625f 2018-10-22 stsp goto done;
1077 ecb28ae0 2018-07-16 stsp }
1078 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1079 1a76625f 2018-10-22 stsp if (err)
1080 1a76625f 2018-10-22 stsp goto done;
1081 867c6645 2018-07-10 stsp
1082 2814baeb 2018-08-01 stsp werase(view->window);
1083 867c6645 2018-07-10 stsp
1084 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1085 a3404814 2018-09-02 stsp wstandout(view->window);
1086 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1087 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1088 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1089 1a76625f 2018-10-22 stsp width++;
1090 1a76625f 2018-10-22 stsp }
1091 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1092 a3404814 2018-09-02 stsp wstandend(view->window);
1093 ecb28ae0 2018-07-16 stsp free(wline);
1094 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1095 1a76625f 2018-10-22 stsp goto done;
1096 0553a4e3 2018-04-30 stsp
1097 899d86c2 2018-05-10 stsp entry = first;
1098 899d86c2 2018-05-10 stsp *last = first;
1099 867c6645 2018-07-10 stsp ncommits = 0;
1100 899d86c2 2018-05-10 stsp while (entry) {
1101 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1102 899d86c2 2018-05-10 stsp break;
1103 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1104 2814baeb 2018-08-01 stsp wstandout(view->window);
1105 2814baeb 2018-08-01 stsp err = draw_commit(view, entry->commit, entry->id);
1106 0cf4efb1 2018-09-29 stsp if (view->focussed && ncommits == selected_idx)
1107 2814baeb 2018-08-01 stsp wstandend(view->window);
1108 0553a4e3 2018-04-30 stsp if (err)
1109 0553a4e3 2018-04-30 stsp break;
1110 0553a4e3 2018-04-30 stsp ncommits++;
1111 899d86c2 2018-05-10 stsp *last = entry;
1112 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1113 80ddbec8 2018-04-29 stsp }
1114 80ddbec8 2018-04-29 stsp
1115 1a57306a 2018-09-02 stsp view_vborder(view);
1116 1a76625f 2018-10-22 stsp done:
1117 1a76625f 2018-10-22 stsp free(id_str);
1118 1a76625f 2018-10-22 stsp free(ncommits_str);
1119 1a76625f 2018-10-22 stsp free(header);
1120 80ddbec8 2018-04-29 stsp return err;
1121 9f7d7167 2018-04-29 stsp }
1122 07b55e75 2018-05-10 stsp
1123 07b55e75 2018-05-10 stsp static void
1124 16482c3b 2018-05-20 stsp scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1125 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1126 07b55e75 2018-05-10 stsp {
1127 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1128 07b55e75 2018-05-10 stsp int nscrolled = 0;
1129 07b55e75 2018-05-10 stsp
1130 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1131 07b55e75 2018-05-10 stsp if (*first_displayed_entry == entry)
1132 07b55e75 2018-05-10 stsp return;
1133 9f7d7167 2018-04-29 stsp
1134 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1135 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1136 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1137 07b55e75 2018-05-10 stsp if (entry) {
1138 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1139 07b55e75 2018-05-10 stsp nscrolled++;
1140 07b55e75 2018-05-10 stsp }
1141 07b55e75 2018-05-10 stsp }
1142 aa075928 2018-05-10 stsp }
1143 aa075928 2018-05-10 stsp
1144 aa075928 2018-05-10 stsp static const struct got_error *
1145 16482c3b 2018-05-20 stsp scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1146 93e45b7c 2018-09-24 stsp struct commit_queue_entry **last_displayed_entry,
1147 1a76625f 2018-10-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1148 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1149 aa075928 2018-05-10 stsp {
1150 aa075928 2018-05-10 stsp const struct got_error *err = NULL;
1151 dd0a52c1 2018-05-20 stsp struct commit_queue_entry *pentry;
1152 aa075928 2018-05-10 stsp int nscrolled = 0;
1153 aa075928 2018-05-10 stsp
1154 1a76625f 2018-10-22 stsp if (*last_displayed_entry == NULL)
1155 1a76625f 2018-10-22 stsp return NULL;
1156 1a76625f 2018-10-22 stsp
1157 aa075928 2018-05-10 stsp do {
1158 93e45b7c 2018-09-24 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1159 aa075928 2018-05-10 stsp if (pentry == NULL) {
1160 1a76625f 2018-10-22 stsp int errcode;
1161 1a76625f 2018-10-22 stsp if (*log_complete)
1162 1a76625f 2018-10-22 stsp return NULL;
1163 1a76625f 2018-10-22 stsp *commits_needed = maxscroll + 20;
1164 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(need_commits);
1165 1a76625f 2018-10-22 stsp if (errcode)
1166 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
1167 1a76625f 2018-10-22 stsp return NULL;
1168 aa075928 2018-05-10 stsp }
1169 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1170 aa075928 2018-05-10 stsp
1171 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1172 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1173 dd0a52c1 2018-05-20 stsp break;
1174 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1175 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1176 aa075928 2018-05-10 stsp
1177 dd0a52c1 2018-05-20 stsp return err;
1178 07b55e75 2018-05-10 stsp }
1179 4a7f7875 2018-05-10 stsp
1180 cd0acaa7 2018-05-20 stsp static const struct got_error *
1181 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1182 0cf4efb1 2018-09-29 stsp struct got_object_id *commit_id, struct got_commit_object *commit,
1183 2a4718d3 2018-09-29 stsp struct got_repository *repo)
1184 cd0acaa7 2018-05-20 stsp {
1185 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1186 cd0acaa7 2018-05-20 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1187 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1188 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1189 cd0acaa7 2018-05-20 stsp
1190 2a4718d3 2018-09-29 stsp err = got_object_open(&obj2, repo, commit_id);
1191 cd0acaa7 2018-05-20 stsp if (err)
1192 cd0acaa7 2018-05-20 stsp return err;
1193 cd0acaa7 2018-05-20 stsp
1194 2a4718d3 2018-09-29 stsp parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1195 9ba79e04 2018-06-11 stsp if (parent_id) {
1196 9ba79e04 2018-06-11 stsp err = got_object_open(&obj1, repo, parent_id->id);
1197 cd0acaa7 2018-05-20 stsp if (err)
1198 cd0acaa7 2018-05-20 stsp goto done;
1199 cd0acaa7 2018-05-20 stsp }
1200 cd0acaa7 2018-05-20 stsp
1201 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1202 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
1203 ea5e7bb5 2018-08-01 stsp err = got_error_from_errno();
1204 ea5e7bb5 2018-08-01 stsp goto done;
1205 ea5e7bb5 2018-08-01 stsp }
1206 ea5e7bb5 2018-08-01 stsp
1207 e5a0f69f 2018-08-18 stsp err = open_diff_view(diff_view, obj1, obj2, repo);
1208 e5a0f69f 2018-08-18 stsp if (err == NULL)
1209 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1210 cd0acaa7 2018-05-20 stsp done:
1211 cd0acaa7 2018-05-20 stsp if (obj1)
1212 cd0acaa7 2018-05-20 stsp got_object_close(obj1);
1213 cd0acaa7 2018-05-20 stsp if (obj2)
1214 cd0acaa7 2018-05-20 stsp got_object_close(obj2);
1215 cd0acaa7 2018-05-20 stsp return err;
1216 4a7f7875 2018-05-10 stsp }
1217 4a7f7875 2018-05-10 stsp
1218 80ddbec8 2018-04-29 stsp static const struct got_error *
1219 0cf4efb1 2018-09-29 stsp browse_commit(struct tog_view **new_view, int begin_x,
1220 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *entry, struct got_repository *repo)
1221 9343a5fb 2018-06-23 stsp {
1222 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1223 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1224 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1225 9343a5fb 2018-06-23 stsp
1226 9343a5fb 2018-06-23 stsp err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1227 9343a5fb 2018-06-23 stsp if (err)
1228 9343a5fb 2018-06-23 stsp return err;
1229 9343a5fb 2018-06-23 stsp
1230 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1231 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1232 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
1233 e5a0f69f 2018-08-18 stsp
1234 e5a0f69f 2018-08-18 stsp err = open_tree_view(tree_view, tree, entry->id, repo);
1235 ad80ab7b 2018-08-04 stsp if (err)
1236 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1237 e5a0f69f 2018-08-18 stsp else
1238 e5a0f69f 2018-08-18 stsp *new_view = tree_view;
1239 1a76625f 2018-10-22 stsp return err;
1240 1a76625f 2018-10-22 stsp }
1241 1a76625f 2018-10-22 stsp
1242 1a76625f 2018-10-22 stsp static void *
1243 1a76625f 2018-10-22 stsp log_thread(void *arg)
1244 1a76625f 2018-10-22 stsp {
1245 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1246 1a76625f 2018-10-22 stsp int errcode = 0;
1247 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1248 1a76625f 2018-10-22 stsp int done = 0;
1249 1a76625f 2018-10-22 stsp
1250 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1251 1a76625f 2018-10-22 stsp if (err)
1252 1a76625f 2018-10-22 stsp return (void *)err;
1253 1a76625f 2018-10-22 stsp
1254 1a76625f 2018-10-22 stsp while (!done && !err) {
1255 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1256 1a76625f 2018-10-22 stsp a->in_repo_path);
1257 1a76625f 2018-10-22 stsp if (err) {
1258 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1259 1a76625f 2018-10-22 stsp return (void *)err;
1260 1a76625f 2018-10-22 stsp err = NULL;
1261 1a76625f 2018-10-22 stsp done = 1;
1262 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1263 1a76625f 2018-10-22 stsp a->commits_needed--;
1264 1a76625f 2018-10-22 stsp
1265 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1266 1a76625f 2018-10-22 stsp if (errcode)
1267 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
1268 1a76625f 2018-10-22 stsp
1269 1a76625f 2018-10-22 stsp if (done)
1270 1a76625f 2018-10-22 stsp a->log_complete = 1;
1271 1a76625f 2018-10-22 stsp else if (*a->quit) {
1272 1a76625f 2018-10-22 stsp done = 1;
1273 1a76625f 2018-10-22 stsp a->log_complete = 1;
1274 1a76625f 2018-10-22 stsp } else if (*a->first_displayed_entry == NULL) {
1275 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1276 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1277 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1278 1a76625f 2018-10-22 stsp }
1279 1a76625f 2018-10-22 stsp
1280 1a76625f 2018-10-22 stsp err = draw_commits(a->view, a->last_displayed_entry,
1281 1a76625f 2018-10-22 stsp a->selected_entry, *a->first_displayed_entry,
1282 1a76625f 2018-10-22 stsp a->commits, *a->selected, a->view->nlines,
1283 1a76625f 2018-10-22 stsp a->in_repo_path, a->commits_needed);
1284 510e8b0a 2018-10-22 stsp #if 0 /* XXX ncurses isn't thread-safe */
1285 510e8b0a 2018-10-22 stsp if (!err) {
1286 1a76625f 2018-10-22 stsp update_panels();
1287 1a76625f 2018-10-22 stsp doupdate();
1288 510e8b0a 2018-10-22 stsp }
1289 510e8b0a 2018-10-22 stsp #endif
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 510e8b0a 2018-10-22 stsp #if 0 /* XXX ncurses isn't thread-safe */
2224 d6144aad 2018-10-22 stsp if (!err) {
2225 d6144aad 2018-10-22 stsp update_panels();
2226 d6144aad 2018-10-22 stsp doupdate();
2227 d6144aad 2018-10-22 stsp }
2228 510e8b0a 2018-10-22 stsp #endif
2229 84451b3e 2018-07-10 stsp done:
2230 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2231 1a76625f 2018-10-22 stsp if (errcode)
2232 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2233 84451b3e 2018-07-10 stsp return err;
2234 84451b3e 2018-07-10 stsp }
2235 84451b3e 2018-07-10 stsp
2236 84451b3e 2018-07-10 stsp static void *
2237 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2238 84451b3e 2018-07-10 stsp {
2239 18430de3 2018-07-10 stsp const struct got_error *err;
2240 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
2241 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
2242 1a76625f 2018-10-22 stsp int errcode;
2243 18430de3 2018-07-10 stsp
2244 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2245 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
2246 18430de3 2018-07-10 stsp
2247 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2248 1a76625f 2018-10-22 stsp if (errcode)
2249 1a76625f 2018-10-22 stsp return (void *)got_error_set_errno(errcode);
2250 18430de3 2018-07-10 stsp
2251 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
2252 c9beca56 2018-07-22 stsp ta->repo = NULL;
2253 c9beca56 2018-07-22 stsp *ta->complete = 1;
2254 1a76625f 2018-10-22 stsp if (!err) {
2255 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
2256 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
2257 e5a0f69f 2018-08-18 stsp a->first_displayed_line, a->last_displayed_line, a->eof,
2258 f7d12f7e 2018-08-01 stsp a->view->nlines);
2259 510e8b0a 2018-10-22 stsp #if 0 /* XXX ncurses isn't thread-safe */
2260 1a76625f 2018-10-22 stsp if (!err) {
2261 1a76625f 2018-10-22 stsp update_panels();
2262 1a76625f 2018-10-22 stsp doupdate();
2263 1a76625f 2018-10-22 stsp }
2264 510e8b0a 2018-10-22 stsp #endif
2265 1a76625f 2018-10-22 stsp }
2266 18430de3 2018-07-10 stsp
2267 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2268 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
2269 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2270 18430de3 2018-07-10 stsp
2271 18430de3 2018-07-10 stsp return (void *)err;
2272 84451b3e 2018-07-10 stsp }
2273 84451b3e 2018-07-10 stsp
2274 245d91c1 2018-07-12 stsp static struct got_object_id *
2275 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
2276 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
2277 245d91c1 2018-07-12 stsp {
2278 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2279 b880a918 2018-07-10 stsp
2280 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2281 245d91c1 2018-07-12 stsp if (!line->annotated)
2282 245d91c1 2018-07-12 stsp return NULL;
2283 245d91c1 2018-07-12 stsp
2284 245d91c1 2018-07-12 stsp return line->id;
2285 245d91c1 2018-07-12 stsp }
2286 245d91c1 2018-07-12 stsp
2287 84451b3e 2018-07-10 stsp static const struct got_error *
2288 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
2289 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
2290 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
2291 b880a918 2018-07-10 stsp {
2292 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
2293 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
2294 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
2295 b880a918 2018-07-10 stsp struct got_object_qid *pid;
2296 b880a918 2018-07-10 stsp
2297 b880a918 2018-07-10 stsp *pobj = NULL;
2298 b880a918 2018-07-10 stsp *obj = NULL;
2299 b880a918 2018-07-10 stsp
2300 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
2301 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
2302 245d91c1 2018-07-12 stsp if (selected_id == NULL)
2303 b880a918 2018-07-10 stsp return NULL;
2304 b880a918 2018-07-10 stsp
2305 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
2306 b880a918 2018-07-10 stsp if (err)
2307 b880a918 2018-07-10 stsp goto done;
2308 b880a918 2018-07-10 stsp
2309 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
2310 b880a918 2018-07-10 stsp if (err)
2311 b880a918 2018-07-10 stsp goto done;
2312 b880a918 2018-07-10 stsp
2313 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
2314 b880a918 2018-07-10 stsp if (pid) {
2315 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
2316 b880a918 2018-07-10 stsp if (err)
2317 b880a918 2018-07-10 stsp goto done;
2318 b880a918 2018-07-10 stsp }
2319 b880a918 2018-07-10 stsp done:
2320 b880a918 2018-07-10 stsp if (commit)
2321 b880a918 2018-07-10 stsp got_object_commit_close(commit);
2322 b880a918 2018-07-10 stsp return err;
2323 b880a918 2018-07-10 stsp }
2324 245d91c1 2018-07-12 stsp
2325 b880a918 2018-07-10 stsp static const struct got_error *
2326 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2327 a70480e0 2018-06-23 stsp {
2328 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2329 245d91c1 2018-07-12 stsp int i;
2330 245d91c1 2018-07-12 stsp
2331 245d91c1 2018-07-12 stsp if (blame->thread) {
2332 1a76625f 2018-10-22 stsp int errcode;
2333 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
2334 1a76625f 2018-10-22 stsp if (errcode)
2335 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2336 1a76625f 2018-10-22 stsp errcode = pthread_join(blame->thread, (void **)&err);
2337 1a76625f 2018-10-22 stsp if (errcode)
2338 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2339 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
2340 1a76625f 2018-10-22 stsp if (errcode)
2341 1a76625f 2018-10-22 stsp return got_error_set_errno(errcode);
2342 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2343 245d91c1 2018-07-12 stsp err = NULL;
2344 245d91c1 2018-07-12 stsp blame->thread = NULL;
2345 245d91c1 2018-07-12 stsp }
2346 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2347 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2348 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2349 245d91c1 2018-07-12 stsp }
2350 245d91c1 2018-07-12 stsp if (blame->f) {
2351 245d91c1 2018-07-12 stsp fclose(blame->f);
2352 245d91c1 2018-07-12 stsp blame->f = NULL;
2353 245d91c1 2018-07-12 stsp }
2354 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
2355 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
2356 245d91c1 2018-07-12 stsp free(blame->lines);
2357 245d91c1 2018-07-12 stsp blame->lines = NULL;
2358 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2359 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2360 245d91c1 2018-07-12 stsp
2361 245d91c1 2018-07-12 stsp return err;
2362 245d91c1 2018-07-12 stsp }
2363 245d91c1 2018-07-12 stsp
2364 245d91c1 2018-07-12 stsp static const struct got_error *
2365 1a76625f 2018-10-22 stsp run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2366 1a76625f 2018-10-22 stsp int *first_displayed_line, int *last_displayed_line, int *selected_line,
2367 1a76625f 2018-10-22 stsp int *done, int *eof, const char *path, struct got_object_id *commit_id,
2368 245d91c1 2018-07-12 stsp struct got_repository *repo)
2369 245d91c1 2018-07-12 stsp {
2370 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2371 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2372 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2373 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2374 27d434c2 2018-09-15 stsp struct got_object *obj = NULL;
2375 1a76625f 2018-10-22 stsp int errcode;
2376 a70480e0 2018-06-23 stsp
2377 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2378 27d434c2 2018-09-15 stsp if (err)
2379 27d434c2 2018-09-15 stsp goto done;
2380 27d434c2 2018-09-15 stsp
2381 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
2382 84451b3e 2018-07-10 stsp if (err)
2383 84451b3e 2018-07-10 stsp goto done;
2384 27d434c2 2018-09-15 stsp
2385 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2386 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2387 84451b3e 2018-07-10 stsp goto done;
2388 84451b3e 2018-07-10 stsp }
2389 a70480e0 2018-06-23 stsp
2390 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
2391 a70480e0 2018-06-23 stsp if (err)
2392 a70480e0 2018-06-23 stsp goto done;
2393 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2394 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2395 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2396 84451b3e 2018-07-10 stsp goto done;
2397 84451b3e 2018-07-10 stsp }
2398 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2399 245d91c1 2018-07-12 stsp blame->f, blob);
2400 84451b3e 2018-07-10 stsp if (err)
2401 84451b3e 2018-07-10 stsp goto done;
2402 a70480e0 2018-06-23 stsp
2403 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2404 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2405 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2406 84451b3e 2018-07-10 stsp goto done;
2407 84451b3e 2018-07-10 stsp }
2408 a70480e0 2018-06-23 stsp
2409 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2410 bd24772e 2018-07-11 stsp if (err)
2411 bd24772e 2018-07-11 stsp goto done;
2412 bd24772e 2018-07-11 stsp
2413 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2414 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2415 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2416 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2417 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2418 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2419 245d91c1 2018-07-12 stsp goto done;
2420 245d91c1 2018-07-12 stsp }
2421 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
2422 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
2423 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
2424 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
2425 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
2426 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2427 e5a0f69f 2018-08-18 stsp blame->cb_args.eof = eof;
2428 245d91c1 2018-07-12 stsp
2429 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2430 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2431 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2432 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2433 245d91c1 2018-07-12 stsp *blame_complete = 0;
2434 245d91c1 2018-07-12 stsp
2435 1a76625f 2018-10-22 stsp errcode = pthread_create(&blame->thread, NULL, blame_thread,
2436 1a76625f 2018-10-22 stsp &blame->thread_args);
2437 1a76625f 2018-10-22 stsp if (errcode) {
2438 1a76625f 2018-10-22 stsp err = got_error_set_errno(errcode);
2439 245d91c1 2018-07-12 stsp goto done;
2440 245d91c1 2018-07-12 stsp }
2441 245d91c1 2018-07-12 stsp
2442 245d91c1 2018-07-12 stsp done:
2443 245d91c1 2018-07-12 stsp if (blob)
2444 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2445 27d434c2 2018-09-15 stsp free(obj_id);
2446 245d91c1 2018-07-12 stsp if (obj)
2447 245d91c1 2018-07-12 stsp got_object_close(obj);
2448 245d91c1 2018-07-12 stsp if (err)
2449 245d91c1 2018-07-12 stsp stop_blame(blame);
2450 245d91c1 2018-07-12 stsp return err;
2451 245d91c1 2018-07-12 stsp }
2452 245d91c1 2018-07-12 stsp
2453 245d91c1 2018-07-12 stsp static const struct got_error *
2454 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2455 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2456 245d91c1 2018-07-12 stsp {
2457 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2458 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2459 dbc6a6b6 2018-07-12 stsp
2460 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2461 245d91c1 2018-07-12 stsp
2462 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2463 dbc6a6b6 2018-07-12 stsp if (err)
2464 7cbe629d 2018-08-04 stsp return err;
2465 245d91c1 2018-07-12 stsp
2466 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2467 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2468 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2469 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2470 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2471 fb2756b9 2018-08-04 stsp s->path = path;
2472 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2473 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2474 fb2756b9 2018-08-04 stsp s->repo = repo;
2475 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2476 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2477 7cbe629d 2018-08-04 stsp
2478 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2479 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2480 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2481 e5a0f69f 2018-08-18 stsp
2482 1a76625f 2018-10-22 stsp return run_blame(&s->blame, view, &s->blame_complete,
2483 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2484 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2485 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2486 7cbe629d 2018-08-04 stsp }
2487 7cbe629d 2018-08-04 stsp
2488 e5a0f69f 2018-08-18 stsp static const struct got_error *
2489 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2490 7cbe629d 2018-08-04 stsp {
2491 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2492 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2493 7cbe629d 2018-08-04 stsp
2494 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2495 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2496 e5a0f69f 2018-08-18 stsp
2497 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2498 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2499 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2500 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2501 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2502 7cbe629d 2018-08-04 stsp }
2503 e5a0f69f 2018-08-18 stsp
2504 e5a0f69f 2018-08-18 stsp free(s->path);
2505 e5a0f69f 2018-08-18 stsp
2506 e5a0f69f 2018-08-18 stsp return err;
2507 7cbe629d 2018-08-04 stsp }
2508 7cbe629d 2018-08-04 stsp
2509 7cbe629d 2018-08-04 stsp static const struct got_error *
2510 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2511 7cbe629d 2018-08-04 stsp {
2512 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2513 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2514 e5a0f69f 2018-08-18 stsp
2515 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2516 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2517 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2518 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2519 e5a0f69f 2018-08-18 stsp
2520 669b5ffa 2018-10-07 stsp view_vborder(view);
2521 e5a0f69f 2018-08-18 stsp return err;
2522 e5a0f69f 2018-08-18 stsp }
2523 e5a0f69f 2018-08-18 stsp
2524 e5a0f69f 2018-08-18 stsp static const struct got_error *
2525 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2526 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2527 e5a0f69f 2018-08-18 stsp {
2528 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2529 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
2530 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2531 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2532 669b5ffa 2018-10-07 stsp int begin_x = 0;
2533 7cbe629d 2018-08-04 stsp
2534 e5a0f69f 2018-08-18 stsp switch (ch) {
2535 e5a0f69f 2018-08-18 stsp case 'q':
2536 e5a0f69f 2018-08-18 stsp s->done = 1;
2537 1a76625f 2018-10-22 stsp break;
2538 e5a0f69f 2018-08-18 stsp case 'k':
2539 e5a0f69f 2018-08-18 stsp case KEY_UP:
2540 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2541 e5a0f69f 2018-08-18 stsp s->selected_line--;
2542 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2543 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2544 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2545 a70480e0 2018-06-23 stsp break;
2546 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2547 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2548 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2549 a70480e0 2018-06-23 stsp break;
2550 e5a0f69f 2018-08-18 stsp }
2551 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2552 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2553 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2554 e5a0f69f 2018-08-18 stsp else
2555 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2556 e5a0f69f 2018-08-18 stsp break;
2557 e5a0f69f 2018-08-18 stsp case 'j':
2558 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2559 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2560 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2561 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2562 e5a0f69f 2018-08-18 stsp s->selected_line++;
2563 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2564 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2565 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2566 e5a0f69f 2018-08-18 stsp break;
2567 e5a0f69f 2018-08-18 stsp case 'b':
2568 e5a0f69f 2018-08-18 stsp case 'p': {
2569 e5a0f69f 2018-08-18 stsp struct got_object_id *id;
2570 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2571 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2572 e5a0f69f 2018-08-18 stsp if (id == NULL || got_object_id_cmp(id,
2573 e5a0f69f 2018-08-18 stsp s->blamed_commit->id) == 0)
2574 a70480e0 2018-06-23 stsp break;
2575 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2576 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2577 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2578 e5a0f69f 2018-08-18 stsp if (err)
2579 a70480e0 2018-06-23 stsp break;
2580 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2581 b700b5d6 2018-07-10 stsp break;
2582 e5a0f69f 2018-08-18 stsp if (ch == 'p' && pobj == NULL)
2583 dbc6a6b6 2018-07-12 stsp break;
2584 e5a0f69f 2018-08-18 stsp s->done = 1;
2585 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2586 e5a0f69f 2018-08-18 stsp s->done = 0;
2587 e5a0f69f 2018-08-18 stsp if (thread_err)
2588 245d91c1 2018-07-12 stsp break;
2589 e5a0f69f 2018-08-18 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
2590 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2591 e5a0f69f 2018-08-18 stsp obj = NULL;
2592 e5a0f69f 2018-08-18 stsp if (pobj) {
2593 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2594 e5a0f69f 2018-08-18 stsp pobj = NULL;
2595 e5a0f69f 2018-08-18 stsp }
2596 6402fb3c 2018-09-15 stsp err = got_object_qid_alloc(&s->blamed_commit, id);
2597 e5a0f69f 2018-08-18 stsp if (err)
2598 e5a0f69f 2018-08-18 stsp goto done;
2599 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2600 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2601 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2602 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2603 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2604 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2605 e5a0f69f 2018-08-18 stsp if (err)
2606 a70480e0 2018-06-23 stsp break;
2607 e5a0f69f 2018-08-18 stsp break;
2608 84451b3e 2018-07-10 stsp }
2609 e5a0f69f 2018-08-18 stsp case 'B': {
2610 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2611 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2612 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2613 e5a0f69f 2018-08-18 stsp break;
2614 e5a0f69f 2018-08-18 stsp s->done = 1;
2615 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2616 e5a0f69f 2018-08-18 stsp s->done = 0;
2617 e5a0f69f 2018-08-18 stsp if (thread_err)
2618 e5a0f69f 2018-08-18 stsp break;
2619 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2620 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2621 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2622 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2623 1a76625f 2018-10-22 stsp err = run_blame(&s->blame, view, &s->blame_complete,
2624 1a76625f 2018-10-22 stsp &s->first_displayed_line, &s->last_displayed_line,
2625 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2626 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2627 e5a0f69f 2018-08-18 stsp if (err)
2628 e5a0f69f 2018-08-18 stsp break;
2629 245d91c1 2018-07-12 stsp break;
2630 e5a0f69f 2018-08-18 stsp }
2631 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2632 e5a0f69f 2018-08-18 stsp case '\r':
2633 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2634 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2635 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2636 e5a0f69f 2018-08-18 stsp if (err)
2637 e5a0f69f 2018-08-18 stsp break;
2638 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2639 e5a0f69f 2018-08-18 stsp break;
2640 669b5ffa 2018-10-07 stsp
2641 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
2642 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
2643 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2644 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2645 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2646 e5a0f69f 2018-08-18 stsp break;
2647 e5a0f69f 2018-08-18 stsp }
2648 669b5ffa 2018-10-07 stsp err = open_diff_view(diff_view, pobj, obj, s->repo);
2649 e5a0f69f 2018-08-18 stsp if (err) {
2650 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2651 e5a0f69f 2018-08-18 stsp break;
2652 e5a0f69f 2018-08-18 stsp }
2653 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
2654 669b5ffa 2018-10-07 stsp err = view_close_child(view);
2655 669b5ffa 2018-10-07 stsp if (err)
2656 669b5ffa 2018-10-07 stsp return err;
2657 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
2658 669b5ffa 2018-10-07 stsp if (err) {
2659 669b5ffa 2018-10-07 stsp view_close(diff_view);
2660 669b5ffa 2018-10-07 stsp break;
2661 669b5ffa 2018-10-07 stsp }
2662 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(diff_view)) {
2663 669b5ffa 2018-10-07 stsp *focus_view = diff_view;
2664 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
2665 669b5ffa 2018-10-07 stsp }
2666 669b5ffa 2018-10-07 stsp } else
2667 669b5ffa 2018-10-07 stsp *new_view = diff_view;
2668 e5a0f69f 2018-08-18 stsp if (pobj) {
2669 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2670 e5a0f69f 2018-08-18 stsp pobj = NULL;
2671 e5a0f69f 2018-08-18 stsp }
2672 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2673 e5a0f69f 2018-08-18 stsp obj = NULL;
2674 e5a0f69f 2018-08-18 stsp if (err)
2675 e5a0f69f 2018-08-18 stsp break;
2676 e5a0f69f 2018-08-18 stsp break;
2677 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2678 e5a0f69f 2018-08-18 stsp case ' ':
2679 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2680 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2681 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2682 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2683 e5a0f69f 2018-08-18 stsp break;
2684 e5a0f69f 2018-08-18 stsp }
2685 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2686 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2687 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2688 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2689 e5a0f69f 2018-08-18 stsp else
2690 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2691 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2692 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2693 e5a0f69f 2018-08-18 stsp break;
2694 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2695 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2696 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2697 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2698 e5a0f69f 2018-08-18 stsp }
2699 e5a0f69f 2018-08-18 stsp break;
2700 e5a0f69f 2018-08-18 stsp default:
2701 e5a0f69f 2018-08-18 stsp break;
2702 a70480e0 2018-06-23 stsp }
2703 a70480e0 2018-06-23 stsp done:
2704 b880a918 2018-07-10 stsp if (pobj)
2705 b880a918 2018-07-10 stsp got_object_close(pobj);
2706 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2707 a70480e0 2018-06-23 stsp }
2708 a70480e0 2018-06-23 stsp
2709 a70480e0 2018-06-23 stsp static const struct got_error *
2710 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2711 9f7d7167 2018-04-29 stsp {
2712 a70480e0 2018-06-23 stsp const struct got_error *error;
2713 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2714 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2715 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2716 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2717 a70480e0 2018-06-23 stsp int ch;
2718 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2719 a70480e0 2018-06-23 stsp
2720 a70480e0 2018-06-23 stsp #ifndef PROFILE
2721 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2722 ad242220 2018-09-08 stsp == -1)
2723 a70480e0 2018-06-23 stsp err(1, "pledge");
2724 a70480e0 2018-06-23 stsp #endif
2725 a70480e0 2018-06-23 stsp
2726 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2727 a70480e0 2018-06-23 stsp switch (ch) {
2728 a70480e0 2018-06-23 stsp case 'c':
2729 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2730 a70480e0 2018-06-23 stsp break;
2731 69069811 2018-08-02 stsp case 'r':
2732 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2733 69069811 2018-08-02 stsp if (repo_path == NULL)
2734 69069811 2018-08-02 stsp err(1, "-r option");
2735 69069811 2018-08-02 stsp break;
2736 a70480e0 2018-06-23 stsp default:
2737 a70480e0 2018-06-23 stsp usage();
2738 a70480e0 2018-06-23 stsp /* NOTREACHED */
2739 a70480e0 2018-06-23 stsp }
2740 a70480e0 2018-06-23 stsp }
2741 a70480e0 2018-06-23 stsp
2742 a70480e0 2018-06-23 stsp argc -= optind;
2743 a70480e0 2018-06-23 stsp argv += optind;
2744 a70480e0 2018-06-23 stsp
2745 69069811 2018-08-02 stsp if (argc == 1)
2746 69069811 2018-08-02 stsp path = argv[0];
2747 69069811 2018-08-02 stsp else
2748 a70480e0 2018-06-23 stsp usage_blame();
2749 69069811 2018-08-02 stsp
2750 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2751 69069811 2018-08-02 stsp if (cwd == NULL) {
2752 69069811 2018-08-02 stsp error = got_error_from_errno();
2753 69069811 2018-08-02 stsp goto done;
2754 69069811 2018-08-02 stsp }
2755 69069811 2018-08-02 stsp if (repo_path == NULL) {
2756 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2757 69069811 2018-08-02 stsp if (repo_path == NULL) {
2758 69069811 2018-08-02 stsp error = got_error_from_errno();
2759 69069811 2018-08-02 stsp goto done;
2760 69069811 2018-08-02 stsp }
2761 69069811 2018-08-02 stsp }
2762 69069811 2018-08-02 stsp
2763 69069811 2018-08-02 stsp
2764 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2765 a70480e0 2018-06-23 stsp if (error != NULL)
2766 66b4983c 2018-06-23 stsp return error;
2767 69069811 2018-08-02 stsp
2768 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2769 69069811 2018-08-02 stsp if (error != NULL)
2770 69069811 2018-08-02 stsp goto done;
2771 a70480e0 2018-06-23 stsp
2772 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2773 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2774 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2775 a70480e0 2018-06-23 stsp if (error != NULL)
2776 66b4983c 2018-06-23 stsp goto done;
2777 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2778 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2779 a70480e0 2018-06-23 stsp } else {
2780 a70480e0 2018-06-23 stsp struct got_object *obj;
2781 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2782 a70480e0 2018-06-23 stsp if (error != NULL)
2783 66b4983c 2018-06-23 stsp goto done;
2784 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
2785 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2786 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2787 a19e88aa 2018-06-23 stsp got_object_close(obj);
2788 a70480e0 2018-06-23 stsp }
2789 a19e88aa 2018-06-23 stsp if (error != NULL)
2790 a19e88aa 2018-06-23 stsp goto done;
2791 a70480e0 2018-06-23 stsp
2792 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2793 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2794 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2795 e1cd8fed 2018-08-01 stsp goto done;
2796 e1cd8fed 2018-08-01 stsp }
2797 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2798 7cbe629d 2018-08-04 stsp if (error)
2799 7cbe629d 2018-08-04 stsp goto done;
2800 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2801 a70480e0 2018-06-23 stsp done:
2802 69069811 2018-08-02 stsp free(repo_path);
2803 69069811 2018-08-02 stsp free(cwd);
2804 a70480e0 2018-06-23 stsp free(commit_id);
2805 a70480e0 2018-06-23 stsp if (repo)
2806 a70480e0 2018-06-23 stsp got_repo_close(repo);
2807 a70480e0 2018-06-23 stsp return error;
2808 ffd1d5e5 2018-06-23 stsp }
2809 ffd1d5e5 2018-06-23 stsp
2810 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2811 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2812 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2813 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2814 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2815 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2816 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2817 ffd1d5e5 2018-06-23 stsp {
2818 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2819 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2820 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2821 ffd1d5e5 2018-06-23 stsp int width, n;
2822 ffd1d5e5 2018-06-23 stsp
2823 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2824 ffd1d5e5 2018-06-23 stsp
2825 f7d12f7e 2018-08-01 stsp werase(view->window);
2826 ffd1d5e5 2018-06-23 stsp
2827 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2828 ffd1d5e5 2018-06-23 stsp return NULL;
2829 ffd1d5e5 2018-06-23 stsp
2830 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2831 ffd1d5e5 2018-06-23 stsp if (err)
2832 ffd1d5e5 2018-06-23 stsp return err;
2833 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2834 a3404814 2018-09-02 stsp wstandout(view->window);
2835 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2836 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2837 a3404814 2018-09-02 stsp wstandend(view->window);
2838 2550e4c3 2018-07-13 stsp free(wline);
2839 2550e4c3 2018-07-13 stsp wline = NULL;
2840 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2841 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2842 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2843 ffd1d5e5 2018-06-23 stsp return NULL;
2844 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2845 ce52c690 2018-06-23 stsp if (err)
2846 ce52c690 2018-06-23 stsp return err;
2847 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2848 2550e4c3 2018-07-13 stsp free(wline);
2849 2550e4c3 2018-07-13 stsp wline = NULL;
2850 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2851 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2852 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2853 ffd1d5e5 2018-06-23 stsp return NULL;
2854 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2855 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2856 a1eca9bb 2018-06-23 stsp return NULL;
2857 ffd1d5e5 2018-06-23 stsp
2858 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2859 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2860 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2861 0cf4efb1 2018-09-29 stsp if (view->focussed)
2862 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2863 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2864 ffd1d5e5 2018-06-23 stsp }
2865 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2866 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
2867 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2868 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2869 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2870 ffd1d5e5 2018-06-23 stsp return NULL;
2871 ffd1d5e5 2018-06-23 stsp n = 1;
2872 ffd1d5e5 2018-06-23 stsp } else {
2873 ffd1d5e5 2018-06-23 stsp n = 0;
2874 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2875 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2876 ffd1d5e5 2018-06-23 stsp }
2877 ffd1d5e5 2018-06-23 stsp
2878 ffd1d5e5 2018-06-23 stsp while (te) {
2879 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2880 1d13200f 2018-07-12 stsp
2881 1d13200f 2018-07-12 stsp if (show_ids) {
2882 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2883 1d13200f 2018-07-12 stsp if (err)
2884 1d13200f 2018-07-12 stsp return got_error_from_errno();
2885 1d13200f 2018-07-12 stsp }
2886 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2887 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2888 1d13200f 2018-07-12 stsp free(id_str);
2889 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2890 1d13200f 2018-07-12 stsp }
2891 1d13200f 2018-07-12 stsp free(id_str);
2892 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2893 ffd1d5e5 2018-06-23 stsp if (err) {
2894 ffd1d5e5 2018-06-23 stsp free(line);
2895 ffd1d5e5 2018-06-23 stsp break;
2896 ffd1d5e5 2018-06-23 stsp }
2897 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2898 0cf4efb1 2018-09-29 stsp if (view->focussed)
2899 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2900 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2901 ffd1d5e5 2018-06-23 stsp }
2902 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2903 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2904 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2905 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
2906 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2907 ffd1d5e5 2018-06-23 stsp free(line);
2908 2550e4c3 2018-07-13 stsp free(wline);
2909 2550e4c3 2018-07-13 stsp wline = NULL;
2910 ffd1d5e5 2018-06-23 stsp n++;
2911 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2912 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2913 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2914 ffd1d5e5 2018-06-23 stsp break;
2915 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2916 ffd1d5e5 2018-06-23 stsp }
2917 ffd1d5e5 2018-06-23 stsp
2918 ffd1d5e5 2018-06-23 stsp return err;
2919 ffd1d5e5 2018-06-23 stsp }
2920 ffd1d5e5 2018-06-23 stsp
2921 ffd1d5e5 2018-06-23 stsp static void
2922 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2923 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2924 ffd1d5e5 2018-06-23 stsp {
2925 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2926 ffd1d5e5 2018-06-23 stsp int i;
2927 ffd1d5e5 2018-06-23 stsp
2928 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2929 ffd1d5e5 2018-06-23 stsp return;
2930 ffd1d5e5 2018-06-23 stsp
2931 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2932 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2933 ffd1d5e5 2018-06-23 stsp if (!isroot)
2934 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2935 ffd1d5e5 2018-06-23 stsp return;
2936 ffd1d5e5 2018-06-23 stsp }
2937 ffd1d5e5 2018-06-23 stsp
2938 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2939 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2940 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2941 ffd1d5e5 2018-06-23 stsp prev = te;
2942 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2943 ffd1d5e5 2018-06-23 stsp }
2944 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2945 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2946 ffd1d5e5 2018-06-23 stsp }
2947 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2948 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2949 ffd1d5e5 2018-06-23 stsp }
2950 ffd1d5e5 2018-06-23 stsp
2951 ffd1d5e5 2018-06-23 stsp static void
2952 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2953 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2954 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2955 ffd1d5e5 2018-06-23 stsp {
2956 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2957 ffd1d5e5 2018-06-23 stsp int n = 0;
2958 ffd1d5e5 2018-06-23 stsp
2959 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2960 ffd1d5e5 2018-06-23 stsp return;
2961 ffd1d5e5 2018-06-23 stsp
2962 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2963 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2964 ffd1d5e5 2018-06-23 stsp else
2965 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2966 ffd1d5e5 2018-06-23 stsp while (next) {
2967 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2968 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2969 ffd1d5e5 2018-06-23 stsp break;
2970 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2971 ffd1d5e5 2018-06-23 stsp }
2972 ffd1d5e5 2018-06-23 stsp }
2973 ffd1d5e5 2018-06-23 stsp
2974 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2975 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2976 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2977 ffd1d5e5 2018-06-23 stsp {
2978 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2979 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2980 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2981 ffd1d5e5 2018-06-23 stsp
2982 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2983 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2984 ce52c690 2018-06-23 stsp if (te)
2985 ce52c690 2018-06-23 stsp len += strlen(te->name);
2986 ce52c690 2018-06-23 stsp
2987 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2988 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2989 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2990 ffd1d5e5 2018-06-23 stsp
2991 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2992 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2993 d9765a41 2018-06-23 stsp while (pt) {
2994 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2995 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2996 cb2ebc8a 2018-06-23 stsp goto done;
2997 cb2ebc8a 2018-06-23 stsp }
2998 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2999 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3000 cb2ebc8a 2018-06-23 stsp goto done;
3001 cb2ebc8a 2018-06-23 stsp }
3002 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3003 ffd1d5e5 2018-06-23 stsp }
3004 ce52c690 2018-06-23 stsp if (te) {
3005 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
3006 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
3007 ce52c690 2018-06-23 stsp goto done;
3008 ce52c690 2018-06-23 stsp }
3009 cb2ebc8a 2018-06-23 stsp }
3010 ce52c690 2018-06-23 stsp done:
3011 ce52c690 2018-06-23 stsp if (err) {
3012 ce52c690 2018-06-23 stsp free(*path);
3013 ce52c690 2018-06-23 stsp *path = NULL;
3014 ce52c690 2018-06-23 stsp }
3015 ce52c690 2018-06-23 stsp return err;
3016 ce52c690 2018-06-23 stsp }
3017 ce52c690 2018-06-23 stsp
3018 ce52c690 2018-06-23 stsp static const struct got_error *
3019 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
3020 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3021 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
3022 ce52c690 2018-06-23 stsp {
3023 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
3024 ce52c690 2018-06-23 stsp char *path;
3025 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
3026 69efd4c4 2018-07-18 stsp
3027 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
3028 ce52c690 2018-06-23 stsp if (err)
3029 ce52c690 2018-06-23 stsp return err;
3030 ffd1d5e5 2018-06-23 stsp
3031 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3032 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
3033 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3034 cdf1ee82 2018-08-01 stsp
3035 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
3036 e5a0f69f 2018-08-18 stsp if (err) {
3037 e5a0f69f 2018-08-18 stsp view_close(blame_view);
3038 e5a0f69f 2018-08-18 stsp free(path);
3039 e5a0f69f 2018-08-18 stsp } else
3040 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
3041 69efd4c4 2018-07-18 stsp return err;
3042 69efd4c4 2018-07-18 stsp }
3043 69efd4c4 2018-07-18 stsp
3044 69efd4c4 2018-07-18 stsp static const struct got_error *
3045 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
3046 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
3047 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
3048 69efd4c4 2018-07-18 stsp {
3049 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
3050 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
3051 69efd4c4 2018-07-18 stsp char *path;
3052 69efd4c4 2018-07-18 stsp
3053 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3054 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
3055 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
3056 e5a0f69f 2018-08-18 stsp
3057 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
3058 69efd4c4 2018-07-18 stsp if (err)
3059 69efd4c4 2018-07-18 stsp return err;
3060 69efd4c4 2018-07-18 stsp
3061 e5a0f69f 2018-08-18 stsp err = open_log_view(log_view, commit_id, repo, path);
3062 ba4f502b 2018-08-04 stsp if (err)
3063 e5a0f69f 2018-08-18 stsp view_close(log_view);
3064 e5a0f69f 2018-08-18 stsp else
3065 e5a0f69f 2018-08-18 stsp *new_view = log_view;
3066 cb2ebc8a 2018-06-23 stsp free(path);
3067 cb2ebc8a 2018-06-23 stsp return err;
3068 ffd1d5e5 2018-06-23 stsp }
3069 ffd1d5e5 2018-06-23 stsp
3070 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3071 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
3072 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
3073 ffd1d5e5 2018-06-23 stsp {
3074 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
3075 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
3076 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3077 ffd1d5e5 2018-06-23 stsp
3078 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
3079 ffd1d5e5 2018-06-23 stsp
3080 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
3081 ffd1d5e5 2018-06-23 stsp if (err != NULL)
3082 ffd1d5e5 2018-06-23 stsp goto done;
3083 ffd1d5e5 2018-06-23 stsp
3084 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3085 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
3086 ffd1d5e5 2018-06-23 stsp goto done;
3087 ffd1d5e5 2018-06-23 stsp }
3088 ffd1d5e5 2018-06-23 stsp
3089 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
3090 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
3091 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3092 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
3093 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
3094 6484ec90 2018-09-29 stsp err = got_error_from_errno();
3095 6484ec90 2018-09-29 stsp goto done;
3096 6484ec90 2018-09-29 stsp }
3097 fb2756b9 2018-08-04 stsp s->repo = repo;
3098 e5a0f69f 2018-08-18 stsp
3099 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
3100 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
3101 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
3102 ad80ab7b 2018-08-04 stsp done:
3103 ad80ab7b 2018-08-04 stsp free(commit_id_str);
3104 6484ec90 2018-09-29 stsp if (err) {
3105 fb2756b9 2018-08-04 stsp free(s->tree_label);
3106 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3107 6484ec90 2018-09-29 stsp }
3108 ad80ab7b 2018-08-04 stsp return err;
3109 ad80ab7b 2018-08-04 stsp }
3110 ad80ab7b 2018-08-04 stsp
3111 e5a0f69f 2018-08-18 stsp static const struct got_error *
3112 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
3113 ad80ab7b 2018-08-04 stsp {
3114 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3115 ad80ab7b 2018-08-04 stsp
3116 fb2756b9 2018-08-04 stsp free(s->tree_label);
3117 6484ec90 2018-09-29 stsp s->tree_label = NULL;
3118 6484ec90 2018-09-29 stsp free(s->commit_id);
3119 6484ec90 2018-09-29 stsp s->commit_id = NULL;
3120 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
3121 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
3122 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
3123 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
3124 ad80ab7b 2018-08-04 stsp free(parent);
3125 ad80ab7b 2018-08-04 stsp
3126 ad80ab7b 2018-08-04 stsp }
3127 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
3128 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
3129 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
3130 e5a0f69f 2018-08-18 stsp
3131 e5a0f69f 2018-08-18 stsp return NULL;
3132 ad80ab7b 2018-08-04 stsp }
3133 ad80ab7b 2018-08-04 stsp
3134 ad80ab7b 2018-08-04 stsp static const struct got_error *
3135 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
3136 ad80ab7b 2018-08-04 stsp {
3137 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
3138 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
3139 e5a0f69f 2018-08-18 stsp char *parent_path;
3140 ad80ab7b 2018-08-04 stsp
3141 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
3142 e5a0f69f 2018-08-18 stsp if (err)
3143 e5a0f69f 2018-08-18 stsp return err;
3144 ffd1d5e5 2018-06-23 stsp
3145 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
3146 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
3147 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3148 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
3149 e5a0f69f 2018-08-18 stsp free(parent_path);
3150 669b5ffa 2018-10-07 stsp
3151 669b5ffa 2018-10-07 stsp view_vborder(view);
3152 e5a0f69f 2018-08-18 stsp return err;
3153 e5a0f69f 2018-08-18 stsp }
3154 ce52c690 2018-06-23 stsp
3155 e5a0f69f 2018-08-18 stsp static const struct got_error *
3156 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3157 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3158 e5a0f69f 2018-08-18 stsp {
3159 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3160 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
3161 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
3162 669b5ffa 2018-10-07 stsp int begin_x = 0;
3163 ffd1d5e5 2018-06-23 stsp
3164 e5a0f69f 2018-08-18 stsp switch (ch) {
3165 e5a0f69f 2018-08-18 stsp case 'i':
3166 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
3167 ffd1d5e5 2018-06-23 stsp break;
3168 e5a0f69f 2018-08-18 stsp case 'l':
3169 669b5ffa 2018-10-07 stsp if (!s->selected_entry)
3170 669b5ffa 2018-10-07 stsp break;
3171 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
3172 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
3173 669b5ffa 2018-10-07 stsp err = log_tree_entry(&log_view, begin_x,
3174 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
3175 669b5ffa 2018-10-07 stsp s->commit_id, s->repo);
3176 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3177 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3178 669b5ffa 2018-10-07 stsp if (err)
3179 669b5ffa 2018-10-07 stsp return err;
3180 669b5ffa 2018-10-07 stsp err = view_set_child(view, log_view);
3181 669b5ffa 2018-10-07 stsp if (err) {
3182 669b5ffa 2018-10-07 stsp view_close(log_view);
3183 669b5ffa 2018-10-07 stsp break;
3184 669b5ffa 2018-10-07 stsp }
3185 669b5ffa 2018-10-07 stsp *focus_view = log_view;
3186 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3187 669b5ffa 2018-10-07 stsp } else
3188 669b5ffa 2018-10-07 stsp *new_view = log_view;
3189 e5a0f69f 2018-08-18 stsp break;
3190 e5a0f69f 2018-08-18 stsp case 'k':
3191 e5a0f69f 2018-08-18 stsp case KEY_UP:
3192 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3193 e5a0f69f 2018-08-18 stsp s->selected--;
3194 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3195 ffd1d5e5 2018-06-23 stsp break;
3196 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
3197 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
3198 e5a0f69f 2018-08-18 stsp break;
3199 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
3200 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
3201 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
3202 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
3203 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3204 e5a0f69f 2018-08-18 stsp s->selected = 0;
3205 69efd4c4 2018-07-18 stsp break;
3206 e5a0f69f 2018-08-18 stsp }
3207 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
3208 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
3209 e5a0f69f 2018-08-18 stsp s->tree == s->root);
3210 e5a0f69f 2018-08-18 stsp break;
3211 e5a0f69f 2018-08-18 stsp case 'j':
3212 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
3213 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
3214 e5a0f69f 2018-08-18 stsp s->selected++;
3215 1d13200f 2018-07-12 stsp break;
3216 e5a0f69f 2018-08-18 stsp }
3217 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
3218 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
3219 e5a0f69f 2018-08-18 stsp break;
3220 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
3221 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
3222 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
3223 e5a0f69f 2018-08-18 stsp s->entries);
3224 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
3225 e5a0f69f 2018-08-18 stsp entry))
3226 ffd1d5e5 2018-06-23 stsp break;
3227 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
3228 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
3229 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3230 e5a0f69f 2018-08-18 stsp break;
3231 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
3232 e5a0f69f 2018-08-18 stsp case '\r':
3233 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
3234 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3235 7837eeac 2018-09-24 stsp case KEY_BACKSPACE:
3236 e5a0f69f 2018-08-18 stsp /* user selected '..' */
3237 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
3238 ffd1d5e5 2018-06-23 stsp break;
3239 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
3240 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
3241 e5a0f69f 2018-08-18 stsp entry);
3242 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
3243 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
3244 e5a0f69f 2018-08-18 stsp s->entries =
3245 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3246 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
3247 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
3248 e5a0f69f 2018-08-18 stsp s->selected_entry =
3249 e5a0f69f 2018-08-18 stsp parent->selected_entry;
3250 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
3251 e5a0f69f 2018-08-18 stsp free(parent);
3252 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
3253 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3254 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
3255 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
3256 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
3257 e5a0f69f 2018-08-18 stsp if (err)
3258 ffd1d5e5 2018-06-23 stsp break;
3259 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
3260 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
3261 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
3262 e5a0f69f 2018-08-18 stsp break;
3263 ffd1d5e5 2018-06-23 stsp }
3264 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
3265 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
3266 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
3267 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
3268 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
3269 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3270 e5a0f69f 2018-08-18 stsp s->tree = child;
3271 e5a0f69f 2018-08-18 stsp s->entries =
3272 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3273 e5a0f69f 2018-08-18 stsp s->selected = 0;
3274 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3275 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
3276 669b5ffa 2018-10-07 stsp struct tog_view *blame_view;
3277 669b5ffa 2018-10-07 stsp int begin_x = view_is_parent_view(view) ?
3278 669b5ffa 2018-10-07 stsp view_split_begin_x(view->begin_x) : 0;
3279 669b5ffa 2018-10-07 stsp
3280 669b5ffa 2018-10-07 stsp err = blame_tree_entry(&blame_view, begin_x,
3281 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents, s->commit_id,
3282 669b5ffa 2018-10-07 stsp s->repo);
3283 e5a0f69f 2018-08-18 stsp if (err)
3284 ffd1d5e5 2018-06-23 stsp break;
3285 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3286 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3287 669b5ffa 2018-10-07 stsp if (err)
3288 669b5ffa 2018-10-07 stsp return err;
3289 669b5ffa 2018-10-07 stsp err = view_set_child(view, blame_view);
3290 669b5ffa 2018-10-07 stsp if (err) {
3291 669b5ffa 2018-10-07 stsp view_close(blame_view);
3292 669b5ffa 2018-10-07 stsp break;
3293 669b5ffa 2018-10-07 stsp }
3294 669b5ffa 2018-10-07 stsp *focus_view = blame_view;
3295 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3296 669b5ffa 2018-10-07 stsp } else
3297 669b5ffa 2018-10-07 stsp *new_view = blame_view;
3298 e5a0f69f 2018-08-18 stsp }
3299 e5a0f69f 2018-08-18 stsp break;
3300 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3301 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3302 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3303 e5a0f69f 2018-08-18 stsp break;
3304 e5a0f69f 2018-08-18 stsp default:
3305 e5a0f69f 2018-08-18 stsp break;
3306 ffd1d5e5 2018-06-23 stsp }
3307 e5a0f69f 2018-08-18 stsp
3308 ffd1d5e5 2018-06-23 stsp return err;
3309 9f7d7167 2018-04-29 stsp }
3310 9f7d7167 2018-04-29 stsp
3311 ffd1d5e5 2018-06-23 stsp __dead static void
3312 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3313 ffd1d5e5 2018-06-23 stsp {
3314 ffd1d5e5 2018-06-23 stsp endwin();
3315 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3316 ffd1d5e5 2018-06-23 stsp getprogname());
3317 ffd1d5e5 2018-06-23 stsp exit(1);
3318 ffd1d5e5 2018-06-23 stsp }
3319 ffd1d5e5 2018-06-23 stsp
3320 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3321 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3322 ffd1d5e5 2018-06-23 stsp {
3323 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3324 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3325 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3326 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3327 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3328 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3329 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3330 ffd1d5e5 2018-06-23 stsp int ch;
3331 5221c383 2018-08-01 stsp struct tog_view *view;
3332 ffd1d5e5 2018-06-23 stsp
3333 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3334 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3335 ad242220 2018-09-08 stsp == -1)
3336 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3337 ffd1d5e5 2018-06-23 stsp #endif
3338 ffd1d5e5 2018-06-23 stsp
3339 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3340 ffd1d5e5 2018-06-23 stsp switch (ch) {
3341 ffd1d5e5 2018-06-23 stsp case 'c':
3342 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3343 ffd1d5e5 2018-06-23 stsp break;
3344 ffd1d5e5 2018-06-23 stsp default:
3345 ffd1d5e5 2018-06-23 stsp usage();
3346 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3347 ffd1d5e5 2018-06-23 stsp }
3348 ffd1d5e5 2018-06-23 stsp }
3349 ffd1d5e5 2018-06-23 stsp
3350 ffd1d5e5 2018-06-23 stsp argc -= optind;
3351 ffd1d5e5 2018-06-23 stsp argv += optind;
3352 ffd1d5e5 2018-06-23 stsp
3353 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3354 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
3355 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3356 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3357 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3358 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3359 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3360 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3361 ffd1d5e5 2018-06-23 stsp } else
3362 ffd1d5e5 2018-06-23 stsp usage_log();
3363 ffd1d5e5 2018-06-23 stsp
3364 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3365 ffd1d5e5 2018-06-23 stsp free(repo_path);
3366 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3367 ffd1d5e5 2018-06-23 stsp return error;
3368 ffd1d5e5 2018-06-23 stsp
3369 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
3370 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3371 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3372 ffd1d5e5 2018-06-23 stsp goto done;
3373 ffd1d5e5 2018-06-23 stsp } else {
3374 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
3375 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3376 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
3377 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
3378 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
3379 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
3380 ffd1d5e5 2018-06-23 stsp }
3381 ffd1d5e5 2018-06-23 stsp }
3382 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3383 ffd1d5e5 2018-06-23 stsp goto done;
3384 ffd1d5e5 2018-06-23 stsp
3385 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3386 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3387 ffd1d5e5 2018-06-23 stsp goto done;
3388 ffd1d5e5 2018-06-23 stsp
3389 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3390 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3391 ffd1d5e5 2018-06-23 stsp goto done;
3392 ffd1d5e5 2018-06-23 stsp
3393 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3394 5221c383 2018-08-01 stsp if (view == NULL) {
3395 5221c383 2018-08-01 stsp error = got_error_from_errno();
3396 5221c383 2018-08-01 stsp goto done;
3397 5221c383 2018-08-01 stsp }
3398 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3399 ad80ab7b 2018-08-04 stsp if (error)
3400 ad80ab7b 2018-08-04 stsp goto done;
3401 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3402 ffd1d5e5 2018-06-23 stsp done:
3403 ffd1d5e5 2018-06-23 stsp free(commit_id);
3404 ffd1d5e5 2018-06-23 stsp if (commit)
3405 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3406 ffd1d5e5 2018-06-23 stsp if (tree)
3407 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3408 ffd1d5e5 2018-06-23 stsp if (repo)
3409 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3410 ffd1d5e5 2018-06-23 stsp return error;
3411 ffd1d5e5 2018-06-23 stsp }
3412 5c5136c5 2018-05-20 stsp static void
3413 9f7d7167 2018-04-29 stsp init_curses(void)
3414 9f7d7167 2018-04-29 stsp {
3415 9f7d7167 2018-04-29 stsp initscr();
3416 9f7d7167 2018-04-29 stsp cbreak();
3417 0833f15d 2018-10-22 stsp halfdelay(10);
3418 9f7d7167 2018-04-29 stsp noecho();
3419 9f7d7167 2018-04-29 stsp nonl();
3420 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3421 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3422 1f475ad8 2018-05-10 stsp curs_set(0);
3423 9f7d7167 2018-04-29 stsp }
3424 9f7d7167 2018-04-29 stsp
3425 4ed7e80c 2018-05-20 stsp __dead static void
3426 9f7d7167 2018-04-29 stsp usage(void)
3427 9f7d7167 2018-04-29 stsp {
3428 9f7d7167 2018-04-29 stsp int i;
3429 9f7d7167 2018-04-29 stsp
3430 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3431 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3432 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3433 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3434 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3435 9f7d7167 2018-04-29 stsp }
3436 9f7d7167 2018-04-29 stsp exit(1);
3437 9f7d7167 2018-04-29 stsp }
3438 9f7d7167 2018-04-29 stsp
3439 c2301be8 2018-04-30 stsp static char **
3440 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3441 c2301be8 2018-04-30 stsp {
3442 c2301be8 2018-04-30 stsp char **argv;
3443 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3444 c2301be8 2018-04-30 stsp
3445 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3446 c2301be8 2018-04-30 stsp if (argv == NULL)
3447 c2301be8 2018-04-30 stsp err(1, "calloc");
3448 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3449 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3450 c2301be8 2018-04-30 stsp err(1, "calloc");
3451 c2301be8 2018-04-30 stsp if (arg1) {
3452 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3453 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3454 c2301be8 2018-04-30 stsp err(1, "calloc");
3455 c2301be8 2018-04-30 stsp }
3456 c2301be8 2018-04-30 stsp
3457 c2301be8 2018-04-30 stsp return argv;
3458 c2301be8 2018-04-30 stsp }
3459 c2301be8 2018-04-30 stsp
3460 9f7d7167 2018-04-29 stsp int
3461 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3462 9f7d7167 2018-04-29 stsp {
3463 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3464 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3465 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3466 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3467 9f7d7167 2018-04-29 stsp
3468 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
3469 9f7d7167 2018-04-29 stsp
3470 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3471 9f7d7167 2018-04-29 stsp switch (ch) {
3472 9f7d7167 2018-04-29 stsp case 'h':
3473 9f7d7167 2018-04-29 stsp hflag = 1;
3474 9f7d7167 2018-04-29 stsp break;
3475 9f7d7167 2018-04-29 stsp default:
3476 9f7d7167 2018-04-29 stsp usage();
3477 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3478 9f7d7167 2018-04-29 stsp }
3479 9f7d7167 2018-04-29 stsp }
3480 9f7d7167 2018-04-29 stsp
3481 9f7d7167 2018-04-29 stsp argc -= optind;
3482 9f7d7167 2018-04-29 stsp argv += optind;
3483 9f7d7167 2018-04-29 stsp optind = 0;
3484 c2301be8 2018-04-30 stsp optreset = 1;
3485 9f7d7167 2018-04-29 stsp
3486 c2301be8 2018-04-30 stsp if (argc == 0) {
3487 f29d3e89 2018-06-23 stsp if (hflag)
3488 f29d3e89 2018-06-23 stsp usage();
3489 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3490 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3491 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3492 c2301be8 2018-04-30 stsp argc = 1;
3493 c2301be8 2018-04-30 stsp } else {
3494 9f7d7167 2018-04-29 stsp int i;
3495 9f7d7167 2018-04-29 stsp
3496 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3497 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3498 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3499 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3500 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3501 9f7d7167 2018-04-29 stsp if (hflag)
3502 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3503 9f7d7167 2018-04-29 stsp break;
3504 9f7d7167 2018-04-29 stsp }
3505 9f7d7167 2018-04-29 stsp }
3506 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3507 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3508 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3509 c2301be8 2018-04-30 stsp if (repo_path) {
3510 c2301be8 2018-04-30 stsp struct got_repository *repo;
3511 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3512 c2301be8 2018-04-30 stsp if (error == NULL)
3513 c2301be8 2018-04-30 stsp got_repo_close(repo);
3514 c2301be8 2018-04-30 stsp } else
3515 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3516 c2301be8 2018-04-30 stsp if (error) {
3517 f29d3e89 2018-06-23 stsp if (hflag) {
3518 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3519 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3520 f29d3e89 2018-06-23 stsp argv[0]);
3521 f29d3e89 2018-06-23 stsp usage();
3522 f29d3e89 2018-06-23 stsp }
3523 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3524 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3525 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3526 ad7de8d9 2018-04-30 stsp free(repo_path);
3527 c2301be8 2018-04-30 stsp return 1;
3528 c2301be8 2018-04-30 stsp }
3529 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3530 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3531 c2301be8 2018-04-30 stsp argc = 2;
3532 c2301be8 2018-04-30 stsp free(repo_path);
3533 9f7d7167 2018-04-29 stsp }
3534 9f7d7167 2018-04-29 stsp }
3535 9f7d7167 2018-04-29 stsp
3536 5c5136c5 2018-05-20 stsp init_curses();
3537 9f7d7167 2018-04-29 stsp
3538 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3539 9f7d7167 2018-04-29 stsp if (error)
3540 9f7d7167 2018-04-29 stsp goto done;
3541 9f7d7167 2018-04-29 stsp done:
3542 9f7d7167 2018-04-29 stsp endwin();
3543 c2301be8 2018-04-30 stsp free(cmd_argv);
3544 9f7d7167 2018-04-29 stsp if (error)
3545 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3546 9f7d7167 2018-04-29 stsp return 0;
3547 9f7d7167 2018-04-29 stsp }