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