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