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