2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
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>
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>
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 9ba79e04 2018-06-11 stsp #include "got_commit_graph.h"
49 00dfcb92 2018-06-11 stsp #include "got_utf8.h"
50 a70480e0 2018-06-23 stsp #include "got_blame.h"
51 c2db6724 2019-01-04 stsp #include "got_privsep.h"
52 1dd54920 2019-05-11 stsp #include "got_path.h"
53 b7165be3 2019-02-05 stsp #include "got_worktree.h"
56 881b2d3e 2018-04-30 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 2bd27830 2018-10-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
63 a4292ac5 2019-05-12 jcs #define CTRL(x) ((x) & 0x1f)
65 9f7d7167 2018-04-29 stsp #ifndef nitems
66 9f7d7167 2018-04-29 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 9f7d7167 2018-04-29 stsp struct tog_cmd {
70 c2301be8 2018-04-30 stsp const char *name;
71 9f7d7167 2018-04-29 stsp const struct got_error *(*cmd_main)(int, char *[]);
72 9f7d7167 2018-04-29 stsp void (*cmd_usage)(void);
75 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
76 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
77 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
78 4ed7e80c 2018-05-20 stsp __dead static void usage_blame(void);
79 ffd1d5e5 2018-06-23 stsp __dead static void usage_tree(void);
81 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
82 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
83 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_blame(int, char *[]);
84 ffd1d5e5 2018-06-23 stsp static const struct got_error* cmd_tree(int, char *[]);
86 4ed7e80c 2018-05-20 stsp static struct tog_cmd tog_commands[] = {
87 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
88 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
89 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
90 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
93 d6b05b5b 2018-08-04 stsp enum tog_view_type {
94 d6b05b5b 2018-08-04 stsp TOG_VIEW_DIFF,
95 d6b05b5b 2018-08-04 stsp TOG_VIEW_LOG,
96 ad80ab7b 2018-08-04 stsp TOG_VIEW_BLAME,
97 ad80ab7b 2018-08-04 stsp TOG_VIEW_TREE
100 c3e9aa98 2019-05-13 jcs #define TOG_EOF_STRING "(END)"
102 ba4f502b 2018-08-04 stsp struct commit_queue_entry {
103 ba4f502b 2018-08-04 stsp TAILQ_ENTRY(commit_queue_entry) entry;
104 ba4f502b 2018-08-04 stsp struct got_object_id *id;
105 ba4f502b 2018-08-04 stsp struct got_commit_object *commit;
108 ba4f502b 2018-08-04 stsp TAILQ_HEAD(commit_queue_head, commit_queue_entry);
109 ba4f502b 2018-08-04 stsp struct commit_queue {
110 ba4f502b 2018-08-04 stsp int ncommits;
111 ba4f502b 2018-08-04 stsp struct commit_queue_head head;
114 15a087fe 2019-02-21 stsp struct tog_diff_view_state {
115 15a087fe 2019-02-21 stsp struct got_object_id *id1, *id2;
117 15a087fe 2019-02-21 stsp int first_displayed_line;
118 15a087fe 2019-02-21 stsp int last_displayed_line;
120 15a087fe 2019-02-21 stsp int diff_context;
121 15a087fe 2019-02-21 stsp struct got_repository *repo;
122 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
124 15a087fe 2019-02-21 stsp /* passed from log view; may be NULL */
125 fb872ab2 2019-02-21 stsp struct tog_view *log_view;
128 1a76625f 2018-10-22 stsp pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
130 1a76625f 2018-10-22 stsp struct tog_log_thread_args {
131 1a76625f 2018-10-22 stsp pthread_cond_t need_commits;
132 1a76625f 2018-10-22 stsp int commits_needed;
133 b01e7d3b 2018-08-04 stsp struct got_commit_graph *graph;
134 1a76625f 2018-10-22 stsp struct commit_queue *commits;
135 1a76625f 2018-10-22 stsp const char *in_repo_path;
136 1a76625f 2018-10-22 stsp struct got_object_id *start_id;
137 1a76625f 2018-10-22 stsp struct got_repository *repo;
138 1a76625f 2018-10-22 stsp int log_complete;
139 1a76625f 2018-10-22 stsp sig_atomic_t *quit;
140 1a76625f 2018-10-22 stsp struct tog_view *view;
141 1a76625f 2018-10-22 stsp struct commit_queue_entry **first_displayed_entry;
142 1a76625f 2018-10-22 stsp struct commit_queue_entry **selected_entry;
145 1a76625f 2018-10-22 stsp struct tog_log_view_state {
146 b01e7d3b 2018-08-04 stsp struct commit_queue commits;
147 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *first_displayed_entry;
148 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *last_displayed_entry;
149 b01e7d3b 2018-08-04 stsp struct commit_queue_entry *selected_entry;
150 b01e7d3b 2018-08-04 stsp int selected;
151 b01e7d3b 2018-08-04 stsp char *in_repo_path;
152 d01904d4 2019-06-25 stsp const char *head_ref_name;
153 b01e7d3b 2018-08-04 stsp struct got_repository *repo;
154 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
155 5036bf37 2018-09-24 stsp struct got_object_id *start_id;
156 1a76625f 2018-10-22 stsp sig_atomic_t quit;
157 1a76625f 2018-10-22 stsp pthread_t thread;
158 1a76625f 2018-10-22 stsp struct tog_log_thread_args thread_args;
159 60493ae3 2019-06-20 stsp struct commit_queue_entry *matched_entry;
160 96e2b566 2019-07-08 stsp struct commit_queue_entry *search_entry;
163 e9424729 2018-08-04 stsp struct tog_blame_cb_args {
164 e9424729 2018-08-04 stsp struct tog_blame_line *lines; /* one per line */
165 e9424729 2018-08-04 stsp int nlines;
167 e9424729 2018-08-04 stsp struct tog_view *view;
168 e9424729 2018-08-04 stsp struct got_object_id *commit_id;
172 e9424729 2018-08-04 stsp struct tog_blame_thread_args {
173 e9424729 2018-08-04 stsp const char *path;
174 e9424729 2018-08-04 stsp struct got_repository *repo;
175 e9424729 2018-08-04 stsp struct tog_blame_cb_args *cb_args;
176 e9424729 2018-08-04 stsp int *complete;
179 e9424729 2018-08-04 stsp struct tog_blame {
181 e9424729 2018-08-04 stsp size_t filesize;
182 e9424729 2018-08-04 stsp struct tog_blame_line *lines;
183 6fcac457 2018-11-19 stsp int nlines;
184 6c4c42e0 2019-06-24 stsp off_t *line_offsets;
185 e9424729 2018-08-04 stsp pthread_t thread;
186 e9424729 2018-08-04 stsp struct tog_blame_thread_args thread_args;
187 e9424729 2018-08-04 stsp struct tog_blame_cb_args cb_args;
188 e9424729 2018-08-04 stsp const char *path;
191 7cbe629d 2018-08-04 stsp struct tog_blame_view_state {
192 7cbe629d 2018-08-04 stsp int first_displayed_line;
193 7cbe629d 2018-08-04 stsp int last_displayed_line;
194 7cbe629d 2018-08-04 stsp int selected_line;
195 7cbe629d 2018-08-04 stsp int blame_complete;
198 7cbe629d 2018-08-04 stsp struct got_object_id_queue blamed_commits;
199 7cbe629d 2018-08-04 stsp struct got_object_qid *blamed_commit;
200 e5a0f69f 2018-08-18 stsp char *path;
201 7cbe629d 2018-08-04 stsp struct got_repository *repo;
202 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
203 ad80ab7b 2018-08-04 stsp struct got_object_id *commit_id;
204 e9424729 2018-08-04 stsp struct tog_blame blame;
205 6c4c42e0 2019-06-24 stsp int matched_line;
208 ad80ab7b 2018-08-04 stsp struct tog_parent_tree {
209 ad80ab7b 2018-08-04 stsp TAILQ_ENTRY(tog_parent_tree) entry;
210 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
211 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
212 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
213 ad80ab7b 2018-08-04 stsp int selected;
216 ad80ab7b 2018-08-04 stsp TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
218 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state {
219 ad80ab7b 2018-08-04 stsp char *tree_label;
220 ad80ab7b 2018-08-04 stsp struct got_tree_object *root;
221 ad80ab7b 2018-08-04 stsp struct got_tree_object *tree;
222 ad80ab7b 2018-08-04 stsp const struct got_tree_entries *entries;
223 ad80ab7b 2018-08-04 stsp struct got_tree_entry *first_displayed_entry;
224 ad80ab7b 2018-08-04 stsp struct got_tree_entry *last_displayed_entry;
225 ad80ab7b 2018-08-04 stsp struct got_tree_entry *selected_entry;
226 416a95c5 2019-01-24 stsp int ndisplayed, selected, show_ids;
227 ad80ab7b 2018-08-04 stsp struct tog_parent_trees parents;
228 7cbe629d 2018-08-04 stsp struct got_object_id *commit_id;
229 ad80ab7b 2018-08-04 stsp struct got_repository *repo;
230 8b473291 2019-02-21 stsp struct got_reflist_head *refs;
231 7c32bd05 2019-06-22 stsp struct got_tree_entry *matched_entry;
235 669b5ffa 2018-10-07 stsp * We implement two types of views: parent views and child views.
237 669b5ffa 2018-10-07 stsp * The 'Tab' key switches between a parent view and its child view.
238 669b5ffa 2018-10-07 stsp * Child views are shown side-by-side to their parent view, provided
239 669b5ffa 2018-10-07 stsp * there is enough screen estate.
241 669b5ffa 2018-10-07 stsp * When a new view is opened from within a parent view, this new view
242 669b5ffa 2018-10-07 stsp * becomes a child view of the parent view, replacing any existing child.
244 669b5ffa 2018-10-07 stsp * When a new view is opened from within a child view, this new view
245 669b5ffa 2018-10-07 stsp * becomes a parent view which will obscure the views below until the
246 669b5ffa 2018-10-07 stsp * user quits the new parent view by typing 'q'.
248 669b5ffa 2018-10-07 stsp * This list of views contains parent views only.
249 669b5ffa 2018-10-07 stsp * Child views are only pointed to by their parent view.
251 bcbd79e2 2018-08-19 stsp TAILQ_HEAD(tog_view_list_head, tog_view);
253 cc3c9aac 2018-08-01 stsp struct tog_view {
254 e5a0f69f 2018-08-18 stsp TAILQ_ENTRY(tog_view) entry;
255 26ed57b2 2018-05-19 stsp WINDOW *window;
256 26ed57b2 2018-05-19 stsp PANEL *panel;
257 97ddc146 2018-08-01 stsp int nlines, ncols, begin_y, begin_x;
258 f7d12f7e 2018-08-01 stsp int lines, cols; /* copies of LINES and COLS */
259 1004088d 2018-09-29 stsp int focussed;
260 669b5ffa 2018-10-07 stsp struct tog_view *parent;
261 669b5ffa 2018-10-07 stsp struct tog_view *child;
262 669b5ffa 2018-10-07 stsp int child_focussed;
264 5dc9f4bc 2018-08-04 stsp /* type-specific state */
265 d6b05b5b 2018-08-04 stsp enum tog_view_type type;
267 b01e7d3b 2018-08-04 stsp struct tog_diff_view_state diff;
268 b01e7d3b 2018-08-04 stsp struct tog_log_view_state log;
269 7cbe629d 2018-08-04 stsp struct tog_blame_view_state blame;
270 ad80ab7b 2018-08-04 stsp struct tog_tree_view_state tree;
273 e5a0f69f 2018-08-18 stsp const struct got_error *(*show)(struct tog_view *);
274 e5a0f69f 2018-08-18 stsp const struct got_error *(*input)(struct tog_view **,
275 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view**, struct tog_view *, int);
276 e5a0f69f 2018-08-18 stsp const struct got_error *(*close)(struct tog_view *);
278 60493ae3 2019-06-20 stsp const struct got_error *(*search_start)(struct tog_view *);
279 60493ae3 2019-06-20 stsp const struct got_error *(*search_next)(struct tog_view *);
280 60493ae3 2019-06-20 stsp int searching;
281 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_FORWARD 1
282 b1bf1435 2019-06-21 stsp #define TOG_SEARCH_BACKWARD 2
283 60493ae3 2019-06-20 stsp int search_next_done;
284 1803e47f 2019-06-22 stsp regex_t regex;
287 ba4f502b 2018-08-04 stsp static const struct got_error *open_diff_view(struct tog_view *,
288 fb872ab2 2019-02-21 stsp struct got_object_id *, struct got_object_id *, struct tog_view *,
289 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
290 5dc9f4bc 2018-08-04 stsp static const struct got_error *show_diff_view(struct tog_view *);
291 e5a0f69f 2018-08-18 stsp static const struct got_error *input_diff_view(struct tog_view **,
292 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
293 e5a0f69f 2018-08-18 stsp static const struct got_error* close_diff_view(struct tog_view *);
295 ba4f502b 2018-08-04 stsp static const struct got_error *open_log_view(struct tog_view *,
296 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *,
297 d01904d4 2019-06-25 stsp struct got_repository *, const char *, const char *, int);
298 ba4f502b 2018-08-04 stsp static const struct got_error * show_log_view(struct tog_view *);
299 e5a0f69f 2018-08-18 stsp static const struct got_error *input_log_view(struct tog_view **,
300 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
301 e5a0f69f 2018-08-18 stsp static const struct got_error *close_log_view(struct tog_view *);
302 60493ae3 2019-06-20 stsp static const struct got_error *search_start_log_view(struct tog_view *);
303 60493ae3 2019-06-20 stsp static const struct got_error *search_next_log_view(struct tog_view *);
305 e5a0f69f 2018-08-18 stsp static const struct got_error *open_blame_view(struct tog_view *, char *,
306 8b473291 2019-02-21 stsp struct got_object_id *, struct got_reflist_head *, struct got_repository *);
307 7cbe629d 2018-08-04 stsp static const struct got_error *show_blame_view(struct tog_view *);
308 e5a0f69f 2018-08-18 stsp static const struct got_error *input_blame_view(struct tog_view **,
309 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
310 e5a0f69f 2018-08-18 stsp static const struct got_error *close_blame_view(struct tog_view *);
311 6c4c42e0 2019-06-24 stsp static const struct got_error *search_start_blame_view(struct tog_view *);
312 6c4c42e0 2019-06-24 stsp static const struct got_error *search_next_blame_view(struct tog_view *);
314 ad80ab7b 2018-08-04 stsp static const struct got_error *open_tree_view(struct tog_view *,
315 8b473291 2019-02-21 stsp struct got_tree_object *, struct got_object_id *,
316 8b473291 2019-02-21 stsp struct got_reflist_head *, struct got_repository *);
317 ad80ab7b 2018-08-04 stsp static const struct got_error *show_tree_view(struct tog_view *);
318 e5a0f69f 2018-08-18 stsp static const struct got_error *input_tree_view(struct tog_view **,
319 878940b7 2018-09-29 stsp struct tog_view **, struct tog_view **, struct tog_view *, int);
320 e5a0f69f 2018-08-18 stsp static const struct got_error *close_tree_view(struct tog_view *);
321 7c32bd05 2019-06-22 stsp static const struct got_error *search_start_tree_view(struct tog_view *);
322 7c32bd05 2019-06-22 stsp static const struct got_error *search_next_tree_view(struct tog_view *);
324 25791caa 2018-10-24 stsp static volatile sig_atomic_t tog_sigwinch_received;
326 25791caa 2018-10-24 stsp static void
327 25791caa 2018-10-24 stsp tog_sigwinch(int signo)
329 25791caa 2018-10-24 stsp tog_sigwinch_received = 1;
332 e5a0f69f 2018-08-18 stsp static const struct got_error *
333 96a765a8 2018-08-04 stsp view_close(struct tog_view *view)
335 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
337 669b5ffa 2018-10-07 stsp if (view->child) {
338 669b5ffa 2018-10-07 stsp view_close(view->child);
339 669b5ffa 2018-10-07 stsp view->child = NULL;
341 e5a0f69f 2018-08-18 stsp if (view->close)
342 e5a0f69f 2018-08-18 stsp err = view->close(view);
343 ea5e7bb5 2018-08-01 stsp if (view->panel)
344 ea5e7bb5 2018-08-01 stsp del_panel(view->panel);
345 ea5e7bb5 2018-08-01 stsp if (view->window)
346 ea5e7bb5 2018-08-01 stsp delwin(view->window);
347 ea5e7bb5 2018-08-01 stsp free(view);
348 e5a0f69f 2018-08-18 stsp return err;
351 ea5e7bb5 2018-08-01 stsp static struct tog_view *
352 b3665f43 2018-08-04 stsp view_open(int nlines, int ncols, int begin_y, int begin_x,
353 0cf4efb1 2018-09-29 stsp enum tog_view_type type)
355 ad80ab7b 2018-08-04 stsp struct tog_view *view = calloc(1, sizeof(*view));
357 ea5e7bb5 2018-08-01 stsp if (view == NULL)
358 ea5e7bb5 2018-08-01 stsp return NULL;
360 d6b05b5b 2018-08-04 stsp view->type = type;
361 f7d12f7e 2018-08-01 stsp view->lines = LINES;
362 f7d12f7e 2018-08-01 stsp view->cols = COLS;
363 207b9029 2018-08-01 stsp view->nlines = nlines ? nlines : LINES - begin_y;
364 207b9029 2018-08-01 stsp view->ncols = ncols ? ncols : COLS - begin_x;
365 97ddc146 2018-08-01 stsp view->begin_y = begin_y;
366 97ddc146 2018-08-01 stsp view->begin_x = begin_x;
367 842167bf 2018-08-01 stsp view->window = newwin(nlines, ncols, begin_y, begin_x);
368 ea5e7bb5 2018-08-01 stsp if (view->window == NULL) {
369 96a765a8 2018-08-04 stsp view_close(view);
370 ea5e7bb5 2018-08-01 stsp return NULL;
372 ea5e7bb5 2018-08-01 stsp view->panel = new_panel(view->window);
373 0cf4efb1 2018-09-29 stsp if (view->panel == NULL ||
374 0cf4efb1 2018-09-29 stsp set_panel_userptr(view->panel, view) != OK) {
375 96a765a8 2018-08-04 stsp view_close(view);
376 ea5e7bb5 2018-08-01 stsp return NULL;
379 ea5e7bb5 2018-08-01 stsp keypad(view->window, TRUE);
380 ea5e7bb5 2018-08-01 stsp return view;
384 0cf4efb1 2018-09-29 stsp view_split_begin_x(int begin_x)
386 2bd27830 2018-10-22 stsp if (begin_x > 0 || COLS < 120)
388 2bd27830 2018-10-22 stsp return (COLS - MAX(COLS / 2, 80));
391 5c60c32a 2018-10-18 stsp static const struct got_error *view_resize(struct tog_view *);
393 5c60c32a 2018-10-18 stsp static const struct got_error *
394 5c60c32a 2018-10-18 stsp view_splitscreen(struct tog_view *view)
396 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
398 5c60c32a 2018-10-18 stsp view->begin_y = 0;
399 5c60c32a 2018-10-18 stsp view->begin_x = view_split_begin_x(0);
400 5c60c32a 2018-10-18 stsp view->nlines = LINES;
401 5c60c32a 2018-10-18 stsp view->ncols = COLS - view->begin_x;
402 5c60c32a 2018-10-18 stsp view->lines = LINES;
403 5c60c32a 2018-10-18 stsp view->cols = COLS;
404 5c60c32a 2018-10-18 stsp err = view_resize(view);
406 5c60c32a 2018-10-18 stsp return err;
408 5c60c32a 2018-10-18 stsp if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
409 638f9024 2019-05-13 stsp return got_error_from_errno("mvwin");
411 5c60c32a 2018-10-18 stsp return NULL;
414 5c60c32a 2018-10-18 stsp static const struct got_error *
415 5c60c32a 2018-10-18 stsp view_fullscreen(struct tog_view *view)
417 5c60c32a 2018-10-18 stsp const struct got_error *err = NULL;
419 5c60c32a 2018-10-18 stsp view->begin_x = 0;
420 5c60c32a 2018-10-18 stsp view->begin_y = 0;
421 5c60c32a 2018-10-18 stsp view->nlines = LINES;
422 5c60c32a 2018-10-18 stsp view->ncols = COLS;
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);
427 5c60c32a 2018-10-18 stsp return err;
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");
432 5c60c32a 2018-10-18 stsp return NULL;
436 5c60c32a 2018-10-18 stsp view_is_parent_view(struct tog_view *view)
438 5c60c32a 2018-10-18 stsp return view->parent == NULL;
441 4d8c2215 2018-08-19 stsp static const struct got_error *
442 f7d12f7e 2018-08-01 stsp view_resize(struct tog_view *view)
444 f7d12f7e 2018-08-01 stsp int nlines, ncols;
446 0cf4efb1 2018-09-29 stsp if (view->lines > LINES)
447 0cf4efb1 2018-09-29 stsp nlines = view->nlines - (view->lines - LINES);
449 0cf4efb1 2018-09-29 stsp nlines = view->nlines + (LINES - view->lines);
451 0cf4efb1 2018-09-29 stsp if (view->cols > COLS)
452 0cf4efb1 2018-09-29 stsp ncols = view->ncols - (view->cols - COLS);
454 0cf4efb1 2018-09-29 stsp ncols = view->ncols + (COLS - view->cols);
456 0cf4efb1 2018-09-29 stsp if (wresize(view->window, nlines, ncols) == ERR)
457 638f9024 2019-05-13 stsp return got_error_from_errno("wresize");
458 a6d7eb8d 2018-10-24 stsp if (replace_panel(view->panel, view->window) == ERR)
459 638f9024 2019-05-13 stsp return got_error_from_errno("replace_panel");
460 25791caa 2018-10-24 stsp wclear(view->window);
462 0cf4efb1 2018-09-29 stsp view->nlines = nlines;
463 0cf4efb1 2018-09-29 stsp view->ncols = ncols;
464 0cf4efb1 2018-09-29 stsp view->lines = LINES;
465 0cf4efb1 2018-09-29 stsp view->cols = COLS;
467 6e3e5d9c 2018-10-18 stsp if (view->child) {
468 5c60c32a 2018-10-18 stsp view->child->begin_x = view_split_begin_x(view->begin_x);
469 5c60c32a 2018-10-18 stsp if (view->child->begin_x == 0) {
470 5c60c32a 2018-10-18 stsp view_fullscreen(view->child);
471 5c60c32a 2018-10-18 stsp if (view->child->focussed)
472 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
474 5c60c32a 2018-10-18 stsp show_panel(view->panel);
476 5c60c32a 2018-10-18 stsp view_splitscreen(view->child);
477 5c60c32a 2018-10-18 stsp show_panel(view->child->panel);
481 5c60c32a 2018-10-18 stsp return NULL;
484 669b5ffa 2018-10-07 stsp static const struct got_error *
485 669b5ffa 2018-10-07 stsp view_close_child(struct tog_view *view)
487 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
489 669b5ffa 2018-10-07 stsp if (view->child == NULL)
490 669b5ffa 2018-10-07 stsp return NULL;
492 669b5ffa 2018-10-07 stsp err = view_close(view->child);
493 669b5ffa 2018-10-07 stsp view->child = NULL;
494 669b5ffa 2018-10-07 stsp return err;
497 669b5ffa 2018-10-07 stsp static const struct got_error *
498 669b5ffa 2018-10-07 stsp view_set_child(struct tog_view *view, struct tog_view *child)
500 669b5ffa 2018-10-07 stsp const struct got_error *err = NULL;
502 669b5ffa 2018-10-07 stsp view->child = child;
503 669b5ffa 2018-10-07 stsp child->parent = view;
504 669b5ffa 2018-10-07 stsp return err;
508 bfddd0d9 2018-09-29 stsp view_is_splitscreen(struct tog_view *view)
510 f5215bb9 2019-02-22 stsp return view->begin_x > 0;
513 34bc9ec9 2019-02-22 stsp static void
514 79fcf3e4 2018-11-04 stsp tog_resizeterm(void)
516 25791caa 2018-10-24 stsp int cols, lines;
517 25791caa 2018-10-24 stsp struct winsize size;
519 25791caa 2018-10-24 stsp if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
520 25791caa 2018-10-24 stsp cols = 80; /* Default */
521 25791caa 2018-10-24 stsp lines = 24;
523 25791caa 2018-10-24 stsp cols = size.ws_col;
524 25791caa 2018-10-24 stsp lines = size.ws_row;
526 25791caa 2018-10-24 stsp resize_term(lines, cols);
529 2b49a8ae 2019-06-22 stsp static const struct got_error *
530 2b49a8ae 2019-06-22 stsp view_search_start(struct tog_view *view)
532 7c32bd05 2019-06-22 stsp const struct got_error *err = NULL;
533 2b49a8ae 2019-06-22 stsp char pattern[1024];
536 2b49a8ae 2019-06-22 stsp if (view->nlines < 1)
537 2b49a8ae 2019-06-22 stsp return NULL;
539 2b49a8ae 2019-06-22 stsp mvwaddstr(view->window, view->begin_y + view->nlines - 1,
540 2b49a8ae 2019-06-22 stsp view->begin_x, "/");
541 2b49a8ae 2019-06-22 stsp wclrtoeol(view->window);
543 2b49a8ae 2019-06-22 stsp nocbreak();
545 2b49a8ae 2019-06-22 stsp ret = wgetnstr(view->window, pattern, sizeof(pattern));
548 2b49a8ae 2019-06-22 stsp if (ret == ERR)
549 2b49a8ae 2019-06-22 stsp return NULL;
551 2b49a8ae 2019-06-22 stsp if (view->searching) {
552 2b49a8ae 2019-06-22 stsp regfree(&view->regex);
553 2b49a8ae 2019-06-22 stsp view->searching = 0;
556 2b49a8ae 2019-06-22 stsp if (regcomp(&view->regex, pattern,
557 f5daf9b1 2019-06-24 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
558 7c32bd05 2019-06-22 stsp err = view->search_start(view);
560 7c32bd05 2019-06-22 stsp regfree(&view->regex);
561 7c32bd05 2019-06-22 stsp return err;
563 2b49a8ae 2019-06-22 stsp view->searching = TOG_SEARCH_FORWARD;
564 2b49a8ae 2019-06-22 stsp view->search_next_done = 0;
565 2b49a8ae 2019-06-22 stsp view->search_next(view);
568 2b49a8ae 2019-06-22 stsp return NULL;
571 0cf4efb1 2018-09-29 stsp static const struct got_error *
572 48fcc512 2018-08-18 stsp view_input(struct tog_view **new, struct tog_view **dead,
573 e4197bf9 2018-08-18 stsp struct tog_view **focus, int *done, struct tog_view *view,
574 48fcc512 2018-08-18 stsp struct tog_view_list_head *views)
576 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
577 669b5ffa 2018-10-07 stsp struct tog_view *v;
578 1a76625f 2018-10-22 stsp int ch, errcode;
580 e5a0f69f 2018-08-18 stsp *new = NULL;
581 e5a0f69f 2018-08-18 stsp *dead = NULL;
582 0cf4efb1 2018-09-29 stsp *focus = NULL;
584 60493ae3 2019-06-20 stsp if (view->searching && !view->search_next_done) {
585 60493ae3 2019-06-20 stsp errcode = pthread_mutex_unlock(&tog_mutex);
586 60493ae3 2019-06-20 stsp if (errcode)
587 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
588 60493ae3 2019-06-20 stsp "pthread_mutex_unlock");
589 60493ae3 2019-06-20 stsp pthread_yield();
590 60493ae3 2019-06-20 stsp errcode = pthread_mutex_lock(&tog_mutex);
591 60493ae3 2019-06-20 stsp if (errcode)
592 60493ae3 2019-06-20 stsp return got_error_set_errno(errcode,
593 60493ae3 2019-06-20 stsp "pthread_mutex_lock");
594 60493ae3 2019-06-20 stsp view->search_next(view);
595 60493ae3 2019-06-20 stsp return NULL;
598 e5a0f69f 2018-08-18 stsp nodelay(stdscr, FALSE);
599 1a76625f 2018-10-22 stsp /* Allow threads to make progress while we are waiting for input. */
600 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
601 1a76625f 2018-10-22 stsp if (errcode)
602 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
603 cc5bac66 2018-10-22 stsp ch = wgetch(view->window);
604 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
605 1a76625f 2018-10-22 stsp if (errcode)
606 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
607 e5a0f69f 2018-08-18 stsp nodelay(stdscr, TRUE);
609 25791caa 2018-10-24 stsp if (tog_sigwinch_received) {
610 25791caa 2018-10-24 stsp tog_resizeterm();
611 25791caa 2018-10-24 stsp tog_sigwinch_received = 0;
612 25791caa 2018-10-24 stsp TAILQ_FOREACH(v, views, entry) {
613 25791caa 2018-10-24 stsp err = view_resize(v);
615 25791caa 2018-10-24 stsp return err;
616 25791caa 2018-10-24 stsp err = v->input(new, dead, focus, v, KEY_RESIZE);
618 25791caa 2018-10-24 stsp return err;
622 e5a0f69f 2018-08-18 stsp switch (ch) {
626 1e37a5c2 2019-05-12 jcs if (view->child) {
627 1e37a5c2 2019-05-12 jcs *focus = view->child;
628 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
629 1e37a5c2 2019-05-12 jcs } else if (view->parent) {
630 1e37a5c2 2019-05-12 jcs *focus = view->parent;
631 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 0;
635 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
636 1e37a5c2 2019-05-12 jcs *dead = view;
642 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
643 1e37a5c2 2019-05-12 jcs if (view->child == NULL)
645 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view->child)) {
646 669b5ffa 2018-10-07 stsp *focus = view->child;
647 669b5ffa 2018-10-07 stsp view->child_focussed = 1;
648 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view->child);
650 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view->child);
653 1e37a5c2 2019-05-12 jcs err = view->child->input(new, dead, focus,
654 1e37a5c2 2019-05-12 jcs view->child, KEY_RESIZE);
656 1e37a5c2 2019-05-12 jcs if (view_is_splitscreen(view)) {
657 1e37a5c2 2019-05-12 jcs *focus = view;
658 1e37a5c2 2019-05-12 jcs view->parent->child_focussed = 1;
659 1e37a5c2 2019-05-12 jcs err = view_fullscreen(view);
661 1e37a5c2 2019-05-12 jcs err = view_splitscreen(view);
665 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view,
666 1e37a5c2 2019-05-12 jcs KEY_RESIZE);
669 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
672 60493ae3 2019-06-20 stsp if (view->search_start)
673 2b49a8ae 2019-06-22 stsp view_search_start(view);
675 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
679 60493ae3 2019-06-20 stsp if (view->search_next && view->searching) {
680 b1bf1435 2019-06-21 stsp view->searching = (ch == 'n' ?
681 b1bf1435 2019-06-21 stsp TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
682 60493ae3 2019-06-20 stsp view->search_next_done = 0;
683 60493ae3 2019-06-20 stsp view->search_next(view);
685 60493ae3 2019-06-20 stsp err = view->input(new, dead, focus, view, ch);
688 1e37a5c2 2019-05-12 jcs err = view->input(new, dead, focus, view, ch);
692 e5a0f69f 2018-08-18 stsp return err;
696 1a57306a 2018-09-02 stsp view_vborder(struct tog_view *view)
698 0cf4efb1 2018-09-29 stsp PANEL *panel;
699 0cf4efb1 2018-09-29 stsp struct tog_view *view_above;
701 669b5ffa 2018-10-07 stsp if (view->parent)
702 669b5ffa 2018-10-07 stsp return view_vborder(view->parent);
704 0cf4efb1 2018-09-29 stsp panel = panel_above(view->panel);
705 0cf4efb1 2018-09-29 stsp if (panel == NULL)
708 0cf4efb1 2018-09-29 stsp view_above = panel_userptr(panel);
709 0cf4efb1 2018-09-29 stsp mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
710 1a57306a 2018-09-02 stsp got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
714 a3404814 2018-09-02 stsp view_needs_focus_indication(struct tog_view *view)
716 669b5ffa 2018-10-07 stsp if (view_is_parent_view(view)) {
717 669b5ffa 2018-10-07 stsp if (view->child == NULL || view->child_focussed)
719 669b5ffa 2018-10-07 stsp if (!view_is_splitscreen(view->child))
721 669b5ffa 2018-10-07 stsp } else if (!view_is_splitscreen(view))
724 669b5ffa 2018-10-07 stsp return view->focussed;
727 bcbd79e2 2018-08-19 stsp static const struct got_error *
728 e5a0f69f 2018-08-18 stsp view_loop(struct tog_view *view)
730 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
731 e5a0f69f 2018-08-18 stsp struct tog_view_list_head views;
732 669b5ffa 2018-10-07 stsp struct tog_view *new_view, *dead_view, *focus_view, *main_view;
733 fd823528 2018-10-22 stsp int fast_refresh = 10;
734 1a76625f 2018-10-22 stsp int done = 0, errcode;
736 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
737 1a76625f 2018-10-22 stsp if (errcode)
738 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
740 e5a0f69f 2018-08-18 stsp TAILQ_INIT(&views);
741 e5a0f69f 2018-08-18 stsp TAILQ_INSERT_HEAD(&views, view, entry);
743 a81bf10d 2018-09-29 stsp main_view = view;
744 1004088d 2018-09-29 stsp view->focussed = 1;
745 878940b7 2018-09-29 stsp err = view->show(view);
747 0cf4efb1 2018-09-29 stsp return err;
748 0cf4efb1 2018-09-29 stsp update_panels();
749 0cf4efb1 2018-09-29 stsp doupdate();
750 e4197bf9 2018-08-18 stsp while (!TAILQ_EMPTY(&views) && !done) {
751 fd823528 2018-10-22 stsp /* Refresh fast during initialization, then become slower. */
752 fd823528 2018-10-22 stsp if (fast_refresh && fast_refresh-- == 0)
753 fd823528 2018-10-22 stsp halfdelay(10); /* switch to once per second */
755 0cf4efb1 2018-09-29 stsp err = view_input(&new_view, &dead_view, &focus_view, &done,
756 e4197bf9 2018-08-18 stsp view, &views);
759 e5a0f69f 2018-08-18 stsp if (dead_view) {
760 669b5ffa 2018-10-07 stsp struct tog_view *prev = NULL;
762 669b5ffa 2018-10-07 stsp if (view_is_parent_view(dead_view))
763 669b5ffa 2018-10-07 stsp prev = TAILQ_PREV(dead_view,
764 669b5ffa 2018-10-07 stsp tog_view_list_head, entry);
765 f41eceb0 2018-10-24 stsp else if (view->parent != dead_view)
766 669b5ffa 2018-10-07 stsp prev = view->parent;
768 669b5ffa 2018-10-07 stsp if (dead_view->parent)
769 669b5ffa 2018-10-07 stsp dead_view->parent->child = NULL;
771 669b5ffa 2018-10-07 stsp TAILQ_REMOVE(&views, dead_view, entry);
773 e5a0f69f 2018-08-18 stsp err = view_close(dead_view);
774 d01904d4 2019-06-25 stsp if (err || (dead_view == main_view && new_view == NULL))
777 0cf4efb1 2018-09-29 stsp if (view == dead_view) {
778 0cf4efb1 2018-09-29 stsp if (focus_view)
779 0cf4efb1 2018-09-29 stsp view = focus_view;
780 669b5ffa 2018-10-07 stsp else if (prev)
781 669b5ffa 2018-10-07 stsp view = prev;
782 669b5ffa 2018-10-07 stsp else if (!TAILQ_EMPTY(&views))
783 0cf4efb1 2018-09-29 stsp view = TAILQ_LAST(&views,
784 0cf4efb1 2018-09-29 stsp tog_view_list_head);
786 0cf4efb1 2018-09-29 stsp view = NULL;
787 669b5ffa 2018-10-07 stsp if (view) {
788 669b5ffa 2018-10-07 stsp if (view->child && view->child_focussed)
789 669b5ffa 2018-10-07 stsp focus_view = view->child;
791 669b5ffa 2018-10-07 stsp focus_view = view;
795 bcbd79e2 2018-08-19 stsp if (new_view) {
796 86c66b02 2018-10-18 stsp struct tog_view *v, *t;
797 86c66b02 2018-10-18 stsp /* Only allow one parent view per type. */
798 86c66b02 2018-10-18 stsp TAILQ_FOREACH_SAFE(v, &views, entry, t) {
799 86c66b02 2018-10-18 stsp if (v->type != new_view->type)
801 86c66b02 2018-10-18 stsp TAILQ_REMOVE(&views, v, entry);
802 86c66b02 2018-10-18 stsp err = view_close(v);
807 bcbd79e2 2018-08-19 stsp TAILQ_INSERT_TAIL(&views, new_view, entry);
808 fed7eaa8 2018-10-24 stsp view = new_view;
809 878940b7 2018-09-29 stsp if (focus_view == NULL)
810 878940b7 2018-09-29 stsp focus_view = new_view;
812 0cf4efb1 2018-09-29 stsp if (focus_view) {
813 0cf4efb1 2018-09-29 stsp show_panel(focus_view->panel);
815 0cf4efb1 2018-09-29 stsp view->focussed = 0;
816 0cf4efb1 2018-09-29 stsp focus_view->focussed = 1;
817 0cf4efb1 2018-09-29 stsp view = focus_view;
818 878940b7 2018-09-29 stsp if (new_view)
819 878940b7 2018-09-29 stsp show_panel(new_view->panel);
820 669b5ffa 2018-10-07 stsp if (view->child && view_is_splitscreen(view->child))
821 669b5ffa 2018-10-07 stsp show_panel(view->child->panel);
823 669b5ffa 2018-10-07 stsp if (view) {
824 1a76625f 2018-10-22 stsp if (focus_view == NULL) {
825 758194b5 2018-10-24 stsp view->focussed = 1;
826 758194b5 2018-10-24 stsp show_panel(view->panel);
827 758194b5 2018-10-24 stsp if (view->child && view_is_splitscreen(view->child))
828 758194b5 2018-10-24 stsp show_panel(view->child->panel);
829 1a76625f 2018-10-22 stsp focus_view = view;
831 669b5ffa 2018-10-07 stsp if (view->parent) {
832 669b5ffa 2018-10-07 stsp err = view->parent->show(view->parent);
836 669b5ffa 2018-10-07 stsp err = view->show(view);
839 669b5ffa 2018-10-07 stsp if (view->child) {
840 669b5ffa 2018-10-07 stsp err = view->child->show(view->child);
844 1a76625f 2018-10-22 stsp update_panels();
845 1a76625f 2018-10-22 stsp doupdate();
849 e5a0f69f 2018-08-18 stsp while (!TAILQ_EMPTY(&views)) {
850 e5a0f69f 2018-08-18 stsp view = TAILQ_FIRST(&views);
851 e5a0f69f 2018-08-18 stsp TAILQ_REMOVE(&views, view, entry);
852 e5a0f69f 2018-08-18 stsp view_close(view);
855 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
856 1a76625f 2018-10-22 stsp if (errcode)
857 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_unlock");
859 e5a0f69f 2018-08-18 stsp return err;
862 4ed7e80c 2018-05-20 stsp __dead static void
863 9f7d7167 2018-04-29 stsp usage_log(void)
866 c70c5802 2018-08-01 stsp fprintf(stderr,
867 c70c5802 2018-08-01 stsp "usage: %s log [-c commit] [-r repository-path] [path]\n",
868 9f7d7167 2018-04-29 stsp getprogname());
872 963b370f 2018-05-20 stsp /* Create newly allocated wide-character string equivalent to a byte string. */
873 80ddbec8 2018-04-29 stsp static const struct got_error *
874 963b370f 2018-05-20 stsp mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
876 00dfcb92 2018-06-11 stsp char *vis = NULL;
877 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
879 963b370f 2018-05-20 stsp *ws = NULL;
880 963b370f 2018-05-20 stsp *wlen = mbstowcs(NULL, s, 0);
881 00dfcb92 2018-06-11 stsp if (*wlen == (size_t)-1) {
882 00dfcb92 2018-06-11 stsp int vislen;
883 00dfcb92 2018-06-11 stsp if (errno != EILSEQ)
884 638f9024 2019-05-13 stsp return got_error_from_errno("mbstowcs");
886 00dfcb92 2018-06-11 stsp /* byte string invalid in current encoding; try to "fix" it */
887 00dfcb92 2018-06-11 stsp err = got_mbsavis(&vis, &vislen, s);
889 00dfcb92 2018-06-11 stsp return err;
890 00dfcb92 2018-06-11 stsp *wlen = mbstowcs(NULL, vis, 0);
891 a7f50699 2018-06-11 stsp if (*wlen == (size_t)-1) {
892 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs"); /* give up */
897 963b370f 2018-05-20 stsp *ws = calloc(*wlen + 1, sizeof(*ws));
898 a7f50699 2018-06-11 stsp if (*ws == NULL) {
899 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
903 00dfcb92 2018-06-11 stsp if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
904 638f9024 2019-05-13 stsp err = got_error_from_errno("mbstowcs");
909 963b370f 2018-05-20 stsp *ws = NULL;
912 963b370f 2018-05-20 stsp return err;
915 963b370f 2018-05-20 stsp /* Format a line for display, ensuring that it won't overflow a width limit. */
916 963b370f 2018-05-20 stsp static const struct got_error *
917 ffd1d5e5 2018-06-23 stsp format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
919 963b370f 2018-05-20 stsp const struct got_error *err = NULL;
920 963b370f 2018-05-20 stsp int cols = 0;
921 963b370f 2018-05-20 stsp wchar_t *wline = NULL;
922 963b370f 2018-05-20 stsp size_t wlen;
925 963b370f 2018-05-20 stsp *wlinep = NULL;
926 b700b5d6 2018-07-10 stsp *widthp = 0;
928 963b370f 2018-05-20 stsp err = mbs2ws(&wline, &wlen, line);
930 963b370f 2018-05-20 stsp return err;
933 b700b5d6 2018-07-10 stsp while (i < wlen && cols < wlimit) {
934 963b370f 2018-05-20 stsp int width = wcwidth(wline[i]);
935 963b370f 2018-05-20 stsp switch (width) {
941 3c1f04f1 2018-09-13 stsp if (cols + width <= wlimit)
942 b700b5d6 2018-07-10 stsp cols += width;
946 963b370f 2018-05-20 stsp if (wline[i] == L'\t')
947 67ceb59d 2019-03-03 stsp cols += TABSIZE - ((cols + 1) % TABSIZE);
951 638f9024 2019-05-13 stsp err = got_error_from_errno("wcwidth");
955 963b370f 2018-05-20 stsp wline[i] = L'\0';
956 b700b5d6 2018-07-10 stsp if (widthp)
957 b700b5d6 2018-07-10 stsp *widthp = cols;
960 963b370f 2018-05-20 stsp free(wline);
962 963b370f 2018-05-20 stsp *wlinep = wline;
963 963b370f 2018-05-20 stsp return err;
966 8b473291 2019-02-21 stsp static const struct got_error*
967 8b473291 2019-02-21 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
968 8b473291 2019-02-21 stsp struct got_object_id *id)
970 8b473291 2019-02-21 stsp static const struct got_error *err = NULL;
971 8b473291 2019-02-21 stsp struct got_reflist_entry *re;
973 8b473291 2019-02-21 stsp const char *name;
975 8b473291 2019-02-21 stsp *refs_str = NULL;
977 8b473291 2019-02-21 stsp SIMPLEQ_FOREACH(re, refs, entry) {
978 8b473291 2019-02-21 stsp if (got_object_id_cmp(re->id, id) != 0)
980 8b473291 2019-02-21 stsp name = got_ref_get_name(re->ref);
981 8b473291 2019-02-21 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
983 8b473291 2019-02-21 stsp if (strncmp(name, "refs/", 5) == 0)
985 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
987 8b473291 2019-02-21 stsp if (strncmp(name, "heads/", 6) == 0)
989 8b473291 2019-02-21 stsp if (strncmp(name, "remotes/", 8) == 0)
991 8b473291 2019-02-21 stsp s = *refs_str;
992 8b473291 2019-02-21 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
993 8b473291 2019-02-21 stsp s ? ", " : "", name) == -1) {
994 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
996 8b473291 2019-02-21 stsp *refs_str = NULL;
1002 8b473291 2019-02-21 stsp return err;
1005 963b370f 2018-05-20 stsp static const struct got_error *
1006 5813d178 2019-03-09 stsp format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1008 5813d178 2019-03-09 stsp char *smallerthan, *at;
1010 5813d178 2019-03-09 stsp smallerthan = strchr(author, '<');
1011 5813d178 2019-03-09 stsp if (smallerthan && smallerthan[1] != '\0')
1012 5813d178 2019-03-09 stsp author = smallerthan + 1;
1013 5813d178 2019-03-09 stsp at = strchr(author, '@');
1015 5813d178 2019-03-09 stsp *at = '\0';
1016 5813d178 2019-03-09 stsp return format_line(wauthor, author_width, author, limit);
1019 5813d178 2019-03-09 stsp static const struct got_error *
1020 2814baeb 2018-08-01 stsp draw_commit(struct tog_view *view, struct got_commit_object *commit,
1021 5813d178 2019-03-09 stsp struct got_object_id *id, struct got_reflist_head *refs,
1022 5813d178 2019-03-09 stsp int author_display_cols)
1024 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1025 b39d25c7 2018-07-10 stsp char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1026 80ddbec8 2018-04-29 stsp char *logmsg0 = NULL, *logmsg = NULL;
1027 5813d178 2019-03-09 stsp char *author = NULL;
1028 bb737323 2018-05-20 stsp wchar_t *wlogmsg = NULL, *wauthor = NULL;
1029 bb737323 2018-05-20 stsp int author_width, logmsg_width;
1030 5813d178 2019-03-09 stsp char *newline, *line = NULL;
1031 bb737323 2018-05-20 stsp int col, limit;
1032 b39d25c7 2018-07-10 stsp static const size_t date_display_cols = 9;
1033 f7d12f7e 2018-08-01 stsp const int avail = view->ncols;
1034 ccb26ccd 2018-11-05 stsp struct tm tm;
1035 45d799e2 2018-12-23 stsp time_t committer_time;
1037 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1038 45d799e2 2018-12-23 stsp if (localtime_r(&committer_time, &tm) == NULL)
1039 638f9024 2019-05-13 stsp return got_error_from_errno("localtime_r");
1040 ccb26ccd 2018-11-05 stsp if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1041 ccb26ccd 2018-11-05 stsp >= sizeof(datebuf))
1042 b39d25c7 2018-07-10 stsp return got_error(GOT_ERR_NO_SPACE);
1044 b39d25c7 2018-07-10 stsp if (avail < date_display_cols)
1045 b39d25c7 2018-07-10 stsp limit = MIN(sizeof(datebuf) - 1, avail);
1047 b39d25c7 2018-07-10 stsp limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1048 2814baeb 2018-08-01 stsp waddnstr(view->window, datebuf, limit);
1049 b39d25c7 2018-07-10 stsp col = limit + 1;
1050 b39d25c7 2018-07-10 stsp if (col > avail)
1051 b39d25c7 2018-07-10 stsp goto done;
1053 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(commit));
1054 5813d178 2019-03-09 stsp if (author == NULL) {
1055 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1056 80ddbec8 2018-04-29 stsp goto done;
1058 5813d178 2019-03-09 stsp err = format_author(&wauthor, &author_width, author, avail - col);
1060 bb737323 2018-05-20 stsp goto done;
1061 2814baeb 2018-08-01 stsp waddwstr(view->window, wauthor);
1062 bb737323 2018-05-20 stsp col += author_width;
1063 5813d178 2019-03-09 stsp while (col <= avail && author_width < author_display_cols + 2) {
1064 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1066 bb737323 2018-05-20 stsp author_width++;
1068 9c2eaf34 2018-05-20 stsp if (col > avail)
1069 9c2eaf34 2018-05-20 stsp goto done;
1071 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1072 bb737323 2018-05-20 stsp if (logmsg0 == NULL) {
1073 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1074 6d9fbc00 2018-04-29 stsp goto done;
1076 bb737323 2018-05-20 stsp logmsg = logmsg0;
1077 bb737323 2018-05-20 stsp while (*logmsg == '\n')
1079 bb737323 2018-05-20 stsp newline = strchr(logmsg, '\n');
1080 bb737323 2018-05-20 stsp if (newline)
1081 bb737323 2018-05-20 stsp *newline = '\0';
1082 bb737323 2018-05-20 stsp limit = avail - col;
1083 bb737323 2018-05-20 stsp err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1085 bb737323 2018-05-20 stsp goto done;
1086 2814baeb 2018-08-01 stsp waddwstr(view->window, wlogmsg);
1087 bb737323 2018-05-20 stsp col += logmsg_width;
1088 bb737323 2018-05-20 stsp while (col <= avail) {
1089 2814baeb 2018-08-01 stsp waddch(view->window, ' ');
1093 80ddbec8 2018-04-29 stsp free(logmsg0);
1094 bb737323 2018-05-20 stsp free(wlogmsg);
1095 5813d178 2019-03-09 stsp free(author);
1096 bb737323 2018-05-20 stsp free(wauthor);
1097 80ddbec8 2018-04-29 stsp free(line);
1098 80ddbec8 2018-04-29 stsp return err;
1101 899d86c2 2018-05-10 stsp static struct commit_queue_entry *
1102 899d86c2 2018-05-10 stsp alloc_commit_queue_entry(struct got_commit_object *commit,
1103 899d86c2 2018-05-10 stsp struct got_object_id *id)
1105 80ddbec8 2018-04-29 stsp struct commit_queue_entry *entry;
1107 80ddbec8 2018-04-29 stsp entry = calloc(1, sizeof(*entry));
1108 80ddbec8 2018-04-29 stsp if (entry == NULL)
1109 899d86c2 2018-05-10 stsp return NULL;
1111 899d86c2 2018-05-10 stsp entry->id = id;
1112 99db9666 2018-05-07 stsp entry->commit = commit;
1113 899d86c2 2018-05-10 stsp return entry;
1116 99db9666 2018-05-07 stsp static void
1117 99db9666 2018-05-07 stsp pop_commit(struct commit_queue *commits)
1119 99db9666 2018-05-07 stsp struct commit_queue_entry *entry;
1121 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1122 ecb28ae0 2018-07-16 stsp TAILQ_REMOVE(&commits->head, entry, entry);
1123 99db9666 2018-05-07 stsp got_object_commit_close(entry->commit);
1124 ecb28ae0 2018-07-16 stsp commits->ncommits--;
1125 9ba79e04 2018-06-11 stsp /* Don't free entry->id! It is owned by the commit graph. */
1126 99db9666 2018-05-07 stsp free(entry);
1129 99db9666 2018-05-07 stsp static void
1130 99db9666 2018-05-07 stsp free_commits(struct commit_queue *commits)
1132 ecb28ae0 2018-07-16 stsp while (!TAILQ_EMPTY(&commits->head))
1133 99db9666 2018-05-07 stsp pop_commit(commits);
1136 c4972b91 2018-05-07 stsp static const struct got_error *
1137 9ba79e04 2018-06-11 stsp queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1138 1a76625f 2018-10-22 stsp int minqueue, struct got_repository *repo, const char *path)
1140 899d86c2 2018-05-10 stsp const struct got_error *err = NULL;
1141 93e45b7c 2018-09-24 stsp int nqueued = 0;
1144 1a76625f 2018-10-22 stsp * We keep all commits open throughout the lifetime of the log
1145 1a76625f 2018-10-22 stsp * view in order to avoid having to re-fetch commits from disk
1146 1a76625f 2018-10-22 stsp * while updating the display.
1148 93e45b7c 2018-09-24 stsp while (nqueued < minqueue) {
1149 93e45b7c 2018-09-24 stsp struct got_object_id *id;
1150 9ba79e04 2018-06-11 stsp struct got_commit_object *commit;
1151 93e45b7c 2018-09-24 stsp struct commit_queue_entry *entry;
1152 1a76625f 2018-10-22 stsp int errcode;
1154 9ba79e04 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1155 9ba79e04 2018-06-11 stsp if (err) {
1156 93e45b7c 2018-09-24 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1158 93e45b7c 2018-09-24 stsp err = got_commit_graph_fetch_commits(graph,
1159 93e45b7c 2018-09-24 stsp minqueue, repo);
1161 ecb28ae0 2018-07-16 stsp return err;
1165 ecb28ae0 2018-07-16 stsp if (id == NULL)
1168 9ba79e04 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1171 9ba79e04 2018-06-11 stsp entry = alloc_commit_queue_entry(commit, id);
1172 9ba79e04 2018-06-11 stsp if (entry == NULL) {
1173 638f9024 2019-05-13 stsp err = got_error_from_errno("alloc_commit_queue_entry");
1177 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1178 1a76625f 2018-10-22 stsp if (errcode) {
1179 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_lock");
1183 1a76625f 2018-10-22 stsp entry->idx = commits->ncommits;
1184 ecb28ae0 2018-07-16 stsp TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1185 ecb28ae0 2018-07-16 stsp nqueued++;
1186 ecb28ae0 2018-07-16 stsp commits->ncommits++;
1188 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1189 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1190 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1191 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1194 9ba79e04 2018-06-11 stsp return err;
1197 99db9666 2018-05-07 stsp static const struct got_error *
1198 19e70ad6 2019-05-14 stsp get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1199 19e70ad6 2019-05-14 stsp struct got_repository *repo)
1201 9ba79e04 2018-06-11 stsp const struct got_error *err = NULL;
1202 9ba79e04 2018-06-11 stsp struct got_reference *head_ref;
1204 9ba79e04 2018-06-11 stsp *head_id = NULL;
1206 19e70ad6 2019-05-14 stsp err = got_ref_open(&head_ref, repo, branch_name, 0);
1208 99db9666 2018-05-07 stsp return err;
1210 9ba79e04 2018-06-11 stsp err = got_ref_resolve(head_id, repo, head_ref);
1211 9ba79e04 2018-06-11 stsp got_ref_close(head_ref);
1212 9ba79e04 2018-06-11 stsp if (err) {
1213 9ba79e04 2018-06-11 stsp *head_id = NULL;
1214 99db9666 2018-05-07 stsp return err;
1217 9ba79e04 2018-06-11 stsp return NULL;
1220 0553a4e3 2018-04-30 stsp static const struct got_error *
1221 2814baeb 2018-08-01 stsp draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1222 ecb28ae0 2018-07-16 stsp struct commit_queue_entry **selected, struct commit_queue_entry *first,
1223 a7f40148 2018-07-18 stsp struct commit_queue *commits, int selected_idx, int limit,
1224 8b473291 2019-02-21 stsp struct got_reflist_head *refs, const char *path, int commits_needed)
1226 0553a4e3 2018-04-30 stsp const struct got_error *err = NULL;
1227 0553a4e3 2018-04-30 stsp struct commit_queue_entry *entry;
1228 60493ae3 2019-06-20 stsp int width;
1229 60493ae3 2019-06-20 stsp int ncommits, author_cols = 10;
1230 1a76625f 2018-10-22 stsp char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1231 8b473291 2019-02-21 stsp char *refs_str = NULL;
1232 ecb28ae0 2018-07-16 stsp wchar_t *wline;
1234 e0d42f60 2018-07-22 stsp entry = first;
1235 e0d42f60 2018-07-22 stsp ncommits = 0;
1236 e0d42f60 2018-07-22 stsp while (entry) {
1237 e0d42f60 2018-07-22 stsp if (ncommits == selected_idx) {
1238 e0d42f60 2018-07-22 stsp *selected = entry;
1241 e0d42f60 2018-07-22 stsp entry = TAILQ_NEXT(entry, entry);
1242 e0d42f60 2018-07-22 stsp ncommits++;
1245 60493ae3 2019-06-20 stsp if (*selected && !(view->searching && view->search_next_done == 0)) {
1246 1a76625f 2018-10-22 stsp err = got_object_id_str(&id_str, (*selected)->id);
1248 ecb28ae0 2018-07-16 stsp return err;
1249 8b473291 2019-02-21 stsp if (refs) {
1250 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, (*selected)->id);
1252 8b473291 2019-02-21 stsp goto done;
1256 359bfafd 2019-02-22 stsp if (commits_needed == 0)
1257 359bfafd 2019-02-22 stsp halfdelay(10); /* disable fast refresh */
1259 8b473291 2019-02-21 stsp if (asprintf(&ncommits_str, " [%d/%d] %s",
1260 1a76625f 2018-10-22 stsp entry ? entry->idx + 1 : 0, commits->ncommits,
1261 60493ae3 2019-06-20 stsp commits_needed > 0 ?
1262 60493ae3 2019-06-20 stsp (view->searching && view->search_next_done == 0
1263 60493ae3 2019-06-20 stsp ? "searching..." : "loading... ") :
1264 8b473291 2019-02-21 stsp (refs_str ? refs_str : "")) == -1) {
1265 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1266 8b473291 2019-02-21 stsp goto done;
1269 1a76625f 2018-10-22 stsp if (path && strcmp(path, "/") != 0) {
1270 c1124f18 2018-12-23 stsp if (asprintf(&header, "commit %s %s%s",
1271 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1272 1a76625f 2018-10-22 stsp path, ncommits_str) == -1) {
1273 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1274 1a76625f 2018-10-22 stsp header = NULL;
1275 1a76625f 2018-10-22 stsp goto done;
1277 c1124f18 2018-12-23 stsp } else if (asprintf(&header, "commit %s%s",
1278 1a76625f 2018-10-22 stsp id_str ? id_str : "........................................",
1279 1a76625f 2018-10-22 stsp ncommits_str) == -1) {
1280 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1281 1a76625f 2018-10-22 stsp header = NULL;
1282 1a76625f 2018-10-22 stsp goto done;
1284 1a76625f 2018-10-22 stsp err = format_line(&wline, &width, header, view->ncols);
1286 1a76625f 2018-10-22 stsp goto done;
1288 2814baeb 2018-08-01 stsp werase(view->window);
1290 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1291 a3404814 2018-09-02 stsp wstandout(view->window);
1292 2814baeb 2018-08-01 stsp waddwstr(view->window, wline);
1293 1a76625f 2018-10-22 stsp while (width < view->ncols) {
1294 1a76625f 2018-10-22 stsp waddch(view->window, ' ');
1297 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
1298 a3404814 2018-09-02 stsp wstandend(view->window);
1299 ecb28ae0 2018-07-16 stsp free(wline);
1300 ecb28ae0 2018-07-16 stsp if (limit <= 1)
1301 1a76625f 2018-10-22 stsp goto done;
1303 5813d178 2019-03-09 stsp /* Grow author column size if necessary. */
1304 899d86c2 2018-05-10 stsp entry = first;
1305 5813d178 2019-03-09 stsp ncommits = 0;
1306 5813d178 2019-03-09 stsp while (entry) {
1307 5813d178 2019-03-09 stsp char *author;
1308 5813d178 2019-03-09 stsp wchar_t *wauthor;
1309 5813d178 2019-03-09 stsp int width;
1310 5813d178 2019-03-09 stsp if (ncommits >= limit - 1)
1312 5813d178 2019-03-09 stsp author = strdup(got_object_commit_get_author(entry->commit));
1313 5813d178 2019-03-09 stsp if (author == NULL) {
1314 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1315 5813d178 2019-03-09 stsp goto done;
1317 5813d178 2019-03-09 stsp err = format_author(&wauthor, &width, author, COLS);
1318 5813d178 2019-03-09 stsp if (author_cols < width)
1319 5813d178 2019-03-09 stsp author_cols = width;
1320 5813d178 2019-03-09 stsp free(wauthor);
1321 5813d178 2019-03-09 stsp free(author);
1322 5813d178 2019-03-09 stsp entry = TAILQ_NEXT(entry, entry);
1325 5813d178 2019-03-09 stsp entry = first;
1326 899d86c2 2018-05-10 stsp *last = first;
1327 867c6645 2018-07-10 stsp ncommits = 0;
1328 899d86c2 2018-05-10 stsp while (entry) {
1329 ecb28ae0 2018-07-16 stsp if (ncommits >= limit - 1)
1331 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1332 2814baeb 2018-08-01 stsp wstandout(view->window);
1333 5813d178 2019-03-09 stsp err = draw_commit(view, entry->commit, entry->id, refs,
1334 5813d178 2019-03-09 stsp author_cols);
1335 b51189f7 2019-03-01 stsp if (ncommits == selected_idx)
1336 2814baeb 2018-08-01 stsp wstandend(view->window);
1338 60493ae3 2019-06-20 stsp goto done;
1339 0553a4e3 2018-04-30 stsp ncommits++;
1340 899d86c2 2018-05-10 stsp *last = entry;
1341 899d86c2 2018-05-10 stsp entry = TAILQ_NEXT(entry, entry);
1344 1a57306a 2018-09-02 stsp view_vborder(view);
1346 1a76625f 2018-10-22 stsp free(id_str);
1347 8b473291 2019-02-21 stsp free(refs_str);
1348 1a76625f 2018-10-22 stsp free(ncommits_str);
1349 1a76625f 2018-10-22 stsp free(header);
1350 80ddbec8 2018-04-29 stsp return err;
1353 07b55e75 2018-05-10 stsp static void
1354 34bc9ec9 2019-02-22 stsp scroll_up(struct tog_view *view,
1355 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1356 07b55e75 2018-05-10 stsp struct commit_queue *commits)
1358 07b55e75 2018-05-10 stsp struct commit_queue_entry *entry;
1359 07b55e75 2018-05-10 stsp int nscrolled = 0;
1361 ecb28ae0 2018-07-16 stsp entry = TAILQ_FIRST(&commits->head);
1362 00ba99a7 2019-05-12 jcs if (*first_displayed_entry == entry)
1365 07b55e75 2018-05-10 stsp entry = *first_displayed_entry;
1366 16482c3b 2018-05-20 stsp while (entry && nscrolled < maxscroll) {
1367 ecb28ae0 2018-07-16 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1368 07b55e75 2018-05-10 stsp if (entry) {
1369 07b55e75 2018-05-10 stsp *first_displayed_entry = entry;
1370 07b55e75 2018-05-10 stsp nscrolled++;
1375 aa075928 2018-05-10 stsp static const struct got_error *
1376 5e224a3e 2019-02-22 stsp trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1377 1a76625f 2018-10-22 stsp pthread_cond_t *need_commits)
1379 5e224a3e 2019-02-22 stsp int errcode;
1380 8a42fca8 2019-02-22 stsp int max_wait = 20;
1382 8a42fca8 2019-02-22 stsp halfdelay(1); /* fast refresh while loading commits */
1384 5e224a3e 2019-02-22 stsp while (*commits_needed > 0) {
1385 5e224a3e 2019-02-22 stsp if (*log_complete)
1388 5e224a3e 2019-02-22 stsp /* Wake the log thread. */
1389 7aafa0d1 2019-02-22 stsp errcode = pthread_cond_signal(need_commits);
1390 7aafa0d1 2019-02-22 stsp if (errcode)
1391 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1392 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1393 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1394 7aafa0d1 2019-02-22 stsp if (errcode)
1395 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1396 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1397 7aafa0d1 2019-02-22 stsp pthread_yield();
1398 7aafa0d1 2019-02-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1399 7aafa0d1 2019-02-22 stsp if (errcode)
1400 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1401 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1403 8a42fca8 2019-02-22 stsp if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1405 5e224a3e 2019-02-22 stsp * Thread is not done yet; lose a key press
1406 5e224a3e 2019-02-22 stsp * and let the user retry... this way the GUI
1407 5e224a3e 2019-02-22 stsp * remains interactive while logging deep paths
1408 5e224a3e 2019-02-22 stsp * with few commits in history.
1410 7aafa0d1 2019-02-22 stsp return NULL;
1414 5e224a3e 2019-02-22 stsp return NULL;
1417 5e224a3e 2019-02-22 stsp static const struct got_error *
1418 34bc9ec9 2019-02-22 stsp scroll_down(struct tog_view *view,
1419 34bc9ec9 2019-02-22 stsp struct commit_queue_entry **first_displayed_entry, int maxscroll,
1420 5e224a3e 2019-02-22 stsp struct commit_queue_entry **last_displayed_entry,
1421 5e224a3e 2019-02-22 stsp struct commit_queue *commits, int *log_complete, int *commits_needed,
1422 5e224a3e 2019-02-22 stsp pthread_cond_t *need_commits)
1424 5e224a3e 2019-02-22 stsp const struct got_error *err = NULL;
1425 5e224a3e 2019-02-22 stsp struct commit_queue_entry *pentry;
1426 5e224a3e 2019-02-22 stsp int nscrolled = 0;
1428 5e224a3e 2019-02-22 stsp if (*last_displayed_entry == NULL)
1429 5e224a3e 2019-02-22 stsp return NULL;
1431 5e224a3e 2019-02-22 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1432 5e224a3e 2019-02-22 stsp if (pentry == NULL && !*log_complete) {
1434 08ebd0a9 2019-02-22 stsp * Ask the log thread for required amount of commits
1435 08ebd0a9 2019-02-22 stsp * plus some amount of pre-fetching.
1437 08ebd0a9 2019-02-22 stsp (*commits_needed) += maxscroll + 20;
1438 5e224a3e 2019-02-22 stsp err = trigger_log_thread(0, commits_needed, log_complete,
1439 5e224a3e 2019-02-22 stsp need_commits);
1441 5e224a3e 2019-02-22 stsp return err;
1445 7226d972 2019-02-21 stsp pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1446 00ba99a7 2019-05-12 jcs if (pentry == NULL)
1449 93e45b7c 2018-09-24 stsp *last_displayed_entry = pentry;
1451 dd0a52c1 2018-05-20 stsp pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1452 dd0a52c1 2018-05-20 stsp if (pentry == NULL)
1454 aa075928 2018-05-10 stsp *first_displayed_entry = pentry;
1455 16482c3b 2018-05-20 stsp } while (++nscrolled < maxscroll);
1457 dd0a52c1 2018-05-20 stsp return err;
1460 cd0acaa7 2018-05-20 stsp static const struct got_error *
1461 0cf4efb1 2018-09-29 stsp open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1462 fb872ab2 2019-02-21 stsp struct got_commit_object *commit, struct got_object_id *commit_id,
1463 8b473291 2019-02-21 stsp struct tog_view *log_view, struct got_reflist_head *refs,
1464 8b473291 2019-02-21 stsp struct got_repository *repo)
1466 cd0acaa7 2018-05-20 stsp const struct got_error *err;
1467 9ba79e04 2018-06-11 stsp struct got_object_qid *parent_id;
1468 e5a0f69f 2018-08-18 stsp struct tog_view *diff_view;
1470 669b5ffa 2018-10-07 stsp diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1471 15a94983 2018-12-23 stsp if (diff_view == NULL)
1472 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1474 4acef5ee 2018-12-24 stsp parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1475 4acef5ee 2018-12-24 stsp err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1476 8b473291 2019-02-21 stsp commit_id, log_view, refs, repo);
1477 e5a0f69f 2018-08-18 stsp if (err == NULL)
1478 e5a0f69f 2018-08-18 stsp *new_view = diff_view;
1479 cd0acaa7 2018-05-20 stsp return err;
1482 80ddbec8 2018-04-29 stsp static const struct got_error *
1483 941e9f74 2019-05-21 stsp tree_view_visit_subtree(struct got_tree_object *subtree,
1484 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s)
1486 941e9f74 2019-05-21 stsp struct tog_parent_tree *parent;
1488 941e9f74 2019-05-21 stsp parent = calloc(1, sizeof(*parent));
1489 941e9f74 2019-05-21 stsp if (parent == NULL)
1490 941e9f74 2019-05-21 stsp return got_error_from_errno("calloc");
1492 941e9f74 2019-05-21 stsp parent->tree = s->tree;
1493 941e9f74 2019-05-21 stsp parent->first_displayed_entry = s->first_displayed_entry;
1494 941e9f74 2019-05-21 stsp parent->selected_entry = s->selected_entry;
1495 941e9f74 2019-05-21 stsp parent->selected = s->selected;
1496 941e9f74 2019-05-21 stsp TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1497 941e9f74 2019-05-21 stsp s->tree = subtree;
1498 941e9f74 2019-05-21 stsp s->entries = got_object_tree_get_entries(s->tree);
1499 941e9f74 2019-05-21 stsp s->selected = 0;
1500 941e9f74 2019-05-21 stsp s->first_displayed_entry = NULL;
1501 941e9f74 2019-05-21 stsp return NULL;
1505 941e9f74 2019-05-21 stsp static const struct got_error *
1506 941e9f74 2019-05-21 stsp browse_commit_tree(struct tog_view **new_view, int begin_x,
1507 941e9f74 2019-05-21 stsp struct commit_queue_entry *entry, const char *path,
1508 941e9f74 2019-05-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
1510 9343a5fb 2018-06-23 stsp const struct got_error *err = NULL;
1511 9343a5fb 2018-06-23 stsp struct got_tree_object *tree;
1512 941e9f74 2019-05-21 stsp struct got_tree_entry *te;
1513 941e9f74 2019-05-21 stsp struct tog_tree_view_state *s;
1514 e5a0f69f 2018-08-18 stsp struct tog_view *tree_view;
1515 b03c880f 2019-05-21 stsp char *slash, *subpath = NULL;
1516 941e9f74 2019-05-21 stsp const char *p;
1518 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree, repo,
1519 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(entry->commit));
1521 9343a5fb 2018-06-23 stsp return err;
1523 669b5ffa 2018-10-07 stsp tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1524 e5a0f69f 2018-08-18 stsp if (tree_view == NULL)
1525 638f9024 2019-05-13 stsp return got_error_from_errno("view_open");
1527 8b473291 2019-02-21 stsp err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1528 941e9f74 2019-05-21 stsp if (err) {
1529 e5a0f69f 2018-08-18 stsp got_object_tree_close(tree);
1530 941e9f74 2019-05-21 stsp return err;
1532 941e9f74 2019-05-21 stsp s = &tree_view->state.tree;
1534 941e9f74 2019-05-21 stsp *new_view = tree_view;
1536 941e9f74 2019-05-21 stsp /* Walk the path and open corresponding tree objects. */
1538 941e9f74 2019-05-21 stsp while (*p) {
1539 941e9f74 2019-05-21 stsp struct got_object_id *tree_id;
1541 941e9f74 2019-05-21 stsp while (p[0] == '/')
1544 941e9f74 2019-05-21 stsp /* Ensure the correct subtree entry is selected. */
1545 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1546 941e9f74 2019-05-21 stsp if (slash == NULL)
1547 941e9f74 2019-05-21 stsp slash = strchr(p, '\0');
1548 941e9f74 2019-05-21 stsp SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1549 941e9f74 2019-05-21 stsp if (strncmp(p, te->name, slash - p) == 0) {
1550 941e9f74 2019-05-21 stsp s->selected_entry = te;
1553 b03c880f 2019-05-21 stsp s->selected++;
1555 941e9f74 2019-05-21 stsp if (s->selected_entry == NULL) {
1556 941e9f74 2019-05-21 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1559 b03c880f 2019-05-21 stsp if (s->tree != s->root)
1560 b03c880f 2019-05-21 stsp s->selected++; /* skip '..' */
1562 67409a31 2019-05-24 stsp if (!S_ISDIR(s->selected_entry->mode)) {
1563 67409a31 2019-05-24 stsp /* Jump to this file's entry. */
1564 67409a31 2019-05-24 stsp s->first_displayed_entry = s->selected_entry;
1565 67409a31 2019-05-24 stsp s->selected = 0;
1569 941e9f74 2019-05-21 stsp slash = strchr(p, '/');
1570 941e9f74 2019-05-21 stsp if (slash)
1571 941e9f74 2019-05-21 stsp subpath = strndup(path, slash - path);
1573 941e9f74 2019-05-21 stsp subpath = strdup(path);
1574 941e9f74 2019-05-21 stsp if (subpath == NULL) {
1575 941e9f74 2019-05-21 stsp err = got_error_from_errno("strdup");
1579 941e9f74 2019-05-21 stsp err = got_object_id_by_path(&tree_id, repo, entry->id,
1584 941e9f74 2019-05-21 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1585 941e9f74 2019-05-21 stsp free(tree_id);
1589 941e9f74 2019-05-21 stsp err = tree_view_visit_subtree(tree, s);
1590 941e9f74 2019-05-21 stsp if (err) {
1591 941e9f74 2019-05-21 stsp got_object_tree_close(tree);
1594 941e9f74 2019-05-21 stsp if (slash == NULL)
1596 941e9f74 2019-05-21 stsp free(subpath);
1597 941e9f74 2019-05-21 stsp subpath = NULL;
1598 941e9f74 2019-05-21 stsp p = slash;
1601 941e9f74 2019-05-21 stsp free(subpath);
1602 1a76625f 2018-10-22 stsp return err;
1605 1a76625f 2018-10-22 stsp static void *
1606 1a76625f 2018-10-22 stsp log_thread(void *arg)
1608 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1609 1a76625f 2018-10-22 stsp int errcode = 0;
1610 1a76625f 2018-10-22 stsp struct tog_log_thread_args *a = arg;
1611 1a76625f 2018-10-22 stsp int done = 0;
1613 1a76625f 2018-10-22 stsp err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1615 1a76625f 2018-10-22 stsp return (void *)err;
1617 1a76625f 2018-10-22 stsp while (!done && !err) {
1618 1a76625f 2018-10-22 stsp err = queue_commits(a->graph, a->commits, 1, a->repo,
1619 1a76625f 2018-10-22 stsp a->in_repo_path);
1620 1a76625f 2018-10-22 stsp if (err) {
1621 1a76625f 2018-10-22 stsp if (err->code != GOT_ERR_ITER_COMPLETED)
1622 1a76625f 2018-10-22 stsp return (void *)err;
1623 1a76625f 2018-10-22 stsp err = NULL;
1625 1a76625f 2018-10-22 stsp } else if (a->commits_needed > 0)
1626 1a76625f 2018-10-22 stsp a->commits_needed--;
1628 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1629 3abe8080 2019-04-10 stsp if (errcode) {
1630 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1631 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1633 3abe8080 2019-04-10 stsp } else if (*a->quit)
1635 3abe8080 2019-04-10 stsp else if (*a->first_displayed_entry == NULL) {
1636 1a76625f 2018-10-22 stsp *a->first_displayed_entry =
1637 1a76625f 2018-10-22 stsp TAILQ_FIRST(&a->commits->head);
1638 1a76625f 2018-10-22 stsp *a->selected_entry = *a->first_displayed_entry;
1642 1a76625f 2018-10-22 stsp a->commits_needed = 0;
1643 1a76625f 2018-10-22 stsp else if (a->commits_needed == 0) {
1644 1a76625f 2018-10-22 stsp errcode = pthread_cond_wait(&a->need_commits,
1645 1a76625f 2018-10-22 stsp &tog_mutex);
1646 1a76625f 2018-10-22 stsp if (errcode)
1647 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1648 2af4a041 2019-05-11 jcs "pthread_cond_wait");
1651 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1652 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1653 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode,
1654 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1656 3abe8080 2019-04-10 stsp a->log_complete = 1;
1657 1a76625f 2018-10-22 stsp return (void *)err;
1660 1a76625f 2018-10-22 stsp static const struct got_error *
1661 1a76625f 2018-10-22 stsp stop_log_thread(struct tog_log_view_state *s)
1663 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1664 1a76625f 2018-10-22 stsp int errcode;
1666 1a76625f 2018-10-22 stsp if (s->thread) {
1667 1a76625f 2018-10-22 stsp s->quit = 1;
1668 1a76625f 2018-10-22 stsp errcode = pthread_cond_signal(&s->thread_args.need_commits);
1669 1a76625f 2018-10-22 stsp if (errcode)
1670 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1671 2af4a041 2019-05-11 jcs "pthread_cond_signal");
1672 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
1673 1a76625f 2018-10-22 stsp if (errcode)
1674 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1675 2af4a041 2019-05-11 jcs "pthread_mutex_unlock");
1676 1a76625f 2018-10-22 stsp errcode = pthread_join(s->thread, (void **)&err);
1677 1a76625f 2018-10-22 stsp if (errcode)
1678 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_join");
1679 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
1680 1a76625f 2018-10-22 stsp if (errcode)
1681 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode,
1682 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
1683 1a76625f 2018-10-22 stsp s->thread = NULL;
1686 1a76625f 2018-10-22 stsp errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1687 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
1688 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_destroy");
1690 1a76625f 2018-10-22 stsp if (s->thread_args.repo) {
1691 1a76625f 2018-10-22 stsp got_repo_close(s->thread_args.repo);
1692 1a76625f 2018-10-22 stsp s->thread_args.repo = NULL;
1695 1a76625f 2018-10-22 stsp if (s->thread_args.graph) {
1696 1a76625f 2018-10-22 stsp got_commit_graph_close(s->thread_args.graph);
1697 1a76625f 2018-10-22 stsp s->thread_args.graph = NULL;
1700 9343a5fb 2018-06-23 stsp return err;
1703 9343a5fb 2018-06-23 stsp static const struct got_error *
1704 1a76625f 2018-10-22 stsp close_log_view(struct tog_view *view)
1706 1a76625f 2018-10-22 stsp const struct got_error *err = NULL;
1707 1a76625f 2018-10-22 stsp struct tog_log_view_state *s = &view->state.log;
1709 1a76625f 2018-10-22 stsp err = stop_log_thread(s);
1710 1a76625f 2018-10-22 stsp free_commits(&s->commits);
1711 1a76625f 2018-10-22 stsp free(s->in_repo_path);
1712 797bc7b9 2018-10-22 stsp s->in_repo_path = NULL;
1713 1a76625f 2018-10-22 stsp free(s->start_id);
1714 797bc7b9 2018-10-22 stsp s->start_id = NULL;
1715 1a76625f 2018-10-22 stsp return err;
1718 1a76625f 2018-10-22 stsp static const struct got_error *
1719 60493ae3 2019-06-20 stsp search_start_log_view(struct tog_view *view)
1721 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1723 60493ae3 2019-06-20 stsp s->matched_entry = NULL;
1724 96e2b566 2019-07-08 stsp s->search_entry = NULL;
1725 60493ae3 2019-06-20 stsp return NULL;
1728 60493ae3 2019-06-20 stsp static int
1729 df0b3d8a 2019-06-28 stsp match_commit(struct got_commit_object *commit, const char *id_str,
1730 df0b3d8a 2019-06-28 stsp regex_t *regex)
1732 60493ae3 2019-06-20 stsp regmatch_t regmatch;
1734 60493ae3 2019-06-20 stsp if (regexec(regex, got_object_commit_get_author(commit), 1,
1735 60493ae3 2019-06-20 stsp ®match, 0) == 0 ||
1736 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_committer(commit), 1,
1737 60493ae3 2019-06-20 stsp ®match, 0) == 0 ||
1738 60493ae3 2019-06-20 stsp regexec(regex, got_object_commit_get_logmsg(commit), 1,
1739 df0b3d8a 2019-06-28 stsp ®match, 0) == 0 ||
1740 df0b3d8a 2019-06-28 stsp regexec(regex, id_str, 1, ®match, 0) == 0)
1746 60493ae3 2019-06-20 stsp static const struct got_error *
1747 60493ae3 2019-06-20 stsp search_next_log_view(struct tog_view *view)
1749 60493ae3 2019-06-20 stsp const struct got_error *err = NULL;
1750 60493ae3 2019-06-20 stsp struct tog_log_view_state *s = &view->state.log;
1751 60493ae3 2019-06-20 stsp struct commit_queue_entry *entry;
1753 60493ae3 2019-06-20 stsp if (!view->searching) {
1754 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1755 60493ae3 2019-06-20 stsp return NULL;
1758 96e2b566 2019-07-08 stsp if (s->search_entry) {
1759 678cbce5 2019-07-28 stsp if (wgetch(view->window) == KEY_BACKSPACE) {
1760 678cbce5 2019-07-28 stsp view->search_next_done = 1;
1761 678cbce5 2019-07-28 stsp return NULL;
1763 96e2b566 2019-07-08 stsp if (view->searching == TOG_SEARCH_FORWARD)
1764 96e2b566 2019-07-08 stsp entry = TAILQ_NEXT(s->search_entry, entry);
1766 96e2b566 2019-07-08 stsp entry = TAILQ_PREV(s->search_entry,
1767 96e2b566 2019-07-08 stsp commit_queue_head, entry);
1768 96e2b566 2019-07-08 stsp } else if (s->matched_entry) {
1769 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1770 b55df7bc 2019-06-21 stsp entry = TAILQ_NEXT(s->selected_entry, entry);
1772 b55df7bc 2019-06-21 stsp entry = TAILQ_PREV(s->selected_entry,
1773 b1bf1435 2019-06-21 stsp commit_queue_head, entry);
1775 20be8d96 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1776 20be8d96 2019-06-21 stsp entry = TAILQ_FIRST(&s->commits.head);
1778 20be8d96 2019-06-21 stsp entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1781 60493ae3 2019-06-20 stsp while (1) {
1782 df0b3d8a 2019-06-28 stsp char *id_str;
1783 60493ae3 2019-06-20 stsp if (entry == NULL) {
1784 f801134a 2019-06-25 stsp if (s->thread_args.log_complete ||
1785 f801134a 2019-06-25 stsp view->searching == TOG_SEARCH_BACKWARD) {
1786 f801134a 2019-06-25 stsp view->search_next_done = 1;
1787 f801134a 2019-06-25 stsp return NULL;
1790 96e2b566 2019-07-08 stsp * Poke the log thread for more commits and return,
1791 96e2b566 2019-07-08 stsp * allowing the main loop to make progress. Search
1792 96e2b566 2019-07-08 stsp * will resume at s->search_entry once we come back.
1794 57b33b64 2019-07-08 stsp s->thread_args.commits_needed++;
1795 57b33b64 2019-07-08 stsp return trigger_log_thread(1,
1796 f801134a 2019-06-25 stsp &s->thread_args.commits_needed,
1797 f801134a 2019-06-25 stsp &s->thread_args.log_complete,
1798 f801134a 2019-06-25 stsp &s->thread_args.need_commits);
1801 df0b3d8a 2019-06-28 stsp err = got_object_id_str(&id_str, entry->id);
1803 df0b3d8a 2019-06-28 stsp return err;
1805 df0b3d8a 2019-06-28 stsp if (match_commit(entry->commit, id_str, &view->regex)) {
1806 60493ae3 2019-06-20 stsp view->search_next_done = 1;
1807 bcf2df4d 2019-06-21 stsp s->matched_entry = entry;
1808 df0b3d8a 2019-06-28 stsp free(id_str);
1811 df0b3d8a 2019-06-28 stsp free(id_str);
1812 96e2b566 2019-07-08 stsp s->search_entry = entry;
1813 b1bf1435 2019-06-21 stsp if (view->searching == TOG_SEARCH_FORWARD)
1814 b1bf1435 2019-06-21 stsp entry = TAILQ_NEXT(entry, entry);
1816 b1bf1435 2019-06-21 stsp entry = TAILQ_PREV(entry, commit_queue_head, entry);
1819 bcf2df4d 2019-06-21 stsp if (s->matched_entry) {
1820 ead14cbe 2019-06-21 stsp int cur = s->selected_entry->idx;
1821 ead14cbe 2019-06-21 stsp while (cur < s->matched_entry->idx) {
1822 60493ae3 2019-06-20 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1824 60493ae3 2019-06-20 stsp return err;
1827 ead14cbe 2019-06-21 stsp while (cur > s->matched_entry->idx) {
1828 ead14cbe 2019-06-21 stsp err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1830 60493ae3 2019-06-20 stsp return err;
1835 96e2b566 2019-07-08 stsp s->search_entry = NULL;
1837 60493ae3 2019-06-20 stsp return NULL;
1840 60493ae3 2019-06-20 stsp static const struct got_error *
1841 ba4f502b 2018-08-04 stsp open_log_view(struct tog_view *view, struct got_object_id *start_id,
1842 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo,
1843 d01904d4 2019-06-25 stsp const char *head_ref_name, const char *path, int check_disk)
1845 80ddbec8 2018-04-29 stsp const struct got_error *err = NULL;
1846 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1847 1a76625f 2018-10-22 stsp struct got_repository *thread_repo = NULL;
1848 1a76625f 2018-10-22 stsp struct got_commit_graph *thread_graph = NULL;
1849 1a76625f 2018-10-22 stsp int errcode;
1851 23721109 2018-10-22 stsp err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1852 ecb28ae0 2018-07-16 stsp if (err != NULL)
1853 ecb28ae0 2018-07-16 stsp goto done;
1855 93e45b7c 2018-09-24 stsp /* The commit queue only contains commits being displayed. */
1856 fb2756b9 2018-08-04 stsp TAILQ_INIT(&s->commits.head);
1857 fb2756b9 2018-08-04 stsp s->commits.ncommits = 0;
1859 8b473291 2019-02-21 stsp s->refs = refs;
1860 fb2756b9 2018-08-04 stsp s->repo = repo;
1861 d01904d4 2019-06-25 stsp s->head_ref_name = head_ref_name;
1862 5036bf37 2018-09-24 stsp s->start_id = got_object_id_dup(start_id);
1863 5036bf37 2018-09-24 stsp if (s->start_id == NULL) {
1864 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1865 5036bf37 2018-09-24 stsp goto done;
1868 e5a0f69f 2018-08-18 stsp view->show = show_log_view;
1869 e5a0f69f 2018-08-18 stsp view->input = input_log_view;
1870 e5a0f69f 2018-08-18 stsp view->close = close_log_view;
1871 60493ae3 2019-06-20 stsp view->search_start = search_start_log_view;
1872 60493ae3 2019-06-20 stsp view->search_next = search_next_log_view;
1874 1a76625f 2018-10-22 stsp err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1876 1a76625f 2018-10-22 stsp goto done;
1877 1a76625f 2018-10-22 stsp err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1878 1a76625f 2018-10-22 stsp 0, thread_repo);
1880 1a76625f 2018-10-22 stsp goto done;
1882 1a76625f 2018-10-22 stsp errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1883 1a76625f 2018-10-22 stsp if (errcode) {
1884 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_cond_init");
1885 1a76625f 2018-10-22 stsp goto done;
1888 1a76625f 2018-10-22 stsp s->thread_args.commits_needed = view->nlines;
1889 1a76625f 2018-10-22 stsp s->thread_args.graph = thread_graph;
1890 1a76625f 2018-10-22 stsp s->thread_args.commits = &s->commits;
1891 1a76625f 2018-10-22 stsp s->thread_args.in_repo_path = s->in_repo_path;
1892 1a76625f 2018-10-22 stsp s->thread_args.start_id = s->start_id;
1893 1a76625f 2018-10-22 stsp s->thread_args.repo = thread_repo;
1894 1a76625f 2018-10-22 stsp s->thread_args.log_complete = 0;
1895 1a76625f 2018-10-22 stsp s->thread_args.quit = &s->quit;
1896 1a76625f 2018-10-22 stsp s->thread_args.view = view;
1897 1a76625f 2018-10-22 stsp s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1898 1a76625f 2018-10-22 stsp s->thread_args.selected_entry = &s->selected_entry;
1901 1a76625f 2018-10-22 stsp close_log_view(view);
1902 ba4f502b 2018-08-04 stsp return err;
1905 e5a0f69f 2018-08-18 stsp static const struct got_error *
1906 ba4f502b 2018-08-04 stsp show_log_view(struct tog_view *view)
1908 fb2756b9 2018-08-04 stsp struct tog_log_view_state *s = &view->state.log;
1910 2b380cc8 2018-10-24 stsp if (s->thread == NULL) {
1911 2b380cc8 2018-10-24 stsp int errcode = pthread_create(&s->thread, NULL, log_thread,
1912 2b380cc8 2018-10-24 stsp &s->thread_args);
1913 2b380cc8 2018-10-24 stsp if (errcode)
1914 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_create");
1917 0cf4efb1 2018-09-29 stsp return draw_commits(view, &s->last_displayed_entry,
1918 e5a0f69f 2018-08-18 stsp &s->selected_entry, s->first_displayed_entry,
1919 8b473291 2019-02-21 stsp &s->commits, s->selected, view->nlines, s->refs,
1920 1a76625f 2018-10-22 stsp s->in_repo_path, s->thread_args.commits_needed);
1923 e5a0f69f 2018-08-18 stsp static const struct got_error *
1924 e5a0f69f 2018-08-18 stsp input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1925 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
1927 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
1928 e5a0f69f 2018-08-18 stsp struct tog_log_view_state *s = &view->state.log;
1929 d01904d4 2019-06-25 stsp char *parent_path, *in_repo_path = NULL;
1930 d01904d4 2019-06-25 stsp struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1931 669b5ffa 2018-10-07 stsp int begin_x = 0;
1932 d01904d4 2019-06-25 stsp struct got_object_id *start_id;
1934 e5a0f69f 2018-08-18 stsp switch (ch) {
1936 1e37a5c2 2019-05-12 jcs s->quit = 1;
1939 1e37a5c2 2019-05-12 jcs case KEY_UP:
1942 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1944 1e37a5c2 2019-05-12 jcs if (s->selected > 0)
1945 1e37a5c2 2019-05-12 jcs s->selected--;
1947 1144d21a 2019-06-21 stsp scroll_up(view, &s->first_displayed_entry, 1,
1948 1144d21a 2019-06-21 stsp &s->commits);
1950 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
1951 a4292ac5 2019-05-12 jcs case CTRL('b'):
1952 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1954 1e37a5c2 2019-05-12 jcs if (TAILQ_FIRST(&s->commits.head) ==
1955 1e37a5c2 2019-05-12 jcs s->first_displayed_entry) {
1956 1e37a5c2 2019-05-12 jcs s->selected = 0;
1959 1e37a5c2 2019-05-12 jcs scroll_up(view, &s->first_displayed_entry,
1960 1e37a5c2 2019-05-12 jcs view->nlines, &s->commits);
1963 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
1966 1e37a5c2 2019-05-12 jcs if (s->first_displayed_entry == NULL)
1968 1e37a5c2 2019-05-12 jcs if (s->selected < MIN(view->nlines - 2,
1969 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1970 1e37a5c2 2019-05-12 jcs s->selected++;
1973 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry, 1,
1974 1e37a5c2 2019-05-12 jcs &s->last_displayed_entry, &s->commits,
1975 1e37a5c2 2019-05-12 jcs &s->thread_args.log_complete,
1976 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1977 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1979 a4292ac5 2019-05-12 jcs case KEY_NPAGE:
1980 a4292ac5 2019-05-12 jcs case CTRL('f'): {
1981 1e37a5c2 2019-05-12 jcs struct commit_queue_entry *first;
1982 1e37a5c2 2019-05-12 jcs first = s->first_displayed_entry;
1983 1e37a5c2 2019-05-12 jcs if (first == NULL)
1985 1e37a5c2 2019-05-12 jcs err = scroll_down(view, &s->first_displayed_entry,
1986 1e37a5c2 2019-05-12 jcs view->nlines, &s->last_displayed_entry,
1987 1e37a5c2 2019-05-12 jcs &s->commits, &s->thread_args.log_complete,
1988 1e37a5c2 2019-05-12 jcs &s->thread_args.commits_needed,
1989 1e37a5c2 2019-05-12 jcs &s->thread_args.need_commits);
1990 1e37a5c2 2019-05-12 jcs if (first == s->first_displayed_entry &&
1991 1e37a5c2 2019-05-12 jcs s->selected < MIN(view->nlines - 2,
1992 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1)) {
1993 1e37a5c2 2019-05-12 jcs /* can't scroll further down */
1994 1e37a5c2 2019-05-12 jcs s->selected = MIN(view->nlines - 2,
1995 1e37a5c2 2019-05-12 jcs s->commits.ncommits - 1);
1997 1e37a5c2 2019-05-12 jcs err = NULL;
2000 1e37a5c2 2019-05-12 jcs case KEY_RESIZE:
2001 1e37a5c2 2019-05-12 jcs if (s->selected > view->nlines - 2)
2002 1e37a5c2 2019-05-12 jcs s->selected = view->nlines - 2;
2003 1e37a5c2 2019-05-12 jcs if (s->selected > s->commits.ncommits - 1)
2004 1e37a5c2 2019-05-12 jcs s->selected = s->commits.ncommits - 1;
2006 1e37a5c2 2019-05-12 jcs case KEY_ENTER:
2009 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2011 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2012 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2013 1e37a5c2 2019-05-12 jcs err = open_diff_view_for_commit(&diff_view, begin_x,
2014 1e37a5c2 2019-05-12 jcs s->selected_entry->commit, s->selected_entry->id,
2015 1e37a5c2 2019-05-12 jcs view, s->refs, s->repo);
2018 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2019 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2021 1e37a5c2 2019-05-12 jcs return err;
2022 1e37a5c2 2019-05-12 jcs err = view_set_child(view, diff_view);
2024 1e37a5c2 2019-05-12 jcs view_close(diff_view);
2027 1e37a5c2 2019-05-12 jcs *focus_view = diff_view;
2028 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2030 1e37a5c2 2019-05-12 jcs *new_view = diff_view;
2033 1e37a5c2 2019-05-12 jcs if (s->selected_entry == NULL)
2035 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2036 1e37a5c2 2019-05-12 jcs begin_x = view_split_begin_x(view->begin_x);
2037 941e9f74 2019-05-21 stsp err = browse_commit_tree(&tree_view, begin_x,
2038 941e9f74 2019-05-21 stsp s->selected_entry, s->in_repo_path, s->refs, s->repo);
2041 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view)) {
2042 1e37a5c2 2019-05-12 jcs err = view_close_child(view);
2044 1e37a5c2 2019-05-12 jcs return err;
2045 1e37a5c2 2019-05-12 jcs err = view_set_child(view, tree_view);
2047 1e37a5c2 2019-05-12 jcs view_close(tree_view);
2050 1e37a5c2 2019-05-12 jcs *focus_view = tree_view;
2051 1e37a5c2 2019-05-12 jcs view->child_focussed = 1;
2053 1e37a5c2 2019-05-12 jcs *new_view = tree_view;
2055 1e37a5c2 2019-05-12 jcs case KEY_BACKSPACE:
2056 1e37a5c2 2019-05-12 jcs if (strcmp(s->in_repo_path, "/") == 0)
2058 1e37a5c2 2019-05-12 jcs parent_path = dirname(s->in_repo_path);
2059 1e37a5c2 2019-05-12 jcs if (parent_path && strcmp(parent_path, ".") != 0) {
2060 1e37a5c2 2019-05-12 jcs err = stop_log_thread(s);
2062 1e37a5c2 2019-05-12 jcs return err;
2063 1e37a5c2 2019-05-12 jcs lv = view_open(view->nlines, view->ncols,
2064 1e37a5c2 2019-05-12 jcs view->begin_y, view->begin_x, TOG_VIEW_LOG);
2065 1e37a5c2 2019-05-12 jcs if (lv == NULL)
2066 638f9024 2019-05-13 stsp return got_error_from_errno(
2067 1e37a5c2 2019-05-12 jcs "view_open");
2068 1e37a5c2 2019-05-12 jcs err = open_log_view(lv, s->start_id, s->refs,
2069 d01904d4 2019-06-25 stsp s->repo, s->head_ref_name, parent_path, 0);
2071 1e37a5c2 2019-05-12 jcs return err;;
2072 1e37a5c2 2019-05-12 jcs if (view_is_parent_view(view))
2073 1e37a5c2 2019-05-12 jcs *new_view = lv;
2075 1e37a5c2 2019-05-12 jcs view_set_child(view->parent, lv);
2076 1e37a5c2 2019-05-12 jcs *focus_view = lv;
2078 1e37a5c2 2019-05-12 jcs return NULL;
2081 e3d2a5c6 2019-06-26 stsp case CTRL('l'):
2082 d01904d4 2019-06-25 stsp err = stop_log_thread(s);
2084 d01904d4 2019-06-25 stsp return err;
2085 d01904d4 2019-06-25 stsp lv = view_open(view->nlines, view->ncols,
2086 d01904d4 2019-06-25 stsp view->begin_y, view->begin_x, TOG_VIEW_LOG);
2087 d01904d4 2019-06-25 stsp if (lv == NULL)
2088 d01904d4 2019-06-25 stsp return got_error_from_errno("view_open");
2089 d01904d4 2019-06-25 stsp err = get_head_commit_id(&start_id, s->head_ref_name ?
2090 d01904d4 2019-06-25 stsp s->head_ref_name : GOT_REF_HEAD, s->repo);
2091 ef129c5e 2019-08-03 stsp if (err) {
2092 ef129c5e 2019-08-03 stsp view_close(lv);
2093 d01904d4 2019-06-25 stsp return err;
2095 d01904d4 2019-06-25 stsp in_repo_path = strdup(s->in_repo_path);
2096 d01904d4 2019-06-25 stsp if (in_repo_path == NULL) {
2097 d01904d4 2019-06-25 stsp free(start_id);
2098 ef129c5e 2019-08-03 stsp view_close(lv);
2099 d01904d4 2019-06-25 stsp return got_error_from_errno("strdup");
2101 d01904d4 2019-06-25 stsp err = open_log_view(lv, start_id, s->refs, s->repo,
2102 d01904d4 2019-06-25 stsp s->head_ref_name, in_repo_path, 0);
2103 ef129c5e 2019-08-03 stsp if (err) {
2104 ef129c5e 2019-08-03 stsp free(start_id);
2105 ef129c5e 2019-08-03 stsp view_close(lv);
2106 d01904d4 2019-06-25 stsp return err;;
2108 d01904d4 2019-06-25 stsp *dead_view = view;
2109 d01904d4 2019-06-25 stsp *new_view = lv;
2115 80ddbec8 2018-04-29 stsp return err;
2118 4ed7e80c 2018-05-20 stsp static const struct got_error *
2119 c2db6724 2019-01-04 stsp apply_unveil(const char *repo_path, const char *worktree_path)
2121 c2db6724 2019-01-04 stsp const struct got_error *error;
2123 37c06ea4 2019-07-15 stsp #ifdef PROFILE
2124 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
2125 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
2127 c2db6724 2019-01-04 stsp if (repo_path && unveil(repo_path, "r") != 0)
2128 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
2130 c2db6724 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
2131 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
2133 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
2134 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
2136 c2db6724 2019-01-04 stsp error = got_privsep_unveil_exec_helpers();
2137 c2db6724 2019-01-04 stsp if (error != NULL)
2138 c2db6724 2019-01-04 stsp return error;
2140 c2db6724 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
2141 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
2143 c2db6724 2019-01-04 stsp return NULL;
2146 a915003a 2019-02-05 stsp static void
2147 a915003a 2019-02-05 stsp init_curses(void)
2149 a915003a 2019-02-05 stsp initscr();
2151 a915003a 2019-02-05 stsp halfdelay(1); /* Do fast refresh while initial view is loading. */
2154 a915003a 2019-02-05 stsp intrflush(stdscr, FALSE);
2155 a915003a 2019-02-05 stsp keypad(stdscr, TRUE);
2156 a915003a 2019-02-05 stsp curs_set(0);
2157 a915003a 2019-02-05 stsp signal(SIGWINCH, tog_sigwinch);
2160 c2db6724 2019-01-04 stsp static const struct got_error *
2161 9f7d7167 2018-04-29 stsp cmd_log(int argc, char *argv[])
2163 80ddbec8 2018-04-29 stsp const struct got_error *error;
2164 ecb28ae0 2018-07-16 stsp struct got_repository *repo = NULL;
2165 ec142235 2019-03-07 stsp struct got_worktree *worktree = NULL;
2166 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2167 899d86c2 2018-05-10 stsp struct got_object_id *start_id = NULL;
2168 ecb28ae0 2018-07-16 stsp char *path = NULL, *repo_path = NULL, *cwd = NULL;
2169 80ddbec8 2018-04-29 stsp char *start_commit = NULL;
2171 04cc582a 2018-08-01 stsp struct tog_view *view;
2173 a55555f2 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2175 80ddbec8 2018-04-29 stsp #ifndef PROFILE
2176 c2db6724 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2177 c2db6724 2019-01-04 stsp NULL) == -1)
2178 80ddbec8 2018-04-29 stsp err(1, "pledge");
2181 ecb28ae0 2018-07-16 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2182 80ddbec8 2018-04-29 stsp switch (ch) {
2184 80ddbec8 2018-04-29 stsp start_commit = optarg;
2187 ecb28ae0 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2188 ecb28ae0 2018-07-16 stsp if (repo_path == NULL)
2189 ecb28ae0 2018-07-16 stsp err(1, "-r option");
2192 17020d27 2019-03-07 stsp usage_log();
2193 80ddbec8 2018-04-29 stsp /* NOTREACHED */
2197 80ddbec8 2018-04-29 stsp argc -= optind;
2198 80ddbec8 2018-04-29 stsp argv += optind;
2200 ecb28ae0 2018-07-16 stsp cwd = getcwd(NULL, 0);
2201 ecb28ae0 2018-07-16 stsp if (cwd == NULL) {
2202 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2203 ecb28ae0 2018-07-16 stsp goto done;
2205 963f97a1 2019-03-18 stsp error = got_worktree_open(&worktree, cwd);
2206 963f97a1 2019-03-18 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2207 963f97a1 2019-03-18 stsp goto done;
2208 963f97a1 2019-03-18 stsp error = NULL;
2210 963f97a1 2019-03-18 stsp if (argc == 0) {
2211 963f97a1 2019-03-18 stsp path = strdup("");
2212 963f97a1 2019-03-18 stsp if (path == NULL) {
2213 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2214 ecb28ae0 2018-07-16 stsp goto done;
2216 963f97a1 2019-03-18 stsp } else if (argc == 1) {
2217 963f97a1 2019-03-18 stsp if (worktree) {
2218 963f97a1 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2220 963f97a1 2019-03-18 stsp if (error)
2221 963f97a1 2019-03-18 stsp goto done;
2223 963f97a1 2019-03-18 stsp path = strdup(argv[0]);
2224 963f97a1 2019-03-18 stsp if (path == NULL) {
2225 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2226 963f97a1 2019-03-18 stsp goto done;
2230 963f97a1 2019-03-18 stsp usage_log();
2232 963f97a1 2019-03-18 stsp repo_path = worktree ?
2233 963f97a1 2019-03-18 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2234 963f97a1 2019-03-18 stsp if (repo_path == NULL) {
2235 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2236 963f97a1 2019-03-18 stsp goto done;
2239 a915003a 2019-02-05 stsp init_curses();
2241 80ddbec8 2018-04-29 stsp error = got_repo_open(&repo, repo_path);
2242 80ddbec8 2018-04-29 stsp if (error != NULL)
2243 ecb28ae0 2018-07-16 stsp goto done;
2245 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo),
2246 c02c541e 2019-03-29 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2247 c02c541e 2019-03-29 stsp if (error)
2248 c02c541e 2019-03-29 stsp goto done;
2250 15a94983 2018-12-23 stsp if (start_commit == NULL)
2251 19e70ad6 2019-05-14 stsp error = get_head_commit_id(&start_id, worktree ?
2252 19e70ad6 2019-05-14 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2255 f2b6a97d 2019-07-15 stsp error = get_head_commit_id(&start_id, start_commit, repo);
2256 f2b6a97d 2019-07-15 stsp if (error) {
2257 f2b6a97d 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
2258 f2b6a97d 2019-07-15 stsp goto done;
2259 f2b6a97d 2019-07-15 stsp error = got_repo_match_object_id_prefix(&start_id,
2260 f2b6a97d 2019-07-15 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2263 80ddbec8 2018-04-29 stsp if (error != NULL)
2264 8b473291 2019-02-21 stsp goto done;
2266 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2267 8b473291 2019-02-21 stsp if (error)
2268 ecb28ae0 2018-07-16 stsp goto done;
2270 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2271 04cc582a 2018-08-01 stsp if (view == NULL) {
2272 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2273 04cc582a 2018-08-01 stsp goto done;
2275 d01904d4 2019-06-25 stsp error = open_log_view(view, start_id, &refs, repo, worktree ?
2276 d01904d4 2019-06-25 stsp got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2277 ba4f502b 2018-08-04 stsp if (error)
2278 ba4f502b 2018-08-04 stsp goto done;
2279 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2281 ecb28ae0 2018-07-16 stsp free(repo_path);
2282 ecb28ae0 2018-07-16 stsp free(cwd);
2283 ecb28ae0 2018-07-16 stsp free(path);
2284 899d86c2 2018-05-10 stsp free(start_id);
2286 ecb28ae0 2018-07-16 stsp got_repo_close(repo);
2287 ec142235 2019-03-07 stsp if (worktree)
2288 ec142235 2019-03-07 stsp got_worktree_close(worktree);
2289 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2290 80ddbec8 2018-04-29 stsp return error;
2293 4ed7e80c 2018-05-20 stsp __dead static void
2294 9f7d7167 2018-04-29 stsp usage_diff(void)
2297 9f7d7167 2018-04-29 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2298 9f7d7167 2018-04-29 stsp getprogname());
2302 b304db33 2018-05-20 stsp static char *
2303 b304db33 2018-05-20 stsp parse_next_line(FILE *f, size_t *len)
2305 b304db33 2018-05-20 stsp char *line;
2306 b304db33 2018-05-20 stsp size_t linelen;
2307 b304db33 2018-05-20 stsp size_t lineno;
2308 b304db33 2018-05-20 stsp const char delim[3] = { '\0', '\0', '\0'};
2310 b304db33 2018-05-20 stsp line = fparseln(f, &linelen, &lineno, delim, 0);
2312 b304db33 2018-05-20 stsp *len = linelen;
2313 b304db33 2018-05-20 stsp return line;
2316 4ed7e80c 2018-05-20 stsp static const struct got_error *
2317 f7d12f7e 2018-08-01 stsp draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2318 a3404814 2018-09-02 stsp int *last_displayed_line, int *eof, int max_lines,
2319 c3e9aa98 2019-05-13 jcs char *header)
2321 61e69b96 2018-05-20 stsp const struct got_error *err;
2322 26ed57b2 2018-05-19 stsp int nlines = 0, nprinted = 0;
2323 b304db33 2018-05-20 stsp char *line;
2324 b304db33 2018-05-20 stsp size_t len;
2325 61e69b96 2018-05-20 stsp wchar_t *wline;
2326 e0b650dd 2018-05-20 stsp int width;
2328 26ed57b2 2018-05-19 stsp rewind(f);
2329 f7d12f7e 2018-08-01 stsp werase(view->window);
2331 a3404814 2018-09-02 stsp if (header) {
2332 a3404814 2018-09-02 stsp err = format_line(&wline, &width, header, view->ncols);
2333 a3404814 2018-09-02 stsp if (err) {
2334 a3404814 2018-09-02 stsp return err;
2337 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2338 a3404814 2018-09-02 stsp wstandout(view->window);
2339 a3404814 2018-09-02 stsp waddwstr(view->window, wline);
2340 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2341 a3404814 2018-09-02 stsp wstandend(view->window);
2342 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2343 a3404814 2018-09-02 stsp waddch(view->window, '\n');
2345 a3404814 2018-09-02 stsp if (max_lines <= 1)
2346 a3404814 2018-09-02 stsp return NULL;
2347 a3404814 2018-09-02 stsp max_lines--;
2351 26ed57b2 2018-05-19 stsp while (nprinted < max_lines) {
2352 b304db33 2018-05-20 stsp line = parse_next_line(f, &len);
2353 26ed57b2 2018-05-19 stsp if (line == NULL) {
2357 26ed57b2 2018-05-19 stsp if (++nlines < *first_displayed_line) {
2358 26ed57b2 2018-05-19 stsp free(line);
2362 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2363 61e69b96 2018-05-20 stsp if (err) {
2364 61e69b96 2018-05-20 stsp free(line);
2365 61e69b96 2018-05-20 stsp return err;
2367 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2368 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2369 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2370 26ed57b2 2018-05-19 stsp if (++nprinted == 1)
2371 26ed57b2 2018-05-19 stsp *first_displayed_line = nlines;
2372 26ed57b2 2018-05-19 stsp free(line);
2373 2550e4c3 2018-07-13 stsp free(wline);
2374 2550e4c3 2018-07-13 stsp wline = NULL;
2376 26ed57b2 2018-05-19 stsp *last_displayed_line = nlines;
2378 1a57306a 2018-09-02 stsp view_vborder(view);
2380 c3e9aa98 2019-05-13 jcs if (*eof) {
2381 c3e9aa98 2019-05-13 jcs while (nprinted < view->nlines) {
2382 c3e9aa98 2019-05-13 jcs waddch(view->window, '\n');
2383 c3e9aa98 2019-05-13 jcs nprinted++;
2386 c3e9aa98 2019-05-13 jcs err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2388 c3e9aa98 2019-05-13 jcs return err;
2391 c3e9aa98 2019-05-13 jcs wstandout(view->window);
2392 c3e9aa98 2019-05-13 jcs waddwstr(view->window, wline);
2393 c3e9aa98 2019-05-13 jcs wstandend(view->window);
2396 26ed57b2 2018-05-19 stsp return NULL;
2399 abd2672a 2018-12-23 stsp static char *
2400 abd2672a 2018-12-23 stsp get_datestr(time_t *time, char *datebuf)
2402 abd2672a 2018-12-23 stsp char *p, *s = ctime_r(time, datebuf);
2403 abd2672a 2018-12-23 stsp p = strchr(s, '\n');
2405 abd2672a 2018-12-23 stsp *p = '\0';
2409 4ed7e80c 2018-05-20 stsp static const struct got_error *
2410 8b473291 2019-02-21 stsp write_commit_info(struct got_object_id *commit_id,
2411 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2413 abd2672a 2018-12-23 stsp const struct got_error *err = NULL;
2414 abd2672a 2018-12-23 stsp char datebuf[26];
2415 15a94983 2018-12-23 stsp struct got_commit_object *commit;
2416 15a94983 2018-12-23 stsp char *id_str = NULL;
2417 45d799e2 2018-12-23 stsp time_t committer_time;
2418 45d799e2 2018-12-23 stsp const char *author, *committer;
2419 8b473291 2019-02-21 stsp char *refs_str = NULL;
2421 8b473291 2019-02-21 stsp if (refs) {
2422 8b473291 2019-02-21 stsp err = build_refs_str(&refs_str, refs, commit_id);
2424 8b473291 2019-02-21 stsp return err;
2427 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
2429 abd2672a 2018-12-23 stsp return err;
2431 15a94983 2018-12-23 stsp err = got_object_id_str(&id_str, commit_id);
2432 15a94983 2018-12-23 stsp if (err) {
2433 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_str");
2434 15a94983 2018-12-23 stsp goto done;
2437 8b473291 2019-02-21 stsp if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2438 8b473291 2019-02-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2439 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2440 abd2672a 2018-12-23 stsp goto done;
2442 45d799e2 2018-12-23 stsp if (fprintf(outfile, "from: %s\n",
2443 45d799e2 2018-12-23 stsp got_object_commit_get_author(commit)) < 0) {
2444 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2445 abd2672a 2018-12-23 stsp goto done;
2447 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2448 abd2672a 2018-12-23 stsp if (fprintf(outfile, "date: %s UTC\n",
2449 45d799e2 2018-12-23 stsp get_datestr(&committer_time, datebuf)) < 0) {
2450 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2451 abd2672a 2018-12-23 stsp goto done;
2453 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2454 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2455 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0 &&
2456 45d799e2 2018-12-23 stsp fprintf(outfile, "via: %s\n", committer) < 0) {
2457 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2458 abd2672a 2018-12-23 stsp goto done;
2460 45d799e2 2018-12-23 stsp if (fprintf(outfile, "%s\n",
2461 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(commit)) < 0) {
2462 638f9024 2019-05-13 stsp err = got_error_from_errno("fprintf");
2463 abd2672a 2018-12-23 stsp goto done;
2466 abd2672a 2018-12-23 stsp free(id_str);
2467 8b473291 2019-02-21 stsp free(refs_str);
2468 15a94983 2018-12-23 stsp got_object_commit_close(commit);
2469 abd2672a 2018-12-23 stsp return err;
2472 abd2672a 2018-12-23 stsp static const struct got_error *
2473 48ae06ee 2018-10-18 stsp create_diff(struct tog_diff_view_state *s)
2475 48ae06ee 2018-10-18 stsp const struct got_error *err = NULL;
2476 48ae06ee 2018-10-18 stsp FILE *f = NULL;
2477 15a94983 2018-12-23 stsp int obj_type;
2479 511a516b 2018-05-19 stsp f = got_opentemp();
2480 48ae06ee 2018-10-18 stsp if (f == NULL) {
2481 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
2482 48ae06ee 2018-10-18 stsp goto done;
2484 fb43ecf1 2019-02-11 stsp if (s->f && fclose(s->f) != 0) {
2485 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2486 fb43ecf1 2019-02-11 stsp goto done;
2490 15a94983 2018-12-23 stsp if (s->id1)
2491 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id1);
2493 15a94983 2018-12-23 stsp err = got_object_get_type(&obj_type, s->repo, s->id2);
2495 15a94983 2018-12-23 stsp goto done;
2497 15a94983 2018-12-23 stsp switch (obj_type) {
2498 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_BLOB:
2499 15a94983 2018-12-23 stsp err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2500 54156555 2018-12-24 stsp s->diff_context, s->repo, f);
2502 26ed57b2 2018-05-19 stsp case GOT_OBJ_TYPE_TREE:
2503 54156555 2018-12-24 stsp err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2504 48ae06ee 2018-10-18 stsp s->diff_context, s->repo, f);
2506 abd2672a 2018-12-23 stsp case GOT_OBJ_TYPE_COMMIT: {
2507 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2508 abd2672a 2018-12-23 stsp struct got_object_qid *pid;
2509 abd2672a 2018-12-23 stsp struct got_commit_object *commit2;
2511 15a94983 2018-12-23 stsp err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2514 15a087fe 2019-02-21 stsp /* Show commit info if we're diffing to a parent/root commit. */
2515 15a087fe 2019-02-21 stsp if (s->id1 == NULL)
2516 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs, s->repo, f);
2518 15a087fe 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(commit2);
2519 15a087fe 2019-02-21 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2520 15a087fe 2019-02-21 stsp if (got_object_id_cmp(s->id1, pid->id) == 0) {
2521 8b473291 2019-02-21 stsp write_commit_info(s->id2, s->refs,
2522 8b473291 2019-02-21 stsp s->repo, f);
2527 abd2672a 2018-12-23 stsp got_object_commit_close(commit2);
2529 15a94983 2018-12-23 stsp err = got_diff_objects_as_commits(s->id1, s->id2,
2530 15a94983 2018-12-23 stsp s->diff_context, s->repo, f);
2534 48ae06ee 2018-10-18 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2538 cbe7f848 2019-02-11 stsp if (f && fflush(f) != 0 && err == NULL)
2539 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2540 48ae06ee 2018-10-18 stsp return err;
2543 f5215bb9 2019-02-22 stsp static void
2544 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(struct tog_view *view)
2546 baf4288f 2019-05-13 stsp mvwaddstr(view->window, 0, 0, "diffing...");
2547 f5215bb9 2019-02-22 stsp update_panels();
2548 f5215bb9 2019-02-22 stsp doupdate();
2551 48ae06ee 2018-10-18 stsp static const struct got_error *
2552 15a94983 2018-12-23 stsp open_diff_view(struct tog_view *view, struct got_object_id *id1,
2553 fb872ab2 2019-02-21 stsp struct got_object_id *id2, struct tog_view *log_view,
2554 8b473291 2019-02-21 stsp struct got_reflist_head *refs, struct got_repository *repo)
2556 48ae06ee 2018-10-18 stsp const struct got_error *err;
2558 15a94983 2018-12-23 stsp if (id1 != NULL && id2 != NULL) {
2559 15a94983 2018-12-23 stsp int type1, type2;
2560 15a94983 2018-12-23 stsp err = got_object_get_type(&type1, repo, id1);
2562 15a94983 2018-12-23 stsp return err;
2563 15a94983 2018-12-23 stsp err = got_object_get_type(&type2, repo, id2);
2565 15a94983 2018-12-23 stsp return err;
2567 15a94983 2018-12-23 stsp if (type1 != type2)
2568 48ae06ee 2018-10-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
2571 15a94983 2018-12-23 stsp if (id1) {
2572 15a94983 2018-12-23 stsp view->state.diff.id1 = got_object_id_dup(id1);
2573 15a94983 2018-12-23 stsp if (view->state.diff.id1 == NULL)
2574 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2576 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2578 15a94983 2018-12-23 stsp view->state.diff.id2 = got_object_id_dup(id2);
2579 48ae06ee 2018-10-18 stsp if (view->state.diff.id2 == NULL) {
2580 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2581 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2582 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2584 48ae06ee 2018-10-18 stsp view->state.diff.f = NULL;
2585 5dc9f4bc 2018-08-04 stsp view->state.diff.first_displayed_line = 1;
2586 5dc9f4bc 2018-08-04 stsp view->state.diff.last_displayed_line = view->nlines;
2587 48ae06ee 2018-10-18 stsp view->state.diff.diff_context = 3;
2588 fb872ab2 2019-02-21 stsp view->state.diff.log_view = log_view;
2589 48ae06ee 2018-10-18 stsp view->state.diff.repo = repo;
2590 8b473291 2019-02-21 stsp view->state.diff.refs = refs;
2592 f5215bb9 2019-02-22 stsp if (log_view && view_is_splitscreen(view))
2593 f5215bb9 2019-02-22 stsp show_log_view(log_view); /* draw vborder */
2594 f5215bb9 2019-02-22 stsp diff_view_indicate_progress(view);
2596 48ae06ee 2018-10-18 stsp err = create_diff(&view->state.diff);
2597 48ae06ee 2018-10-18 stsp if (err) {
2598 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2599 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2600 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2601 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2602 48ae06ee 2018-10-18 stsp return err;
2605 e5a0f69f 2018-08-18 stsp view->show = show_diff_view;
2606 e5a0f69f 2018-08-18 stsp view->input = input_diff_view;
2607 e5a0f69f 2018-08-18 stsp view->close = close_diff_view;
2609 5dc9f4bc 2018-08-04 stsp return NULL;
2612 e5a0f69f 2018-08-18 stsp static const struct got_error *
2613 5dc9f4bc 2018-08-04 stsp close_diff_view(struct tog_view *view)
2615 e5a0f69f 2018-08-18 stsp const struct got_error *err = NULL;
2617 48ae06ee 2018-10-18 stsp free(view->state.diff.id1);
2618 48ae06ee 2018-10-18 stsp view->state.diff.id1 = NULL;
2619 48ae06ee 2018-10-18 stsp free(view->state.diff.id2);
2620 48ae06ee 2018-10-18 stsp view->state.diff.id2 = NULL;
2621 e5a0f69f 2018-08-18 stsp if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2622 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2623 e5a0f69f 2018-08-18 stsp return err;
2626 5dc9f4bc 2018-08-04 stsp static const struct got_error *
2627 5dc9f4bc 2018-08-04 stsp show_diff_view(struct tog_view *view)
2629 a3404814 2018-09-02 stsp const struct got_error *err;
2630 fb2756b9 2018-08-04 stsp struct tog_diff_view_state *s = &view->state.diff;
2631 a3404814 2018-09-02 stsp char *id_str1 = NULL, *id_str2, *header;
2633 a3404814 2018-09-02 stsp if (s->id1) {
2634 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str1, s->id1);
2636 a3404814 2018-09-02 stsp return err;
2638 a3404814 2018-09-02 stsp err = got_object_id_str(&id_str2, s->id2);
2640 a3404814 2018-09-02 stsp return err;
2642 56765ebb 2018-12-23 stsp if (asprintf(&header, "diff %s %s",
2643 a3404814 2018-09-02 stsp id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2644 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2645 a3404814 2018-09-02 stsp free(id_str1);
2646 a3404814 2018-09-02 stsp free(id_str2);
2647 a3404814 2018-09-02 stsp return err;
2649 a3404814 2018-09-02 stsp free(id_str1);
2650 a3404814 2018-09-02 stsp free(id_str2);
2652 e5a0f69f 2018-08-18 stsp return draw_file(view, s->f, &s->first_displayed_line,
2653 a3404814 2018-09-02 stsp &s->last_displayed_line, &s->eof, view->nlines,
2657 15a087fe 2019-02-21 stsp static const struct got_error *
2658 15a087fe 2019-02-21 stsp set_selected_commit(struct tog_diff_view_state *s,
2659 15a087fe 2019-02-21 stsp struct commit_queue_entry *entry)
2661 d7a04538 2019-02-21 stsp const struct got_error *err;
2662 d7a04538 2019-02-21 stsp const struct got_object_id_queue *parent_ids;
2663 d7a04538 2019-02-21 stsp struct got_commit_object *selected_commit;
2664 d7a04538 2019-02-21 stsp struct got_object_qid *pid;
2666 15a087fe 2019-02-21 stsp free(s->id2);
2667 15a087fe 2019-02-21 stsp s->id2 = got_object_id_dup(entry->id);
2668 15a087fe 2019-02-21 stsp if (s->id2 == NULL)
2669 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
2671 d7a04538 2019-02-21 stsp err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2673 d7a04538 2019-02-21 stsp return err;
2674 d7a04538 2019-02-21 stsp parent_ids = got_object_commit_get_parent_ids(selected_commit);
2675 15a087fe 2019-02-21 stsp free(s->id1);
2676 d7a04538 2019-02-21 stsp pid = SIMPLEQ_FIRST(parent_ids);
2677 d7a04538 2019-02-21 stsp s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2678 0311546b 2019-02-21 stsp got_object_commit_close(selected_commit);
2679 15a087fe 2019-02-21 stsp return NULL;
2682 0cf4efb1 2018-09-29 stsp static const struct got_error *
2683 bcbd79e2 2018-08-19 stsp input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2684 878940b7 2018-09-29 stsp struct tog_view **focus_view, struct tog_view *view, int ch)
2686 bcbd79e2 2018-08-19 stsp const struct got_error *err = NULL;
2687 e5a0f69f 2018-08-18 stsp struct tog_diff_view_state *s = &view->state.diff;
2688 fb872ab2 2019-02-21 stsp struct tog_log_view_state *ls;
2689 fb872ab2 2019-02-21 stsp struct commit_queue_entry *entry;
2692 e5a0f69f 2018-08-18 stsp switch (ch) {
2694 1e37a5c2 2019-05-12 jcs case KEY_UP:
2695 1e37a5c2 2019-05-12 jcs if (s->first_displayed_line > 1)
2696 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2698 1e37a5c2 2019-05-12 jcs case KEY_PPAGE:
2699 a60a9dc4 2019-05-13 jcs case CTRL('b'):
2700 00ba99a7 2019-05-12 jcs if (s->first_displayed_line == 1)
2703 1e37a5c2 2019-05-12 jcs while (i++ < view->nlines - 1 &&
2704 1e37a5c2 2019-05-12 jcs s->first_displayed_line > 1)
2705 1e37a5c2 2019-05-12 jcs s->first_displayed_line--;
2708 1e37a5c2 2019-05-12 jcs case KEY_DOWN:
2709 1e37a5c2 2019-05-12 jcs if (!s->eof)
2710 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2712 1e37a5c2 2019-05-12 jcs case KEY_NPAGE:
2713 a60a9dc4 2019-05-13 jcs case CTRL('f'):
2715 00ba99a7 2019-05-12 jcs if (s->eof)
2718 1e37a5c2 2019-05-12 jcs while (!s->eof && i++ < view->nlines - 1) {
2719 1e37a5c2 2019-05-12 jcs char *line;
2720 1e37a5c2 2019-05-12 jcs line = parse_next_line(s->f, NULL);
2721 1e37a5c2 2019-05-12 jcs s->first_displayed_line++;
2722 1e37a5c2 2019-05-12 jcs if (line == NULL)
2727 1e37a5c2 2019-05-12 jcs if (s->diff_context > 0) {
2728 1e37a5c2 2019-05-12 jcs s->diff_context--;
2729 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2730 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2734 1e37a5c2 2019-05-12 jcs if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2735 1e37a5c2 2019-05-12 jcs s->diff_context++;
2736 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2737 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2742 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2744 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2745 1e37a5c2 2019-05-12 jcs entry = TAILQ_PREV(ls->selected_entry,
2746 1e37a5c2 2019-05-12 jcs commit_queue_head, entry);
2747 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2750 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2755 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2759 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2760 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2762 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2763 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2767 1e37a5c2 2019-05-12 jcs if (s->log_view == NULL)
2769 1e37a5c2 2019-05-12 jcs ls = &s->log_view->state.log;
2771 1e37a5c2 2019-05-12 jcs if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2772 1e37a5c2 2019-05-12 jcs ls->thread_args.commits_needed++;
2774 1e37a5c2 2019-05-12 jcs /* Display "loading..." in log view. */
2775 1e37a5c2 2019-05-12 jcs show_log_view(s->log_view);
2776 1e37a5c2 2019-05-12 jcs update_panels();
2777 1e37a5c2 2019-05-12 jcs doupdate();
2779 1e37a5c2 2019-05-12 jcs err = trigger_log_thread(1 /* load_all */,
2780 1e37a5c2 2019-05-12 jcs &ls->thread_args.commits_needed,
2781 1e37a5c2 2019-05-12 jcs &ls->thread_args.log_complete,
2782 1e37a5c2 2019-05-12 jcs &ls->thread_args.need_commits);
2786 1e37a5c2 2019-05-12 jcs err = input_log_view(NULL, NULL, NULL, s->log_view,
2791 1e37a5c2 2019-05-12 jcs entry = TAILQ_NEXT(ls->selected_entry, entry);
2792 1e37a5c2 2019-05-12 jcs if (entry == NULL)
2795 1e37a5c2 2019-05-12 jcs err = set_selected_commit(s, entry);
2799 1e37a5c2 2019-05-12 jcs s->first_displayed_line = 1;
2800 1e37a5c2 2019-05-12 jcs s->last_displayed_line = view->nlines;
2802 1e37a5c2 2019-05-12 jcs diff_view_indicate_progress(view);
2803 1e37a5c2 2019-05-12 jcs err = create_diff(s);
2809 bcbd79e2 2018-08-19 stsp return err;
2812 4ed7e80c 2018-05-20 stsp static const struct got_error *
2813 9f7d7167 2018-04-29 stsp cmd_diff(int argc, char *argv[])
2815 26ed57b2 2018-05-19 stsp const struct got_error *error = NULL;
2816 26ed57b2 2018-05-19 stsp struct got_repository *repo = NULL;
2817 8b473291 2019-02-21 stsp struct got_reflist_head refs;
2818 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2819 26ed57b2 2018-05-19 stsp char *repo_path = NULL;
2820 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
2822 ea5e7bb5 2018-08-01 stsp struct tog_view *view;
2824 70ac5f84 2019-03-28 stsp SIMPLEQ_INIT(&refs);
2826 26ed57b2 2018-05-19 stsp #ifndef PROFILE
2827 eb6600df 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2828 eb6600df 2019-01-04 stsp NULL) == -1)
2829 26ed57b2 2018-05-19 stsp err(1, "pledge");
2832 26ed57b2 2018-05-19 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2833 26ed57b2 2018-05-19 stsp switch (ch) {
2835 17020d27 2019-03-07 stsp usage_diff();
2836 26ed57b2 2018-05-19 stsp /* NOTREACHED */
2840 26ed57b2 2018-05-19 stsp argc -= optind;
2841 26ed57b2 2018-05-19 stsp argv += optind;
2843 26ed57b2 2018-05-19 stsp if (argc == 0) {
2844 26ed57b2 2018-05-19 stsp usage_diff(); /* TODO show local worktree changes */
2845 26ed57b2 2018-05-19 stsp } else if (argc == 2) {
2846 26ed57b2 2018-05-19 stsp repo_path = getcwd(NULL, 0);
2847 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2848 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2849 15a94983 2018-12-23 stsp id_str1 = argv[0];
2850 15a94983 2018-12-23 stsp id_str2 = argv[1];
2851 26ed57b2 2018-05-19 stsp } else if (argc == 3) {
2852 26ed57b2 2018-05-19 stsp repo_path = realpath(argv[0], NULL);
2853 26ed57b2 2018-05-19 stsp if (repo_path == NULL)
2854 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2855 15a94983 2018-12-23 stsp id_str1 = argv[1];
2856 15a94983 2018-12-23 stsp id_str2 = argv[2];
2858 26ed57b2 2018-05-19 stsp usage_diff();
2860 a915003a 2019-02-05 stsp init_curses();
2862 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2863 eb6600df 2019-01-04 stsp if (error)
2864 eb6600df 2019-01-04 stsp goto done;
2866 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), NULL);
2867 26ed57b2 2018-05-19 stsp if (error)
2868 26ed57b2 2018-05-19 stsp goto done;
2870 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
2871 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
2872 26ed57b2 2018-05-19 stsp if (error)
2873 26ed57b2 2018-05-19 stsp goto done;
2875 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
2876 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
2877 26ed57b2 2018-05-19 stsp if (error)
2878 26ed57b2 2018-05-19 stsp goto done;
2880 8b473291 2019-02-21 stsp error = got_ref_list(&refs, repo);
2881 8b473291 2019-02-21 stsp if (error)
2882 8b473291 2019-02-21 stsp goto done;
2884 0cf4efb1 2018-09-29 stsp view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2885 ea5e7bb5 2018-08-01 stsp if (view == NULL) {
2886 638f9024 2019-05-13 stsp error = got_error_from_errno("view_open");
2887 ea5e7bb5 2018-08-01 stsp goto done;
2889 8b473291 2019-02-21 stsp error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2890 5dc9f4bc 2018-08-04 stsp if (error)
2891 5dc9f4bc 2018-08-04 stsp goto done;
2892 e5a0f69f 2018-08-18 stsp error = view_loop(view);
2894 c02c541e 2019-03-29 stsp free(repo_path);
2896 921be706 2019-06-28 stsp got_repo_close(repo);
2897 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2898 26ed57b2 2018-05-19 stsp return error;
2901 4ed7e80c 2018-05-20 stsp __dead static void
2902 9f7d7167 2018-04-29 stsp usage_blame(void)
2905 69069811 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2906 9f7d7167 2018-04-29 stsp getprogname());
2910 84451b3e 2018-07-10 stsp struct tog_blame_line {
2911 84451b3e 2018-07-10 stsp int annotated;
2912 84451b3e 2018-07-10 stsp struct got_object_id *id;
2915 4ed7e80c 2018-05-20 stsp static const struct got_error *
2916 f7d12f7e 2018-08-01 stsp draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2917 f7d12f7e 2018-08-01 stsp const char *path, struct tog_blame_line *lines, int nlines,
2918 f7d12f7e 2018-08-01 stsp int blame_complete, int selected_line, int *first_displayed_line,
2919 f7d12f7e 2018-08-01 stsp int *last_displayed_line, int *eof, int max_lines)
2921 84451b3e 2018-07-10 stsp const struct got_error *err;
2922 84451b3e 2018-07-10 stsp int lineno = 0, nprinted = 0;
2923 84451b3e 2018-07-10 stsp char *line;
2924 84451b3e 2018-07-10 stsp size_t len;
2925 84451b3e 2018-07-10 stsp wchar_t *wline;
2926 b700b5d6 2018-07-10 stsp int width, wlimit;
2927 84451b3e 2018-07-10 stsp struct tog_blame_line *blame_line;
2928 ee41ec32 2018-07-10 stsp struct got_object_id *prev_id = NULL;
2929 ab089a2a 2018-07-12 stsp char *id_str;
2931 ab089a2a 2018-07-12 stsp err = got_object_id_str(&id_str, id);
2933 ab089a2a 2018-07-12 stsp return err;
2935 84451b3e 2018-07-10 stsp rewind(f);
2936 f7d12f7e 2018-08-01 stsp werase(view->window);
2938 c1124f18 2018-12-23 stsp if (asprintf(&line, "commit %s", id_str) == -1) {
2939 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2940 ab089a2a 2018-07-12 stsp free(id_str);
2941 ab089a2a 2018-07-12 stsp return err;
2944 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2945 ab089a2a 2018-07-12 stsp free(line);
2946 2550e4c3 2018-07-13 stsp line = NULL;
2947 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2948 a3404814 2018-09-02 stsp wstandout(view->window);
2949 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2950 a3404814 2018-09-02 stsp if (view_needs_focus_indication(view))
2951 a3404814 2018-09-02 stsp wstandend(view->window);
2952 2550e4c3 2018-07-13 stsp free(wline);
2953 2550e4c3 2018-07-13 stsp wline = NULL;
2954 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2955 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2957 084063cd 2018-07-12 stsp if (asprintf(&line, "[%d/%d] %s%s",
2958 084063cd 2018-07-12 stsp *first_displayed_line - 1 + selected_line, nlines,
2959 512d0df1 2019-02-22 stsp blame_complete ? "" : "annotating... ", path) == -1) {
2960 ab089a2a 2018-07-12 stsp free(id_str);
2961 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2963 ab089a2a 2018-07-12 stsp free(id_str);
2964 f7d12f7e 2018-08-01 stsp err = format_line(&wline, &width, line, view->ncols);
2965 3f60a8ef 2018-07-10 stsp free(line);
2966 2550e4c3 2018-07-13 stsp line = NULL;
2968 3f60a8ef 2018-07-10 stsp return err;
2969 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
2970 2550e4c3 2018-07-13 stsp free(wline);
2971 2550e4c3 2018-07-13 stsp wline = NULL;
2972 30f8fd5e 2019-06-04 stsp if (width < view->ncols - 1)
2973 f7d12f7e 2018-08-01 stsp waddch(view->window, '\n');
2976 ab089a2a 2018-07-12 stsp while (nprinted < max_lines - 2) {
2977 84451b3e 2018-07-10 stsp line = parse_next_line(f, &len);
2978 84451b3e 2018-07-10 stsp if (line == NULL) {
2982 84451b3e 2018-07-10 stsp if (++lineno < *first_displayed_line) {
2983 84451b3e 2018-07-10 stsp free(line);
2987 f7d12f7e 2018-08-01 stsp wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2988 b700b5d6 2018-07-10 stsp err = format_line(&wline, &width, line, wlimit);
2989 84451b3e 2018-07-10 stsp if (err) {
2990 84451b3e 2018-07-10 stsp free(line);
2991 84451b3e 2018-07-10 stsp return err;
2994 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
2995 f7d12f7e 2018-08-01 stsp wstandout(view->window);
2997 84451b3e 2018-07-10 stsp blame_line = &lines[lineno - 1];
2998 ee41ec32 2018-07-10 stsp if (blame_line->annotated && prev_id &&
2999 ee41ec32 2018-07-10 stsp got_object_id_cmp(prev_id, blame_line->id) == 0)
3000 f7d12f7e 2018-08-01 stsp waddstr(view->window, " ");
3001 ee41ec32 2018-07-10 stsp else if (blame_line->annotated) {
3002 84451b3e 2018-07-10 stsp char *id_str;
3003 84451b3e 2018-07-10 stsp err = got_object_id_str(&id_str, blame_line->id);
3004 84451b3e 2018-07-10 stsp if (err) {
3005 84451b3e 2018-07-10 stsp free(line);
3006 2550e4c3 2018-07-13 stsp free(wline);
3007 84451b3e 2018-07-10 stsp return err;
3009 f7d12f7e 2018-08-01 stsp wprintw(view->window, "%.8s ", id_str);
3010 84451b3e 2018-07-10 stsp free(id_str);
3011 ee41ec32 2018-07-10 stsp prev_id = blame_line->id;
3013 f7d12f7e 2018-08-01 stsp waddstr(view->window, "........ ");
3014 ee41ec32 2018-07-10 stsp prev_id = NULL;
3017 f7d12f7e 2018-08-01 stsp waddwstr(view->window, wline);
3018 b700b5d6 2018-07-10 stsp while (width < wlimit) {
3019 f7d12f7e 2018-08-01 stsp waddch(view->window, ' ');
3022 0cf4efb1 2018-09-29 stsp if (view->focussed && nprinted == selected_line - 1)
3023 f7d12f7e 2018-08-01 stsp wstandend(view->window);
3024 84451b3e 2018-07-10 stsp if (++nprinted == 1)
3025 84451b3e 2018-07-10 stsp *first_displayed_line = lineno;
3026 84451b3e 2018-07-10 stsp free(line);
3027 2550e4c3 2018-07-13 stsp free(wline);
3028 2550e4c3 2018-07-13 stsp wline = NULL;
3030 84451b3e 2018-07-10 stsp *last_displayed_line = lineno;
3032 1a57306a 2018-09-02 stsp view_vborder(view);
3034 84451b3e 2018-07-10 stsp return NULL;
3037 84451b3e 2018-07-10 stsp static const struct got_error *
3038 84451b3e 2018-07-10 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3040 84451b3e 2018-07-10 stsp const struct got_error *err = NULL;
3041 84451b3e 2018-07-10 stsp struct tog_blame_cb_args *a = arg;
3042 84451b3e 2018-07-10 stsp struct tog_blame_line *line;
3043 1a76625f 2018-10-22 stsp int errcode;
3045 d68a0a7d 2018-07-10 stsp if (nlines != a->nlines ||
3046 d68a0a7d 2018-07-10 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3047 84451b3e 2018-07-10 stsp return got_error(GOT_ERR_RANGE);
3049 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3050 1a76625f 2018-10-22 stsp if (errcode)
3051 2af4a041 2019-05-11 jcs return got_error_set_errno(errcode, "pthread_mutex_lock");
3053 18430de3 2018-07-10 stsp if (*a->quit) { /* user has quit the blame view */
3054 d68a0a7d 2018-07-10 stsp err = got_error(GOT_ERR_ITER_COMPLETED);
3055 d68a0a7d 2018-07-10 stsp goto done;
3058 d68a0a7d 2018-07-10 stsp if (lineno == -1)
3059 d68a0a7d 2018-07-10 stsp goto done; /* no change in this commit */
3061 84451b3e 2018-07-10 stsp line = &a->lines[lineno - 1];
3062 d68a0a7d 2018-07-10 stsp if (line->annotated)
3063 d68a0a7d 2018-07-10 stsp goto done;
3065 84451b3e 2018-07-10 stsp line->id = got_object_id_dup(id);
3066 84451b3e 2018-07-10 stsp if (line->id == NULL) {
3067 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
3068 84451b3e 2018-07-10 stsp goto done;
3070 84451b3e 2018-07-10 stsp line->annotated = 1;
3072 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3073 1a76625f 2018-10-22 stsp if (errcode)
3074 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3075 84451b3e 2018-07-10 stsp return err;
3078 84451b3e 2018-07-10 stsp static void *
3079 84451b3e 2018-07-10 stsp blame_thread(void *arg)
3081 18430de3 2018-07-10 stsp const struct got_error *err;
3082 18430de3 2018-07-10 stsp struct tog_blame_thread_args *ta = arg;
3083 245d91c1 2018-07-12 stsp struct tog_blame_cb_args *a = ta->cb_args;
3084 1a76625f 2018-10-22 stsp int errcode;
3086 ab089a2a 2018-07-12 stsp err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3087 245d91c1 2018-07-12 stsp blame_cb, ta->cb_args);
3089 1a76625f 2018-10-22 stsp errcode = pthread_mutex_lock(&tog_mutex);
3090 1a76625f 2018-10-22 stsp if (errcode)
3091 2af4a041 2019-05-11 jcs return (void *)got_error_set_errno(errcode,
3092 2af4a041 2019-05-11 jcs "pthread_mutex_lock");
3094 c9beca56 2018-07-22 stsp got_repo_close(ta->repo);
3095 c9beca56 2018-07-22 stsp ta->repo = NULL;
3096 c9beca56 2018-07-22 stsp *ta->complete = 1;
3098 1a76625f 2018-10-22 stsp errcode = pthread_mutex_unlock(&tog_mutex);
3099 1a76625f 2018-10-22 stsp if (errcode && err == NULL)
3100 2af4a041 2019-05-11 jcs err = got_error_set_errno(errcode, "pthread_mutex_unlock");