Blame


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