Blame


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