Blame


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