Blame


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