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