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 6e3e5d9c 2018-10-18 stsp if (view->child) {
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 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1294 e5a0f69f 2018-08-18 stsp s->selected--;
1295 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
1296 31120ada 2018-04-30 stsp break;
1297 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry, 1,
1298 e5a0f69f 2018-08-18 stsp &s->commits);
1299 e5a0f69f 2018-08-18 stsp break;
1300 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1301 e5a0f69f 2018-08-18 stsp if (TAILQ_FIRST(&s->commits.head) ==
1302 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
1303 e5a0f69f 2018-08-18 stsp s->selected = 0;
1304 80ddbec8 2018-04-29 stsp break;
1305 e5a0f69f 2018-08-18 stsp }
1306 e5a0f69f 2018-08-18 stsp scroll_up(&s->first_displayed_entry,
1307 e5a0f69f 2018-08-18 stsp view->nlines, &s->commits);
1308 e5a0f69f 2018-08-18 stsp break;
1309 e5a0f69f 2018-08-18 stsp case 'j':
1310 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1311 e5a0f69f 2018-08-18 stsp if (s->selected < MIN(view->nlines - 2,
1312 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1313 e5a0f69f 2018-08-18 stsp s->selected++;
1314 80ddbec8 2018-04-29 stsp break;
1315 e5a0f69f 2018-08-18 stsp }
1316 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry, 1,
1317 93e45b7c 2018-09-24 stsp &s->last_displayed_entry, &s->commits,
1318 e5a0f69f 2018-08-18 stsp s->graph, s->repo, s->in_repo_path);
1319 e5a0f69f 2018-08-18 stsp if (err) {
1320 4d7951a5 2018-08-04 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1321 e5a0f69f 2018-08-18 stsp break;
1322 4d7951a5 2018-08-04 stsp err = NULL;
1323 ecb28ae0 2018-07-16 stsp }
1324 e5a0f69f 2018-08-18 stsp break;
1325 e5a0f69f 2018-08-18 stsp case KEY_NPAGE: {
1326 e5a0f69f 2018-08-18 stsp struct commit_queue_entry *first;
1327 e5a0f69f 2018-08-18 stsp first = s->first_displayed_entry;
1328 e5a0f69f 2018-08-18 stsp err = scroll_down(&s->first_displayed_entry,
1329 93e45b7c 2018-09-24 stsp view->nlines, &s->last_displayed_entry,
1330 e5a0f69f 2018-08-18 stsp &s->commits, s->graph, s->repo,
1331 e5a0f69f 2018-08-18 stsp s->in_repo_path);
1332 93e45b7c 2018-09-24 stsp if (err && err->code != GOT_ERR_ITER_COMPLETED)
1333 9343a5fb 2018-06-23 stsp break;
1334 e5a0f69f 2018-08-18 stsp if (first == s->first_displayed_entry &&
1335 e5a0f69f 2018-08-18 stsp s->selected < MIN(view->nlines - 2,
1336 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1)) {
1337 e5a0f69f 2018-08-18 stsp /* can't scroll further down */
1338 e5a0f69f 2018-08-18 stsp s->selected = MIN(view->nlines - 2,
1339 e5a0f69f 2018-08-18 stsp s->commits.ncommits - 1);
1340 e5a0f69f 2018-08-18 stsp }
1341 e5a0f69f 2018-08-18 stsp err = NULL;
1342 e5a0f69f 2018-08-18 stsp break;
1343 80ddbec8 2018-04-29 stsp }
1344 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
1345 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines - 2)
1346 e5a0f69f 2018-08-18 stsp s->selected = view->nlines - 2;
1347 e5a0f69f 2018-08-18 stsp if (s->selected > s->commits.ncommits - 1)
1348 e5a0f69f 2018-08-18 stsp s->selected = s->commits.ncommits - 1;
1349 e5a0f69f 2018-08-18 stsp break;
1350 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
1351 e5a0f69f 2018-08-18 stsp case '\r':
1352 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1353 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1354 669b5ffa 2018-10-07 stsp err = open_diff_view_for_commit(&diff_view, begin_x,
1355 2a4718d3 2018-09-29 stsp s->selected_entry->id, s->selected_entry->commit,
1356 e5a0f69f 2018-08-18 stsp s->repo);
1357 bfddd0d9 2018-09-29 stsp if (err)
1358 bfddd0d9 2018-09-29 stsp break;
1359 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1360 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1361 669b5ffa 2018-10-07 stsp if (err)
1362 669b5ffa 2018-10-07 stsp return err;
1363 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
1364 669b5ffa 2018-10-07 stsp if (err) {
1365 669b5ffa 2018-10-07 stsp view_close(diff_view);
1366 669b5ffa 2018-10-07 stsp break;
1367 669b5ffa 2018-10-07 stsp }
1368 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(diff_view)) {
1369 669b5ffa 2018-10-07 stsp *focus_view = diff_view;
1370 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1371 669b5ffa 2018-10-07 stsp }
1372 669b5ffa 2018-10-07 stsp } else
1373 669b5ffa 2018-10-07 stsp *new_view = diff_view;
1374 e5a0f69f 2018-08-18 stsp break;
1375 e5a0f69f 2018-08-18 stsp case 't':
1376 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1377 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
1378 669b5ffa 2018-10-07 stsp err = browse_commit(&tree_view, begin_x,
1379 0cf4efb1 2018-09-29 stsp s->selected_entry, s->repo);
1380 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
1381 669b5ffa 2018-10-07 stsp err = view_close_child(view);
1382 669b5ffa 2018-10-07 stsp if (err)
1383 669b5ffa 2018-10-07 stsp return err;
1384 669b5ffa 2018-10-07 stsp err = view_set_child(view, tree_view);
1385 669b5ffa 2018-10-07 stsp if (err) {
1386 669b5ffa 2018-10-07 stsp view_close(tree_view);
1387 669b5ffa 2018-10-07 stsp break;
1388 669b5ffa 2018-10-07 stsp }
1389 669b5ffa 2018-10-07 stsp *focus_view = tree_view;
1390 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
1391 669b5ffa 2018-10-07 stsp } else
1392 669b5ffa 2018-10-07 stsp *new_view = tree_view;
1393 e5a0f69f 2018-08-18 stsp break;
1394 5036bf37 2018-09-24 stsp case KEY_BACKSPACE:
1395 5036bf37 2018-09-24 stsp if (strcmp(s->in_repo_path, "/") == 0)
1396 5036bf37 2018-09-24 stsp break;
1397 5036bf37 2018-09-24 stsp parent_path = dirname(s->in_repo_path);
1398 5036bf37 2018-09-24 stsp if (parent_path && strcmp(parent_path, ".") != 0) {
1399 5036bf37 2018-09-24 stsp struct tog_view *lv;
1400 0cf4efb1 2018-09-29 stsp lv = view_open(view->nlines, view->ncols,
1401 0cf4efb1 2018-09-29 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
1402 5036bf37 2018-09-24 stsp if (lv == NULL)
1403 5036bf37 2018-09-24 stsp return got_error_from_errno();
1404 5036bf37 2018-09-24 stsp err = open_log_view(lv, s->start_id, s->repo,
1405 5036bf37 2018-09-24 stsp parent_path);
1406 5036bf37 2018-09-24 stsp if (err)
1407 5036bf37 2018-09-24 stsp break;
1408 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
1409 669b5ffa 2018-10-07 stsp *new_view = lv;
1410 669b5ffa 2018-10-07 stsp else {
1411 669b5ffa 2018-10-07 stsp view_set_child(view->parent, lv);
1412 669b5ffa 2018-10-07 stsp *dead_view = view;
1413 669b5ffa 2018-10-07 stsp }
1414 5036bf37 2018-09-24 stsp }
1415 5036bf37 2018-09-24 stsp break;
1416 e5a0f69f 2018-08-18 stsp default:
1417 e5a0f69f 2018-08-18 stsp break;
1418 899d86c2 2018-05-10 stsp }
1419 e5a0f69f 2018-08-18 stsp
1420 80ddbec8 2018-04-29 stsp return err;
1421 80ddbec8 2018-04-29 stsp }
1422 80ddbec8 2018-04-29 stsp
1423 4ed7e80c 2018-05-20 stsp static const struct got_error *
1424 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
1425 9f7d7167 2018-04-29 stsp {
1426 80ddbec8 2018-04-29 stsp const struct got_error *error;
1427 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
1428 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
1429 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
1430 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
1431 80ddbec8 2018-04-29 stsp int ch;
1432 04cc582a 2018-08-01 stsp struct tog_view *view;
1433 80ddbec8 2018-04-29 stsp
1434 80ddbec8 2018-04-29 stsp #ifndef PROFILE
1435 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1436 ad242220 2018-09-08 stsp == -1)
1437 80ddbec8 2018-04-29 stsp err(1, "pledge");
1438 80ddbec8 2018-04-29 stsp #endif
1439 80ddbec8 2018-04-29 stsp
1440 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1441 80ddbec8 2018-04-29 stsp switch (ch) {
1442 80ddbec8 2018-04-29 stsp case 'c':
1443 80ddbec8 2018-04-29 stsp start_commit = optarg;
1444 80ddbec8 2018-04-29 stsp break;
1445 ecb28ae0 2018-07-16 stsp case 'r':
1446 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1447 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
1448 ecb28ae0 2018-07-16 stsp err(1, "-r option");
1449 ecb28ae0 2018-07-16 stsp break;
1450 80ddbec8 2018-04-29 stsp default:
1451 80ddbec8 2018-04-29 stsp usage();
1452 80ddbec8 2018-04-29 stsp /* NOTREACHED */
1453 80ddbec8 2018-04-29 stsp }
1454 80ddbec8 2018-04-29 stsp }
1455 80ddbec8 2018-04-29 stsp
1456 80ddbec8 2018-04-29 stsp argc -= optind;
1457 80ddbec8 2018-04-29 stsp argv += optind;
1458 80ddbec8 2018-04-29 stsp
1459 ecb28ae0 2018-07-16 stsp if (argc == 0)
1460 ecb28ae0 2018-07-16 stsp path = strdup("");
1461 ecb28ae0 2018-07-16 stsp else if (argc == 1)
1462 ecb28ae0 2018-07-16 stsp path = strdup(argv[0]);
1463 ecb28ae0 2018-07-16 stsp else
1464 80ddbec8 2018-04-29 stsp usage_log();
1465 ecb28ae0 2018-07-16 stsp if (path == NULL)
1466 ecb28ae0 2018-07-16 stsp return got_error_from_errno();
1467 80ddbec8 2018-04-29 stsp
1468 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
1469 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
1470 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1471 ecb28ae0 2018-07-16 stsp goto done;
1472 ecb28ae0 2018-07-16 stsp }
1473 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1474 ecb28ae0 2018-07-16 stsp repo_path = strdup(cwd);
1475 ecb28ae0 2018-07-16 stsp if (repo_path == NULL) {
1476 ecb28ae0 2018-07-16 stsp error = got_error_from_errno();
1477 ecb28ae0 2018-07-16 stsp goto done;
1478 ecb28ae0 2018-07-16 stsp }
1479 ecb28ae0 2018-07-16 stsp }
1480 ecb28ae0 2018-07-16 stsp
1481 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
1482 80ddbec8 2018-04-29 stsp if (error != NULL)
1483 ecb28ae0 2018-07-16 stsp goto done;
1484 80ddbec8 2018-04-29 stsp
1485 80ddbec8 2018-04-29 stsp if (start_commit == NULL) {
1486 899d86c2 2018-05-10 stsp error = get_head_commit_id(&start_id, repo);
1487 80ddbec8 2018-04-29 stsp if (error != NULL)
1488 ecb28ae0 2018-07-16 stsp goto done;
1489 80ddbec8 2018-04-29 stsp } else {
1490 80ddbec8 2018-04-29 stsp struct got_object *obj;
1491 80ddbec8 2018-04-29 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
1492 80ddbec8 2018-04-29 stsp if (error == NULL) {
1493 6402fb3c 2018-09-15 stsp start_id = got_object_id_dup(got_object_get_id(obj));
1494 899d86c2 2018-05-10 stsp if (start_id == NULL)
1495 80ddbec8 2018-04-29 stsp error = got_error_from_errno();
1496 ecb28ae0 2018-07-16 stsp goto done;
1497 80ddbec8 2018-04-29 stsp }
1498 80ddbec8 2018-04-29 stsp }
1499 80ddbec8 2018-04-29 stsp if (error != NULL)
1500 ecb28ae0 2018-07-16 stsp goto done;
1501 ecb28ae0 2018-07-16 stsp
1502 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1503 04cc582a 2018-08-01 stsp if (view == NULL) {
1504 04cc582a 2018-08-01 stsp error = got_error_from_errno();
1505 04cc582a 2018-08-01 stsp goto done;
1506 04cc582a 2018-08-01 stsp }
1507 ba4f502b 2018-08-04 stsp error = open_log_view(view, start_id, repo, path);
1508 ba4f502b 2018-08-04 stsp if (error)
1509 ba4f502b 2018-08-04 stsp goto done;
1510 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1511 ecb28ae0 2018-07-16 stsp done:
1512 ecb28ae0 2018-07-16 stsp free(repo_path);
1513 ecb28ae0 2018-07-16 stsp free(cwd);
1514 ecb28ae0 2018-07-16 stsp free(path);
1515 899d86c2 2018-05-10 stsp free(start_id);
1516 ecb28ae0 2018-07-16 stsp if (repo)
1517 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
1518 80ddbec8 2018-04-29 stsp return error;
1519 9f7d7167 2018-04-29 stsp }
1520 9f7d7167 2018-04-29 stsp
1521 4ed7e80c 2018-05-20 stsp __dead static void
1522 9f7d7167 2018-04-29 stsp usage_diff(void)
1523 9f7d7167 2018-04-29 stsp {
1524 80ddbec8 2018-04-29 stsp endwin();
1525 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1526 9f7d7167 2018-04-29 stsp getprogname());
1527 9f7d7167 2018-04-29 stsp exit(1);
1528 b304db33 2018-05-20 stsp }
1529 b304db33 2018-05-20 stsp
1530 b304db33 2018-05-20 stsp static char *
1531 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
1532 b304db33 2018-05-20 stsp {
1533 b304db33 2018-05-20 stsp char *line;
1534 b304db33 2018-05-20 stsp size_t linelen;
1535 b304db33 2018-05-20 stsp size_t lineno;
1536 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
1537 b304db33 2018-05-20 stsp
1538 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
1539 b304db33 2018-05-20 stsp if (len)
1540 b304db33 2018-05-20 stsp *len = linelen;
1541 b304db33 2018-05-20 stsp return line;
1542 26ed57b2 2018-05-19 stsp }
1543 26ed57b2 2018-05-19 stsp
1544 4ed7e80c 2018-05-20 stsp static const struct got_error *
1545 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1546 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
1547 a3404814 2018-09-02 stsp char * header)
1548 26ed57b2 2018-05-19 stsp {
1549 61e69b96 2018-05-20 stsp const struct got_error *err;
1550 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
1551 b304db33 2018-05-20 stsp char *line;
1552 b304db33 2018-05-20 stsp size_t len;
1553 61e69b96 2018-05-20 stsp wchar_t *wline;
1554 e0b650dd 2018-05-20 stsp int width;
1555 26ed57b2 2018-05-19 stsp
1556 26ed57b2 2018-05-19 stsp rewind(f);
1557 f7d12f7e 2018-08-01 stsp werase(view->window);
1558 a3404814 2018-09-02 stsp
1559 a3404814 2018-09-02 stsp if (header) {
1560 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
1561 a3404814 2018-09-02 stsp if (err) {
1562 a3404814 2018-09-02 stsp return err;
1563 a3404814 2018-09-02 stsp }
1564 a3404814 2018-09-02 stsp
1565 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1566 a3404814 2018-09-02 stsp wstandout(view->window);
1567 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
1568 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1569 a3404814 2018-09-02 stsp wstandend(view->window);
1570 a3404814 2018-09-02 stsp if (width < view->ncols)
1571 a3404814 2018-09-02 stsp waddch(view->window, '\n');
1572 26ed57b2 2018-05-19 stsp
1573 a3404814 2018-09-02 stsp if (max_lines <= 1)
1574 a3404814 2018-09-02 stsp return NULL;
1575 a3404814 2018-09-02 stsp max_lines--;
1576 a3404814 2018-09-02 stsp }
1577 a3404814 2018-09-02 stsp
1578 26ed57b2 2018-05-19 stsp *eof = 0;
1579 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
1580 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
1581 26ed57b2 2018-05-19 stsp if (line == NULL) {
1582 26ed57b2 2018-05-19 stsp *eof = 1;
1583 26ed57b2 2018-05-19 stsp break;
1584 26ed57b2 2018-05-19 stsp }
1585 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
1586 26ed57b2 2018-05-19 stsp free(line);
1587 26ed57b2 2018-05-19 stsp continue;
1588 26ed57b2 2018-05-19 stsp }
1589 26ed57b2 2018-05-19 stsp
1590 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1591 61e69b96 2018-05-20 stsp if (err) {
1592 61e69b96 2018-05-20 stsp free(line);
1593 61e69b96 2018-05-20 stsp return err;
1594 61e69b96 2018-05-20 stsp }
1595 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1596 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1597 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1598 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
1599 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
1600 26ed57b2 2018-05-19 stsp free(line);
1601 2550e4c3 2018-07-13 stsp free(wline);
1602 2550e4c3 2018-07-13 stsp wline = NULL;
1603 26ed57b2 2018-05-19 stsp }
1604 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
1605 26ed57b2 2018-05-19 stsp
1606 1a57306a 2018-09-02 stsp view_vborder(view);
1607 26ed57b2 2018-05-19 stsp
1608 26ed57b2 2018-05-19 stsp return NULL;
1609 9f7d7167 2018-04-29 stsp }
1610 9f7d7167 2018-04-29 stsp
1611 4ed7e80c 2018-05-20 stsp static const struct got_error *
1612 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
1613 26ed57b2 2018-05-19 stsp {
1614 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
1615 48ae06ee 2018-10-18 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1616 48ae06ee 2018-10-18 stsp FILE *f = NULL;
1617 26ed57b2 2018-05-19 stsp
1618 48ae06ee 2018-10-18 stsp if (s->id1) {
1619 48ae06ee 2018-10-18 stsp err = got_object_open(&obj1, s->repo, s->id1);
1620 48ae06ee 2018-10-18 stsp if (err)
1621 48ae06ee 2018-10-18 stsp return err;
1622 48ae06ee 2018-10-18 stsp }
1623 26ed57b2 2018-05-19 stsp
1624 48ae06ee 2018-10-18 stsp err = got_object_open(&obj2, s->repo, s->id2);
1625 48ae06ee 2018-10-18 stsp if (err)
1626 48ae06ee 2018-10-18 stsp goto done;
1627 48ae06ee 2018-10-18 stsp
1628 511a516b 2018-05-19 stsp f = got_opentemp();
1629 48ae06ee 2018-10-18 stsp if (f == NULL) {
1630 48ae06ee 2018-10-18 stsp err = got_error_from_errno();
1631 48ae06ee 2018-10-18 stsp goto done;
1632 48ae06ee 2018-10-18 stsp }
1633 48ae06ee 2018-10-18 stsp if (s->f)
1634 48ae06ee 2018-10-18 stsp fclose(s->f);
1635 48ae06ee 2018-10-18 stsp s->f = f;
1636 26ed57b2 2018-05-19 stsp
1637 cd0acaa7 2018-05-20 stsp switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1638 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
1639 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1640 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1641 26ed57b2 2018-05-19 stsp break;
1642 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
1643 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_trees(obj1, obj2, "", "",
1644 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
1645 26ed57b2 2018-05-19 stsp break;
1646 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_COMMIT:
1647 48ae06ee 2018-10-18 stsp err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1648 48ae06ee 2018-10-18 stsp s->repo, f);
1649 26ed57b2 2018-05-19 stsp break;
1650 26ed57b2 2018-05-19 stsp default:
1651 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1652 48ae06ee 2018-10-18 stsp break;
1653 26ed57b2 2018-05-19 stsp }
1654 48ae06ee 2018-10-18 stsp done:
1655 48ae06ee 2018-10-18 stsp if (obj1)
1656 48ae06ee 2018-10-18 stsp got_object_close(obj1);
1657 48ae06ee 2018-10-18 stsp got_object_close(obj2);
1658 48ae06ee 2018-10-18 stsp if (f)
1659 48ae06ee 2018-10-18 stsp fflush(f);
1660 48ae06ee 2018-10-18 stsp return err;
1661 48ae06ee 2018-10-18 stsp }
1662 26ed57b2 2018-05-19 stsp
1663 48ae06ee 2018-10-18 stsp static const struct got_error *
1664 48ae06ee 2018-10-18 stsp open_diff_view(struct tog_view *view, struct got_object *obj1,
1665 48ae06ee 2018-10-18 stsp struct got_object *obj2, struct got_repository *repo)
1666 48ae06ee 2018-10-18 stsp {
1667 48ae06ee 2018-10-18 stsp const struct got_error *err;
1668 5dc9f4bc 2018-08-04 stsp
1669 48ae06ee 2018-10-18 stsp if (obj1 != NULL && obj2 != NULL &&
1670 48ae06ee 2018-10-18 stsp got_object_get_type(obj1) != got_object_get_type(obj2))
1671 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
1672 48ae06ee 2018-10-18 stsp
1673 48ae06ee 2018-10-18 stsp if (obj1) {
1674 48ae06ee 2018-10-18 stsp struct got_object_id *id1;
1675 48ae06ee 2018-10-18 stsp id1 = got_object_id_dup(got_object_get_id(obj1));
1676 48ae06ee 2018-10-18 stsp if (id1 == NULL)
1677 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1678 48ae06ee 2018-10-18 stsp view->state.diff.id1 = id1;
1679 48ae06ee 2018-10-18 stsp } else
1680 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1681 48ae06ee 2018-10-18 stsp
1682 48ae06ee 2018-10-18 stsp view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1683 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
1684 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1685 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1686 48ae06ee 2018-10-18 stsp return got_error_from_errno();
1687 48ae06ee 2018-10-18 stsp }
1688 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
1689 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
1690 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
1691 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
1692 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
1693 5dc9f4bc 2018-08-04 stsp
1694 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
1695 48ae06ee 2018-10-18 stsp if (err) {
1696 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1697 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1698 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
1699 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
1700 48ae06ee 2018-10-18 stsp return err;
1701 48ae06ee 2018-10-18 stsp }
1702 48ae06ee 2018-10-18 stsp
1703 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
1704 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
1705 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
1706 e5a0f69f 2018-08-18 stsp
1707 5dc9f4bc 2018-08-04 stsp return NULL;
1708 5dc9f4bc 2018-08-04 stsp }
1709 5dc9f4bc 2018-08-04 stsp
1710 e5a0f69f 2018-08-18 stsp static const struct got_error *
1711 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
1712 5dc9f4bc 2018-08-04 stsp {
1713 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1714 e5a0f69f 2018-08-18 stsp
1715 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
1716 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
1717 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
1718 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
1719 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1720 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
1721 e5a0f69f 2018-08-18 stsp return err;
1722 5dc9f4bc 2018-08-04 stsp }
1723 5dc9f4bc 2018-08-04 stsp
1724 5dc9f4bc 2018-08-04 stsp static const struct got_error *
1725 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
1726 5dc9f4bc 2018-08-04 stsp {
1727 a3404814 2018-09-02 stsp const struct got_error *err;
1728 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
1729 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
1730 a3404814 2018-09-02 stsp
1731 a3404814 2018-09-02 stsp if (s->id1) {
1732 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
1733 a3404814 2018-09-02 stsp if (err)
1734 a3404814 2018-09-02 stsp return err;
1735 a3404814 2018-09-02 stsp }
1736 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
1737 a3404814 2018-09-02 stsp if (err)
1738 a3404814 2018-09-02 stsp return err;
1739 26ed57b2 2018-05-19 stsp
1740 a3404814 2018-09-02 stsp if (asprintf(&header, "diff: %s %s",
1741 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1742 a3404814 2018-09-02 stsp err = got_error_from_errno();
1743 a3404814 2018-09-02 stsp free(id_str1);
1744 a3404814 2018-09-02 stsp free(id_str2);
1745 a3404814 2018-09-02 stsp return err;
1746 a3404814 2018-09-02 stsp }
1747 a3404814 2018-09-02 stsp free(id_str1);
1748 a3404814 2018-09-02 stsp free(id_str2);
1749 a3404814 2018-09-02 stsp
1750 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
1751 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
1752 a3404814 2018-09-02 stsp header);
1753 0cf4efb1 2018-09-29 stsp }
1754 0cf4efb1 2018-09-29 stsp
1755 0cf4efb1 2018-09-29 stsp static const struct got_error *
1756 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1757 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1758 e5a0f69f 2018-08-18 stsp {
1759 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
1760 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
1761 e5a0f69f 2018-08-18 stsp int i;
1762 e5a0f69f 2018-08-18 stsp
1763 e5a0f69f 2018-08-18 stsp switch (ch) {
1764 e5a0f69f 2018-08-18 stsp case 'k':
1765 e5a0f69f 2018-08-18 stsp case KEY_UP:
1766 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > 1)
1767 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1768 26ed57b2 2018-05-19 stsp break;
1769 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
1770 e5a0f69f 2018-08-18 stsp i = 0;
1771 e5a0f69f 2018-08-18 stsp while (i++ < view->nlines - 1 &&
1772 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
1773 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
1774 e5a0f69f 2018-08-18 stsp break;
1775 e5a0f69f 2018-08-18 stsp case 'j':
1776 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
1777 e5a0f69f 2018-08-18 stsp if (!s->eof)
1778 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1779 e5a0f69f 2018-08-18 stsp break;
1780 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
1781 e5a0f69f 2018-08-18 stsp case ' ':
1782 e5a0f69f 2018-08-18 stsp i = 0;
1783 e5a0f69f 2018-08-18 stsp while (!s->eof && i++ < view->nlines - 1) {
1784 e5a0f69f 2018-08-18 stsp char *line;
1785 e5a0f69f 2018-08-18 stsp line = parse_next_line(s->f, NULL);
1786 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
1787 e5a0f69f 2018-08-18 stsp if (line == NULL)
1788 e5a0f69f 2018-08-18 stsp break;
1789 48ae06ee 2018-10-18 stsp }
1790 48ae06ee 2018-10-18 stsp break;
1791 48ae06ee 2018-10-18 stsp case '[':
1792 48ae06ee 2018-10-18 stsp if (s->diff_context > 0) {
1793 48ae06ee 2018-10-18 stsp s->diff_context--;
1794 48ae06ee 2018-10-18 stsp err = create_diff(s);
1795 48ae06ee 2018-10-18 stsp }
1796 48ae06ee 2018-10-18 stsp break;
1797 48ae06ee 2018-10-18 stsp case ']':
1798 48ae06ee 2018-10-18 stsp if (s->diff_context < INT_MAX) {
1799 48ae06ee 2018-10-18 stsp s->diff_context++;
1800 48ae06ee 2018-10-18 stsp err = create_diff(s);
1801 bcbd79e2 2018-08-19 stsp }
1802 bcbd79e2 2018-08-19 stsp break;
1803 e5a0f69f 2018-08-18 stsp default:
1804 e5a0f69f 2018-08-18 stsp break;
1805 26ed57b2 2018-05-19 stsp }
1806 e5a0f69f 2018-08-18 stsp
1807 bcbd79e2 2018-08-19 stsp return err;
1808 26ed57b2 2018-05-19 stsp }
1809 26ed57b2 2018-05-19 stsp
1810 4ed7e80c 2018-05-20 stsp static const struct got_error *
1811 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
1812 9f7d7167 2018-04-29 stsp {
1813 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
1814 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
1815 26ed57b2 2018-05-19 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
1816 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
1817 26ed57b2 2018-05-19 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1818 26ed57b2 2018-05-19 stsp int ch;
1819 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
1820 26ed57b2 2018-05-19 stsp
1821 26ed57b2 2018-05-19 stsp #ifndef PROFILE
1822 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1823 ad242220 2018-09-08 stsp == -1)
1824 26ed57b2 2018-05-19 stsp err(1, "pledge");
1825 26ed57b2 2018-05-19 stsp #endif
1826 26ed57b2 2018-05-19 stsp
1827 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1828 26ed57b2 2018-05-19 stsp switch (ch) {
1829 26ed57b2 2018-05-19 stsp default:
1830 26ed57b2 2018-05-19 stsp usage();
1831 26ed57b2 2018-05-19 stsp /* NOTREACHED */
1832 26ed57b2 2018-05-19 stsp }
1833 26ed57b2 2018-05-19 stsp }
1834 26ed57b2 2018-05-19 stsp
1835 26ed57b2 2018-05-19 stsp argc -= optind;
1836 26ed57b2 2018-05-19 stsp argv += optind;
1837 26ed57b2 2018-05-19 stsp
1838 26ed57b2 2018-05-19 stsp if (argc == 0) {
1839 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
1840 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
1841 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
1842 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1843 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1844 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[0];
1845 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[1];
1846 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
1847 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
1848 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
1849 26ed57b2 2018-05-19 stsp return got_error_from_errno();
1850 26ed57b2 2018-05-19 stsp obj_id_str1 = argv[1];
1851 26ed57b2 2018-05-19 stsp obj_id_str2 = argv[2];
1852 26ed57b2 2018-05-19 stsp } else
1853 26ed57b2 2018-05-19 stsp usage_diff();
1854 26ed57b2 2018-05-19 stsp
1855 26ed57b2 2018-05-19 stsp error = got_repo_open(&repo, repo_path);
1856 26ed57b2 2018-05-19 stsp free(repo_path);
1857 26ed57b2 2018-05-19 stsp if (error)
1858 26ed57b2 2018-05-19 stsp goto done;
1859 26ed57b2 2018-05-19 stsp
1860 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1861 26ed57b2 2018-05-19 stsp if (error)
1862 26ed57b2 2018-05-19 stsp goto done;
1863 26ed57b2 2018-05-19 stsp
1864 26ed57b2 2018-05-19 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1865 26ed57b2 2018-05-19 stsp if (error)
1866 26ed57b2 2018-05-19 stsp goto done;
1867 26ed57b2 2018-05-19 stsp
1868 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
1869 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
1870 ea5e7bb5 2018-08-01 stsp error = got_error_from_errno();
1871 ea5e7bb5 2018-08-01 stsp goto done;
1872 ea5e7bb5 2018-08-01 stsp }
1873 5dc9f4bc 2018-08-04 stsp error = open_diff_view(view, obj1, obj2, repo);
1874 5dc9f4bc 2018-08-04 stsp if (error)
1875 5dc9f4bc 2018-08-04 stsp goto done;
1876 e5a0f69f 2018-08-18 stsp error = view_loop(view);
1877 26ed57b2 2018-05-19 stsp done:
1878 26ed57b2 2018-05-19 stsp got_repo_close(repo);
1879 26ed57b2 2018-05-19 stsp if (obj1)
1880 26ed57b2 2018-05-19 stsp got_object_close(obj1);
1881 26ed57b2 2018-05-19 stsp if (obj2)
1882 26ed57b2 2018-05-19 stsp got_object_close(obj2);
1883 26ed57b2 2018-05-19 stsp return error;
1884 9f7d7167 2018-04-29 stsp }
1885 9f7d7167 2018-04-29 stsp
1886 4ed7e80c 2018-05-20 stsp __dead static void
1887 9f7d7167 2018-04-29 stsp usage_blame(void)
1888 9f7d7167 2018-04-29 stsp {
1889 80ddbec8 2018-04-29 stsp endwin();
1890 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1891 9f7d7167 2018-04-29 stsp getprogname());
1892 9f7d7167 2018-04-29 stsp exit(1);
1893 9f7d7167 2018-04-29 stsp }
1894 84451b3e 2018-07-10 stsp
1895 84451b3e 2018-07-10 stsp struct tog_blame_line {
1896 84451b3e 2018-07-10 stsp int annotated;
1897 84451b3e 2018-07-10 stsp struct got_object_id *id;
1898 84451b3e 2018-07-10 stsp };
1899 9f7d7167 2018-04-29 stsp
1900 4ed7e80c 2018-05-20 stsp static const struct got_error *
1901 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1902 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
1903 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
1904 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
1905 84451b3e 2018-07-10 stsp {
1906 84451b3e 2018-07-10 stsp const struct got_error *err;
1907 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
1908 84451b3e 2018-07-10 stsp char *line;
1909 84451b3e 2018-07-10 stsp size_t len;
1910 84451b3e 2018-07-10 stsp wchar_t *wline;
1911 b700b5d6 2018-07-10 stsp int width, wlimit;
1912 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
1913 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
1914 ab089a2a 2018-07-12 stsp char *id_str;
1915 ab089a2a 2018-07-12 stsp
1916 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
1917 ab089a2a 2018-07-12 stsp if (err)
1918 ab089a2a 2018-07-12 stsp return err;
1919 84451b3e 2018-07-10 stsp
1920 84451b3e 2018-07-10 stsp rewind(f);
1921 f7d12f7e 2018-08-01 stsp werase(view->window);
1922 84451b3e 2018-07-10 stsp
1923 ab089a2a 2018-07-12 stsp if (asprintf(&line, "commit: %s", id_str) == -1) {
1924 ab089a2a 2018-07-12 stsp err = got_error_from_errno();
1925 ab089a2a 2018-07-12 stsp free(id_str);
1926 ab089a2a 2018-07-12 stsp return err;
1927 ab089a2a 2018-07-12 stsp }
1928 ab089a2a 2018-07-12 stsp
1929 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1930 ab089a2a 2018-07-12 stsp free(line);
1931 2550e4c3 2018-07-13 stsp line = NULL;
1932 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1933 a3404814 2018-09-02 stsp wstandout(view->window);
1934 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1935 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1936 a3404814 2018-09-02 stsp wstandend(view->window);
1937 2550e4c3 2018-07-13 stsp free(wline);
1938 2550e4c3 2018-07-13 stsp wline = NULL;
1939 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1940 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1941 ab089a2a 2018-07-12 stsp
1942 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
1943 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
1944 12a0b23b 2018-07-12 stsp blame_complete ? "" : "annotating ", path) == -1) {
1945 ab089a2a 2018-07-12 stsp free(id_str);
1946 3f60a8ef 2018-07-10 stsp return got_error_from_errno();
1947 ab089a2a 2018-07-12 stsp }
1948 ab089a2a 2018-07-12 stsp free(id_str);
1949 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
1950 3f60a8ef 2018-07-10 stsp free(line);
1951 2550e4c3 2018-07-13 stsp line = NULL;
1952 3f60a8ef 2018-07-10 stsp if (err)
1953 3f60a8ef 2018-07-10 stsp return err;
1954 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
1955 2550e4c3 2018-07-13 stsp free(wline);
1956 2550e4c3 2018-07-13 stsp wline = NULL;
1957 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
1958 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
1959 3f60a8ef 2018-07-10 stsp
1960 84451b3e 2018-07-10 stsp *eof = 0;
1961 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
1962 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
1963 84451b3e 2018-07-10 stsp if (line == NULL) {
1964 84451b3e 2018-07-10 stsp *eof = 1;
1965 84451b3e 2018-07-10 stsp break;
1966 84451b3e 2018-07-10 stsp }
1967 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
1968 84451b3e 2018-07-10 stsp free(line);
1969 84451b3e 2018-07-10 stsp continue;
1970 84451b3e 2018-07-10 stsp }
1971 84451b3e 2018-07-10 stsp
1972 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1973 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
1974 84451b3e 2018-07-10 stsp if (err) {
1975 84451b3e 2018-07-10 stsp free(line);
1976 84451b3e 2018-07-10 stsp return err;
1977 84451b3e 2018-07-10 stsp }
1978 84451b3e 2018-07-10 stsp
1979 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
1980 f7d12f7e 2018-08-01 stsp wstandout(view->window);
1981 b700b5d6 2018-07-10 stsp
1982 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
1983 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
1984 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
1985 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
1986 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
1987 84451b3e 2018-07-10 stsp char *id_str;
1988 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
1989 84451b3e 2018-07-10 stsp if (err) {
1990 84451b3e 2018-07-10 stsp free(line);
1991 2550e4c3 2018-07-13 stsp free(wline);
1992 84451b3e 2018-07-10 stsp return err;
1993 84451b3e 2018-07-10 stsp }
1994 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
1995 84451b3e 2018-07-10 stsp free(id_str);
1996 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
1997 ee41ec32 2018-07-10 stsp } else {
1998 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
1999 ee41ec32 2018-07-10 stsp prev_id = NULL;
2000 ee41ec32 2018-07-10 stsp }
2001 84451b3e 2018-07-10 stsp
2002 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2003 b700b5d6 2018-07-10 stsp while (width < wlimit) {
2004 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
2005 b700b5d6 2018-07-10 stsp width++;
2006 b700b5d6 2018-07-10 stsp }
2007 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2008 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2009 84451b3e 2018-07-10 stsp if (++nprinted == 1)
2010 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
2011 84451b3e 2018-07-10 stsp free(line);
2012 2550e4c3 2018-07-13 stsp free(wline);
2013 2550e4c3 2018-07-13 stsp wline = NULL;
2014 84451b3e 2018-07-10 stsp }
2015 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
2016 84451b3e 2018-07-10 stsp
2017 1a57306a 2018-09-02 stsp view_vborder(view);
2018 84451b3e 2018-07-10 stsp
2019 84451b3e 2018-07-10 stsp return NULL;
2020 84451b3e 2018-07-10 stsp }
2021 84451b3e 2018-07-10 stsp
2022 84451b3e 2018-07-10 stsp static const struct got_error *
2023 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2024 84451b3e 2018-07-10 stsp {
2025 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
2026 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
2027 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
2028 84451b3e 2018-07-10 stsp
2029 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
2030 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2031 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
2032 84451b3e 2018-07-10 stsp
2033 84451b3e 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
2034 84451b3e 2018-07-10 stsp return got_error_from_errno();
2035 84451b3e 2018-07-10 stsp
2036 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
2037 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
2038 d68a0a7d 2018-07-10 stsp goto done;
2039 d68a0a7d 2018-07-10 stsp }
2040 d68a0a7d 2018-07-10 stsp
2041 d68a0a7d 2018-07-10 stsp if (lineno == -1)
2042 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
2043 d68a0a7d 2018-07-10 stsp
2044 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
2045 d68a0a7d 2018-07-10 stsp if (line->annotated)
2046 d68a0a7d 2018-07-10 stsp goto done;
2047 d68a0a7d 2018-07-10 stsp
2048 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
2049 84451b3e 2018-07-10 stsp if (line->id == NULL) {
2050 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2051 84451b3e 2018-07-10 stsp goto done;
2052 84451b3e 2018-07-10 stsp }
2053 84451b3e 2018-07-10 stsp line->annotated = 1;
2054 84451b3e 2018-07-10 stsp
2055 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
2056 245d91c1 2018-07-12 stsp a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
2057 e5a0f69f 2018-08-18 stsp a->last_displayed_line, a->eof, a->view->nlines);
2058 84451b3e 2018-07-10 stsp done:
2059 84451b3e 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0)
2060 84451b3e 2018-07-10 stsp return got_error_from_errno();
2061 84451b3e 2018-07-10 stsp return err;
2062 84451b3e 2018-07-10 stsp }
2063 84451b3e 2018-07-10 stsp
2064 84451b3e 2018-07-10 stsp static void *
2065 84451b3e 2018-07-10 stsp blame_thread(void *arg)
2066 84451b3e 2018-07-10 stsp {
2067 18430de3 2018-07-10 stsp const struct got_error *err;
2068 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
2069 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
2070 18430de3 2018-07-10 stsp
2071 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2072 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
2073 18430de3 2018-07-10 stsp
2074 18430de3 2018-07-10 stsp if (pthread_mutex_lock(a->mutex) != 0)
2075 18430de3 2018-07-10 stsp return (void *)got_error_from_errno();
2076 18430de3 2018-07-10 stsp
2077 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
2078 c9beca56 2018-07-22 stsp ta->repo = NULL;
2079 c9beca56 2018-07-22 stsp *ta->complete = 1;
2080 c9beca56 2018-07-22 stsp if (!err)
2081 f7d12f7e 2018-08-01 stsp err = draw_blame(a->view, a->commit_id, a->f, a->path,
2082 f7d12f7e 2018-08-01 stsp a->lines, a->nlines, 1, *a->selected_line,
2083 e5a0f69f 2018-08-18 stsp a->first_displayed_line, a->last_displayed_line, a->eof,
2084 f7d12f7e 2018-08-01 stsp a->view->nlines);
2085 18430de3 2018-07-10 stsp
2086 18430de3 2018-07-10 stsp if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
2087 18430de3 2018-07-10 stsp err = got_error_from_errno();
2088 18430de3 2018-07-10 stsp
2089 18430de3 2018-07-10 stsp return (void *)err;
2090 84451b3e 2018-07-10 stsp }
2091 84451b3e 2018-07-10 stsp
2092 245d91c1 2018-07-12 stsp static struct got_object_id *
2093 245d91c1 2018-07-12 stsp get_selected_commit_id(struct tog_blame_line *lines,
2094 245d91c1 2018-07-12 stsp int first_displayed_line, int selected_line)
2095 245d91c1 2018-07-12 stsp {
2096 245d91c1 2018-07-12 stsp struct tog_blame_line *line;
2097 b880a918 2018-07-10 stsp
2098 245d91c1 2018-07-12 stsp line = &lines[first_displayed_line - 1 + selected_line - 1];
2099 245d91c1 2018-07-12 stsp if (!line->annotated)
2100 245d91c1 2018-07-12 stsp return NULL;
2101 245d91c1 2018-07-12 stsp
2102 245d91c1 2018-07-12 stsp return line->id;
2103 245d91c1 2018-07-12 stsp }
2104 245d91c1 2018-07-12 stsp
2105 84451b3e 2018-07-10 stsp static const struct got_error *
2106 245d91c1 2018-07-12 stsp open_selected_commit(struct got_object **pobj, struct got_object **obj,
2107 b880a918 2018-07-10 stsp struct tog_blame_line *lines, int first_displayed_line,
2108 b880a918 2018-07-10 stsp int selected_line, struct got_repository *repo)
2109 b880a918 2018-07-10 stsp {
2110 b880a918 2018-07-10 stsp const struct got_error *err = NULL;
2111 b880a918 2018-07-10 stsp struct got_commit_object *commit = NULL;
2112 245d91c1 2018-07-12 stsp struct got_object_id *selected_id;
2113 b880a918 2018-07-10 stsp struct got_object_qid *pid;
2114 b880a918 2018-07-10 stsp
2115 b880a918 2018-07-10 stsp *pobj = NULL;
2116 b880a918 2018-07-10 stsp *obj = NULL;
2117 b880a918 2018-07-10 stsp
2118 245d91c1 2018-07-12 stsp selected_id = get_selected_commit_id(lines,
2119 245d91c1 2018-07-12 stsp first_displayed_line, selected_line);
2120 245d91c1 2018-07-12 stsp if (selected_id == NULL)
2121 b880a918 2018-07-10 stsp return NULL;
2122 b880a918 2018-07-10 stsp
2123 245d91c1 2018-07-12 stsp err = got_object_open(obj, repo, selected_id);
2124 b880a918 2018-07-10 stsp if (err)
2125 b880a918 2018-07-10 stsp goto done;
2126 b880a918 2018-07-10 stsp
2127 b880a918 2018-07-10 stsp err = got_object_commit_open(&commit, repo, *obj);
2128 b880a918 2018-07-10 stsp if (err)
2129 b880a918 2018-07-10 stsp goto done;
2130 b880a918 2018-07-10 stsp
2131 b880a918 2018-07-10 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
2132 b880a918 2018-07-10 stsp if (pid) {
2133 b880a918 2018-07-10 stsp err = got_object_open(pobj, repo, pid->id);
2134 b880a918 2018-07-10 stsp if (err)
2135 b880a918 2018-07-10 stsp goto done;
2136 b880a918 2018-07-10 stsp }
2137 b880a918 2018-07-10 stsp done:
2138 b880a918 2018-07-10 stsp if (commit)
2139 b880a918 2018-07-10 stsp got_object_commit_close(commit);
2140 b880a918 2018-07-10 stsp return err;
2141 b880a918 2018-07-10 stsp }
2142 245d91c1 2018-07-12 stsp
2143 b880a918 2018-07-10 stsp static const struct got_error *
2144 245d91c1 2018-07-12 stsp stop_blame(struct tog_blame *blame)
2145 a70480e0 2018-06-23 stsp {
2146 a70480e0 2018-06-23 stsp const struct got_error *err = NULL;
2147 245d91c1 2018-07-12 stsp int i;
2148 245d91c1 2018-07-12 stsp
2149 245d91c1 2018-07-12 stsp if (blame->thread) {
2150 245d91c1 2018-07-12 stsp if (pthread_join(blame->thread, (void **)&err) != 0)
2151 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2152 245d91c1 2018-07-12 stsp if (err && err->code == GOT_ERR_ITER_COMPLETED)
2153 245d91c1 2018-07-12 stsp err = NULL;
2154 245d91c1 2018-07-12 stsp blame->thread = NULL;
2155 245d91c1 2018-07-12 stsp }
2156 245d91c1 2018-07-12 stsp if (blame->thread_args.repo) {
2157 245d91c1 2018-07-12 stsp got_repo_close(blame->thread_args.repo);
2158 245d91c1 2018-07-12 stsp blame->thread_args.repo = NULL;
2159 245d91c1 2018-07-12 stsp }
2160 245d91c1 2018-07-12 stsp if (blame->f) {
2161 245d91c1 2018-07-12 stsp fclose(blame->f);
2162 245d91c1 2018-07-12 stsp blame->f = NULL;
2163 245d91c1 2018-07-12 stsp }
2164 245d91c1 2018-07-12 stsp for (i = 0; i < blame->nlines; i++)
2165 245d91c1 2018-07-12 stsp free(blame->lines[i].id);
2166 245d91c1 2018-07-12 stsp free(blame->lines);
2167 245d91c1 2018-07-12 stsp blame->lines = NULL;
2168 75c32340 2018-07-23 stsp free(blame->cb_args.commit_id);
2169 75c32340 2018-07-23 stsp blame->cb_args.commit_id = NULL;
2170 245d91c1 2018-07-12 stsp
2171 245d91c1 2018-07-12 stsp return err;
2172 245d91c1 2018-07-12 stsp }
2173 245d91c1 2018-07-12 stsp
2174 245d91c1 2018-07-12 stsp static const struct got_error *
2175 7cc84d77 2018-08-01 stsp run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2176 7cc84d77 2018-08-01 stsp struct tog_view *view, int *blame_complete,
2177 245d91c1 2018-07-12 stsp int *first_displayed_line, int *last_displayed_line,
2178 e5a0f69f 2018-08-18 stsp int *selected_line, int *done, int *eof, const char *path,
2179 245d91c1 2018-07-12 stsp struct got_object_id *commit_id,
2180 245d91c1 2018-07-12 stsp struct got_repository *repo)
2181 245d91c1 2018-07-12 stsp {
2182 245d91c1 2018-07-12 stsp const struct got_error *err = NULL;
2183 84451b3e 2018-07-10 stsp struct got_blob_object *blob = NULL;
2184 245d91c1 2018-07-12 stsp struct got_repository *thread_repo = NULL;
2185 27d434c2 2018-09-15 stsp struct got_object_id *obj_id = NULL;
2186 27d434c2 2018-09-15 stsp struct got_object *obj = NULL;
2187 a70480e0 2018-06-23 stsp
2188 27d434c2 2018-09-15 stsp err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2189 27d434c2 2018-09-15 stsp if (err)
2190 27d434c2 2018-09-15 stsp goto done;
2191 27d434c2 2018-09-15 stsp
2192 27d434c2 2018-09-15 stsp err = got_object_open(&obj, repo, obj_id);
2193 84451b3e 2018-07-10 stsp if (err)
2194 84451b3e 2018-07-10 stsp goto done;
2195 27d434c2 2018-09-15 stsp
2196 84451b3e 2018-07-10 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2197 84451b3e 2018-07-10 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2198 84451b3e 2018-07-10 stsp goto done;
2199 84451b3e 2018-07-10 stsp }
2200 a70480e0 2018-06-23 stsp
2201 84451b3e 2018-07-10 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
2202 a70480e0 2018-06-23 stsp if (err)
2203 a70480e0 2018-06-23 stsp goto done;
2204 245d91c1 2018-07-12 stsp blame->f = got_opentemp();
2205 245d91c1 2018-07-12 stsp if (blame->f == NULL) {
2206 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2207 84451b3e 2018-07-10 stsp goto done;
2208 84451b3e 2018-07-10 stsp }
2209 245d91c1 2018-07-12 stsp err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2210 245d91c1 2018-07-12 stsp blame->f, blob);
2211 84451b3e 2018-07-10 stsp if (err)
2212 84451b3e 2018-07-10 stsp goto done;
2213 a70480e0 2018-06-23 stsp
2214 245d91c1 2018-07-12 stsp blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2215 245d91c1 2018-07-12 stsp if (blame->lines == NULL) {
2216 84451b3e 2018-07-10 stsp err = got_error_from_errno();
2217 84451b3e 2018-07-10 stsp goto done;
2218 84451b3e 2018-07-10 stsp }
2219 a70480e0 2018-06-23 stsp
2220 245d91c1 2018-07-12 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2221 bd24772e 2018-07-11 stsp if (err)
2222 bd24772e 2018-07-11 stsp goto done;
2223 bd24772e 2018-07-11 stsp
2224 7cc84d77 2018-08-01 stsp blame->cb_args.view = view;
2225 245d91c1 2018-07-12 stsp blame->cb_args.lines = blame->lines;
2226 245d91c1 2018-07-12 stsp blame->cb_args.nlines = blame->nlines;
2227 245d91c1 2018-07-12 stsp blame->cb_args.mutex = mutex;
2228 245d91c1 2018-07-12 stsp blame->cb_args.commit_id = got_object_id_dup(commit_id);
2229 245d91c1 2018-07-12 stsp if (blame->cb_args.commit_id == NULL) {
2230 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2231 245d91c1 2018-07-12 stsp goto done;
2232 245d91c1 2018-07-12 stsp }
2233 245d91c1 2018-07-12 stsp blame->cb_args.f = blame->f;
2234 245d91c1 2018-07-12 stsp blame->cb_args.path = path;
2235 245d91c1 2018-07-12 stsp blame->cb_args.first_displayed_line = first_displayed_line;
2236 245d91c1 2018-07-12 stsp blame->cb_args.selected_line = selected_line;
2237 245d91c1 2018-07-12 stsp blame->cb_args.last_displayed_line = last_displayed_line;
2238 245d91c1 2018-07-12 stsp blame->cb_args.quit = done;
2239 e5a0f69f 2018-08-18 stsp blame->cb_args.eof = eof;
2240 245d91c1 2018-07-12 stsp
2241 245d91c1 2018-07-12 stsp blame->thread_args.path = path;
2242 245d91c1 2018-07-12 stsp blame->thread_args.repo = thread_repo;
2243 245d91c1 2018-07-12 stsp blame->thread_args.cb_args = &blame->cb_args;
2244 245d91c1 2018-07-12 stsp blame->thread_args.complete = blame_complete;
2245 245d91c1 2018-07-12 stsp *blame_complete = 0;
2246 245d91c1 2018-07-12 stsp
2247 245d91c1 2018-07-12 stsp if (pthread_create(&blame->thread, NULL, blame_thread,
2248 245d91c1 2018-07-12 stsp &blame->thread_args) != 0) {
2249 245d91c1 2018-07-12 stsp err = got_error_from_errno();
2250 245d91c1 2018-07-12 stsp goto done;
2251 245d91c1 2018-07-12 stsp }
2252 245d91c1 2018-07-12 stsp
2253 245d91c1 2018-07-12 stsp done:
2254 245d91c1 2018-07-12 stsp if (blob)
2255 245d91c1 2018-07-12 stsp got_object_blob_close(blob);
2256 27d434c2 2018-09-15 stsp free(obj_id);
2257 245d91c1 2018-07-12 stsp if (obj)
2258 245d91c1 2018-07-12 stsp got_object_close(obj);
2259 245d91c1 2018-07-12 stsp if (err)
2260 245d91c1 2018-07-12 stsp stop_blame(blame);
2261 245d91c1 2018-07-12 stsp return err;
2262 245d91c1 2018-07-12 stsp }
2263 245d91c1 2018-07-12 stsp
2264 245d91c1 2018-07-12 stsp static const struct got_error *
2265 e5a0f69f 2018-08-18 stsp open_blame_view(struct tog_view *view, char *path,
2266 e1cd8fed 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2267 245d91c1 2018-07-12 stsp {
2268 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL;
2269 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2270 dbc6a6b6 2018-07-12 stsp
2271 fb2756b9 2018-08-04 stsp SIMPLEQ_INIT(&s->blamed_commits);
2272 245d91c1 2018-07-12 stsp
2273 fb2756b9 2018-08-04 stsp if (pthread_mutex_init(&s->mutex, NULL) != 0)
2274 7cbe629d 2018-08-04 stsp return got_error_from_errno();
2275 245d91c1 2018-07-12 stsp
2276 fb2756b9 2018-08-04 stsp err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2277 dbc6a6b6 2018-07-12 stsp if (err)
2278 7cbe629d 2018-08-04 stsp return err;
2279 245d91c1 2018-07-12 stsp
2280 fb2756b9 2018-08-04 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2281 fb2756b9 2018-08-04 stsp s->first_displayed_line = 1;
2282 fb2756b9 2018-08-04 stsp s->last_displayed_line = view->nlines;
2283 fb2756b9 2018-08-04 stsp s->selected_line = 1;
2284 fb2756b9 2018-08-04 stsp s->blame_complete = 0;
2285 fb2756b9 2018-08-04 stsp s->path = path;
2286 e5a0f69f 2018-08-18 stsp if (s->path == NULL)
2287 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2288 fb2756b9 2018-08-04 stsp s->repo = repo;
2289 fb2756b9 2018-08-04 stsp s->commit_id = commit_id;
2290 e9424729 2018-08-04 stsp memset(&s->blame, 0, sizeof(s->blame));
2291 7cbe629d 2018-08-04 stsp
2292 e5a0f69f 2018-08-18 stsp view->show = show_blame_view;
2293 e5a0f69f 2018-08-18 stsp view->input = input_blame_view;
2294 e5a0f69f 2018-08-18 stsp view->close = close_blame_view;
2295 e5a0f69f 2018-08-18 stsp
2296 e5a0f69f 2018-08-18 stsp return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2297 e5a0f69f 2018-08-18 stsp &s->first_displayed_line, &s->last_displayed_line,
2298 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2299 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2300 7cbe629d 2018-08-04 stsp }
2301 7cbe629d 2018-08-04 stsp
2302 e5a0f69f 2018-08-18 stsp static const struct got_error *
2303 7cbe629d 2018-08-04 stsp close_blame_view(struct tog_view *view)
2304 7cbe629d 2018-08-04 stsp {
2305 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2306 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2307 7cbe629d 2018-08-04 stsp
2308 e5a0f69f 2018-08-18 stsp if (s->blame.thread)
2309 e5a0f69f 2018-08-18 stsp err = stop_blame(&s->blame);
2310 e5a0f69f 2018-08-18 stsp
2311 fb2756b9 2018-08-04 stsp while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2312 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
2313 fb2756b9 2018-08-04 stsp blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2314 fb2756b9 2018-08-04 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2315 7cbe629d 2018-08-04 stsp got_object_qid_free(blamed_commit);
2316 7cbe629d 2018-08-04 stsp }
2317 e5a0f69f 2018-08-18 stsp
2318 e5a0f69f 2018-08-18 stsp free(s->path);
2319 e5a0f69f 2018-08-18 stsp
2320 e5a0f69f 2018-08-18 stsp return err;
2321 7cbe629d 2018-08-04 stsp }
2322 7cbe629d 2018-08-04 stsp
2323 7cbe629d 2018-08-04 stsp static const struct got_error *
2324 7cbe629d 2018-08-04 stsp show_blame_view(struct tog_view *view)
2325 7cbe629d 2018-08-04 stsp {
2326 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2327 e5a0f69f 2018-08-18 stsp struct tog_blame_view_state *s = &view->state.blame;
2328 e5a0f69f 2018-08-18 stsp
2329 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0)
2330 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2331 e5a0f69f 2018-08-18 stsp
2332 e5a0f69f 2018-08-18 stsp err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2333 e5a0f69f 2018-08-18 stsp s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2334 e5a0f69f 2018-08-18 stsp s->selected_line, &s->first_displayed_line,
2335 e5a0f69f 2018-08-18 stsp &s->last_displayed_line, &s->eof, view->nlines);
2336 e5a0f69f 2018-08-18 stsp
2337 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2338 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2339 e5a0f69f 2018-08-18 stsp
2340 669b5ffa 2018-10-07 stsp view_vborder(view);
2341 e5a0f69f 2018-08-18 stsp return err;
2342 e5a0f69f 2018-08-18 stsp }
2343 e5a0f69f 2018-08-18 stsp
2344 e5a0f69f 2018-08-18 stsp static const struct got_error *
2345 878940b7 2018-09-29 stsp input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2346 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2347 e5a0f69f 2018-08-18 stsp {
2348 7cbe629d 2018-08-04 stsp const struct got_error *err = NULL, *thread_err = NULL;
2349 7cbe629d 2018-08-04 stsp struct got_object *obj = NULL, *pobj = NULL;
2350 7cbe629d 2018-08-04 stsp struct tog_view *diff_view;
2351 fb2756b9 2018-08-04 stsp struct tog_blame_view_state *s = &view->state.blame;
2352 669b5ffa 2018-10-07 stsp int begin_x = 0;
2353 7cbe629d 2018-08-04 stsp
2354 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2355 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2356 e5a0f69f 2018-08-18 stsp goto done;
2357 e5a0f69f 2018-08-18 stsp }
2358 a70480e0 2018-06-23 stsp
2359 e5a0f69f 2018-08-18 stsp switch (ch) {
2360 e5a0f69f 2018-08-18 stsp case 'q':
2361 e5a0f69f 2018-08-18 stsp s->done = 1;
2362 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2363 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2364 e5a0f69f 2018-08-18 stsp goto done;
2365 e5a0f69f 2018-08-18 stsp }
2366 e5a0f69f 2018-08-18 stsp return stop_blame(&s->blame);
2367 e5a0f69f 2018-08-18 stsp case 'k':
2368 e5a0f69f 2018-08-18 stsp case KEY_UP:
2369 e5a0f69f 2018-08-18 stsp if (s->selected_line > 1)
2370 e5a0f69f 2018-08-18 stsp s->selected_line--;
2371 e5a0f69f 2018-08-18 stsp else if (s->selected_line == 1 &&
2372 e5a0f69f 2018-08-18 stsp s->first_displayed_line > 1)
2373 e5a0f69f 2018-08-18 stsp s->first_displayed_line--;
2374 a70480e0 2018-06-23 stsp break;
2375 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
2376 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line == 1) {
2377 e5a0f69f 2018-08-18 stsp s->selected_line = 1;
2378 a70480e0 2018-06-23 stsp break;
2379 e5a0f69f 2018-08-18 stsp }
2380 e5a0f69f 2018-08-18 stsp if (s->first_displayed_line > view->nlines - 2)
2381 e5a0f69f 2018-08-18 stsp s->first_displayed_line -=
2382 e5a0f69f 2018-08-18 stsp (view->nlines - 2);
2383 e5a0f69f 2018-08-18 stsp else
2384 e5a0f69f 2018-08-18 stsp s->first_displayed_line = 1;
2385 e5a0f69f 2018-08-18 stsp break;
2386 e5a0f69f 2018-08-18 stsp case 'j':
2387 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
2388 e5a0f69f 2018-08-18 stsp if (s->selected_line < view->nlines - 2 &&
2389 e5a0f69f 2018-08-18 stsp s->first_displayed_line +
2390 e5a0f69f 2018-08-18 stsp s->selected_line <= s->blame.nlines)
2391 e5a0f69f 2018-08-18 stsp s->selected_line++;
2392 e5a0f69f 2018-08-18 stsp else if (s->last_displayed_line <
2393 e5a0f69f 2018-08-18 stsp s->blame.nlines)
2394 e5a0f69f 2018-08-18 stsp s->first_displayed_line++;
2395 e5a0f69f 2018-08-18 stsp break;
2396 e5a0f69f 2018-08-18 stsp case 'b':
2397 e5a0f69f 2018-08-18 stsp case 'p': {
2398 e5a0f69f 2018-08-18 stsp struct got_object_id *id;
2399 e5a0f69f 2018-08-18 stsp id = get_selected_commit_id(s->blame.lines,
2400 e5a0f69f 2018-08-18 stsp s->first_displayed_line, s->selected_line);
2401 e5a0f69f 2018-08-18 stsp if (id == NULL || got_object_id_cmp(id,
2402 e5a0f69f 2018-08-18 stsp s->blamed_commit->id) == 0)
2403 a70480e0 2018-06-23 stsp break;
2404 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2405 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2406 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2407 e5a0f69f 2018-08-18 stsp if (err)
2408 a70480e0 2018-06-23 stsp break;
2409 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2410 b700b5d6 2018-07-10 stsp break;
2411 e5a0f69f 2018-08-18 stsp if (ch == 'p' && pobj == NULL)
2412 dbc6a6b6 2018-07-12 stsp break;
2413 e5a0f69f 2018-08-18 stsp s->done = 1;
2414 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2415 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2416 e5a0f69f 2018-08-18 stsp goto done;
2417 dbc6a6b6 2018-07-12 stsp }
2418 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2419 e5a0f69f 2018-08-18 stsp s->done = 0;
2420 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2421 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2422 e5a0f69f 2018-08-18 stsp goto done;
2423 e5a0f69f 2018-08-18 stsp }
2424 e5a0f69f 2018-08-18 stsp if (thread_err)
2425 245d91c1 2018-07-12 stsp break;
2426 e5a0f69f 2018-08-18 stsp id = got_object_get_id(ch == 'b' ? obj : pobj);
2427 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2428 e5a0f69f 2018-08-18 stsp obj = NULL;
2429 e5a0f69f 2018-08-18 stsp if (pobj) {
2430 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2431 e5a0f69f 2018-08-18 stsp pobj = NULL;
2432 e5a0f69f 2018-08-18 stsp }
2433 6402fb3c 2018-09-15 stsp err = got_object_qid_alloc(&s->blamed_commit, id);
2434 e5a0f69f 2018-08-18 stsp if (err)
2435 e5a0f69f 2018-08-18 stsp goto done;
2436 e5a0f69f 2018-08-18 stsp SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2437 e5a0f69f 2018-08-18 stsp s->blamed_commit, entry);
2438 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2439 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2440 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2441 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2442 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof,
2443 e5a0f69f 2018-08-18 stsp s->path, s->blamed_commit->id, s->repo);
2444 e5a0f69f 2018-08-18 stsp if (err)
2445 a70480e0 2018-06-23 stsp break;
2446 e5a0f69f 2018-08-18 stsp break;
2447 84451b3e 2018-07-10 stsp }
2448 e5a0f69f 2018-08-18 stsp case 'B': {
2449 e5a0f69f 2018-08-18 stsp struct got_object_qid *first;
2450 e5a0f69f 2018-08-18 stsp first = SIMPLEQ_FIRST(&s->blamed_commits);
2451 e5a0f69f 2018-08-18 stsp if (!got_object_id_cmp(first->id, s->commit_id))
2452 e5a0f69f 2018-08-18 stsp break;
2453 e5a0f69f 2018-08-18 stsp s->done = 1;
2454 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0) {
2455 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2456 e5a0f69f 2018-08-18 stsp goto done;
2457 e5a0f69f 2018-08-18 stsp }
2458 e5a0f69f 2018-08-18 stsp thread_err = stop_blame(&s->blame);
2459 e5a0f69f 2018-08-18 stsp s->done = 0;
2460 e5a0f69f 2018-08-18 stsp if (pthread_mutex_lock(&s->mutex) != 0) {
2461 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2462 e5a0f69f 2018-08-18 stsp goto done;
2463 e5a0f69f 2018-08-18 stsp }
2464 e5a0f69f 2018-08-18 stsp if (thread_err)
2465 e5a0f69f 2018-08-18 stsp break;
2466 e5a0f69f 2018-08-18 stsp SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2467 e5a0f69f 2018-08-18 stsp got_object_qid_free(s->blamed_commit);
2468 e5a0f69f 2018-08-18 stsp s->blamed_commit =
2469 e5a0f69f 2018-08-18 stsp SIMPLEQ_FIRST(&s->blamed_commits);
2470 e5a0f69f 2018-08-18 stsp err = run_blame(&s->blame, &s->mutex, view,
2471 e5a0f69f 2018-08-18 stsp &s->blame_complete,
2472 e5a0f69f 2018-08-18 stsp &s->first_displayed_line,
2473 e5a0f69f 2018-08-18 stsp &s->last_displayed_line,
2474 e5a0f69f 2018-08-18 stsp &s->selected_line, &s->done, &s->eof, s->path,
2475 e5a0f69f 2018-08-18 stsp s->blamed_commit->id, s->repo);
2476 e5a0f69f 2018-08-18 stsp if (err)
2477 e5a0f69f 2018-08-18 stsp break;
2478 245d91c1 2018-07-12 stsp break;
2479 e5a0f69f 2018-08-18 stsp }
2480 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
2481 e5a0f69f 2018-08-18 stsp case '\r':
2482 e5a0f69f 2018-08-18 stsp err = open_selected_commit(&pobj, &obj,
2483 e5a0f69f 2018-08-18 stsp s->blame.lines, s->first_displayed_line,
2484 e5a0f69f 2018-08-18 stsp s->selected_line, s->repo);
2485 e5a0f69f 2018-08-18 stsp if (err)
2486 e5a0f69f 2018-08-18 stsp break;
2487 e5a0f69f 2018-08-18 stsp if (pobj == NULL && obj == NULL)
2488 e5a0f69f 2018-08-18 stsp break;
2489 669b5ffa 2018-10-07 stsp
2490 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
2491 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
2492 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2493 e5a0f69f 2018-08-18 stsp if (diff_view == NULL) {
2494 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2495 e5a0f69f 2018-08-18 stsp break;
2496 e5a0f69f 2018-08-18 stsp }
2497 669b5ffa 2018-10-07 stsp err = open_diff_view(diff_view, pobj, obj, s->repo);
2498 e5a0f69f 2018-08-18 stsp if (err) {
2499 e5a0f69f 2018-08-18 stsp view_close(diff_view);
2500 e5a0f69f 2018-08-18 stsp break;
2501 e5a0f69f 2018-08-18 stsp }
2502 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
2503 669b5ffa 2018-10-07 stsp err = view_close_child(view);
2504 669b5ffa 2018-10-07 stsp if (err)
2505 669b5ffa 2018-10-07 stsp return err;
2506 669b5ffa 2018-10-07 stsp err = view_set_child(view, diff_view);
2507 669b5ffa 2018-10-07 stsp if (err) {
2508 669b5ffa 2018-10-07 stsp view_close(diff_view);
2509 669b5ffa 2018-10-07 stsp break;
2510 669b5ffa 2018-10-07 stsp }
2511 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(diff_view)) {
2512 669b5ffa 2018-10-07 stsp *focus_view = diff_view;
2513 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
2514 669b5ffa 2018-10-07 stsp }
2515 669b5ffa 2018-10-07 stsp } else
2516 669b5ffa 2018-10-07 stsp *new_view = diff_view;
2517 e5a0f69f 2018-08-18 stsp if (pobj) {
2518 e5a0f69f 2018-08-18 stsp got_object_close(pobj);
2519 e5a0f69f 2018-08-18 stsp pobj = NULL;
2520 e5a0f69f 2018-08-18 stsp }
2521 e5a0f69f 2018-08-18 stsp got_object_close(obj);
2522 e5a0f69f 2018-08-18 stsp obj = NULL;
2523 e5a0f69f 2018-08-18 stsp if (err)
2524 e5a0f69f 2018-08-18 stsp break;
2525 e5a0f69f 2018-08-18 stsp break;
2526 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
2527 e5a0f69f 2018-08-18 stsp case ' ':
2528 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line >= s->blame.nlines &&
2529 e5a0f69f 2018-08-18 stsp s->selected_line < view->nlines - 2) {
2530 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2531 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2532 e5a0f69f 2018-08-18 stsp break;
2533 e5a0f69f 2018-08-18 stsp }
2534 e5a0f69f 2018-08-18 stsp if (s->last_displayed_line + view->nlines - 2
2535 e5a0f69f 2018-08-18 stsp <= s->blame.nlines)
2536 e5a0f69f 2018-08-18 stsp s->first_displayed_line +=
2537 e5a0f69f 2018-08-18 stsp view->nlines - 2;
2538 e5a0f69f 2018-08-18 stsp else
2539 e5a0f69f 2018-08-18 stsp s->first_displayed_line =
2540 e5a0f69f 2018-08-18 stsp s->blame.nlines -
2541 e5a0f69f 2018-08-18 stsp (view->nlines - 3);
2542 e5a0f69f 2018-08-18 stsp break;
2543 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
2544 e5a0f69f 2018-08-18 stsp if (s->selected_line > view->nlines - 2) {
2545 e5a0f69f 2018-08-18 stsp s->selected_line = MIN(s->blame.nlines,
2546 e5a0f69f 2018-08-18 stsp view->nlines - 2);
2547 e5a0f69f 2018-08-18 stsp }
2548 e5a0f69f 2018-08-18 stsp break;
2549 e5a0f69f 2018-08-18 stsp default:
2550 e5a0f69f 2018-08-18 stsp break;
2551 a70480e0 2018-06-23 stsp }
2552 e5a0f69f 2018-08-18 stsp
2553 e5a0f69f 2018-08-18 stsp if (pthread_mutex_unlock(&s->mutex) != 0)
2554 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
2555 a70480e0 2018-06-23 stsp done:
2556 b880a918 2018-07-10 stsp if (pobj)
2557 b880a918 2018-07-10 stsp got_object_close(pobj);
2558 245d91c1 2018-07-12 stsp return thread_err ? thread_err : err;
2559 a70480e0 2018-06-23 stsp }
2560 a70480e0 2018-06-23 stsp
2561 a70480e0 2018-06-23 stsp static const struct got_error *
2562 9f7d7167 2018-04-29 stsp cmd_blame(int argc, char *argv[])
2563 9f7d7167 2018-04-29 stsp {
2564 a70480e0 2018-06-23 stsp const struct got_error *error;
2565 a70480e0 2018-06-23 stsp struct got_repository *repo = NULL;
2566 69069811 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2567 a70480e0 2018-06-23 stsp struct got_object_id *commit_id = NULL;
2568 a70480e0 2018-06-23 stsp char *commit_id_str = NULL;
2569 a70480e0 2018-06-23 stsp int ch;
2570 e1cd8fed 2018-08-01 stsp struct tog_view *view;
2571 a70480e0 2018-06-23 stsp
2572 a70480e0 2018-06-23 stsp #ifndef PROFILE
2573 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2574 ad242220 2018-09-08 stsp == -1)
2575 a70480e0 2018-06-23 stsp err(1, "pledge");
2576 a70480e0 2018-06-23 stsp #endif
2577 a70480e0 2018-06-23 stsp
2578 69069811 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2579 a70480e0 2018-06-23 stsp switch (ch) {
2580 a70480e0 2018-06-23 stsp case 'c':
2581 a70480e0 2018-06-23 stsp commit_id_str = optarg;
2582 a70480e0 2018-06-23 stsp break;
2583 69069811 2018-08-02 stsp case 'r':
2584 69069811 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2585 69069811 2018-08-02 stsp if (repo_path == NULL)
2586 69069811 2018-08-02 stsp err(1, "-r option");
2587 69069811 2018-08-02 stsp break;
2588 a70480e0 2018-06-23 stsp default:
2589 a70480e0 2018-06-23 stsp usage();
2590 a70480e0 2018-06-23 stsp /* NOTREACHED */
2591 a70480e0 2018-06-23 stsp }
2592 a70480e0 2018-06-23 stsp }
2593 a70480e0 2018-06-23 stsp
2594 a70480e0 2018-06-23 stsp argc -= optind;
2595 a70480e0 2018-06-23 stsp argv += optind;
2596 a70480e0 2018-06-23 stsp
2597 69069811 2018-08-02 stsp if (argc == 1)
2598 69069811 2018-08-02 stsp path = argv[0];
2599 69069811 2018-08-02 stsp else
2600 a70480e0 2018-06-23 stsp usage_blame();
2601 69069811 2018-08-02 stsp
2602 69069811 2018-08-02 stsp cwd = getcwd(NULL, 0);
2603 69069811 2018-08-02 stsp if (cwd == NULL) {
2604 69069811 2018-08-02 stsp error = got_error_from_errno();
2605 69069811 2018-08-02 stsp goto done;
2606 69069811 2018-08-02 stsp }
2607 69069811 2018-08-02 stsp if (repo_path == NULL) {
2608 69069811 2018-08-02 stsp repo_path = strdup(cwd);
2609 69069811 2018-08-02 stsp if (repo_path == NULL) {
2610 69069811 2018-08-02 stsp error = got_error_from_errno();
2611 69069811 2018-08-02 stsp goto done;
2612 69069811 2018-08-02 stsp }
2613 69069811 2018-08-02 stsp }
2614 69069811 2018-08-02 stsp
2615 69069811 2018-08-02 stsp
2616 69069811 2018-08-02 stsp error = got_repo_open(&repo, repo_path);
2617 a70480e0 2018-06-23 stsp if (error != NULL)
2618 66b4983c 2018-06-23 stsp return error;
2619 69069811 2018-08-02 stsp
2620 69069811 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
2621 69069811 2018-08-02 stsp if (error != NULL)
2622 69069811 2018-08-02 stsp goto done;
2623 a70480e0 2018-06-23 stsp
2624 a70480e0 2018-06-23 stsp if (commit_id_str == NULL) {
2625 a70480e0 2018-06-23 stsp struct got_reference *head_ref;
2626 a70480e0 2018-06-23 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2627 a70480e0 2018-06-23 stsp if (error != NULL)
2628 66b4983c 2018-06-23 stsp goto done;
2629 a70480e0 2018-06-23 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2630 a70480e0 2018-06-23 stsp got_ref_close(head_ref);
2631 a70480e0 2018-06-23 stsp } else {
2632 a70480e0 2018-06-23 stsp struct got_object *obj;
2633 a70480e0 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2634 a70480e0 2018-06-23 stsp if (error != NULL)
2635 66b4983c 2018-06-23 stsp goto done;
2636 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
2637 a19e88aa 2018-06-23 stsp if (commit_id == NULL)
2638 66b4983c 2018-06-23 stsp error = got_error_from_errno();
2639 a19e88aa 2018-06-23 stsp got_object_close(obj);
2640 a70480e0 2018-06-23 stsp }
2641 a19e88aa 2018-06-23 stsp if (error != NULL)
2642 a19e88aa 2018-06-23 stsp goto done;
2643 a70480e0 2018-06-23 stsp
2644 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2645 e1cd8fed 2018-08-01 stsp if (view == NULL) {
2646 e1cd8fed 2018-08-01 stsp error = got_error_from_errno();
2647 e1cd8fed 2018-08-01 stsp goto done;
2648 e1cd8fed 2018-08-01 stsp }
2649 7cbe629d 2018-08-04 stsp error = open_blame_view(view, in_repo_path, commit_id, repo);
2650 7cbe629d 2018-08-04 stsp if (error)
2651 7cbe629d 2018-08-04 stsp goto done;
2652 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2653 a70480e0 2018-06-23 stsp done:
2654 69069811 2018-08-02 stsp free(repo_path);
2655 69069811 2018-08-02 stsp free(cwd);
2656 a70480e0 2018-06-23 stsp free(commit_id);
2657 a70480e0 2018-06-23 stsp if (repo)
2658 a70480e0 2018-06-23 stsp got_repo_close(repo);
2659 a70480e0 2018-06-23 stsp return error;
2660 ffd1d5e5 2018-06-23 stsp }
2661 ffd1d5e5 2018-06-23 stsp
2662 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2663 f7d12f7e 2018-08-01 stsp draw_tree_entries(struct tog_view *view,
2664 f7d12f7e 2018-08-01 stsp struct got_tree_entry **first_displayed_entry,
2665 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **last_displayed_entry,
2666 ffd1d5e5 2018-06-23 stsp struct got_tree_entry **selected_entry, int *ndisplayed,
2667 f7d12f7e 2018-08-01 stsp const char *label, int show_ids, const char *parent_path,
2668 ce52c690 2018-06-23 stsp const struct got_tree_entries *entries, int selected, int limit, int isroot)
2669 ffd1d5e5 2018-06-23 stsp {
2670 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2671 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te;
2672 ffd1d5e5 2018-06-23 stsp wchar_t *wline;
2673 ffd1d5e5 2018-06-23 stsp int width, n;
2674 ffd1d5e5 2018-06-23 stsp
2675 ffd1d5e5 2018-06-23 stsp *ndisplayed = 0;
2676 ffd1d5e5 2018-06-23 stsp
2677 f7d12f7e 2018-08-01 stsp werase(view->window);
2678 ffd1d5e5 2018-06-23 stsp
2679 ffd1d5e5 2018-06-23 stsp if (limit == 0)
2680 ffd1d5e5 2018-06-23 stsp return NULL;
2681 ffd1d5e5 2018-06-23 stsp
2682 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, label, view->ncols);
2683 ffd1d5e5 2018-06-23 stsp if (err)
2684 ffd1d5e5 2018-06-23 stsp return err;
2685 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2686 a3404814 2018-09-02 stsp wstandout(view->window);
2687 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2688 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2689 a3404814 2018-09-02 stsp wstandend(view->window);
2690 2550e4c3 2018-07-13 stsp free(wline);
2691 2550e4c3 2018-07-13 stsp wline = NULL;
2692 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2693 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2694 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2695 ffd1d5e5 2018-06-23 stsp return NULL;
2696 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, parent_path, view->ncols);
2697 ce52c690 2018-06-23 stsp if (err)
2698 ce52c690 2018-06-23 stsp return err;
2699 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2700 2550e4c3 2018-07-13 stsp free(wline);
2701 2550e4c3 2018-07-13 stsp wline = NULL;
2702 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2703 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2704 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2705 ffd1d5e5 2018-06-23 stsp return NULL;
2706 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2707 a1eca9bb 2018-06-23 stsp if (--limit <= 0)
2708 a1eca9bb 2018-06-23 stsp return NULL;
2709 ffd1d5e5 2018-06-23 stsp
2710 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2711 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL) {
2712 ffd1d5e5 2018-06-23 stsp if (selected == 0) {
2713 0cf4efb1 2018-09-29 stsp if (view->focussed)
2714 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2715 ffd1d5e5 2018-06-23 stsp *selected_entry = NULL;
2716 ffd1d5e5 2018-06-23 stsp }
2717 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ..\n"); /* parent directory */
2718 0cf4efb1 2018-09-29 stsp if (selected == 0 && view->focussed)
2719 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2720 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2721 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2722 ffd1d5e5 2018-06-23 stsp return NULL;
2723 ffd1d5e5 2018-06-23 stsp n = 1;
2724 ffd1d5e5 2018-06-23 stsp } else {
2725 ffd1d5e5 2018-06-23 stsp n = 0;
2726 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry)
2727 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2728 ffd1d5e5 2018-06-23 stsp }
2729 ffd1d5e5 2018-06-23 stsp
2730 ffd1d5e5 2018-06-23 stsp while (te) {
2731 1d13200f 2018-07-12 stsp char *line = NULL, *id_str = NULL;
2732 1d13200f 2018-07-12 stsp
2733 1d13200f 2018-07-12 stsp if (show_ids) {
2734 1d13200f 2018-07-12 stsp err = got_object_id_str(&id_str, te->id);
2735 1d13200f 2018-07-12 stsp if (err)
2736 1d13200f 2018-07-12 stsp return got_error_from_errno();
2737 1d13200f 2018-07-12 stsp }
2738 1d13200f 2018-07-12 stsp if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2739 1d13200f 2018-07-12 stsp te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2740 1d13200f 2018-07-12 stsp free(id_str);
2741 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2742 1d13200f 2018-07-12 stsp }
2743 1d13200f 2018-07-12 stsp free(id_str);
2744 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2745 ffd1d5e5 2018-06-23 stsp if (err) {
2746 ffd1d5e5 2018-06-23 stsp free(line);
2747 ffd1d5e5 2018-06-23 stsp break;
2748 ffd1d5e5 2018-06-23 stsp }
2749 ffd1d5e5 2018-06-23 stsp if (n == selected) {
2750 0cf4efb1 2018-09-29 stsp if (view->focussed)
2751 0cf4efb1 2018-09-29 stsp wstandout(view->window);
2752 ffd1d5e5 2018-06-23 stsp *selected_entry = te;
2753 ffd1d5e5 2018-06-23 stsp }
2754 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2755 f7d12f7e 2018-08-01 stsp if (width < view->ncols)
2756 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2757 0cf4efb1 2018-09-29 stsp if (n == selected && view->focussed)
2758 f7d12f7e 2018-08-01 stsp wstandend(view->window);
2759 ffd1d5e5 2018-06-23 stsp free(line);
2760 2550e4c3 2018-07-13 stsp free(wline);
2761 2550e4c3 2018-07-13 stsp wline = NULL;
2762 ffd1d5e5 2018-06-23 stsp n++;
2763 ffd1d5e5 2018-06-23 stsp (*ndisplayed)++;
2764 ffd1d5e5 2018-06-23 stsp *last_displayed_entry = te;
2765 ffd1d5e5 2018-06-23 stsp if (--limit <= 0)
2766 ffd1d5e5 2018-06-23 stsp break;
2767 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2768 ffd1d5e5 2018-06-23 stsp }
2769 ffd1d5e5 2018-06-23 stsp
2770 ffd1d5e5 2018-06-23 stsp return err;
2771 ffd1d5e5 2018-06-23 stsp }
2772 ffd1d5e5 2018-06-23 stsp
2773 ffd1d5e5 2018-06-23 stsp static void
2774 ffd1d5e5 2018-06-23 stsp tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2775 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries, int isroot)
2776 ffd1d5e5 2018-06-23 stsp {
2777 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *te, *prev;
2778 ffd1d5e5 2018-06-23 stsp int i;
2779 ffd1d5e5 2018-06-23 stsp
2780 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == NULL)
2781 ffd1d5e5 2018-06-23 stsp return;
2782 ffd1d5e5 2018-06-23 stsp
2783 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2784 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry == te) {
2785 ffd1d5e5 2018-06-23 stsp if (!isroot)
2786 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2787 ffd1d5e5 2018-06-23 stsp return;
2788 ffd1d5e5 2018-06-23 stsp }
2789 ffd1d5e5 2018-06-23 stsp
2790 ffd1d5e5 2018-06-23 stsp /* XXX this is stupid... switch to TAILQ? */
2791 ffd1d5e5 2018-06-23 stsp for (i = 0; i < maxscroll; i++) {
2792 ffd1d5e5 2018-06-23 stsp while (te != *first_displayed_entry) {
2793 ffd1d5e5 2018-06-23 stsp prev = te;
2794 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_NEXT(te, entry);
2795 ffd1d5e5 2018-06-23 stsp }
2796 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = prev;
2797 ffd1d5e5 2018-06-23 stsp te = SIMPLEQ_FIRST(&entries->head);
2798 ffd1d5e5 2018-06-23 stsp }
2799 ffd1d5e5 2018-06-23 stsp if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2800 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = NULL;
2801 ffd1d5e5 2018-06-23 stsp }
2802 ffd1d5e5 2018-06-23 stsp
2803 ffd1d5e5 2018-06-23 stsp static void
2804 ffd1d5e5 2018-06-23 stsp tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2805 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *last_displayed_entry,
2806 ffd1d5e5 2018-06-23 stsp const struct got_tree_entries *entries)
2807 ffd1d5e5 2018-06-23 stsp {
2808 ffd1d5e5 2018-06-23 stsp struct got_tree_entry *next;
2809 ffd1d5e5 2018-06-23 stsp int n = 0;
2810 ffd1d5e5 2018-06-23 stsp
2811 ffd1d5e5 2018-06-23 stsp if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2812 ffd1d5e5 2018-06-23 stsp return;
2813 ffd1d5e5 2018-06-23 stsp
2814 ffd1d5e5 2018-06-23 stsp if (*first_displayed_entry)
2815 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2816 ffd1d5e5 2018-06-23 stsp else
2817 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_FIRST(&entries->head);
2818 ffd1d5e5 2018-06-23 stsp while (next) {
2819 ffd1d5e5 2018-06-23 stsp *first_displayed_entry = next;
2820 ffd1d5e5 2018-06-23 stsp if (++n >= maxscroll)
2821 ffd1d5e5 2018-06-23 stsp break;
2822 ffd1d5e5 2018-06-23 stsp next = SIMPLEQ_NEXT(next, entry);
2823 ffd1d5e5 2018-06-23 stsp }
2824 ffd1d5e5 2018-06-23 stsp }
2825 ffd1d5e5 2018-06-23 stsp
2826 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2827 ce52c690 2018-06-23 stsp tree_entry_path(char **path, struct tog_parent_trees *parents,
2828 ce52c690 2018-06-23 stsp struct got_tree_entry *te)
2829 ffd1d5e5 2018-06-23 stsp {
2830 cb2ebc8a 2018-06-23 stsp const struct got_error *err = NULL;
2831 ffd1d5e5 2018-06-23 stsp struct tog_parent_tree *pt;
2832 ffd1d5e5 2018-06-23 stsp size_t len = 2; /* for leading slash and NUL */
2833 ffd1d5e5 2018-06-23 stsp
2834 d9765a41 2018-06-23 stsp TAILQ_FOREACH(pt, parents, entry)
2835 ffd1d5e5 2018-06-23 stsp len += strlen(pt->selected_entry->name) + 1 /* slash */;
2836 ce52c690 2018-06-23 stsp if (te)
2837 ce52c690 2018-06-23 stsp len += strlen(te->name);
2838 ce52c690 2018-06-23 stsp
2839 ce52c690 2018-06-23 stsp *path = calloc(1, len);
2840 ffd1d5e5 2018-06-23 stsp if (path == NULL)
2841 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
2842 ffd1d5e5 2018-06-23 stsp
2843 ce52c690 2018-06-23 stsp (*path)[0] = '/';
2844 d9765a41 2018-06-23 stsp pt = TAILQ_LAST(parents, tog_parent_trees);
2845 d9765a41 2018-06-23 stsp while (pt) {
2846 ce52c690 2018-06-23 stsp if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2847 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2848 cb2ebc8a 2018-06-23 stsp goto done;
2849 cb2ebc8a 2018-06-23 stsp }
2850 ce52c690 2018-06-23 stsp if (strlcat(*path, "/", len) >= len) {
2851 cb2ebc8a 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2852 cb2ebc8a 2018-06-23 stsp goto done;
2853 cb2ebc8a 2018-06-23 stsp }
2854 d9765a41 2018-06-23 stsp pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2855 ffd1d5e5 2018-06-23 stsp }
2856 ce52c690 2018-06-23 stsp if (te) {
2857 ce52c690 2018-06-23 stsp if (strlcat(*path, te->name, len) >= len) {
2858 ce52c690 2018-06-23 stsp err = got_error(GOT_ERR_NO_SPACE);
2859 ce52c690 2018-06-23 stsp goto done;
2860 ce52c690 2018-06-23 stsp }
2861 cb2ebc8a 2018-06-23 stsp }
2862 ce52c690 2018-06-23 stsp done:
2863 ce52c690 2018-06-23 stsp if (err) {
2864 ce52c690 2018-06-23 stsp free(*path);
2865 ce52c690 2018-06-23 stsp *path = NULL;
2866 ce52c690 2018-06-23 stsp }
2867 ce52c690 2018-06-23 stsp return err;
2868 ce52c690 2018-06-23 stsp }
2869 ce52c690 2018-06-23 stsp
2870 ce52c690 2018-06-23 stsp static const struct got_error *
2871 0cf4efb1 2018-09-29 stsp blame_tree_entry(struct tog_view **new_view, int begin_x,
2872 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2873 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2874 ce52c690 2018-06-23 stsp {
2875 ce52c690 2018-06-23 stsp const struct got_error *err = NULL;
2876 ce52c690 2018-06-23 stsp char *path;
2877 e5a0f69f 2018-08-18 stsp struct tog_view *blame_view;
2878 69efd4c4 2018-07-18 stsp
2879 ce52c690 2018-06-23 stsp err = tree_entry_path(&path, parents, te);
2880 ce52c690 2018-06-23 stsp if (err)
2881 ce52c690 2018-06-23 stsp return err;
2882 ffd1d5e5 2018-06-23 stsp
2883 669b5ffa 2018-10-07 stsp blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
2884 e5a0f69f 2018-08-18 stsp if (blame_view == NULL)
2885 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2886 cdf1ee82 2018-08-01 stsp
2887 e5a0f69f 2018-08-18 stsp err = open_blame_view(blame_view, path, commit_id, repo);
2888 e5a0f69f 2018-08-18 stsp if (err) {
2889 e5a0f69f 2018-08-18 stsp view_close(blame_view);
2890 e5a0f69f 2018-08-18 stsp free(path);
2891 e5a0f69f 2018-08-18 stsp } else
2892 e5a0f69f 2018-08-18 stsp *new_view = blame_view;
2893 69efd4c4 2018-07-18 stsp return err;
2894 69efd4c4 2018-07-18 stsp }
2895 69efd4c4 2018-07-18 stsp
2896 69efd4c4 2018-07-18 stsp static const struct got_error *
2897 669b5ffa 2018-10-07 stsp log_tree_entry(struct tog_view **new_view, int begin_x,
2898 e5a0f69f 2018-08-18 stsp struct got_tree_entry *te, struct tog_parent_trees *parents,
2899 e5a0f69f 2018-08-18 stsp struct got_object_id *commit_id, struct got_repository *repo)
2900 69efd4c4 2018-07-18 stsp {
2901 e5a0f69f 2018-08-18 stsp struct tog_view *log_view;
2902 69efd4c4 2018-07-18 stsp const struct got_error *err = NULL;
2903 69efd4c4 2018-07-18 stsp char *path;
2904 69efd4c4 2018-07-18 stsp
2905 669b5ffa 2018-10-07 stsp log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
2906 e5a0f69f 2018-08-18 stsp if (log_view == NULL)
2907 e5a0f69f 2018-08-18 stsp return got_error_from_errno();
2908 e5a0f69f 2018-08-18 stsp
2909 69efd4c4 2018-07-18 stsp err = tree_entry_path(&path, parents, te);
2910 69efd4c4 2018-07-18 stsp if (err)
2911 69efd4c4 2018-07-18 stsp return err;
2912 69efd4c4 2018-07-18 stsp
2913 e5a0f69f 2018-08-18 stsp err = open_log_view(log_view, commit_id, repo, path);
2914 ba4f502b 2018-08-04 stsp if (err)
2915 e5a0f69f 2018-08-18 stsp view_close(log_view);
2916 e5a0f69f 2018-08-18 stsp else
2917 e5a0f69f 2018-08-18 stsp *new_view = log_view;
2918 cb2ebc8a 2018-06-23 stsp free(path);
2919 cb2ebc8a 2018-06-23 stsp return err;
2920 ffd1d5e5 2018-06-23 stsp }
2921 ffd1d5e5 2018-06-23 stsp
2922 ffd1d5e5 2018-06-23 stsp static const struct got_error *
2923 ad80ab7b 2018-08-04 stsp open_tree_view(struct tog_view *view, struct got_tree_object *root,
2924 5221c383 2018-08-01 stsp struct got_object_id *commit_id, struct got_repository *repo)
2925 ffd1d5e5 2018-06-23 stsp {
2926 ffd1d5e5 2018-06-23 stsp const struct got_error *err = NULL;
2927 ad80ab7b 2018-08-04 stsp char *commit_id_str = NULL;
2928 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2929 ffd1d5e5 2018-06-23 stsp
2930 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->parents);
2931 ffd1d5e5 2018-06-23 stsp
2932 ffd1d5e5 2018-06-23 stsp err = got_object_id_str(&commit_id_str, commit_id);
2933 ffd1d5e5 2018-06-23 stsp if (err != NULL)
2934 ffd1d5e5 2018-06-23 stsp goto done;
2935 ffd1d5e5 2018-06-23 stsp
2936 fb2756b9 2018-08-04 stsp if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2937 ffd1d5e5 2018-06-23 stsp err = got_error_from_errno();
2938 ffd1d5e5 2018-06-23 stsp goto done;
2939 ffd1d5e5 2018-06-23 stsp }
2940 ffd1d5e5 2018-06-23 stsp
2941 fb2756b9 2018-08-04 stsp s->root = s->tree = root;
2942 fb2756b9 2018-08-04 stsp s->entries = got_object_tree_get_entries(root);
2943 fb2756b9 2018-08-04 stsp s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2944 6484ec90 2018-09-29 stsp s->commit_id = got_object_id_dup(commit_id);
2945 6484ec90 2018-09-29 stsp if (s->commit_id == NULL) {
2946 6484ec90 2018-09-29 stsp err = got_error_from_errno();
2947 6484ec90 2018-09-29 stsp goto done;
2948 6484ec90 2018-09-29 stsp }
2949 fb2756b9 2018-08-04 stsp s->repo = repo;
2950 e5a0f69f 2018-08-18 stsp
2951 e5a0f69f 2018-08-18 stsp view->show = show_tree_view;
2952 e5a0f69f 2018-08-18 stsp view->input = input_tree_view;
2953 e5a0f69f 2018-08-18 stsp view->close = close_tree_view;
2954 ad80ab7b 2018-08-04 stsp done:
2955 ad80ab7b 2018-08-04 stsp free(commit_id_str);
2956 6484ec90 2018-09-29 stsp if (err) {
2957 fb2756b9 2018-08-04 stsp free(s->tree_label);
2958 6484ec90 2018-09-29 stsp s->tree_label = NULL;
2959 6484ec90 2018-09-29 stsp }
2960 ad80ab7b 2018-08-04 stsp return err;
2961 ad80ab7b 2018-08-04 stsp }
2962 ad80ab7b 2018-08-04 stsp
2963 e5a0f69f 2018-08-18 stsp static const struct got_error *
2964 ad80ab7b 2018-08-04 stsp close_tree_view(struct tog_view *view)
2965 ad80ab7b 2018-08-04 stsp {
2966 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2967 ad80ab7b 2018-08-04 stsp
2968 fb2756b9 2018-08-04 stsp free(s->tree_label);
2969 6484ec90 2018-09-29 stsp s->tree_label = NULL;
2970 6484ec90 2018-09-29 stsp free(s->commit_id);
2971 6484ec90 2018-09-29 stsp s->commit_id = NULL;
2972 fb2756b9 2018-08-04 stsp while (!TAILQ_EMPTY(&s->parents)) {
2973 ad80ab7b 2018-08-04 stsp struct tog_parent_tree *parent;
2974 fb2756b9 2018-08-04 stsp parent = TAILQ_FIRST(&s->parents);
2975 fb2756b9 2018-08-04 stsp TAILQ_REMOVE(&s->parents, parent, entry);
2976 ad80ab7b 2018-08-04 stsp free(parent);
2977 ad80ab7b 2018-08-04 stsp
2978 ad80ab7b 2018-08-04 stsp }
2979 fb2756b9 2018-08-04 stsp if (s->tree != s->root)
2980 fb2756b9 2018-08-04 stsp got_object_tree_close(s->tree);
2981 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->root);
2982 e5a0f69f 2018-08-18 stsp
2983 e5a0f69f 2018-08-18 stsp return NULL;
2984 ad80ab7b 2018-08-04 stsp }
2985 ad80ab7b 2018-08-04 stsp
2986 ad80ab7b 2018-08-04 stsp static const struct got_error *
2987 ad80ab7b 2018-08-04 stsp show_tree_view(struct tog_view *view)
2988 ad80ab7b 2018-08-04 stsp {
2989 ad80ab7b 2018-08-04 stsp const struct got_error *err = NULL;
2990 fb2756b9 2018-08-04 stsp struct tog_tree_view_state *s = &view->state.tree;
2991 e5a0f69f 2018-08-18 stsp char *parent_path;
2992 ad80ab7b 2018-08-04 stsp
2993 e5a0f69f 2018-08-18 stsp err = tree_entry_path(&parent_path, &s->parents, NULL);
2994 e5a0f69f 2018-08-18 stsp if (err)
2995 e5a0f69f 2018-08-18 stsp return err;
2996 ffd1d5e5 2018-06-23 stsp
2997 e5a0f69f 2018-08-18 stsp err = draw_tree_entries(view, &s->first_displayed_entry,
2998 e5a0f69f 2018-08-18 stsp &s->last_displayed_entry, &s->selected_entry,
2999 e5a0f69f 2018-08-18 stsp &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3000 e5a0f69f 2018-08-18 stsp s->entries, s->selected, view->nlines, s->tree == s->root);
3001 e5a0f69f 2018-08-18 stsp free(parent_path);
3002 669b5ffa 2018-10-07 stsp
3003 669b5ffa 2018-10-07 stsp view_vborder(view);
3004 e5a0f69f 2018-08-18 stsp return err;
3005 e5a0f69f 2018-08-18 stsp }
3006 ce52c690 2018-06-23 stsp
3007 e5a0f69f 2018-08-18 stsp static const struct got_error *
3008 e5a0f69f 2018-08-18 stsp input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3009 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
3010 e5a0f69f 2018-08-18 stsp {
3011 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
3012 e5a0f69f 2018-08-18 stsp struct tog_tree_view_state *s = &view->state.tree;
3013 669b5ffa 2018-10-07 stsp struct tog_view *log_view;
3014 669b5ffa 2018-10-07 stsp int begin_x = 0;
3015 ffd1d5e5 2018-06-23 stsp
3016 e5a0f69f 2018-08-18 stsp switch (ch) {
3017 e5a0f69f 2018-08-18 stsp case 'i':
3018 e5a0f69f 2018-08-18 stsp s->show_ids = !s->show_ids;
3019 ffd1d5e5 2018-06-23 stsp break;
3020 e5a0f69f 2018-08-18 stsp case 'l':
3021 669b5ffa 2018-10-07 stsp if (!s->selected_entry)
3022 669b5ffa 2018-10-07 stsp break;
3023 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view))
3024 669b5ffa 2018-10-07 stsp begin_x = view_split_begin_x(view->begin_x);
3025 669b5ffa 2018-10-07 stsp err = log_tree_entry(&log_view, begin_x,
3026 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents,
3027 669b5ffa 2018-10-07 stsp s->commit_id, s->repo);
3028 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3029 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3030 669b5ffa 2018-10-07 stsp if (err)
3031 669b5ffa 2018-10-07 stsp return err;
3032 669b5ffa 2018-10-07 stsp err = view_set_child(view, log_view);
3033 669b5ffa 2018-10-07 stsp if (err) {
3034 669b5ffa 2018-10-07 stsp view_close(log_view);
3035 669b5ffa 2018-10-07 stsp break;
3036 669b5ffa 2018-10-07 stsp }
3037 669b5ffa 2018-10-07 stsp *focus_view = log_view;
3038 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3039 669b5ffa 2018-10-07 stsp } else
3040 669b5ffa 2018-10-07 stsp *new_view = log_view;
3041 e5a0f69f 2018-08-18 stsp break;
3042 e5a0f69f 2018-08-18 stsp case 'k':
3043 e5a0f69f 2018-08-18 stsp case KEY_UP:
3044 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3045 e5a0f69f 2018-08-18 stsp s->selected--;
3046 e5a0f69f 2018-08-18 stsp if (s->selected > 0)
3047 ffd1d5e5 2018-06-23 stsp break;
3048 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry, 1,
3049 e5a0f69f 2018-08-18 stsp s->entries, s->tree == s->root);
3050 e5a0f69f 2018-08-18 stsp break;
3051 e5a0f69f 2018-08-18 stsp case KEY_PPAGE:
3052 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_FIRST(&s->entries->head) ==
3053 e5a0f69f 2018-08-18 stsp s->first_displayed_entry) {
3054 e5a0f69f 2018-08-18 stsp if (s->tree != s->root)
3055 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3056 e5a0f69f 2018-08-18 stsp s->selected = 0;
3057 69efd4c4 2018-07-18 stsp break;
3058 e5a0f69f 2018-08-18 stsp }
3059 e5a0f69f 2018-08-18 stsp tree_scroll_up(&s->first_displayed_entry,
3060 e5a0f69f 2018-08-18 stsp view->nlines, s->entries,
3061 e5a0f69f 2018-08-18 stsp s->tree == s->root);
3062 e5a0f69f 2018-08-18 stsp break;
3063 e5a0f69f 2018-08-18 stsp case 'j':
3064 e5a0f69f 2018-08-18 stsp case KEY_DOWN:
3065 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1) {
3066 e5a0f69f 2018-08-18 stsp s->selected++;
3067 1d13200f 2018-07-12 stsp break;
3068 e5a0f69f 2018-08-18 stsp }
3069 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry, 1,
3070 e5a0f69f 2018-08-18 stsp s->last_displayed_entry, s->entries);
3071 e5a0f69f 2018-08-18 stsp break;
3072 e5a0f69f 2018-08-18 stsp case KEY_NPAGE:
3073 e5a0f69f 2018-08-18 stsp tree_scroll_down(&s->first_displayed_entry,
3074 e5a0f69f 2018-08-18 stsp view->nlines, s->last_displayed_entry,
3075 e5a0f69f 2018-08-18 stsp s->entries);
3076 e5a0f69f 2018-08-18 stsp if (SIMPLEQ_NEXT(s->last_displayed_entry,
3077 e5a0f69f 2018-08-18 stsp entry))
3078 ffd1d5e5 2018-06-23 stsp break;
3079 e5a0f69f 2018-08-18 stsp /* can't scroll any further; move cursor down */
3080 e5a0f69f 2018-08-18 stsp if (s->selected < s->ndisplayed - 1)
3081 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3082 e5a0f69f 2018-08-18 stsp break;
3083 e5a0f69f 2018-08-18 stsp case KEY_ENTER:
3084 e5a0f69f 2018-08-18 stsp case '\r':
3085 e5a0f69f 2018-08-18 stsp if (s->selected_entry == NULL) {
3086 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3087 7837eeac 2018-09-24 stsp case KEY_BACKSPACE:
3088 e5a0f69f 2018-08-18 stsp /* user selected '..' */
3089 e5a0f69f 2018-08-18 stsp if (s->tree == s->root)
3090 ffd1d5e5 2018-06-23 stsp break;
3091 e5a0f69f 2018-08-18 stsp parent = TAILQ_FIRST(&s->parents);
3092 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&s->parents, parent,
3093 e5a0f69f 2018-08-18 stsp entry);
3094 e5a0f69f 2018-08-18 stsp got_object_tree_close(s->tree);
3095 e5a0f69f 2018-08-18 stsp s->tree = parent->tree;
3096 e5a0f69f 2018-08-18 stsp s->entries =
3097 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3098 e5a0f69f 2018-08-18 stsp s->first_displayed_entry =
3099 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry;
3100 e5a0f69f 2018-08-18 stsp s->selected_entry =
3101 e5a0f69f 2018-08-18 stsp parent->selected_entry;
3102 e5a0f69f 2018-08-18 stsp s->selected = parent->selected;
3103 e5a0f69f 2018-08-18 stsp free(parent);
3104 e5a0f69f 2018-08-18 stsp } else if (S_ISDIR(s->selected_entry->mode)) {
3105 e5a0f69f 2018-08-18 stsp struct tog_parent_tree *parent;
3106 e5a0f69f 2018-08-18 stsp struct got_tree_object *child;
3107 e5a0f69f 2018-08-18 stsp err = got_object_open_as_tree(&child,
3108 e5a0f69f 2018-08-18 stsp s->repo, s->selected_entry->id);
3109 e5a0f69f 2018-08-18 stsp if (err)
3110 ffd1d5e5 2018-06-23 stsp break;
3111 e5a0f69f 2018-08-18 stsp parent = calloc(1, sizeof(*parent));
3112 e5a0f69f 2018-08-18 stsp if (parent == NULL) {
3113 e5a0f69f 2018-08-18 stsp err = got_error_from_errno();
3114 e5a0f69f 2018-08-18 stsp break;
3115 ffd1d5e5 2018-06-23 stsp }
3116 e5a0f69f 2018-08-18 stsp parent->tree = s->tree;
3117 e5a0f69f 2018-08-18 stsp parent->first_displayed_entry =
3118 e5a0f69f 2018-08-18 stsp s->first_displayed_entry;
3119 e5a0f69f 2018-08-18 stsp parent->selected_entry = s->selected_entry;
3120 e5a0f69f 2018-08-18 stsp parent->selected = s->selected;
3121 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3122 e5a0f69f 2018-08-18 stsp s->tree = child;
3123 e5a0f69f 2018-08-18 stsp s->entries =
3124 e5a0f69f 2018-08-18 stsp got_object_tree_get_entries(s->tree);
3125 e5a0f69f 2018-08-18 stsp s->selected = 0;
3126 e5a0f69f 2018-08-18 stsp s->first_displayed_entry = NULL;
3127 e5a0f69f 2018-08-18 stsp } else if (S_ISREG(s->selected_entry->mode)) {
3128 669b5ffa 2018-10-07 stsp struct tog_view *blame_view;
3129 669b5ffa 2018-10-07 stsp int begin_x = view_is_parent_view(view) ?
3130 669b5ffa 2018-10-07 stsp view_split_begin_x(view->begin_x) : 0;
3131 669b5ffa 2018-10-07 stsp
3132 669b5ffa 2018-10-07 stsp err = blame_tree_entry(&blame_view, begin_x,
3133 669b5ffa 2018-10-07 stsp s->selected_entry, &s->parents, s->commit_id,
3134 669b5ffa 2018-10-07 stsp s->repo);
3135 e5a0f69f 2018-08-18 stsp if (err)
3136 ffd1d5e5 2018-06-23 stsp break;
3137 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
3138 669b5ffa 2018-10-07 stsp err = view_close_child(view);
3139 669b5ffa 2018-10-07 stsp if (err)
3140 669b5ffa 2018-10-07 stsp return err;
3141 669b5ffa 2018-10-07 stsp err = view_set_child(view, blame_view);
3142 669b5ffa 2018-10-07 stsp if (err) {
3143 669b5ffa 2018-10-07 stsp view_close(blame_view);
3144 669b5ffa 2018-10-07 stsp break;
3145 669b5ffa 2018-10-07 stsp }
3146 669b5ffa 2018-10-07 stsp *focus_view = blame_view;
3147 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
3148 669b5ffa 2018-10-07 stsp } else
3149 669b5ffa 2018-10-07 stsp *new_view = blame_view;
3150 e5a0f69f 2018-08-18 stsp }
3151 e5a0f69f 2018-08-18 stsp break;
3152 e5a0f69f 2018-08-18 stsp case KEY_RESIZE:
3153 e5a0f69f 2018-08-18 stsp if (s->selected > view->nlines)
3154 e5a0f69f 2018-08-18 stsp s->selected = s->ndisplayed - 1;
3155 e5a0f69f 2018-08-18 stsp break;
3156 e5a0f69f 2018-08-18 stsp default:
3157 e5a0f69f 2018-08-18 stsp break;
3158 ffd1d5e5 2018-06-23 stsp }
3159 e5a0f69f 2018-08-18 stsp
3160 ffd1d5e5 2018-06-23 stsp return err;
3161 9f7d7167 2018-04-29 stsp }
3162 9f7d7167 2018-04-29 stsp
3163 ffd1d5e5 2018-06-23 stsp __dead static void
3164 ffd1d5e5 2018-06-23 stsp usage_tree(void)
3165 ffd1d5e5 2018-06-23 stsp {
3166 ffd1d5e5 2018-06-23 stsp endwin();
3167 ffd1d5e5 2018-06-23 stsp fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3168 ffd1d5e5 2018-06-23 stsp getprogname());
3169 ffd1d5e5 2018-06-23 stsp exit(1);
3170 ffd1d5e5 2018-06-23 stsp }
3171 ffd1d5e5 2018-06-23 stsp
3172 ffd1d5e5 2018-06-23 stsp static const struct got_error *
3173 ffd1d5e5 2018-06-23 stsp cmd_tree(int argc, char *argv[])
3174 ffd1d5e5 2018-06-23 stsp {
3175 ffd1d5e5 2018-06-23 stsp const struct got_error *error;
3176 ffd1d5e5 2018-06-23 stsp struct got_repository *repo = NULL;
3177 ffd1d5e5 2018-06-23 stsp char *repo_path = NULL;
3178 ffd1d5e5 2018-06-23 stsp struct got_object_id *commit_id = NULL;
3179 ffd1d5e5 2018-06-23 stsp char *commit_id_arg = NULL;
3180 ffd1d5e5 2018-06-23 stsp struct got_commit_object *commit = NULL;
3181 ffd1d5e5 2018-06-23 stsp struct got_tree_object *tree = NULL;
3182 ffd1d5e5 2018-06-23 stsp int ch;
3183 5221c383 2018-08-01 stsp struct tog_view *view;
3184 ffd1d5e5 2018-06-23 stsp
3185 ffd1d5e5 2018-06-23 stsp #ifndef PROFILE
3186 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3187 ad242220 2018-09-08 stsp == -1)
3188 ffd1d5e5 2018-06-23 stsp err(1, "pledge");
3189 ffd1d5e5 2018-06-23 stsp #endif
3190 ffd1d5e5 2018-06-23 stsp
3191 ffd1d5e5 2018-06-23 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
3192 ffd1d5e5 2018-06-23 stsp switch (ch) {
3193 ffd1d5e5 2018-06-23 stsp case 'c':
3194 ffd1d5e5 2018-06-23 stsp commit_id_arg = optarg;
3195 ffd1d5e5 2018-06-23 stsp break;
3196 ffd1d5e5 2018-06-23 stsp default:
3197 ffd1d5e5 2018-06-23 stsp usage();
3198 ffd1d5e5 2018-06-23 stsp /* NOTREACHED */
3199 ffd1d5e5 2018-06-23 stsp }
3200 ffd1d5e5 2018-06-23 stsp }
3201 ffd1d5e5 2018-06-23 stsp
3202 ffd1d5e5 2018-06-23 stsp argc -= optind;
3203 ffd1d5e5 2018-06-23 stsp argv += optind;
3204 ffd1d5e5 2018-06-23 stsp
3205 ffd1d5e5 2018-06-23 stsp if (argc == 0) {
3206 ffd1d5e5 2018-06-23 stsp repo_path = getcwd(NULL, 0);
3207 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3208 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3209 ffd1d5e5 2018-06-23 stsp } else if (argc == 1) {
3210 ffd1d5e5 2018-06-23 stsp repo_path = realpath(argv[0], NULL);
3211 ffd1d5e5 2018-06-23 stsp if (repo_path == NULL)
3212 ffd1d5e5 2018-06-23 stsp return got_error_from_errno();
3213 ffd1d5e5 2018-06-23 stsp } else
3214 ffd1d5e5 2018-06-23 stsp usage_log();
3215 ffd1d5e5 2018-06-23 stsp
3216 ffd1d5e5 2018-06-23 stsp error = got_repo_open(&repo, repo_path);
3217 ffd1d5e5 2018-06-23 stsp free(repo_path);
3218 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3219 ffd1d5e5 2018-06-23 stsp return error;
3220 ffd1d5e5 2018-06-23 stsp
3221 ffd1d5e5 2018-06-23 stsp if (commit_id_arg == NULL) {
3222 ffd1d5e5 2018-06-23 stsp error = get_head_commit_id(&commit_id, repo);
3223 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3224 ffd1d5e5 2018-06-23 stsp goto done;
3225 ffd1d5e5 2018-06-23 stsp } else {
3226 ffd1d5e5 2018-06-23 stsp struct got_object *obj;
3227 ffd1d5e5 2018-06-23 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3228 ffd1d5e5 2018-06-23 stsp if (error == NULL) {
3229 6402fb3c 2018-09-15 stsp commit_id = got_object_id_dup(got_object_get_id(obj));
3230 ffd1d5e5 2018-06-23 stsp if (commit_id == NULL)
3231 ffd1d5e5 2018-06-23 stsp error = got_error_from_errno();
3232 ffd1d5e5 2018-06-23 stsp }
3233 ffd1d5e5 2018-06-23 stsp }
3234 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3235 ffd1d5e5 2018-06-23 stsp goto done;
3236 ffd1d5e5 2018-06-23 stsp
3237 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3238 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3239 ffd1d5e5 2018-06-23 stsp goto done;
3240 ffd1d5e5 2018-06-23 stsp
3241 ffd1d5e5 2018-06-23 stsp error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3242 ffd1d5e5 2018-06-23 stsp if (error != NULL)
3243 ffd1d5e5 2018-06-23 stsp goto done;
3244 ffd1d5e5 2018-06-23 stsp
3245 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3246 5221c383 2018-08-01 stsp if (view == NULL) {
3247 5221c383 2018-08-01 stsp error = got_error_from_errno();
3248 5221c383 2018-08-01 stsp goto done;
3249 5221c383 2018-08-01 stsp }
3250 ad80ab7b 2018-08-04 stsp error = open_tree_view(view, tree, commit_id, repo);
3251 ad80ab7b 2018-08-04 stsp if (error)
3252 ad80ab7b 2018-08-04 stsp goto done;
3253 e5a0f69f 2018-08-18 stsp error = view_loop(view);
3254 ffd1d5e5 2018-06-23 stsp done:
3255 ffd1d5e5 2018-06-23 stsp free(commit_id);
3256 ffd1d5e5 2018-06-23 stsp if (commit)
3257 ffd1d5e5 2018-06-23 stsp got_object_commit_close(commit);
3258 ffd1d5e5 2018-06-23 stsp if (tree)
3259 ffd1d5e5 2018-06-23 stsp got_object_tree_close(tree);
3260 ffd1d5e5 2018-06-23 stsp if (repo)
3261 ffd1d5e5 2018-06-23 stsp got_repo_close(repo);
3262 ffd1d5e5 2018-06-23 stsp return error;
3263 ffd1d5e5 2018-06-23 stsp }
3264 5c5136c5 2018-05-20 stsp static void
3265 9f7d7167 2018-04-29 stsp init_curses(void)
3266 9f7d7167 2018-04-29 stsp {
3267 9f7d7167 2018-04-29 stsp initscr();
3268 9f7d7167 2018-04-29 stsp cbreak();
3269 9f7d7167 2018-04-29 stsp noecho();
3270 9f7d7167 2018-04-29 stsp nonl();
3271 9f7d7167 2018-04-29 stsp intrflush(stdscr, FALSE);
3272 9f7d7167 2018-04-29 stsp keypad(stdscr, TRUE);
3273 1f475ad8 2018-05-10 stsp curs_set(0);
3274 9f7d7167 2018-04-29 stsp }
3275 9f7d7167 2018-04-29 stsp
3276 4ed7e80c 2018-05-20 stsp __dead static void
3277 9f7d7167 2018-04-29 stsp usage(void)
3278 9f7d7167 2018-04-29 stsp {
3279 9f7d7167 2018-04-29 stsp int i;
3280 9f7d7167 2018-04-29 stsp
3281 c2301be8 2018-04-30 stsp fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3282 9f7d7167 2018-04-29 stsp "Available commands:\n", getprogname());
3283 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3284 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = &tog_commands[i];
3285 c2301be8 2018-04-30 stsp fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3286 9f7d7167 2018-04-29 stsp }
3287 9f7d7167 2018-04-29 stsp exit(1);
3288 9f7d7167 2018-04-29 stsp }
3289 9f7d7167 2018-04-29 stsp
3290 c2301be8 2018-04-30 stsp static char **
3291 c2301be8 2018-04-30 stsp make_argv(const char *arg0, const char *arg1)
3292 c2301be8 2018-04-30 stsp {
3293 c2301be8 2018-04-30 stsp char **argv;
3294 c2301be8 2018-04-30 stsp int argc = (arg1 == NULL ? 1 : 2);
3295 c2301be8 2018-04-30 stsp
3296 c2301be8 2018-04-30 stsp argv = calloc(argc, sizeof(char *));
3297 c2301be8 2018-04-30 stsp if (argv == NULL)
3298 c2301be8 2018-04-30 stsp err(1, "calloc");
3299 c2301be8 2018-04-30 stsp argv[0] = strdup(arg0);
3300 c2301be8 2018-04-30 stsp if (argv[0] == NULL)
3301 c2301be8 2018-04-30 stsp err(1, "calloc");
3302 c2301be8 2018-04-30 stsp if (arg1) {
3303 c2301be8 2018-04-30 stsp argv[1] = strdup(arg1);
3304 c2301be8 2018-04-30 stsp if (argv[1] == NULL)
3305 c2301be8 2018-04-30 stsp err(1, "calloc");
3306 c2301be8 2018-04-30 stsp }
3307 c2301be8 2018-04-30 stsp
3308 c2301be8 2018-04-30 stsp return argv;
3309 c2301be8 2018-04-30 stsp }
3310 c2301be8 2018-04-30 stsp
3311 9f7d7167 2018-04-29 stsp int
3312 9f7d7167 2018-04-29 stsp main(int argc, char *argv[])
3313 9f7d7167 2018-04-29 stsp {
3314 9f7d7167 2018-04-29 stsp const struct got_error *error = NULL;
3315 9f7d7167 2018-04-29 stsp struct tog_cmd *cmd = NULL;
3316 9f7d7167 2018-04-29 stsp int ch, hflag = 0;
3317 c2301be8 2018-04-30 stsp char **cmd_argv = NULL;
3318 9f7d7167 2018-04-29 stsp
3319 9f7d7167 2018-04-29 stsp setlocale(LC_ALL, "");
3320 9f7d7167 2018-04-29 stsp
3321 9f7d7167 2018-04-29 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
3322 9f7d7167 2018-04-29 stsp switch (ch) {
3323 9f7d7167 2018-04-29 stsp case 'h':
3324 9f7d7167 2018-04-29 stsp hflag = 1;
3325 9f7d7167 2018-04-29 stsp break;
3326 9f7d7167 2018-04-29 stsp default:
3327 9f7d7167 2018-04-29 stsp usage();
3328 9f7d7167 2018-04-29 stsp /* NOTREACHED */
3329 9f7d7167 2018-04-29 stsp }
3330 9f7d7167 2018-04-29 stsp }
3331 9f7d7167 2018-04-29 stsp
3332 9f7d7167 2018-04-29 stsp argc -= optind;
3333 9f7d7167 2018-04-29 stsp argv += optind;
3334 9f7d7167 2018-04-29 stsp optind = 0;
3335 c2301be8 2018-04-30 stsp optreset = 1;
3336 9f7d7167 2018-04-29 stsp
3337 c2301be8 2018-04-30 stsp if (argc == 0) {
3338 f29d3e89 2018-06-23 stsp if (hflag)
3339 f29d3e89 2018-06-23 stsp usage();
3340 c2301be8 2018-04-30 stsp /* Build an argument vector which runs a default command. */
3341 9f7d7167 2018-04-29 stsp cmd = &tog_commands[0];
3342 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, NULL);
3343 c2301be8 2018-04-30 stsp argc = 1;
3344 c2301be8 2018-04-30 stsp } else {
3345 9f7d7167 2018-04-29 stsp int i;
3346 9f7d7167 2018-04-29 stsp
3347 c2301be8 2018-04-30 stsp /* Did the user specific a command? */
3348 9f7d7167 2018-04-29 stsp for (i = 0; i < nitems(tog_commands); i++) {
3349 c2301be8 2018-04-30 stsp if (strncmp(tog_commands[i].name, argv[0],
3350 9f7d7167 2018-04-29 stsp strlen(argv[0])) == 0) {
3351 9f7d7167 2018-04-29 stsp cmd = &tog_commands[i];
3352 9f7d7167 2018-04-29 stsp if (hflag)
3353 9f7d7167 2018-04-29 stsp tog_commands[i].cmd_usage();
3354 9f7d7167 2018-04-29 stsp break;
3355 9f7d7167 2018-04-29 stsp }
3356 9f7d7167 2018-04-29 stsp }
3357 9f7d7167 2018-04-29 stsp if (cmd == NULL) {
3358 c2301be8 2018-04-30 stsp /* Did the user specify a repository? */
3359 c2301be8 2018-04-30 stsp char *repo_path = realpath(argv[0], NULL);
3360 c2301be8 2018-04-30 stsp if (repo_path) {
3361 c2301be8 2018-04-30 stsp struct got_repository *repo;
3362 c2301be8 2018-04-30 stsp error = got_repo_open(&repo, repo_path);
3363 c2301be8 2018-04-30 stsp if (error == NULL)
3364 c2301be8 2018-04-30 stsp got_repo_close(repo);
3365 c2301be8 2018-04-30 stsp } else
3366 ad7de8d9 2018-04-30 stsp error = got_error_from_errno();
3367 c2301be8 2018-04-30 stsp if (error) {
3368 f29d3e89 2018-06-23 stsp if (hflag) {
3369 f29d3e89 2018-06-23 stsp fprintf(stderr, "%s: '%s' is not a "
3370 f29d3e89 2018-06-23 stsp "known command\n", getprogname(),
3371 f29d3e89 2018-06-23 stsp argv[0]);
3372 f29d3e89 2018-06-23 stsp usage();
3373 f29d3e89 2018-06-23 stsp }
3374 ad7de8d9 2018-04-30 stsp fprintf(stderr, "%s: '%s' is neither a known "
3375 ad7de8d9 2018-04-30 stsp "command nor a path to a repository\n",
3376 c2301be8 2018-04-30 stsp getprogname(), argv[0]);
3377 ad7de8d9 2018-04-30 stsp free(repo_path);
3378 c2301be8 2018-04-30 stsp return 1;
3379 c2301be8 2018-04-30 stsp }
3380 c2301be8 2018-04-30 stsp cmd = &tog_commands[0];
3381 c2301be8 2018-04-30 stsp cmd_argv = make_argv(cmd->name, repo_path);
3382 c2301be8 2018-04-30 stsp argc = 2;
3383 c2301be8 2018-04-30 stsp free(repo_path);
3384 9f7d7167 2018-04-29 stsp }
3385 9f7d7167 2018-04-29 stsp }
3386 9f7d7167 2018-04-29 stsp
3387 5c5136c5 2018-05-20 stsp init_curses();
3388 9f7d7167 2018-04-29 stsp
3389 c2301be8 2018-04-30 stsp error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3390 9f7d7167 2018-04-29 stsp if (error)
3391 9f7d7167 2018-04-29 stsp goto done;
3392 9f7d7167 2018-04-29 stsp done:
3393 9f7d7167 2018-04-29 stsp endwin();
3394 c2301be8 2018-04-30 stsp free(cmd_argv);
3395 9f7d7167 2018-04-29 stsp if (error)
3396 9f7d7167 2018-04-29 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3397 9f7d7167 2018-04-29 stsp return 0;
3398 9f7d7167 2018-04-29 stsp }